Skip to content

Commit

Permalink
Merge pull request #161 from zanerock/work-liquid-labs/liq-work/160
Browse files Browse the repository at this point in the history
Support creating issue on 'work start'
  • Loading branch information
zanerock authored Nov 28, 2023
2 parents 57340ca + 126c5ed commit ed3f4a3
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 25 deletions.
51 changes: 47 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@liquid-labs/condition-eval": "^1.0.0-alpha.17",
"@liquid-labs/federated-json": "^1.0.0-alpha.33",
"@liquid-labs/git-toolkit": "^1.0.0-alpha.14",
"@liquid-labs/github-toolkit": "^1.0.0-alpha.16",
"@liquid-labs/github-toolkit": "^1.0.0-alpha.17",
"@liquid-labs/http-smart-response": "^1.0.0-alpha.3",
"@liquid-labs/liq-handlers-lib": "^1.0.0-alpha.13",
"@liquid-labs/liq-projects-lib": "^1.0.0-alpha.12",
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/work/_lib/answer-set-to-md.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getGitHubOrgBasenameAndVersion } from '@liquid-labs/github-toolkit'
import { getGitHubOrgAndProjectBasename } from '@liquid-labs/github-toolkit'

const answerSetToMd = async({
app,
Expand Down Expand Up @@ -61,7 +61,7 @@ const answerSetToMd = async({
md += '\n\nRelated projects: '
md += (await Promise.all(otherProjects.map(async({ name: otherProjFQN }) => {
const { packageJSON: otherPkgJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(otherProjFQN)
const { org: otherGitHubOrg } = getGitHubOrgBasenameAndVersion({ pkgJSON : otherPkgJSON })
const { org: otherGitHubOrg } = getGitHubOrgAndProjectBasename({ pkgJSON : otherPkgJSON })

const [, otherProject] = otherProjFQN.split('/')
const projectURL = await app.ext.integrations.callHook({
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/work/_lib/determine-work-status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
determineOriginAndMain,
hasBranch
} from '@liquid-labs/git-toolkit'
import { getGitHubOrgBasenameAndVersion } from '@liquid-labs/github-toolkit'
import { getGitHubOrgAndProjectBasename } from '@liquid-labs/github-toolkit'
import { tryExec } from '@liquid-labs/shell-toolkit'

import { WORKSPACE } from './constants'
Expand Down Expand Up @@ -138,7 +138,7 @@ const generateProjectsReport = async({
projectStatus.pullRequests = []
reporter.push(`Retrieving pull requests associated with head '${workKey}'...`)
const { packageJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(projectFQN)
const { org: ghOrg, projectBasename } = getGitHubOrgBasenameAndVersion({ packageJSON })
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })
let reportPRs =
await octocache.paginate(`GET /repos/${ghOrg}/${projectBasename}/pulls`, { head : workKey, state : 'all' })
// As of 2023-03-08, the 'head' argument requires (for cross-repo merges) the name of the individual that
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/work/_lib/save-lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const saveFiles = async({ app, backupOnly, description, files, noBackup, reporte
if (impProj === undefined) {
const currDir = req.get('X-CWD')
npmName = (await getPackageJSON({ pkgDir : currDir })).name;
({ projectPath } = app.ext._liqProjects.playgroundMonitor.getProjectData(npmName))
({ projectPath } = await app.ext._liqProjects.playgroundMonitor.getProjectData(npmName))
}

return [npmName, f, projectPath]
Expand Down
6 changes: 2 additions & 4 deletions src/handlers/work/_lib/submit-lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fsPath from 'node:path'
import createError from 'http-errors'

import { determineOriginAndMain, verifyBranchInSync, verifyClean } from '@liquid-labs/git-toolkit'
import { getGitHubOrgBasenameAndVersion } from '@liquid-labs/github-toolkit'
import { getGitHubOrgAndProjectBasename } from '@liquid-labs/github-toolkit'
import { httpSmartResponse } from '@liquid-labs/http-smart-response'
import { cleanupQAFiles, runQA, saveQAFiles } from '@liquid-labs/liq-qa-lib'
import { tryExec } from '@liquid-labs/shell-toolkit'
Expand Down Expand Up @@ -159,7 +159,7 @@ const doSubmit = async({ all, app, cache, projects, reporter, req, res, workKey
const prURLs = []
for (const { name: projectFQN, private: isPrivate } of projects) {
const { packageJSON, projectPath } = await app.ext._liqProjects.playgroundMonitor.getProjectData(projectFQN)
const { org: gitHubOrg, projectBasename } = getGitHubOrgBasenameAndVersion({ packageJSON })
const { org: gitHubOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })

const qaFiles = await saveQAFiles({ projectPath, reporter })

Expand All @@ -186,8 +186,6 @@ const doSubmit = async({ all, app, cache, projects, reporter, req, res, workKey
const remote = setRemote({ isPrivate, projectPath })
tryExec(`cd '${projectPath}' && git push ${remote} ${workKey}`)

console.log('packageJSON:', packageJSON) // DEBUG

prURLs.push(...await app.ext.integrations.callHook({
providerFor : 'pull request',
providerArgs : { pkgJSON : packageJSON },
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/work/_lib/work-db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
workBranchName,
verifyIsOnBranch
} from '@liquid-labs/git-toolkit'
import { determineGitHubLogin, getGitHubOrgBasenameAndVersion } from '@liquid-labs/github-toolkit'
import { determineGitHubLogin, getGitHubOrgAndProjectBasename } from '@liquid-labs/github-toolkit'
import { crossLinkDevProjects } from '@liquid-labs/liq-projects-lib'
import { Octocache } from '@liquid-labs/octocache'
import { PLUGABLE_PLAYGROUND } from '@liquid-labs/plugable-defaults'
Expand Down Expand Up @@ -70,7 +70,7 @@ const WorkDB = class WorkDB {
const octocache = new Octocache({ authToken : this.#authToken })
for (const project of projects) {
const { packageJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(project)
const { org: ghOrg, projectBasename } = getGitHubOrgBasenameAndVersion({ packageJSON })
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })
const projectData = await octocache.request(`GET /repos/${ghOrg}/${projectBasename}`)
workData.projects.push({
name : project,
Expand Down Expand Up @@ -143,7 +143,7 @@ const WorkDB = class WorkDB {
reporter.push(`Processing work branch for <em>${project}<rst>...`)

const { packageJSON, projectPath } = await app.ext._liqProjects.playgroundMonitor.getProjectData(project)
const { org: ghOrg, projectBasename } = getGitHubOrgBasenameAndVersion({ packageJSON })
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })

let repoData
const ghProject = ghOrg + '/' + projectBasename
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/work/issues/_lib/add-lib.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { claimIssues, getGitHubOrgBasenameAndVersion, verifyIssuesAvailable } from '@liquid-labs/github-toolkit'
import { claimIssues, getGitHubOrgAndProjectBasename, verifyIssuesAvailable } from '@liquid-labs/github-toolkit'
import { httpSmartResponse } from '@liquid-labs/http-smart-response'
import { Octocache } from '@liquid-labs/octocache'

Expand All @@ -20,7 +20,7 @@ const doAddIssues = async({ app, cache, reporter, req, res, workKey }) => {
const primaryProject = workData.projects[0].name
const { packageJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(primaryProject)

const { org: ghOrg, projectBasename } = getGitHubOrgBasenameAndVersion({ packageJSON })
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })
issues = issues.map((i) => i.match(/^\d+$/) ? ghOrg + '/' + projectBasename + '/' + i : i)

await verifyIssuesAvailable({ authToken, issues, noAutoAssign, notClosed : true, reporter })
Expand Down
92 changes: 86 additions & 6 deletions src/handlers/work/start.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createError from 'http-errors'

import {
claimIssues,
createIssue,
determineGitHubLogin,
getGitHubOrgAndProjectBasename,
verifyIssuesAvailable
Expand All @@ -22,6 +23,27 @@ const help = {
const method = 'post'
const path = ['work', 'start']
const parameters = [
{
name : 'issueBug',
isBoolean : true,
description : "Labels the created issue as a 'bug' rather than the default 'enhancement'. Only valid when 'createIssues' is set."
},
{
name : 'issueDeliverables',
description : "A list of newline or a double semi-colon (';;') seperated deliverables items. Only valid when 'createIssue' is set."
},
{
name : 'issueNotes',
description : "Notes to include in the issue body. Only valid when 'createIssue' is set."
},
{
name : 'issueOverview',
description : "The overview text to use when creating an issue. Only valid if 'createIssue' is set."
},
{
name : 'issueTitle',
description : "Creates an issue with the given title. Requires 'issueOverview' and 'issueDeliverables' also be set."
},
{
name : 'projects',
isMultivalue : true,
Expand All @@ -36,7 +58,30 @@ Object.freeze(parameters)
const func = ({ app, cache, reporter }) => async(req, res) => {
reporter = reporter.isolate()

let { assignee, comment, issues, noAutoAssign = false, projects } = req.vars
const {
assignee,
comment,
issueBug,
issueDeliverables,
issueNotes,
issueOverview,
issueTitle,
noAutoAssign = false
} = req.vars
let { issues = [], projects } = req.vars

if (issues.length === 0 && issueTitle === undefined) {
throw createError.BadRequest("Must provides 'issues' or create an issue with 'issueTitle', etc.")
}

if (issueTitle !== undefined && (issueDeliverables === undefined || issueOverview === undefined)) {
throw createError.BadRequest("Parameters 'issueOverview' and 'issueDeliverables' must be provided when 'issueTitle' is set.")
}
else if (issueTitle === undefined
&& (issueBug !== undefined || issueDeliverables !== undefined || issueOverview !== undefined || issueNotes !== undefined)) {
throw createError.BadRequest("Parameters 'issueBug', 'issueOverview', 'issueDeliverables', and 'issueNotes' are only valid when 'issueTitle' is set.")
}

// First, let's process projects. If nothing specified, assume the current, implied project.
if (projects === undefined) {
const currPkgJSON = await getPackageJSON({ pkgDir : req.get('X-CWD') })
Expand All @@ -50,19 +95,54 @@ const func = ({ app, cache, reporter }) => async(req, res) => {
}
}

// technically, we dont't always need this, but we usually do; this is the data for the 'primary' project
const { packageJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(projects[0])
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })

const credDB = app.ext.credentialsDB
const authToken = await credDB.getToken('GITHUB_API')

if (issueTitle !== undefined) {
const labels = []
if (issueBug === true) {
labels.push('bug')
}
else {
labels.push('enhancement')
}

const deliverables = issueDeliverables.split(/(?:\n|;;)/)
const deliverablesText = '- [ ] ' + deliverables.join('\n- [ ] ')

let issueBody = `## Overview
${issueOverview}
## Deliverables
${deliverablesText}`

if (issueNotes !== undefined) {
issueBody += `
## Notes
${issueNotes}`
} // if (notes)

const projectFQN = ghOrg + '/' + projectBasename

const { number } = await createIssue({ authToken, projectFQN, title : issueTitle, body : issueBody, labels, reporter })
issues.unshift(number + '')
}// if (createIssue)

// Normalize issues as '<org>/<project>/<issue number>'
issues = await Promise.all(issues.map(async(i) => {
if (i.match(/^\d+$/)) {
const { packageJSON } = await app.ext._liqProjects.playgroundMonitor.getProjectData(projects[0])
const { org: ghOrg, projectBasename } = getGitHubOrgAndProjectBasename({ packageJSON })
return ghOrg + '/' + projectBasename + '/' + i
}
return i
}))

const credDB = app.ext.credentialsDB
const authToken = await credDB.getToken('GITHUB_API')

const githubLogin = (await determineGitHubLogin({ authToken })).login
// TODO: this should be an integration hook point
await verifyIssuesAvailable({ authToken, availableFor : githubLogin, issues, noAutoAssign, reporter })
Expand Down

0 comments on commit ed3f4a3

Please sign in to comment.