-
Notifications
You must be signed in to change notification settings - Fork 1
129 lines (112 loc) · 4.51 KB
/
Copy pathissue-labels-migrate.yml
File metadata and controls
129 lines (112 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Migrate Legacy Labels
on:
workflow_dispatch:
permissions:
issues: write
jobs:
migrate:
runs-on: ubuntu-latest
steps:
- name: "Rename legacy labels to type: equivalents"
uses: actions/github-script@v8
with:
script: |
// Legacy label → canonical type: label.
// Rename preserves all issue associations automatically.
const MIGRATIONS = [
{ from: 'bug', to: 'type:bug' },
{ from: 'question', to: 'type:question' },
{ from: 'enhancement', to: 'type:enhancement' },
{ from: 'documentation', to: 'type:documentation' }
];
function isAlreadyExistsError(err) {
const errors = err?.errors
|| err?.response?.data?.errors
|| err?.data?.errors
|| [];
const hasTypedError = Array.isArray(errors)
&& errors.some(e => e?.code === 'already_exists' && e?.field === 'name');
const message = String(err?.message || '');
const messageHasAlreadyExists = message.includes('already_exists') && message.includes('Label');
return (err?.status === 422 && hasTypedError) || messageHasAlreadyExists;
}
async function relabelItems(from, to) {
const items = await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
labels: from,
per_page: 100
});
if (items.length === 0) {
core.info(`No issues or PRs found with legacy label "${from}"`);
return;
}
for (const item of items) {
const existing = (item.labels || [])
.map(l => (typeof l === 'string' ? l : l.name))
.filter(Boolean);
if (!existing.includes(to)) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: item.number,
labels: [to]
});
}
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: item.number,
name: from
});
} catch (err) {
if (err.status !== 404) {
throw err;
}
}
}
core.info(`Re-labeled ${items.length} issues/PRs: ${from} → ${to}`);
}
for (const { from, to } of MIGRATIONS) {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: from
});
// Old label exists — rename it
await github.rest.issues.updateLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: from,
new_name: to
});
core.info(`Migrated label: ${from} → ${to}`);
} catch (err) {
if (err.status === 404) {
core.info(`Legacy label "${from}" not found — no migration needed`);
} else if (isAlreadyExistsError(err)) {
core.info(`Target label "${to}" already exists; applying fallback migration for "${from}"`);
await relabelItems(from, to);
try {
await github.rest.issues.deleteLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: from
});
core.info(`Deleted legacy label "${from}" after fallback migration`);
} catch (deleteErr) {
if (deleteErr.status === 404) {
core.info(`Legacy label "${from}" already removed`);
} else {
throw deleteErr;
}
}
} else {
core.warning(`Failed to migrate ${from}: ${err.message}`);
}
}
}
core.info('Label migration complete');