From a620f83d409c287c0d63278fed8e08fd5ad9d20e Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Thu, 23 Sep 2021 13:05:35 -0500 Subject: [PATCH] feat: support triggering build by event (#65) * feat: support triggering build by event * chore: update docs for new param * chore: address linter feedback --- DOCS.md | 77 ++++++++++++++++++----- Makefile | 2 + cmd/vela-downstream/build.go | 92 ++++++++++++++++++++++++++++ cmd/vela-downstream/build_test.go | 98 ++++++++++++++++++++++++++++++ cmd/vela-downstream/main.go | 48 +++++++++------ cmd/vela-downstream/plugin.go | 35 ++++++----- cmd/vela-downstream/plugin_test.go | 55 +++++++++++------ cmd/vela-downstream/repo.go | 11 +--- cmd/vela-downstream/repo_test.go | 41 +++---------- cmd/vela-downstream/status.go | 44 -------------- cmd/vela-downstream/status_test.go | 51 ---------------- 11 files changed, 352 insertions(+), 202 deletions(-) create mode 100644 cmd/vela-downstream/build.go create mode 100644 cmd/vela-downstream/build_test.go delete mode 100644 cmd/vela-downstream/status.go delete mode 100644 cmd/vela-downstream/status_test.go diff --git a/DOCS.md b/DOCS.md index c1fe6f9..78d371a 100644 --- a/DOCS.md +++ b/DOCS.md @@ -22,12 +22,59 @@ steps: image: target/vela-downstream:latest pull: always parameters: - branch: master repos: - octocat/hello-world server: https://vela-server.localhost ``` +Sample of triggering a downstream build for a specific branch: + +```diff +steps: + - name: trigger_hello-world + image: target/vela-downstream:latest + pull: always + parameters: ++ branch: main + repos: + - octocat/hello-world + server: https://vela-server.localhost +``` + +Sample of triggering a downstream build for a specific event: + +```diff +steps: + - name: trigger_hello-world + image: target/vela-downstream:latest + pull: always + parameters: ++ event: tag + repos: + - octocat/hello-world + server: https://vela-server.localhost +``` + +Sample of triggering a downstream build for a specific status: + +> **NOTE:** +> +> You can provide a list of statuses to the plugin. +> +> The first build found matching either of the statuses will be triggered. + +```diff +steps: + - name: trigger_hello-world + image: target/vela-downstream:latest + pull: always + parameters: + repos: + - octocat/hello-world + server: https://vela-server.localhost ++ status: [ success, failure ] +``` + Sample of triggering a downstream build for multiple repos: ```diff @@ -36,7 +83,6 @@ steps: image: target/vela-downstream:latest pull: always parameters: - branch: master repos: - octocat/hello-world + - go-vela/hello-world @@ -45,7 +91,11 @@ steps: Sample of triggering a downstream build for multiple repos with different branches: -> **NOTE:** Use the @ symbol at the end of the org/repo to provide a unique branch per repo. +> **NOTE:** +> +> Use the @ symbol at the end of the org/repo to provide a unique branch per repo. +> +> This will override the value set for the `branch` parameter. ```diff steps: @@ -53,7 +103,6 @@ steps: image: target/vela-downstream:latest pull: always parameters: -- branch: master repos: - - octocat/hello-world + - octocat/hello-world@test @@ -77,7 +126,6 @@ steps: pull: always + secrets: [ downstream_token ] parameters: - branch: master repos: - octocat/hello-world server: https://vela-server.localhost @@ -104,7 +152,6 @@ steps: image: target/vela-downstream:latest pull: always parameters: - branch: master repos: - octocat/hello-world server: https://vela-server.localhost @@ -123,14 +170,15 @@ steps: The following parameters are used to configure the image: -| Name | Description | Required | Default | Environment Variables | -| ----------- | ------------------------------------------------------- | -------- | --------- | ----------------------------------------------- | -| `branch` | default branch to trigger a build on | `true` | `master` | `PARAMETER_BRANCH`
`DOWNSTREAM_BRANCH` | -| `log_level` | set the log level for the plugin | `true` | `info` | `PARAMETER_LOG_LEVEL`
`DOWNSTREAM_LOG_LEVEL` | -| `repos` | list of / names to trigger a build on | `true` | `N/A` | `PARAMETER_REPOS`
`DOWNSTREAM_REPOS` | -| `server` | Vela server to communicate with | `true` | `N/A` | `PARAMETER_SERVER`
`DOWNSTREAM_SERVER` | -| `status` | list of acceptable build statuses to trigger a build on | `true` | `success` | `PARAMETER_STATUS`
`DOWNSTREAM_STATUS` | -| `token` | token for communication with Vela | `true` | `N/A` | `PARAMETER_TOKEN`
`DOWNSTREAM_TOKEN` | +| Name | Description | Required | Default | Environment Variables | +| ----------- | ------------------------------------------------ | -------- | ------------- | ----------------------------------------------- | +| `branch` | branch to trigger a build on | `true` | `master` | `PARAMETER_BRANCH`
`DOWNSTREAM_BRANCH` | +| `event` | event to trigger a build on | `true` | `push` | `PARAMETER_EVENT`
`DOWNSTREAM_EVENT` | +| `log_level` | set the log level for the plugin | `true` | `info` | `PARAMETER_LOG_LEVEL`
`DOWNSTREAM_LOG_LEVEL` | +| `repos` | list of / names to trigger a build on | `true` | `N/A` | `PARAMETER_REPOS`
`DOWNSTREAM_REPOS` | +| `server` | Vela server to communicate with | `true` | `N/A` | `PARAMETER_SERVER`
`DOWNSTREAM_SERVER` | +| `status` | list of statuses to trigger a build on | `true` | `[ success ]` | `PARAMETER_STATUS`
`DOWNSTREAM_STATUS` | +| `token` | token for communication with Vela | `true` | `N/A` | `PARAMETER_TOKEN`
`DOWNSTREAM_TOKEN` | ## Template @@ -146,7 +194,6 @@ steps: image: target/vela-downstream:latest pull: always parameters: - branch: master + log_level: trace repos: - octocat/hello-world diff --git a/Makefile b/Makefile index 5b11843..72ff059 100644 --- a/Makefile +++ b/Makefile @@ -243,5 +243,7 @@ docker-run: -e DOWNSTREAM_TOKEN \ -e PARAMETER_LOG_LEVEL \ -e PARAMETER_BRANCH \ + -e PARAMETER_EVENT \ -e PARAMETER_REPOS \ + -e PARAMETER_STATUS \ vela-downstream:local diff --git a/cmd/vela-downstream/build.go b/cmd/vela-downstream/build.go new file mode 100644 index 0000000..4d467fb --- /dev/null +++ b/cmd/vela-downstream/build.go @@ -0,0 +1,92 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package main + +import ( + "fmt" + "strings" + + "github.com/go-vela/types/constants" + "github.com/sirupsen/logrus" +) + +// Build represents the plugin configuration for Build information. +type Build struct { + // branch to trigger a build for the repo + Branch string + // event to trigger a build for the repo + Event string + // status to trigger a build for the repo + Status []string +} + +// Validate verifies the Build is properly configured. +func (b *Build) Validate() error { + logrus.Trace("validating build configuration") + + // verify build branch is provided + if len(b.Branch) == 0 { + return fmt.Errorf("no build branch provided") + } + + // verify build event is provided + if len(b.Event) == 0 { + return fmt.Errorf("no build event provided") + } + + // create a list of valid events for a build + validEvents := []string{ + constants.EventComment, + constants.EventDeploy, + constants.EventPull, + constants.EventPush, + constants.EventTag, + } + + // verify the build event provided is valid + if !contains(validEvents, b.Event) { + return fmt.Errorf("invalid build event provided: %s", b.Event) + } + + // verify build status is provided + if len(b.Status) == 0 { + return fmt.Errorf("no build status provided") + } + + // create a list of valid statuses for a build + validStatuses := []string{ + constants.StatusCanceled, + constants.StatusError, + constants.StatusFailure, + constants.StatusKilled, + constants.StatusPending, + constants.StatusRunning, + constants.StatusSuccess, + } + + // iterate through the build statuses provided + for _, status := range b.Status { + // verify the build status provided is valid + if !contains(validStatuses, status) { + return fmt.Errorf("invalid build status provided: %s", status) + } + } + + return nil +} + +// contains checks if the provided input string is found in the given list of +// strings. If the input string is not found, then the function returns false. +func contains(list []string, input string) bool { + // iterate through the list of strings + for _, item := range list { + // check if the item matches the input + if strings.EqualFold(item, input) { + return true + } + } + + return false +} diff --git a/cmd/vela-downstream/build_test.go b/cmd/vela-downstream/build_test.go new file mode 100644 index 0000000..62b740b --- /dev/null +++ b/cmd/vela-downstream/build_test.go @@ -0,0 +1,98 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package main + +import ( + "testing" + + "github.com/go-vela/types/constants" +) + +func TestDownstream_Build_Validate(t *testing.T) { + // setup types + b := &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + } + + // run test + err := b.Validate() + if err != nil { + t.Errorf("Validate returned err: %v", err) + } +} + +func TestDownstream_Build_Validate_NoBranch(t *testing.T) { + // setup types + b := &Build{ + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + } + + // run test + err := b.Validate() + if err == nil { + t.Errorf("Validate should have returned err") + } +} + +func TestDownstream_Build_Validate_NoEvent(t *testing.T) { + // setup types + b := &Build{ + Branch: "master", + Status: []string{constants.StatusSuccess}, + } + + // run test + err := b.Validate() + if err == nil { + t.Errorf("Validate should have returned err") + } +} + +func TestDownstream_Build_Validate_NoStatus(t *testing.T) { + // setup types + b := &Build{ + Branch: "master", + Event: constants.EventPush, + } + + // run test + err := b.Validate() + if err == nil { + t.Errorf("Validate should have returned err") + } +} + +func TestDownstream_Build_Validate_InvalidEvent(t *testing.T) { + // setup types + b := &Build{ + Branch: "master", + Event: "foo", + Status: []string{constants.StatusSuccess}, + } + + // run test + err := b.Validate() + if err == nil { + t.Errorf("Validate should have returned err") + } +} + +func TestDownstream_Build_Validate_InvalidStatus(t *testing.T) { + // setup types + b := &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{"foo"}, + } + + // run test + err := b.Validate() + if err == nil { + t.Errorf("Validate should have returned err") + } +} diff --git a/cmd/vela-downstream/main.go b/cmd/vela-downstream/main.go index 7e8da6a..cf5188b 100644 --- a/cmd/vela-downstream/main.go +++ b/cmd/vela-downstream/main.go @@ -67,6 +67,30 @@ func main() { Value: "info", }, + // Build Flags + + &cli.StringFlag{ + EnvVars: []string{"PARAMETER_BRANCH", "DOWNSTREAM_BRANCH"}, + FilePath: "/vela/parameters/downstream/branch,/vela/secrets/downstream/branch", + Name: "build.branch", + Usage: "branch to trigger a build for the repo", + Value: "master", + }, + &cli.StringFlag{ + EnvVars: []string{"PARAMETER_EVENT", "DOWNSTREAM_EVENT"}, + FilePath: "/vela/parameters/downstream/event,/vela/secrets/downstream/event", + Name: "build.event", + Usage: "event to trigger a build for the repo", + Value: constants.EventPush, + }, + &cli.StringSliceFlag{ + EnvVars: []string{"PARAMETER_STATUS", "DOWNSTREAM_STATUS"}, + FilePath: "/vela/parameters/downstream/status,/vela/secrets/downstream/status", + Name: "build.status", + Usage: "list of statuses to trigger a build for the repo", + Value: cli.NewStringSlice(constants.StatusSuccess), + }, + // Config Flags &cli.StringFlag{ @@ -84,26 +108,12 @@ func main() { // Repo Flags - &cli.StringFlag{ - EnvVars: []string{"PARAMETER_BRANCH", "DOWNSTREAM_BRANCH"}, - FilePath: "/vela/parameters/downstream/branch,/vela/secrets/downstream/branch", - Name: "repo.branch", - Usage: "default branch to trigger builds for the repo", - Value: "master", - }, &cli.StringSliceFlag{ EnvVars: []string{"PARAMETER_REPOS", "DOWNSTREAM_REPOS"}, FilePath: "/vela/parameters/downstream/repos,/vela/secrets/downstream/repos", Name: "repo.names", Usage: "list of / names to trigger", }, - &cli.StringSliceFlag{ - EnvVars: []string{"PARAMETER_STATUS", "DOWNSTREAM_STATUS"}, - FilePath: "/vela/parameters/downstream/status,/vela/secrets/downstream/status", - Name: "status", - Usage: "status of last build to trigger - example: (error|failure|running|success)", - Value: cli.NewStringSlice(constants.StatusSuccess), - }, } err = app.Run(os.Args) @@ -142,6 +152,12 @@ func run(c *cli.Context) error { // create the plugin p := &Plugin{ + // build configuration + Build: &Build{ + Branch: c.String("build.branch"), + Event: c.String("build.event"), + Status: c.StringSlice("build.status"), + }, // config configuration Config: &Config{ Server: c.String("config.server"), @@ -151,10 +167,8 @@ func run(c *cli.Context) error { }, // repo configuration Repo: &Repo{ - Branch: c.String("repo.branch"), - Names: c.StringSlice("repo.names"), + Names: c.StringSlice("repo.names"), }, - Status: &Status{"status"}, } // validate the plugin diff --git a/cmd/vela-downstream/plugin.go b/cmd/vela-downstream/plugin.go index d9c1b02..082930d 100644 --- a/cmd/vela-downstream/plugin.go +++ b/cmd/vela-downstream/plugin.go @@ -15,12 +15,12 @@ import ( // Plugin represents the configuration loaded for the plugin. type Plugin struct { + // build arguments loaded for the plugin + Build *Build // config arguments loaded for the plugin Config *Config // repo arguments loaded for the plugin Repo *Repo - // list of acceptable Vela build statuses to trigger a build for - Status *Status } // Exec formats and runs the commands for triggering builds in Vela. @@ -34,7 +34,7 @@ func (p *Plugin) Exec() error { } // parse list of repos to trigger builds on - repos, err := p.Repo.Parse() + repos, err := p.Repo.Parse(p.Build.Branch) if err != nil { return err } @@ -44,7 +44,7 @@ func (p *Plugin) Exec() error { // create new build type to store last successful build build := library.Build{} - logrus.Infof("Listing last 500 builds for %s", repo.GetFullName()) + logrus.Infof("listing last 500 builds for %s", repo.GetFullName()) // create options for listing builds opts := &vela.ListOptions{ @@ -80,15 +80,18 @@ func (p *Plugin) Exec() error { opts.Page = resp.NextPage } - logrus.Debugf("Searching for latest build on branch %s with status %s", + logrus.Debugf("searching for latest %s build on branch %s with status %s", + p.Build.Event, repo.GetBranch(), - p.Status, + p.Build.Status, ) // iterate through list of builds for the repo for _, b := range builds { - // check if the build branch matches and is of an acceptable status - if b.GetBranch() == repo.GetBranch() && contains(p.Status, b.GetStatus()) { + // check if the build branch, event and status match + if b.GetBranch() == repo.GetBranch() && + b.GetEvent() == p.Build.Event && + contains(p.Build.Status, b.GetStatus()) { // update the build object to the current build build = b @@ -99,9 +102,10 @@ func (p *Plugin) Exec() error { // check if we found a build to restart if build.GetNumber() == 0 { - return fmt.Errorf("no build with status %s on branch %s found for %s", - p.Status, + return fmt.Errorf("no %s build on branch %s with status %s found for %s", + p.Build.Event, repo.GetBranch(), + p.Build.Status, repo.GetFullName(), ) } @@ -124,19 +128,20 @@ func (p *Plugin) Exec() error { func (p *Plugin) Validate() error { logrus.Debug("validating plugin configuration") - // validate config configuration - err := p.Config.Validate() + // validate build configuration + err := p.Build.Validate() if err != nil { return err } - // validate repo configuration - err = p.Repo.Validate() + // validate config configuration + err = p.Config.Validate() if err != nil { return err } - err = p.Status.Validate() + // validate repo configuration + err = p.Repo.Validate() if err != nil { return err } diff --git a/cmd/vela-downstream/plugin_test.go b/cmd/vela-downstream/plugin_test.go index 5b8e396..c0dad27 100644 --- a/cmd/vela-downstream/plugin_test.go +++ b/cmd/vela-downstream/plugin_test.go @@ -13,15 +13,18 @@ import ( func TestDownstream_Plugin_Exec_Error(t *testing.T) { // setup types p := &Plugin{ + Build: &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + }, Config: &Config{ Server: "http://vela.localhost.com", Token: "superSecretVelaToken", }, Repo: &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@master"}, + Names: []string{"go-vela/hello-world@master"}, }, - Status: &Status{constants.StatusSuccess}, } err := p.Exec() @@ -33,15 +36,18 @@ func TestDownstream_Plugin_Exec_Error(t *testing.T) { func TestDownstream_Plugin_Validate(t *testing.T) { // setup types p := &Plugin{ + Build: &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + }, Config: &Config{ Server: "http://vela.localhost.com", Token: "superSecretVelaToken", }, Repo: &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@master"}, + Names: []string{"go-vela/hello-world@master"}, }, - Status: &Status{constants.StatusSuccess}, } err := p.Validate() @@ -50,15 +56,17 @@ func TestDownstream_Plugin_Validate(t *testing.T) { } } -func TestDownstream_Plugin_Validate_NoConfig(t *testing.T) { +func TestDownstream_Plugin_Validate_NoBuild(t *testing.T) { // setup types p := &Plugin{ - Config: &Config{}, + Build: &Build{}, + Config: &Config{ + Server: "http://vela.localhost.com", + Token: "superSecretVelaToken", + }, Repo: &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@master"}, + Names: []string{"go-vela/hello-world@master"}, }, - Status: &Status{constants.StatusSuccess}, } err := p.Validate() @@ -67,15 +75,18 @@ func TestDownstream_Plugin_Validate_NoConfig(t *testing.T) { } } -func TestDownstream_Plugin_Validate_NoRepo(t *testing.T) { +func TestDownstream_Plugin_Validate_NoConfig(t *testing.T) { // setup types p := &Plugin{ - Config: &Config{ - Server: "http://vela.localhost.com", - Token: "superSecretVelaToken", + Build: &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + }, + Config: &Config{}, + Repo: &Repo{ + Names: []string{"go-vela/hello-world@master"}, }, - Repo: &Repo{}, - Status: &Status{constants.StatusSuccess}, } err := p.Validate() @@ -84,15 +95,19 @@ func TestDownstream_Plugin_Validate_NoRepo(t *testing.T) { } } -func TestDownstream_Plugin_Validate_NoStatus(t *testing.T) { +func TestDownstream_Plugin_Validate_NoRepo(t *testing.T) { // setup types p := &Plugin{ + Build: &Build{ + Branch: "master", + Event: constants.EventPush, + Status: []string{constants.StatusSuccess}, + }, Config: &Config{ Server: "http://vela.localhost.com", Token: "superSecretVelaToken", }, - Repo: &Repo{}, - Status: &Status{constants.StatusSuccess}, + Repo: &Repo{}, } err := p.Validate() diff --git a/cmd/vela-downstream/repo.go b/cmd/vela-downstream/repo.go index 9683bde..a1961b9 100644 --- a/cmd/vela-downstream/repo.go +++ b/cmd/vela-downstream/repo.go @@ -15,14 +15,12 @@ import ( // Repo represents the plugin configuration for Repo information. type Repo struct { - // default branch to trigger a build for the repo - Branch string // list of Vela repos to trigger a build for Names []string } // Parse verifies the Repo is properly configured. -func (r *Repo) Parse() ([]*library.Repo, error) { +func (r *Repo) Parse(branch string) ([]*library.Repo, error) { logrus.Trace("parsing repos from provided configuration") // create new repos type to store parsed repos @@ -60,7 +58,7 @@ func (r *Repo) Parse() ([]*library.Repo, error) { // check if a branch was parsed from the input if len(repo.GetBranch()) == 0 { // set the default branch from the provided input - repo.SetBranch(r.Branch) + repo.SetBranch(branch) } // set the full name for the repo @@ -79,11 +77,6 @@ func (r *Repo) Parse() ([]*library.Repo, error) { func (r *Repo) Validate() error { logrus.Trace("validating repo configuration") - // verify repo branch is provided - if len(r.Branch) == 0 { - return fmt.Errorf("no repo branch provided") - } - // verify repo names are provided if len(r.Names) == 0 { return fmt.Errorf("no repo names provided") diff --git a/cmd/vela-downstream/repo_test.go b/cmd/vela-downstream/repo_test.go index c0fc74c..6299a2a 100644 --- a/cmd/vela-downstream/repo_test.go +++ b/cmd/vela-downstream/repo_test.go @@ -14,8 +14,7 @@ import ( func TestDownstream_Repo_Parse(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@test", "go-vela/hello-world"}, + Names: []string{"go-vela/hello-world@test", "go-vela/hello-world"}, } r1 := new(library.Repo) @@ -33,7 +32,7 @@ func TestDownstream_Repo_Parse(t *testing.T) { want := []*library.Repo{r1, r2} // run test - got, err := r.Parse() + got, err := r.Parse("master") if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -46,12 +45,11 @@ func TestDownstream_Repo_Parse(t *testing.T) { func TestDownstream_Repo_Parse_MultipleSlashes(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world/master"}, + Names: []string{"go-vela/hello-world/master"}, } // run test - got, err := r.Parse() + got, err := r.Parse("master") if err == nil { t.Errorf("Parse should have returned err") } @@ -64,12 +62,11 @@ func TestDownstream_Repo_Parse_MultipleSlashes(t *testing.T) { func TestDownstream_Repo_Parse_MultipleAtSigns(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@master@"}, + Names: []string{"go-vela/hello-world@master@"}, } // run test - got, err := r.Parse() + got, err := r.Parse("master") if err == nil { t.Errorf("Parse should have returned err") } @@ -82,8 +79,7 @@ func TestDownstream_Repo_Parse_MultipleAtSigns(t *testing.T) { func TestDownstream_Repo_Validate(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world@master"}, + Names: []string{"go-vela/hello-world@master"}, } err := r.Validate() @@ -92,24 +88,9 @@ func TestDownstream_Repo_Validate(t *testing.T) { } } -func TestDownstream_Repo_Validate_NoBranch(t *testing.T) { - // setup types - r := &Repo{ - Names: []string{"go-vela/hello-world@master"}, - } - - // run test - err := r.Validate() - if err == nil { - t.Errorf("Validate should have returned err") - } -} - func TestDownstream_Repo_Validate_NoNames(t *testing.T) { // setup types - r := &Repo{ - Branch: "master", - } + r := &Repo{} // run test err := r.Validate() @@ -121,8 +102,7 @@ func TestDownstream_Repo_Validate_NoNames(t *testing.T) { func TestDownstream_Repo_Validate_NoSlash(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela_hello-world"}, + Names: []string{"go-vela_hello-world"}, } // run test @@ -135,8 +115,7 @@ func TestDownstream_Repo_Validate_NoSlash(t *testing.T) { func TestDownstream_Repo_Validate_MultipleSlashes(t *testing.T) { // setup types r := &Repo{ - Branch: "master", - Names: []string{"go-vela/hello-world/master"}, + Names: []string{"go-vela/hello-world/master"}, } // run test diff --git a/cmd/vela-downstream/status.go b/cmd/vela-downstream/status.go deleted file mode 100644 index 846eb78..0000000 --- a/cmd/vela-downstream/status.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "fmt" - "strings" - - "github.com/go-vela/types/constants" - "github.com/sirupsen/logrus" -) - -type Status []string - -// Validate verifies the Config is properly configured. -func (s *Status) Validate() error { - logrus.Trace("validating config configuration") - - acceptableStatus := Status{ - constants.StatusError, - constants.StatusFailure, - constants.StatusKilled, - constants.StatusCanceled, - constants.StatusPending, - constants.StatusRunning, - constants.StatusSuccess, - } - - for _, v := range *s { - if !contains(&acceptableStatus, v) { - return fmt.Errorf("status %s not of acceptable type %s", s, acceptableStatus) - } - } - - return nil -} - -// contains checks to see if a []string contains a string. -func contains(acceptableStatus *Status, s string) bool { - for _, v := range *acceptableStatus { - if strings.EqualFold(v, s) { - return true - } - } - return false -} diff --git a/cmd/vela-downstream/status_test.go b/cmd/vela-downstream/status_test.go deleted file mode 100644 index 536ea1a..0000000 --- a/cmd/vela-downstream/status_test.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package main - -import ( - "testing" - - "github.com/go-vela/types/constants" -) - -func TestDownstream_Status_Validate(t *testing.T) { - // setup types - s := &Status{constants.StatusSuccess} - - err := s.Validate() - if err != nil { - t.Errorf("Validate returned err: %v", err) - } -} - -func TestDownstream_Status_Validate_Multiple(t *testing.T) { - // setup types - s := &Status{constants.StatusSuccess, constants.StatusCanceled} - - err := s.Validate() - if err != nil { - t.Errorf("Validate returned err: %v", err) - } -} - -func TestDownstream_Status_Validate_MultipleOneInvalid(t *testing.T) { - // setup types - s := &Status{constants.StatusSuccess, "foo"} - - err := s.Validate() - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestDownstream_Status_Validate_InvalidStatus(t *testing.T) { - // setup types - s := &Status{"foo"} - - err := s.Validate() - if err == nil { - t.Errorf("Validate should have returned err") - } -}