Implement --web for gh pr checks (#2146)

This commit is contained in:
Jakub Warczarek 2020-11-17 19:27:07 +01:00 committed by GitHub
parent 00617216b8
commit b205faa941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 5 deletions

View file

@ -24,6 +24,8 @@ type ChecksOptions struct {
Branch func() (string, error)
Remotes func() (context.Remotes, error)
WebMode bool
SelectorArg string
}
@ -60,6 +62,8 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co
},
}
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser to show details about checks")
return cmd
}
@ -70,7 +74,7 @@ func checksRun(opts *ChecksOptions) error {
}
apiClient := api.NewClientFromHTTP(httpClient)
pr, _, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg)
pr, baseRepo, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg)
if err != nil {
return err
}
@ -84,6 +88,16 @@ func checksRun(opts *ChecksOptions) error {
return fmt.Errorf("no checks reported on the '%s' branch", pr.BaseRefName)
}
isTerminal := opts.IO.IsStdoutTTY()
if opts.WebMode {
openURL := ghrepo.GenerateRepoURL(baseRepo, "pull/%d/checks", pr.Number)
if isTerminal {
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
}
return utils.OpenInBrowser(openURL)
}
passing := 0
failing := 0
pending := 0
@ -164,9 +178,8 @@ func checksRun(opts *ChecksOptions) error {
if b0 == b1 {
if n0 == n1 {
return l0 < l1
} else {
return n0 < n1
}
return n0 < n1
}
return (b0 == "fail") || (b0 == "pending" && b1 == "success")
@ -175,7 +188,7 @@ func checksRun(opts *ChecksOptions) error {
tp := utils.NewTablePrinter(opts.IO)
for _, o := range outputs {
if opts.IO.IsStdoutTTY() {
if isTerminal {
tp.AddField(o.mark, nil, o.markColor)
tp.AddField(o.name, nil, nil)
tp.AddField(o.elapsed, nil, nil)
@ -211,7 +224,7 @@ func checksRun(opts *ChecksOptions) error {
summary = fmt.Sprintf("%s\n%s", cs.Bold(summary), tallies)
}
if opts.IO.IsStdoutTTY() {
if isTerminal {
fmt.Fprintln(opts.IO.Out, summary)
fmt.Fprintln(opts.IO.Out)
}

View file

@ -9,6 +9,7 @@ import (
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/httpmock"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
@ -200,3 +201,63 @@ func Test_checksRun(t *testing.T) {
})
}
}
func TestChecksRun_web(t *testing.T) {
tests := []struct {
name string
isTTY bool
wantStderr string
wantStdout string
}{
{
name: "tty",
isTTY: true,
wantStderr: "Opening github.com/OWNER/REPO/pull/123/checks in your browser.\n",
wantStdout: "",
},
{
name: "nontty",
isTTY: false,
wantStderr: "",
wantStdout: "",
},
}
reg := &httpmock.Registry{}
opts := &ChecksOptions{
WebMode: true,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
SelectorArg: "123",
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
reg.Register(
httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.FileResponse("./fixtures/allPassing.json"))
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(tc.isTTY)
io.SetStdinTTY(tc.isTTY)
io.SetStderrTTY(tc.isTTY)
opts.IO = io
cs, teardown := test.InitCmdStubber()
defer teardown()
cs.Stub("") // browser open
err := checksRun(opts)
assert.NoError(t, err)
assert.Equal(t, tc.wantStdout, stdout.String())
assert.Equal(t, tc.wantStderr, stderr.String())
reg.Verify(t)
})
}
}