Skip to content

Commit 1e9f30f

Browse files
Merge pull request #53 from kpj2006/sync-pr-labels
try using another api for checking
2 parents 8e6adff + 446a962 commit 1e9f30f

3 files changed

Lines changed: 248 additions & 33 deletions

File tree

.github/release-drafter.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ categories:
5454
- title: '👥 Contributors'
5555
labels:
5656
- 'first-time-contributor'
57-
- 'external-contributor'
57+
- 'repeat-contributor'
58+
- 'org-member'
5859

5960
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
6061
change-title-escapes: '\<*_&'

.github/workflows/setup-labels.yml

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
name: Setup Repository Labels
2+
3+
on:
4+
workflow_dispatch: # Manual trigger
5+
push:
6+
branches: [main, master]
7+
paths:
8+
- '.github/workflows/setup-labels.yml'
9+
10+
permissions:
11+
issues: write
12+
13+
jobs:
14+
create-labels:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Create all required labels
18+
uses: actions/github-script@v7
19+
with:
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
21+
script: |
22+
// Define all labels with colors and descriptions
23+
const requiredLabels = [
24+
// ==================== CONTRIBUTOR LABELS ====================
25+
{
26+
name: 'org-member',
27+
color: '0E8A16',
28+
description: 'Member of the organization with admin/maintain permissions'
29+
},
30+
{
31+
name: 'first-time-contributor',
32+
color: '7057FF',
33+
description: 'First PR of an external contributor'
34+
},
35+
{
36+
name: 'repeat-contributor',
37+
color: '6F42C1',
38+
description: 'PR from an external contributor who already had PRs merged'
39+
},
40+
41+
// ==================== ISSUE TRACKING LABELS ====================
42+
{
43+
name: 'no-issue-linked',
44+
color: 'D73A4A',
45+
description: 'PR is not linked to any issue'
46+
},
47+
48+
// ==================== FILE TYPE LABELS ====================
49+
{
50+
name: 'documentation',
51+
color: '0075CA',
52+
description: 'Changes to documentation files'
53+
},
54+
{
55+
name: 'frontend',
56+
color: 'FEF2C0',
57+
description: 'Changes to frontend code'
58+
},
59+
{
60+
name: 'backend',
61+
color: 'BFD4F2',
62+
description: 'Changes to backend code'
63+
},
64+
{
65+
name: 'javascript',
66+
color: 'F1E05A',
67+
description: 'JavaScript/TypeScript code changes'
68+
},
69+
{
70+
name: 'python',
71+
color: '3572A5',
72+
description: 'Python code changes'
73+
},
74+
{
75+
name: 'configuration',
76+
color: 'EDEDED',
77+
description: 'Configuration file changes'
78+
},
79+
{
80+
name: 'github-actions',
81+
color: '2088FF',
82+
description: 'GitHub Actions workflow changes'
83+
},
84+
{
85+
name: 'dependencies',
86+
color: '0366D6',
87+
description: 'Dependency file changes'
88+
},
89+
{
90+
name: 'tests',
91+
color: 'C5DEF5',
92+
description: 'Test file changes'
93+
},
94+
{
95+
name: 'docker',
96+
color: '0DB7ED',
97+
description: 'Docker-related changes'
98+
},
99+
{
100+
name: 'ci-cd',
101+
color: '6E5494',
102+
description: 'CI/CD pipeline changes'
103+
},
104+
105+
// ==================== SIZE LABELS ====================
106+
{
107+
name: 'size/XS',
108+
color: '00FF00',
109+
description: 'Extra small PR (≤10 lines changed)'
110+
},
111+
{
112+
name: 'size/S',
113+
color: '77FF00',
114+
description: 'Small PR (11-50 lines changed)'
115+
},
116+
{
117+
name: 'size/M',
118+
color: 'FFFF00',
119+
description: 'Medium PR (51-200 lines changed)'
120+
},
121+
{
122+
name: 'size/L',
123+
color: 'FF9900',
124+
description: 'Large PR (201-500 lines changed)'
125+
},
126+
{
127+
name: 'size/XL',
128+
color: 'FF0000',
129+
description: 'Extra large PR (>500 lines changed)'
130+
}
131+
];
132+
133+
console.log('='.repeat(60));
134+
console.log('🏷️ REPOSITORY LABEL SETUP');
135+
console.log('='.repeat(60));
136+
console.log(`Total labels to create: ${requiredLabels.length}\n`);
137+
138+
// Get existing labels with pagination
139+
const existingLabels = await github.paginate(
140+
github.rest.issues.listLabelsForRepo,
141+
{
142+
owner: context.repo.owner,
143+
repo: context.repo.repo,
144+
per_page: 100
145+
}
146+
);
147+
148+
const existingLabelNames = existingLabels.map(label => label.name);
149+
150+
let created = 0;
151+
let updated = 0;
152+
let skipped = 0;
153+
let failed = 0;
154+
155+
// Process each label
156+
for (const label of requiredLabels) {
157+
try {
158+
if (!existingLabelNames.includes(label.name)) {
159+
// Create new label
160+
await github.rest.issues.createLabel({
161+
owner: context.repo.owner,
162+
repo: context.repo.repo,
163+
name: label.name,
164+
color: label.color,
165+
description: label.description
166+
});
167+
console.log(`✅ Created: ${label.name} (#${label.color})`);
168+
created++;
169+
} else {
170+
// Update existing label (in case color/description changed)
171+
const existingLabel = existingLabels.find(l => l.name === label.name);
172+
if (existingLabel.color !== label.color || existingLabel.description !== label.description) {
173+
await github.rest.issues.updateLabel({
174+
owner: context.repo.owner,
175+
repo: context.repo.repo,
176+
name: label.name,
177+
color: label.color,
178+
description: label.description
179+
});
180+
console.log(`🔄 Updated: ${label.name} (#${label.color})`);
181+
updated++;
182+
} else {
183+
console.log(`⏭️ Skipped: ${label.name} (already exists)`);
184+
skipped++;
185+
}
186+
}
187+
} catch (error) {
188+
console.log(`❌ Failed: ${label.name} - ${error.message}`);
189+
failed++;
190+
}
191+
}
192+
193+
// Summary
194+
console.log('\n' + '='.repeat(60));
195+
console.log('📊 SUMMARY');
196+
console.log('='.repeat(60));
197+
console.log(`✅ Created: ${created}`);
198+
console.log(`🔄 Updated: ${updated}`);
199+
console.log(`⏭️ Skipped: ${skipped}`);
200+
console.log(`❌ Failed: ${failed}`);
201+
console.log('='.repeat(60));
202+
203+
// Fail the step if any labels failed to create/update
204+
if (failed > 0) {
205+
core.setFailed(`Label setup failed! ${failed} label(s) could not be created or updated.`);
206+
} else if (created > 0 || updated > 0) {
207+
console.log('\n🎉 Label setup complete! Your repository is ready.');
208+
} else {
209+
console.log('\n✨ All labels are already up to date.');
210+
}

