Skip to content

Commit

Permalink
Switch to listPullRequestsAssociatedWithCommit to fix private repo is…
Browse files Browse the repository at this point in the history
…sue with search
  • Loading branch information
lfre committed Aug 19, 2019
1 parent 2be5ed7 commit 44a3c46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/index.js
Expand Up @@ -98,7 +98,7 @@ async function onDeployment(context) {
// skip for started or failed statuses
if (state !== 'success' || !headSha || environment !== 'staging') return;

const pullNumber = await getPullRequestNumber(context.github, headSha);
const pullNumber = await getPullRequestNumber(context, headSha);

if (!pullNumber) return;

Expand Down Expand Up @@ -157,7 +157,7 @@ async function onStatus(context) {

if (state !== 'success' || !headSha) return;

const pullNumber = await getPullRequestNumber(context.github, headSha);
const pullNumber = await getPullRequestNumber(context, headSha);

if (!pullNumber) return;

Expand Down
29 changes: 17 additions & 12 deletions app/util/index.js
Expand Up @@ -27,22 +27,27 @@ ${linebreak}

/**
* Finds the pull request number from a commit hash
* @param {object} github Octokit github client
* @param {object} context The github event contextt
* @param {string} headSha The commit hash in the PR
*/
async function getPullRequestNumber(github, headSha) {
const { data: { items = [] } = {} } = await github.search.issuesAndPullRequests({
q: `SHA=${headSha}`,
per_page: 1
async function getPullRequestNumber(context, headSha) {
const {
data: pullRequests = []
} = await context.github.repos.listPullRequestsAssociatedWithCommit(
context.repo({
commit_sha: headSha,
per_page: 100
})
);

let pullNumber = null;

pullRequests.some(({ state, number }) => {
if (state === 'closed') return false;
pullNumber = number;
return true;
});

if (!items.length) return null;

// find the pr number
const { number: pullNumber, state } = items.pop();

if (state === 'closed') return null;

return pullNumber;
}

Expand Down

0 comments on commit 44a3c46

Please sign in to comment.