|
25 | 25 | { from: 'documentation', to: 'type:documentation' } |
26 | 26 | ]; |
27 | 27 |
|
| 28 | + function isAlreadyExistsError(err) { |
| 29 | + return err?.status === 422 |
| 30 | + && Array.isArray(err?.errors) |
| 31 | + && err.errors.some(e => e.code === 'already_exists' && e.field === 'name'); |
| 32 | + } |
| 33 | +
|
| 34 | + async function relabelItems(from, to) { |
| 35 | + const items = await github.paginate(github.rest.issues.listForRepo, { |
| 36 | + owner: context.repo.owner, |
| 37 | + repo: context.repo.repo, |
| 38 | + state: 'all', |
| 39 | + labels: from, |
| 40 | + per_page: 100 |
| 41 | + }); |
| 42 | +
|
| 43 | + if (items.length === 0) { |
| 44 | + core.info(`No issues or PRs found with legacy label "${from}"`); |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + for (const item of items) { |
| 49 | + const existing = (item.labels || []) |
| 50 | + .map(l => (typeof l === 'string' ? l : l.name)) |
| 51 | + .filter(Boolean); |
| 52 | +
|
| 53 | + if (!existing.includes(to)) { |
| 54 | + await github.rest.issues.addLabels({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + issue_number: item.number, |
| 58 | + labels: [to] |
| 59 | + }); |
| 60 | + } |
| 61 | +
|
| 62 | + try { |
| 63 | + await github.rest.issues.removeLabel({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + issue_number: item.number, |
| 67 | + name: from |
| 68 | + }); |
| 69 | + } catch (err) { |
| 70 | + if (err.status !== 404) { |
| 71 | + throw err; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + core.info(`Re-labeled ${items.length} issues/PRs: ${from} → ${to}`); |
| 77 | + } |
| 78 | +
|
28 | 79 | for (const { from, to } of MIGRATIONS) { |
29 | 80 | try { |
30 | 81 | await github.rest.issues.getLabel({ |
|
43 | 94 | } catch (err) { |
44 | 95 | if (err.status === 404) { |
45 | 96 | core.info(`Legacy label "${from}" not found — no migration needed`); |
| 97 | + } else if (isAlreadyExistsError(err)) { |
| 98 | + core.info(`Target label "${to}" already exists; applying fallback migration for "${from}"`); |
| 99 | +
|
| 100 | + await relabelItems(from, to); |
| 101 | +
|
| 102 | + try { |
| 103 | + await github.rest.issues.deleteLabel({ |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + name: from |
| 107 | + }); |
| 108 | + core.info(`Deleted legacy label "${from}" after fallback migration`); |
| 109 | + } catch (deleteErr) { |
| 110 | + if (deleteErr.status === 404) { |
| 111 | + core.info(`Legacy label "${from}" already removed`); |
| 112 | + } else { |
| 113 | + throw deleteErr; |
| 114 | + } |
| 115 | + } |
46 | 116 | } else { |
47 | 117 | core.warning(`Failed to migrate ${from}: ${err.message}`); |
48 | 118 | } |
|
0 commit comments