Skip to content

Commit

Permalink
feat: remove PR on test success
Browse files Browse the repository at this point in the history
add tests for PR closing
  • Loading branch information
ghinks committed Apr 10, 2021
1 parent 16ec3e0 commit c514690
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions test/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit c514690

Please sign in to comment.