Skip to content

Commit

Permalink
fix(repo): add missing approve-build flag to add and update (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Feb 12, 2024
1 parent ddb971f commit 074c98d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 13 deletions.
1 change: 1 addition & 0 deletions action/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *Config) Add(client *vela.Client) error {
Trusted: vela.Bool(c.Trusted),
Active: vela.Bool(c.Active),
PipelineType: vela.String(c.PipelineType),
ApproveBuild: vela.String(c.ApproveBuild),
}

logrus.Tracef("adding repo %s/%s", c.Org, c.Name)
Expand Down
3 changes: 3 additions & 0 deletions action/repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestRepo_Config_Add(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "fork-always",
Output: "",
},
},
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestRepo_Config_Add(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "fork-no-write",
Output: "json",
},
},
Expand Down Expand Up @@ -130,6 +132,7 @@ func TestRepo_Config_Add(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "never",
Output: "yaml",
},
},
Expand Down
1 change: 1 addition & 0 deletions action/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
Active bool
Events []string
PipelineType string
ApproveBuild string
Page int
PerPage int
Output string
Expand Down
1 change: 1 addition & 0 deletions action/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Config) Update(client *vela.Client) error {
Trusted: vela.Bool(c.Trusted),
Active: vela.Bool(c.Active),
PipelineType: vela.String(c.PipelineType),
ApproveBuild: vela.String(c.ApproveBuild),
}

if len(c.Events) > 0 {
Expand Down
3 changes: 3 additions & 0 deletions action/repo/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestRepo_Config_Update(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "fork-always",
Output: "",
},
},
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestRepo_Config_Update(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "fork-no-write",
Output: "json",
},
},
Expand Down Expand Up @@ -122,6 +124,7 @@ func TestRepo_Config_Update(t *testing.T) {
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
ApproveBuild: "never",
Output: "yaml",
},
},
Expand Down
16 changes: 16 additions & 0 deletions action/repo/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package repo
import (
"fmt"

"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)

Expand All @@ -28,5 +29,20 @@ func (c *Config) Validate() error {
}
}

// check if approve build setting is valid if supplied
if c.Action == "add" || c.Action == "update" {
if len(c.ApproveBuild) > 0 &&
c.ApproveBuild != constants.ApproveForkAlways &&
c.ApproveBuild != constants.ApproveForkNoWrite &&
c.ApproveBuild != constants.ApproveNever {
return fmt.Errorf(
"invalid input for approve-build: must be `%s`, `%s`, or `%s`",
constants.ApproveForkAlways,
constants.ApproveForkNoWrite,
constants.ApproveNever,
)
}
}

return nil
}
47 changes: 34 additions & 13 deletions action/repo/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func TestRepo_Config_Validate(t *testing.T) {
Output: "",
},
},
{
failure: true,
config: &Config{
Action: "add",
Org: "github",
Name: "octocat",
ApproveBuild: "invalid",
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
},
},
{
failure: false,
config: &Config{
Expand Down Expand Up @@ -70,19 +80,30 @@ func TestRepo_Config_Validate(t *testing.T) {
{
failure: false,
config: &Config{
Action: "update",
Org: "github",
Name: "octocat",
Link: "https://github.com/github/octocat",
Clone: "https://github.com/github/octocat.git",
Branch: "main",
Timeout: 60,
Visibility: "public",
Private: false,
Trusted: false,
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
Output: "",
Action: "update",
Org: "github",
Name: "octocat",
Link: "https://github.com/github/octocat",
Clone: "https://github.com/github/octocat.git",
Branch: "main",
Timeout: 60,
Visibility: "public",
Private: false,
Trusted: false,
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
ApproveBuild: "fork-no-write",
Output: "",
},
},
{
failure: true,
config: &Config{
Action: "update",
Org: "github",
Name: "octocat",
ApproveBuild: "invalid",
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
},
},
{
Expand Down
10 changes: 10 additions & 0 deletions command/repo/add.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//nolint:dupl // ignore duplicate of update
package repo

import (
Expand Down Expand Up @@ -115,6 +116,12 @@ var CommandAdd = &cli.Command{
Usage: "type of base pipeline for the compiler to render",
Value: constants.PipelineTypeYAML,
},
&cli.StringFlag{
EnvVars: []string{"VELA_APPROVE_BUILD", "REPO_APPROVE_BUILD"},
Name: "approve-build",
Aliases: []string{"ab", "approve-build-setting"},
Usage: "when to require admin approval to run builds from outside contributors (`fork-always`, `fork-no-write`, or `never`)",
},

// Output Flags

Expand All @@ -139,6 +146,8 @@ EXAMPLES:
$ {{.HelpName}} --org MyOrg --repo MyRepo --counter 90
6. Add a repository with a starlark pipeline file.
$ {{.HelpName}} --org MyOrg --repo MyRepo --pipeline-type starlark
7. Add a repository with approve build setting set to fork-no-write.
$ {{.HelpName}} --org MyOrg --repo MyRepo --approve-build fork-no-write
DOCUMENTATION:
Expand Down Expand Up @@ -182,6 +191,7 @@ func add(c *cli.Context) error {
Active: c.Bool("active"),
Events: c.StringSlice("event"),
PipelineType: c.String("pipeline-type"),
ApproveBuild: c.String("approve-build"),
Output: c.String(internal.FlagOutput),
}

Expand Down
10 changes: 10 additions & 0 deletions command/repo/update.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//nolint:dupl // ignore duplicate of add
package repo

import (
Expand Down Expand Up @@ -115,6 +116,12 @@ var CommandUpdate = &cli.Command{
Usage: "type of base pipeline for the compiler to render",
Value: constants.PipelineTypeYAML,
},
&cli.StringFlag{
EnvVars: []string{"VELA_APPROVE_BUILD", "REPO_APPROVE_BUILD"},
Name: "approve-build",
Aliases: []string{"ab", "approve-build-setting"},
Usage: "when to require admin approval to run builds from outside contributors (`fork-always`, `fork-no-write`, or `never`)",
},

// Output Flags

Expand All @@ -137,6 +144,8 @@ EXAMPLES:
$ {{.HelpName}}
5. Update a repository with a new build number.
$ {{.HelpName}} --org MyOrg --repo MyRepo --counter 200
6. Update a repository with approve build setting set to fork-always.
$ {{.HelpName}} --org MyOrg --repo MyRepo --approve-build fork-always
DOCUMENTATION:
Expand Down Expand Up @@ -180,6 +189,7 @@ func update(c *cli.Context) error {
Active: c.Bool("active"),
Events: c.StringSlice("event"),
PipelineType: c.String("pipeline-type"),
ApproveBuild: c.String("approve-build"),
Output: c.String(internal.FlagOutput),
}

Expand Down

0 comments on commit 074c98d

Please sign in to comment.