-
Notifications
You must be signed in to change notification settings - Fork 343
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 comment target flag and parsing #1241
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e70fc38
Add target cli option.
tasdomas 2324313
Add comment target parsing.
tasdomas 17512f1
Update driver code.
tasdomas 5b1deb4
Use comment target parsing.
tasdomas 6b99ce9
Remove --issue flag for comment create.
tasdomas 85281d9
Handle --target=auto for comment create.
tasdomas f1f4f62
Remove default commitSha flag value.
tasdomas 4e15890
Remove comment --target flag conflicting flag list.
tasdomas dc43ca0
Silent default for --target flag.
tasdomas acae963
Fix passing of commit flag value to comment target.
tasdomas edc1b7c
Merge branch 'feature-comment-target' into d009-comment-target
tasdomas c0d9761
Fix comment create cli param definition.
tasdomas 5d2785a
Fix handling of commit comments.
tasdomas e7a7996
Address review comments.
tasdomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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( | ||
'cml: the --commitSha flag will be deprecated, please use --target="commit#<sha>"' | ||
); | ||
commentTarget = `commit#${commit}`; | ||
} | ||
if (pr) { | ||
drv.warn('cml: the --pr flag will be deprecated, please use --target="pr"'); | ||
commentTarget = 'pr'; | ||
} | ||
// Handle comment targets that are incomplete, e.g. 'pr' or 'commit'. | ||
let prId; | ||
let commitPr; | ||
switch (commentTarget) { | ||
case 'commit': | ||
return { target: 'commit', commitSha: drv.sha }; | ||
case 'pr': | ||
case 'auto': | ||
// Determine PR id from forge env vars (if we're in a PR context). | ||
prId = drv.pr; | ||
if (prId) { | ||
return { target: 'pr', prNumber: prId }; | ||
tasdomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// 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) { | ||
[prId] = commitPr.url.split('/').slice(-1); | ||
return { target: 'pr', prNumber: prId }; | ||
} | ||
// If target is 'auto', fallback to issuing commit comments. | ||
if (commentTarget === 'auto') { | ||
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(`comment target "${commentTarget}" could not be parsed`); | ||
} | ||
const targetType = commentTarget.slice(0, separatorPos); | ||
const id = commentTarget.slice(separatorPos + 1); | ||
switch (targetType) { | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what syntaxes are supported?
123
#123
issue#123
pr#123
mr#123
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid targets are:
auto
determine comment target automatically:pr
- determine pr by context or head commit (making allowances for how github's PR merge commits)pr#123
- specific PR numbercommit
- current HEAD commitcommit#adfa12
- specific commitissue#123
- specific issueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need:#123
to auto-infer pr-or-issuemr
alias forpr
mr#123
alias forpr#123
why do we havecommit#d34db3
? Doesn't--commit-sha=d34db3
already cover this?full URL (e.g. if they want to target another PR returned bycml pr
)moved to #1228 (review)