Skip to content

Commit

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

* use ToDatabase for determining empty event set and add some comments

* remove test file

* upgrade types

* add middleware test for default events mask

* add reopened to testRepo in db pkg

* nil check over reflect against empty struct for update repo
  • Loading branch information
ecrupper committed Dec 11, 2023
1 parent 5fc317f commit 6fcde86
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 56 deletions.
57 changes: 57 additions & 0 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-vela/server/util"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -73,6 +74,8 @@ func CreateRepo(c *gin.Context) {
defaultTimeout := c.Value("defaultTimeout").(int64)
maxBuildLimit := c.Value("maxBuildLimit").(int64)
defaultRepoEvents := c.Value("defaultRepoEvents").([]string)
defaultRepoEventsMask := c.Value("defaultRepoEventsMask").(int64)

ctx := c.Request.Context()

// capture body from API request
Expand Down Expand Up @@ -159,6 +162,14 @@ func CreateRepo(c *gin.Context) {
}
}

// set allow events based on input if given
if input.GetAllowEvents().ToDatabase() != 0 {
r.SetAllowEvents(input.GetAllowEvents())
} else {
r.SetAllowEvents(defaultAllowedEvents(defaultRepoEvents, defaultRepoEventsMask))
}

// -- DEPRECATED SECTION --
// set default events if no events are passed in
if !input.GetAllowPull() && !input.GetAllowPush() &&
!input.GetAllowDeploy() && !input.GetAllowTag() &&
Expand All @@ -184,6 +195,7 @@ func CreateRepo(c *gin.Context) {
r.SetAllowPush(input.GetAllowPush())
r.SetAllowTag(input.GetAllowTag())
}
// -- END DEPRECATED SECTION --

if len(input.GetPipelineType()) == 0 {
r.SetPipelineType(constants.PipelineTypeYAML)
Expand Down Expand Up @@ -326,3 +338,48 @@ func CreateRepo(c *gin.Context) {

c.JSON(http.StatusCreated, r)
}

// defaultAllowedEvents is a helper function that generates an Events struct that results
// from an admin-provided `sliceDefaults` or an admin-provided `maskDefaults`. If the admin
// supplies a mask, that will be the default. Otherwise, it will be the legacy event list.
func defaultAllowedEvents(sliceDefaults []string, maskDefaults int64) *library.Events {
if maskDefaults > 0 {
return library.NewEventsFromMask(maskDefaults)
}

events := new(library.Events)

for _, event := range sliceDefaults {
switch event {
case constants.EventPull:
pull := new(actions.Pull)
pull.SetOpened(true)
pull.SetSynchronize(true)

events.SetPullRequest(pull)
case constants.EventPush:
push := events.GetPush()
push.SetBranch(true)

events.SetPush(push)
case constants.EventTag:
tag := events.GetPush()
tag.SetTag(true)

events.SetPush(tag)
case constants.EventDeploy:
deploy := new(actions.Deploy)
deploy.SetCreated(true)

events.SetDeployment(deploy)
case constants.EventComment:
comment := new(actions.Comment)
comment.SetCreated(true)
comment.SetEdited(true)

events.SetComment(comment)
}
}

return events
}
16 changes: 16 additions & 0 deletions api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func UpdateRepo(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
maxBuildLimit := c.Value("maxBuildLimit").(int64)
defaultRepoEvents := c.Value("defaultRepoEvents").([]string)
defaultRepoEventsMask := c.Value("defaultRepoEventsMask").(int64)
ctx := c.Request.Context()

// update engine logger with API metadata
Expand Down Expand Up @@ -170,6 +172,14 @@ func UpdateRepo(c *gin.Context) {
r.SetActive(input.GetActive())
}

// set allow events based on input if given
if input.AllowEvents != nil {
r.SetAllowEvents(input.GetAllowEvents())

eventsChanged = true
}

// -- DEPRECATED SECTION --
if input.AllowPull != nil {
// update allow_pull if set
r.SetAllowPull(input.GetAllowPull())
Expand Down Expand Up @@ -212,6 +222,12 @@ func UpdateRepo(c *gin.Context) {
r.SetAllowPull(true)
r.SetAllowPush(true)
}
// -- END DEPRECATED SECTION --

// set default events if no events are enabled
if r.GetAllowEvents().ToDatabase() == 0 {
r.SetAllowEvents(defaultAllowedEvents(defaultRepoEvents, defaultRepoEventsMask))
}

if len(input.GetPipelineType()) != 0 {
// ensure the pipeline type matches one of the expected values
Expand Down
15 changes: 8 additions & 7 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,16 @@ func PostWebhook(c *gin.Context) {
}

// verify the build has a valid event and the repo allows that event type
if (b.GetEvent() == constants.EventPush && !repo.GetAllowPush()) ||
(b.GetEvent() == constants.EventPull && !repo.GetAllowPull()) ||
(b.GetEvent() == constants.EventComment && !repo.GetAllowComment()) ||
(b.GetEvent() == constants.EventTag && !repo.GetAllowTag()) ||
(b.GetEvent() == constants.EventDeploy && !repo.GetAllowDeploy()) {
retErr := fmt.Errorf("%s: %s does not have %s events enabled", baseErr, repo.GetFullName(), b.GetEvent())
if !repo.EventAllowed(b.GetEvent(), b.GetEventAction()) {
var actionErr string
if len(b.GetEventAction()) > 0 {
actionErr = ":" + b.GetEventAction()
}

retErr := fmt.Errorf("%s: %s does not have %s%s events enabled", baseErr, repo.GetFullName(), b.GetEvent(), actionErr)
util.HandleError(c, http.StatusBadRequest, retErr)

h.SetStatus(constants.StatusFailure)
h.SetStatus(constants.StatusSkipped)
h.SetError(retErr.Error())

return
Expand Down
5 changes: 5 additions & 0 deletions cmd/vela-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func main() {
Usage: "override default events for newly activated repositories",
Value: cli.NewStringSlice(constants.EventPush),
},
&cli.Int64Flag{
EnvVars: []string{"VELA_DEFAULT_REPO_EVENTS_MASK"},
Name: "default-repo-events-mask",
Usage: "set default event mask for newly activated repositories",
},
// Token Manager Flags
&cli.DurationFlag{
EnvVars: []string{"VELA_USER_ACCESS_TOKEN_DURATION", "USER_ACCESS_TOKEN_DURATION"},
Expand Down
1 change: 1 addition & 0 deletions cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func server(c *cli.Context) error {
middleware.SecureCookie(c.Bool("vela-enable-secure-cookie")),
middleware.Worker(c.Duration("worker-active-interval")),
middleware.DefaultRepoEvents(c.StringSlice("default-repo-events")),
middleware.DefaultRepoEventsMask(c.Int64("default-repo-events-mask")),
middleware.AllowlistSchedule(c.StringSlice("vela-schedule-allowlist")),
middleware.ScheduleFrequency(c.Duration("schedule-minimum-frequency")),
)
Expand Down
Loading

0 comments on commit 6fcde86

Please sign in to comment.