chore: add sync labels workflow

This commit is contained in:
JaredLiu committed 2025-12-07 13:43:25 +08:00
1 parent 6e0a6e3b3b
commit 6034f1aaa1
2 files changed
+151

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
- name: approved
color: '#65e00d'
description: 'This prompt has been approved by the official authority.'
- name: prompt-submission
color: '#3f74ea'
description: 'This is a prompt submission.'
- name: bug
color: '#d73a4a'
description: "Something isn't working"
- name: documentation
color: '#0075ca'
description: 'Improvements or additions to documentation'
- name: duplicate
color: '#cfd3d7'
description: 'This issue or pull request already exists'
- name: enhancement
color: '#a2eeef'
description: 'New feature or request'
- name: good first issue
color: '#7057ff'
description: 'Good for newcomers'
- name: help wanted
color: '#008672'
description: 'Extra attention is needed'
- name: invalid
color: '#e99695'
description: "This doesn't seem right"
- name: question
color: '#d876e3'
description: 'Further information is requested'
- name: wontfix
color: '#ffffff'
description: 'This will not be worked on'
+107
View File
@@ -0,0 +1,107 @@
name: Sync Labels
on:
workflow_dispatch:
push:
branches:
- main
paths:
- '.github/labels.yml'
permissions:
issues: write
jobs:
sync-labels:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Sync labels
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
// Read labels.yml file
const labelsFile = fs.readFileSync('.github/labels.yml', 'utf8');
// Simple YAML parsing for array format
const lines = labelsFile.split('\n');
const labels = [];
let currentLabel = null;
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith('- name:')) {
if (currentLabel) labels.push(currentLabel);
const nameMatch = trimmed.match(/- name:\s*(.+)/);
if (nameMatch) {
currentLabel = {
name: nameMatch[1].trim().replace(/^['"]|['"]$/g, ''),
color: '',
description: ''
};
}
} else if (trimmed.startsWith('color:')) {
if (currentLabel) {
const colorMatch = trimmed.match(/color:\s*(.+)/);
if (colorMatch) {
currentLabel.color = colorMatch[1].trim().replace(/^['"]|['"]$/g, '');
}
}
} else if (trimmed.startsWith('description:')) {
if (currentLabel) {
const descMatch = trimmed.match(/description:\s*(.+)/);
if (descMatch) {
currentLabel.description = descMatch[1].trim().replace(/^['"]|['"]$/g, '');
}
}
}
}
if (currentLabel) labels.push(currentLabel);
// Get existing labels
const existingLabels = await github.rest.issues.listLabelsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
});
const existingLabelNames = new Set(existingLabels.data.map(label => label.name));
// Sync labels
for (const label of labels) {
const labelName = label.name;
const labelColor = label.color.replace('#', '');
const labelDescription = label.description || '';
try {
if (existingLabelNames.has(labelName)) {
// Update existing label
await github.rest.issues.updateLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName,
color: labelColor,
description: labelDescription,
});
console.log(`Updated label: ${labelName}`);
} else {
// Create new label
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName,
color: labelColor,
description: labelDescription,
});
console.log(`Created label: ${labelName}`);
}
} catch (error) {
console.error(`Error syncing label ${labelName}:`, error.message);
}
}
console.log('Labels sync completed!');