Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add browse on submit #32

Merged
merged 17 commits into from
Feb 26, 2023
51 changes: 38 additions & 13 deletions src/handlers/work/submit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const parameters = [
isBoolean : true,
description : 'When set, will continue even if the local repository is not clean.'
},
{
name : 'noBrowse',
isBoolean : true,
description : 'Supresses default behavior of opening a browser to the newly created pull request.'
},
{
name : 'noClosed',
isBoolean : true,
Expand Down Expand Up @@ -110,7 +115,7 @@ const func = ({ app, cache, model, reporter }) => async(req, res) => {
if (workUnit === undefined) throw createError.NotFound(`No such active unit of work '${workKey}' found.`)

const { dirtyOK, noPush = false } = req.vars
let { assignees, closes, closeTarget, noCloses, projects } = req.vars
let { assignees, closes, closeTarget, noBrowse = false, noCloses = false, projects } = req.vars

// determine assignee(s)
if (assignees === undefined) {
Expand Down Expand Up @@ -172,6 +177,8 @@ const func = ({ app, cache, model, reporter }) => async(req, res) => {
}
// we are ready to generate QA files and submit work

const prURLs = []
const prCalls = []
for (const { name: projectFQN, private: isPrivate } of projects) {
const [org, project] = projectFQN.split('/')
const projectPath = fsPath.join(app.liq.playground(), org, project)
Expand Down Expand Up @@ -201,6 +208,10 @@ const func = ({ app, cache, model, reporter }) => async(req, res) => {
if (isPrivate === true) { ([remote] = determineOriginAndMain({ projectPath, reporter })) }
else { remote = WORKSPACE }
tryExec(`cd '${projectPath}' && git push ${remote} ${workBranch}`)

for (const pr of openPRs) {
prURLs.push(`${GH_BASE_URL}/${org}/${project}/pull/${pr.number}`)
}
}
else { // we create the PR
reporter.push(`Creating PR for <em>${projectFQN}<rst> branch <code>${workBranch}<rst>...`)
Expand Down Expand Up @@ -231,18 +242,17 @@ const func = ({ app, cache, model, reporter }) => async(req, res) => {
const repoData = await octocache.request(`GET /repos/${org}/${project}`)
const base = repoData.default_branch

await octocache.request(
'POST /repos/{owner}/{repo}/pulls',
{
owner : org,
repo : project,
title : workUnit.description,
body,
head,
base
},
{ noClear : true } // should be OK
)
prCalls.push(doPR({ base, body, head, octocache, org, project, prURLs, workUnit }))
}
}

if (prCalls.length > 0) {
await Promise.all(prCalls)
}

if (noBrowse !== true) {
for (const url of prURLs) {
tryExec(`open ${url}`, { noThrow : true })
}
}

Expand All @@ -253,4 +263,19 @@ const func = ({ app, cache, model, reporter }) => async(req, res) => {
})
}

const doPR = async({ base, body, head, octocache, org, project, prURLs, workUnit }) => {
const pr = await octocache.request(
'POST /repos/{owner}/{repo}/pulls',
{
owner : org,
repo : project,
title : workUnit.description,
body,
head,
base
})

prURLs.push(`${GH_BASE_URL}/${org}/${project}/pull/${pr.number}`)
}

export { func, help, parameters, paths, method }