diff --git a/lib/api.js b/lib/api.js index b45e864b..c0f13975 100644 --- a/lib/api.js +++ b/lib/api.js @@ -85,8 +85,14 @@ async function handlePullRequestUpdate(context, eventName, event) { return; } - await update(context, event.pull_request); - await merge(context, event.pull_request); + const pullRequest = await fetchPullRequest( + context, + event.pull_request.base.repo, + event.pull_request + ); + + await update(context, pullRequest); + await merge(context, pullRequest); } async function handleCheckUpdate(context, eventName, event) { @@ -126,8 +132,13 @@ async function handlePullRequestReviewUpdate(context, eventName, event) { const { action, review } = event; if (action === "submitted") { if (review.state === "approved") { - await update(context, event.pull_request); - await merge(context, event.pull_request); + const pullRequest = await fetchPullRequest( + context, + event.pull_request.base.repo, + event.pull_request + ); + await update(context, pullRequest); + await merge(context, pullRequest); } else { logger.info("Review state is not approved:", review.state); logger.info("Action ignored:", eventName, action); @@ -280,17 +291,7 @@ async function handleIssueComment(context, eventName, event) { if (issue.pull_request == null) { logger.info("Comment not on a PR, skipping"); } else { - const { octokit } = context; - - logger.debug("Getting pull request info for", issue.number, "..."); - let { data: pullRequest } = await octokit.pulls.get({ - owner: repository.owner.login, - repo: repository.name, - pull_number: issue.number - }); - - logger.trace("Full PR:", pullRequest); - + const pullRequest = await fetchPullRequest(context, repository, issue); await update(context, pullRequest); await merge(context, pullRequest); } @@ -299,4 +300,19 @@ async function handleIssueComment(context, eventName, event) { } } +async function fetchPullRequest(context, repository, issue) { + const { octokit } = context; + const { number } = issue; + + logger.debug("Getting pull request info for", number, "..."); + let { data: pullRequest } = await octokit.pulls.get({ + owner: repository.owner.login, + repo: repository.name, + pull_number: number + }); + + logger.trace("Full PR:", pullRequest); + return pullRequest; +} + module.exports = { executeLocally, executeGitHubAction };