Skip to content

Commit b945534

Browse files
authored
feat(build): add job states filter to build view command (#735)
* feat(build): add job states filter to build view command Adds a --job-states (-s) flag to `bk build view` that filters jobs by state using the new JobStates option in go-buildkite v4.17.0. * test(build): add tests for build view job states filter Extract buildGetOptions method on ViewCmd and add unit tests covering multiple, single, and empty job state filtering.
1 parent 2a9bbea commit b945534

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

cmd/build/view.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import (
2121
)
2222

2323
type ViewCmd struct {
24-
BuildNumber string `arg:"" optional:"" help:"Build number to view (omit for most recent build)"`
25-
Pipeline string `help:"The pipeline to use. This can be a {pipeline slug} or in the format {org slug}/{pipeline slug}." short:"p"`
26-
Branch string `help:"Filter builds to this branch." short:"b"`
27-
User string `help:"Filter builds to this user. You can use name or email." short:"u" xor:"userfilter"`
28-
Mine bool `help:"Filter builds to only my user." xor:"userfilter"`
29-
Web bool `help:"Open the build in a web browser." short:"w"`
24+
BuildNumber string `arg:"" optional:"" help:"Build number to view (omit for most recent build)"`
25+
Pipeline string `help:"The pipeline to use. This can be a {pipeline slug} or in the format {org slug}/{pipeline slug}." short:"p"`
26+
Branch string `help:"Filter builds to this branch." short:"b"`
27+
User string `help:"Filter builds to this user. You can use name or email." short:"u" xor:"userfilter"`
28+
Mine bool `help:"Filter builds to only my user." xor:"userfilter"`
29+
JobStates []string `help:"Filter jobs by state. Valid states: running, scheduled, passed, failed, canceled, skipped, not_run, broken." short:"s" sep:","`
30+
Web bool `help:"Open the build in a web browser." short:"w"`
3031
output.OutputFlags
3132
}
3233

@@ -55,11 +56,21 @@ Examples:
5556
# A shortcut to view your builds is --mine
5657
$ bk build view --mine
5758
59+
# Filter to only show failed and broken jobs
60+
$ bk build view -s failed,broken
61+
5862
# You can combine most of these flags
5963
# To view most recent build by greg on the deploy-pipeline
6064
$ bk build view -p deploy-pipeline -u "greg"`
6165
}
6266

67+
func (c *ViewCmd) buildGetOptions() *buildkite.BuildGetOptions {
68+
if len(c.JobStates) > 0 {
69+
return &buildkite.BuildGetOptions{JobStates: c.JobStates}
70+
}
71+
return nil
72+
}
73+
6374
func (c *ViewCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
6475
f, err := factory.New(factory.WithDebug(globals.EnableDebug()))
6576
if err != nil {
@@ -150,7 +161,7 @@ func (c *ViewCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
150161
opts.Organization,
151162
opts.Pipeline,
152163
fmt.Sprint(opts.BuildNumber),
153-
nil,
164+
c.buildGetOptions(),
154165
)
155166
if apiErr != nil {
156167
mu.Lock()

cmd/build/view_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package build
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestViewCmd_BuildGetOptions_WithJobStates(t *testing.T) {
8+
cmd := &ViewCmd{
9+
JobStates: []string{"failed", "broken"},
10+
}
11+
12+
opts := cmd.buildGetOptions()
13+
if opts == nil {
14+
t.Fatal("Expected non-nil BuildGetOptions")
15+
}
16+
17+
if len(opts.JobStates) != 2 {
18+
t.Fatalf("Expected 2 job states, got %d", len(opts.JobStates))
19+
}
20+
21+
if opts.JobStates[0] != "failed" {
22+
t.Errorf("Expected first state to be 'failed', got %q", opts.JobStates[0])
23+
}
24+
25+
if opts.JobStates[1] != "broken" {
26+
t.Errorf("Expected second state to be 'broken', got %q", opts.JobStates[1])
27+
}
28+
}
29+
30+
func TestViewCmd_BuildGetOptions_Empty(t *testing.T) {
31+
cmd := &ViewCmd{}
32+
33+
opts := cmd.buildGetOptions()
34+
if opts != nil {
35+
t.Errorf("Expected nil BuildGetOptions when no job states, got %+v", opts)
36+
}
37+
}
38+
39+
func TestViewCmd_BuildGetOptions_SingleState(t *testing.T) {
40+
cmd := &ViewCmd{
41+
JobStates: []string{"running"},
42+
}
43+
44+
opts := cmd.buildGetOptions()
45+
if opts == nil {
46+
t.Fatal("Expected non-nil BuildGetOptions")
47+
}
48+
49+
if len(opts.JobStates) != 1 {
50+
t.Fatalf("Expected 1 job state, got %d", len(opts.JobStates))
51+
}
52+
53+
if opts.JobStates[0] != "running" {
54+
t.Errorf("Expected state to be 'running', got %q", opts.JobStates[0])
55+
}
56+
}

0 commit comments

Comments
 (0)