Skip to content

Commit

Permalink
feat(manage-merge-queue): do not allow ready for merge when PR is not…
Browse files Browse the repository at this point in the history
… core approved (#546)

* do not allow ready for merge when PR is not core approved

* update to check for approvals satisfied check

* fixed tests

* fixed tests

* fixed tests

* update comment body

---------

Co-authored-by: Pooja Donekal <[email protected]>
Co-authored-by: Dan Adajian <[email protected]>
  • Loading branch information
3 people authored Feb 19, 2024
1 parent 8e29319 commit d247648
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 6 deletions.
374 changes: 373 additions & 1 deletion dist/676.index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/676.index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42180,13 +42180,15 @@ var map = {
7473,
832,
710,
445,
205,
676
],
"./manage-merge-queue.ts": [
7473,
832,
710,
445,
205,
676
],
Expand Down Expand Up @@ -42216,13 +42218,15 @@ var map = {
1004,
832,
710,
445,
205,
676
],
"./prepare-queued-pr-for-merge.ts": [
1004,
832,
710,
445,
205,
676
],
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/helpers/manage-merge-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { setCommitStatus } from './set-commit-status';
import { updateMergeQueue } from '../utils/update-merge-queue';
import { paginateAllOpenPullRequests } from '../utils/paginate-open-pull-requests';
import { updatePrWithDefaultBranch } from './prepare-queued-pr-for-merge';
import { approvalsSatisfied } from './approvals-satisfied';
import { createPrComment } from './create-pr-comment';

export class ManageMergeQueue extends HelperInputs {
login?: string;
Expand All @@ -42,6 +44,11 @@ export const manageMergeQueue = async ({ login, slack_webhook_url }: ManageMerge
core.info('This PR is not in the merge queue.');
return removePrFromQueue(pullRequest);
}
const prMeetsRequiredApprovals = await approvalsSatisfied();
if (!prMeetsRequiredApprovals) {
await createPrComment({ body: 'PRs must meet all required approvals before entering the merge queue.' });
return removePrFromQueue(pullRequest);
}
const queuedPrs = await getQueuedPullRequests();
const queuePosition = queuedPrs.length;
if (pullRequest.labels.find(label => label.name === JUMP_THE_QUEUE_PR_LABEL)) {
Expand Down
50 changes: 47 additions & 3 deletions test/helpers/manage-merge-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import { removeLabelIfExists } from '../../src/helpers/remove-label';
import { setCommitStatus } from '../../src/helpers/set-commit-status';
import { updateMergeQueue } from '../../src/utils/update-merge-queue';
import { updatePrWithDefaultBranch } from '../../src/helpers/prepare-queued-pr-for-merge';
import { approvalsSatisfied } from '../../src/helpers/approvals-satisfied';
import { createPrComment } from '../../src/helpers/create-pr-comment';

jest.mock('../../src/helpers/remove-label');
jest.mock('../../src/helpers/set-commit-status');
jest.mock('../../src/utils/notify-user');
jest.mock('../../src/utils/update-merge-queue');
jest.mock('../../src/helpers/approvals-satisfied');
jest.mock('../../src/helpers/create-pr-comment');
jest.mock('../../src/utils/../../src/helpers/prepare-queued-pr-for-merge');
jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
Expand Down Expand Up @@ -69,6 +73,38 @@ describe('manageMergeQueue', () => {
});
});

describe('pr not core approved case', () => {
const queuedPrs = [{ labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }, { labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? queuedPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: {
merged: false,
head: { sha: 'sha' },
number: 123,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(false);
await manageMergeQueue();
});

it('should check for no commit status being published', () => {
expect(setCommitStatus).not.toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'pending',
description: 'This PR is in line to merge.'
});
});

it('should add pr comment', () => {
expect(createPrComment).toHaveBeenCalled();
});
});

describe('pr not ready for merge case', () => {
const queuedPrs = [{ labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }, { labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }];
beforeEach(async () => {
Expand All @@ -83,6 +119,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: 'QUEUED FOR MERGE #2' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down Expand Up @@ -118,6 +155,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down Expand Up @@ -161,6 +199,7 @@ describe('manageMergeQueue', () => {
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: pullRequest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down Expand Up @@ -204,6 +243,7 @@ describe('manageMergeQueue', () => {
}
}));
(octokitGraphql as unknown as jest.Mock).mockRejectedValue(new Error('Auto merge is not allowed for this repo'));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down Expand Up @@ -248,6 +288,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down Expand Up @@ -277,6 +318,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: JUMP_THE_QUEUE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand All @@ -301,7 +343,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));

(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });

expect(notifyUser).toHaveBeenCalled();
Expand All @@ -325,6 +367,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });

expect(notifyUser).not.toHaveBeenCalled();
Expand All @@ -342,7 +385,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));

(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login });

expect(notifyUser).not.toHaveBeenCalled();
Expand All @@ -360,7 +403,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));

(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ slack_webhook_url });

expect(notifyUser).not.toHaveBeenCalled();
Expand All @@ -383,6 +426,7 @@ describe('manageMergeQueue', () => {
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});

Expand Down

0 comments on commit d247648

Please sign in to comment.