Skip to content

Commit 3ac7329

Browse files
committed
Isolate pr status command
1 parent 7cc74c5 commit 3ac7329

17 files changed

+610
-518
lines changed

command/issue.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cli/cli/pkg/cmd/pr/shared"
1616
"github.com/cli/cli/pkg/cmdutil"
1717
"github.com/cli/cli/pkg/githubtemplate"
18+
"github.com/cli/cli/pkg/text"
1819
"github.com/cli/cli/utils"
1920
"github.com/spf13/cobra"
2021
"github.com/spf13/pflag"
@@ -298,28 +299,28 @@ func issueStatus(cmd *cobra.Command, args []string) error {
298299
fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo))
299300
fmt.Fprintln(out, "")
300301

301-
printHeader(out, "Issues assigned to you")
302+
shared.PrintHeader(out, "Issues assigned to you")
302303
if issuePayload.Assigned.TotalCount > 0 {
303304
printIssues(out, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues)
304305
} else {
305306
message := " There are no issues assigned to you"
306-
printMessage(out, message)
307+
shared.PrintMessage(out, message)
307308
}
308309
fmt.Fprintln(out)
309310

310-
printHeader(out, "Issues mentioning you")
311+
shared.PrintHeader(out, "Issues mentioning you")
311312
if issuePayload.Mentioned.TotalCount > 0 {
312313
printIssues(out, " ", issuePayload.Mentioned.TotalCount, issuePayload.Mentioned.Issues)
313314
} else {
314-
printMessage(out, " There are no issues mentioning you")
315+
shared.PrintMessage(out, " There are no issues mentioning you")
315316
}
316317
fmt.Fprintln(out)
317318

318-
printHeader(out, "Issues opened by you")
319+
shared.PrintHeader(out, "Issues opened by you")
319320
if issuePayload.Authored.TotalCount > 0 {
320321
printIssues(out, " ", issuePayload.Authored.TotalCount, issuePayload.Authored.Issues)
321322
} else {
322-
printMessage(out, " There are no issues opened by you")
323+
shared.PrintMessage(out, " There are no issues opened by you")
323324
}
324325
fmt.Fprintln(out)
325326

