From 54b91db6b0dcec6a5a3ee7451a1c34686bfc5363 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 19 Jul 2024 17:15:05 +0200 Subject: [PATCH] Add PULLREQUEST_DRONE_PULL_REQUEST drone env (#3939) --- .../frontend/metadata/drone_compatibility.go | 11 ++ .../metadata/drone_compatibility_test.go | 130 +++++++++++++++++- 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/pipeline/frontend/metadata/drone_compatibility.go b/pipeline/frontend/metadata/drone_compatibility.go index 16dae443a8..dfa796f9bb 100644 --- a/pipeline/frontend/metadata/drone_compatibility.go +++ b/pipeline/frontend/metadata/drone_compatibility.go @@ -20,6 +20,7 @@ func SetDroneEnviron(env map[string]string) { // webhook copyEnv("CI_COMMIT_BRANCH", "DRONE_BRANCH", env) copyEnv("CI_COMMIT_PULL_REQUEST", "DRONE_PULL_REQUEST", env) + copyEnv("CI_COMMIT_PULL_REQUEST", "PULLREQUEST_DRONE_PULL_REQUEST", env) copyEnv("CI_COMMIT_TAG", "DRONE_TAG", env) copyEnv("CI_COMMIT_SOURCE_BRANCH", "DRONE_SOURCE_BRANCH", env) copyEnv("CI_COMMIT_TARGET_BRANCH", "DRONE_TARGET_BRANCH", env) @@ -58,6 +59,16 @@ func SetDroneEnviron(env map[string]string) { // misc copyEnv("CI_SYSTEM_HOST", "DRONE_SYSTEM_HOST", env) copyEnv("CI_STEP_NUMBER", "DRONE_STEP_NUMBER", env) + + // some quirks + + // Legacy env var to prevent the plugin from throwing an error + // when converting an empty string to a number + // + // plugins affected: "plugins/manifest" + if env["CI_COMMIT_PULL_REQUEST"] == "" { + env["PULLREQUEST_DRONE_PULL_REQUEST"] = "0" + } } func copyEnv(woodpecker, drone string, env map[string]string) { diff --git a/pipeline/frontend/metadata/drone_compatibility_test.go b/pipeline/frontend/metadata/drone_compatibility_test.go index c2334585c2..2e06fdabf8 100644 --- a/pipeline/frontend/metadata/drone_compatibility_test.go +++ b/pipeline/frontend/metadata/drone_compatibility_test.go @@ -23,13 +23,14 @@ import ( "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata" ) -func TestSetDroneEnviron(t *testing.T) { +func TestSetDroneEnvironOnPull(t *testing.T) { woodpeckerVars := `CI=woodpecker CI_COMMIT_AUTHOR=6543 CI_COMMIT_AUTHOR_AVATAR=https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173 CI_COMMIT_BRANCH=main CI_COMMIT_MESSAGE=fix testscript CI_COMMIT_PULL_REQUEST=9 +CI_COMMIT_PULL_REQUEST_LABELS=tests,bugfix CI_COMMIT_REF=refs/pull/9/head CI_COMMIT_REFSPEC=fix_fail-on-err:main CI_COMMIT_SHA=a778b069d9f5992786d2db9be493b43868cfce76 @@ -99,7 +100,130 @@ DRONE_REPO_OWNER=Epsilon_02 DRONE_REPO_SCM=git DRONE_SOURCE_BRANCH=fix_fail-on-err DRONE_SYSTEM_HOST=ci.codeberg.org -DRONE_TARGET_BRANCH=main` +DRONE_TARGET_BRANCH=main +PULLREQUEST_DRONE_PULL_REQUEST=9` + + env := convertListToEnvMap(t, woodpeckerVars) + metadata.SetDroneEnviron(env) + // filter only new added env vars + for k := range convertListToEnvMap(t, woodpeckerVars) { + delete(env, k) + } + assert.EqualValues(t, convertListToEnvMap(t, droneVars), env) +} + +func TestSetDroneEnvironOnPush(t *testing.T) { + woodpeckerVars := `CI_COMMIT_AUTHOR=test +CI_COMMIT_AUTHOR_AVATAR=http://1.2.3.4:3000/avatars/dd46a756faad4727fb679320751f6dea +CI_COMMIT_AUTHOR_EMAIL=test@noreply.localhost +CI_COMMIT_BRANCH=main +CI_COMMIT_MESSAGE=revert 9b2aed1392fc097ef7b027712977722fb004d463 +CI_COMMIT_PULL_REQUEST= +CI_COMMIT_PULL_REQUEST_LABELS= +CI_COMMIT_REF=refs/heads/main +CI_COMMIT_REFSPEC= +CI_COMMIT_SHA=8826c98181353075bbeee8f99b400496488e3523 +CI_COMMIT_SOURCE_BRANCH= +CI_COMMIT_TAG= +CI_COMMIT_TARGET_BRANCH= +CI_COMMIT_URL=http://1.2.3.4:3000/test/woodpecker-test/commit/8826c98181353075bbeee8f99b400496488e3523 +CI_FORGE_TYPE=gitea +CI_FORGE_URL=http://1.2.3.4:3000 +CI_MACHINE=hagalaz +CI_PIPELINE_CREATED=1721328737 +CI_PIPELINE_DEPLOY_TARGET= +CI_PIPELINE_DEPLOY_TASK= +CI_PIPELINE_EVENT=push +CI_PIPELINE_FILES=[".woodpecker.yaml"] +CI_PIPELINE_FINISHED=1721328738 +CI_PIPELINE_FORGE_URL=http://1.2.3.4:3000/test/woodpecker-test/commit/8826c98181353075bbeee8f99b400496488e3523 +CI_PIPELINE_NUMBER=24 +CI_PIPELINE_PARENT=23 +CI_PIPELINE_STARTED=1721328737 +CI_PIPELINE_STATUS=success +CI_PIPELINE_URL=http://1.2.3.4:8000/repos/2/pipeline/24 +CI_PREV_COMMIT_AUTHOR=test +CI_PREV_COMMIT_AUTHOR_AVATAR=http://1.2.3.4:3000/avatars/dd46a756faad4727fb679320751f6dea +CI_PREV_COMMIT_AUTHOR_EMAIL=test@noreply.localhost +CI_PREV_COMMIT_BRANCH=main +CI_PREV_COMMIT_MESSAGE=revert 9b2aed1392fc097ef7b027712977722fb004d463 +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_PIPELINE_CREATED=1721086039 +CI_PREV_PIPELINE_DEPLOY_TARGET= +CI_PREV_PIPELINE_DEPLOY_TASK= +CI_PREV_PIPELINE_EVENT=push +CI_PREV_PIPELINE_FINISHED=1721086056 +CI_PREV_PIPELINE_FORGE_URL=http://1.2.3.4:3000/test/woodpecker-test/commit/8826c98181353075bbeee8f99b400496488e3523 +CI_PREV_PIPELINE_NUMBER=23 +CI_PREV_PIPELINE_PARENT=0 +CI_PREV_PIPELINE_STARTED=1721086039 +CI_PREV_PIPELINE_STATUS=failure +CI_PREV_PIPELINE_URL=http://1.2.3.4:8000/repos/2/pipeline/23 +CI_REPO=test/woodpecker-test +CI_REPO_CLONE_SSH_URL=user@1.2.3.4:test/woodpecker-test.git +CI_REPO_CLONE_URL=http://1.2.3.4:3000/test/woodpecker-test.git +CI_REPO_DEFAULT_BRANCH=main +CI_REPO_NAME=woodpecker-test +CI_REPO_OWNER=test +CI_REPO_PRIVATE=false +CI_REPO_REMOTE_ID=4 +CI_REPO_SCM=git +CI_REPO_TRUSTED=false +CI_REPO_URL=http://1.2.3.4:3000/test/woodpecker-test +CI_STEP_FINISHED=1721328738 +CI_STEP_NAME= +CI_STEP_NUMBER=0 +CI_STEP_STARTED=1721328737 +CI_STEP_STATUS=success +CI_STEP_URL=http://1.2.3.4:8000/repos/2/pipeline/24 +CI_SYSTEM_HOST=1.2.3.4:8000 +CI_SYSTEM_NAME=woodpecker +CI_SYSTEM_PLATFORM=linux/amd64 +CI_SYSTEM_URL=http://1.2.3.4:8000 +CI_SYSTEM_VERSION=2.7.0 +CI_WORKFLOW_NAME=woodpecker +CI_WORKFLOW_NUMBER=1 +CI_WORKSPACE=/usr/local/src/1.2.3.4/test/woodpecker-test` + + droneVars := `DRONE_BRANCH=main +DRONE_BUILD_CREATED=1721328737 +DRONE_BUILD_EVENT=push +DRONE_BUILD_FINISHED=1721328738 +DRONE_BUILD_LINK=http://1.2.3.4:8000/repos/2/pipeline/24 +DRONE_BUILD_NUMBER=24 +DRONE_BUILD_PARENT=23 +DRONE_BUILD_STARTED=1721328737 +DRONE_BUILD_STATUS=success +DRONE_COMMIT=8826c98181353075bbeee8f99b400496488e3523 +DRONE_COMMIT_AUTHOR=test +DRONE_COMMIT_AUTHOR_AVATAR=http://1.2.3.4:3000/avatars/dd46a756faad4727fb679320751f6dea +DRONE_COMMIT_AUTHOR_EMAIL=test@noreply.localhost +DRONE_COMMIT_AUTHOR_NAME=test +DRONE_COMMIT_BEFORE=8826c98181353075bbeee8f99b400496488e3523 +DRONE_COMMIT_BRANCH=main +DRONE_COMMIT_LINK=http://1.2.3.4:3000/test/woodpecker-test/commit/8826c98181353075bbeee8f99b400496488e3523 +DRONE_COMMIT_MESSAGE=revert 9b2aed1392fc097ef7b027712977722fb004d463 +DRONE_COMMIT_REF=refs/heads/main +DRONE_COMMIT_SHA=8826c98181353075bbeee8f99b400496488e3523 +DRONE_GIT_HTTP_URL=http://1.2.3.4:3000/test/woodpecker-test.git +DRONE_PULL_REQUEST= +DRONE_REMOTE_URL=http://1.2.3.4:3000/test/woodpecker-test.git +DRONE_REPO=test/woodpecker-test +DRONE_REPO_BRANCH=main +DRONE_REPO_LINK=http://1.2.3.4:3000/test/woodpecker-test +DRONE_REPO_NAME=woodpecker-test +DRONE_REPO_OWNER=test +DRONE_REPO_PRIVATE=false +DRONE_REPO_SCM=git +DRONE_SOURCE_BRANCH= +DRONE_STEP_NUMBER=0 +DRONE_SYSTEM_HOST=1.2.3.4:8000 +DRONE_TAG= +DRONE_TARGET_BRANCH= +PULLREQUEST_DRONE_PULL_REQUEST=0` env := convertListToEnvMap(t, woodpeckerVars) metadata.SetDroneEnviron(env) @@ -114,7 +238,7 @@ func convertListToEnvMap(t *testing.T, list string) map[string]string { result := make(map[string]string) for _, s := range strings.Split(list, "\n") { before, after, _ := strings.Cut(strings.TrimSpace(s), "=") - if before == "" || after == "" { + if before == "" { t.Fatal("helper function got invalid test data") } result[before] = after