Skip to content

Commit

Permalink
feat: support triggering build by event (#65)
Browse files Browse the repository at this point in the history
* feat: support triggering build by event

* chore: update docs for new param

* chore: address linter feedback
  • Loading branch information
jbrockopp authored Sep 23, 2021
1 parent 2959afd commit a620f83
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 202 deletions.
77 changes: 62 additions & 15 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +83,6 @@ steps:
image: target/vela-downstream:latest
pull: always
parameters:
branch: master
repos:
- octocat/hello-world
+ - go-vela/hello-world
Expand All @@ -45,15 +91,18 @@ 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:
- name: trigger_multiple
image: target/vela-downstream:latest
pull: always
parameters:
- branch: master
repos:
- - octocat/hello-world
+ - octocat/hello-world@test
Expand All @@ -77,7 +126,6 @@ steps:
pull: always
+ secrets: [ downstream_token ]
parameters:
branch: master
repos:
- octocat/hello-world
server: https://vela-server.localhost
Expand All @@ -104,7 +152,6 @@ steps:
image: target/vela-downstream:latest
pull: always
parameters:
branch: master
repos:
- octocat/hello-world
server: https://vela-server.localhost
Expand All @@ -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`<br>`DOWNSTREAM_BRANCH` |
| `log_level` | set the log level for the plugin | `true` | `info` | `PARAMETER_LOG_LEVEL`<br>`DOWNSTREAM_LOG_LEVEL` |
| `repos` | list of <org>/<repo> names to trigger a build on | `true` | `N/A` | `PARAMETER_REPOS`<br>`DOWNSTREAM_REPOS` |
| `server` | Vela server to communicate with | `true` | `N/A` | `PARAMETER_SERVER`<br>`DOWNSTREAM_SERVER` |
| `status` | list of acceptable build statuses to trigger a build on | `true` | `success` | `PARAMETER_STATUS`<br>`DOWNSTREAM_STATUS` |
| `token` | token for communication with Vela | `true` | `N/A` | `PARAMETER_TOKEN`<br>`DOWNSTREAM_TOKEN` |
| Name | Description | Required | Default | Environment Variables |
| ----------- | ------------------------------------------------ | -------- | ------------- | ----------------------------------------------- |
| `branch` | branch to trigger a build on | `true` | `master` | `PARAMETER_BRANCH`<br>`DOWNSTREAM_BRANCH` |
| `event` | event to trigger a build on | `true` | `push` | `PARAMETER_EVENT`<br>`DOWNSTREAM_EVENT` |
| `log_level` | set the log level for the plugin | `true` | `info` | `PARAMETER_LOG_LEVEL`<br>`DOWNSTREAM_LOG_LEVEL` |
| `repos` | list of <org>/<repo> names to trigger a build on | `true` | `N/A` | `PARAMETER_REPOS`<br>`DOWNSTREAM_REPOS` |
| `server` | Vela server to communicate with | `true` | `N/A` | `PARAMETER_SERVER`<br>`DOWNSTREAM_SERVER` |
| `status` | list of statuses to trigger a build on | `true` | `[ success ]` | `PARAMETER_STATUS`<br>`DOWNSTREAM_STATUS` |
| `token` | token for communication with Vela | `true` | `N/A` | `PARAMETER_TOKEN`<br>`DOWNSTREAM_TOKEN` |

## Template

Expand All @@ -146,7 +194,6 @@ steps:
image: target/vela-downstream:latest
pull: always
parameters:
branch: master
+ log_level: trace
repos:
- octocat/hello-world
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
92 changes: 92 additions & 0 deletions cmd/vela-downstream/build.go
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
}
98 changes: 98 additions & 0 deletions cmd/vela-downstream/build_test.go
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")
}
}
Loading

0 comments on commit a620f83

Please sign in to comment.