Skip to content

Commit

Permalink
feat(repo): support new allow_events struct (#509)
Browse files Browse the repository at this point in the history
* feat(repo): support new allow_events struct

* init legacy allow events

* make clean and fix a test
  • Loading branch information
ecrupper authored Dec 12, 2023
1 parent cf6aee3 commit 3c55490
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 91 deletions.
91 changes: 64 additions & 27 deletions action/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -40,36 +41,12 @@ func (c *Config) Add(client *vela.Client) error {
PipelineType: vela.String(c.PipelineType),
}

// iterate through all events provided
for _, event := range c.Events {
// check if the repository should allow push events
if event == constants.EventPush {
r.AllowPush = vela.Bool(true)
}

// check if the repository should allow pull_request events
if event == constants.EventPull || event == AlternatePull {
r.AllowPull = vela.Bool(true)
}

// check if the repository should allow tag events
if event == constants.EventTag {
r.AllowTag = vela.Bool(true)
}

// check if the repository should allow deployment events
if event == constants.EventDeploy || event == AlternateDeploy {
r.AllowDeploy = vela.Bool(true)
}
logrus.Tracef("adding repo %s/%s", c.Org, c.Name)

// check if the repository should allow comment events
if event == constants.EventComment {
r.AllowComment = vela.Bool(true)
}
if len(c.Events) > 0 {
populateEvents(r, c.Events)
}

logrus.Tracef("adding repo %s/%s", c.Org, c.Name)

// send API call to add a repository
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#RepoService.Add
Expand Down Expand Up @@ -107,3 +84,63 @@ func (c *Config) Add(client *vela.Client) error {
return output.Stdout(repo)
}
}

// populateEvents is a helper function designed to fill both the legacy `Allow<_>` fields
// as well as the `AllowEvents` struct with the correct values based on a slice input.
func populateEvents(r *library.Repo, events []string) {
result := new(library.Events)
push := new(actions.Push)
pull := new(actions.Pull)
comment := new(actions.Comment)
deploy := new(actions.Deploy)

// -- legacy allow events init --
r.SetAllowPush(false)
r.SetAllowPull(false)
r.SetAllowTag(false)
r.SetAllowDeploy(false)
r.SetAllowComment(false)

// iterate through all events provided
for _, event := range events {
switch event {
case constants.EventPush, constants.EventPush + ":branch":
r.SetAllowPush(true)
push.SetBranch(true)
case constants.EventTag, constants.EventPush + ":" + constants.EventTag:
r.SetAllowTag(true)
push.SetTag(true)
case constants.EventPull, AlternatePull:
r.SetAllowPull(true)
pull.SetOpened(true)
pull.SetReopened(true)
pull.SetSynchronize(true)
case constants.EventDeploy, AlternateDeploy, constants.EventDeploy + ":" + constants.ActionCreated:
r.SetAllowDeploy(true)
deploy.SetCreated(true)
case constants.EventComment:
r.SetAllowComment(true)
comment.SetCreated(true)
comment.SetEdited(true)
case constants.EventPull + ":" + constants.ActionOpened:
pull.SetOpened(true)
case constants.EventPull + ":" + constants.ActionEdited:
pull.SetEdited(true)
case constants.EventPull + ":" + constants.ActionSynchronize:
pull.SetSynchronize(true)
case constants.EventPull + ":" + constants.ActionReopened:
pull.SetReopened(true)
case constants.EventComment + ":" + constants.ActionCreated:
comment.SetCreated(true)
case constants.EventComment + ":" + constants.ActionEdited:
comment.SetEdited(true)
}
}

result.SetPush(push)
result.SetPullRequest(pull)
result.SetDeployment(deploy)
result.SetComment(comment)

r.SetAllowEvents(result)
}
83 changes: 83 additions & 0 deletions action/repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"testing"

"github.com/go-vela/server/mock/server"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
"github.com/google/go-cmp/cmp"

"github.com/go-vela/sdk-go/vela"
)
Expand Down Expand Up @@ -149,3 +152,83 @@ func TestRepo_Config_Add(t *testing.T) {
}
}
}

func TestRepo_populateEvents(t *testing.T) {
// setup types
tBool := true
fBool := false

// setup tests
tests := []struct {
name string
events []string
want *library.Repo
}{
{
name: "happy path legacy events",
events: []string{"push", "pull_request", "tag", "deploy", "comment"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &tBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &tBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
},
PullRequest: &actions.Pull{
Opened: &tBool,
Reopened: &tBool,
Synchronize: &tBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
},
Comment: &actions.Comment{
Created: &tBool,
Edited: &tBool,
},
},
},
},
{
name: "action specific",
events: []string{"push:branch", "push:tag", "pull_request:opened", "pull_request:edited", "deployment:created", "comment:created"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &fBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &fBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
},
PullRequest: &actions.Pull{
Opened: &tBool,
Edited: &tBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
},
Comment: &actions.Comment{
Created: &tBool,
},
},
},
},
}