.github/workflows/sync-pr-labels.yml

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: Sync PR Labels
22

33
on:
4-
pull_request:
5-
types: [opened, reopened, synchronize, edited]
64
pull_request_target:
75
types: [opened, reopened, synchronize, edited]
86

@@ -219,37 +217,48 @@ jobs:
219217
sizeLabel = 'size/XL';
220218
}
221219
222-
console.log(`Applying size label: ${sizeLabel}`);
220+
console.log(`Calculated size label: ${sizeLabel}`);
223221
224-
// Remove any existing size labels first
222+
// Get current labels on the PR
225223
const currentLabels = await github.rest.issues.listLabelsOnIssue({
226224
owner: context.repo.owner,
227225
repo: context.repo.repo,
228226
issue_number: prNumber
229227
});
230228
231-
const sizeLabelsToRemove = currentLabels.data
229+
const existingSizeLabels = currentLabels.data
232230
.map(label => label.name)
233231
.filter(name => name.startsWith('size/'));
234232
235-
for (const label of sizeLabelsToRemove) {
236-
await github.rest.issues.removeLabel({
237-
owner: context.repo.owner,
238-
repo: context.repo.repo,
239-
issue_number: prNumber,
240-
name: label
241-
});
233+
// Check if the size label needs to be changed
234+
if (existingSizeLabels.length === 1 && existingSizeLabels[0] === sizeLabel) {
235+
console.log(`Size label ${sizeLabel} is already correct, no changes needed`);
236+
return;
237+
}
238+
239+
// Remove outdated size labels only if they differ
240+
if (existingSizeLabels.length > 0) {
241+
console.log(`Removing outdated size labels: ${existingSizeLabels.join(', ')}`);
242+
for (const label of existingSizeLabels) {
243+
await github.rest.issues.removeLabel({
244+
owner: context.repo.owner,
245+
repo: context.repo.repo,
246+
issue_number: prNumber,
247+
name: label
248+
});
249+
}
242250
}
243251
244252
// Apply the new size label
253+
console.log(`Applying new size label: ${sizeLabel}`);
245254
await github.rest.issues.addLabels({
246255
owner: context.repo.owner,
247256
repo: context.repo.repo,
248257
issue_number: prNumber,
249258
labels: [sizeLabel]
250259
});
251260
252-
# STEP 4: Contributor-based labels
261+
# STEP 4: Contributor-based labels(in this later we can add logic of team p as discussed on discord)
253262
- name: Apply contributor-based labels
254263
uses: actions/github-script@v7
255264
env:
@@ -270,37 +279,32 @@ jobs:
270279
271280
const contributorLabels = [];
272281
273-
// Check if contributor is a member of the organization
274-
try {
275-
await github.rest.orgs.checkMembershipForUser({
276-
org: context.repo.owner,
277-
username: prAuthor
278-
});
279-
contributorLabels.push('member');
280-
} catch (error) {
281-
// Not a member
282-
if (commits.data.length <= 1) {
283-
contributorLabels.push('first-time-contributor');
284-
} else {
285-
contributorLabels.push('external-contributor');
286-
}
287-
}
288-
289-
// Check if PR author is a collaborator
282+
// First check if maintainer
283+
let isMaintainer = false;
290284
try {
291285
const permissionLevel = await github.rest.repos.getCollaboratorPermissionLevel({
292286
owner: context.repo.owner,
293287
repo: context.repo.repo,
294288
username: prAuthor
295289
});
296290
297-
if (permissionLevel.data.permission === 'admin' || permissionLevel.data.permission === 'maintain') {
298-
contributorLabels.push('maintainer');
291+
if (['admin', 'maintain'].includes(permissionLevel.data.permission)) {
292+
contributorLabels.push('org-Member');
293+
isMaintainer = true;
299294
}
300295
} catch (error) {
301296
console.log('Could not check collaborator status');
302297
}
303298
299+
// If not maintainer, check contributor type
300+
if (!isMaintainer) {
301+
if (commits.data.length <= 1) {
302+
contributorLabels.push('first-time-contributor');
303+
} else {
304+
contributorLabels.push('repeat-contributor');
305+
}
306+
}
307+
304308
if (contributorLabels.length > 0) {
305309
console.log(`Applying contributor-based labels: ${contributorLabels.join(', ')}`);
306310

0 commit comments

Comments
 (0)