Skip to content

Commit

Permalink
feat(build): add cancel functionality (#186)
Browse files Browse the repository at this point in the history
* feat(build): prep cancel functionality

* feat(build): update with cancel api endpoint

* feat(build): revert back to cancel api endpoint

* temporary dependency

* temporary dependency

* minor changes for testing troubleshooting

* fix: add authenticating changes from master to cancel build

* fix: remove debug statements

* fix: nolint files that are similar, but not duplicative

* chore: update to use sdk-go latest

* fix: make clean

* remove response body

Co-authored-by: EmmanuelMeinen <Emmanuel.Meinen@target.com>
Co-authored-by: David May <1301201+wass3r@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 13, 2021
1 parent b3d7844 commit e276b64
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 6 deletions.
3 changes: 3 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const (
// addAction defines the action for creating a resource.
addAction = "add"

// cancelAction defines the action for canceling of a resource.
cancelAction = "cancel"

// chownAction defines the action for changing ownership of a resource.
chownAction = "chown"

Expand Down
57 changes: 57 additions & 0 deletions action/build/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package build

import (
"github.com/go-vela/cli/internal/output"

"github.com/go-vela/sdk-go/vela"

"github.com/sirupsen/logrus"
)

// Cancel cancels a build based off the provided configuration.
func (c *Config) Cancel(client *vela.Client) error {
logrus.Debug("executing cancel for build configuration")

logrus.Tracef("canceling build %s/%s/%d", c.Org, c.Repo, c.Number)

// send API call to cancel a build
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Cancel
build, _, err := client.Build.Cancel(c.Org, c.Repo, c.Number)
if err != nil {
return err
}

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the build in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(build)
case output.DriverJSON:
// output the build in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(build)
case output.DriverSpew:
// output the build in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(build)
case output.DriverYAML:
// output the build in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(&build)
default:
// output the build in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(build)
}
}
111 changes: 111 additions & 0 deletions action/build/cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package build contains resources for managing builds
// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative
package build

import (
"net/http/httptest"
"testing"

"github.com/go-vela/mock/server"

"github.com/go-vela/sdk-go/vela"
)

func TestBuild_Config_Cancel(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

// create a vela client
client, err := vela.NewClient(s.URL, "", nil)
if err != nil {
t.Errorf("unable to create client: %v", err)
}

// setup tests
tests := []struct {
failure bool
config *Config
}{
{
failure: false,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 1,
Output: "",
},
},
{
failure: false,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 1,
Output: "dump",
},
},
{
failure: false,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 1,
Output: "json",
},
},
{
failure: false,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 1,
Output: "spew",
},
},
{
failure: false,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 1,
Output: "yaml",
},
},
{
failure: true,
config: &Config{
Action: "cancel",
Org: "github",
Repo: "octocat",
Number: 0,
Output: "",
},
},
}

// run tests
for _, test := range tests {
err := test.config.Cancel(client)

if test.failure {
if err == nil {
t.Errorf("Cancel should have returned err")
}

continue
}

if err != nil {
t.Errorf("Cancel returned err: %v", err)
}
}
}
114 changes: 114 additions & 0 deletions action/build_cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package action contains all cli actions
// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative
package action

import (
"fmt"

"github.com/go-vela/cli/action/build"
"github.com/go-vela/cli/internal"
"github.com/go-vela/cli/internal/client"

"github.com/urfave/cli/v2"
)

