diff --git a/.github/scripts/compute-backport-targets.mjs b/.github/scripts/compute-backport-targets.mjs index 6140b3693a7..648aeb56754 100644 --- a/.github/scripts/compute-backport-targets.mjs +++ b/.github/scripts/compute-backport-targets.mjs @@ -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() { diff --git a/.github/scripts/compute-backport-targets.test.mjs b/.github/scripts/compute-backport-targets.test.mjs index ddeb1a2e47f..dd360f76c20 100644 --- a/.github/scripts/compute-backport-targets.test.mjs +++ b/.github/scripts/compute-backport-targets.test.mjs @@ -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 } */ + 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 } */ diff --git a/.github/scripts/fixtures/mock-github-event-labeled.json b/.github/scripts/fixtures/mock-github-event-labeled.json new file mode 100644 index 00000000000..c1b75a9e99d --- /dev/null +++ b/.github/scripts/fixtures/mock-github-event-labeled.json @@ -0,0 +1,7 @@ +{ + "action": "labeled", + "label": { "name": "Backport to Beta" }, + "pull_request": { + "labels": ["release", "Backport to Stable", "Backport to Beta"] + } +}