Skip to content

Commit 026dc46

Browse files
committed
Merge remote-tracking branch 'origin' into artifact-download
2 parents a449a1a + b6277e7 commit 026dc46

File tree

15 files changed

+1508
-35
lines changed

15 files changed

+1508
-35
lines changed

cmd/gen-docs/main.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"strings"
78

89
"github.com/cli/cli/internal/docs"
@@ -13,27 +14,30 @@ import (
1314
)
1415

1516
func main() {
16-
var flagError pflag.ErrorHandling
17-
docCmd := pflag.NewFlagSet("", flagError)
18-
manPage := docCmd.BoolP("man-page", "", false, "Generate manual pages")
19-
website := docCmd.BoolP("website", "", false, "Generate website pages")
20-
dir := docCmd.StringP("doc-path", "", "", "Path directory where you want generate doc files")
21-
help := docCmd.BoolP("help", "h", false, "Help about any command")
22-
23-
if err := docCmd.Parse(os.Args); err != nil {
17+
if err := run(os.Args); err != nil {
18+
fmt.Fprintln(os.Stderr, err)
2419
os.Exit(1)
2520
}
21+
}
22+
23+
func run(args []string) error {
24+
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
25+
manPage := flags.BoolP("man-page", "", false, "Generate manual pages")
26+
website := flags.BoolP("website", "", false, "Generate website pages")
27+
dir := flags.StringP("doc-path", "", "", "Path directory where you want generate doc files")
28+
help := flags.BoolP("help", "h", false, "Help about any command")
29+
30+
if err := flags.Parse(args); err != nil {
31+
return err
32+
}
2633

2734
if *help {
28-
_, err := fmt.Fprintf(os.Stderr, "Usage of %s:\n\n%s", os.Args[0], docCmd.FlagUsages())
29-
if err != nil {
30-
fatal(err)
31-
}
32-
os.Exit(1)
35+
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n%s", filepath.Base(args[0]), flags.FlagUsages())
36+
return nil
3337
}
3438

3539
if *dir == "" {
36-
fatal("no dir set")
40+
return fmt.Errorf("error: --doc-path not set")
3741
}
3842

3943
io, _, _, _ := iostreams.Test()
@@ -43,15 +47,13 @@ func main() {
4347
}, "", "")
4448
rootCmd.InitDefaultHelpCmd()
4549

46-
err := os.MkdirAll(*dir, 0755)
47-
if err != nil {
48-
fatal(err)
50+
if err := os.MkdirAll(*dir, 0755); err != nil {
51+
return err
4952
}
5053

5154
if *website {
52-
err = docs.GenMarkdownTreeCustom(rootCmd, *dir, filePrepender, linkHandler)
53-
if err != nil {
54-
fatal(err)
55+
if err := docs.GenMarkdownTreeCustom(rootCmd, *dir, filePrepender, linkHandler); err != nil {
56+
return err
5557
}
5658
}
5759

@@ -62,11 +64,12 @@ func main() {
6264
Source: "",
6365
Manual: "",
6466
}
65-
err = docs.GenManTree(rootCmd, header, *dir)
66-
if err != nil {
67-
fatal(err)
67+
if err := docs.GenManTree(rootCmd, header, *dir); err != nil {
68+
return err
6869
}
6970
}
71+
72+
return nil
7073
}
7174

7275
func filePrepender(filename string) string {
@@ -82,11 +85,6 @@ func linkHandler(name string) string {
8285
return fmt.Sprintf("./%s", strings.TrimSuffix(name, ".md"))
8386
}
8487

85-
func fatal(msg interface{}) {
86-
fmt.Fprintln(os.Stderr, msg)
87-
os.Exit(1)
88-
}
89-
9088
type browser struct{}
9189

9290
func (b *browser) Browse(url string) error {

cmd/gen-docs/main_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func Test_run(t *testing.T) {
10+
dir := t.TempDir()
11+
args := []string{"--man-page", "--website", "--doc-path", dir}
12+
err := run(args)
13+
if err != nil {
14+
t.Fatalf("got error: %v", err)
15+
}
16+
17+
manPage, err := ioutil.ReadFile(dir + "/gh-issue-create.1")
18+
if err != nil {
19+
t.Fatalf("error reading `gh-issue-create.1`: %v", err)
20+
}
21+
if !strings.Contains(string(manPage), `\fBgh issue create`) {
22+
t.Fatal("man page corrupted")
23+
}
24+
25+
markdownPage, err := ioutil.ReadFile(dir + "/gh_issue_create.md")
26+
if err != nil {
27+
t.Fatalf("error reading `gh_issue_create.md`: %v", err)
28+
}
29+
if !strings.Contains(string(markdownPage), `## gh issue create`) {
30+
t.Fatal("markdown page corrupted")
31+
}
32+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/stretchr/testify v1.6.1
3434
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
3535
golang.org/x/sync v0.0.0-20190423024810-112230192c58
36+
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d
3637
golang.org/x/text v0.3.4 // indirect
3738
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
3839
)

pkg/cmd/run/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
cmdList "github.com/cli/cli/pkg/cmd/run/list"
66
cmdRerun "github.com/cli/cli/pkg/cmd/run/rerun"
77
cmdView "github.com/cli/cli/pkg/cmd/run/view"
8+
cmdWatch "github.com/cli/cli/pkg/cmd/run/watch"
89
"github.com/cli/cli/pkg/cmdutil"
910
"github.com/spf13/cobra"
1011
)
@@ -25,6 +26,7 @@ func NewCmdRun(f *cmdutil.Factory) *cobra.Command {
2526
cmd.AddCommand(cmdView.NewCmdView(f, nil))
2627
cmd.AddCommand(cmdRerun.NewCmdRerun(f, nil))
2728
cmd.AddCommand(cmdDownload.NewCmdDownload(f, nil))
29+
cmd.AddCommand(cmdWatch.NewCmdWatch(f, nil))
2830

2931
return cmd
3032
}

pkg/cmd/run/shared/shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func IsFailureState(c Conclusion) bool {
148148
}
149149

150150
type RunsPayload struct {
151+
TotalCount int `json:"total_count"`
151152
WorkflowRuns []Run `json:"workflow_runs"`
152153
}
153154

pkg/cmd/run/view/view.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ import (
2121
"github.com/spf13/cobra"
2222
)
2323

24+
type browser interface {
25+
Browse(string) error
26+
}
27+
2428
type ViewOptions struct {
2529
HttpClient func() (*http.Client, error)
2630
IO *iostreams.IOStreams
2731
BaseRepo func() (ghrepo.Interface, error)
32+
Browser browser
2833

2934
RunID string
3035
JobID string
3136
Verbose bool
3237
ExitStatus bool
3338
Log bool
39+
Web bool
3440

3541
Prompt bool
3642

@@ -42,6 +48,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4248
IO: f.IOStreams,
4349
HttpClient: f.HttpClient,
4450
Now: time.Now,
51+
Browser: f.Browser,
4552
}
4653
cmd := &cobra.Command{
4754
Use: "view [<run-id>]",
@@ -87,6 +94,10 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
8794
}
8895
}
8996

97+
if opts.Web && opts.Log {
98+
return &cmdutil.FlagError{Err: errors.New("specify only one of --web or --log")}
99+
}
100+
90101
if runF != nil {
91102
return runF(opts)
92103
}
@@ -98,6 +109,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
98109
cmd.Flags().BoolVar(&opts.ExitStatus, "exit-status", false, "Exit with non-zero status if run failed")
99110
cmd.Flags().StringVarP(&opts.JobID, "job", "j", "", "View a specific job ID from a run")
100111
cmd.Flags().BoolVar(&opts.Log, "log", false, "View full log for either a run or specific job")
112+
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open run in the browser")
101113

102114
return cmd
103115
}
@@ -171,6 +183,18 @@ func runView(opts *ViewOptions) error {
171183
}
172184
}
173185

