From d64dcdca95c8705750489fb94534872b78c0d131 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 29 Jan 2020 12:00:04 -0600 Subject: [PATCH 1/5] print repo info for status commands --- api/queries_issue.go | 14 +++++++++++--- api/queries_pr.go | 11 +++++++++++ command/issue.go | 8 ++++++++ command/pr.go | 9 ++++++++- context/formatted.go | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 context/formatted.go diff --git a/api/queries_issue.go b/api/queries_issue.go index 934467e05..96f3574d4 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -8,9 +8,10 @@ import ( ) type IssuesPayload struct { - Assigned IssuesAndTotalCount - Mentioned IssuesAndTotalCount - Authored IssuesAndTotalCount + ParentRepo string + Assigned IssuesAndTotalCount + Mentioned IssuesAndTotalCount + Authored IssuesAndTotalCount } type IssuesAndTotalCount struct { @@ -108,6 +109,9 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) TotalCount int Nodes []Issue } + Parent struct { + NameWithOwner string + } HasIssuesEnabled bool } } @@ -115,6 +119,9 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) query := fragments + ` query($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { + parent { + nameWithOwner + } hasIssuesEnabled assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount @@ -154,6 +161,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) } payload := IssuesPayload{ + ParentRepo: resp.Repository.Parent.NameWithOwner, Assigned: IssuesAndTotalCount{ Issues: resp.Repository.Assigned.Nodes, TotalCount: resp.Repository.Assigned.TotalCount, diff --git a/api/queries_pr.go b/api/queries_pr.go index 734245acb..6daaa1c85 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -8,6 +8,7 @@ import ( ) type PullRequestsPayload struct { + ParentRepo string ViewerCreated PullRequestAndTotalCount ReviewRequested PullRequestAndTotalCount CurrentPR *PullRequest @@ -139,6 +140,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu type response struct { Repository struct { + Parent struct { + NameWithOwner string + } PullRequests edges PullRequest *PullRequest } @@ -185,6 +189,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix := ` query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { + parent { + nameWithOwner + } pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page) { totalCount edges { @@ -199,6 +206,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix = ` query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { + parent { + nameWithOwner + } pullRequest(number: $number) { ...prWithReviews } @@ -269,6 +279,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu } payload := PullRequestsPayload{ + ParentRepo: resp.Repository.Parent.NameWithOwner, ViewerCreated: PullRequestAndTotalCount{ PullRequests: viewerCreated, TotalCount: resp.ViewerCreated.TotalCount, diff --git a/command/issue.go b/command/issue.go index 711df46a6..5ada3d249 100644 --- a/command/issue.go +++ b/command/issue.go @@ -11,6 +11,7 @@ import ( "time" "github.com/cli/cli/api" + "github.com/cli/cli/context" "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/githubtemplate" @@ -175,6 +176,13 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) + repoInfo, err := context.FormattedInfo(ctx, issuePayload.ParentRepo) + if err != nil { + return err + } + + fmt.Fprintln(out, repoInfo) + printHeader(out, "Issues assigned to you") if issuePayload.Assigned.TotalCount > 0 { printIssues(out, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues) diff --git a/command/pr.go b/command/pr.go index 1a227cc63..e4906d087 100644 --- a/command/pr.go +++ b/command/pr.go @@ -97,12 +97,19 @@ func prStatus(cmd *cobra.Command, args []string) error { return err } + out := colorableOut(cmd) + prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser) if err != nil { return err } - out := colorableOut(cmd) + repoInfo, err := context.FormattedInfo(ctx, prPayload.ParentRepo) + if err != nil { + return err + } + + fmt.Fprintln(out, repoInfo) printHeader(out, "Current branch") if prPayload.CurrentPR != nil { diff --git a/context/formatted.go b/context/formatted.go new file mode 100644 index 000000000..a4250c6c9 --- /dev/null +++ b/context/formatted.go @@ -0,0 +1,33 @@ +package context + +import ( + "fmt" + "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/utils" +) + +func FormattedInfo(c Context, parentRepo string) (string, error) { + baseRepo, err := c.BaseRepo() + if err != nil { + return "", err + } + authLogin, err := c.AuthLogin() + if err != nil { + return "", err + } + + forkInfo := "" + if parentRepo != "" { + forkInfo = fmt.Sprintf("(fork of %s) ", parentRepo) + } + + out := fmt.Sprintf("%s%s %s%s%s", + utils.Gray("in "), + utils.Magenta(ghrepo.FullName(baseRepo)), + utils.Yellow(forkInfo), + utils.Gray("as "), + utils.Magenta(authLogin), + ) + + return out, nil +} From 86cb09fe8455864ec166210911eb42d35319e7ab Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 29 Jan 2020 17:41:36 -0600 Subject: [PATCH 2/5] Revert "print repo info for status commands" This reverts commit d64dcdca95c8705750489fb94534872b78c0d131. --- api/queries_issue.go | 14 +++----------- api/queries_pr.go | 11 ----------- command/issue.go | 8 -------- command/pr.go | 9 +-------- context/formatted.go | 33 --------------------------------- 5 files changed, 4 insertions(+), 71 deletions(-) delete mode 100644 context/formatted.go diff --git a/api/queries_issue.go b/api/queries_issue.go index 96f3574d4..934467e05 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -8,10 +8,9 @@ import ( ) type IssuesPayload struct { - ParentRepo string - Assigned IssuesAndTotalCount - Mentioned IssuesAndTotalCount - Authored IssuesAndTotalCount + Assigned IssuesAndTotalCount + Mentioned IssuesAndTotalCount + Authored IssuesAndTotalCount } type IssuesAndTotalCount struct { @@ -109,9 +108,6 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) TotalCount int Nodes []Issue } - Parent struct { - NameWithOwner string - } HasIssuesEnabled bool } } @@ -119,9 +115,6 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) query := fragments + ` query($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - parent { - nameWithOwner - } hasIssuesEnabled assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount @@ -161,7 +154,6 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) } payload := IssuesPayload{ - ParentRepo: resp.Repository.Parent.NameWithOwner, Assigned: IssuesAndTotalCount{ Issues: resp.Repository.Assigned.Nodes, TotalCount: resp.Repository.Assigned.TotalCount, diff --git a/api/queries_pr.go b/api/queries_pr.go index 6daaa1c85..734245acb 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -8,7 +8,6 @@ import ( ) type PullRequestsPayload struct { - ParentRepo string ViewerCreated PullRequestAndTotalCount ReviewRequested PullRequestAndTotalCount CurrentPR *PullRequest @@ -140,9 +139,6 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu type response struct { Repository struct { - Parent struct { - NameWithOwner string - } PullRequests edges PullRequest *PullRequest } @@ -189,9 +185,6 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix := ` query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - parent { - nameWithOwner - } pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page) { totalCount edges { @@ -206,9 +199,6 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix = ` query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - parent { - nameWithOwner - } pullRequest(number: $number) { ...prWithReviews } @@ -279,7 +269,6 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu } payload := PullRequestsPayload{ - ParentRepo: resp.Repository.Parent.NameWithOwner, ViewerCreated: PullRequestAndTotalCount{ PullRequests: viewerCreated, TotalCount: resp.ViewerCreated.TotalCount, diff --git a/command/issue.go b/command/issue.go index 5ada3d249..711df46a6 100644 --- a/command/issue.go +++ b/command/issue.go @@ -11,7 +11,6 @@ import ( "time" "github.com/cli/cli/api" - "github.com/cli/cli/context" "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/githubtemplate" @@ -176,13 +175,6 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) - repoInfo, err := context.FormattedInfo(ctx, issuePayload.ParentRepo) - if err != nil { - return err - } - - fmt.Fprintln(out, repoInfo) - printHeader(out, "Issues assigned to you") if issuePayload.Assigned.TotalCount > 0 { printIssues(out, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues) diff --git a/command/pr.go b/command/pr.go index e4906d087..1a227cc63 100644 --- a/command/pr.go +++ b/command/pr.go @@ -97,19 +97,12 @@ func prStatus(cmd *cobra.Command, args []string) error { return err } - out := colorableOut(cmd) - prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser) if err != nil { return err } - repoInfo, err := context.FormattedInfo(ctx, prPayload.ParentRepo) - if err != nil { - return err - } - - fmt.Fprintln(out, repoInfo) + out := colorableOut(cmd) printHeader(out, "Current branch") if prPayload.CurrentPR != nil { diff --git a/context/formatted.go b/context/formatted.go deleted file mode 100644 index a4250c6c9..000000000 --- a/context/formatted.go +++ /dev/null @@ -1,33 +0,0 @@ -package context - -import ( - "fmt" - "github.com/cli/cli/internal/ghrepo" - "github.com/cli/cli/utils" -) - -func FormattedInfo(c Context, parentRepo string) (string, error) { - baseRepo, err := c.BaseRepo() - if err != nil { - return "", err - } - authLogin, err := c.AuthLogin() - if err != nil { - return "", err - } - - forkInfo := "" - if parentRepo != "" { - forkInfo = fmt.Sprintf("(fork of %s) ", parentRepo) - } - - out := fmt.Sprintf("%s%s %s%s%s", - utils.Gray("in "), - utils.Magenta(ghrepo.FullName(baseRepo)), - utils.Yellow(forkInfo), - utils.Gray("as "), - utils.Magenta(authLogin), - ) - - return out, nil -} From d81cf009f5b9198f6c38689570c3531cfebba8bc Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 29 Jan 2020 17:47:29 -0600 Subject: [PATCH 3/5] simpler repo context --- command/issue.go | 4 ++++ command/pr.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/command/issue.go b/command/issue.go index 711df46a6..7637485a3 100644 --- a/command/issue.go +++ b/command/issue.go @@ -175,6 +175,10 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) + fmt.Fprint(out, utils.Gray("Relevant issues in ")) + fmt.Fprint(out, utils.Cyan(ghrepo.FullName(baseRepo))) + fmt.Fprintln(out, "") + printHeader(out, "Issues assigned to you") if issuePayload.Assigned.TotalCount > 0 { printIssues(out, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues) diff --git a/command/pr.go b/command/pr.go index 1a227cc63..561ab387e 100644 --- a/command/pr.go +++ b/command/pr.go @@ -104,6 +104,10 @@ func prStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) + fmt.Fprint(out, utils.Gray("Relevant pull requests in ")) + fmt.Fprint(out, utils.Cyan(ghrepo.FullName(baseRepo))) + fmt.Fprintln(out, "") + printHeader(out, "Current branch") if prPayload.CurrentPR != nil { printPrs(out, 0, *prPayload.CurrentPR) From 34248f989273599b299b759b606dea0703a197c1 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 29 Jan 2020 17:51:18 -0600 Subject: [PATCH 4/5] fix tests --- command/issue_test.go | 3 ++- command/pr_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/command/issue_test.go b/command/issue_test.go index cc18dcdf2..b4e56dc29 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -58,7 +58,8 @@ func TestIssueStatus_blankSlate(t *testing.T) { t.Errorf("error running command `issue status`: %v", err) } - expectedOutput := `Issues assigned to you + expectedOutput := `Relevant issues in OWNER/REPO +Issues assigned to you There are no issues assigned to you Issues mentioning you diff --git a/command/pr_test.go b/command/pr_test.go index d51e32325..42f210c13 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -136,7 +136,8 @@ func TestPRStatus_blankSlate(t *testing.T) { t.Errorf("error running command `pr status`: %v", err) } - expected := `Current branch + expected := `Relevant pull requests in OWNER/REPO +Current branch There is no pull request associated with [blueberries] Created by you From d7bf83a82c78d87db131e06d48340adeca9d4e09 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 30 Jan 2020 14:00:43 -0600 Subject: [PATCH 5/5] strip color, add whitespace --- command/issue.go | 4 ++-- command/issue_test.go | 4 +++- command/pr.go | 4 ++-- command/pr_test.go | 4 +++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/command/issue.go b/command/issue.go index 7637485a3..516bd3faf 100644 --- a/command/issue.go +++ b/command/issue.go @@ -175,8 +175,8 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) - fmt.Fprint(out, utils.Gray("Relevant issues in ")) - fmt.Fprint(out, utils.Cyan(ghrepo.FullName(baseRepo))) + fmt.Fprintln(out, "") + fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Issues assigned to you") diff --git a/command/issue_test.go b/command/issue_test.go index b4e56dc29..98ba6119e 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -58,7 +58,9 @@ func TestIssueStatus_blankSlate(t *testing.T) { t.Errorf("error running command `issue status`: %v", err) } - expectedOutput := `Relevant issues in OWNER/REPO + expectedOutput := ` +Relevant issues in OWNER/REPO + Issues assigned to you There are no issues assigned to you diff --git a/command/pr.go b/command/pr.go index 561ab387e..624debac0 100644 --- a/command/pr.go +++ b/command/pr.go @@ -104,8 +104,8 @@ func prStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) - fmt.Fprint(out, utils.Gray("Relevant pull requests in ")) - fmt.Fprint(out, utils.Cyan(ghrepo.FullName(baseRepo))) + fmt.Fprintln(out, "") + fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Current branch") diff --git a/command/pr_test.go b/command/pr_test.go index 42f210c13..3fbfb0b8e 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -136,7 +136,9 @@ func TestPRStatus_blankSlate(t *testing.T) { t.Errorf("error running command `pr status`: %v", err) } - expected := `Relevant pull requests in OWNER/REPO + expected := ` +Relevant pull requests in OWNER/REPO + Current branch There is no pull request associated with [blueberries]