-
Notifications
You must be signed in to change notification settings - Fork 29
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)!: add pending approval timeout #1227
Open
ecrupper
wants to merge
3
commits into
main
Choose a base branch
from
enhance/pending-approval-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package build | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/server/api/types" | ||
"github.com/go-vela/server/constants" | ||
"github.com/go-vela/server/database" | ||
"github.com/go-vela/server/queue" | ||
"github.com/go-vela/server/queue/models" | ||
"github.com/go-vela/server/scm" | ||
) | ||
|
||
// TrafficBuild is a helper function that will determine whether to publish a build to the queue or place it | ||
// in pending approval status. | ||
func TrafficBuild(c *gin.Context, l *logrus.Entry, b *types.Build, r *types.Repo, item *models.Item) error { | ||
// if the webhook was from a Pull event from a forked repository, verify it is allowed to run | ||
if b.GetFork() { | ||
l.Tracef("inside %s workflow for fork PR build %s/%d", r.GetApproveBuild(), r.GetFullName(), b.GetNumber()) | ||
|
||
switch r.GetApproveBuild() { | ||
case constants.ApproveForkAlways: | ||
err := gatekeepBuild(c, b, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
case constants.ApproveForkNoWrite: | ||
// determine if build sender has write access to parent repo. If not, this call will result in an error | ||
level, err := scm.FromContext(c).RepoAccess(c.Request.Context(), b.GetSender(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) | ||
if err != nil || (level != "admin" && level != "write") { | ||
err = gatekeepBuild(c, b, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
l.Debugf("fork PR build %s/%d automatically running without approval. sender %s has %s access", r.GetFullName(), b.GetNumber(), b.GetSender(), level) | ||
case constants.ApproveOnce: | ||
// determine if build sender is in the contributors list for the repo | ||
// | ||
// NOTE: this call is cumbersome for repos with lots of contributors. Potential TODO: improve this if | ||
// GitHub adds a single-contributor API endpoint. | ||
contributor, err := scm.FromContext(c).RepoContributor(c.Request.Context(), r.GetOwner(), b.GetSender(), r.GetOrg(), r.GetName()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !contributor { | ||
err = gatekeepBuild(c, b, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
fallthrough | ||
case constants.ApproveNever: | ||
fallthrough | ||
default: | ||
l.Debugf("fork PR build %s/%d automatically running without approval", r.GetFullName(), b.GetNumber()) | ||
} | ||
} | ||
|
||
// send API call to set the status on the commit | ||
err := scm.FromContext(c).Status(c.Request.Context(), r.GetOwner(), b, r.GetOrg(), r.GetName()) | ||
if err != nil { | ||
l.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) | ||
} | ||
|
||
// publish the build to the queue | ||
go Enqueue( | ||
context.WithoutCancel(c.Request.Context()), | ||
queue.FromGinContext(c), | ||
database.FromContext(c), | ||
item, | ||
b.GetHost(), | ||
) | ||
|
||
return nil | ||
} | ||
|
||
// gatekeepBuild is a helper function that will set the status of a build to 'pending approval' and | ||
// send a status update to the SCM. | ||
func gatekeepBuild(c *gin.Context, b *types.Build, r *types.Repo) error { | ||
l := c.MustGet("logger").(*logrus.Entry) | ||
|
||
l = l.WithFields(logrus.Fields{ | ||
"org": r.GetOrg(), | ||
"repo": r.GetName(), | ||
"repo_id": r.GetID(), | ||
"build": b.GetNumber(), | ||
"build_id": b.GetID(), | ||
}) | ||
|
||
l.Debug("fork PR build waiting for approval") | ||
|
||
b.SetStatus(constants.StatusPendingApproval) | ||
|
||
_, err := database.FromContext(c).UpdateBuild(c, b) | ||
if err != nil { | ||
return fmt.Errorf("unable to update build for %s/%d: %w", r.GetFullName(), b.GetNumber(), err) | ||
} | ||
|
||
l.Info("build updated") | ||
|
||
// update the build components to pending approval status | ||
err = UpdateComponentStatuses(c, b, constants.StatusPendingApproval) | ||
if err != nil { | ||
return fmt.Errorf("unable to update build components for %s/%d: %w", r.GetFullName(), b.GetNumber(), err) | ||
} | ||
|
||
// send API call to set the status on the commit | ||
err = scm.FromContext(c).Status(c, r.GetOwner(), b, r.GetOrg(), r.GetName()) | ||
if err != nil { | ||
l.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts on separating the logic from the action taken?
do the determining,
return bool error
then gatekeep or enqueue afterwards?seems cleaner that way, like how we separate logic and API response writer code in other spots
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning bool based on some logic might also make the function more testable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea. There's a few spots within TrafficBuild and gatekeepBuild that leverage contextual services, which may make testing a little tricky. But I'll see what I can do.