ci: Backport only the newly added label on labeled events (no-changelog) (#35695)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Charlie Kolb
2026-08-14 07:41:39 +00:00
committed by GitHub
co-authored by Claude Fable 5
parent 9b97fb5b63
commit efba60faf8
3 changed files with 32 additions and 2 deletions
+14 -1
View File
@@ -1,6 +1,7 @@
// Creates backport PR's according to labels on merged PR
import {
getEventFromGithubEventPath,
getPullRequestById,
readPrLabels,
resolveRcBranchForTrack,
@@ -85,7 +86,19 @@ async function fetchPossiblePullRequestFromEnv() {
export async function getLabels() {
const pullRequest = await fetchPossiblePullRequestFromEnv();
return new Set(readPrLabels(pullRequest));
if (pullRequest) {
return new Set(readPrLabels(pullRequest));
}
const event = getEventFromGithubEventPath();
// On a `labeled` event only the added label counts — other backport labels
// on the PR were already handled when they were added (or on merge), and
// reprocessing them would recreate those backport PRs.
if (event.action === 'labeled' && event.label?.name) {
return new Set([event.label.name]);
}
return new Set(readPrLabels(event.pull_request));
}
async function main() {
@@ -1,6 +1,6 @@
import { describe, it, mock, before } from 'node:test';
import assert from 'node:assert/strict';
import { readPrLabels } from './github-helpers.mjs';
import { getEventFromGithubEventPath, readPrLabels } from './github-helpers.mjs';
/**
* Run these tests by running
@@ -13,6 +13,7 @@ import { readPrLabels } from './github-helpers.mjs';
mock.module('./github-helpers.mjs', {
namedExports: {
ensureEnvVar: () => {}, // no-op
getEventFromGithubEventPath: getEventFromGithubEventPath,
readPrLabels: readPrLabels,
resolveRcBranchForTrack: mockResolveRcBranchForTrack,
writeGithubOutput: () => {}, //no-op
@@ -77,6 +78,15 @@ describe('Compute backport targets', () => {
assert.ok(labels.has('release'));
assert.ok(labels.has('Backport to Stable'));
});
it('Should only use the added label on a labeled event', async () => {
process.env.GITHUB_EVENT_PATH = './fixtures/mock-github-event-labeled.json';
/** @type { Set<string> } */
const labels = await getLabels();
assert.equal(labels.size, 1);
assert.ok(labels.has('Backport to Beta'));
});
it('Should parse labels properly in manual workflow context', async () => {
process.env.PULL_REQUEST_ID = '123';
/** @type { Set<string> } */
@@ -0,0 +1,7 @@
{
"action": "labeled",
"label": { "name": "Backport to Beta" },
"pull_request": {
"labels": ["release", "Backport to Stable", "Backport to Beta"]
}
}