Skip to content

Commit

Permalink
move event allowed method to types
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 18, 2023
1 parent 725379e commit 3e00d49
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 34 deletions.
2 changes: 1 addition & 1 deletion api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func CreateBuild(c *gin.Context) {
}

// verify repo has event configured
if !util.EventAllowed(input.GetEvent(), input.GetEventAction(), r) {
if !r.EventAllowed(input.GetEvent(), input.GetEventAction()) {
retErr := fmt.Errorf("unable to create new build: %s does not have %s%s events enabled", r.GetFullName(), input.GetEvent(), ":"+input.GetEventAction())

util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func PostWebhook(c *gin.Context) {
}

// verify the build has a valid event and the repo allows that event type
if !util.EventAllowed(b.GetEvent(), b.GetEventAction(), repo) {
if !repo.EventAllowed(b.GetEvent(), b.GetEventAction()) {
retErr := fmt.Errorf("%s: %s does not have %s%s events enabled", baseErr, repo.GetFullName(), b.GetEvent(), ":"+b.GetEventAction())
util.HandleError(c, http.StatusBadRequest, retErr)

Expand Down
32 changes: 0 additions & 32 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"html"
"strings"

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

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -131,34 +130,3 @@ func CheckAllowlist(r *library.Repo, allowlist []string) bool {

return false
}

// ParseEventMask is a helper function to calculate the bit mask for
// a given event.
func EventAllowed(event, action string, r *library.Repo) (allowed bool) {
allowed = false

if len(action) > 0 {
event = event + ":" + action
}

switch event {
case constants.EventPush:
allowed = r.GetAllowEvents().GetPush().GetBranch()
case constants.EventPull + ":" + constants.ActionOpened:
allowed = r.GetAllowEvents().GetPullRequest().GetOpened()
case constants.EventPull + ":" + constants.ActionSynchronize:
allowed = r.GetAllowEvents().GetPullRequest().GetSynchronize()
case constants.EventPull + ":" + constants.ActionEdited:
allowed = r.GetAllowEvents().GetPullRequest().GetEdited()
case constants.EventTag:
allowed = r.GetAllowEvents().GetPush().GetTag()
case constants.EventComment + ":" + constants.ActionCreated:
allowed = r.GetAllowEvents().GetComment().GetCreated()
case constants.EventComment + ":" + constants.ActionEdited:
allowed = r.GetAllowEvents().GetComment().GetEdited()
case constants.EventDeploy:
allowed = r.GetAllowEvents().GetDeployment().GetCreated()
}

return
}

0 comments on commit 3e00d49

Please sign in to comment.