Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Oct 12, 2023
1 parent b1c4198 commit 4dc8711
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions api/build/auto_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@ import (
"github.com/go-vela/server/internal/token"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)

// AutoCancel is a helper function that checks to see if any pending or running
// builds for the repo can be replaced by the current build.
func AutoCancel(c *gin.Context, b *library.Build, rBs []*library.Build, r *library.Repo, pending, running bool) error {
func AutoCancel(c *gin.Context, b *library.Build, rBs []*library.Build, r *library.Repo, cancelOpts *pipeline.CancelOptions) error {
// iterate through pending and running builds
for _, rB := range rBs {
// if build is the current build, continue
if rB.GetID() == b.GetID() {
continue
}

// ensure criteria is met before auto canceling
// ensure criteria is met before auto canceling (push to same branch, or pull with same action from same head_ref)
if (strings.EqualFold(rB.GetEvent(), constants.EventPush) &&
strings.EqualFold(b.GetEvent(), constants.EventPush) &&
strings.EqualFold(b.GetBranch(), rB.GetBranch())) ||
(strings.EqualFold(rB.GetEvent(), constants.EventPull) &&
strings.EqualFold(b.GetEventAction(), rB.GetEventAction()) &&
strings.EqualFold(b.GetHeadRef(), rB.GetHeadRef())) {
switch {
case strings.EqualFold(rB.GetStatus(), constants.StatusPending) && pending:
case strings.EqualFold(rB.GetStatus(), constants.StatusPending) && cancelOpts.Pending:
// pending build will be handled gracefully by worker once pulled off queue
rB.SetStatus(constants.StatusCanceled)

_, err := database.FromContext(c).UpdateBuild(c, rB)
if err != nil {
return err
}
case strings.EqualFold(rB.GetStatus(), constants.StatusRunning) && running:
case strings.EqualFold(rB.GetStatus(), constants.StatusRunning) && cancelOpts.Running:
// call cancelRunning routine for builds already running on worker
err := cancelRunning(c, rB, r)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func PostWebhook(c *gin.Context) {
}

// call auto cancel routine
err = build.AutoCancel(c, b, rBs, repo, p.Metadata.AutoCancel.Pending, p.Metadata.AutoCancel.Running)
err = build.AutoCancel(c, b, rBs, repo, p.Metadata.AutoCancel)
if err != nil {
logrus.Errorf("unable to cancel running build: %v", err)
}
Expand Down

0 comments on commit 4dc8711

Please sign in to comment.