Skip to content

Support job retrying #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2024
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
98 changes: 98 additions & 0 deletions internal/graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import (
"encoding/base64"
"fmt"
"strings"
)

func GenerateGraphQLID(prefix, uuid string) string {
var graphqlID strings.Builder
wr := base64.NewEncoder(base64.StdEncoding, &graphqlID)
fmt.Fprintf(wr, "%s%s", prefix, uuid)
wr.Close()

return graphqlID.String()
}
1 change: 1 addition & 0 deletions pkg/cmd/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewCmdJob(f *factory.Factory) *cobra.Command {
}

cmd.AddCommand(NewCmdJobUnblock(f))
cmd.AddCommand(NewCmdJobRetry(f))

return &cmd
}
50 changes: 50 additions & 0 deletions pkg/cmd/job/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package job

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/graphql"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/cobra"
)

const jobCommandPrefix = "JobTypeCommand---"

func NewCmdJobRetry(f *factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "retry <job id>",
DisableFlagsInUseLine: true,
Short: "Retry a job",
Long: heredoc.Doc(`
Use this command to retry build jobs.
`),
Args: cobra.ExactArgs(1),
Example: "$ bk job retry 0190046e-e199-453b-a302-a21a4d649d31",
RunE: func(cmd *cobra.Command, args []string) error {
// given a job UUID argument, we need to generate the GraphQL ID matching
uuid := args[0]
graphqlID := util.GenerateGraphQLID(jobCommandPrefix, uuid)

var err error
var j *graphql.RetryJobResponse
spinErr := spinner.New().
Title("Retrying job").
Action(func() {
j, err = graphql.RetryJob(cmd.Context(), f.GraphQLClient, graphqlID)
}).
Run()
if spinErr != nil {
return spinErr
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), "Successfully retried job: "+j.JobTypeCommandRetry.JobTypeCommand.Url)

return err
},
}

return cmd
}
10 changes: 10 additions & 0 deletions pkg/cmd/job/retry.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation RetryJob($id: ID!) {
jobTypeCommandRetry(input: {id: $id}) {
jobTypeCommand {
id
state
url
}
}
}

15 changes: 3 additions & 12 deletions pkg/cmd/job/unblock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package job

import (
"encoding/base64"
"errors"
"fmt"
"io"
Expand All @@ -10,13 +9,14 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/graphql"
bk_io "github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/cobra"
"github.com/vektah/gqlparser/v2/gqlerror"
)

const jobCommandPrefix = "JobTypeBlock---"
const jobBlockPrefix = "JobTypeBlock---"

func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {
var data string
Expand All @@ -34,7 +34,7 @@ func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
// given a job UUID argument, we need to generate the GraphQL ID matching
uuid := args[0]
graphqlID := generateGraphQLID(uuid)
graphqlID := util.GenerateGraphQLID(jobBlockPrefix, uuid)

// get unblock step fields if available
var fields *string
Expand Down Expand Up @@ -89,12 +89,3 @@ func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {

return cmd
}

func generateGraphQLID(uuid string) string {
var graphqlID strings.Builder
wr := base64.NewEncoder(base64.StdEncoding, &graphqlID)
fmt.Fprintf(wr, "%s%s", jobCommandPrefix, uuid)
wr.Close()

return graphqlID.String()
}