|
7 | 7 | - main |
8 | 8 | paths: |
9 | 9 | - .github/labels.json |
| 10 | + - .github/workflows/sync-labels.yml |
10 | 11 |
|
11 | 12 | permissions: |
12 | 13 | issues: write |
| 14 | + contents: read |
13 | 15 |
|
14 | 16 | jobs: |
15 | 17 | sync: |
|
18 | 20 | - name: Checkout repository |
19 | 21 | uses: actions/checkout@v4 |
20 | 22 |
|
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 |
23 | 25 | 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