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

chore(webhook): improve on some comments during the webhook flow #944

Merged
merged 4 commits into from
Aug 30, 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
1 change: 1 addition & 0 deletions api/build/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func GetBuildExecutable(c *gin.Context) {
"subject": cl.Subject,
}).Infof("reading build executable %s/%d", r.GetFullName(), b.GetNumber())

// send database call to pop the requested build executable from the table
bExecutable, err := database.FromContext(c).PopBuildExecutable(ctx, b.GetID())
if err != nil {
retErr := fmt.Errorf("unable to pop build executable: %w", err)
Expand Down
10 changes: 8 additions & 2 deletions api/build/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
"github.com/sirupsen/logrus"
)

// PublishToQueue is a helper function that creates
// a build item and publishes it to the queue.
// PublishToQueue is a helper function that pushes the build executable to the database
// and publishes a queue item (build, repo, user) to the queue.
func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) {
// marshal pipeline build into byte data to add to the build executable object
byteExecutable, err := json.Marshal(p)
if err != nil {
logrus.Errorf("Failed to marshal build executable %d for %s: %v", b.GetNumber(), r.GetFullName(), err)
Expand All @@ -30,10 +31,12 @@ func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interf
return
}

// create build executable to push to database
bExecutable := new(library.BuildExecutable)
bExecutable.SetBuildID(b.GetID())
bExecutable.SetData(byteExecutable)

// send database call to create a build executable
err = db.CreateBuildExecutable(ctx, bExecutable)
if err != nil {
logrus.Errorf("Failed to publish build executable to database %d for %s: %v", b.GetNumber(), r.GetFullName(), err)
Expand All @@ -44,6 +47,7 @@ func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interf
return
}

// convert build, repo, and user into queue item
item := types.ToItem(b, r, u)

logrus.Infof("Converting queue item to json for build %d for %s", b.GetNumber(), r.GetFullName())
Expand All @@ -60,6 +64,7 @@ func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interf

logrus.Infof("Establishing route for build %d for %s", b.GetNumber(), r.GetFullName())

// determine the route on which to publish the queue item
route, err := queue.Route(&p.Worker)
if err != nil {
logrus.Errorf("unable to set route for build %d for %s: %v", b.GetNumber(), r.GetFullName(), err)
Expand All @@ -72,6 +77,7 @@ func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interf

logrus.Infof("Publishing item for build %d for %s to queue %s", b.GetNumber(), r.GetFullName(), route)

// push item on to the queue
err = queue.Push(context.Background(), route, byteItem)
if err != nil {
logrus.Errorf("Retrying; Failed to publish build %d for %s: %v", b.GetNumber(), r.GetFullName(), err)
Expand Down
19 changes: 12 additions & 7 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func PostWebhook(c *gin.Context) {
// -------------------- End of TODO: --------------------

// process the webhook from the source control provider
// comment, number, h, r, b
//
// populate build, hook, repo resources as well as PR Number / PR Comment if necessary
webhook, err := scm.FromContext(c).ProcessWebhook(c.Request)
if err != nil {
retErr := fmt.Errorf("unable to parse webhook: %w", err)
Expand Down Expand Up @@ -142,7 +143,7 @@ func PostWebhook(c *gin.Context) {
return
}

// if there were actual changes to the repo, return the repo object
// if there were actual changes to the repo (database call populated ID field), return the repo object
if r.GetID() != 0 {
c.JSON(http.StatusOK, r)
return
Expand Down Expand Up @@ -345,7 +346,8 @@ func PostWebhook(c *gin.Context) {
logrus.Debug("updating status to pending")
b.SetStatus(constants.StatusPending)

// if this is a comment on a pull_request event
// if the event is issue_comment and the issue is a pull request,
// call SCM for more data not provided in webhook payload
if strings.EqualFold(b.GetEvent(), constants.EventComment) && webhook.PRNumber > 0 {
commit, branch, baseref, headref, err := scm.FromContext(c).GetPullRequest(u, repo, webhook.PRNumber)
if err != nil {
Expand All @@ -366,6 +368,7 @@ func PostWebhook(c *gin.Context) {

// variable to store changeset files
var files []string

// check if the build event is not issue_comment or pull_request
if !strings.EqualFold(b.GetEvent(), constants.EventComment) &&
!strings.EqualFold(b.GetEvent(), constants.EventPull) {
Expand Down Expand Up @@ -423,7 +426,7 @@ func PostWebhook(c *gin.Context) {
time.Sleep(time.Duration(i) * time.Second)
}

// send API call to attempt to capture the pipeline
// send database call to attempt to capture the pipeline if we already processed it before
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, b.GetCommit(), repo)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
Expand Down Expand Up @@ -463,7 +466,7 @@ func PostWebhook(c *gin.Context) {
return
}

// update repo fields with any changes from SCM process
// update DB record of repo (repo) with any changes captured from webhook payload (r)
repo.SetTopics(r.GetTopics())
repo.SetBranch(r.GetBranch())

Expand Down Expand Up @@ -525,7 +528,7 @@ func PostWebhook(c *gin.Context) {
// before compiling. After we're done compiling, we reset the pipeline type.
repo.SetPipelineType(pipelineType)

// skip the build if only the init or clone steps are found
// skip the build if pipeline compiled to only the init and clone steps
skip := build.SkipEmptyBuild(p)
if skip != "" {
// set build to successful status
Expand Down Expand Up @@ -678,6 +681,8 @@ func PostWebhook(c *gin.Context) {
)
}

// handleRepositoryEvent is a helper function that processes repository events from the SCM and updates
// the database resources with any relevant changes resulting from the event, such as name changes, transfers, etc.
func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadata, h *library.Hook, r *library.Repo) (*library.Repo, error) {
logrus.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName())

Expand All @@ -690,7 +695,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat
}()

switch h.GetEventAction() {
// if action is rename, go through rename routine
// if action is renamed or transferred, go through rename routine
case constants.ActionRenamed, constants.ActionTransferred:
r, err := renameRepository(ctx, h, r, c, m)
if err != nil {
Expand Down