-
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 approve command (#508)
* feat(build): add approve command * remove alias for approve
- Loading branch information
Showing
9 changed files
with
311 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package build | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-vela/cli/internal/output" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Approve approves a build based off the provided configuration. | ||
func (c *Config) Approve(client *vela.Client) error { | ||
logrus.Debug("executing approve for build configuration") | ||
|
||
logrus.Tracef("approving build %s/%s/%d", c.Org, c.Repo, c.Number) | ||
|
||
// send API call to approve a build | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Approve | ||
_, err := client.Build.Approve(c.Org, c.Repo, c.Number) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// output the build in stdout format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout | ||
return output.Stdout(fmt.Sprintf("successfully approved build %s/%s/%d", c.Org, c.Repo, c.Number)) | ||
} |
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,65 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package build | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/server/mock/server" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
) | ||
|
||
func TestBuild_Config_Approve(t *testing.T) { | ||
// setup test server | ||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
// create a vela client | ||
client, err := vela.NewClient(s.URL, "vela", 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: "approve", | ||
Org: "github", | ||
Repo: "octocat", | ||
Number: 1, | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "approve", | ||
Org: "github", | ||
Repo: "octocat", | ||
Number: 0, | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Approve(client) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Approve should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Approve 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,24 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/go-vela/cli/command/build" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// approveCmds defines the commands for approving resources. | ||
var approveCmds = &cli.Command{ | ||
Name: "approve", | ||
Category: "Resource Management", | ||
Description: "Use this command to approve a resource for Vela.", | ||
Usage: "Approve a resource for Vela via subcommands", | ||
UseShortOptionHandling: true, | ||
Subcommands: []*cli.Command{ | ||
// add the sub command for approving a build | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/command/build?tab=doc#CommandApprove | ||
build.CommandApprove, | ||
}, | ||
} |
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,107 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package build | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-vela/cli/action" | ||
"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" | ||
) | ||
|
||
// CommandApprove defines the command for Approveing a build. | ||
var CommandApprove = &cli.Command{ | ||
Name: internal.FlagBuild, | ||
Description: "Use this command to Approve a build.", | ||
Usage: "Approve the provided build", | ||
Action: approve, | ||
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", "number", "bn"}, | ||
Usage: "provide the number for the build", | ||
}, | ||
}, | ||
CustomHelpTemplate: fmt.Sprintf(`%s | ||
EXAMPLES: | ||
1. Approve existing build for a repository. | ||
$ {{.HelpName}} --org MyOrg --repo MyRepo --build 1 | ||
2. Approve existing build for a repository when config or environment variables are set. | ||
$ {{.HelpName}} --build 1 | ||
DOCUMENTATION: | ||
https://go-vela.github.io/docs/reference/cli/build/Approve/ | ||
`, cli.CommandHelpTemplate), | ||
} | ||
|
||
// helper function to capture the provided | ||
// input and create the object used to | ||
// approve a build. | ||
func approve(c *cli.Context) error { | ||
// load variables from the config file | ||
err := action.Load(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// grab first command line argument, if it exists, and set it as resource | ||
err = internal.ProcessArgs(c, internal.FlagBuild, "int") | ||
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: internal.ActionApprove, | ||
Org: c.String(internal.FlagOrg), | ||
Repo: c.String(internal.FlagRepo), | ||
Number: c.Int(internal.FlagBuild), | ||
} | ||
|
||
// 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 Approve call for the build configuration | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config.Approve | ||
return b.Approve(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,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package build | ||
|
||
import ( | ||
"flag" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/cli/test" | ||
"github.com/go-vela/server/mock/server" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestBuild_Approve(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") | ||
|
||
// 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 := approve(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("approve should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("approve 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
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