-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new feature flag... for PR removal
- Loading branch information
Showing
9 changed files
with
292 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const wiby = require('../..') | ||
|
||
exports.desc = 'Use this command to close the PRs raised against your dependents. wiby will go off to the dependent’s repo and close the PRs raised that trigger jobs `package.json` pointing to your latest version (with the new changes) triggering the dependent’s CI to run.' | ||
|
||
exports.builder = (yargs) => yargs | ||
.option('dependent', { | ||
desc: 'URL of a dependent', | ||
type: 'string', | ||
conflicts: 'config' | ||
}) | ||
.option('pull-request', { | ||
desc: 'Raise a draft PR in addition to creating a branch', | ||
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 = (params) => { | ||
const config = params.dependent | ||
? { | ||
dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'] }] | ||
} | ||
: wiby.validate({ config: params.config }) | ||
|
||
return wiby.closePR(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const github = require('../lib/github') | ||
const logger = require('./logger') | ||
const context = require('./context') | ||
const gitURLParse = require('git-url-parse') | ||
|
||
const debug = logger('wiby:closepr') | ||
|
||
module.exports = async ({ dependents }) => { | ||
const closedPrs = []; | ||
for (const { repository: url, pullRequest } of dependents) { | ||
if (pullRequest) { | ||
const dependentPkgInfo = gitURLParse(url) | ||
const parentBranchName = await context.getParentBranchName() | ||
const branch = await context.getTestingBranchName(parentBranchName) | ||
const resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch) | ||
const closedPR = await closeDependencyPR(dependentPkgInfo.owner, dependentPkgInfo.name, branch, resp.data.check_runs) | ||
if (!!closedPR) { | ||
closedPrs.push(closedPR) | ||
} | ||
} | ||
} | ||
return closedPrs; | ||
} | ||
|
||
const closeDependencyPR = module.exports.closeDependencyPR = async function closeDependencyPR (owner, repo, branch, checkRuns) { | ||
if (!checkRuns || checkRuns.length === 0) { | ||
return | ||
} | ||
const prsToClose = checkRuns.reduce((acc, check) => { | ||
if (check.status === 'completed' && | ||
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 await Promise.all(prsToClose.map((pr) => github.closePR(owner, repo, pr.number))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
/* | ||
const tap = require('tap') | ||
const gitFixture = require('../fixtures/git') | ||
const childProcess = require('child_process') | ||
const nock = require('nock') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') | ||
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) | ||
tap.test('closePRs command', async (tap) => { | ||
tap.beforeEach(async () => { | ||
nock.disableNetConnect() | ||
gitFixture.init() | ||
}) | ||
tap.afterEach(async () => { | ||
nock.cleanAll() | ||
nock.enableNetConnect() | ||
}) | ||
tap.test('close PRs should result in a successful outcome', async (t) => { | ||
}) | ||
tap.end() | ||
}) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
const nock = require('nock') | ||
const tap = require('tap') | ||
const context = require('../lib/context') | ||
const gitFixture = require('./fixtures/git') | ||
const closePR = require('../lib/closePR') | ||
|
||
tap.test('close PR', (t) => { | ||
t.test('checks on internal call to github', (t) => { | ||
const owner = 'pkgjs' | ||
const repo = 'wiby' | ||
const branch = 'wiby-main' | ||
t.plan(6) | ||
t.test('no checkruns returns', async (t) => { | ||
const result = await closePR.closeDependencyPR(owner, repo, branch, null) | ||
t.equal(result, undefined) | ||
}) | ||
t.test('status not completed returns empty array', async (t) => { | ||
const result = await closePR.closeDependencyPR(owner, repo, branch, [{ status: 'queued' }]) | ||
t.equal(result.length, 0) | ||
}) | ||
t.test('conclusion not a success returns empty array', async (t) => { | ||
const result = await closePR.closeDependencyPR(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 closePR.closeDependencyPR(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 closePR.closeDependencyPR(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 closePR.closeDependencyPR(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) | ||
}) | ||
}) | ||
t.test('closePR Cli function tests', (t) => { | ||
t.plan(2) | ||
t.test('closePR should not close PRs', async(t) => { | ||
//nock setup | ||
nock('https://api.github.com') | ||
.get(/repos.*check\-runs/) | ||
.reply(200, { | ||
data: {} | ||
}) | ||
const result = await closePR({ | ||
dependents: [ | ||
{ | ||
repository: 'https://github.com/wiby-test/fakeRepo', | ||
pullRequest: true | ||
} | ||
] | ||
}) | ||
|
||
t.equal(result.length, 0) | ||
}) | ||
t.test('closePR should close a single PR', async(t) => { | ||
gitFixture.init() | ||
const branch = context.getTestingBranchName(await context.getParentBranchName()) | ||
//nock setup | ||
nock('https://api.github.com') | ||
.get(/repos.*check\-runs/) | ||
.reply(200, { | ||
check_runs: [ | ||
{ | ||
status: 'completed', | ||
conclusion: 'success', | ||
pull_requests: [{ | ||
number: 1, | ||
head: { | ||
ref: branch | ||
} | ||
}, { | ||
number: 2, | ||
head: { | ||
ref: 'any-other-branch' | ||
} | ||
}] | ||
} | ||
] | ||
}) | ||
.patch(/repos.*pulls\/1/) | ||
.reply(200, { | ||
state: 'closed', | ||
title: 'wiby test pr' | ||
}) | ||
const result = await closePR({ | ||
dependents: [ | ||
{ | ||
repository: 'https://github.com/wiby-test/fakeRepo1' | ||
}, | ||
{ | ||
repository: 'https://github.com/wiby-test/fakeRepo2', | ||
pullRequest: true | ||
} | ||
] | ||
}) | ||
|
||
t.equal(result.length, 1) | ||
}) | ||
}) | ||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
/* | ||
Mocks of HTTP calls for close-pr command | ||
*/ | ||
const nock = require('nock') | ||
|
||
nock.disableNetConnect() | ||
|
||
function nockRepo (nockInstance, owner, repo, branch) { | ||
return nockInstance | ||
// /repos/{owner}/{repo}/commits/{ref}/check-runs | ||
.get(`repos/${owner}/${repo}/commits/${branch}/check-runs`) | ||
.reply(200, { | ||
check_runs: [ | ||
{ | ||
status: 'completed', | ||
conclusion: 'success', | ||
pull_requests: [{ | ||
number: 1, | ||
head: { | ||
ref: branch | ||
} | ||
}, { | ||
status: 'completed', | ||
conclusion: 'success', | ||
number: 2, | ||
head: { | ||
ref: 'any-other-branch' | ||
} | ||
}] | ||
} | ||
] | ||
}) | ||
.patch(/repos.*pulls\/1/) | ||
.reply(200, { | ||
state: 'closed', | ||
title: 'wiby test pr' | ||
}) | ||
} | ||
|
||
function buildNock () { | ||
let nockInstance = nock('https://api.github.com') | ||
|
||
nockInstance = nockRepo(nockInstance, 'fakeRepo') | ||
|
||
return nockInstance | ||
} | ||
|
||
buildNock() |
Oops, something went wrong.