X Tutup
Skip to content

Commit b205faa

Browse files
authored
Implement --web for gh pr checks (cli#2146)
1 parent 0061721 commit b205faa

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

pkg/cmd/pr/checks/checks.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type ChecksOptions struct {
2424
Branch func() (string, error)
2525
Remotes func() (context.Remotes, error)
2626

27+
WebMode bool
28+
2729
SelectorArg string
2830
}
2931

@@ -60,6 +62,8 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co
6062
},
6163
}
6264

65+
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser to show details about checks")
66+
6367
return cmd
6468
}
6569

@@ -70,7 +74,7 @@ func checksRun(opts *ChecksOptions) error {
7074
}
7175
apiClient := api.NewClientFromHTTP(httpClient)
7276

73-
pr, _, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg)
77+
pr, baseRepo, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg)
7478
if err != nil {
7579
return err
7680
}
@@ -84,6 +88,16 @@ func checksRun(opts *ChecksOptions) error {
8488
return fmt.Errorf("no checks reported on the '%s' branch", pr.BaseRefName)
8589
}
8690

91+
isTerminal := opts.IO.IsStdoutTTY()
92+
93+
if opts.WebMode {
94+
openURL := ghrepo.GenerateRepoURL(baseRepo, "pull/%d/checks", pr.Number)
95+
if isTerminal {
96+
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
97+
}
98+
return utils.OpenInBrowser(openURL)
99+
}
100+
87101
passing := 0
88102
failing := 0
89103
pending := 0
@@ -164,9 +178,8 @@ func checksRun(opts *ChecksOptions) error {
164178
if b0 == b1 {
165179
if n0 == n1 {
166180
return l0 < l1
167-
} else {
168-
return n0 < n1
169181
}
182+
return n0 < n1
170183
}
171184

172185
return (b0 == "fail") || (b0 == "pending" && b1 == "success")
@@ -175,7 +188,7 @@ func checksRun(opts *ChecksOptions) error {
175188
tp := utils.NewTablePrinter(opts.IO)
176189

177190
for _, o := range outputs {
178-
if opts.IO.IsStdoutTTY() {
191+
if isTerminal {
179192
tp.AddField(o.mark, nil, o.markColor)
180193
tp.AddField(o.name, nil, nil)
181194
tp.AddField(o.elapsed, nil, nil)
@@ -211,7 +224,7 @@ func checksRun(opts *ChecksOptions) error {
211224
summary = fmt.Sprintf("%s\n%s", cs.Bold(summary), tallies)
212225
}
213226

214-
if opts.IO.IsStdoutTTY() {
227+
if isTerminal {
215228
fmt.Fprintln(opts.IO.Out, summary)
216229
fmt.Fprintln(opts.IO.Out)
217230
}

pkg/cmd/pr/checks/checks_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/cli/cli/pkg/cmdutil"
1010
"github.com/cli/cli/pkg/httpmock"
1111
"github.com/cli/cli/pkg/iostreams"
12+
"github.com/cli/cli/test"
1213
"github.com/google/shlex"
1314
"github.com/stretchr/testify/assert"
1415
)
@@ -200,3 +201,63 @@ func Test_checksRun(t *testing.T) {
200201
})
201202
}
202203
}
204+
205+
func TestChecksRun_web(t *testing.T) {
206+
tests := []struct {
207+
name string
208+
isTTY bool
209+
wantStderr string
210+
wantStdout string
211+
}{
212+
{
213+
name: "tty",
214+
isTTY: true,
215+
wantStderr: "Opening github.com/OWNER/REPO/pull/123/checks in your browser.\n",
216+
wantStdout: "",
217+
},
218+
{
219+
name: "nontty",
220+
isTTY: false,
221+
wantStderr: "",
222+
wantStdout: "",
223+
},
224+
}
225+
226+
reg := &httpmock.Registry{}
227+
228+
opts := &ChecksOptions{
229+
WebMode: true,
230+
HttpClient: func() (*http.Client, error) {
231+
return &http.Client{Transport: reg}, nil
232+
},
233+
BaseRepo: func() (ghrepo.Interface, error) {
234+
return ghrepo.New("OWNER", "REPO"), nil
235+
},
236+
SelectorArg: "123",
237+
}
238+
239+
for _, tc := range tests {
240+
t.Run(tc.name, func(t *testing.T) {
241+
reg.Register(
242+
httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.FileResponse("./fixtures/allPassing.json"))
243+
244+
io, _, stdout, stderr := iostreams.Test()
245+
io.SetStdoutTTY(tc.isTTY)
246+
io.SetStdinTTY(tc.isTTY)
247+
io.SetStderrTTY(tc.isTTY)
248+
249+
opts.IO = io
250+
251+
cs, teardown := test.InitCmdStubber()
252+
defer teardown()
253+
254+
cs.Stub("") // browser open
255+
256+
err := checksRun(opts)
257+
assert.NoError(t, err)
258+
assert.Equal(t, tc.wantStdout, stdout.String())
259+
assert.Equal(t, tc.wantStderr, stderr.String())
260+
reg.Verify(t)
261+
})
262+
}
263+
}

0 commit comments

Comments
 (0)
X Tutup