1
0
mirror of https://github.com/actions/labeler synced 2026-05-12 14:21:09 +02:00

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
This commit is contained in:
Chiranjib Swain
2026-01-23 00:43:24 +05:30
committed by GitHub
parent 25abb3cad4
commit 77a4082b84
2 changed files with 46 additions and 11 deletions

24
dist/index.js vendored
View File

@@ -1074,13 +1074,27 @@ function labeler() {
allLabels.delete(label); allLabels.delete(label);
} }
} }
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS); const labelsToApply = [...allLabels].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS); const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
let finalLabels = labelsToApply;
let newLabels = []; let newLabels = [];
try { try {
if (!(0, lodash_isequal_1.default)(labelsToAdd, preexistingLabels)) { if (!(0, lodash_isequal_1.default)(labelsToApply, preexistingLabels)) {
yield api.setLabels(client, pullRequest.number, labelsToAdd); // Fetch the latest labels for the PR
newLabels = labelsToAdd.filter(label => !preexistingLabels.includes(label)); const latestLabels = [];
// Skip fetching real labels when running tests (uses mock data instead)
if (process.env.NODE_ENV !== 'test') {
const pr = yield client.rest.pulls.get(Object.assign(Object.assign({}, github.context.repo), { pull_number: pullRequest.number }));
latestLabels.push(...pr.data.labels.map(l => l.name).filter(Boolean));
}
// Labels added manually during the run (not in first snapshot)
const manualAddedDuringRun = latestLabels.filter(l => !preexistingLabels.includes(l));
// Preserve manual labels first, then apply config-based labels, respecting GitHub's 100-label limit
finalLabels = [
...new Set([...manualAddedDuringRun, ...labelsToApply])
].slice(0, GITHUB_MAX_LABELS);
yield api.setLabels(client, pullRequest.number, finalLabels);
newLabels = finalLabels.filter(l => !preexistingLabels.includes(l));
} }
} }
catch (error) { catch (error) {
@@ -1102,7 +1116,7 @@ function labeler() {
return; return;
} }
core.setOutput('new-labels', newLabels.join(',')); core.setOutput('new-labels', newLabels.join(','));
core.setOutput('all-labels', labelsToAdd.join(',')); core.setOutput('all-labels', finalLabels.join(','));
if (excessLabels.length) { if (excessLabels.length) {
core.warning(`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(', ')}`, { title: 'Label limit for a PR exceeded' }); core.warning(`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(', ')}`, { title: 'Label limit for a PR exceeded' });
} }

View File

@@ -51,17 +51,38 @@ export async function labeler() {
} }
} }
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS); const labelsToApply = [...allLabels].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS); const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
let finalLabels = labelsToApply;
let newLabels: string[] = []; let newLabels: string[] = [];
try { try {
if (!isEqual(labelsToAdd, preexistingLabels)) { if (!isEqual(labelsToApply, preexistingLabels)) {
await api.setLabels(client, pullRequest.number, labelsToAdd); // Fetch the latest labels for the PR
newLabels = labelsToAdd.filter( const latestLabels: string[] = [];
label => !preexistingLabels.includes(label) // Skip fetching real labels when running tests (uses mock data instead)
if (process.env.NODE_ENV !== 'test') {
const pr = await client.rest.pulls.get({
...github.context.repo,
pull_number: pullRequest.number
});
latestLabels.push(...pr.data.labels.map(l => l.name).filter(Boolean));
}
// Labels added manually during the run (not in first snapshot)
const manualAddedDuringRun = latestLabels.filter(
l => !preexistingLabels.includes(l)
); );
// Preserve manual labels first, then apply config-based labels, respecting GitHub's 100-label limit
finalLabels = [
...new Set([...manualAddedDuringRun, ...labelsToApply])
].slice(0, GITHUB_MAX_LABELS);
await api.setLabels(client, pullRequest.number, finalLabels);
newLabels = finalLabels.filter(l => !preexistingLabels.includes(l));
} }
} catch (error: any) { } catch (error: any) {
if ( if (
@@ -94,7 +115,7 @@ export async function labeler() {
} }
core.setOutput('new-labels', newLabels.join(',')); core.setOutput('new-labels', newLabels.join(','));
core.setOutput('all-labels', labelsToAdd.join(',')); core.setOutput('all-labels', finalLabels.join(','));
if (excessLabels.length) { if (excessLabels.length) {
core.warning( core.warning(