diff --git a/api/queries.go b/api/queries.go index 1a405a858..5daf10244 100644 --- a/api/queries.go +++ b/api/queries.go @@ -14,6 +14,7 @@ type PullRequestsPayload struct { type PullRequest struct { Number int Title string + State string URL string HeadRefName string } @@ -275,3 +276,96 @@ func PullRequestsForBranch(client *Client, ghRepo Repo, branch string) ([]PullRe return prs, nil } + +func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]PullRequest, error) { + type response struct { + Repository struct { + PullRequests struct { + Edges []struct { + Node PullRequest + } + PageInfo struct { + HasNextPage bool + EndCursor string + } + } + } + } + + query := ` + query( + $owner: String!, + $repo: String!, + $limit: Int!, + $endCursor: String, + $baseBranch: String, + $labels: [String!], + $state: [PullRequestState!] = OPEN + ) { + repository(owner: $owner, name: $repo) { + pullRequests( + states: $state, + baseRefName: $baseBranch, + labels: $labels, + first: $limit, + after: $endCursor, + orderBy: {field: CREATED_AT, direction: DESC} + ) { + edges { + node { + number + title + state + url + headRefName + } + } + pageInfo { + hasNextPage + endCursor + } + } + } + }` + + prs := []PullRequest{} + pageLimit := min(limit, 100) + variables := map[string]interface{}{} + for name, val := range vars { + variables[name] = val + } + + for { + variables["limit"] = pageLimit + var data response + err := client.GraphQL(query, variables, &data) + if err != nil { + return nil, err + } + prData := data.Repository.PullRequests + + for _, edge := range prData.Edges { + prs = append(prs, edge.Node) + if len(prs) == limit { + goto done + } + } + + if prData.PageInfo.HasNextPage { + variables["endCursor"] = prData.PageInfo.EndCursor + pageLimit = min(pageLimit, limit-len(prs)) + continue + } + done: + break + } + + return prs, nil +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/command/completion.go b/command/completion.go new file mode 100644 index 000000000..2a528a1ba --- /dev/null +++ b/command/completion.go @@ -0,0 +1,43 @@ +package command + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + RootCmd.AddCommand(completionCmd) + completionCmd.Flags().StringP("shell", "s", "bash", "The type of shell") +} + +var completionCmd = &cobra.Command{ + Use: "completion", + Hidden: true, + Short: "Generates completion scripts", + Long: `To enable completion in your shell, run: + + eval "$(gh completion)" + +You can add that to your '~/.bash_profile' to enable completion whenever you +start a new shell. + +When installing with Homebrew, see https://docs.brew.sh/Shell-Completion +`, + RunE: func(cmd *cobra.Command, args []string) error { + shellType, err := cmd.Flags().GetString("shell") + if err != nil { + return err + } + + switch shellType { + case "bash": + RootCmd.GenBashCompletion(cmd.OutOrStdout()) + case "zsh": + RootCmd.GenZshCompletion(cmd.OutOrStdout()) + default: + return fmt.Errorf("unsupported shell type: %s", shellType) + } + return nil + }, +} diff --git a/command/completion_test.go b/command/completion_test.go new file mode 100644 index 000000000..8cea652b1 --- /dev/null +++ b/command/completion_test.go @@ -0,0 +1,50 @@ +package command + +import ( + "bytes" + "strings" + "testing" +) + +func TestCompletion_bash(t *testing.T) { + out := bytes.Buffer{} + completionCmd.SetOut(&out) + + RootCmd.SetArgs([]string{"completion"}) + _, err := RootCmd.ExecuteC() + if err != nil { + t.Fatal(err) + } + + outStr := out.String() + if !strings.Contains(outStr, "complete -o default -F __start_gh gh") { + t.Errorf("problem in bash completion:\n%s", outStr) + } +} + +func TestCompletion_zsh(t *testing.T) { + out := bytes.Buffer{} + completionCmd.SetOut(&out) + + RootCmd.SetArgs([]string{"completion", "-s", "zsh"}) + _, err := RootCmd.ExecuteC() + if err != nil { + t.Fatal(err) + } + + outStr := out.String() + if !strings.Contains(outStr, "#compdef _gh gh") { + t.Errorf("problem in zsh completion:\n%s", outStr) + } +} + +func TestCompletion_unsupported(t *testing.T) { + out := bytes.Buffer{} + completionCmd.SetOut(&out) + + RootCmd.SetArgs([]string{"completion", "-s", "fish"}) + _, err := RootCmd.ExecuteC() + if err == nil || err.Error() != "unsupported shell type: fish" { + t.Fatal(err) + } +} diff --git a/command/issue.go b/command/issue.go index 3111e28cb..8d1fcb1d7 100644 --- a/command/issue.go +++ b/command/issue.go @@ -45,8 +45,6 @@ var issueCreateCmd = &cobra.Command{ } func issueList(cmd *cobra.Command, args []string) error { - cmd.SilenceUsage = true - ctx := contextForCommand(cmd) apiClient, err := apiClientForContext(ctx) if err != nil { @@ -97,8 +95,6 @@ func issueList(cmd *cobra.Command, args []string) error { } func issueView(cmd *cobra.Command, args []string) error { - cmd.SilenceUsage = true - ctx := contextForCommand(cmd) baseRepo, err := ctx.BaseRepo() @@ -191,6 +187,6 @@ func issueCreate(cmd *cobra.Command, args []string) error { func printIssues(issues ...api.Issue) { for _, issue := range issues { - fmt.Printf(" #%d %s\n", issue.Number, truncateTitle(issue.Title, 70)) + fmt.Printf(" #%d %s\n", issue.Number, truncate(70, issue.Title)) } } diff --git a/command/issue_test.go b/command/issue_test.go index d796c1819..4571bc43e 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -2,17 +2,19 @@ package command import ( "os" + "os/exec" "regexp" "testing" "github.com/github/gh-cli/test" + "github.com/github/gh-cli/utils" ) -func TestIssueList(t *testing.T) { +func TestIssueStatus(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() - jsonFile, _ := os.Open("../test/fixtures/issueList.json") + jsonFile, _ := os.Open("../test/fixtures/issueStatus.json") defer jsonFile.Close() http.StubResponse(200, jsonFile) @@ -43,8 +45,12 @@ func TestIssueView(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - teardown, callCount := mockOpenInBrowser() - defer teardown() + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() output, err := test.RunCommand(RootCmd, "issue view 8") if err != nil { @@ -55,7 +61,11 @@ func TestIssueView(t *testing.T) { t.Errorf("command output expected got an empty string") } - if *callCount != 1 { - t.Errorf("OpenInBrowser should be called 1 time but was called %d time(s)", *callCount) + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + if url != "https://github.com/OWNER/REPO/issues/8" { + t.Errorf("got: %q", url) } } diff --git a/command/pr.go b/command/pr.go index 720405da8..efbf8d995 100644 --- a/command/pr.go +++ b/command/pr.go @@ -2,41 +2,49 @@ package command import ( "fmt" + "os" "strconv" "github.com/github/gh-cli/api" "github.com/github/gh-cli/utils" "github.com/spf13/cobra" + "golang.org/x/crypto/ssh/terminal" ) func init() { RootCmd.AddCommand(prCmd) - prCmd.AddCommand( - &cobra.Command{ - Use: "list", - Short: "List pull requests", - RunE: prList, - }, - &cobra.Command{ - Use: "view []", - Short: "Open a pull request in the browser", - RunE: prView, - }, - ) + prCmd.AddCommand(prListCmd) + prCmd.AddCommand(prStatusCmd) + prCmd.AddCommand(prViewCmd) + + prListCmd.Flags().IntP("limit", "L", 30, "maximum number of items to fetch") + prListCmd.Flags().StringP("state", "s", "open", "filter by state") + prListCmd.Flags().StringP("base", "b", "", "filter by base branch") + prListCmd.Flags().StringArrayP("label", "l", nil, "filter by label") } var prCmd = &cobra.Command{ Use: "pr", Short: "Work with pull requests", - Long: `This command allows you to -work with pull requests.`, - Args: cobra.MinimumNArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - return fmt.Errorf("%+v is not a valid PR command", args) - }, + Long: `Helps you work with pull requests.`, +} +var prListCmd = &cobra.Command{ + Use: "list", + Short: "List pull requests", + RunE: prList, +} +var prStatusCmd = &cobra.Command{ + Use: "status", + Short: "Show status of relevant pull requests", + RunE: prStatus, +} +var prViewCmd = &cobra.Command{ + Use: "view [pr-number]", + Short: "Open a pull request in the browser", + RunE: prView, } -func prList(cmd *cobra.Command, args []string) error { +func prStatus(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd) apiClient, err := apiClientForContext(ctx) if err != nil { @@ -89,6 +97,117 @@ func prList(cmd *cobra.Command, args []string) error { return nil } +func prList(cmd *cobra.Command, args []string) error { + ctx := contextForCommand(cmd) + apiClient, err := apiClientForContext(ctx) + if err != nil { + return err + } + + baseRepo, err := ctx.BaseRepo() + if err != nil { + return err + } + + limit, err := cmd.Flags().GetInt("limit") + if err != nil { + return err + } + state, err := cmd.Flags().GetString("state") + if err != nil { + return err + } + baseBranch, err := cmd.Flags().GetString("base") + if err != nil { + return err + } + labels, err := cmd.Flags().GetStringArray("label") + if err != nil { + return err + } + + var graphqlState []string + switch state { + case "open": + graphqlState = []string{"OPEN"} + case "closed": + graphqlState = []string{"CLOSED"} + case "merged": + graphqlState = []string{"MERGED"} + case "all": + graphqlState = []string{"OPEN", "CLOSED", "MERGED"} + default: + return fmt.Errorf("invalid state: %s", state) + } + + params := map[string]interface{}{ + "owner": baseRepo.RepoOwner(), + "repo": baseRepo.RepoName(), + "state": graphqlState, + } + if len(labels) > 0 { + params["labels"] = labels + } + if baseBranch != "" { + params["baseBranch"] = baseBranch + } + + prs, err := api.PullRequestList(apiClient, params, limit) + if err != nil { + return err + } + + tty := false + ttyWidth := 80 + out := cmd.OutOrStdout() + if outFile, isFile := out.(*os.File); isFile { + fd := int(outFile.Fd()) + tty = terminal.IsTerminal(fd) + if w, _, err := terminal.GetSize(fd); err == nil { + ttyWidth = w + } + } + + numWidth := 0 + maxTitleWidth := 0 + for _, pr := range prs { + numLen := len(strconv.Itoa(pr.Number)) + 1 + if numLen > numWidth { + numWidth = numLen + } + if len(pr.Title) > maxTitleWidth { + maxTitleWidth = len(pr.Title) + } + } + + branchWidth := 40 + titleWidth := ttyWidth - branchWidth - 2 - numWidth - 2 + + if maxTitleWidth < titleWidth { + branchWidth += titleWidth - maxTitleWidth + titleWidth = maxTitleWidth + } + + for _, pr := range prs { + if tty { + prNum := fmt.Sprintf("% *s", numWidth, fmt.Sprintf("#%d", pr.Number)) + switch pr.State { + case "OPEN": + prNum = utils.Green(prNum) + case "CLOSED": + prNum = utils.Red(prNum) + case "MERGED": + prNum = utils.Magenta(prNum) + } + prBranch := utils.Cyan(truncate(branchWidth, pr.HeadRefName)) + fmt.Fprintf(out, "%s %-*s %s\n", prNum, titleWidth, truncate(titleWidth, pr.Title), prBranch) + } else { + fmt.Fprintf(out, "%d\t%s\t%s\n", pr.Number, pr.Title, pr.HeadRefName) + } + } + return nil +} + func prView(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd) baseRepo, err := ctx.BaseRepo() @@ -129,7 +248,7 @@ func prView(cmd *cobra.Command, args []string) error { func printPrs(prs ...api.PullRequest) { for _, pr := range prs { - fmt.Printf(" #%d %s %s\n", pr.Number, truncateTitle(pr.Title, 50), utils.Cyan("["+pr.HeadRefName+"]")) + fmt.Printf(" #%d %s %s\n", pr.Number, truncate(50, pr.Title), utils.Cyan("["+pr.HeadRefName+"]")) } } @@ -141,7 +260,7 @@ func printMessage(s string) { fmt.Println(utils.Gray(s)) } -func truncateTitle(title string, maxLength int) string { +func truncate(maxLength int, title string) string { if len(title) > maxLength { return title[0:maxLength-3] + "..." } diff --git a/command/pr_test.go b/command/pr_test.go index 5ac21b9bd..863632ee9 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -1,24 +1,37 @@ package command import ( + "bytes" + "encoding/json" + "io/ioutil" "os" + "os/exec" + "reflect" "regexp" "testing" "github.com/github/gh-cli/test" + "github.com/github/gh-cli/utils" ) -func TestPRList(t *testing.T) { +func eq(t *testing.T, got interface{}, expected interface{}) { + t.Helper() + if !reflect.DeepEqual(got, expected) { + t.Errorf("expected: %v, got: %v", expected, got) + } +} + +func TestPRStatus(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() - jsonFile, _ := os.Open("../test/fixtures/prList.json") + jsonFile, _ := os.Open("../test/fixtures/prStatus.json") defer jsonFile.Close() http.StubResponse(200, jsonFile) - output, err := test.RunCommand(RootCmd, "pr list") + output, err := test.RunCommand(RootCmd, "pr status") if err != nil { - t.Errorf("error running command `pr list`: %v", err) + t.Errorf("error running command `pr status`: %v", err) } expectedPrs := []*regexp.Regexp{ @@ -35,6 +48,57 @@ func TestPRList(t *testing.T) { } } +func TestPRList(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + out := bytes.Buffer{} + prListCmd.SetOut(&out) + + RootCmd.SetArgs([]string{"pr", "list"}) + _, err := RootCmd.ExecuteC() + if err != nil { + t.Fatal(err) + } + + eq(t, out.String(), `32 New feature feature +29 Fixed bad bug bug-fix +28 Improve documentation docs +`) +} + +func TestPRList_filtering(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + respBody := bytes.NewBufferString(`{ "data": {} }`) + http.StubResponse(200, respBody) + + prListCmd.SetOut(ioutil.Discard) + + RootCmd.SetArgs([]string{"pr", "list", "-s", "all", "-l", "one", "-l", "two"}) + _, err := RootCmd.ExecuteC() + if err != nil { + t.Fatal(err) + } + + bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) + reqBody := struct { + Variables struct { + State []string + Labels []string + } + }{} + json.Unmarshal(bodyBytes, &reqBody) + + eq(t, reqBody.Variables.State, []string{"OPEN", "CLOSED", "MERGED"}) + eq(t, reqBody.Variables.Labels, []string{"one", "two"}) +} + func TestPRView(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -43,8 +107,12 @@ func TestPRView(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - teardown, callCount := mockOpenInBrowser() - defer teardown() + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() output, err := test.RunCommand(RootCmd, "pr view") if err != nil { @@ -55,8 +123,12 @@ func TestPRView(t *testing.T) { t.Errorf("command output expected got an empty string") } - if *callCount != 1 { - t.Errorf("OpenInBrowser should be called 1 time but was called %d time(s)", *callCount) + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + if url != "https://github.com/OWNER/REPO/pull/10" { + t.Errorf("got: %q", url) } } @@ -68,16 +140,20 @@ func TestPRView_NoActiveBranch(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - teardown, callCount := mockOpenInBrowser() - defer teardown() + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() output, err := test.RunCommand(RootCmd, "pr view") if err == nil || err.Error() != "the 'master' branch has no open pull requests" { t.Errorf("error running command `pr view`: %v", err) } - if *callCount > 0 { - t.Errorf("OpenInBrowser should NOT be called but was called %d time(s)", *callCount) + if seenCmd != nil { + t.Fatalf("unexpected command: %v", seenCmd.Args) } // Now run again but provide a PR number @@ -90,7 +166,11 @@ func TestPRView_NoActiveBranch(t *testing.T) { t.Errorf("command output expected got an empty string") } - if *callCount != 1 { - t.Errorf("OpenInBrowser should be called once but was called %d time(s)", *callCount) + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + if url != "https://github.com/OWNER/REPO/pull/23" { + t.Errorf("got: %q", url) } } diff --git a/command/testing.go b/command/testing.go index a6ccf4807..2e1cf9505 100644 --- a/command/testing.go +++ b/command/testing.go @@ -3,7 +3,6 @@ package command import ( "github.com/github/gh-cli/api" "github.com/github/gh-cli/context" - "github.com/github/gh-cli/utils" ) func initBlankContext(repo, branch string) { @@ -23,17 +22,15 @@ func initFakeHTTP() *api.FakeHTTP { return http } -func mockOpenInBrowser() (func(), *int) { - callCount := 0 - originalOpenInBrowser := utils.OpenInBrowser - teardown := func() { - utils.OpenInBrowser = originalOpenInBrowser - } - - utils.OpenInBrowser = func(_ string) error { - callCount++ - return nil - } - - return teardown, &callCount +// outputStub implements a simple utils.Runnable +type outputStub struct { + output []byte +} + +func (s outputStub) Output() ([]byte, error) { + return s.output, nil +} + +func (s outputStub) Run() error { + return nil } diff --git a/git/git.go b/git/git.go index b3b63388e..a2a385f7f 100644 --- a/git/git.go +++ b/git/git.go @@ -8,12 +8,13 @@ import ( "os/exec" "path/filepath" "strings" + + "github.com/github/gh-cli/utils" ) func Dir() (string, error) { dirCmd := exec.Command("git", "rev-parse", "-q", "--git-dir") - dirCmd.Stderr = nil - output, err := dirCmd.Output() + output, err := utils.PrepareCmd(dirCmd).Output() if err != nil { return "", fmt.Errorf("Not a git repository (or any of the parent directories): .git") } @@ -33,7 +34,7 @@ func Dir() (string, error) { func WorkdirName() (string, error) { toplevelCmd := exec.Command("git", "rev-parse", "--show-toplevel") toplevelCmd.Stderr = nil - output, err := toplevelCmd.Output() + output, err := utils.PrepareCmd(toplevelCmd).Output() dir := firstLine(output) if dir == "" { return "", fmt.Errorf("unable to determine git working directory") @@ -44,8 +45,7 @@ func WorkdirName() (string, error) { func HasFile(segments ...string) bool { // The blessed way to resolve paths within git dir since Git 2.5.0 pathCmd := exec.Command("git", "rev-parse", "-q", "--git-path", filepath.Join(segments...)) - pathCmd.Stderr = nil - if output, err := pathCmd.Output(); err == nil { + if output, err := utils.PrepareCmd(pathCmd).Output(); err == nil { if lines := outputLines(output); len(lines) == 1 { if _, err := os.Stat(lines[0]); err == nil { return true @@ -97,8 +97,7 @@ func BranchAtRef(paths ...string) (name string, err error) { func Editor() (string, error) { varCmd := exec.Command("git", "var", "GIT_EDITOR") - varCmd.Stderr = nil - output, err := varCmd.Output() + output, err := utils.PrepareCmd(varCmd).Output() if err != nil { return "", fmt.Errorf("Can't load git var: GIT_EDITOR") } @@ -112,8 +111,7 @@ func Head() (string, error) { func SymbolicFullName(name string) (string, error) { parseCmd := exec.Command("git", "rev-parse", "--symbolic-full-name", name) - parseCmd.Stderr = nil - output, err := parseCmd.Output() + output, err := utils.PrepareCmd(parseCmd).Output() if err != nil { return "", fmt.Errorf("Unknown revision or path not in the working tree: %s", name) } @@ -145,9 +143,7 @@ func CommentChar(text string) (string, error) { func Show(sha string) (string, error) { cmd := exec.Command("git", "-c", "log.showSignature=false", "show", "-s", "--format=%s%n%+b", sha) - cmd.Stderr = nil - - output, err := cmd.Output() + output, err := utils.PrepareCmd(cmd).Output() return strings.TrimSpace(string(output)), err } @@ -157,7 +153,7 @@ func Log(sha1, sha2 string) (string, error) { "-c", "log.showSignature=false", "log", "--no-color", "--format=%h (%aN, %ar)%n%w(78,3,3)%s%n%+b", "--cherry", shaRange) - outputs, err := cmd.Output() + outputs, err := utils.PrepareCmd(cmd).Output() if err != nil { return "", fmt.Errorf("Can't load git log %s..%s", sha1, sha2) } @@ -167,14 +163,13 @@ func Log(sha1, sha2 string) (string, error) { func listRemotes() ([]string, error) { remoteCmd := exec.Command("git", "remote", "-v") - remoteCmd.Stderr = nil - output, err := remoteCmd.Output() + output, err := utils.PrepareCmd(remoteCmd).Output() return outputLines(output), err } func Config(name string) (string, error) { configCmd := exec.Command("git", "config", name) - output, err := configCmd.Output() + output, err := utils.PrepareCmd(configCmd).Output() if err != nil { return "", fmt.Errorf("unknown config key: %s", name) } @@ -190,21 +185,16 @@ func ConfigAll(name string) ([]string, error) { } configCmd := exec.Command("git", "config", mode, name) - output, err := configCmd.Output() + output, err := utils.PrepareCmd(configCmd).Output() if err != nil { return nil, fmt.Errorf("Unknown config %s", name) } return outputLines(output), nil } -func Run(args ...string) error { - cmd := exec.Command("git", args...) - return cmd.Run() -} - func LocalBranches() ([]string, error) { branchesCmd := exec.Command("git", "branch", "--list") - output, err := branchesCmd.Output() + output, err := utils.PrepareCmd(branchesCmd).Output() if err != nil { return nil, err } @@ -218,9 +208,6 @@ func LocalBranches() ([]string, error) { func outputLines(output []byte) []string { lines := strings.TrimSuffix(string(output), "\n") - if lines == "" { - return []string{} - } return strings.Split(lines, "\n") } diff --git a/test/fixtures/issueStatus.json b/test/fixtures/issueStatus.json new file mode 100644 index 000000000..784c0cc8f --- /dev/null +++ b/test/fixtures/issueStatus.json @@ -0,0 +1,47 @@ +{ + "data": { + "assigned": { + "issues": { + "edges": [ + { + "node": { + "number": 9, + "title": "corey thinks squash tastes bad" + } + }, + { + "node": { + "number": 10, + "title": "broccoli is a superfood" + } + } + ] + } + }, + "mentioned": { + "issues": { + "edges": [ + { + "node": { + "number": 8, + "title": "rabbits eat carrots" + } + }, + { + "node": { + "number": 11, + "title": "swiss chard is neutral" + } + } + ] + } + }, + "recent": { + "issues": { + "edges": [] + } + }, + + "pageInfo": { "hasNextPage": false } + } +} diff --git a/test/fixtures/prList.json b/test/fixtures/prList.json index 444fa5e01..7ff9502a0 100644 --- a/test/fixtures/prList.json +++ b/test/fixtures/prList.json @@ -1,50 +1,38 @@ -{"data":{ - "repository": { - "pullRequests": { - "edges": [ - { - "node": { - "number": 10, - "title": "Blueberries are a good fruit", - "url": "https://github.com/github/gh-cli/pull/10", - "headRefName": "[blueberries]" +{ + "data": { + "repository": { + "pullRequests": { + "edges": [ + { + "node": { + "number": 32, + "title": "New feature", + "url": "https://github.com/monalisa/hello/pull/32", + "headRefName": "feature" + } + }, + { + "node": { + "number": 29, + "title": "Fixed bad bug", + "url": "https://github.com/monalisa/hello/pull/29", + "headRefName": "bug-fix" + } + }, + { + "node": { + "number": 28, + "title": "Improve documentation", + "url": "https://github.com/monalisa/hello/pull/28", + "headRefName": "docs" + } } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" } - ] + } } - }, - "viewerCreated": { - "edges": [ - { - "node": { - "number": 8, - "title": "Strawberries are not actually berries", - "url": "https://github.com/github/gh-cli/pull/8", - "headRefName": "[strawberries]" - } - } - ], - "pageInfo": { "hasNextPage": false } - }, - "reviewRequested": { - "edges": [ - { - "node": { - "number": 9, - "title": "Apples are tasty", - "url": "https://github.com/github/gh-cli/pull/9", - "headRefName": "[apples]" - } - }, - { - "node": { - "number": 11, - "title": "Figs are my favorite", - "url": "https://github.com/github/gh-cli/pull/1", - "headRefName": "[figs]" - } - } - ], - "pageInfo": { "hasNextPage": false } } -}} \ No newline at end of file +} \ No newline at end of file diff --git a/test/fixtures/prStatus.json b/test/fixtures/prStatus.json new file mode 100644 index 000000000..444fa5e01 --- /dev/null +++ b/test/fixtures/prStatus.json @@ -0,0 +1,50 @@ +{"data":{ + "repository": { + "pullRequests": { + "edges": [ + { + "node": { + "number": 10, + "title": "Blueberries are a good fruit", + "url": "https://github.com/github/gh-cli/pull/10", + "headRefName": "[blueberries]" + } + } + ] + } + }, + "viewerCreated": { + "edges": [ + { + "node": { + "number": 8, + "title": "Strawberries are not actually berries", + "url": "https://github.com/github/gh-cli/pull/8", + "headRefName": "[strawberries]" + } + } + ], + "pageInfo": { "hasNextPage": false } + }, + "reviewRequested": { + "edges": [ + { + "node": { + "number": 9, + "title": "Apples are tasty", + "url": "https://github.com/github/gh-cli/pull/9", + "headRefName": "[apples]" + } + }, + { + "node": { + "number": 11, + "title": "Figs are my favorite", + "url": "https://github.com/github/gh-cli/pull/1", + "headRefName": "[figs]" + } + } + ], + "pageInfo": { "hasNextPage": false } + } +}} \ No newline at end of file diff --git a/test/fixtures/prView.json b/test/fixtures/prView.json index 444fa5e01..5ac892498 100644 --- a/test/fixtures/prView.json +++ b/test/fixtures/prView.json @@ -6,7 +6,7 @@ "node": { "number": 10, "title": "Blueberries are a good fruit", - "url": "https://github.com/github/gh-cli/pull/10", + "url": "https://github.com/OWNER/REPO/pull/10", "headRefName": "[blueberries]" } } @@ -19,7 +19,7 @@ "node": { "number": 8, "title": "Strawberries are not actually berries", - "url": "https://github.com/github/gh-cli/pull/8", + "url": "https://github.com/OWNER/REPO/pull/8", "headRefName": "[strawberries]" } } @@ -32,7 +32,7 @@ "node": { "number": 9, "title": "Apples are tasty", - "url": "https://github.com/github/gh-cli/pull/9", + "url": "https://github.com/OWNER/REPO/pull/9", "headRefName": "[apples]" } }, @@ -40,7 +40,7 @@ "node": { "number": 11, "title": "Figs are my favorite", - "url": "https://github.com/github/gh-cli/pull/1", + "url": "https://github.com/OWNER/REPO/pull/1", "headRefName": "[figs]" } } diff --git a/utils/prepare_cmd.go b/utils/prepare_cmd.go new file mode 100644 index 000000000..0129d1a5b --- /dev/null +++ b/utils/prepare_cmd.go @@ -0,0 +1,76 @@ +package utils + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" +) + +// Runnable is typically an exec.Cmd or its stub in tests +type Runnable interface { + Output() ([]byte, error) + Run() error +} + +// PrepareCmd extends exec.Cmd with extra error reporting features and provides a +// hook to stub command execution in tests +var PrepareCmd = func(cmd *exec.Cmd) Runnable { + return &cmdWithStderr{cmd} +} + +// SetPrepareCmd overrides PrepareCmd and returns a func to revert it back +func SetPrepareCmd(fn func(*exec.Cmd) Runnable) func() { + origPrepare := PrepareCmd + PrepareCmd = fn + return func() { + PrepareCmd = origPrepare + } +} + +// cmdWithStderr augments exec.Cmd by adding stderr to the error message +type cmdWithStderr struct { + *exec.Cmd +} + +func (c cmdWithStderr) Output() ([]byte, error) { + if os.Getenv("DEBUG") != "" { + fmt.Fprintf(os.Stderr, "%v\n", c.Cmd.Args) + } + errStream := &bytes.Buffer{} + c.Cmd.Stderr = errStream + out, err := c.Cmd.Output() + if err != nil { + err = &CmdError{errStream, c.Cmd.Args, err} + } + return out, err +} + +func (c cmdWithStderr) Run() error { + if os.Getenv("DEBUG") != "" { + fmt.Fprintf(os.Stderr, "%v\n", c.Cmd.Args) + } + errStream := &bytes.Buffer{} + c.Cmd.Stderr = errStream + err := c.Cmd.Run() + if err != nil { + err = &CmdError{errStream, c.Cmd.Args, err} + } + return err +} + +// CmdError provides more visibility into why an exec.Cmd had failed +type CmdError struct { + Stderr *bytes.Buffer + Args []string + Err error +} + +func (e CmdError) Error() string { + msg := e.Stderr.String() + if msg != "" && !strings.HasSuffix(msg, "\n") { + msg += "\n" + } + return fmt.Sprintf("%s%s: %s", msg, e.Args[0], e.Err) +} diff --git a/utils/utils.go b/utils/utils.go index ff1eb1993..b034a18d9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -27,7 +27,7 @@ func ConcatPaths(paths ...string) string { return strings.Join(paths, "/") } -var OpenInBrowser = func(url string) error { +func OpenInBrowser(url string) error { browser := os.Getenv("BROWSER") if browser == "" { browser = searchBrowserLauncher(runtime.GOOS) @@ -45,7 +45,8 @@ var OpenInBrowser = func(url string) error { } endingArgs := append(browserArgs[1:], url) - return exec.Command(browserArgs[0], endingArgs...).Run() + browseCmd := exec.Command(browserArgs[0], endingArgs...) + return PrepareCmd(browseCmd).Run() } func searchBrowserLauncher(goos string) (browser string) {