Skip to content

Commit

Permalink
fix(hook): in case of pr:merged, default branch is the destination br…
Browse files Browse the repository at this point in the history
…anch (#5584)
  • Loading branch information
sguiheux authored Dec 10, 2020
1 parent debbaf8 commit 7ebebeb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
37 changes: 27 additions & 10 deletions engine/hooks/bitbucket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@ func getVariableFromBitbucketServerChange(payload map[string]interface{}, change
payload[GIT_HASH_SHORT] = sdk.StringFirstN(change.ToHash, 7)
}

func getVariableFromBitbucketServerRepository(payload map[string]interface{}, repo *sdk.BitbucketServerRepository) {
func getVariableFromBitbucketServerDestRepository(payload map[string]interface{}, repo *sdk.BitbucketServerRepository) {
if repo == nil {
return
}
payload[GIT_REPOSITORY_DEST] = fmt.Sprintf("%s/%s", repo.Project.Key, repo.Slug)
}

func getVariableFromBitbucketServerSrcRepository(payload map[string]interface{}, repo *sdk.BitbucketServerRepository) {
if repo == nil {
return
}
payload[GIT_REPOSITORY_BEFORE] = fmt.Sprintf("%s/%s", repo.Project.Key, repo.Slug)
}

func getVariableFromBitbucketServerRepository(payload map[string]interface{}, repo *sdk.BitbucketServerRepository) {
if repo == nil {
return
}
Expand All @@ -112,17 +118,28 @@ func getVariableFromBitbucketServerPullRequest(payload map[string]interface{}, p
if pr == nil {
return
}

payload[PR_ID] = pr.ID
payload[PR_STATE] = pr.State
payload[PR_TITLE] = pr.Title
payload[GIT_BRANCH] = pr.FromRef.DisplayID
payload[GIT_HASH] = pr.FromRef.LatestCommit
payload[GIT_BRANCH_DEST] = pr.ToRef.DisplayID
payload[GIT_HASH_DEST] = pr.ToRef.LatestCommit
payload[GIT_HASH_SHORT] = sdk.StringFirstN(pr.FromRef.LatestCommit, 7)

getVariableFromBitbucketServerRepository(payload, &pr.ToRef.Repository)
getVariableFromBitbucketServerSrcRepository(payload, &pr.FromRef.Repository)

if payload[GIT_EVENT] == "pr:merged" {
payload[GIT_BRANCH] = pr.ToRef.DisplayID
payload[GIT_HASH] = pr.ToRef.LatestCommit
payload[GIT_HASH_SHORT] = sdk.StringFirstN(pr.ToRef.LatestCommit, 7)
payload[GIT_BRANCH_BEFORE] = pr.FromRef.DisplayID
payload[GIT_HASH_BEFORE] = pr.FromRef.LatestCommit
getVariableFromBitbucketServerRepository(payload, &pr.ToRef.Repository)
getVariableFromBitbucketServerSrcRepository(payload, &pr.FromRef.Repository)
} else {
payload[GIT_BRANCH] = pr.FromRef.DisplayID
payload[GIT_HASH] = pr.FromRef.LatestCommit
payload[GIT_HASH_SHORT] = sdk.StringFirstN(pr.FromRef.LatestCommit, 7)
payload[GIT_BRANCH_DEST] = pr.ToRef.DisplayID
payload[GIT_HASH_DEST] = pr.ToRef.LatestCommit
getVariableFromBitbucketServerRepository(payload, &pr.FromRef.Repository)
getVariableFromBitbucketServerDestRepository(payload, &pr.ToRef.Repository)
}
}

func getPayloadFromBitbucketServerPRComment(payload map[string]interface{}, comment *sdk.BitbucketServerComment) {
Expand Down
19 changes: 10 additions & 9 deletions engine/hooks/tasks_bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,19 @@ func Test_doWebHookExecutionBitbucketPRMerged(t *testing.T) {
test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR])
test.Equal(t, "[email protected]", hs[0].Payload[GIT_AUTHOR_EMAIL])

test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH_DEST])
test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY_DEST])
test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH_DEST])
test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH])
test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY])
test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH])
test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT])

test.Equal(t, "pr:merged", hs[0].Payload[GIT_EVENT])
test.Equal(t, "src_branch", hs[0].Payload[GIT_BRANCH_BEFORE])
test.Equal(t, "12345671234567", hs[0].Payload[GIT_HASH_BEFORE])

test.Equal(t, "pr:opened", hs[0].Payload[GIT_EVENT])
test.Equal(t, "src_branch", hs[0].Payload[GIT_BRANCH])
test.Equal(t, "12345671234567", hs[0].Payload[GIT_HASH])
test.Equal(t, "1234567", hs[0].Payload[GIT_HASH_SHORT])
test.Equal(t, "666", hs[0].Payload[PR_ID])
test.Equal(t, "MERGED", hs[0].Payload[PR_STATE])
test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE])
test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY])
test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE])

}

Expand Down Expand Up @@ -2226,7 +2227,7 @@ var bitbucketPrModified = `
}`
var bitbucketPrMerged = `
{
"eventKey": "pr:opened",
"eventKey": "pr:merged",
"date": "2019-10-15T09:33:38+0200",
"actor": {
"name": "john.doe",
Expand Down
28 changes: 15 additions & 13 deletions engine/hooks/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ const (
PR_COMMENT_AUTHOR = "git.pr.comment.author"
PR_COMMENT_AUTHOR_EMAIL = "git.pr.comment.author.email"

GIT_AUTHOR = "git.author"
GIT_AUTHOR_EMAIL = "git.author.email"
GIT_BRANCH = "git.branch"
GIT_BRANCH_DEST = "git.branch.dest"
GIT_TAG = "git.tag"
GIT_HASH_BEFORE = "git.hash.before"
GIT_HASH = "git.hash"
GIT_HASH_DEST = "git.hash.dest"
GIT_HASH_SHORT = "git.hash.short"
GIT_REPOSITORY = "git.repository"
GIT_REPOSITORY_DEST = "git.repository.dest"
GIT_EVENT = "git.hook"
GIT_MESSAGE = "git.message"
GIT_AUTHOR = "git.author"
GIT_AUTHOR_EMAIL = "git.author.email"
GIT_BRANCH = "git.branch"
GIT_BRANCH_BEFORE = "git.branch.before"
GIT_BRANCH_DEST = "git.branch.dest"
GIT_TAG = "git.tag"
GIT_HASH_BEFORE = "git.hash.before"
GIT_HASH = "git.hash"
GIT_HASH_DEST = "git.hash.dest"
GIT_HASH_SHORT = "git.hash.short"
GIT_REPOSITORY = "git.repository"
GIT_REPOSITORY_BEFORE = "git.repository.before"
GIT_REPOSITORY_DEST = "git.repository.dest"
GIT_EVENT = "git.hook"
GIT_MESSAGE = "git.message"

CDS_TRIGGERED_BY_USERNAME = "cds.triggered_by.username"
CDS_TRIGGERED_BY_FULLNAME = "cds.triggered_by.fullname"
Expand Down

0 comments on commit 7ebebeb

Please sign in to comment.