-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6a9ba3
commit 8ebf72c
Showing
1 changed file
with
36 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,77 @@ | ||
name: Sync + Delete GitHub Labels | ||
name: Copy GitHub Labels | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
sync-labels: | ||
copy-labels: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Sync labels from source to target repository | ||
- 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], | ||
}); | ||
console.log("Labels fetched: ", response.data); // Debug output | ||
const labels = response.data; | ||
if (labels.length === 0) { | ||
console.log("No labels found in the source repository."); // Debug output | ||
} | ||
// Fetch all labels from the target repository and delete them | ||
const existingLabels = await github.rest.issues.listLabelsForRepo({ | ||
owner: targetOwner, | ||
repo: targetRepo, | ||
}); | ||
// Delete old labels | ||
for (const label of existingLabels.data) { | ||
await github.rest.issues.deleteLabel({ | ||
owner: targetOwner, | ||
repo: targetRepo, | ||
name: label.name, | ||
}); | ||
} | ||
// Fetch labels from the source repository | ||
const sourceLabels = await github.rest.issues.listLabelsForRepo({ | ||
owner: sourceRepo.split('/')[0], | ||
repo: sourceRepo.split('/')[1], | ||
}); | ||
// Create labels in the target repository using the source labels | ||
for (const label of sourceLabels.data) { | ||
await github.rest.issues.createLabel({ | ||
owner: targetOwner, | ||
repo: targetRepo, | ||
name: label.name, | ||
color: label.color, | ||
description: label.description || '', | ||
}).catch(error => { | ||
// Loop through labels and create/update them in the target repository | ||
for (const label of labels) { | ||
try { | ||
// Try to create the label | ||
const createResponse = await github.rest.issues.createLabel({ | ||
owner: targetOwner, | ||
repo: targetRepo, | ||
name: label.name, | ||
color: label.color, | ||
description: label.description || '', | ||
}); | ||
console.log("Label created: ", createResponse.data); // Debug output | ||
} catch (error) { | ||
console.log("Error creating label: ", error.message); // Debug output | ||
// If label exists (error 422), update it | ||
if (error.status === 422) { | ||
await github.rest.issues.updateLabel({ | ||
const updateResponse = await github.rest.issues.updateLabel({ | ||
owner: targetOwner, | ||
repo: targetRepo, | ||
current_name: label.name, | ||
name: label.name, | ||
color: label.color, | ||
description: label.description || '', | ||
}); | ||
console.log("Label updated: ", updateResponse.data); // Debug output | ||
} | ||
}); | ||
} | ||
} |