Skip to content

Commit 77a4082

Browse files
author
Chiranjib Swain
authored
Fix: Preserve manually added labels during workflow run and refine label sync logic (#917)
* Refactor labeler function to improve label management and deduplication logic * Refactor labeler function to improve label handling and ensure manual labels are prioritized
1 parent 25abb3c commit 77a4082

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

‎dist/index.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,13 +1074,27 @@ function labeler() {
10741074
allLabels.delete(label);
10751075
}
10761076
}
1077-
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
1077+
const labelsToApply = [...allLabels].slice(0, GITHUB_MAX_LABELS);
10781078
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
1079+
let finalLabels = labelsToApply;
10791080
let newLabels = [];
10801081
try {
1081-
if (!(0, lodash_isequal_1.default)(labelsToAdd, preexistingLabels)) {
1082-
yield api.setLabels(client, pullRequest.number, labelsToAdd);
1083-
newLabels = labelsToAdd.filter(label => !preexistingLabels.includes(label));
1082+
if (!(0, lodash_isequal_1.default)(labelsToApply, preexistingLabels)) {
1083+
// Fetch the latest labels for the PR
1084+
const latestLabels = [];
1085+
// Skip fetching real labels when running tests (uses mock data instead)
1086+
if (process.env.NODE_ENV !== 'test') {
1087+
const pr = yield client.rest.pulls.get(Object.assign(Object.assign({}, github.context.repo), { pull_number: pullRequest.number }));
1088+
latestLabels.push(...pr.data.labels.map(l => l.name).filter(Boolean));
1089+
}
1090+
// Labels added manually during the run (not in first snapshot)
1091+
const manualAddedDuringRun = latestLabels.filter(l => !preexistingLabels.includes(l));
1092+
// Preserve manual labels first, then apply config-based labels, respecting GitHub's 100-label limit
1093+
finalLabels = [
1094+
...new Set([...manualAddedDuringRun, ...labelsToApply])
1095+
].slice(0, GITHUB_MAX_LABELS);
1096+
yield api.setLabels(client, pullRequest.number, finalLabels);
1097+
newLabels = finalLabels.filter(l => !preexistingLabels.includes(l));
10841098
}
10851099
}
10861100
catch (error) {
@@ -1102,7 +1116,7 @@ function labeler() {
11021116
return;
11031117
}
11041118
core.setOutput('new-labels', newLabels.join(','));
1105-
core.setOutput('all-labels', labelsToAdd.join(','));
1119+
core.setOutput('all-labels', finalLabels.join(','));
11061120
if (excessLabels.length) {
11071121
core.warning(`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(', ')}`, { title: 'Label limit for a PR exceeded' });
11081122
}

‎src/labeler.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,38 @@ export async function labeler() {
5151
}
5252
}
5353

54-
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
54+
const labelsToApply = [...allLabels].slice(0, GITHUB_MAX_LABELS);
5555
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
5656

57+
let finalLabels = labelsToApply;
5758
let newLabels: string[] = [];
5859

5960
try {
60-
if (!isEqual(labelsToAdd, preexistingLabels)) {
61-
await api.setLabels(client, pullRequest.number, labelsToAdd);
62-
newLabels = labelsToAdd.filter(
63-
label => !preexistingLabels.includes(label)
61+
if (!isEqual(labelsToApply, preexistingLabels)) {
62+
// Fetch the latest labels for the PR
63+
const latestLabels: string[] = [];
64+
// Skip fetching real labels when running tests (uses mock data instead)
65+
if (process.env.NODE_ENV !== 'test') {
66+
const pr = await client.rest.pulls.get({
67+
...github.context.repo,
68+
pull_number: pullRequest.number
69+
});
70+
latestLabels.push(...pr.data.labels.map(l => l.name).filter(Boolean));
71+
}
72+
73+
// Labels added manually during the run (not in first snapshot)
74+
const manualAddedDuringRun = latestLabels.filter(
75+
l => !preexistingLabels.includes(l)
6476
);
77+
78+
// Preserve manual labels first, then apply config-based labels, respecting GitHub's 100-label limit
79+
finalLabels = [
80+
...new Set([...manualAddedDuringRun, ...labelsToApply])
81+
].slice(0, GITHUB_MAX_LABELS);
82+
83+
await api.setLabels(client, pullRequest.number, finalLabels);
84+
85+
newLabels = finalLabels.filter(l => !preexistingLabels.includes(l));
6586
}
6687
} catch (error: any) {
6788
if (
@@ -94,7 +115,7 @@ export async function labeler() {
94115
}
95116

96117
core.setOutput('new-labels', newLabels.join(','));
97-
core.setOutput('all-labels', labelsToAdd.join(','));
118+
core.setOutput('all-labels', finalLabels.join(','));
98119

99120
if (excessLabels.length) {
100121
core.warning(

0 commit comments

Comments
 (0)