Skip to content

Commit

Permalink
Support bitbucket server pr:deleted event
Browse files Browse the repository at this point in the history
If a pull request is deleted, Atlantis should delete the locks.
Fixes #498
  • Loading branch information
lkysow committed Mar 1, 2019
1 parent 7bf5f07 commit 2fb8f4d
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 17 deletions.
2 changes: 1 addition & 1 deletion server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (e *EventParser) GetBitbucketServerPullEventType(eventTypeHeader string) mo
switch eventTypeHeader {
case bitbucketserver.PullCreatedHeader:
return models.OpenedPullEvent
case bitbucketserver.PullMergedHeader, bitbucketserver.PullDeclinedHeader:
case bitbucketserver.PullMergedHeader, bitbucketserver.PullDeclinedHeader, bitbucketserver.PullDeletedHeader:
return models.ClosedPullEvent
}
return models.OtherPullEvent
Expand Down
4 changes: 4 additions & 0 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,10 @@ func TestGetBitbucketServerEventType(t *testing.T) {
header: "pr:declined",
exp: models.ClosedPullEvent,
},
{
header: "pr:deleted",
exp: models.ClosedPullEvent,
},
{
header: "random",
exp: models.OtherPullEvent,
Expand Down
1 change: 1 addition & 0 deletions server/events/vcs/bitbucketserver/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
PullCreatedHeader = "pr:opened"
PullMergedHeader = "pr:merged"
PullDeclinedHeader = "pr:declined"
PullDeletedHeader = "pr:deleted"
PullCommentCreatedHeader = "pr:comment:added"
)

Expand Down
2 changes: 1 addition & 1 deletion server/events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (e *EventsController) handleBitbucketServerPost(w http.ResponseWriter, r *h
}
}
switch eventType {
case bitbucketserver.PullCreatedHeader, bitbucketserver.PullMergedHeader, bitbucketserver.PullDeclinedHeader:
case bitbucketserver.PullCreatedHeader, bitbucketserver.PullMergedHeader, bitbucketserver.PullDeclinedHeader, bitbucketserver.PullDeletedHeader:
e.Logger.Debug("handling as pull request state changed event")
e.handleBitbucketServerPullRequestEvent(w, eventType, body, reqID)
return
Expand Down
77 changes: 77 additions & 0 deletions server/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,83 @@ func TestPost_GitlabMergeRequestSuccess(t *testing.T) {
responseContains(t, w, http.StatusOK, "Pull request cleaned successfully")
}

// Test Bitbucket server pull closed events.
func TestPost_BBServerPullClosed(t *testing.T) {
cases := []struct {
header string
}{
{
"pr:deleted",
},
{
"pr:merged",
},
{
"pr:declined",
},
}

for _, c := range cases {
t.Run(c.header, func(t *testing.T) {
RegisterMockTestingT(t)
pullCleaner := emocks.NewMockPullCleaner()
whitelist, err := events.NewRepoWhitelistChecker("*")
Ok(t, err)
ec := &server.EventsController{
PullCleaner: pullCleaner,
Parser: &events.EventParser{
BitbucketUser: "bb-user",
BitbucketToken: "bb-token",
BitbucketServerURL: "https://bbserver.com",
},
RepoWhitelistChecker: whitelist,
SupportedVCSHosts: []models.VCSHostType{models.BitbucketServer},
VCSClient: nil,
}

// Build HTTP request.
requestBytes, err := ioutil.ReadFile(filepath.Join("testfixtures", "bb-server-pull-deleted-event.json"))
// Replace the eventKey field with our event type.
requestJSON := strings.Replace(string(requestBytes), `"eventKey":"pr:deleted",`, fmt.Sprintf(`"eventKey":"%s",`, c.header), -1)
Ok(t, err)
req, err := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(requestJSON)))
Ok(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Event-Key", c.header)
req.Header.Set("X-Request-ID", "request-id")

// Send the request.
w := httptest.NewRecorder()
ec.Post(w, req)

// Make our assertions.
responseContains(t, w, 200, "Pull request cleaned successfully")

expRepo := models.Repo{
FullName: "project/repository",
Owner: "project",
Name: "repository",
CloneURL: "https://bb-user:[email protected]/scm/proj/repository.git",
SanitizedCloneURL: "https://bbserver.com/scm/proj/repository.git",
VCSHost: models.VCSHost{
Hostname: "bbserver.com",
Type: models.BitbucketServer,
},
}
pullCleaner.VerifyWasCalledOnce().CleanUpPull(expRepo, models.PullRequest{
Num: 10,
HeadCommit: "2d9fb6b9a46eafb1dcef7b008d1a429d45ca742c",
URL: "https://bbserver.com/projects/PROJ/repos/repository/pull-requests/10",
HeadBranch: "decline-me",
BaseBranch: "master",
Author: "admin",
State: models.OpenPullState,
BaseRepo: expRepo,
})
})
}
}