186+
if opts.Web {
187+
url := run.URL
188+
if selectedJob != nil {
189+
url = selectedJob.URL + "?check_suite_focus=true"
190+
}
191+
if opts.IO.IsStdoutTTY() {
192+
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(url))
193+
}
194+
195+
return opts.Browser.Browse(url)
196+
}
197+
174198
opts.IO.StartProgressIndicator()
175199

176200
if opts.Log && selectedJob != nil {

pkg/cmd/run/view/view_test.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ func TestNewCmdView(t *testing.T) {
3737
Prompt: true,
3838
},
3939
},
40+
{
41+
name: "web tty",
42+
tty: true,
43+
cli: "--web",
44+
wants: ViewOptions{
45+
Prompt: true,
46+
Web: true,
47+
},
48+
},
49+
{
50+
name: "web nontty",
51+
cli: "1234 --web",
52+
wants: ViewOptions{
53+
Web: true,
54+
RunID: "1234",
55+
},
56+
},
57+
{
58+
name: "disallow web and log",
59+
tty: true,
60+
cli: "-w --log",
61+
wantsErr: true,
62+
},
4063
{
4164
name: "exit status",
4265
cli: "--exit-status 1234",
@@ -128,13 +151,14 @@ func TestNewCmdView(t *testing.T) {
128151
func TestViewRun(t *testing.T) {
129152

130153
tests := []struct {
131-
name string
132-
httpStubs func(*httpmock.Registry)
133-
askStubs func(*prompt.AskStubber)
134-
opts *ViewOptions
135-
tty bool
136-
wantErr bool
137-
wantOut string
154+
name string
155+
httpStubs func(*httpmock.Registry)
156+
askStubs func(*prompt.AskStubber)
157+
opts *ViewOptions
158+
tty bool
159+
wantErr bool
160+
wantOut string
161+
browsedURL string
138162
}{
139163
{
140164
name: "associate with PR",
@@ -492,6 +516,39 @@ func TestViewRun(t *testing.T) {
492516
},
493517
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",
494518
},
519+
{
520+
name: "web run",
521+
tty: true,
522+
opts: &ViewOptions{
523+
RunID: "3",
524+
Web: true,
525+
},
526+
httpStubs: func(reg *httpmock.Registry) {
527+
reg.Register(
528+
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/3"),
529+
httpmock.JSONResponse(shared.SuccessfulRun))
530+
},
531+
browsedURL: "runs/3",
532+
wantOut: "Opening runs/3 in your browser.\n",
533+
},
534+
{
535+
name: "web job",
536+
tty: true,
537+
opts: &ViewOptions{
538+
JobID: "10",
539+
Web: true,
540+
},
541+
httpStubs: func(reg *httpmock.Registry) {
542+
reg.Register(
543+
httpmock.REST("GET", "repos/OWNER/REPO/actions/jobs/10"),
544+
httpmock.JSONResponse(shared.SuccessfulJob))
545+
reg.Register(
546+
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/3"),
547+
httpmock.JSONResponse(shared.SuccessfulRun))
548+
},
549+
browsedURL: "jobs/10?check_suite_focus=true",
550+
wantOut: "Opening jobs/10 in your browser.\n",
551+
},
495552
}
496553

497554
for _, tt := range tests {
@@ -519,6 +576,9 @@ func TestViewRun(t *testing.T) {
519576
tt.askStubs(as)
520577
}
521578

579+
browser := &cmdutil.TestBrowser{}
580+
tt.opts.Browser = browser
581+
522582
t.Run(tt.name, func(t *testing.T) {
523583
err := runView(tt.opts)
524584
if tt.wantErr {
@@ -531,6 +591,9 @@ func TestViewRun(t *testing.T) {
531591
assert.NoError(t, err)
532592
}
533593
assert.Equal(t, tt.wantOut, stdout.String())
594+
if tt.browsedURL != "" {
595+
assert.Equal(t, tt.browsedURL, browser.BrowsedURL())
596+
}
534597
reg.Verify(t)
535598
})
536599
}

0 commit comments

Comments
 (0)