Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repo)!: support allow_events struct #1023

Merged
merged 8 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
wass3rw3rk marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -177,7 +177,7 @@

defer func() {
// send API call to update the webhook
_, err = database.FromContext(c).UpdateHook(ctx, h)

Check failure on line 180 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L180

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:180:32: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		_, err = database.FromContext(c).UpdateHook(ctx, h)
		                             ^
if err != nil {
logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand Down Expand Up @@ -256,15 +256,16 @@
}

// 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()) {
wass3rw3rk marked this conversation as resolved.
Show resolved Hide resolved
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 Expand Up @@ -786,7 +787,7 @@
case "archived", "unarchived", constants.ActionEdited:
logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName())
// send call to get repository from database
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())

Check failure on line 790 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L790

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:790:38: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
		                                   ^
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -797,7 +798,7 @@
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)

Check failure on line 801 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L801

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:801:40: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)
		                                     ^
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

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 @@ -23,7 +23,7 @@
"k8s.io/apimachinery/pkg/util/wait"
)

func server(c *cli.Context) error {

Check failure on line 26 in cmd/vela-server/server.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/vela-server/server.go#L26

Function 'server' is too long (200 > 160) (funlen)
Raw output
cmd/vela-server/server.go:26: Function 'server' is too long (200 > 160) (funlen)
func server(c *cli.Context) error {
// set log formatter
switch c.String("log-formatter") {
case "json":
Expand Down Expand Up @@ -119,6 +119,7 @@
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
Loading