From c514690f13fa9c887d1c98af373620daeaccfa35 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Fri, 9 Apr 2021 20:37:19 -0400 Subject: [PATCH] feat: remove PR on test success add tests for PR closing --- lib/result.js | 29 ++++++++++++------------- test/result.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/lib/result.js b/lib/result.js index 5ca9c47..9d2c7b9 100644 --- a/lib/result.js +++ b/lib/result.js @@ -37,9 +37,7 @@ module.exports = async function ({ dependents, pullrequest }) { const parentBranchName = await context.getParentBranchName() const branch = await context.getTestingBranchName(parentBranchName) let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch) - if (resp.data && resp.data.check_runs) { - await updatePRStatus(dependentPkgInfo.owner, dependentPkgInfo.name, branch, resp.data.check_runs) - } + await updatePRStatus(dependentPkgInfo.owner, dependentPkgInfo.name, branch, resp.data.check_runs) if (resp.data.check_runs.length === 0) { resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch) } @@ -166,25 +164,26 @@ function resolveCodeAndExit (overallStatus) { } } -async function updatePRStatus (owner, repo, branch, checkRuns) { +const updatePRStatus = module.exports.updatePRStatus = async function updatePRStatus (owner, repo, branch, checkRuns) { if (!checkRuns || checkRuns.length === 0) { return } const prsToClose = checkRuns.reduce((acc, check) => { if (check.status === 'completed' && - check.conclusion === 'success') { - if (check.pull_requests && check.pull_requests.length !== 0) { - check.pull_requests.forEach((pr) => { - if (pr.head.ref === branch) { - acc.push({ - number: pr.number - }) - } - }) - } + check.conclusion === 'success' && + check.pull_requests && + check.pull_requests.length !== 0) { + check.pull_requests.forEach((pr) => { + if (pr.head.ref === branch) { + acc.push({ + number: pr.number + }) + } + }) } return acc }, []) debug(`Dependent module: ${JSON.stringify(prsToClose, null, 2)}`) - return prsToClose.map((pr) => github.updatePRStatus(owner, repo, pr.number)) + const result = await prsToClose.map((pr) => github.updatePRStatus(owner, repo, pr.number)) + return result } diff --git a/test/result.js b/test/result.js index 121f416..051a248 100644 --- a/test/result.js +++ b/test/result.js @@ -119,4 +119,62 @@ tap.test('wiby.result()', async (tap) => { tap.fail('Should not get here') } }) + + tap.test('update PR Status handles in uncompleted and completed', (t) => { + const owner = 'pkgjs' + const repo = 'wiby' + const branch = 'wiby-main' + t.plan(6) + t.test('no checkruns returns', async (t) => { + const result = await wiby.result.updatePRStatus(owner, repo, branch, null) + t.equal(result, undefined) + }) + t.test('status not completed returns empty array', async (t) => { + const result = await wiby.result.updatePRStatus(owner, repo, branch, [{ status: 'bad' }]) + t.equal(result.length, 0) + }) + t.test('conclusion not a success returns empty array', async (t) => { + const result = await wiby.result.updatePRStatus(owner, repo, branch, [{ status: 'bad', conclusion: 'bad' }]) + t.equal(result.length, 0) + }) + t.test('no pull requests returns empty array', async (t) => { + const result = await wiby.result.updatePRStatus(owner, repo, branch, [{ + status: 'completed', + conclusion: 'success' + }]) + t.equal(result.length, 0) + }) + t.test('empty pull requests returns empty array', async (t) => { + const result = await wiby.result.updatePRStatus(owner, repo, branch, [{ + status: 'completed', + conclusion: 'success', + pull_requests: [] + }]) + t.equal(result.length, 0) + }) + t.test('pull requests with numbers returns values', async (t) => { + nock('https://api.github.com') + // get package json + .patch('/repos/pkgjs/wiby/pulls/1') + .reply(200, { + data: {} + }) + const result = await wiby.result.updatePRStatus(owner, repo, branch, [{ + status: 'completed', + conclusion: 'success', + pull_requests: [{ + number: 1, + head: { + ref: branch + } + }, { + number: 2, + head: { + ref: 'any-other-branch' + } + }] + }]) + t.equal(result.length, 1) + }) + }) })