diff --git a/internal/cmd/stack/flags.go b/internal/cmd/stack/flags.go index 35850f3..d7fb1b0 100644 --- a/internal/cmd/stack/flags.go +++ b/internal/cmd/stack/flags.go @@ -13,6 +13,11 @@ var flagCommitSHA = &cli.StringFlag{ Usage: "Commit SHA for the newly created run", } +var flagRequiredCommitSHA = &cli.StringFlag{ + Name: "sha", + Usage: "SHA of the commit to set as canonical for the stack", +} + var flagRun = &cli.StringFlag{ Name: "run", Usage: "ID of the run", diff --git a/internal/cmd/stack/set_current_commit.go b/internal/cmd/stack/set_current_commit.go new file mode 100644 index 0000000..a46b93f --- /dev/null +++ b/internal/cmd/stack/set_current_commit.go @@ -0,0 +1,44 @@ +package stack + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/shurcooL/graphql" + "github.com/urfave/cli/v2" + + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" +) + +func setCurrentCommit(cliCtx *cli.Context) error { + var mutation struct { + SetCurrentCommmit struct { + TrackedCommit *struct { + Hash string `graphql:"hash"` + Message string `graphql:"message"` + } `grapqhl:"trackedCommit"` + } `graphql:"stackSetCurrentCommit(id: $stack, sha: $sha)"` + } + + variables := map[string]interface{}{ + "sha": graphql.String(cliCtx.String(flagRequiredCommitSHA.Name)), + "stack": graphql.ID(stackID), + } + + ctx := context.Background() + + if err := authenticated.Client.Mutate(ctx, &mutation, variables); err != nil { + return err + } + + commit := mutation.SetCurrentCommmit.TrackedCommit + if commit == nil { + return errors.New("no tracked commit set on the Stack") + } + + _, err := fmt.Fprintf(os.Stdout, "Current commit set to %q: (SHA %s)", commit.Message, commit.Hash) + + return err +} diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index 97abe77..83c00fb 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -56,6 +56,13 @@ func Command() *cli.Command { }, Action: runTrigger("PROPOSED", "preview"), }, + { + Category: "Stack management", + Name: "set-current-commit", + Usage: "Set current commit on the stack", + Flags: []cli.Flag{flagRequiredCommitSHA}, + Action: setCurrentCommit, + }, { Category: "Run management", Name: "task",