Skip to content

Commit e15189f

Browse files
committed
Address PR comments
1 parent d1b153c commit e15189f

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

pkg/cmd/workflow/view/view.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7-
"path/filepath"
87
"strings"
98

109
"github.com/MakeNowJust/heredoc"
@@ -41,7 +40,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4140
}
4241

4342
cmd := &cobra.Command{
44-
Use: "view [<run-id> | <file name>]",
43+
Use: "view [<workflow-id> | <workflow name> | <file name>]",
4544
Short: "View the summary of a workflow",
4645
Args: cobra.MaximumNArgs(1),
4746
Hidden: true,
@@ -56,7 +55,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
5655
// support `-R, --repo` override
5756
opts.BaseRepo = f.BaseRepo
5857

59-
opts.Raw = !opts.IO.CanPrompt()
58+
opts.Raw = !opts.IO.IsStdoutTTY()
6059

6160
if len(args) > 0 {
6261
opts.Selector = args[0]
@@ -66,6 +65,10 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
6665
opts.Prompt = true
6766
}
6867

68+
if !opts.YAML && opts.Ref != "" {
69+
return &cmdutil.FlagError{Err: errors.New("`--yaml` required when specifying `--ref`")}
70+
}
71+
6972
if runF != nil {
7073
return runF(opts)
7174
}
@@ -101,7 +104,6 @@ func runView(opts *ViewOptions) error {
101104

102105
if opts.Web {
103106
var url string
104-
hostname := repo.RepoHost()
105107
if opts.YAML {
106108
ref := opts.Ref
107109
if ref == "" {
@@ -112,10 +114,9 @@ func runView(opts *ViewOptions) error {
112114
return err
113115
}
114116
}
115-
url = fmt.Sprintf("https://%s/%s/blob/%s/%s", hostname, ghrepo.FullName(repo), ref, workflow.Path)
117+
url = ghrepo.GenerateRepoURL(repo, "blob/%s/%s", ref, workflow.Path)
116118
} else {
117-
baseName := filepath.Base(workflow.Path)
118-
url = fmt.Sprintf("https://%s/%s/actions/workflows/%s", hostname, ghrepo.FullName(repo), baseName)
119+
url = ghrepo.GenerateRepoURL(repo, "actions/workflows/%s", workflow.Base())
119120
}
120121
if opts.IO.IsStdoutTTY() {
121122
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(url))
@@ -145,14 +146,11 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
145146
yaml, err := getWorkflowContent(client, repo, opts.Ref, workflow)
146147
opts.IO.StopProgressIndicator()
147148
if err != nil {
148-
if s, ok := err.(api.HTTPError); ok {
149-
if s.StatusCode == 404 {
150-
base := filepath.Base(workflow.Path)
151-
if opts.Ref != "" {
152-
return fmt.Errorf("could not find workflow file %s on %s, try specifying a different ref", base, opts.Ref)
153-
}
154-
return fmt.Errorf("could not find workflow file %s, try specifying a branch or tag using --ref", base)
149+
if s, ok := err.(api.HTTPError); ok && s.StatusCode == 404 {
150+
if opts.Ref != "" {
151+
return fmt.Errorf("could not find workflow file %s on %s, try specifying a different ref", workflow.Base(), opts.Ref)
155152
}
153+
return fmt.Errorf("could not find workflow file %s, try specifying a branch or tag using `--ref`", workflow.Base())
156154
}
157155
return fmt.Errorf("could not get workflow file content: %w", err)
158156
}
@@ -168,7 +166,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
168166
cs := opts.IO.ColorScheme()
169167
out := opts.IO.Out
170168

171-
fileName := filepath.Base(workflow.Path)
169+
fileName := workflow.Base()
172170
fmt.Fprintf(out, "%s - %s\n", cs.Bold(workflow.Name), cs.Gray(fileName))
173171
fmt.Fprintf(out, "ID: %s", cs.Cyanf("%d", workflow.ID))
174172

@@ -215,7 +213,7 @@ func viewWorkflowInfo(opts *ViewOptions, client *api.Client, workflow *shared.Wo
215213
tp := utils.NewTablePrinter(opts.IO)
216214

217215
// Header
218-
filename := filepath.Base(workflow.Path)
216+
filename := workflow.Base()
219217
fmt.Fprintf(out, "%s - %s\n", cs.Bold(workflow.Name), cs.Cyan(filename))
220218
fmt.Fprintf(out, "ID: %s\n\n", cs.Cyanf("%d", workflow.ID))
221219

pkg/cmd/workflow/view/view_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,32 @@ func TestNewCmdView(t *testing.T) {
9090
},
9191
},
9292
{
93-
name: "ref tty",
94-
cli: "--ref 456",
93+
name: "ref tty",
94+
cli: "--ref 456",
95+
tty: true,
96+
wantsErr: true,
97+
},
98+
{
99+
name: "ref nontty",
100+
cli: "123 -r 456",
101+
wantsErr: true,
102+
},
103+
{
104+
name: "yaml ref tty",
105+
cli: "--yaml --ref 456",
95106
tty: true,
96107
wants: ViewOptions{
97108
Prompt: true,
109+
YAML: true,
98110
Ref: "456",
99111
},
100112
},
101113
{
102-
name: "ref nontty",
103-
cli: "123 -r 456",
114+
name: "yaml ref nontty",
115+
cli: "123 -y -r 456",
104116
wants: ViewOptions{
105117
Raw: true,
118+
YAML: true,
106119
Ref: "456",
107120
Selector: "123",
108121
},
@@ -319,7 +332,7 @@ func TestViewRun(t *testing.T) {
319332
)
320333
},
321334
wantErr: true,
322-
wantErrOut: "could not find workflow file flow.yml, try specifying a branch or tag using --ref",
335+
wantErrOut: "could not find workflow file flow.yml, try specifying a branch or tag using `--ref`",
323336
},
324337
{
325338
name: "workflow with yaml and ref",

0 commit comments

Comments
 (0)