From 0e25926faf882ee049266837aa504a20f2130054 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 6 Apr 2020 09:40:55 +0200 Subject: [PATCH 1/4] fix(hooks): compute git variable on pullrequest --- engine/hooks/bitbucket_cloud.go | 6 +- engine/hooks/bitbucket_server.go | 14 +-- engine/hooks/github.go | 10 +- engine/hooks/gitlab.go | 10 +- engine/hooks/tasks_bitbucket_test.go | 168 +++++++++++++-------------- engine/hooks/variable.go | 26 +++-- 6 files changed, 118 insertions(+), 116 deletions(-) diff --git a/engine/hooks/bitbucket_cloud.go b/engine/hooks/bitbucket_cloud.go index dc9bfb06af..5bfbbeaa35 100644 --- a/engine/hooks/bitbucket_cloud.go +++ b/engine/hooks/bitbucket_cloud.go @@ -68,9 +68,9 @@ func getVariableFromBitbucketCloudChange(ctx context.Context, payload map[string log.Warning(ctx, "Uknown push type: %s", change.New.Type) return } - payload[GIT_HASH_BEFORE] = change.Old.Target.Hash - payload[GIT_HASH] = change.New.Target.Hash - hashShort := change.New.Target.Hash + payload[GIT_HASH] = change.Old.Target.Hash + payload[GIT_HASH_DEST] = change.New.Target.Hash + hashShort := change.Old.Target.Hash if len(hashShort) >= 7 { hashShort = hashShort[:7] } diff --git a/engine/hooks/bitbucket_server.go b/engine/hooks/bitbucket_server.go index f49e9c0f82..f2b6394fa9 100644 --- a/engine/hooks/bitbucket_server.go +++ b/engine/hooks/bitbucket_server.go @@ -91,14 +91,14 @@ func getVariableFromBitbucketServerRepository(payload map[string]interface{}, re if repo == nil { return } - payload[GIT_REPOSITORY] = fmt.Sprintf("%s/%s", repo.Project.Key, repo.Slug) + 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) + payload[GIT_REPOSITORY] = fmt.Sprintf("%s/%s", repo.Project.Key, repo.Slug) } func getVariableFromBitbucketServerAuthor(payload map[string]interface{}, actor *sdk.BitbucketServerActor) { @@ -119,11 +119,11 @@ func getVariableFromBitbucketServerPullRequest(payload map[string]interface{}, p payload[PR_ID] = pr.ID payload[PR_STATE] = pr.State payload[PR_TITLE] = pr.Title - payload[GIT_BRANCH_BEFORE] = pr.FromRef.DisplayID - payload[GIT_HASH_BEFORE] = pr.FromRef.LatestCommit - payload[GIT_BRANCH] = pr.ToRef.DisplayID - payload[GIT_HASH] = pr.ToRef.LatestCommit - hashShort := pr.ToRef.LatestCommit + 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 + hashShort := pr.FromRef.LatestCommit if len(hashShort) >= 7 { hashShort = hashShort[:7] } diff --git a/engine/hooks/github.go b/engine/hooks/github.go index a579737504..54d4cfd7d3 100644 --- a/engine/hooks/github.go +++ b/engine/hooks/github.go @@ -38,16 +38,16 @@ func (s *Service) generatePayloadFromGithubRequest(ctx context.Context, t *sdk.T } } if request.Before != "" { - payload[GIT_HASH_BEFORE] = request.Before - } - if request.After != "" { - payload[GIT_HASH] = request.After - hashShort := request.After + payload[GIT_HASH] = request.Before + hashShort := request.Before if len(hashShort) >= 7 { hashShort = hashShort[:7] } payload[GIT_HASH_SHORT] = hashShort } + if request.After != "" { + payload[GIT_HASH_DEST] = request.After + } getPayloadFromRepository(payload, request.Repository) getPayloadFromCommit(payload, request.HeadCommit) diff --git a/engine/hooks/gitlab.go b/engine/hooks/gitlab.go index 02ed29d742..4a4447238c 100644 --- a/engine/hooks/gitlab.go +++ b/engine/hooks/gitlab.go @@ -46,16 +46,16 @@ func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.T } } if request.Before != "" { - payload[GIT_HASH_BEFORE] = request.Before - } - if request.After != "" { - payload[GIT_HASH] = request.After - hashShort := request.After + payload[GIT_HASH] = request.Before + hashShort := request.Before if len(hashShort) >= 7 { hashShort = hashShort[:7] } payload[GIT_HASH_SHORT] = hashShort } + if request.After != "" { + payload[GIT_HASH_DEST] = request.After + } getPayloadFromGitlabProject(payload, request.Project) getPayloadFromGitlabCommit(payload, request.Commits) diff --git a/engine/hooks/tasks_bitbucket_test.go b/engine/hooks/tasks_bitbucket_test.go index 7ca0227266..224f8611e5 100644 --- a/engine/hooks/tasks_bitbucket_test.go +++ b/engine/hooks/tasks_bitbucket_test.go @@ -64,18 +64,18 @@ func Test_doWebHookExecutionBitbucketPRReviewerUpdated(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:reviewer:updated", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) } func Test_doWebHookExecutionBitbucketPRReviewerApproved(t *testing.T) { @@ -107,18 +107,18 @@ func Test_doWebHookExecutionBitbucketPRReviewerApproved(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:reviewer:approved", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "francois.samin", hs[0].Payload[PR_REVIEWER]) test.Equal(t, "francois.samin@foo", hs[0].Payload[PR_REVIEWER_EMAIL]) @@ -156,18 +156,18 @@ func Test_doWebHookExecutionBitbucketPRReviewerUnapproved(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:reviewer:unapproved", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "francois.samin", hs[0].Payload[PR_REVIEWER]) test.Equal(t, "francois.samin@foo", hs[0].Payload[PR_REVIEWER_EMAIL]) @@ -205,18 +205,18 @@ func Test_doWebHookExecutionBitbucketPRReviewerNeedsWork(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:reviewer:needs_work", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "francois.samin", hs[0].Payload[PR_REVIEWER]) test.Equal(t, "francois.samin@foo", hs[0].Payload[PR_REVIEWER_EMAIL]) @@ -254,18 +254,18 @@ func Test_doWebHookExecutionBitbucketPRCommentAdded(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:comment:added", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "my comment added", hs[0].Payload[PR_COMMENT_TEXT]) test.Equal(t, "steven.guiheux", hs[0].Payload[PR_COMMENT_AUTHOR]) @@ -302,18 +302,18 @@ func Test_doWebHookExecutionBitbucketPRCommentDeleted(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:comment:deleted", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "my comment deleted", hs[0].Payload[PR_COMMENT_TEXT]) test.Equal(t, "steven.guiheux", hs[0].Payload[PR_COMMENT_AUTHOR]) @@ -350,18 +350,18 @@ func Test_doWebHookExecutionBitbucketPRCommentModified(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:comment:edited", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "my comment edited", hs[0].Payload[PR_COMMENT_TEXT]) test.Equal(t, "steven.guiheux", hs[0].Payload[PR_COMMENT_AUTHOR]) @@ -400,18 +400,18 @@ func Test_doWebHookExecutionBitbucketPROpened(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:opened", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) } @@ -444,18 +444,18 @@ func Test_doWebHookExecutionBitbucketPRModified(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:modified", 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, "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, "OPEN", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) test.Equal(t, "Update README.md", hs[0].Payload[PR_PREVIOUS_TITLE]) test.Equal(t, "prev_branch", hs[0].Payload[PR_PREVIOUS_BRANCH]) @@ -491,18 +491,18 @@ func Test_doWebHookExecutionBitbucketPRMerged(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:opened", 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, "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, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) } @@ -535,18 +535,18 @@ func Test_doWebHookExecutionBitbucketPRDeleted(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:deleted", 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, "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, "DELETED", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) } func Test_doWebHookExecutionBitbucketPRDeclined(t *testing.T) { @@ -578,18 +578,18 @@ func Test_doWebHookExecutionBitbucketPRDeclined(t *testing.T) { test.Equal(t, "john.doe", hs[0].Payload[GIT_AUTHOR]) test.Equal(t, "john.doe@targate.fr", hs[0].Payload[GIT_AUTHOR_EMAIL]) - test.Equal(t, "dest_branch", hs[0].Payload[GIT_BRANCH]) - test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY_BEFORE]) - test.Equal(t, "654321654321", hs[0].Payload[GIT_HASH]) - test.Equal(t, "6543216", hs[0].Payload[GIT_HASH_SHORT]) + 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, "pr:declined", 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, "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, "DECLINED", hs[0].Payload[PR_STATE]) test.Equal(t, "My First PR", hs[0].Payload[PR_TITLE]) - test.Equal(t, "my/repo", hs[0].Payload[GIT_REPOSITORY]) + test.Equal(t, "fork/repo", hs[0].Payload[GIT_REPOSITORY]) } func Test_doWebHookExecutionBitbucketMultiple(t *testing.T) { diff --git a/engine/hooks/variable.go b/engine/hooks/variable.go index 87004ddff5..97a6956854 100644 --- a/engine/hooks/variable.go +++ b/engine/hooks/variable.go @@ -19,18 +19,20 @@ 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_BEFORE = "git.branch.before" - GIT_TAG = "git.tag" - GIT_HASH_BEFORE = "git.hash.before" - GIT_HASH = "git.hash" - GIT_HASH_SHORT = "git.hash.short" - GIT_REPOSITORY = "git.repository" - GIT_REPOSITORY_BEFORE = "git.repository.before" - 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_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" From fc429f3d227b609794f33bd9d43e886873bf18b3 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 6 Apr 2020 10:34:08 +0200 Subject: [PATCH 2/4] fix: add doc --- docs/content/docs/concepts/variables.md | 49 ++++++++++++++++++++----- engine/hooks/bitbucket_cloud.go | 6 +-- engine/hooks/github.go | 8 ++-- engine/hooks/gitlab.go | 10 ++--- engine/hooks/variable.go | 3 +- 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/docs/content/docs/concepts/variables.md b/docs/content/docs/concepts/variables.md index e6ed3d6412..18059be490 100644 --- a/docs/content/docs/concepts/variables.md +++ b/docs/content/docs/concepts/variables.md @@ -97,15 +97,46 @@ echo $CDS_PARENT_APPLICATION Here is the list of git variables: -- `{{.git.hash}}` -- `{{.git.hash.short}}` -- `{{.git.url}}` -- `{{.git.http_url}}` -- `{{.git.branch}}` -- `{{.git.tag}}` -- `{{.git.author}}` -- `{{.git.message}}` -- `{{.git.server}}` +- `{{.git.hash.before}}`: SHA of the most recent commit before the push +- `{{.git.hash}}`: SHA of the most recent commit after the push +- `{{.git.hash.short}}`: Short version of git.hash +- `{{.git.hook}}`: Name of the event that trigger the run +- `{{.git.url}}`: Git ssh URL used to clone +- `{{.git.http_url}}`: Git http url used to clone +- `{{.git.branch}}`: + - Push event: Name of the branch where the push occured + - PullRequest event: Name of the source branch +- `{{.git.tag}}`: Name of the tag that triggered the run +- `{{.git.author}}`: Name of the most recent commit author +- `{{.git.author.email}}`: Email of the most recent commit author +- `{{.git.message}}`: Git message of the most recent commit +- `{{.git.server}}`: Name of the repository manager +- `{{.git.repository}}`: + - Push event: Name of the repository + - PullRequest event: Name of the source repository + +Here is the list of git variables available only for Bitbucket server + +- `{{.git.hash.dest}}`: SHA of the most rcent commit on destination branch ( PullRequest event ) +- `{{.git.branch.dest}}`: Name of the destination branch on a pull request event +- `{{.git.repository.dest}}`: Name of the target repository on a pull request event +- `{{.git.pr.id}}`: Identifier of the pullrequest +- `{{.git.pr.title}}`: Title of the pullrequest +- `{{.git.pr.state}}`: Status of the pullrequest +- `{{.git.pr.previous.title}}`: Previous title of the pullrequest +- `{{.git.pr.previous.branch}}`: Previous target branch of the pullrequest +- `{{.git.pr.previous.hash}}`: Previous target hash of the pullrequest +- `{{.git.pr.previous.state}}`: Previous status of the pullrequest +- `{{.git.pr.reviewer}}`: Name of the reviewer +- `{{.git.pr.reviewer.email}}`: Email of the reviewer +- `{{.git.pr.reviewer.status}}`: Status of the review +- `{{.git.pr.reviewer.role}}`: Role of the reviewer +- `{{.git.pr.comment}}`: Comment written by the reviewer +- `{{.git.pr.comment.before}}`: Previous comment +- `{{.git.pr.comment.author}}`: Author name of the comment +- `{{.git.pr.comment.author.email}}` Author email of the comment + + ## Pipeline parameters diff --git a/engine/hooks/bitbucket_cloud.go b/engine/hooks/bitbucket_cloud.go index 5bfbbeaa35..dc9bfb06af 100644 --- a/engine/hooks/bitbucket_cloud.go +++ b/engine/hooks/bitbucket_cloud.go @@ -68,9 +68,9 @@ func getVariableFromBitbucketCloudChange(ctx context.Context, payload map[string log.Warning(ctx, "Uknown push type: %s", change.New.Type) return } - payload[GIT_HASH] = change.Old.Target.Hash - payload[GIT_HASH_DEST] = change.New.Target.Hash - hashShort := change.Old.Target.Hash + payload[GIT_HASH_BEFORE] = change.Old.Target.Hash + payload[GIT_HASH] = change.New.Target.Hash + hashShort := change.New.Target.Hash if len(hashShort) >= 7 { hashShort = hashShort[:7] } diff --git a/engine/hooks/github.go b/engine/hooks/github.go index 54d4cfd7d3..0cf62af2a5 100644 --- a/engine/hooks/github.go +++ b/engine/hooks/github.go @@ -38,16 +38,16 @@ func (s *Service) generatePayloadFromGithubRequest(ctx context.Context, t *sdk.T } } if request.Before != "" { - payload[GIT_HASH] = request.Before + payload[GIT_HASH_BEFORE] = request.Before + } + if request.After != "" { + payload[GIT_HASH] = request.After hashShort := request.Before if len(hashShort) >= 7 { hashShort = hashShort[:7] } payload[GIT_HASH_SHORT] = hashShort } - if request.After != "" { - payload[GIT_HASH_DEST] = request.After - } getPayloadFromRepository(payload, request.Repository) getPayloadFromCommit(payload, request.HeadCommit) diff --git a/engine/hooks/gitlab.go b/engine/hooks/gitlab.go index 4a4447238c..d93d3c4f13 100644 --- a/engine/hooks/gitlab.go +++ b/engine/hooks/gitlab.go @@ -46,15 +46,15 @@ func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.T } } if request.Before != "" { - payload[GIT_HASH] = request.Before + payload[GIT_HASH_BEFORE] = request.Before + } + if request.After != "" { + payload[GIT_HASH] = request.After hashShort := request.Before if len(hashShort) >= 7 { hashShort = hashShort[:7] } - payload[GIT_HASH_SHORT] = hashShort - } - if request.After != "" { - payload[GIT_HASH_DEST] = request.After + payload[GIT_HASH] = hashShort } getPayloadFromGitlabProject(payload, request.Project) diff --git a/engine/hooks/variable.go b/engine/hooks/variable.go index 97a6956854..ef264a5ac4 100644 --- a/engine/hooks/variable.go +++ b/engine/hooks/variable.go @@ -6,7 +6,7 @@ const ( PR_STATE = "git.pr.state" PR_PREVIOUS_TITLE = "git.pr.previous.title" PR_PREVIOUS_BRANCH = "git.pr.previous.branch" - PR_PREVIOUS_HASH = "git.pr.previous.has" + PR_PREVIOUS_HASH = "git.pr.previous.hash" PR_PREVIOUS_STATE = "git.pr.previous.state" PR_REVIEWER = "git.pr.reviewer" @@ -22,7 +22,6 @@ const ( 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" From 5d99764f9970b6a14580aa98df34782557b46e11 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 6 Apr 2020 10:40:27 +0200 Subject: [PATCH 3/4] fix: short --- engine/hooks/github.go | 2 +- engine/hooks/gitlab.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/hooks/github.go b/engine/hooks/github.go index 0cf62af2a5..a579737504 100644 --- a/engine/hooks/github.go +++ b/engine/hooks/github.go @@ -42,7 +42,7 @@ func (s *Service) generatePayloadFromGithubRequest(ctx context.Context, t *sdk.T } if request.After != "" { payload[GIT_HASH] = request.After - hashShort := request.Before + hashShort := request.After if len(hashShort) >= 7 { hashShort = hashShort[:7] } diff --git a/engine/hooks/gitlab.go b/engine/hooks/gitlab.go index d93d3c4f13..48abdcf52b 100644 --- a/engine/hooks/gitlab.go +++ b/engine/hooks/gitlab.go @@ -50,7 +50,7 @@ func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.T } if request.After != "" { payload[GIT_HASH] = request.After - hashShort := request.Before + hashShort := request.After if len(hashShort) >= 7 { hashShort = hashShort[:7] } From d9f061f6b62260e2e43761b4cef3d9f8132b95a0 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 6 Apr 2020 11:06:57 +0200 Subject: [PATCH 4/4] fix: short hash --- engine/hooks/gitlab.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/hooks/gitlab.go b/engine/hooks/gitlab.go index 48abdcf52b..02ed29d742 100644 --- a/engine/hooks/gitlab.go +++ b/engine/hooks/gitlab.go @@ -54,7 +54,7 @@ func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.T if len(hashShort) >= 7 { hashShort = hashShort[:7] } - payload[GIT_HASH] = hashShort + payload[GIT_HASH_SHORT] = hashShort } getPayloadFromGitlabProject(payload, request.Project)