feat(pr create): add --json and --jq output#12622
Conversation
509440e to
42ed61f
Compare
|
Rebased this branch onto the latest This should address the stale Local verification run:
|
babakks
left a comment
There was a problem hiding this comment.
Thanks for the PR, @LouisLau-art, and sorry for the late review! 🙏
I haven't yet tried it to create a real PR, but the PR looks very good so that I'm mostly confident the changes are in the right direction. It just needs a few changes.
Once you update the branch, I'll go over the changes again and do proper end-to-end tests.
| createPullRequest(input: $input) { | ||
| pullRequest { | ||
| id | ||
| number | ||
| url | ||
| } |
There was a problem hiding this comment.
Please remote the extra tab:
| createPullRequest(input: $input) { | |
| pullRequest { | |
| id | |
| number | |
| url | |
| } | |
| createPullRequest(input: $input) { | |
| pullRequest { | |
| id | |
| number | |
| url | |
| } |
| opts.Body = "my body" | ||
| opts.HeadBranch = "feature" | ||
| exporter := cmdutil.NewJSONExporter() | ||
| exporter.SetFields([]string{"id", "url", "number"}) |
There was a problem hiding this comment.
Instead of the inline slice, it's better to use the createOutputFields variable in here, to make sure we're covering all fields here.
| exporter.SetFields([]string{"id", "url", "number"}) | |
| exporter.SetFields(createOutputFields) |
| Exporter cmdutil.Exporter | ||
| } | ||
|
|
||
| var createOutputFields = []string{"id", "url", "number"} |
There was a problem hiding this comment.
We need a test for this, similar to:
cli/pkg/cmd/pr/view/view_test.go
Lines 29 to 79 in 42ed61f
| { "data": { "createPullRequest": { "pullRequest": { | ||
| "URL": "https://github.com/OWNER/REPO/pull/12" | ||
| } } } }`, | ||
| { "data": { "createPullRequest": { "pullRequest": { | ||
| "ID": "PR_kwDOA", | ||
| "Number": 12, | ||
| "URL": "https://github.com/OWNER/REPO/pull/12" | ||
| } } } }`, |
There was a problem hiding this comment.
We need to also update all other test cases (lots of them, not just in this test function), to include the newly added number field. The correct notation should be number, but since we've used Pascal casing for ID and URL (I don't know why 🤷), I recommended keeping it (i.e. adding Number instead of number).
| setupJsonFlags(cmd, exportTarget, fields, true) | ||
| } | ||
|
|
||
| func AddJSONAndJQFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { |
There was a problem hiding this comment.
We need a godoc above this to avoid future confusion/questions:
| func AddJSONAndJQFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { | |
| // AddJSONAndJQFlags adds --json and --jq flags, but not --template. Shorthands are also omitted. | |
| // | |
| // Meant to be used in cases where --template (or -t) is already taken. | |
| func AddJSONAndJQFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { |
| var tplFlag *pflag.Flag | ||
| if withTemplateFlag { | ||
| tplFlag = f.Lookup("template") | ||
| } | ||
|
|
||
| templateValue := "" | ||
| if tplFlag != nil { | ||
| templateValue = tplFlag.Value.String() | ||
| } | ||
|
|
There was a problem hiding this comment.
Let's simplify this:
| var tplFlag *pflag.Flag | |
| if withTemplateFlag { | |
| tplFlag = f.Lookup("template") | |
| } | |
| templateValue := "" | |
| if tplFlag != nil { | |
| templateValue = tplFlag.Value.String() | |
| } | |
| var tplFlag *pflag.Flag | |
| var templateValue string | |
| if withTemplateFlag { | |
| tplFlag = f.Lookup("template") | |
| templateValue = tplFlag.Value.String() | |
| } |
| name string | ||
| fields []string | ||
| args []string | ||
| withTplFlag bool |
There was a problem hiding this comment.
Maybe name this field templateFlagTaken with a short comment on the intent:
| withTplFlag bool | |
| // templateFlagTaken indicates that the host command already has a --template flag for a different purpose (e.g. pr create). | |
| templateFlagTaken bool |
|
I'm closing this PR in favour of #13348 as seems to be stale now. |
Fixes #11247.
Summary
--jsonand--jqsupport togh pr createvia a newcmdutil.AddJSONAndJQFlagshelper (no JSON--templateflag).gh pr create --template <file>independent from JSON output mode.--dry-runtogether with--jsonforgh pr create.numberin theCreatePullRequestmutation response so--json numberworks.Test
go test ./pkg/cmdutil ./pkg/cmd/pr/create ./api