-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support triggering build by event (#65)
* feat: support triggering build by event * chore: update docs for new param * chore: address linter feedback
- Loading branch information
Showing
11 changed files
with
352 additions
and
202 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
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,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 | ||
} |
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,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") | ||
} | ||
} |
Oops, something went wrong.