Skip to content

Commit

Permalink
fix: avoid double responses
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Sep 12, 2024
1 parent f4a949d commit 2139c9c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,6 @@ func PostWebhook(c *gin.Context) {
}
}

c.JSON(http.StatusCreated, b)

// regardless of whether the build is published to queue, we want to attempt to auto-cancel if no errors
defer func() {
if err == nil && build.ShouldAutoCancel(p.Metadata.AutoCancel, b, repo.GetBranch()) {
Expand Down Expand Up @@ -531,6 +529,10 @@ func PostWebhook(c *gin.Context) {
}
}()

// track if we have already responded to the http request
// helps prevent multiple responses to the same request in the event of errors
responded := false

// if the webhook was from a Pull event from a forked repository, verify it is allowed to run
if webhook.PullRequest.IsFromFork {
l.Tracef("inside %s workflow for fork PR build %s/%d", repo.GetApproveBuild(), repo.GetFullName(), b.GetNumber())
Expand All @@ -540,8 +542,12 @@ func PostWebhook(c *gin.Context) {
err = gatekeepBuild(c, b, repo)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)
} else {
c.JSON(http.StatusCreated, b)
}

responded = true

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

View workflow job for this annotation

GitHub Actions / golangci

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

ineffectual assignment to responded (ineffassign)
Raw output
api/webhook/post.go:549:4: ineffectual assignment to responded (ineffassign)
			responded = true
			^

return
case constants.ApproveForkNoWrite:
// determine if build sender has write access to parent repo. If not, this call will result in an error
Expand All @@ -550,8 +556,12 @@ func PostWebhook(c *gin.Context) {
err = gatekeepBuild(c, b, repo)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)
} else {
c.JSON(http.StatusCreated, b)
}

responded = true

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

View workflow job for this annotation

GitHub Actions / golangci

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

ineffectual assignment to responded (ineffassign)
Raw output
api/webhook/post.go:563:5: ineffectual assignment to responded (ineffassign)
				responded = true
				^

return
}

Expand All @@ -564,14 +574,20 @@ func PostWebhook(c *gin.Context) {
contributor, err := scm.FromContext(c).RepoContributor(ctx, repo.GetOwner(), b.GetSender(), repo.GetOrg(), repo.GetName())
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)

responded = true
}

if !contributor {
err = gatekeepBuild(c, b, repo)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)
} else if !responded {
c.JSON(http.StatusCreated, b)
}

responded = true

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

View workflow job for this annotation

GitHub Actions / golangci

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

ineffectual assignment to responded (ineffassign)
Raw output
api/webhook/post.go:589:5: ineffectual assignment to responded (ineffassign)
				responded = true
				^

return
}

Expand All @@ -597,6 +613,11 @@ func PostWebhook(c *gin.Context) {
item,
b.GetHost(),
)

// respond only when necessary
if !responded {
c.JSON(http.StatusCreated, b)
}
}

// handleRepositoryEvent is a helper function that processes repository events from the SCM and updates
Expand Down

0 comments on commit 2139c9c

Please sign in to comment.