@@ -713,7 +714,7 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue)
713714
if !table.IsTTY() {
714715
table.AddField(issue.State, nil, nil)
715716
}
716-
table.AddField(replaceExcessiveWhitespace(issue.Title), nil, nil)
717+
table.AddField(text.ReplaceExcessiveWhitespace(issue.Title), nil, nil)
717718
table.AddField(labels, nil, utils.Gray)
718719
if table.IsTTY() {
719720
table.AddField(utils.FuzzyAgo(ago), nil, utils.Gray)

command/pr.go

Lines changed: 1 addition & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package command
22

33
import (
4-
"errors"
54
"fmt"
6-
"io"
7-
"regexp"
85
"strconv"
9-
"strings"
106

117
"github.com/MakeNowJust/heredoc"
128
"github.com/cli/cli/api"
13-
"github.com/cli/cli/context"
14-
"github.com/cli/cli/git"
159
"github.com/cli/cli/internal/ghrepo"
1610
"github.com/cli/cli/pkg/cmd/pr/shared"
1711
"github.com/cli/cli/pkg/cmdutil"
@@ -26,7 +20,6 @@ func init() {
2620

2721
RootCmd.AddCommand(prCmd)
2822
prCmd.AddCommand(prCreateCmd)
29-
prCmd.AddCommand(prStatusCmd)
3023
prCmd.AddCommand(prCloseCmd)
3124
prCmd.AddCommand(prReopenCmd)
3225
prCmd.AddCommand(prReadyCmd)
@@ -68,12 +61,6 @@ var prListCmd = &cobra.Command{
6861
`),
6962
RunE: prList,
7063
}
71-
var prStatusCmd = &cobra.Command{
72-
Use: "status",
73-
Short: "Show status of relevant pull requests",
74-
Args: cmdutil.NoArgsQuoteReminder,
75-
RunE: prStatus,
76-
}
7764
var prCloseCmd = &cobra.Command{
7865
Use: "close {<number> | <url> | <branch>}",
7966
Short: "Close a pull request",
@@ -93,72 +80,6 @@ var prReadyCmd = &cobra.Command{
9380
RunE: prReady,
9481
}
9582

96-
func prStatus(cmd *cobra.Command, args []string) error {
97-
ctx := contextForCommand(cmd)
98-
apiClient, err := apiClientForContext(ctx)
99-
if err != nil {
100-
return err
101-
}
102-
103-
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
104-
if err != nil {
105-
return err
106-
}
107-
108-
repoOverride, _ := cmd.Flags().GetString("repo")
109-
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
110-
111-
if err != nil && repoOverride == "" && !errors.Is(err, git.ErrNotOnAnyBranch) {
112-
return fmt.Errorf("could not query for pull request for current branch: %w", err)
113-
}
114-
115-
// the `@me` macro is available because the API lookup is ElasticSearch-based
116-
currentUser := "@me"
117-
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)
118-
if err != nil {
119-
return err
120-
}
121-
122-
out := colorableOut(cmd)
123-
124-
fmt.Fprintln(out, "")
125-
fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo))
126-
fmt.Fprintln(out, "")
127-
128-
printHeader(out, "Current branch")
129-
currentPR := prPayload.CurrentPR
130-
currentBranch, _ := ctx.Branch()
131-
if currentPR != nil && currentPR.State != "OPEN" && prPayload.DefaultBranch == currentBranch {
132-
currentPR = nil
133-
}
134-
if currentPR != nil {
135-
printPrs(out, 1, *currentPR)
136-
} else if currentPRHeadRef == "" {
137-
printMessage(out, " There is no current branch")
138-
} else {
139-
printMessage(out, fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")))
140-
}
141-
fmt.Fprintln(out)
142-
143-
printHeader(out, "Created by you")
144-
if prPayload.ViewerCreated.TotalCount > 0 {
145-
printPrs(out, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...)
146-
} else {
147-
printMessage(out, " You have no open pull requests")
148-
}
149-
fmt.Fprintln(out)
150-
151-
printHeader(out, "Requesting a code review from you")
152-
if prPayload.ReviewRequested.TotalCount > 0 {
153-
printPrs(out, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...)
154-
} else {
155-
printMessage(out, " You have no pull requests to review")
156-
}
157-
fmt.Fprintln(out)
158-
159-
return nil
160-
}
161-
16283
func prList(cmd *cobra.Command, args []string) error {
16384
ctx := contextForCommand(cmd)
16485
apiClient, err := apiClientForContext(ctx)
@@ -271,7 +192,7 @@ func prList(cmd *cobra.Command, args []string) error {
271192
prNum = "#" + prNum
272193
}
273194
table.AddField(prNum, nil, shared.ColorFuncForPR(pr))
274-
table.AddField(replaceExcessiveWhitespace(pr.Title), nil, nil)
195+
table.AddField(text.ReplaceExcessiveWhitespace(pr.Title), nil, nil)
275196
table.AddField(pr.HeadLabel(), nil, utils.Cyan)
276197
if !table.IsTTY() {
277198
table.AddField(prStateWithDraft(&pr), nil, nil)
@@ -385,124 +306,3 @@ func prReady(cmd *cobra.Command, args []string) error {
385306

386307
return nil
387308
}
388-
389-
func prSelectorForCurrentBranch(ctx context.Context, baseRepo ghrepo.Interface) (prNumber int, prHeadRef string, err error) {
390-
prHeadRef, err = ctx.Branch()
391-
if err != nil {
392-
return
393-
}
394-
branchConfig := git.ReadBranchConfig(prHeadRef)
395-
396-
// the branch is configured to merge a special PR head ref
397-
prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`)
398-
if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil {
399-
prNumber, _ = strconv.Atoi(m[1])
400-
return
401-
}
402-
403-
var branchOwner string
404-
if branchConfig.RemoteURL != nil {
405-
// the branch merges from a remote specified by URL
406-
if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil {
407-
branchOwner = r.RepoOwner()
408-
}
409-
} else if branchConfig.RemoteName != "" {
410-
// the branch merges from a remote specified by name
411-
rem, _ := ctx.Remotes()
412-
if r, err := rem.FindByName(branchConfig.RemoteName); err == nil {
413-
branchOwner = r.RepoOwner()
414-
}
415-
}
416-
417-
if branchOwner != "" {
418-
if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") {
419-
prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/")
420-
}
421-
// prepend `OWNER:` if this branch is pushed to a fork
422-
if !strings.EqualFold(branchOwner, baseRepo.RepoOwner()) {
423-
prHeadRef = fmt.Sprintf("%s:%s", branchOwner, prHeadRef)
424-
}
425-
}
426-
427-
return
428-
}
429-
430-
func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
431-
for _, pr := range prs {
432-
prNumber := fmt.Sprintf("#%d", pr.Number)
433-
434-
prStateColorFunc := utils.Green
435-
if pr.IsDraft {
436-
prStateColorFunc = utils.Gray
437-
} else if pr.State == "MERGED" {
438-
prStateColorFunc = utils.Magenta
439-
} else if pr.State == "CLOSED" {
440-
prStateColorFunc = utils.Red
441-
}
442-
443-
fmt.Fprintf(w, " %s %s %s", prStateColorFunc(prNumber), text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]"))
444-
445-
checks := pr.ChecksStatus()
446-
reviews := pr.ReviewStatus()
447-
448-
if pr.State == "OPEN" {
449-
reviewStatus := reviews.ChangesRequested || reviews.Approved || reviews.ReviewRequired
450-
if checks.Total > 0 || reviewStatus {
451-
// show checks & reviews on their own line
452-
fmt.Fprintf(w, "\n ")
453-
}
454-
455-
if checks.Total > 0 {
456-
var summary string
457-
if checks.Failing > 0 {
458-
if checks.Failing == checks.Total {
459-
summary = utils.Red("× All checks failing")
460-
} else {
461-
summary = utils.Red(fmt.Sprintf("× %d/%d checks failing", checks.Failing, checks.Total))
462-
}
463-
} else if checks.Pending > 0 {
464-
summary = utils.Yellow("- Checks pending")
465-
} else if checks.Passing == checks.Total {
466-
summary = utils.Green("✓ Checks passing")
467-
}
468-
fmt.Fprint(w, summary)
469-
}
470-
471-
if checks.Total > 0 && reviewStatus {
472-
// add padding between checks & reviews
473-
fmt.Fprint(w, " ")
474-
}
475-
476-
if reviews.ChangesRequested {
477-
fmt.Fprint(w, utils.Red("+ Changes requested"))
478-
} else if reviews.ReviewRequired {
479-
fmt.Fprint(w, utils.Yellow("- Review required"))
480-
} else if reviews.Approved {
481-
fmt.Fprint(w, utils.Green("✓ Approved"))
482-
}
483-
} else {
484-
fmt.Fprintf(w, " - %s", shared.StateTitleWithColor(pr))
485-
}
486-
487-
fmt.Fprint(w, "\n")
488-
}
489-
remaining := totalCount - len(prs)
490-
if remaining > 0 {
491-
fmt.Fprintf(w, utils.Gray(" And %d more\n"), remaining)
492-
}
493-
}
494-
495-
func printHeader(w io.Writer, s string) {
496-
fmt.Fprintln(w, utils.Bold(s))
497-
}
498-
499-
func printMessage(w io.Writer, s string) {
500-
fmt.Fprintln(w, utils.Gray(s))
501-
}
502-
503-
func replaceExcessiveWhitespace(s string) string {
504-
s = strings.TrimSpace(s)
505-
s = regexp.MustCompile(`\r?\n`).ReplaceAllString(s, " ")
506-
s = regexp.MustCompile(`\s{2,}`).ReplaceAllString(s, " ")
507-
return s
508-
}

0 commit comments

Comments
 (0)