// run tests
for _, test := range tests {
repo := new(library.Repo)
populateEvents(repo, test.events)

if diff := cmp.Diff(test.want, repo); diff != "" {
t.Errorf("populateEvents failed for %s mismatch (-want +got):\n%s", test.name, diff)
}
}
}
1 change: 0 additions & 1 deletion action/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type Config struct {
Trusted bool
Active bool
Events []string
DropEvents []string
PipelineType string
Page int
PerPage int
Expand Down
52 changes: 2 additions & 50 deletions action/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/go-vela/sdk-go/vela"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -40,55 +39,8 @@ func (c *Config) Update(client *vela.Client) error {
PipelineType: vela.String(c.PipelineType),
}

// iterate through all events provided
for _, event := range c.Events {
// check if the repository should allow push events
if event == constants.EventPush {
r.AllowPush = vela.Bool(true)
}

// check if the repository should allow pull_request events
if event == constants.EventPull || event == AlternatePull {
r.AllowPull = vela.Bool(true)
}

// check if the repository should allow tag events
if event == constants.EventTag {
r.AllowTag = vela.Bool(true)
}

// check if the repository should allow deployment events
if event == constants.EventDeploy || event == AlternateDeploy {
r.AllowDeploy = vela.Bool(true)
}

// check if the repository should allow comment events
if event == constants.EventComment {
r.AllowComment = vela.Bool(true)
}
}

// iterate through all drop events provided
for _, event := range c.DropEvents {
if event == constants.EventPush {
r.AllowPush = vela.Bool(false)
}

if event == constants.EventPull || event == AlternatePull {
r.AllowPull = vela.Bool(false)
}

if event == constants.EventTag {
r.AllowTag = vela.Bool(false)
}

if event == constants.EventDeploy || event == AlternateDeploy {
r.AllowDeploy = vela.Bool(false)
}

if event == constants.EventComment {
r.AllowComment = vela.Bool(false)
}
if len(c.Events) > 0 {
populateEvents(r, c.Events)
}

logrus.Tracef("updating repo %s/%s", c.Org, c.Name)
Expand Down
2 changes: 0 additions & 2 deletions action/repo/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func TestRepo_Config_Update(t *testing.T) {
Trusted: false,
Active: true,
Events: []string{"push", "pull_request", "comment", "deployment", "tag"},
DropEvents: []string{"push", "pull_request", "comment", "deployment", "tag"},
PipelineType: "yaml",
Output: "",
},
Expand All @@ -62,7 +61,6 @@ func TestRepo_Config_Update(t *testing.T) {
Private: false,
Trusted: false,
Active: true,
DropEvents: []string{"deployment", "tag"},
PipelineType: "yaml",
Output: "dump",
},
Expand Down
11 changes: 1 addition & 10 deletions command/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ var CommandUpdate = &cli.Command{
Aliases: []string{"events", "add-event", "add-events", "e"},
Usage: "webhook event(s) repository responds to",
},
&cli.StringSliceFlag{
EnvVars: []string{"VELA_DROP_EVENTS", "REPO_DROP_EVENTS"},
Name: "drop-event",
Aliases: []string{"drop-events", "de"},
Usage: "remove webhook event(s) repository responds to",
},
&cli.StringFlag{
EnvVars: []string{"VELA_PIPELINE_TYPE", "PIPELINE_TYPE"},
Name: "pipeline-type",
Expand All @@ -136,9 +130,7 @@ EXAMPLES:
1. Update a repository with push and pull request enabled.
$ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request
2. Update a repository with all event types enabled.
$ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request --event tag --event deployment --event comment
3. Update a repository to disable the comment and deploy events.
$ {{.HelpName}} --org MyOrg --repo MyRepo --event push --drop-events deployment,comment
$ {{.HelpName}} --org MyOrg --repo MyRepo --event push,pull_request,tag,deployment,comment
3. Update a repository with a longer build timeout.
$ {{.HelpName}} --org MyOrg --repo MyRepo --timeout 90
4. Update a repository when config or environment variables are set.
Expand Down Expand Up @@ -187,7 +179,6 @@ func update(c *cli.Context) error {
Trusted: c.Bool("trusted"),
Active: c.Bool("active"),
Events: c.StringSlice("event"),
DropEvents: c.StringSlice("drop-event"),
PipelineType: c.String("pipeline-type"),
Output: c.String(internal.FlagOutput),
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/go-vela/types v0.22.1-0.20231211143329-1eae2f5e371b
github.com/go-vela/worker v0.22.0
github.com/golang-jwt/jwt/v5 v5.1.0
github.com/google/go-cmp v0.6.0
github.com/gosuri/uitable v0.0.4
github.com/joho/godotenv v1.5.1
github.com/manifoldco/promptui v0.9.0
Expand Down Expand Up @@ -71,7 +72,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-github/v56 v56.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down

0 comments on commit 3c55490

Please sign in to comment.