-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): add cancel functionality (#186)
* 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
1 parent
b3d7844
commit e276b64
Showing
9 changed files
with
393 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
Oops, something went wrong.