Skip to content

Commit

Permalink
feat: remove PR on test success
Browse files Browse the repository at this point in the history
remove the PR during the result scan
  • Loading branch information
ghinks committed Apr 8, 2021
1 parent d1566eb commit 16ec3e0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
11 changes: 10 additions & 1 deletion bin/commands/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ exports.builder = (yargs) => yargs
type: 'string',
conflicts: 'config'
})
.option('pullrequest', {
desc: 'close a draft PR created in the test phase',
alias: 'pr',
type: 'boolean',
conflicts: 'config'
})
.option('config', {
desc: 'Path to the configuration file. By default it will try to load the configuration from the first file it finds in the current working directory: `.wiby.json`, `.wiby.js`',
type: 'string'
})

exports.handler = async (params) => {
const config = params.dependent
? { dependents: [{ repository: params.dependent }] }
? {
dependents: [{ repository: params.dependent }],
pullrequest: !!params.pullrequest
}
: wiby.validate({ config: params.config })

const result = await wiby.result(config)
Expand Down
9 changes: 9 additions & 0 deletions lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ module.exports.createPR = async function createPR (owner, repo, title, head, bas
body
})
}

module.exports.updatePRStatus = async function updatePRStatus (owner, repo, pullNumber) {
return octokit.rest.pulls.update({
owner,
repo,
pull_number: pullNumber,
state: 'closed'
})
}
28 changes: 27 additions & 1 deletion lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze

const PENDING_RESULT_EXIT_CODE = 64

module.exports = async function ({ dependents }) {
module.exports = async function ({ dependents, pullrequest }) {
const parentPkgJSON = await context.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
const output = { status: pipelineStatusesEnum.PENDING, results: [] }
Expand All @@ -37,6 +37,9 @@ module.exports = async function ({ dependents }) {
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)
}
if (resp.data.check_runs.length === 0) {
resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
}
Expand Down Expand Up @@ -162,3 +165,26 @@ function resolveCodeAndExit (overallStatus) {
process.exit(PENDING_RESULT_EXIT_CODE)
}
}

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
})
}
})
}
}
return acc
}, [])
debug(`Dependent module: ${JSON.stringify(prsToClose, null, 2)}`)
return prsToClose.map((pr) => github.updatePRStatus(owner, repo, pr.number))
}

0 comments on commit 16ec3e0

Please sign in to comment.