Skip to content

Commit

Permalink
Mitigate race condition around setting size labels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lerebear committed Mar 2, 2024
1 parent 4b744e0 commit 0ed1635
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function pullRequestEventContext(overrides = {}): object {
head: {
ref: 'topic'
},
labels: [],
number: 1,
user: {
login: 'lerebear'
Expand All @@ -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: () => {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ async function applyLabel(
config: Configuration
): Promise<void> {
const octokit = github.getOctokit(core.getInput('token'))

const labelPrefix = configOrDefault(
config.labeling?.categoryLabelPrefix,
DEFAULT_LABEL_PREFIX
Expand All @@ -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)) {
Expand Down

0 comments on commit 0ed1635

Please sign in to comment.