Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkg/cmd/run/shared/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ func RenderJobs(cs *iostreams.ColorScheme, jobs []Job, verbose bool) string {
return strings.Join(lines, "\n")
}

func RenderJobsCompact(cs *iostreams.ColorScheme, jobs []Job) string {
lines := []string{}
for _, job := range jobs {
elapsed := job.CompletedAt.Sub(job.StartedAt)
elapsedStr := fmt.Sprintf(" in %s", elapsed)
if elapsed < 0 {
elapsedStr = ""
}
symbol, symbolColor := Symbol(cs, job.Status, job.Conclusion)
id := cs.Cyanf("%d", job.ID)
lines = append(lines, fmt.Sprintf("%s %s%s (ID %s)", symbolColor(symbol), cs.Bold(job.Name), elapsedStr, id))

if job.Status == Completed && job.Conclusion == Success {
continue
}

var inProgressStepLine string
var failedStepLines []string

for _, step := range job.Steps {
stepSymbol, stepSymColor := Symbol(cs, step.Status, step.Conclusion)
stepLine := fmt.Sprintf(" %s %s", stepSymColor(stepSymbol), step.Name)

if IsFailureState(step.Conclusion) {
failedStepLines = append(failedStepLines, stepLine)
}

if step.Status == InProgress {
inProgressStepLine = stepLine
}
}

lines = append(lines, failedStepLines...)

if inProgressStepLine != "" {
lines = append(lines, inProgressStepLine)
}
}

return strings.Join(lines, "\n")
}

func RenderAnnotations(cs *iostreams.ColorScheme, annotations []Annotation) string {
lines := []string{}

Expand Down
15 changes: 13 additions & 2 deletions pkg/cmd/run/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type WatchOptions struct {
RunID string
Interval int
ExitStatus bool
Compact bool

Prompt bool

Expand All @@ -48,13 +49,19 @@ func NewCmdWatch(f *cmdutil.Factory, runF func(*WatchOptions) error) *cobra.Comm
Long: heredoc.Docf(`
Watch a run until it completes, showing its progress.

By default, all steps are displayed. The %[1]s--compact%[1]s option can be used to only
show the relevant/failed steps.

This command does not support authenticating via fine grained PATs
as it is not currently possible to create a PAT with the %[1]schecks:read%[1]s permission.
`, "`"),
Example: heredoc.Doc(`
# Watch a run until it's done
$ gh run watch

# Watch a run in compact mode
$ gh run watch --compact
Comment on lines +62 to +63
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: This can be pulled up to appear after the simple gh run watch example.


# Run some other command when the run is finished
$ gh run watch && notify-send 'run is done!'
`),
Expand All @@ -78,6 +85,7 @@ func NewCmdWatch(f *cmdutil.Factory, runF func(*WatchOptions) error) *cobra.Comm
},
}
cmd.Flags().BoolVar(&opts.ExitStatus, "exit-status", false, "Exit with non-zero status if run fails")
cmd.Flags().BoolVar(&opts.Compact, "compact", false, "Show only relevant/failed steps")
cmd.Flags().IntVarP(&opts.Interval, "interval", "i", defaultInterval, "Refresh interval in seconds")

return cmd
Expand Down Expand Up @@ -252,8 +260,11 @@ func renderRun(out io.Writer, opts WatchOptions, client *api.Client, repo ghrepo
}

fmt.Fprintln(out, cs.Bold("JOBS"))

fmt.Fprintln(out, shared.RenderJobs(cs, jobs, true))
if opts.Compact {
fmt.Fprintln(out, shared.RenderJobsCompact(cs, jobs))
} else {
fmt.Fprintln(out, shared.RenderJobs(cs, jobs, true))
}

if missingAnnotationsPermissions {
fmt.Fprintln(out)
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/run/watch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func TestNewCmdWatch(t *testing.T) {
ExitStatus: true,
},
},
{
name: "compact status",
cli: "1234 --compact",
wants: WatchOptions{
Interval: defaultInterval,
RunID: "1234",
Compact: true,
},
},
Comment on lines +60 to +68
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a test case in TestWatchRun to cover the changes we made.

I think it's okay to just have a single test case that covers all cases. I know adding such tests is a bit annoying. 😄 So, let me know if I can help with that. 🙏

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@babakks: Regarding tests, I looked into it.
Apparently, there needs to be separate tests for in-progress and completed scenarios. 🤔
It'd be great if you added tests. Thanks!

}

for _, tt := range tests {
Expand Down
Loading