Skip to content

Commit 10c248d

Browse files
committed
Merge remote-tracking branch 'origin/master' into issue-status-view
2 parents 29b5593 + 262976b commit 10c248d

File tree

15 files changed

+674
-136
lines changed

15 files changed

+674
-136
lines changed

api/queries.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type PullRequestsPayload struct {
1414
type PullRequest struct {
1515
Number int
1616
Title string
17+
State string
1718
URL string
1819
HeadRefName string
1920
}
@@ -275,3 +276,96 @@ func PullRequestsForBranch(client *Client, ghRepo Repo, branch string) ([]PullRe
275276

276277
return prs, nil
277278
}
279+
280+
func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]PullRequest, error) {
281+
type response struct {
282+
Repository struct {
283+
PullRequests struct {
284+
Edges []struct {
285+
Node PullRequest
286+
}
287+
PageInfo struct {
288+
HasNextPage bool
289+
EndCursor string
290+
}
291+
}
292+
}
293+
}
294+
295+
query := `
296+
query(
297+
$owner: String!,
298+
$repo: String!,
299+
$limit: Int!,
300+
$endCursor: String,
301+
$baseBranch: String,
302+
$labels: [String!],
303+
$state: [PullRequestState!] = OPEN
304+
) {
305+
repository(owner: $owner, name: $repo) {
306+
pullRequests(
307+
states: $state,
308+
baseRefName: $baseBranch,
309+
labels: $labels,
310+
first: $limit,
311+
after: $endCursor,
312+
orderBy: {field: CREATED_AT, direction: DESC}
313+
) {
314+
edges {
315+
node {
316+
number
317+
title
318+
state
319+
url
320+
headRefName
321+
}
322+
}
323+
pageInfo {
324+
hasNextPage
325+
endCursor
326+
}
327+
}
328+
}
329+
}`
330+
331+
prs := []PullRequest{}
332+
pageLimit := min(limit, 100)
333+
variables := map[string]interface{}{}
334+
for name, val := range vars {
335+
variables[name] = val
336+
}
337+
338+
for {
339+
variables["limit"] = pageLimit
340+
var data response
341+
err := client.GraphQL(query, variables, &data)
342+
if err != nil {
343+
return nil, err
344+
}
345+
prData := data.Repository.PullRequests
346+
347+
for _, edge := range prData.Edges {
348+
prs = append(prs, edge.Node)
349+
if len(prs) == limit {
350+
goto done
351+
}
352+
}
353+
354+
if prData.PageInfo.HasNextPage {
355+
variables["endCursor"] = prData.PageInfo.EndCursor
356+
pageLimit = min(pageLimit, limit-len(prs))
357+
continue
358+
}
359+
done:
360+
break
361+
}
362+
363+
return prs, nil
364+
}
365+
366+
func min(a, b int) int {
367+
if a < b {
368+
return a
369+
}
370+
return b
371+
}

command/completion.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
RootCmd.AddCommand(completionCmd)
11+
completionCmd.Flags().StringP("shell", "s", "bash", "The type of shell")
12+
}
13+
14+
var completionCmd = &cobra.Command{
15+
Use: "completion",
16+
Hidden: true,
17+
Short: "Generates completion scripts",
18+
Long: `To enable completion in your shell, run:
19+
20+
eval "$(gh completion)"
21+
22+
You can add that to your '~/.bash_profile' to enable completion whenever you
23+
start a new shell.
24+
25+
When installing with Homebrew, see https://docs.brew.sh/Shell-Completion
26+
`,
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
shellType, err := cmd.Flags().GetString("shell")
29+
if err != nil {
30+
return err
31+
}
32+
33+
switch shellType {
34+
case "bash":
35+
RootCmd.GenBashCompletion(cmd.OutOrStdout())
36+
case "zsh":
37+
RootCmd.GenZshCompletion(cmd.OutOrStdout())
38+
default:
39+
return fmt.Errorf("unsupported shell type: %s", shellType)
40+
}
41+
return nil
42+
},
43+
}

