Skip to content

Commit

Permalink
SUP-2321 Accept field data for bk job unblock (#298)
Browse files Browse the repository at this point in the history
* Add fields to unblock mutation

* Accept field data for unblock jobs
  • Loading branch information
jradtilbrook authored Jun 25, 2024
1 parent d6f2656 commit b61b351
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
4 changes: 4 additions & 0 deletions genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ operations:
optional: pointer

generated: internal/graphql/generated.go

bindings:
JSON:
type: string
14 changes: 10 additions & 4 deletions internal/graphql/generated.go

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

35 changes: 31 additions & 4 deletions pkg/cmd/job/unblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"encoding/base64"
"errors"
"fmt"
"io"
"strings"

"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/pkg/cmd/factory"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/cobra"
Expand All @@ -17,9 +19,12 @@ import (
const jobCommandPrefix = "JobTypeBlock---"

func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {
return &cobra.Command{
Use: "unblock <job id>",
Short: "Unblock a job",
var data string

cmd := &cobra.Command{
Use: "unblock <job id>",
DisableFlagsInUseLine: true,
Short: "Unblock a job",
Long: heredoc.Doc(`
Use this command to unblock build jobs.
Currently, this does not support submitting fields to the step.
Expand All @@ -31,11 +36,29 @@ func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {
uuid := args[0]
graphqlID := generateGraphQLID(uuid)

// get unblock step fields if available
var fields *string
if bk_io.HasDataAvailable(cmd.InOrStdin()) {
stdin := new(strings.Builder)
_, err := io.Copy(stdin, cmd.InOrStdin())
if err != nil {
return err
}
input := stdin.String()
fields = &input
} else if data != "" {
fields = &data
} else {
// the graphql API errors if providing a null fields value so we need to provide and empty json object
input := "{}"
fields = &input
}

var err error
spinErr := spinner.New().
Title("Unblocking job").
Action(func() {
_, err = graphql.UnblockJob(cmd.Context(), f.GraphQLClient, graphqlID)
_, err = graphql.UnblockJob(cmd.Context(), f.GraphQLClient, graphqlID, fields)
}).
Run()
if spinErr != nil {
Expand All @@ -61,6 +84,10 @@ func NewCmdJobUnblock(f *factory.Factory) *cobra.Command {
return err
},
}

cmd.Flags().StringVar(&data, "data", "", "JSON formatted data to unblock the job.")

return cmd
}

func generateGraphQLID(uuid string) string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/job/unblock.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation UnblockJob($id: ID!) {
jobTypeBlockUnblock(input: {id: $id}) {
mutation UnblockJob($id: ID!, $fields: JSON) {
jobTypeBlockUnblock(input: {id: $id, fields: $fields}) {
jobTypeBlock {
id
state
Expand Down

0 comments on commit b61b351

Please sign in to comment.