Copy GitHub Labels #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Copy GitHub Labels | |
on: | |
workflow_dispatch: | |
jobs: | |
copy-labels: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Copy labels from another repository | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const sourceRepo = 'StephanAkkerman/fintwit-bot'; | |
const targetOwner = context.repo.owner; | |
const targetRepo = context.repo.repo; | |
// Fetch labels from the source repository | |
const response = await github.rest.issues.listLabelsForRepo({ | |
owner: sourceRepo.split('/')[0], | |
repo: sourceRepo.split('/')[1], | |
}); | |
const labels = response.data; | |
// Loop through labels and create/update them in the target repository | |
for (const label of labels) { | |
try { | |
// Try to create the label | |
await github.rest.issues.createLabel({ | |
owner: targetOwner, | |
repo: targetRepo, | |
name: label.name, | |
color: label.color, | |
description: label.description || '', | |
}); | |
} catch (error) { | |
// If label exists (error 422), update it | |
if (error.status === 422) { | |
await github.rest.issues.updateLabel({ | |
owner: targetOwner, | |
repo: targetRepo, | |
current_name: label.name, | |
name: label.name, | |
color: label.color, | |
description: label.description || '', | |
}); | |
} | |
} | |
} |