Skip to content

Commit cc872ba

Browse files
authored
Refactor PR and issue label handling logic
1 parent a760b67 commit cc872ba

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

.github/workflows/autolabel-pr-issue.yml

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ jobs:
2828
2929
// Check if triggered by issue event
3030
if (context.eventName === 'issues') {
31-
// Find all open PRs that link to this issue
3231
const issueNumber = context.payload.issue.number;
3332
console.log(`Issue #${issueNumber} labels were updated`);
3433
35-
// Search for PRs that mention this issue
3634
const { data: pullRequests } = await github.rest.pulls.list({
3735
owner: context.repo.owner,
3836
repo: context.repo.repo,
@@ -46,7 +44,6 @@ jobs:
4644
new RegExp(`(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\\s+#${issueNumber}\\b`, 'i'),
4745
new RegExp(`#${issueNumber}\\b`)
4846
];
49-
5047
if (patterns.some(p => p.test(prText))) {
5148
linkedPRs.push(pr.number);
5249
}
@@ -60,7 +57,6 @@ jobs:
6057
console.log(`Found linked PRs: ${linkedPRs.join(', ')}`);
6158
return JSON.stringify({ prs: linkedPRs, issue: issueNumber });
6259
} else {
63-
// Triggered by PR event - original logic
6460
prBody = context.payload.pull_request.body || '';
6561
prTitle = context.payload.pull_request.title || '';
6662
@@ -74,9 +70,7 @@ jobs:
7470
7571
patterns.forEach(pattern => {
7672
const matches = [...textToSearch.matchAll(pattern)];
77-
matches.forEach(match => {
78-
issueNumbers.add(match[1]);
79-
});
73+
matches.forEach(match => issueNumbers.add(match[1]));
8074
});
8175
8276
const issues = Array.from(issueNumbers);
@@ -97,19 +91,13 @@ jobs:
9791
script: |
9892
const extractData = JSON.parse('${{ steps.extract-issues.outputs.result }}');
9993
100-
// Labels to exclude from being applied to PRs
101-
const excludedLabels = ['recode', 'hacktoberfest-accepted'];
102-
10394
let issueNumbers = [];
10495
let prsToUpdate = [];
10596
106-
// Handle both PR and issue events
10797
if (extractData.issue) {
108-
// Issue event - update all linked PRs
10998
issueNumbers = [extractData.issue];
11099
prsToUpdate = extractData.prs || [];
111100
} else {
112-
// PR event - update the current PR
113101
issueNumbers = extractData.issues || [];
114102
prsToUpdate = extractData.prs || [];
115103
}
@@ -130,15 +118,7 @@ jobs:
130118
});
131119
132120
console.log(`Issue #${issueNumber} labels:`, issue.data.labels.map(l => l.name));
133-
134-
issue.data.labels.forEach(label => {
135-
// Only add label if it's not in the excluded list
136-
if (!excludedLabels.includes(label.name.toLowerCase())) {
137-
allLabels.add(label.name);
138-
} else {
139-
console.log(`Excluding label: ${label.name}`);
140-
}
141-
});
121+
issue.data.labels.forEach(label => allLabels.add(label.name));
142122
} catch (error) {
143123
console.log(`Could not fetch issue #${issueNumber}:`, error.message);
144124
}
@@ -168,17 +148,14 @@ jobs:
168148
return;
169149
}
170150
171-
// Update each PR
172151
for (const prNumber of prsToUpdate) {
173152
try {
174-
// First, get current PR labels
175153
const { data: pr } = await github.rest.pulls.get({
176154
owner: context.repo.owner,
177155
repo: context.repo.repo,
178156
pull_number: prNumber
179157
});
180158
181-
// Remove all existing labels first (to handle removed issue labels)
182159
const currentLabels = pr.labels.map(l => l.name);
183160
if (currentLabels.length > 0) {
184161
for (const label of currentLabels) {
@@ -195,17 +172,15 @@ jobs:
195172
}
196173
}
197174
198-
// Apply new labels
199175
await github.rest.issues.addLabels({
200176
owner: context.repo.owner,
201177
repo: context.repo.repo,
202178
issue_number: prNumber,
203179
labels: labels
204180
});
205181
206-
console.log(`Successfully applied ${labels.length} labels to PR #${prNumber}`);
182+
console.log(`Successfully applied ${labels.length} labels to PR #${prNumber}`);
207183
208-
// Add a comment to the PR
209184
await github.rest.issues.createComment({
210185
owner: context.repo.owner,
211186
repo: context.repo.repo,
@@ -215,4 +190,4 @@ jobs:
215190
} catch (error) {
216191
console.error(`Error updating PR #${prNumber}:`, error.message);
217192
}
218-
}
193+
}

0 commit comments

Comments
 (0)