Skip to content

Commit

Permalink
feat: remove PR on test success
Browse files Browse the repository at this point in the history
change the pullRequest to be per dependent object
  • Loading branch information
ghinks committed Apr 15, 2021
1 parent a9f028d commit 562ace0
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
12 changes: 7 additions & 5 deletions .wiby.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"dependents": [
{
"repository": "https://github.com/wiby-test/partial"
"repository": "https://github.com/wiby-test/partial",
"pullRequest": false
},
{
"repository": "git://github.com/wiby-test/fail"
"repository": "git://github.com/wiby-test/fail",
"pullRequest": false
},
{
"repository": "git+https://github.com/wiby-test/pass"
"repository": "git+https://github.com/wiby-test/pass",
"pullRequest": true
}
],
"pullRequest": false
]
}
3 changes: 1 addition & 2 deletions bin/commands/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ exports.builder = (yargs) => yargs
exports.handler = async (params) => {
const config = params.dependent
? {
dependents: [{ repository: params.dependent }],
pullRequest: !!params['pull-request']
dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'] }]
}
: wiby.validate({ config: params.config })

Expand Down
3 changes: 1 addition & 2 deletions bin/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ exports.builder = (yargs) => yargs
exports.handler = (params) => {
const config = params.dependent
? {
dependents: [{ repository: params.dependent }],
pullRequest: !!params['pull-request']
dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'] }]
}
: wiby.validate({ config: params.config })

Expand Down
7 changes: 3 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ const dependentSchema = joi.object({
'git',
'git+https'
]
})
}),
pullRequest: joi.boolean().optional()
}).unknown(false)

exports.schema = joi.object({
dependents: joi.array().items(dependentSchema),
pullRequest: joi.boolean().optional()

dependents: joi.array().items(dependentSchema)
}).unknown(false)

exports.validate = ({ config: configFilePath }) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports.createPR = async function createPR (owner, repo, title, head, bas
})
}

module.exports.updatePRStatus = async function updatePRStatus (owner, repo, pullNumber) {
module.exports.closePR = async function closePR (owner, repo, pullNumber) {
return octokit.rest.pulls.update({
owner,
repo,
Expand Down
12 changes: 7 additions & 5 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze

const PENDING_RESULT_EXIT_CODE = 64

module.exports = async function ({ dependents, pullRequest }) {
module.exports = async function ({ dependents }) {
const parentPkgJSON = await context.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
const output = { status: pipelineStatusesEnum.PENDING, results: [] }
const allDependentsChecks = []

debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)

for (const { repository: url } of dependents) {
for (const { repository: url, pullRequest } of dependents) {
const dependentPkgInfo = gitURLParse(url)
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
Expand All @@ -37,7 +37,9 @@ 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)
await updatePRStatus(dependentPkgInfo.owner, dependentPkgInfo.name, branch, resp.data.check_runs)
if (pullRequest) {
await closePR(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 @@ -164,7 +166,7 @@ function resolveCodeAndExit (overallStatus) {
}
}

const updatePRStatus = module.exports.updatePRStatus = async function updatePRStatus (owner, repo, branch, checkRuns) {
const closePR = module.exports.closePR = async function closePR (owner, repo, branch, checkRuns) {
if (!checkRuns || checkRuns.length === 0) {
return
}
Expand All @@ -184,6 +186,6 @@ const updatePRStatus = module.exports.updatePRStatus = async function updatePRSt
return acc
}, [])
debug(`Dependent module: ${JSON.stringify(prsToClose, null, 2)}`)
const result = await prsToClose.map((pr) => github.updatePRStatus(owner, repo, pr.number))
const result = await prsToClose.map((pr) => github.closePR(owner, repo, pr.number))
return result
}
4 changes: 2 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const logger = require('./logger')
const testCommandNamespace = 'wiby:test'
const debug = logger(testCommandNamespace)

module.exports = async function ({ dependents, pullRequest }) {
module.exports = async function ({ dependents }) {
// enable log output for test command without DEBUG env
logger.enableLogs(testCommandNamespace)

Expand All @@ -21,7 +21,7 @@ module.exports = async function ({ dependents, pullRequest }) {
const parentDependencyLink = await context.getDependencyLink(parentRepositoryInfo.owner, parentRepositoryInfo.name, parentBranchName)
debug('Commit URL to test:', parentDependencyLink)

for (const { repository: url } of dependents) {
for (const { repository: url, pullRequest } of dependents) {
const dependentRepositoryInfo = gitURLParse(url)
const dependentPkgJson = await github.getPackageJson(dependentRepositoryInfo.owner, dependentRepositoryInfo.name)
debug(`Dependent module: ${dependentRepositoryInfo.owner}/${dependentRepositoryInfo.name}`)
Expand Down
12 changes: 7 additions & 5 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ tap.test('config validation', async (tap) => {
tap.strictSame(normalizedConfig, {
dependents: [
{
repository: 'https://github.com/wiby-test/partial'
repository: 'https://github.com/wiby-test/partial',
pullRequest: false
},
{
repository: 'git://github.com/wiby-test/fail'
repository: 'git://github.com/wiby-test/fail',
pullRequest: false
},
{
repository: 'git+https://github.com/wiby-test/pass'
repository: 'git+https://github.com/wiby-test/pass',
pullRequest: true
}
],
pullRequest: false
]
})
tap.end()
})
Expand Down
12 changes: 6 additions & 6 deletions test/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,26 @@ tap.test('wiby.result()', async (tap) => {
const branch = 'wiby-main'
t.plan(6)
t.test('no checkruns returns', async (t) => {
const result = await wiby.result.updatePRStatus(owner, repo, branch, null)
const result = await wiby.result.closePR(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: 'queued' }])
const result = await wiby.result.closePR(owner, repo, branch, [{ status: 'queued' }])
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: 'queued', conclusion: 'failure' }])
const result = await wiby.result.closePR(owner, repo, branch, [{ status: 'queued', conclusion: 'failure' }])
t.equal(result.length, 0)
})
t.test('no pull requests returns empty array', async (t) => {
const result = await wiby.result.updatePRStatus(owner, repo, branch, [{
const result = await wiby.result.closePR(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, [{
const result = await wiby.result.closePR(owner, repo, branch, [{
status: 'completed',
conclusion: 'success',
pull_requests: []
Expand All @@ -159,7 +159,7 @@ tap.test('wiby.result()', async (tap) => {
.reply(200, {
data: {}
})
const result = await wiby.result.updatePRStatus(owner, repo, branch, [{
const result = await wiby.result.closePR(owner, repo, branch, [{
status: 'completed',
conclusion: 'success',
pull_requests: [{
Expand Down

0 comments on commit 562ace0

Please sign in to comment.