X Tutup
Skip to content

Commit 168bd33

Browse files
committed
pr command scriptability improvements
This commit improves behavior and error handling for pr commands when run unattached to a tty. - error if pr create and no -t/-f - error if pr create and -w - machine readable pr list - machine readable pr view - various cleanup of informational messages
1 parent e18776c commit 168bd33

File tree

7 files changed

+679
-38
lines changed

7 files changed

+679
-38
lines changed

command/pr.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ func prList(cmd *cobra.Command, args []string) error {
293293
})
294294

295295
title := listHeader(ghrepo.FullName(baseRepo), "pull request", len(listResult.PullRequests), listResult.TotalCount, hasFilters)
296-
// TODO: avoid printing header if piped to a script
297-
fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title)
296+
if connectedToTerminal(cmd) {
297+
fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title)
298+
}
298299

299300
table := utils.NewTablePrinter(cmd.OutOrStdout())
300301
for _, pr := range listResult.PullRequests {
@@ -303,6 +304,10 @@ func prList(cmd *cobra.Command, args []string) error {
303304
prNum = "#" + prNum
304305
}
305306
table.AddField(prNum, nil, colorFuncForPR(pr))
307+
if !table.IsTTY() {
308+
table.AddField(pr.State, nil, nil)
309+
table.AddField(prReadyState(&pr), nil, nil)
310+
}
306311
table.AddField(replaceExcessiveWhitespace(pr.Title), nil, nil)
307312
table.AddField(pr.HeadLabel(), nil, utils.Cyan)
308313
table.EndRow()
@@ -357,6 +362,10 @@ func prView(cmd *cobra.Command, args []string) error {
357362
return err
358363
}
359364

365+
if web && !connectedToTerminal(cmd) {
366+
return errors.New("--web unsupported when not attached to a tty")
367+
}
368+
360369
pr, _, err := prFromArgs(ctx, apiClient, cmd, args)
361370
if err != nil {
362371
return err
@@ -367,8 +376,13 @@ func prView(cmd *cobra.Command, args []string) error {
367376
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
368377
return utils.OpenInBrowser(openURL)
369378
}
370-
out := colorableOut(cmd)
371-
return printPrPreview(out, pr)
379+
380+
if connectedToTerminal(cmd) {
381+
out := colorableOut(cmd)
382+
return printHumanPrPreview(out, pr)
383+
}
384+
385+
return printRawPrPreview(cmd.OutOrStdout(), pr)
372386
}
373387

374388
func prClose(cmd *cobra.Command, args []string) error {
@@ -482,6 +496,9 @@ func prMerge(cmd *cobra.Command, args []string) error {
482496
}
483497

484498
if enabledFlagCount == 0 {
499+
if !connectedToTerminal(cmd) {
500+
return errors.New("--merge, --rebase, or --squash required when not attached to a tty")
501+
}
485502
isInteractive = true
486503
} else if enabledFlagCount > 1 {
487504
return errors.New("expected exactly one of --merge, --rebase, or --squash to be true")
@@ -513,7 +530,9 @@ func prMerge(cmd *cobra.Command, args []string) error {
513530
return fmt.Errorf("API call failed: %w", err)
514531
}
515532

516-
fmt.Fprintf(colorableOut(cmd), "%s %s pull request #%d (%s)\n", utils.Magenta("✔"), action, pr.Number, pr.Title)
533+
if connectedToTerminal(cmd) {
534+
fmt.Fprintf(colorableOut(cmd), "%s %s pull request #%d (%s)\n", utils.Magenta("✔"), action, pr.Number, pr.Title)
535+
}
517536

518537
if deleteBranch {
519538
branchSwitchString := ""
@@ -560,7 +579,9 @@ func prMerge(cmd *cobra.Command, args []string) error {
560579
}
561580
}
562581

563-
fmt.Fprintf(colorableOut(cmd), "%s Deleted branch %s%s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName), branchSwitchString)
582+
if connectedToTerminal(cmd) {
583+
fmt.Fprintf(colorableOut(cmd), "%s Deleted branch %s%s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName), branchSwitchString)
584+
}
564585
}
565586

566587
return nil
@@ -620,7 +641,46 @@ func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool) (api.PullReque
620641
return mergeMethod, deleteBranch, nil
621642
}
622643

623-
func printPrPreview(out io.Writer, pr *api.PullRequest) error {
644+
func prReadyState(pr *api.PullRequest) string {
645+
switch pr.State {
646+
case "OPEN":
647+
if pr.IsDraft {
648+
return "draft"
649+
} else {
650+
return "ready"
651+
}
652+
case "CLOSED":
653+
return "unreviewable"
654+
case "MERGED":
655+
return "unreviewable"
656+
}
657+
658+
return "unknown"
659+
}
660+
661+
func printRawPrPreview(out io.Writer, pr *api.PullRequest) error {
662+
reviewers := prReviewerList(*pr)
663+
assignees := prAssigneeList(*pr)
664+
labels := prLabelList(*pr)
665+
projects := prProjectList(*pr)
666+
667+
fmt.Fprintf(out, "title:\t%s\n", pr.Title)
668+
fmt.Fprintf(out, "state:\t%s\n", pr.State)
669+
fmt.Fprintf(out, "ready:\t%s\n", prReadyState(pr))
670+
fmt.Fprintf(out, "author:\t%s\n", pr.Author.Login)
671+
fmt.Fprintf(out, "labels:\t%s\n", labels)
672+
fmt.Fprintf(out, "assignees:\t%s\n", assignees)
673+
fmt.Fprintf(out, "reviewers:\t%s\n", reviewers)
674+
fmt.Fprintf(out, "projects:\t%s\n", projects)
675+
fmt.Fprintf(out, "milestone:\t%s\n", pr.Milestone.Title)
676+
677+
fmt.Fprintln(out, "--")
678+
fmt.Fprintln(out, pr.Body)
679+
680+
return nil
681+
}
682+
683+
func printHumanPrPreview(out io.Writer, pr *api.PullRequest) error {
624684
// Header (Title and State)
625685
fmt.Fprintln(out, utils.Bold(pr.Title))
626686
fmt.Fprintf(out, "%s", prStateTitleWithColor(*pr))

command/pr_create.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ func prCreate(cmd *cobra.Command, _ []string) error {
205205
message = "\nCreating draft pull request for %s into %s in %s\n\n"
206206
}
207207

208-
fmt.Fprintf(colorableErr(cmd), message,
209-
utils.Cyan(headBranch),
210-
utils.Cyan(baseBranch),
211-
ghrepo.FullName(baseRepo))
212-
if (title == "" || body == "") && defaultsErr != nil {
213-
fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %s\n", utils.Yellow("!"), defaultsErr)
208+
if connectedToTerminal(cmd) {
209+
fmt.Fprintf(colorableErr(cmd), message,
210+
utils.Cyan(headBranch),
211+
utils.Cyan(baseBranch),
212+
ghrepo.FullName(baseRepo))
213+
if (title == "" || body == "") && defaultsErr != nil {
214+
fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %s\n", utils.Yellow("!"), defaultsErr)
215+
}
214216
}
215217
}
216218

@@ -223,7 +225,16 @@ func prCreate(cmd *cobra.Command, _ []string) error {
223225
Milestones: milestoneTitles,
224226
}
225227

226-
interactive := !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body"))
228+
if !connectedToTerminal(cmd) {
229+
if isWeb {
230+
return errors.New("--web unsupported when not attached to a tty")
231+
}
232+
if !cmd.Flags().Changed("title") && !autofill {
233+
return errors.New("--title or --fill required when not attached to a tty")
234+
}
235+
}
236+
237+
interactive := connectedToTerminal(cmd) && !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body"))
227238

228239
if !isWeb && !autofill && interactive {
229240
var nonLegacyTemplateFiles []string

0 commit comments

Comments
 (0)
X Tutup