Skip to content

Commit

Permalink
feat: raise PR on branch create
Browse files Browse the repository at this point in the history
raise a PR when the --pullrequest=true or if set in the wiby.json file.
  • Loading branch information
ghinks committed Apr 7, 2021
1 parent ef70cbb commit 761feef
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .wiby.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
{
"repository": "git+https://github.com/wiby-test/pass"
}
]
],
"pullrequest": false
}
11 changes: 10 additions & 1 deletion bin/commands/test.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: '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 }] }
? {
dependents: [{ repository: params.dependent }],
pullrequest: !!params.pullrequest
}
: wiby.validate({ config: params.config })

return wiby.test(config)
Expand Down
4 changes: 3 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const dependentSchema = joi.object({
}).unknown(false)

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

}).unknown(false)

exports.validate = ({ config: configFilePath }) => {
Expand Down
11 changes: 11 additions & 0 deletions lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,14 @@ module.exports.getCommitStatusesForRef = async function getCommitStatusesForRef
ref: branch
})
}

module.exports.createPR = async function createPR (owner, repo, title, head, base, draft) {
return octokit.pulls.create({
owner,
repo,
title,
head,
base,
draft
})
}
16 changes: 15 additions & 1 deletion 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 }) {
module.exports = async function ({ dependents, pullrequest }) {
// enable log output for test command without DEBUG env
logger.enableLogs(testCommandNamespace)

Expand All @@ -32,6 +32,9 @@ module.exports = async function ({ dependents }) {

const patchedPackageJSON = applyPatch(parentDependencyLink, parentPkgJSON.name, dependentPkgJson, parentPkgJSON.name)
await pushPatch(patchedPackageJSON, dependentRepositoryInfo.owner, dependentRepositoryInfo.name, parentPkgJSON.name, parentBranchName)
if (pullrequest) {
await createPR(dependentRepositoryInfo.owner, dependentRepositoryInfo.name, parentBranchName)
}
}
}

Expand All @@ -58,3 +61,14 @@ async function pushPatch (dependentPkgJson, dependentOwner, dependentRepo, paren
await github.createBranch(dependentOwner, dependentRepo, commitSha, branch)
debug(`Changes pushed to https://github.com/${dependentOwner}/${dependentRepo}/blob/${branch}/package.json`)
}

const createPR = module.exports.createPR = async function createPR (dependentOwner, dependentRepo, parentBranchName) {
const owner = dependentOwner
const repo = dependentRepo
const title = 'title #1'
const head = context.getTestingBranchName(parentBranchName)
const draft = true
const result = await github.createPR(owner, repo, title, head, parentBranchName, draft)
debug(`PR raised upon ${result.data.html_url}`)
return result
}
2 changes: 1 addition & 1 deletion test/cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tap.test('test command', async (tap) => {
})

tap.test('test command should call test module with dependent URI', async (tap) => {
const result = childProcess.execSync(`${wibyCommand} test --dependent="https://github.com/wiby-test/fakeRepo"`, {
const result = childProcess.execSync(`${wibyCommand} test --dependent="https://github.com/wiby-test/fakeRepo" --pullrequest=true`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js`
Expand Down
3 changes: 2 additions & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ tap.test('config validation', async (tap) => {
{
repository: 'git+https://github.com/wiby-test/pass'
}
]
],
pullrequest: false
})
tap.end()
})
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/http/test-command-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ function nockRepo (nockInstance, repo) {
// create branch in dependent
.post(`/repos/wiby-test/${repo}/git/refs`)
.reply(200, {})
// create PR when requested
.post(`/repos/wiby-test/${repo}/pulls`)
.reply(201, {
html_url: 'https://github.com/pkgjs/wiby/pull/1'
})
}

function buildNock () {
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ tap.test('wiby.test()', async (tap) => {
)
tap.end()
})
tap.test('test create PR', (t) => {
const htmlURL = `https://github.com/${CONFIG.PKG_ORG}/${CONFIG.DEP_REPO}/pull/1`
nock('https://api.github.com')
.post('/repos/pkgjs/wiby/pulls')
.reply(201, {
html_url: htmlURL
})
const dependentOwner = 'pkgjs'
const dependentRepo = 'wiby'
const parentBranchName = 'main'
wiby.test.createPR(dependentOwner, dependentRepo, parentBranchName)
.then((result) => {
t.equal(result.data.html_url, htmlURL)
t.end()
})
})
})

0 comments on commit 761feef

Please sign in to comment.