diff --git a/src/test/groovy/IsUpstreamTriggerStepTests.groovy b/src/test/groovy/IsUpstreamTriggerStepTests.groovy index 3d2bde8b0..10daa7cf0 100644 --- a/src/test/groovy/IsUpstreamTriggerStepTests.groovy +++ b/src/test/groovy/IsUpstreamTriggerStepTests.groovy @@ -47,7 +47,49 @@ 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() + } + + @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() } @@ -68,6 +110,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 +145,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 6932f6f8c..ac28b2bdc 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. @@ -1556,21 +1556,22 @@ 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() ``` ## isUpstreamTrigger -Check if the build was triggered by an upstream job. +Check if the build was triggered by an upstream job, being it possible to add a filter for the upstream cause. ``` def upstreamTrigger = isUpstreamTrigger() +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 9023338b3..9766da62f 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 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-') */ -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.toUpperCase()) + } + return true } return false diff --git a/vars/isUpstreamTrigger.txt b/vars/isUpstreamTrigger.txt index 053eafa85..97f48d247 100644 --- a/vars/isUpstreamTrigger.txt +++ b/vars/isUpstreamTrigger.txt @@ -1,5 +1,8 @@ -Check if the build was triggered by an upstream job. +Check if the build was triggered by an upstream job, being it possible to add some filters. ``` 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. 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. ```