support --web for run view

This commit is contained in:
vilmibm 2021-04-04 21:27:20 -05:00
parent 216cfb631f
commit 7fc9295786
2 changed files with 72 additions and 7 deletions

View file

@ -20,16 +20,22 @@ import (
"github.com/spf13/cobra"
)
type browser interface {
Browse(string) error
}
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Browser browser
RunID string
JobID string
Verbose bool
ExitStatus bool
Log bool
Web bool
Prompt bool
@ -41,6 +47,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
IO: f.IOStreams,
HttpClient: f.HttpClient,
Now: time.Now,
Browser: f.Browser,
}
cmd := &cobra.Command{
Use: "view [<run-id>]",
@ -86,6 +93,10 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
}
}
if opts.Web && opts.Log {
return &cmdutil.FlagError{Err: errors.New("only one of --web or --log can be passed at a time")}
}
if runF != nil {
return runF(opts)
}
@ -97,6 +108,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
cmd.Flags().BoolVar(&opts.ExitStatus, "exit-status", false, "Exit with non-zero status if run failed")
cmd.Flags().StringVarP(&opts.JobID, "job", "j", "", "View a specific job ID from a run")
cmd.Flags().BoolVar(&opts.Log, "log", false, "View full log for either a run or specific job")
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open run in the browser")
return cmd
}
@ -155,6 +167,14 @@ func runView(opts *ViewOptions) error {
return fmt.Errorf("failed to get run: %w", err)
}
if opts.Web {
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(run.URL))
}
return opts.Browser.Browse(run.URL)
}
if opts.Prompt {
opts.IO.StartProgressIndicator()
jobs, err = shared.GetJobs(client, repo, *run)

View file

@ -36,6 +36,29 @@ func TestNewCmdView(t *testing.T) {
Prompt: true,
},
},
{
name: "web tty",
tty: true,
cli: "--web",
wants: ViewOptions{
Prompt: true,
Web: true,
},
},
{
name: "web nontty",
cli: "1234 --web",
wants: ViewOptions{
Web: true,
RunID: "1234",
},
},
{
name: "disallow web and log",
tty: true,
cli: "-w --log",
wantsErr: true,
},
{
name: "exit status",
cli: "--exit-status 1234",
@ -127,13 +150,14 @@ func TestNewCmdView(t *testing.T) {
func TestViewRun(t *testing.T) {
tests := []struct {
name string
httpStubs func(*httpmock.Registry)
askStubs func(*prompt.AskStubber)
opts *ViewOptions
tty bool
wantErr bool
wantOut string
name string
httpStubs func(*httpmock.Registry)
askStubs func(*prompt.AskStubber)
opts *ViewOptions
tty bool
wantErr bool
wantOut string
browsedURL string
}{
{
name: "associate with PR",
@ -431,6 +455,21 @@ func TestViewRun(t *testing.T) {
},
wantOut: "\n✓ trunk successful · 3\nTriggered via push about 59 minutes ago\n\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\nTo see the full job log, try: gh run view --log --job=10\nview this run on GitHub: runs/3\n",
},
{
name: "web",
tty: true,
opts: &ViewOptions{
RunID: "3",
Web: true,
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/3"),
httpmock.JSONResponse(shared.SuccessfulRun))
},
browsedURL: "runs/3",
wantOut: "Opening runs/3 in your browser.\n",
},
}
for _, tt := range tests {
@ -458,6 +497,9 @@ func TestViewRun(t *testing.T) {
tt.askStubs(as)
}
browser := &cmdutil.TestBrowser{}
tt.opts.Browser = browser
t.Run(tt.name, func(t *testing.T) {
err := runView(tt.opts)
if tt.wantErr {
@ -470,6 +512,9 @@ func TestViewRun(t *testing.T) {
assert.NoError(t, err)
}
assert.Equal(t, tt.wantOut, stdout.String())
if tt.browsedURL != "" {
assert.Equal(t, tt.browsedURL, browser.BrowsedURL())
}
reg.Verify(t)
})
}