Skip to content

Commit 0ccefb1

Browse files
authored
Merge pull request #32 from rosspeili/fix/label-sync-update
fix(ci): update existing label colors via GitHub API
2 parents 81714fd + 2080b59 commit 0ccefb1

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

.github/LABELS.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ Colors are set in [labels.json](labels.json). Adjust in the GitHub UI if a label
153153

154154
**Automatic (after merge to `main`):**
155155

156-
1. Merge a PR that includes `.github/labels.json`.
157-
2. Workflow **Sync GitHub labels** runs on push, or run it manually: **Actions****Sync GitHub labels****Run workflow**.
156+
1. Merge a PR that includes `.github/labels.json` (or workflow changes).
157+
2. Workflow **Sync GitHub labels** runs on push, or run it manually: **Actions****Sync GitHub labels****Run workflow** → branch **`main`**.
158158

159-
**Manual (first time or without Actions):**
159+
The workflow uses the GitHub API to **create and update** labels (including **color** changes). Re-run after editing colors in `labels.json`.
160160

161-
1. Open **Issues****Labels****New label**.
162-
2. Create each entry from [labels.json](labels.json) (name, description, color).
161+
**Manual (without Actions):**
163162

164-
`delete-other-labels` is **false** — existing labels not listed in `labels.json` are kept.
163+
1. Open **Issues****Labels** → edit each label, or use `gh label edit <name> --color <hex> --description "..."`.
164+
165+
Labels not listed in `labels.json` are kept (nothing is deleted).
165166

166167
---
167168

.github/workflows/sync-labels.yml

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ on:
77
- main
88
paths:
99
- .github/labels.json
10+
- .github/workflows/sync-labels.yml
1011

1112
permissions:
1213
issues: write
14+
contents: read
1315

1416
jobs:
1517
sync:
@@ -18,9 +20,43 @@ jobs:
1820
- name: Checkout repository
1921
uses: actions/checkout@v4
2022

21-
- name: Sync labels from .github/labels.json
22-
uses: EndBug/label-sync@v2
23+
- name: Create or update labels from labels.json
24+
uses: actions/github-script@v7
2325
with:
24-
config-file: .github/labels.json
25-
delete-other-labels: false
26-
token: ${{ secrets.GITHUB_TOKEN }}
26+
script: |
27+
const fs = require('fs');
28+
const labels = JSON.parse(
29+
fs.readFileSync('.github/labels.json', 'utf8')
30+
);
31+
const { owner, repo } = context.repo;
32+
33+
for (const { name, color, description } of labels) {
34+
const colorHex = String(color).replace(/^#/, '');
35+
const desc = description || '';
36+
37+
try {
38+
await github.rest.issues.getLabel({ owner, repo, name });
39+
await github.rest.issues.updateLabel({
40+
owner,
41+
repo,
42+
name,
43+
color: colorHex,
44+
description: desc,
45+
});
46+
core.info(`Updated: ${name} (#${colorHex})`);
47+
} catch (error) {
48+
if (error.status === 404) {
49+
await github.rest.issues.createLabel({
50+
owner,
51+
repo,
52+
name,
53+
color: colorHex,
54+
description: desc,
55+
});
56+
core.info(`Created: ${name} (#${colorHex})`);
57+
} else {
58+
core.setFailed(`Failed on ${name}: ${error.message}`);
59+
throw error;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)