From 842121982a80222ed07ba01bea59e893a0f48601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 10:25:10 +0100 Subject: [PATCH 1/9] feat: add isUpstreamPRTrigger step --- src/test/groovy/ApmBasePipelineTest.groovy | 1 + src/test/groovy/GitCheckoutStepTests.groovy | 15 +++ .../IsUpstreamPRTriggerStepTests.groovy | 109 ++++++++++++++++++ vars/README.md | 7 ++ vars/isUpstreamPRTrigger.groovy | 31 +++++ vars/isUpstreamPRTrigger.txt | 5 + 6 files changed, 168 insertions(+) create mode 100644 src/test/groovy/IsUpstreamPRTriggerStepTests.groovy create mode 100644 vars/isUpstreamPRTrigger.groovy create mode 100644 vars/isUpstreamPRTrigger.txt diff --git a/src/test/groovy/ApmBasePipelineTest.groovy b/src/test/groovy/ApmBasePipelineTest.groovy index 7bd4e9a8e..0385b1594 100644 --- a/src/test/groovy/ApmBasePipelineTest.groovy +++ b/src/test/groovy/ApmBasePipelineTest.groovy @@ -480,6 +480,7 @@ class ApmBasePipelineTest extends DeclarativePipelineTest { def script = loadScript('vars/isTag.groovy') return script.call() }) + helper.registerAllowedMethod('isUpstreamPRTrigger', { return false }) helper.registerAllowedMethod('isUpstreamTrigger', { return false }) helper.registerAllowedMethod('isUserTrigger', { return false }) helper.registerAllowedMethod('is32', { diff --git a/src/test/groovy/GitCheckoutStepTests.groovy b/src/test/groovy/GitCheckoutStepTests.groovy index ce1e9eb9f..4088df98f 100644 --- a/src/test/groovy/GitCheckoutStepTests.groovy +++ b/src/test/groovy/GitCheckoutStepTests.groovy @@ -266,6 +266,21 @@ class GitCheckoutStepTests extends ApmBasePipelineTest { assertJobStatusFailure() } + @Test + void testUpstreamPRTriggered() throws Exception { + helper.registerAllowedMethod('isUpstreamPRTrigger', {return true}) + script.scm = 'SCM' + try { + script.call(basedir: 'sub-folder', branch: 'master', + credentialsId: 'credentials-id') + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'branch=master, repo=null or credentialsId=credentials-id')) + assertJobStatusFailure() + } + @Test void testUpstreamTriggered() throws Exception { helper.registerAllowedMethod('isUpstreamTrigger', {return true}) diff --git a/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy b/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy new file mode 100644 index 000000000..79734d3a9 --- /dev/null +++ b/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy @@ -0,0 +1,109 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import net.sf.json.JSONNull +import org.junit.Before +import org.junit.Test +import static org.junit.Assert.assertTrue +import static org.junit.Assert.assertFalse + +class IsUpstreamPRTriggerStepTests extends ApmBasePipelineTest { + + @Override + @Before + void setUp() throws Exception { + super.setUp() + script = loadScript('vars/isUpstreamPRTrigger.groovy') + } + + @Test + void test_with_upstream_cause_from_a_branch() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/master" build number 5', + upstreamBuild: 5, + upstreamProject: 'apm-integration-tests/master', + upstreamUrl: 'job/apm-integration-tests/job/master/' + ] + ] + } + + def ret = script.call() + printCallStack() + assertFalse(ret) + assertFalse(assertMethodCallContainsPattern('log', 'isUpstreamPRTrigger: apm-integration-tests/PR-695')) + assertJobStatusSuccess() + } + + @Test + void test_with_upstream_cause_from_a_PR() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', + upstreamBuild: 5, + upstreamProject: 'apm-integration-tests/PR-695', + upstreamUrl: 'job/apm-integration-tests/job/PR-695/' + ] + ] + } + + def ret = script.call() + printCallStack() + assertTrue(ret) + assertTrue(assertMethodCallContainsPattern('log', 'isUpstreamPRTrigger: apm-integration-tests/PR-695')) + assertJobStatusSuccess() + } + + @Test + void test_with_trigger_cause() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.triggers.TimerTrigger$TimerTriggerCause', + shortDescription: 'Started by a timmer', + ] + ] + } + + def ret = script.call() + printCallStack() + assertFalse(ret) + assertJobStatusSuccess() + } + + @Test + void test_with_upstream_cause_without_upstreamProject() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', + upstreamBuild: 5 + ] + ] + } + + def ret = script.call() + printCallStack() + assertFalse(ret) + assertJobStatusSuccess() + } +} diff --git a/vars/README.md b/vars/README.md index 74aa670fc..1d7936be1 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1547,6 +1547,13 @@ Check it the build was triggered by a timer (scheduled job). def timmerTrigger = isTimerTrigger() ``` +## isUpstreamPRTrigger +Check if the build was triggered by an upstream job from a pull-request. + +``` +def upstreamPRTrigger = isUpstreamPRTrigger() +``` + ## isUpstreamTrigger Check if the build was triggered by an upstream job. diff --git a/vars/isUpstreamPRTrigger.groovy b/vars/isUpstreamPRTrigger.groovy new file mode 100644 index 000000000..bbcffc730 --- /dev/null +++ b/vars/isUpstreamPRTrigger.groovy @@ -0,0 +1,31 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +/** + Check it the build was triggered by an upstream job from a pull request. + + def upstreamPRTrigger = isUpstreamPRTrigger() +*/ +def call(){ + def buildCause = currentBuild.getBuildCauses()?.find{ it._class == 'hudson.model.Cause$UpstreamCause'} + if (buildCause?.upstreamProject?.toUpperCase()?.contains("PR-")) { + log(level: 'DEBUG', text: "isUpstreamPRTrigger: ${buildCause?.upstreamProject?.toString()}") + return true + } + + return false +} diff --git a/vars/isUpstreamPRTrigger.txt b/vars/isUpstreamPRTrigger.txt new file mode 100644 index 000000000..15166978f --- /dev/null +++ b/vars/isUpstreamPRTrigger.txt @@ -0,0 +1,5 @@ +Check if the build was triggered by an upstream job from a pull request. + +``` +def upstreamPRTrigger = isUpstreamPRTrigger() +``` From f1b3f5218f3d2ccd13c11371dd3cb4e0f88cf073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 12:40:11 +0100 Subject: [PATCH 2/9] Revert "feat: add isUpstreamPRTrigger step" This reverts commit 842121982a80222ed07ba01bea59e893a0f48601. --- src/test/groovy/ApmBasePipelineTest.groovy | 1 - src/test/groovy/GitCheckoutStepTests.groovy | 15 --- .../IsUpstreamPRTriggerStepTests.groovy | 109 ------------------ vars/README.md | 7 -- vars/isUpstreamPRTrigger.groovy | 31 ----- vars/isUpstreamPRTrigger.txt | 5 - 6 files changed, 168 deletions(-) delete mode 100644 src/test/groovy/IsUpstreamPRTriggerStepTests.groovy delete mode 100644 vars/isUpstreamPRTrigger.groovy delete mode 100644 vars/isUpstreamPRTrigger.txt diff --git a/src/test/groovy/ApmBasePipelineTest.groovy b/src/test/groovy/ApmBasePipelineTest.groovy index 0385b1594..7bd4e9a8e 100644 --- a/src/test/groovy/ApmBasePipelineTest.groovy +++ b/src/test/groovy/ApmBasePipelineTest.groovy @@ -480,7 +480,6 @@ class ApmBasePipelineTest extends DeclarativePipelineTest { def script = loadScript('vars/isTag.groovy') return script.call() }) - helper.registerAllowedMethod('isUpstreamPRTrigger', { return false }) helper.registerAllowedMethod('isUpstreamTrigger', { return false }) helper.registerAllowedMethod('isUserTrigger', { return false }) helper.registerAllowedMethod('is32', { diff --git a/src/test/groovy/GitCheckoutStepTests.groovy b/src/test/groovy/GitCheckoutStepTests.groovy index 4088df98f..ce1e9eb9f 100644 --- a/src/test/groovy/GitCheckoutStepTests.groovy +++ b/src/test/groovy/GitCheckoutStepTests.groovy @@ -266,21 +266,6 @@ class GitCheckoutStepTests extends ApmBasePipelineTest { assertJobStatusFailure() } - @Test - void testUpstreamPRTriggered() throws Exception { - helper.registerAllowedMethod('isUpstreamPRTrigger', {return true}) - script.scm = 'SCM' - try { - script.call(basedir: 'sub-folder', branch: 'master', - credentialsId: 'credentials-id') - } catch(e){ - //NOOP - } - printCallStack() - assertTrue(assertMethodCallContainsPattern('error', 'branch=master, repo=null or credentialsId=credentials-id')) - assertJobStatusFailure() - } - @Test void testUpstreamTriggered() throws Exception { helper.registerAllowedMethod('isUpstreamTrigger', {return true}) diff --git a/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy b/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy deleted file mode 100644 index 79734d3a9..000000000 --- a/src/test/groovy/IsUpstreamPRTriggerStepTests.groovy +++ /dev/null @@ -1,109 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -import net.sf.json.JSONNull -import org.junit.Before -import org.junit.Test -import static org.junit.Assert.assertTrue -import static org.junit.Assert.assertFalse - -class IsUpstreamPRTriggerStepTests extends ApmBasePipelineTest { - - @Override - @Before - void setUp() throws Exception { - super.setUp() - script = loadScript('vars/isUpstreamPRTrigger.groovy') - } - - @Test - void test_with_upstream_cause_from_a_branch() throws Exception { - binding.getVariable('currentBuild').getBuildCauses = { - return [ - [ - _class: 'hudson.model.Cause$UpstreamCause', - shortDescription: 'Started by upstream project "apm-integration-tests/master" build number 5', - upstreamBuild: 5, - upstreamProject: 'apm-integration-tests/master', - upstreamUrl: 'job/apm-integration-tests/job/master/' - ] - ] - } - - def ret = script.call() - printCallStack() - assertFalse(ret) - assertFalse(assertMethodCallContainsPattern('log', 'isUpstreamPRTrigger: apm-integration-tests/PR-695')) - assertJobStatusSuccess() - } - - @Test - void test_with_upstream_cause_from_a_PR() throws Exception { - binding.getVariable('currentBuild').getBuildCauses = { - return [ - [ - _class: 'hudson.model.Cause$UpstreamCause', - shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', - upstreamBuild: 5, - upstreamProject: 'apm-integration-tests/PR-695', - upstreamUrl: 'job/apm-integration-tests/job/PR-695/' - ] - ] - } - - def ret = script.call() - printCallStack() - assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('log', 'isUpstreamPRTrigger: apm-integration-tests/PR-695')) - assertJobStatusSuccess() - } - - @Test - void test_with_trigger_cause() throws Exception { - binding.getVariable('currentBuild').getBuildCauses = { - return [ - [ - _class: 'hudson.triggers.TimerTrigger$TimerTriggerCause', - shortDescription: 'Started by a timmer', - ] - ] - } - - def ret = script.call() - printCallStack() - assertFalse(ret) - assertJobStatusSuccess() - } - - @Test - void test_with_upstream_cause_without_upstreamProject() throws Exception { - binding.getVariable('currentBuild').getBuildCauses = { - return [ - [ - _class: 'hudson.model.Cause$UpstreamCause', - shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', - upstreamBuild: 5 - ] - ] - } - - def ret = script.call() - printCallStack() - assertFalse(ret) - assertJobStatusSuccess() - } -} diff --git a/vars/README.md b/vars/README.md index 1d7936be1..74aa670fc 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1547,13 +1547,6 @@ Check it the build was triggered by a timer (scheduled job). def timmerTrigger = isTimerTrigger() ``` -## isUpstreamPRTrigger -Check if the build was triggered by an upstream job from a pull-request. - -``` -def upstreamPRTrigger = isUpstreamPRTrigger() -``` - ## isUpstreamTrigger Check if the build was triggered by an upstream job. diff --git a/vars/isUpstreamPRTrigger.groovy b/vars/isUpstreamPRTrigger.groovy deleted file mode 100644 index bbcffc730..000000000 --- a/vars/isUpstreamPRTrigger.groovy +++ /dev/null @@ -1,31 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -/** - Check it the build was triggered by an upstream job from a pull request. - - def upstreamPRTrigger = isUpstreamPRTrigger() -*/ -def call(){ - def buildCause = currentBuild.getBuildCauses()?.find{ it._class == 'hudson.model.Cause$UpstreamCause'} - if (buildCause?.upstreamProject?.toUpperCase()?.contains("PR-")) { - log(level: 'DEBUG', text: "isUpstreamPRTrigger: ${buildCause?.upstreamProject?.toString()}") - return true - } - - return false -} diff --git a/vars/isUpstreamPRTrigger.txt b/vars/isUpstreamPRTrigger.txt deleted file mode 100644 index 15166978f..000000000 --- a/vars/isUpstreamPRTrigger.txt +++ /dev/null @@ -1,5 +0,0 @@ -Check if the build was triggered by an upstream job from a pull request. - -``` -def upstreamPRTrigger = isUpstreamPRTrigger() -``` From fda2c58c1cb8427b6de0e80b24daca12e0e17800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 12:59:41 +0100 Subject: [PATCH 3/9] chore: adapt isUpstreamTrigger to filter the usptream cause --- .../groovy/IsUpstreamTriggerStepTests.groovy | 60 ++++++++++++++++++- vars/README.md | 3 +- vars/isUpstreamTrigger.groovy | 14 ++++- vars/isUpstreamTrigger.txt | 3 +- 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/test/groovy/IsUpstreamTriggerStepTests.groovy b/src/test/groovy/IsUpstreamTriggerStepTests.groovy index 3d2bde8b0..cb9fd96bc 100644 --- a/src/test/groovy/IsUpstreamTriggerStepTests.groovy +++ b/src/test/groovy/IsUpstreamTriggerStepTests.groovy @@ -47,7 +47,28 @@ class IsUpstreamTriggerStepTests extends ApmBasePipelineTest { def ret = script.call() printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('log', 'isUpstreamTrigger: apm-integration-tests/PR-695')) + assertTrue(assertMethodCallContainsPattern('log', "isUpstreamTrigger: apm-integration-tests/PR-695, filter: 'all'")) + assertJobStatusSuccess() + } + + @Test + void test_with_upstream_cause_and_filter() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', + upstreamBuild: 5, + upstreamProject: 'apm-integration-tests/PR-695', + upstreamUrl: 'job/apm-integration-tests/job/PR-695/' + ] + ] + } + + def ret = script.call(filter: 'PR-') + printCallStack() + assertTrue(ret) + assertTrue(assertMethodCallContainsPattern('log', "isUpstreamTrigger: apm-integration-tests/PR-695, filter: 'PR-'")) assertJobStatusSuccess() } @@ -68,6 +89,24 @@ class IsUpstreamTriggerStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void test_with_trigger_cause_and_filter() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.triggers.TimerTrigger$TimerTriggerCause', + shortDescription: 'Started by a timmer', + ] + ] + } + + def ret = script.call(filter: 'PR-') + printCallStack() + assertFalse(ret) + assertFalse(assertMethodCallContainsPattern('log', "filter: 'PR-'")) + assertJobStatusSuccess() + } + @Test void test_with_upstream_cause_without_upstreamProject() throws Exception { binding.getVariable('currentBuild').getBuildCauses = { @@ -85,4 +124,23 @@ class IsUpstreamTriggerStepTests extends ApmBasePipelineTest { assertFalse(ret) assertJobStatusSuccess() } + + @Test + void test_with_upstream_cause_and_filter_without_upstreamProject() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', + upstreamBuild: 5 + ] + ] + } + + def ret = script.call(filter: 'PR-') + printCallStack() + assertFalse(ret) + assertFalse(assertMethodCallContainsPattern('log', "isUpstreamTrigger: apm-integration-tests/PR-695, filter: 'PR-'")) + assertJobStatusSuccess() + } } diff --git a/vars/README.md b/vars/README.md index 74aa670fc..a81ea317a 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1548,10 +1548,11 @@ def timmerTrigger = isTimerTrigger() ``` ## isUpstreamTrigger -Check if the build was triggered by an upstream job. +Check it the build was triggered by an upstream job, being it possible to add some filters. ``` def upstreamTrigger = isUpstreamTrigger() +def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') ``` ## isUserTrigger diff --git a/vars/isUpstreamTrigger.groovy b/vars/isUpstreamTrigger.groovy index 9023338b3..f164cba01 100644 --- a/vars/isUpstreamTrigger.groovy +++ b/vars/isUpstreamTrigger.groovy @@ -16,14 +16,22 @@ // under the License. /** - Check it the build was triggered by an upstream job. + Check it the build was triggered by an upstream job, being possible to add some filters. def upstreamTrigger = isUpstreamTrigger() + def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') */ -def call(){ +def call(Map args=[:]){ + def filter = args.get('filter', 'all') + def buildCause = currentBuild.getBuildCauses()?.find{ it._class == 'hudson.model.Cause$UpstreamCause'} if (buildCause?.upstreamProject?.trim()) { - log(level: 'DEBUG', text: "isUpstreamTrigger: ${buildCause?.upstreamProject?.toString()}") + log(level: 'DEBUG', text: "isUpstreamTrigger: ${buildCause?.upstreamProject?.toString()}, filter: '${filter}'") + // evaluate filter + if (filter != 'all' ) { + return buildCause?.upstreamProject?.toUpperCase()?.contains(filter) + } + return true } return false diff --git a/vars/isUpstreamTrigger.txt b/vars/isUpstreamTrigger.txt index 053eafa85..bd2fe5999 100644 --- a/vars/isUpstreamTrigger.txt +++ b/vars/isUpstreamTrigger.txt @@ -1,5 +1,6 @@ -Check if the build was triggered by an upstream job. +Check it the build was triggered by an upstream job, being it possible to add some filters. ``` def upstreamTrigger = isUpstreamTrigger() +def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') ``` From 4247bbdfbba724ddfbdd457b905748f118a385d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:00:58 +0100 Subject: [PATCH 4/9] fix: typo --- vars/README.md | 2 +- vars/isUpstreamTrigger.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/README.md b/vars/README.md index a81ea317a..26b148877 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1548,7 +1548,7 @@ def timmerTrigger = isTimerTrigger() ``` ## isUpstreamTrigger -Check it the build was triggered by an upstream job, being it possible to add some filters. +Check if the build was triggered by an upstream job, being it possible to add some filters. ``` def upstreamTrigger = isUpstreamTrigger() diff --git a/vars/isUpstreamTrigger.txt b/vars/isUpstreamTrigger.txt index bd2fe5999..8d74fb916 100644 --- a/vars/isUpstreamTrigger.txt +++ b/vars/isUpstreamTrigger.txt @@ -1,4 +1,4 @@ -Check it the build was triggered by an upstream job, being it possible to add some filters. +Check if the build was triggered by an upstream job, being it possible to add some filters. ``` def upstreamTrigger = isUpstreamTrigger() From b21f8d2eb0286225a0aacb1454b96d29d8c82fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:02:40 +0100 Subject: [PATCH 5/9] fix: more typos --- vars/README.md | 8 ++++---- vars/isBranchIndexTrigger.groovy | 2 +- vars/isBranchIndexTrigger.txt | 2 +- vars/isCommentTrigger.groovy | 2 +- vars/isCommentTrigger.txt | 2 +- vars/isTimerTrigger.groovy | 2 +- vars/isTimerTrigger.txt | 2 +- vars/isUpstreamTrigger.groovy | 2 +- vars/isUserTrigger.groovy | 2 +- vars/isUserTrigger.txt | 2 +- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/vars/README.md b/vars/README.md index 26b148877..07fde1a28 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1401,7 +1401,7 @@ Whether the build is based on a Branch or no ``` ## isBranchIndexTrigger -Check it the build was triggered by a Branch index. +Check if the build was triggered by a Branch index. ``` def branchIndexTrigger = isBranchIndexTrigger() @@ -1418,7 +1418,7 @@ def branchIndexTrigger = isBranchIndexTrigger() ``` ## isCommentTrigger -Check it the build was triggered by a comment in GitHub and the user is an Elastic user. +Check if the build was triggered by a comment in GitHub and the user is an Elastic user. it stores the comment owner username in the GITHUB_COMMENT_AUTHOR environment variable and the comment itself in the GITHUB_COMMENT environment variable. @@ -1541,7 +1541,7 @@ Whether the build is based on a Tag Request or no ``` ## isTimerTrigger -Check it the build was triggered by a timer (scheduled job). +Check if the build was triggered by a timer (scheduled job). ``` def timmerTrigger = isTimerTrigger() @@ -1556,7 +1556,7 @@ def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') ``` ## isUserTrigger -Check it the build was triggered by a user. +Check if the build was triggered by a user. it stores the username in the BUILD_CAUSE_USER environment variable. ``` diff --git a/vars/isBranchIndexTrigger.groovy b/vars/isBranchIndexTrigger.groovy index fa09ce1fd..5b28c9139 100644 --- a/vars/isBranchIndexTrigger.groovy +++ b/vars/isBranchIndexTrigger.groovy @@ -16,7 +16,7 @@ // under the License. /** - Check it the build was triggered by a Branch index. + Check if the build was triggered by a Branch index. def branchIndexTrigger = isBranchIndexTrigger() */ diff --git a/vars/isBranchIndexTrigger.txt b/vars/isBranchIndexTrigger.txt index fed4fe341..ef04c4a0c 100644 --- a/vars/isBranchIndexTrigger.txt +++ b/vars/isBranchIndexTrigger.txt @@ -1,4 +1,4 @@ -Check it the build was triggered by a Branch index. +Check if the build was triggered by a Branch index. ``` def branchIndexTrigger = isBranchIndexTrigger() diff --git a/vars/isCommentTrigger.groovy b/vars/isCommentTrigger.groovy index 94687904e..86e742700 100644 --- a/vars/isCommentTrigger.groovy +++ b/vars/isCommentTrigger.groovy @@ -17,7 +17,7 @@ import com.cloudbees.groovy.cps.NonCPS /** - Check it the build was triggered by a comment in GitHub. + Check if the build was triggered by a comment in GitHub. def commentTrigger = isCommentTrigger() */ diff --git a/vars/isCommentTrigger.txt b/vars/isCommentTrigger.txt index 6ff8b80bd..d507ca839 100644 --- a/vars/isCommentTrigger.txt +++ b/vars/isCommentTrigger.txt @@ -1,4 +1,4 @@ -Check it the build was triggered by a comment in GitHub and the user is an Elastic user. +Check if the build was triggered by a comment in GitHub and the user is an Elastic user. it stores the comment owner username in the GITHUB_COMMENT_AUTHOR environment variable and the comment itself in the GITHUB_COMMENT environment variable. diff --git a/vars/isTimerTrigger.groovy b/vars/isTimerTrigger.groovy index a8e7715f5..b00280248 100644 --- a/vars/isTimerTrigger.groovy +++ b/vars/isTimerTrigger.groovy @@ -16,7 +16,7 @@ // under the License. /** - Check it the build was triggered by a timer (scheduled job). + Check if the build was triggered by a timer (scheduled job). def timmerTrigger = isTimerTrigger() */ diff --git a/vars/isTimerTrigger.txt b/vars/isTimerTrigger.txt index d4aa6bdd8..d3264fda3 100644 --- a/vars/isTimerTrigger.txt +++ b/vars/isTimerTrigger.txt @@ -1,4 +1,4 @@ -Check it the build was triggered by a timer (scheduled job). +Check if the build was triggered by a timer (scheduled job). ``` def timmerTrigger = isTimerTrigger() diff --git a/vars/isUpstreamTrigger.groovy b/vars/isUpstreamTrigger.groovy index f164cba01..b5d8e7bb2 100644 --- a/vars/isUpstreamTrigger.groovy +++ b/vars/isUpstreamTrigger.groovy @@ -16,7 +16,7 @@ // under the License. /** - Check it the build was triggered by an upstream job, being possible to add some filters. + Check if the build was triggered by an upstream job, being possible to add some filters. def upstreamTrigger = isUpstreamTrigger() def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') diff --git a/vars/isUserTrigger.groovy b/vars/isUserTrigger.groovy index 2e93c195a..fc25c7af8 100644 --- a/vars/isUserTrigger.groovy +++ b/vars/isUserTrigger.groovy @@ -16,7 +16,7 @@ // under the License. /** - Check it the build was triggered by a user. + Check if the build was triggered by a user. def userTrigger = isUserTrigger() */ diff --git a/vars/isUserTrigger.txt b/vars/isUserTrigger.txt index c6027f136..9d40a780d 100644 --- a/vars/isUserTrigger.txt +++ b/vars/isUserTrigger.txt @@ -1,4 +1,4 @@ -Check it the build was triggered by a user. +Check if the build was triggered by a user. it stores the username in the BUILD_CAUSE_USER environment variable. ``` From eab57b02279596359b55bb8300dcd3df9f6bdc89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:12:06 +0100 Subject: [PATCH 6/9] chore: align comparison case Co-authored-by: Ivan Fernandez Calvo --- vars/isUpstreamTrigger.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars/isUpstreamTrigger.groovy b/vars/isUpstreamTrigger.groovy index b5d8e7bb2..59a6a3d9f 100644 --- a/vars/isUpstreamTrigger.groovy +++ b/vars/isUpstreamTrigger.groovy @@ -29,7 +29,7 @@ def call(Map args=[:]){ log(level: 'DEBUG', text: "isUpstreamTrigger: ${buildCause?.upstreamProject?.toString()}, filter: '${filter}'") // evaluate filter if (filter != 'all' ) { - return buildCause?.upstreamProject?.toUpperCase()?.contains(filter) + return buildCause?.upstreamProject?.toUpperCase()?.contains(filter.toUpperCase()) } return true From ba1a89d7b447faf03478b45d8c76d3c1ff29fa5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:29:31 +0100 Subject: [PATCH 7/9] chore: add a test for lowercase filters --- .../groovy/IsUpstreamTriggerStepTests.groovy | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/groovy/IsUpstreamTriggerStepTests.groovy b/src/test/groovy/IsUpstreamTriggerStepTests.groovy index cb9fd96bc..10daa7cf0 100644 --- a/src/test/groovy/IsUpstreamTriggerStepTests.groovy +++ b/src/test/groovy/IsUpstreamTriggerStepTests.groovy @@ -72,6 +72,27 @@ class IsUpstreamTriggerStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void test_with_upstream_cause_and_lowercase_filter() throws Exception { + binding.getVariable('currentBuild').getBuildCauses = { + return [ + [ + _class: 'hudson.model.Cause$UpstreamCause', + shortDescription: 'Started by upstream project "apm-integration-tests/PR-695" build number 5', + upstreamBuild: 5, + upstreamProject: 'apm-integration-tests/PR-695', + upstreamUrl: 'job/apm-integration-tests/job/PR-695/' + ] + ] + } + + def ret = script.call(filter: 'pr-') + printCallStack() + assertTrue(ret) + assertTrue(assertMethodCallContainsPattern('log', "isUpstreamTrigger: apm-integration-tests/PR-695, filter: 'pr-'")) + assertJobStatusSuccess() + } + @Test void test_with_trigger_cause() throws Exception { binding.getVariable('currentBuild').getBuildCauses = { From 8de2ba7f1fc7c2e2453b675555dd8c8e10fda2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:29:47 +0100 Subject: [PATCH 8/9] docs: document new param --- vars/isUpstreamTrigger.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vars/isUpstreamTrigger.txt b/vars/isUpstreamTrigger.txt index 8d74fb916..97f48d247 100644 --- a/vars/isUpstreamTrigger.txt +++ b/vars/isUpstreamTrigger.txt @@ -4,3 +4,5 @@ Check if the build was triggered by an upstream job, being it possible to add so def upstreamTrigger = isUpstreamTrigger() def upstreamTrigger = isUpstreamTrigger(filter: 'PR-') ``` + +* filter: The string filter to be used when selecting the ustream build cause. If no filter is set, then 'all' will be used. From dd564f64e3c195e122b86404859ad5f36ad0231f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 11 Mar 2021 13:30:41 +0100 Subject: [PATCH 9/9] docs: better description --- vars/README.md | 2 +- vars/isUpstreamTrigger.groovy | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/README.md b/vars/README.md index 64ebe20ec..ac28b2bdc 100644 --- a/vars/README.md +++ b/vars/README.md @@ -1563,7 +1563,7 @@ def timmerTrigger = isTimerTrigger() ``` ## isUpstreamTrigger -Check if the build was triggered by an upstream job, being it possible to add some filters. +Check if the build was triggered by an upstream job, being it possible to add a filter for the upstream cause. ``` def upstreamTrigger = isUpstreamTrigger() diff --git a/vars/isUpstreamTrigger.groovy b/vars/isUpstreamTrigger.groovy index 59a6a3d9f..9766da62f 100644 --- a/vars/isUpstreamTrigger.groovy +++ b/vars/isUpstreamTrigger.groovy @@ -16,7 +16,7 @@ // under the License. /** - Check if the build was triggered by an upstream job, being possible to add some filters. + Check if the build was triggered by an upstream job, being possible to add a filter for the upstream cause. def upstreamTrigger = isUpstreamTrigger() def upstreamTrigger = isUpstreamTrigger(filter: 'PR-')