Skip to content

Commit df053a2

Browse files
Copilotswissspidy
andcommitted
Add reusable workflow for managing repository labels
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 23797fb commit df053a2

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
name: Manage Labels
3+
4+
'on':
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- main
9+
- master
10+
paths:
11+
- 'composer.json'
12+
13+
jobs:
14+
manage-labels:
15+
uses: wp-cli/.github/.github/workflows/reusable-manage-labels.yml@main
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
name: Manage Repository Labels
3+
4+
'on':
5+
workflow_call:
6+
7+
permissions:
8+
issues: write
9+
contents: read
10+
11+
jobs:
12+
manage-labels:
13+
name: Create/Update Repository Labels
14+
runs-on: ubuntu-latest
15+
if: ${{ github.repository_owner == 'wp-cli' }}
16+
steps:
17+
- name: Check out source code
18+
uses: actions/checkout@v5
19+
20+
- name: Set up PHP environment
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: 'latest'
24+
env:
25+
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: Check existence of composer.json file
28+
id: check_composer_file
29+
uses: andstor/file-existence-action@v3
30+
with:
31+
files: "composer.json"
32+
33+
- name: Get commands from composer.json
34+
id: get-commands
35+
if: steps.check_composer_file.outputs.files_exists == 'true'
36+
run: |
37+
# Extract commands from composer.json using composer config
38+
COMMANDS=$(composer config extra.commands 2>/dev/null || echo "[]")
39+
echo "commands=${COMMANDS}" >> "$GITHUB_OUTPUT"
40+
echo "Commands found: ${COMMANDS}"
41+
42+
- name: Create/Update labels
43+
uses: actions/github-script@v7
44+
env:
45+
COMMANDS_JSON: ${{ steps.get-commands.outputs.commands }}
46+
with:
47+
script: |
48+
// Standard labels that should exist in every repository
49+
const standardLabels = [
50+
{ name: 'good-first-issue', color: '7057ff', description: 'Good for newcomers' },
51+
{ name: 'help-wanted', color: '008672', description: 'Extra attention is needed' },
52+
{ name: 'scope:documentation', color: 'FEF2C0', description: 'Related to documentation' },
53+
{ name: 'scope:testing', color: 'FEF2C0', description: 'Related to testing' }
54+
];
55+
56+
// Parse commands from composer.json
57+
const commandsEnv = process.env.COMMANDS_JSON || '[]';
58+
let commands = [];
59+
60+
try {
61+
commands = JSON.parse(commandsEnv);
62+
if (!Array.isArray(commands)) {
63+
commands = [];
64+
}
65+
} catch (e) {
66+
console.log('No commands found or invalid JSON format');
67+
commands = [];
68+
}
69+
70+
// Generate command-specific labels
71+
const commandLabels = commands.map(command => {
72+
// Convert command to label format: replace spaces with dashes
73+
const labelName = 'command:' + command.replace(/\s+/g, '-');
74+
return {
75+
name: labelName,
76+
color: 'D4C5F9',
77+
description: `Related to '${command}' command`
78+
};
79+
});
80+
81+
// Combine all labels
82+
const allLabels = [...standardLabels, ...commandLabels];
83+
84+
console.log(`Creating/updating ${allLabels.length} labels...`);
85+
86+
// Create or update each label
87+
for (const label of allLabels) {
88+
try {
89+
// Try to get existing label
90+
try {
91+
const existing = await github.rest.issues.getLabel({
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
name: label.name
95+
});
96+
97+
// Update if it exists
98+
await github.rest.issues.updateLabel({
99+
owner: context.repo.owner,
100+
repo: context.repo.repo,
101+
name: label.name,
102+
color: label.color,
103+
description: label.description
104+
});
105+
console.log(`✓ Updated label: ${label.name}`);
106+
} catch (error) {
107+
if (error.status === 404) {
108+
// Create if it doesn't exist
109+
await github.rest.issues.createLabel({
110+
owner: context.repo.owner,
111+
repo: context.repo.repo,
112+
name: label.name,
113+
color: label.color,
114+
description: label.description
115+
});
116+
console.log(`✓ Created label: ${label.name}`);
117+
} else {
118+
throw error;
119+
}
120+
}
121+
} catch (error) {
122+
console.error(`✗ Failed to process label '${label.name}': ${error.message}`);
123+
}
124+
}
125+
126+
console.log('Label management complete!');

.github/workflows/sync-workflows.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
^.editorconfig
2525
^.github/workflows/code-quality.yml
2626
^.github/workflows/regenerate-readme.yml
27+
^.github/workflows/manage-labels.yml
2728
^AGENTS.md
2829
TARGET_REPOS: |
2930
wp-cli/admin-command

0 commit comments

Comments
 (0)