From 7fc9295786f100df848706f48ae66e37d1698659 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Sun, 4 Apr 2021 21:27:20 -0500 Subject: [PATCH] support --web for run view --- pkg/cmd/run/view/view.go | 20 ++++++++++++ pkg/cmd/run/view/view_test.go | 59 ++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/run/view/view.go b/pkg/cmd/run/view/view.go index d04668ae8..a15c51e74 100644 --- a/pkg/cmd/run/view/view.go +++ b/pkg/cmd/run/view/view.go @@ -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 []", @@ -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) diff --git a/pkg/cmd/run/view/view_test.go b/pkg/cmd/run/view/view_test.go index 3149957c0..5628cd146 100644 --- a/pkg/cmd/run/view/view_test.go +++ b/pkg/cmd/run/view/view_test.go @@ -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) }) }