command/completion_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package command
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestCompletion_bash(t *testing.T) {
10+
out := bytes.Buffer{}
11+
completionCmd.SetOut(&out)
12+
13+
RootCmd.SetArgs([]string{"completion"})
14+
_, err := RootCmd.ExecuteC()
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
outStr := out.String()
20+
if !strings.Contains(outStr, "complete -o default -F __start_gh gh") {
21+
t.Errorf("problem in bash completion:\n%s", outStr)
22+
}
23+
}
24+
25+
func TestCompletion_zsh(t *testing.T) {
26+
out := bytes.Buffer{}
27+
completionCmd.SetOut(&out)
28+
29+
RootCmd.SetArgs([]string{"completion", "-s", "zsh"})
30+
_, err := RootCmd.ExecuteC()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
35+
outStr := out.String()
36+
if !strings.Contains(outStr, "#compdef _gh gh") {
37+
t.Errorf("problem in zsh completion:\n%s", outStr)
38+
}
39+
}
40+
41+
func TestCompletion_unsupported(t *testing.T) {
42+
out := bytes.Buffer{}
43+
completionCmd.SetOut(&out)
44+
45+
RootCmd.SetArgs([]string{"completion", "-s", "fish"})
46+
_, err := RootCmd.ExecuteC()
47+
if err == nil || err.Error() != "unsupported shell type: fish" {
48+
t.Fatal(err)
49+
}
50+
}

command/issue.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ var issueCreateCmd = &cobra.Command{
4545
}
4646

4747
func issueList(cmd *cobra.Command, args []string) error {
48-
cmd.SilenceUsage = true
49-
5048
ctx := contextForCommand(cmd)
5149
apiClient, err := apiClientForContext(ctx)
5250
if err != nil {
@@ -97,8 +95,6 @@ func issueList(cmd *cobra.Command, args []string) error {
9795
}
9896

9997
func issueView(cmd *cobra.Command, args []string) error {
100-
cmd.SilenceUsage = true
101-
10298
ctx := contextForCommand(cmd)
10399

104100
baseRepo, err := ctx.BaseRepo()
@@ -191,6 +187,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {
191187

192188
func printIssues(issues ...api.Issue) {
193189
for _, issue := range issues {
194-
fmt.Printf(" #%d %s\n", issue.Number, truncateTitle(issue.Title, 70))
190+
fmt.Printf(" #%d %s\n", issue.Number, truncate(70, issue.Title))
195191
}
196192
}

command/issue_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ package command
22

33
import (
44
"os"
5+
"os/exec"
56
"regexp"
67
"testing"
78

89
"github.com/github/gh-cli/test"
10+
"github.com/github/gh-cli/utils"
911
)
1012

11-
func TestIssueList(t *testing.T) {
13+
func TestIssueStatus(t *testing.T) {
1214
initBlankContext("OWNER/REPO", "master")
1315
http := initFakeHTTP()
1416

15-
jsonFile, _ := os.Open("../test/fixtures/issueList.json")
17+
jsonFile, _ := os.Open("../test/fixtures/issueStatus.json")
1618
defer jsonFile.Close()
1719
http.StubResponse(200, jsonFile)
1820

@@ -43,8 +45,12 @@ func TestIssueView(t *testing.T) {
4345
defer jsonFile.Close()
4446
http.StubResponse(200, jsonFile)
4547

46-
teardown, callCount := mockOpenInBrowser()
47-
defer teardown()
48+
var seenCmd *exec.Cmd
49+
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
50+
seenCmd = cmd
51+
return &outputStub{}
52+
})
53+
defer restoreCmd()
4854

4955
output, err := test.RunCommand(RootCmd, "issue view 8")
5056
if err != nil {
@@ -55,7 +61,11 @@ func TestIssueView(t *testing.T) {
5561
t.Errorf("command output expected got an empty string")
5662
}
5763

58-
if *callCount != 1 {
59-
t.Errorf("OpenInBrowser should be called 1 time but was called %d time(s)", *callCount)
64+
if seenCmd == nil {
65+
t.Fatal("expected a command to run")
66+
}
67+
url := seenCmd.Args[len(seenCmd.Args)-1]
68+
if url != "https://github.com/OWNER/REPO/issues/8" {
69+
t.Errorf("got: %q", url)
6070
}
6171
}

0 commit comments

Comments
 (0)