// BuildCancel defines the command for canceling a build.
var BuildCancel = &cli.Command{
Name: internal.FlagBuild,
Description: "Use this command to cancel a build.",
Usage: "Cancel the provided build",
Action: buildCancel,
Flags: []cli.Flag{

// Repo Flags

&cli.StringFlag{
EnvVars: []string{"VELA_ORG", "BUILD_ORG"},
Name: internal.FlagOrg,
Aliases: []string{"o"},
Usage: "provide the organization for the build",
},
&cli.StringFlag{
EnvVars: []string{"VELA_REPO", "BUILD_REPO"},
Name: internal.FlagRepo,
Aliases: []string{"r"},
Usage: "provide the repository for the build",
},

// Build Flags

&cli.IntFlag{
EnvVars: []string{"VELA_BUILD", "BUILD_NUMBER"},
Name: internal.FlagBuild,
Aliases: []string{"b"},
Usage: "provide the number for the build",
},

// Output Flags

&cli.StringFlag{
EnvVars: []string{"VELA_OUTPUT", "BUILD_OUTPUT"},
Name: internal.FlagOutput,
Aliases: []string{"op"},
Usage: "format the output in json, spew or yaml",
},
},
CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
1. Cancel existing build for a repository.
$ {{.HelpName}} --org MyOrg --repo MyRepo --build 1
2. Cancel existing build for a repository when config or environment variables are set.
$ {{.HelpName}} --build 1
DOCUMENTATION:
https://go-vela.github.io/docs/cli/build/cancel/
`, cli.CommandHelpTemplate),
}

// helper function to capture the provided
// input and create the object used to
// cancel a build.
func buildCancel(c *cli.Context) error {
// load variables from the config file
err := load(c)
if err != nil {
return err
}

// parse the Vela client from the context
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse
client, err := client.Parse(c)
if err != nil {
return err
}

// create the build configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config
b := &build.Config{
Action: cancelAction,
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Number: c.Int(internal.FlagBuild),
Output: c.String(internal.FlagOutput),
}

// validate build configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config.Validate
err = b.Validate()
if err != nil {
return err
}

// execute the cancel call for the build configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config.Cancel
return b.Cancel(client)
}
74 changes: 74 additions & 0 deletions action/build_cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package action contains all cli actions
// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative
package action

import (
"flag"
"net/http/httptest"
"testing"

"github.com/go-vela/cli/test"
"github.com/go-vela/mock/server"

"github.com/urfave/cli/v2"
)

func TestAction_BuildCancel(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

// setup flags
authSet := flag.NewFlagSet("test", 0)
authSet.String("api.addr", s.URL, "doc")
authSet.String("api.token.access", test.TestTokenGood, "doc")
authSet.String("api.token.refresh", "superSecretRefreshToken", "doc")

fullSet := flag.NewFlagSet("test", 0)
fullSet.String("api.addr", s.URL, "doc")
fullSet.String("api.token.access", test.TestTokenGood, "doc")
fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc")
fullSet.String("org", "github", "doc")
fullSet.String("repo", "octocat", "doc")
fullSet.Int("build", 1, "doc")
fullSet.String("output", "json", "doc")

// setup tests
tests := []struct {
failure bool
set *flag.FlagSet
}{
{
failure: false,
set: fullSet,
},
{
failure: true,
set: authSet,
},
{
failure: true,
set: flag.NewFlagSet("test", 0),
},
}

// run tests
for _, test := range tests {
err := buildCancel(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil))

if test.failure {
if err == nil {
t.Errorf("buildCancel should have returned err")
}

continue
}

if err != nil {
t.Errorf("buildCancel returned err: %v", err)
}
}
}
27 changes: 27 additions & 0 deletions cmd/vela-cli/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"github.com/go-vela/cli/action"

"github.com/urfave/cli/v2"
)

// cancelCmds defines the commands for canceling resources.
var cancelCmds = &cli.Command{
Name: "cancel",
Category: "Resource Management",
Aliases: []string{"cx"},
Description: "Use this command to cancel a resource for Vela.",
Usage: "Cancel a resource for Vela via subcommands",
UseShortOptionHandling: true,
Subcommands: []*cli.Command{
// add the sub command for canceling a build
//
// https://pkg.go.dev/github.com/go-vela/cli/action?tab=doc#BuildCancel
action.BuildCancel,
},
}
Loading

0 comments on commit e276b64

Please sign in to comment.