Skip to content
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

SUP-2321 Accept field data for bk job unblock #298

Merged
merged 2 commits into from
Jun 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
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