Skip to content

Commit

Permalink
chore: prlinter crashes if it runs alongside itself (#33129)
Browse files Browse the repository at this point in the history
There is a race condition between multiple runs of the PR linter: it
finds a review that it wants to dismiss, but if that already has been
dismissed by another PR linter running in parallel the API call fails
and the linter does too.

Catch this specific case.

----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
rix0rrr and mergify[bot] authored Jan 24, 2025
1 parent 255af9b commit 49fa74f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tools/@aws-cdk/prlint/linter-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ export class PullRequestLinterBase {
message,
});
} catch (e: any) {
if (githubResponseErrors(e).includes('Can not dismiss a dismissed pull request review')) {
// Can happen because of race conditions. We already achieved the result we wanted, so ignore.
continue;
}

// This can fail with a "not found" for some reason
throw new Error(`Dismissing review failed, user is probably not authorized: ${JSON.stringify(e, undefined, 2)}`);
}
Expand Down Expand Up @@ -386,4 +391,11 @@ export function mergeLinterActions(a: LinterActions, b: LinterActions): LinterAc

function nonEmpty<A>(xs: A[]): A[] | undefined {
return xs.length > 0 ? xs : undefined;
}

/**
* Return error messages from an exception thrown by GitHub
*/
function githubResponseErrors(e: Error): string[] {
return (e as any).response?.data?.errors ?? [];
}
62 changes: 61 additions & 1 deletion tools/@aws-cdk/prlint/test/linter-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const linter = new PullRequestLinterBase({
});

beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();

octomock.pulls.get.mockReturnValue({
data: {
Expand All @@ -34,6 +34,66 @@ beforeEach(() => {
octomock.repos.listCommitStatusesForRef.mockReturnValue({ data: [] });
});

test('ignore if dismissing reviews throws a specific "already dismissed" error', async () => {
// GIVEN
octomock.pulls.listReviews.mockReturnValue({
data: [
{
id: 1111,
user: { login: 'aws-cdk-automation' },
state: 'CHANGES_REQUESTED',
body: 'some comment',
},
]
});
octomock.pulls.dismissReview.mockRejectedValue({
status: 422,
response: {
data: {
errors: [
'Can not dismiss a dismissed pull request review',
],
},
},
});

// WHEN
await linter.executeActions({
dismissPreviousReview: true,
});

// THEN: no error
});

test('throw if dismissing reviews throws any other error', async () => {
// GIVEN
octomock.pulls.listReviews.mockReturnValue({
data: [
{
id: 1111,
user: { login: 'aws-cdk-automation' },
state: 'CHANGES_REQUESTED',
body: 'some comment',
},
]
});
octomock.pulls.dismissReview.mockRejectedValue({
status: 422,
response: {
data: {
errors: [
'Review not found',
],
},
},
});

// THEN
await expect(linter.executeActions({
dismissPreviousReview: true,
})).rejects.toThrow(/Dismissing review failed/);
});

test('dismissing a review dismisses and changes the text of all previous reviews', async () => {
// GIVEN
octomock.pulls.listReviews.mockReturnValue({
Expand Down

0 comments on commit 49fa74f

Please sign in to comment.