From df5287bb653e232cc849f5bf61b8bb82d790b1ba Mon Sep 17 00:00:00 2001 From: hg <36822348+hg@users.noreply.github.com> Date: Thu, 8 Aug 2024 22:01:59 +0500 Subject: [PATCH] Set `CI_PREV_COMMIT_{SOURCE,TARGET}_BRANCH` as mentioned in the documentation (#4001) --- .../frontend/metadata/drone_compatibility_test.go | 2 ++ pipeline/frontend/metadata/environment.go | 15 ++++++++++++--- server/pipeline/stepbuilder/metadata_test.go | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pipeline/frontend/metadata/drone_compatibility_test.go b/pipeline/frontend/metadata/drone_compatibility_test.go index 2e06fdabf..228c87b0a 100644 --- a/pipeline/frontend/metadata/drone_compatibility_test.go +++ b/pipeline/frontend/metadata/drone_compatibility_test.go @@ -151,6 +151,8 @@ CI_PREV_COMMIT_REF=refs/heads/main CI_PREV_COMMIT_REFSPEC= CI_PREV_COMMIT_SHA=8826c98181353075bbeee8f99b400496488e3523 CI_PREV_COMMIT_URL=http://1.2.3.4:3000/test/woodpecker-test/commit/8826c98181353075bbeee8f99b400496488e3523 +CI_PREV_COMMIT_SOURCE_BRANCH= +CI_PREV_COMMIT_TARGET_BRANCH= CI_PREV_PIPELINE_CREATED=1721086039 CI_PREV_PIPELINE_DEPLOY_TARGET= CI_PREV_PIPELINE_DEPLOY_TASK= diff --git a/pipeline/frontend/metadata/environment.go b/pipeline/frontend/metadata/environment.go index ccb4396b8..9b0cf1656 100644 --- a/pipeline/frontend/metadata/environment.go +++ b/pipeline/frontend/metadata/environment.go @@ -30,19 +30,26 @@ var ( maxChangedFiles = 500 ) -// Environ returns the metadata as a map of environment variables. -func (m *Metadata) Environ() map[string]string { +func getSourceTargetBranches(refspec string) (string, string) { var ( sourceBranch string targetBranch string ) - branchParts := strings.Split(m.Curr.Commit.Refspec, ":") + branchParts := strings.Split(refspec, ":") if len(branchParts) == 2 { //nolint:mnd sourceBranch = branchParts[0] targetBranch = branchParts[1] } + return sourceBranch, targetBranch +} + +// Environ returns the metadata as a map of environment variables. +func (m *Metadata) Environ() map[string]string { + sourceBranch, targetBranch := getSourceTargetBranches(m.Curr.Commit.Refspec) + prevSourceBranch, prevTargetBranch := getSourceTargetBranches(m.Prev.Commit.Refspec) + params := map[string]string{ "CI": m.Sys.Name, "CI_REPO": path.Join(m.Repo.Owner, m.Repo.Name), @@ -102,6 +109,8 @@ func (m *Metadata) Environ() map[string]string { "CI_PREV_COMMIT_AUTHOR": m.Prev.Commit.Author.Name, "CI_PREV_COMMIT_AUTHOR_EMAIL": m.Prev.Commit.Author.Email, "CI_PREV_COMMIT_AUTHOR_AVATAR": m.Prev.Commit.Author.Avatar, + "CI_PREV_COMMIT_SOURCE_BRANCH": prevSourceBranch, + "CI_PREV_COMMIT_TARGET_BRANCH": prevTargetBranch, "CI_PREV_PIPELINE_NUMBER": strconv.FormatInt(m.Prev.Number, 10), "CI_PREV_PIPELINE_PARENT": strconv.FormatInt(m.Prev.Parent, 10), diff --git a/server/pipeline/stepbuilder/metadata_test.go b/server/pipeline/stepbuilder/metadata_test.go index a0d451425..af8978e27 100644 --- a/server/pipeline/stepbuilder/metadata_test.go +++ b/server/pipeline/stepbuilder/metadata_test.go @@ -49,7 +49,7 @@ func TestMetadataFromStruct(t *testing.T) { "CI_COMMIT_TAG": "", "CI_COMMIT_TARGET_BRANCH": "", "CI_COMMIT_URL": "", "CI_FORGE_TYPE": "", "CI_FORGE_URL": "", "CI_PIPELINE_CREATED": "0", "CI_PIPELINE_DEPLOY_TARGET": "", "CI_PIPELINE_DEPLOY_TASK": "", "CI_PIPELINE_EVENT": "", "CI_PIPELINE_FINISHED": "0", "CI_PIPELINE_FILES": "[]", "CI_PIPELINE_NUMBER": "0", "CI_PIPELINE_PARENT": "0", "CI_PIPELINE_STARTED": "0", "CI_PIPELINE_STATUS": "", "CI_PIPELINE_URL": "/repos/0/pipeline/0", "CI_PIPELINE_FORGE_URL": "", - "CI_PREV_COMMIT_AUTHOR": "", "CI_PREV_COMMIT_AUTHOR_AVATAR": "", "CI_PREV_COMMIT_AUTHOR_EMAIL": "", "CI_PREV_COMMIT_BRANCH": "", + "CI_PREV_COMMIT_AUTHOR": "", "CI_PREV_COMMIT_AUTHOR_AVATAR": "", "CI_PREV_COMMIT_AUTHOR_EMAIL": "", "CI_PREV_COMMIT_BRANCH": "", "CI_PREV_COMMIT_SOURCE_BRANCH": "", "CI_PREV_COMMIT_TARGET_BRANCH": "", "CI_PREV_COMMIT_MESSAGE": "", "CI_PREV_COMMIT_REF": "", "CI_PREV_COMMIT_REFSPEC": "", "CI_PREV_COMMIT_SHA": "", "CI_PREV_COMMIT_URL": "", "CI_PREV_PIPELINE_CREATED": "0", "CI_PREV_PIPELINE_DEPLOY_TARGET": "", "CI_PREV_PIPELINE_DEPLOY_TASK": "", "CI_PREV_PIPELINE_EVENT": "", "CI_PREV_PIPELINE_FINISHED": "0", "CI_PREV_PIPELINE_NUMBER": "0", "CI_PREV_PIPELINE_PARENT": "0", "CI_PREV_PIPELINE_STARTED": "0", "CI_PREV_PIPELINE_STATUS": "", "CI_PREV_PIPELINE_URL": "/repos/0/pipeline/0", "CI_PREV_PIPELINE_FORGE_URL": "", "CI_REPO": "", "CI_REPO_CLONE_URL": "", "CI_REPO_CLONE_SSH_URL": "", "CI_REPO_DEFAULT_BRANCH": "", "CI_REPO_REMOTE_ID": "", @@ -84,7 +84,7 @@ func TestMetadataFromStruct(t *testing.T) { "CI_COMMIT_TAG": "", "CI_COMMIT_TARGET_BRANCH": "", "CI_COMMIT_URL": "", "CI_FORGE_TYPE": "gitea", "CI_FORGE_URL": "https://gitea.com", "CI_PIPELINE_CREATED": "0", "CI_PIPELINE_DEPLOY_TARGET": "", "CI_PIPELINE_DEPLOY_TASK": "", "CI_PIPELINE_EVENT": "", "CI_PIPELINE_FINISHED": "0", "CI_PIPELINE_FILES": `["test.go","markdown file.md"]`, "CI_PIPELINE_NUMBER": "3", "CI_PIPELINE_PARENT": "0", "CI_PIPELINE_STARTED": "0", "CI_PIPELINE_STATUS": "", "CI_PIPELINE_URL": "https://example.com/repos/0/pipeline/3", "CI_PIPELINE_FORGE_URL": "", - "CI_PREV_COMMIT_AUTHOR": "", "CI_PREV_COMMIT_AUTHOR_AVATAR": "", "CI_PREV_COMMIT_AUTHOR_EMAIL": "", "CI_PREV_COMMIT_BRANCH": "", + "CI_PREV_COMMIT_AUTHOR": "", "CI_PREV_COMMIT_AUTHOR_AVATAR": "", "CI_PREV_COMMIT_AUTHOR_EMAIL": "", "CI_PREV_COMMIT_BRANCH": "", "CI_PREV_COMMIT_SOURCE_BRANCH": "", "CI_PREV_COMMIT_TARGET_BRANCH": "", "CI_PREV_COMMIT_MESSAGE": "", "CI_PREV_COMMIT_REF": "", "CI_PREV_COMMIT_REFSPEC": "", "CI_PREV_COMMIT_SHA": "", "CI_PREV_COMMIT_URL": "", "CI_PREV_PIPELINE_CREATED": "0", "CI_PREV_PIPELINE_DEPLOY_TARGET": "", "CI_PREV_PIPELINE_DEPLOY_TASK": "", "CI_PREV_PIPELINE_EVENT": "", "CI_PREV_PIPELINE_FINISHED": "0", "CI_PREV_PIPELINE_NUMBER": "2", "CI_PREV_PIPELINE_PARENT": "0", "CI_PREV_PIPELINE_STARTED": "0", "CI_PREV_PIPELINE_STATUS": "", "CI_PREV_PIPELINE_URL": "https://example.com/repos/0/pipeline/2", "CI_PREV_PIPELINE_FORGE_URL": "", "CI_REPO": "testUser/testRepo", "CI_REPO_CLONE_URL": "https://gitea.com/testUser/testRepo.git", "CI_REPO_CLONE_SSH_URL": "git@gitea.com:testUser/testRepo.git",