-
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.
Merge pull request #93 from ghinks/feature/close-draft-pr
- Loading branch information
Showing
17 changed files
with
468 additions
and
11 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 |
---|---|---|
@@ -1,13 +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 | ||
} | ||
] | ||
} |
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,29 @@ | ||
'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('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, pullRequest: true }] | ||
} | ||
: wiby.validate({ config: params.config }) | ||
|
||
const result = await wiby.closePR(config) | ||
// TODO, something more like the result process output | ||
const output = `${result.length} PRs closed` | ||
console.log(output) | ||
} |
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,49 @@ | ||
'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) { | ||
return | ||
} | ||
// TODO, in reality multiple checks could exist and they may not all have passed | ||
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,7 @@ | ||
query getExistingRepoBranches($owner: String!, $repo: String!) { | ||
repository(name: $repo, owner: $owner) { | ||
defaultBranchRef { | ||
name | ||
} | ||
} | ||
} |
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
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,43 @@ | ||
'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 wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') | ||
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) | ||
|
||
tap.test('closePRs command', async (t) => { | ||
t.beforeEach(async () => { | ||
nock.disableNetConnect() | ||
gitFixture.init() | ||
}) | ||
|
||
t.test('close-pr should fail if config and dependent provided', async (t) => { | ||
try { | ||
childProcess.execSync(`${wibyCommand} close-pr --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`) | ||
tap.fail() | ||
} catch (err) { | ||
tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive')) | ||
} | ||
}) | ||
t.test('close-pr should call and close the PR on command with dependent argument', async (t) => { | ||
const result = childProcess.execSync(`${wibyCommand} close-pr --dependent="https://github.com/wiby-test/fakeRepo"`, { | ||
env: { | ||
...process.env, | ||
NODE_OPTIONS: `-r ${fixturesPath}/http/close-pr-command-positive.js` | ||
} | ||
}).toString() | ||
t.match(result, '1 PRs closed\n') | ||
}) | ||
t.test('close-pr should call and close the PR on command using wiby.json settings', async (t) => { | ||
const result = childProcess.execSync(`${wibyCommand} close-pr`, { | ||
env: { | ||
...process.env, | ||
NODE_OPTIONS: `-r ${fixturesPath}/http/close-pr-command-positive.js` | ||
} | ||
}).toString() | ||
t.match(result, '1 PRs closed\n') | ||
}) | ||
}) |
Oops, something went wrong.