Skip to content

Commit

Permalink
Command to set current stack commit (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinwyszynski authored May 10, 2021
1 parent b06a203 commit 4df338d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions internal/cmd/stack/set_current_commit.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4df338d

Please sign in to comment.