From 0ed1635e4adf87c119a6d19e7bb2eaa04283acd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9r=C3=A8?= Date: Sat, 2 Mar 2024 15:21:16 -0800 Subject: [PATCH] Mitigate race condition around setting size labels By fetching labels from the API just before we apply them, we reduce the chance that the set of labels has changed since the workflow started. --- __tests__/main.test.ts | 5 +++-- badges/coverage.svg | 2 +- dist/index.js | 7 ++++++- src/main.ts | 11 ++++++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 315d725..c3a8273 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -34,7 +34,6 @@ function pullRequestEventContext(overrides = {}): object { head: { ref: 'topic' }, - labels: [], number: 1, user: { login: 'lerebear' @@ -51,10 +50,12 @@ describe('action', () => { jest.spyOn(github, 'getOctokit').mockImplementation((token: string): any => { if (token === 'xxx') { return { + paginate: () => [], rest: { issues: { getLabel: () => {}, - addLabels: () => {} + addLabels: () => {}, + listComments: () => {} } } } diff --git a/badges/coverage.svg b/badges/coverage.svg index 2bd6b8f..337ea38 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 64.74%Coverage64.74% \ No newline at end of file +Coverage: 64.96%Coverage64.96% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 9f2f46b..df21989 100644 --- a/dist/index.js +++ b/dist/index.js @@ -16912,7 +16912,12 @@ async function applyLabel(pull, score, config) { const newLabelName = `${labelPrefix}${score.category.label.name}`; const labelsToAdd = [newLabelName]; const labelsToRemove = []; - for (const existingLabel of pull.labels) { + const existingLabels = await octokit.paginate(octokit.rest.issues.listLabelsOnIssue, { + owner: pull.base.repo.owner.login, + repo: pull.base.repo.name, + issue_number: pull.number + }); + for (const existingLabel of existingLabels) { if (existingLabel.name === newLabelName) { labelsToAdd.pop(); } diff --git a/src/main.ts b/src/main.ts index 3b8f2d7..eca889e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -180,6 +180,7 @@ async function applyLabel( config: Configuration ): Promise { const octokit = github.getOctokit(core.getInput('token')) + const labelPrefix = configOrDefault( config.labeling?.categoryLabelPrefix, DEFAULT_LABEL_PREFIX @@ -188,7 +189,15 @@ async function applyLabel( const labelsToAdd = [newLabelName] const labelsToRemove = [] - for (const existingLabel of pull.labels) { + const existingLabels = await octokit.paginate( + octokit.rest.issues.listLabelsOnIssue, + { + owner: pull.base.repo.owner.login, + repo: pull.base.repo.name, + issue_number: pull.number + } + ) + for (const existingLabel of existingLabels) { if (existingLabel.name === newLabelName) { labelsToAdd.pop() } else if (existingLabel.name.startsWith(labelPrefix)) {