Skip to content

Commit

Permalink
ensure we only run check for events that are refs changed
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Currah <[email protected]>
  • Loading branch information
ryancurrah committed Dec 15, 2023
1 parent 37958b8 commit 132bc9c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions eventsources/sources/bitbucketserver/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
"github.com/mitchellh/mapstructure"
"golang.org/x/exp/slices"

"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/common/logging"
Expand Down Expand Up @@ -94,13 +95,13 @@ func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Requ
return
}

// When enabled and webhook event type is repo:refs_changed, checks if a pull request is opened for the commit,
// if one is opened the event will be skipped.
if bitbucketserverEventSource.SkipRefsOnOpenPullRequest {
// When SkipRefsOnOpenPullRequest is enabled and webhook event type is repo:refs_changed,
// check if a Pull Request is opened for the commit, if one is opened the event will be skipped.
if bitbucketserverEventSource.SkipRefsOnOpenPullRequest && slices.Contains(bitbucketserverEventSource.Events, "repo:refs_changed") {
refsChanged := refsChangedWebhookEvent{}
err := json.Unmarshal(body, &refsChanged)
if err != nil {
logger.Errorf("unable to inspect webhook body", zap.Error(err))
logger.Errorf("failed to read webhook body", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
Expand All @@ -110,14 +111,14 @@ func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Requ
if refsChanged.EventKey == "repo:refs_changed" && len(refsChanged.Changes) > 0 && refsChanged.Changes[0].Type != "DELETE" {
hasOpenPR, err := router.refsChangedHasOpenPullRequest(refsChanged)
if err != nil {
logger.Errorf("unable to check if changed ref has an open pull request", zap.Error(err))
logger.Errorf("failed to check if changed ref has an open pull request", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}

// Do not publish an event if a Pull Request is already opened since we will
// receive a pr:from_ref_updated event which would start multiple workflows
// receive a pr:from_ref_updated event, which would start multiple workflows
if hasOpenPR {
logger.Info("request successfully processed")
common.SendSuccessResponse(writer, "success")
Expand Down

0 comments on commit 132bc9c

Please sign in to comment.