-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Allow creation of issue comments in cml (#1202) * Driver support for issue comments. * Create or update issue comments. * Add e2e issue comment creation tests for github and bitbucket drivers. * Add gitlab issue comment e2e test. * Add comment target flag and parsing (#1241) * Add target cli option. * Add comment target parsing. * Add debug logging in comment target parsing. Co-authored-by: DavidGOrtega <[email protected]> Co-authored-by: Casper da Costa-Luis <[email protected]>
- Loading branch information
1 parent
03e009f
commit 384cdb5
Showing
13 changed files
with
495 additions
and
78 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
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
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,75 @@ | ||
const winston = require('winston'); | ||
|
||
const SEPARATOR = '/'; | ||
|
||
async function parseCommentTarget(opts = {}) { | ||
const { commitSha: commit, pr, target, drv } = opts; | ||
|
||
let commentTarget = target; | ||
// Handle legacy comment target flags. | ||
if (commit) { | ||
drv.warn( | ||
`Deprecation warning: use --target="commit${SEPARATOR}<sha>" instead of --commit-sha=<sha>` | ||
); | ||
commentTarget = `commit${SEPARATOR}${commit}`; | ||
} | ||
if (pr) { | ||
drv.warn('Deprecation warning: use --target=pr instead of --pr'); | ||
commentTarget = 'pr'; | ||
} | ||
// Handle comment targets that are incomplete, e.g. 'pr' or 'commit'. | ||
let prNumber; | ||
let commitPr; | ||
switch (commentTarget.toLowerCase()) { | ||
case 'commit': | ||
winston.debug(`Comment target "commit" mapped to "commit/${drv.sha}"`); | ||
return { target: 'commit', commitSha: drv.sha }; | ||
case 'pr': | ||
case 'auto': | ||
// Determine PR id from forge env vars (if we're in a PR context). | ||
prNumber = drv.pr; | ||
if (prNumber) { | ||
winston.debug( | ||
`Comment target "${commentTarget}" mapped to "pr/${prNumber}"` | ||
); | ||
return { target: 'pr', prNumber: prNumber }; | ||
} | ||
// Or fallback to determining PR by HEAD commit. | ||
// TODO: handle issue with PR HEAD commit not matching source branch in github. | ||
[commitPr = {}] = await drv.commitPrs({ commitSha: drv.sha }); | ||
if (commitPr.url) { | ||
[prNumber] = commitPr.url.split('/').slice(-1); | ||
winston.debug( | ||
`Comment target "${commentTarget}" mapped to "pr/${prNumber}" based on commit "${drv.sha}"` | ||
); | ||
return { target: 'pr', prNumber }; | ||
} | ||
// If target is 'auto', fallback to issuing commit comments. | ||
if (commentTarget === 'auto') { | ||
winston.debug( | ||
`Comment target "${commentTarget}" mapped to "commit/${drv.sha}"` | ||
); | ||
return { target: 'commit', commitSha: drv.sha }; | ||
} | ||
throw new Error(`PR for commit sha "${drv.sha}" not found`); | ||
} | ||
// Handle qualified comment targets, e.g. 'issue/id'. | ||
const separatorPos = commentTarget.indexOf(SEPARATOR); | ||
if (separatorPos === -1) { | ||
throw new Error(`Failed to parse comment --target="${commentTarget}"`); | ||
} | ||
const targetType = commentTarget.slice(0, separatorPos); | ||
const id = commentTarget.slice(separatorPos + 1); | ||
switch (targetType.toLowerCase()) { | ||
case 'commit': | ||
return { target: targetType, commitSha: id }; | ||
case 'pr': | ||
return { target: targetType, prNumber: id }; | ||
case 'issue': | ||
return { target: targetType, issueId: id }; | ||
default: | ||
throw new Error(`unsupported comment target "${commentTarget}"`); | ||
} | ||
} | ||
|
||
module.exports = { parseCommentTarget }; |
Oops, something went wrong.