Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ dist/
tmp-CHANGELOG.md

.envrc

# IDE files
*.code-workspace
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/hashicorp/go-getter/v2 v2.2.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/hashicorp/terraform-config-inspect v0.0.0-20230413234026-f1617e8a5fcc
github.com/kr/pretty v0.3.1
github.com/mcdafydd/go-azuredevops v0.12.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU=
github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.16.2 h1:mpkHZh/Tv+xet3sy3F9Ld4FyI2tUpWe9x3XtPx9f1a0=
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/events/events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ func (e *VCSEventsController) handleBitbucketCloudPullRequestEvent(w http.Respon
e.respond(w, logging.Error, http.StatusBadRequest, "Error parsing pull data: %s %s=%s", err, bitbucketCloudRequestIDHeader, reqID)
return
}
pullEventType := e.Parser.GetBitbucketCloudPullEventType(eventType)
e.Logger.Debug("SHA is %q", pull.HeadCommit)
pullEventType := e.Parser.GetBitbucketCloudPullEventType(eventType, pull.HeadCommit, pull.URL)
e.Logger.Info("identified event as type %q", pullEventType.String())
resp := e.handlePullRequestEvent(e.Logger, baseRepo, headRepo, pull, user, pullEventType)

Expand Down
14 changes: 12 additions & 2 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/go-playground/validator/v10"
"github.com/google/go-github/v52/github"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/mcdafydd/go-azuredevops/azuredevops"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/command"
Expand All @@ -34,6 +35,8 @@ import (
const gitlabPullOpened = "opened"
const usagesCols = 90

var lastBitbucketSha, _ = lru.New[string, string](300)

// PullCommand is a command to run on a pull request.
type PullCommand interface {
// CommandName is the name of the command we're running.
Expand Down Expand Up @@ -267,7 +270,7 @@ type EventParsing interface {

// GetBitbucketCloudPullEventType returns the type of the pull request
// event given the Bitbucket Cloud header.
GetBitbucketCloudPullEventType(eventTypeHeader string) models.PullRequestEventType
GetBitbucketCloudPullEventType(eventTypeHeader string, sha string, pr string) models.PullRequestEventType

// ParseBitbucketServerPullEvent parses a pull request event from Bitbucket
// Server.
Expand Down Expand Up @@ -343,11 +346,18 @@ func (e *EventParser) ParseAPIPlanRequest(vcsHostType models.VCSHostType, repoFu

// GetBitbucketCloudPullEventType returns the type of the pull request
// event given the Bitbucket Cloud header.
func (e *EventParser) GetBitbucketCloudPullEventType(eventTypeHeader string) models.PullRequestEventType {
func (e *EventParser) GetBitbucketCloudPullEventType(eventTypeHeader string, sha string, pr string) models.PullRequestEventType {
switch eventTypeHeader {
case bitbucketcloud.PullCreatedHeader:
lastBitbucketSha.Add(pr, sha)
return models.OpenedPullEvent
case bitbucketcloud.PullUpdatedHeader:
lastSha, _ := lastBitbucketSha.Get(pr)
if sha == lastSha {
// No change, ignore
return models.OtherPullEvent
}
lastBitbucketSha.Add(pr, sha)
return models.UpdatedPullEvent
case bitbucketcloud.PullFulfilledHeader, bitbucketcloud.PullRejectedHeader:
return models.ClosedPullEvent
Expand Down
40 changes: 39 additions & 1 deletion server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,42 @@ func TestParseBitbucketCloudPullEvent_States(t *testing.T) {
}
}

func TestBitBucketNonCodeChangesAreIgnored(t *testing.T) {
// lets say a user opens a PR
act := parser.GetBitbucketCloudPullEventType("pullrequest:created", "fakeSha", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.OpenedPullEvent, act)
// Another update with same SHA should be ignored
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.OtherPullEvent, act)
// Only if SHA changes do we act
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha2", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.UpdatedPullEvent, act)

// If sha changes in seperate PR,
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "otherPRSha", "https://github.com/fakeorg/fakerepo/pull/2")
Equals(t, models.UpdatedPullEvent, act)
// We will still ignore same shas in first PR
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha2", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.OtherPullEvent, act)
}

func TestBitbucketShaCacheExpires(t *testing.T) {
// lets say a user opens a PR
act := parser.GetBitbucketCloudPullEventType("pullrequest:created", "fakeSha", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.OpenedPullEvent, act)
// Another update with same SHA should be ignored
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.OtherPullEvent, act)
// But after 300 times, the cache should expire
// this is so we don't have ever increasing memory usage
for i := 0; i < 302; i++ {
parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha", fmt.Sprintf("https://github.com/fakeorg/fakerepo/pull/%d", i))
}
// and now SHA will seen as a change again
act = parser.GetBitbucketCloudPullEventType("pullrequest:updated", "fakeSha", "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, models.UpdatedPullEvent, act)
}

func TestGetBitbucketCloudEventType(t *testing.T) {
cases := []struct {
header string
Expand Down Expand Up @@ -1012,7 +1048,9 @@ func TestGetBitbucketCloudEventType(t *testing.T) {
}
for _, c := range cases {
t.Run(c.header, func(t *testing.T) {
act := parser.GetBitbucketCloudPullEventType(c.header)
// we pass in the header as the SHA so the SHA changes each time
// the code will ignore duplicate SHAS to avoid extra TF plans
act := parser.GetBitbucketCloudPullEventType(c.header, c.header, "https://github.com/fakeorg/fakerepo/pull/1")
Equals(t, c.exp, act)
})
}
Expand Down
4 changes: 2 additions & 2 deletions server/events/mocks/mock_event_parsing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.