Skip to content

Commit

Permalink
fix: query for queued prs after removing from queue (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Dec 29, 2021
1 parent 10801c4 commit eda3b75
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 76 deletions.
59 changes: 52 additions & 7 deletions dist/473.index.js

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

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

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions dist/61.index.js

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

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

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

26 changes: 12 additions & 14 deletions src/helpers/manage-merge-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ limitations under the License.

import * as core from '@actions/core';
import { FIRST_QUEUED_PR_LABEL, QUEUED_FOR_MERGE_PREFIX, READY_FOR_MERGE_PR_LABEL } from '../constants';
import { PullRequest, PullRequestSearchResults } from '../types';
import { PullRequest } from '../types';
import { context } from '@actions/github';
import { map } from 'bluebird';
import { octokit } from '../octokit';
import { removeLabelIfExists } from './remove-label';
import { setCommitStatus } from './set-commit-status';
import { updateMergeQueue } from '../utils/update-merge-queue';

export const manageMergeQueue = async () => {
const {
data: { items, total_count: queuePosition }
} = await getQueuedPrData();
const issue_number = context.issue.number;
const { data: pullRequest } = await octokit.pulls.get({ pull_number: issue_number, ...context.repo });
if (pullRequest.merged || !pullRequest.labels.find(label => label.name === READY_FOR_MERGE_PR_LABEL)) {
core.info('This PR is not in the merge queue.');
return removePRFromQueue(pullRequest, items);
return removePRFromQueue(pullRequest);
}

const {
data: { total_count: queuePosition }
} = await getQueuedPrData();
const isFirstQueuePosition = queuePosition === 1 || pullRequest.labels.find(label => label.name === FIRST_QUEUED_PR_LABEL);
return Promise.all([
octokit.issues.addLabels({
Expand All @@ -47,17 +48,14 @@ export const manageMergeQueue = async () => {
]);
};

const removePRFromQueue = async (pullRequest: PullRequest, queuedPrs: PullRequestSearchResults) => {
const removePRFromQueue = async (pullRequest: PullRequest) => {
const queueLabel = pullRequest.labels.find(label => label.name?.startsWith(QUEUED_FOR_MERGE_PREFIX))?.name;
if (queueLabel) {
await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label =>
octokit.issues.removeLabel({
name: label,
issue_number: pullRequest.number,
...context.repo
})
);
await updateMergeQueue(queuedPrs);
await map([READY_FOR_MERGE_PR_LABEL, queueLabel], label => removeLabelIfExists(label, pullRequest.number));
const {
data: { items }
} = await getQueuedPrData();
await updateMergeQueue(items);
}
};

Expand Down
8 changes: 5 additions & 3 deletions src/helpers/remove-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ interface RemoveLabel {
label: string;
}

export const removeLabel = ({ label }: RemoveLabel) =>
export const removeLabel = ({ label }: RemoveLabel) => removeLabelIfExists(label, context.issue.number);

export const removeLabelIfExists = (labelName: string, issue_number: number) =>
octokit.issues
.removeLabel({
name: label,
issue_number: context.issue.number,
name: labelName,
issue_number,
...context.repo
})
.catch(error => {
Expand Down
7 changes: 2 additions & 5 deletions src/utils/update-merge-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QUEUED_FOR_MERGE_PREFIX } from '../constants';
import { context } from '@actions/github';
import { map } from 'bluebird';
import { octokit } from '../octokit';
import { removeLabelIfExists } from '../helpers/remove-label';

export const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => {
const prsSortedByQueuePosition = queuedPrs
Expand All @@ -28,11 +29,7 @@ export const updateMergeQueue = (queuedPrs: PullRequestSearchResults) => {
issue_number: pull_number,
...context.repo
}),
octokit.issues.removeLabel({
name: label,
issue_number: pull_number,
...context.repo
})
removeLabelIfExists(label, pull_number)
]);
});
};
29 changes: 7 additions & 22 deletions test/helpers/manage-merge-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { READY_FOR_MERGE_PR_LABEL } from '../../src/constants';
import { context } from '@actions/github';
import { manageMergeQueue } from '../../src/helpers/manage-merge-queue';
import { octokit } from '../../src/octokit';
import { removeLabelIfExists } from '../../src/helpers/remove-label';
import { setCommitStatus } from '../../src/helpers/set-commit-status';
import { updateMergeQueue } from '../../src/utils/update-merge-queue';

jest.mock('../../src/helpers/remove-label');
jest.mock('../../src/helpers/set-commit-status');
jest.mock('../../src/utils/update-merge-queue');
jest.mock('@actions/core');
Expand All @@ -28,8 +30,7 @@ jest.mock('@actions/github', () => ({
rest: {
pulls: { get: jest.fn() },
issues: {
addLabels: jest.fn(),
removeLabel: jest.fn()
addLabels: jest.fn()
},
search: { issuesAndPullRequests: jest.fn() }
}
Expand Down Expand Up @@ -63,16 +64,8 @@ describe('manageMergeQueue', () => {
});

it('should call remove label with correct params', () => {
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: READY_FOR_MERGE_PR_LABEL,
issue_number: 123,
...context.repo
});
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: 'QUEUED FOR MERGE #1',
issue_number: 123,
...context.repo
});
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123);
});

it('should call updateMergeQueue with correct params', () => {
Expand Down Expand Up @@ -105,16 +98,8 @@ describe('manageMergeQueue', () => {
});

it('should call remove label with correct params', () => {
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: READY_FOR_MERGE_PR_LABEL,
issue_number: 123,
...context.repo
});
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: 'QUEUED FOR MERGE #2',
issue_number: 123,
...context.repo
});
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #2', 123);
});

it('should call updateMergeQueue with correct params', () => {
Expand Down
27 changes: 6 additions & 21 deletions test/utils/update-merge-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ limitations under the License.
import { PullRequestSearchResults } from '../../src/types';
import { context } from '@actions/github';
import { octokit } from '../../src/octokit';
import { removeLabelIfExists } from '../../src/helpers/remove-label';
import { updateMergeQueue } from '../../src/utils/update-merge-queue';

jest.mock('../../src/helpers/remove-label');
jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
Expand All @@ -24,7 +26,6 @@ jest.mock('@actions/github', () => ({
pulls: { get: jest.fn() },
issues: {
addLabels: jest.fn(),
removeLabel: jest.fn(),
listLabelsOnIssue: jest.fn()
},
search: { issuesAndPullRequests: jest.fn() }
Expand Down Expand Up @@ -62,16 +63,8 @@ describe('updateMergeQueue', () => {
});

it('should call remove label with correct params', () => {
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: 'QUEUED FOR MERGE #2',
issue_number: 123,
...context.repo
});
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
name: 'QUEUED FOR MERGE #3',
issue_number: 456,
...context.repo
});
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #2', 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #3', 456);
});
});

Expand Down Expand Up @@ -104,16 +97,8 @@ describe('updateMergeQueue', () => {
});

it('should call remove label with correct params', () => {
expect(octokit.issues.removeLabel).not.toHaveBeenCalledWith({
issue_number: 123,
name: 'QUEUED FOR MERGE #1',
...context.repo
});
expect(octokit.issues.removeLabel).toHaveBeenCalledWith({
issue_number: 456,
name: 'QUEUED FOR MERGE #3',
...context.repo
});
expect(removeLabelIfExists).not.toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #3', 456);
});
});

Expand Down

0 comments on commit eda3b75

Please sign in to comment.