Skip to content

Commit

Permalink
Retrieve all PR reviews when checking approvals (#190)
Browse files Browse the repository at this point in the history
The results from octokit.pulls.listReviews are paginated; if
there were more than 30 "reviews" (note that this includes
standalone comments on the PR) before the requested number
of approvals, the PR would be considered unapproved and wouldn't
get merged.

By using the `octokit.paginate` helper we make sure to retrieve
all reviews.
  • Loading branch information
jonathanperret authored May 12, 2022
1 parent 8b306ad commit dfd59ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ async function fetchApprovalReviewCount(context, pullRequest) {
}

logger.debug("Getting reviews for", number, "...");
let { data: reviews } = await octokit.pulls.listReviews({
const reviews = await octokit.paginate(octokit.pulls.listReviews, {
owner: pullRequest.base.repo.owner.login,
repo: pullRequest.base.repo.name,
pull_number: number
Expand Down Expand Up @@ -22095,7 +22095,7 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/***/ ((module) => {

"use strict";
module.exports = JSON.parse('{"name":"automerge-action","version":"0.15.1","description":"GitHub action to automatically merge pull requests","main":"lib/api.js","author":"Pascal","license":"MIT","private":true,"bin":{"automerge-action":"./bin/automerge.js"},"scripts":{"test":"jest","it":"node it/it.js","lint":"prettier -l lib/** test/** && eslint .","compile":"ncc build bin/automerge.js --license LICENSE -o dist","prepublish":"yarn lint && yarn test && yarn compile"},"dependencies":{"@actions/core":"^1.6.0","@octokit/rest":"^18.12.0","argparse":"^2.0.1","fs-extra":"^10.0.1","object-resolve-path":"^1.1.1","tmp":"^0.2.1"},"devDependencies":{"@vercel/ncc":"^0.33.3","dotenv":"^16.0.0","eslint":"^8.11.0","eslint-plugin-jest":"^26.1.3","jest":"^27.5.1","prettier":"^2.6.0"},"prettier":{"trailingComma":"none","arrowParens":"avoid"}}');
module.exports = JSON.parse('{"name":"automerge-action","version":"0.15.2","description":"GitHub action to automatically merge pull requests","main":"lib/api.js","author":"Pascal","license":"MIT","private":true,"bin":{"automerge-action":"./bin/automerge.js"},"scripts":{"test":"jest","it":"node it/it.js","lint":"prettier -l lib/** test/** && eslint .","compile":"ncc build bin/automerge.js --license LICENSE -o dist","prepublish":"yarn lint && yarn test && yarn compile"},"dependencies":{"@actions/core":"^1.6.0","@octokit/rest":"^18.12.0","argparse":"^2.0.1","fs-extra":"^10.0.1","object-resolve-path":"^1.1.1","tmp":"^0.2.1"},"devDependencies":{"@vercel/ncc":"^0.33.3","dotenv":"^16.0.0","eslint":"^8.11.0","eslint-plugin-jest":"^26.1.3","jest":"^27.5.1","prettier":"^2.6.0"},"prettier":{"trailingComma":"none","arrowParens":"avoid"}}');

/***/ })

Expand Down
2 changes: 1 addition & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ async function fetchApprovalReviewCount(context, pullRequest) {
}

logger.debug("Getting reviews for", number, "...");
let { data: reviews } = await octokit.pulls.listReviews({
const reviews = await octokit.paginate(octokit.pulls.listReviews, {
owner: pullRequest.base.repo.owner.login,
repo: pullRequest.base.repo.name,
pull_number: number
Expand Down
25 changes: 11 additions & 14 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ test("only merge PRs with required approvals", async () => {
pulls: {
list: jest.fn(() => ({ data: [pr] })),
merge: jest.fn(() => (merged = true)),
listReviews: jest.fn(() => ({ data: [] }))
}
listReviews: Symbol("listReviews")
},
paginate: jest.fn(() => [])
};

const event = {
Expand All @@ -63,24 +64,20 @@ test("only merge PRs with required approvals", async () => {
expect(merged).toEqual(false); // if there's no approval, it should fail

merged = false;
octokit.pulls.listReviews.mockReturnValueOnce({
data: [
{ state: "APPROVED", user: { login: "approval_user" } },
{ state: "APPROVED", user: { login: "approval_user2" } }
]
});
octokit.paginate.mockReturnValueOnce([
{ state: "APPROVED", user: { login: "approval_user" } },
{ state: "APPROVED", user: { login: "approval_user2" } }
]);

// WHEN
await api.executeGitHubAction({ config, octokit }, "check_suite", event);
expect(merged).toEqual(true); // if there are two approvals, it should succeed

merged = false;
octokit.pulls.listReviews.mockReturnValueOnce({
data: [
{ state: "APPROVED", user: { login: "approval_user" } },
{ state: "APPROVED", user: { login: "approval_user" } }
]
});
octokit.paginate.mockReturnValueOnce([
{ state: "APPROVED", user: { login: "approval_user" } },
{ state: "APPROVED", user: { login: "approval_user" } }
]);

// WHEN a user has given
await api.executeGitHubAction({ config, octokit }, "check_suite", event);
Expand Down

0 comments on commit dfd59ba

Please sign in to comment.