func TestPost_PullOpenedOrUpdated(t *testing.T) {
cases := []struct {
Description string
Expand Down
32 changes: 17 additions & 15 deletions server/logging/simple_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,25 @@ func (l *SimpleLogger) Err(format string, a ...interface{}) {

// Log writes the log at level.
func (l *SimpleLogger) Log(level LogLevel, format string, a ...interface{}) {
levelStr := l.levelToString(level)
msg := l.capitalizeFirstLetter(fmt.Sprintf(format, a...))

// Only log this message if configured to log at this level.
if l.Level <= level {
datetime := time.Now().Format("2006/01/02 15:04:05-0700")
var caller string
if l.Level <= Debug {
file, line := l.callSite(3)
caller = fmt.Sprintf(" %s:%d", file, line)
if l != nil {
levelStr := l.levelToString(level)
msg := l.capitalizeFirstLetter(fmt.Sprintf(format, a...))

// Only log this message if configured to log at this level.
if l.Level <= level {
datetime := time.Now().Format("2006/01/02 15:04:05-0700")
var caller string
if l.Level <= Debug {
file, line := l.callSite(3)
caller = fmt.Sprintf(" %s:%d", file, line)
}
l.Logger.Printf("%s [%s]%s %s: %s\n", datetime, levelStr, caller, l.Source, msg) // noline: errcheck
}
l.Logger.Printf("%s [%s]%s %s: %s\n", datetime, levelStr, caller, l.Source, msg) // noline: errcheck
}

// Keep history at all log levels.
if l.KeepHistory {
l.saveToHistory(levelStr, msg)
// Keep history at all log levels.
if l.KeepHistory {
l.saveToHistory(levelStr, msg)
}
}
}

Expand Down
101 changes: 101 additions & 0 deletions server/testfixtures/bb-server-pull-deleted-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"eventKey":"pr:deleted",
"date":"2017-09-19T11:16:17+1000",
"actor":{
"name":"admin",
"emailAddress":"[email protected]",
"id":1,
"displayName":"Administrator",
"active":true,
"slug":"admin",
"type":"NORMAL"
},
"pullRequest":{
"id":10,
"version":3,
"title":"Commit message",
"state":"OPEN",
"open":true,
"closed":false,
"createdDate":1505783668760,
"updatedDate":1505783750704,
"fromRef":{
"id":"refs/heads/decline-me",
"displayId":"decline-me",
"latestCommit":"2d9fb6b9a46eafb1dcef7b008d1a429d45ca742c",
"repository":{
"slug":"repository",
"id":84,
"name":"repository",
"scmId":"git",
"state":"AVAILABLE",
"statusMessage":"Available",
"forkable":true,
"project":{
"key":"PROJ",
"id":84,
"name":"project",
"public":false,
"type":"NORMAL"
},
"public":false
}
},
"toRef":{
"id":"refs/heads/master",
"displayId":"master",
"latestCommit":"7e48f426f0a6e47c5b5e862c31be6ca965f82c9c",
"repository":{
"slug":"repository",
"id":84,
"name":"repository",
"scmId":"git",
"state":"AVAILABLE",
"statusMessage":"Available",
"forkable":true,
"project":{
"key":"PROJ",
"id":84,
"name":"project",
"public":false,
"type":"NORMAL"
},
"public":false
}
},
"locked":false,
"author":{
"user":{
"name":"admin",
"emailAddress":"[email protected]",
"id":1,
"displayName":"Administrator",
"active":true,
"slug":"admin",
"type":"NORMAL"
},
"role":"AUTHOR",
"approved":false,
"status":"UNAPPROVED"
},
"reviewers":[
{
"user":{
"name":"user",
"emailAddress":"[email protected]",
"id":2,
"displayName":"User",
"active":true,
"slug":"user",
"type":"NORMAL"
},
"role":"REVIEWER",
"approved":false,
"status":"UNAPPROVED"
}
],
"participants":[

]
}
}

0 comments on commit 2fb8f4d

Please sign in to comment.