Skip to content

Commit 5d2bd04

Browse files
JoeColeman95mcncl
andauthored
Migrate job commands from Cobra to Kong (#577)
* Migrating cancel.go to Kong in new location * Migrate job list to Kong * Migrating job retry to Kong * Migrate job unblock to Kong, removing def in root.go * Moving GraphQL schemas to cmd/job/graphql --------- Co-authored-by: Ben McNicholl <[email protected]>
1 parent a1ddc14 commit 5d2bd04

File tree

18 files changed

+418
-398
lines changed

18 files changed

+418
-398
lines changed

cmd/job/cancel.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package job
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/alecthomas/kong"
8+
"github.com/buildkite/cli/v3/internal/cli"
9+
"github.com/buildkite/cli/v3/internal/graphql"
10+
bk_io "github.com/buildkite/cli/v3/internal/io"
11+
"github.com/buildkite/cli/v3/internal/util"
12+
"github.com/buildkite/cli/v3/internal/version"
13+
"github.com/buildkite/cli/v3/pkg/cmd/factory"
14+
"github.com/buildkite/cli/v3/pkg/cmd/validation"
15+
)
16+
17+
type CancelCmd struct {
18+
JobID string `arg:"" help:"Job ID to cancel" required:""`
19+
Web bool `help:"Open the job in a web browser after it has been cancelled" short:"w"`
20+
}
21+
22+
func (c *CancelCmd) Help() string {
23+
return `
24+
Examples:
25+
# Cancel a job (with confirmation prompt)
26+
$ bk job cancel 0190046e-e199-453b-a302-a21a4d649d31
27+
28+
# Cancel a job without confirmation (useful for automation)
29+
$ bk job --yes cancel 0190046e-e199-453b-a302-a21a4d649d31
30+
31+
# Cancel a job and open it in browser
32+
$ bk job --yes cancel 0190046e-e199-453b-a302-a21a4d649d31 --web
33+
`
34+
}
35+
36+
func (c *CancelCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
37+
f, err := factory.New(version.Version)
38+
if err != nil {
39+
return err
40+
}
41+
42+
f.SkipConfirm = globals.SkipConfirmation()
43+
f.NoInput = globals.DisableInput()
44+
f.Quiet = globals.IsQuiet()
45+
46+
if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil {
47+
return err
48+
}
49+
50+
ctx := context.Background()
51+
graphqlID := util.GenerateGraphQLID("JobTypeCommand---", c.JobID)
52+
53+
confirmed, err := bk_io.Confirm(f, fmt.Sprintf("Cancel job %s", c.JobID))
54+
if err != nil {
55+
return err
56+
}
57+
58+
if !confirmed {
59+
return nil
60+
}
61+
62+
return c.cancelJob(ctx, c.JobID, graphqlID, f)
63+
}
64+
65+
func (c *CancelCmd) cancelJob(ctx context.Context, displayID, apiID string, f *factory.Factory) error {
66+
var err error
67+
var result *graphql.CancelJobResponse
68+
spinErr := bk_io.SpinWhile(f, fmt.Sprintf("Cancelling job %s", displayID), func() {
69+
result, err = graphql.CancelJob(ctx, f.GraphQLClient, apiID)
70+
})
71+
if spinErr != nil {
72+
return spinErr
73+
}
74+
if err != nil {
75+
return err
76+
}
77+
78+
job := result.JobTypeCommandCancel.JobTypeCommand
79+
fmt.Printf("Job canceled: %s\n", job.Url)
80+
81+
return util.OpenInWebBrowser(c.Web, job.Url)
82+
}

cmd/job/cancel_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package job
2+
3+
import (
4+
"testing"
5+
6+
"github.com/buildkite/cli/v3/internal/util"
7+
)
8+
9+
func TestCancelCmdStructure(t *testing.T) {
10+
t.Parallel()
11+
12+
cmd := &CancelCmd{
13+
JobID: "01993829-d2e7-4834-9611-bbeb8c1290db",
14+
Web: true,
15+
}
16+
17+
if cmd.JobID == "" {
18+
t.Error("JobID should be set")
19+
}
20+
21+
if !cmd.Web {
22+
t.Error("Web flag should be true")
23+
}
24+
}
25+
26+
func TestGraphQLIDGeneration(t *testing.T) {
27+
t.Parallel()
28+
29+
jobUUID := "01993829-d2e7-4834-9611-bbeb8c1290db"
30+
graphqlID := util.GenerateGraphQLID("JobTypeCommand---", jobUUID)
31+
32+
if graphqlID == "" {
33+
t.Error("GraphQL ID should not be empty")
34+
}
35+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ mutation CancelJob($jobId: ID!) {
88
url
99
}
1010
}
11-
}
11+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@ query ListJobsByState($org: ID!, $state: [JobStates!], $first: Int, $after: Stri
115115
}
116116
}
117117
}
118-
}
118+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ mutation RetryJob($id: ID!) {
77
}
88
}
99
}
10-

0 commit comments

Comments
 (0)