Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion src/test/groovy/IsUpstreamTriggerStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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 = {
Expand All @@ -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()
}
}
11 changes: 6 additions & 5 deletions vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.

Expand Down Expand Up @@ -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.

```
Expand Down
2 changes: 1 addition & 1 deletion vars/isBranchIndexTrigger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down
2 changes: 1 addition & 1 deletion vars/isBranchIndexTrigger.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion vars/isCommentTrigger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down
2 changes: 1 addition & 1 deletion vars/isCommentTrigger.txt
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion vars/isTimerTrigger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down
2 changes: 1 addition & 1 deletion vars/isTimerTrigger.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
14 changes: 11 additions & 3 deletions vars/isUpstreamTrigger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Comment thread
v1v marked this conversation as resolved.
}

return true
}
return false
Expand Down
5 changes: 4 additions & 1 deletion vars/isUpstreamTrigger.txt
Original file line number Diff line number Diff line change
@@ -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-')
Comment thread
mdelapenya marked this conversation as resolved.
```

* filter: The string filter to be used when selecting the ustream build cause. If no filter is set, then 'all' will be used.
2 changes: 1 addition & 1 deletion vars/isUserTrigger.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down
2 changes: 1 addition & 1 deletion vars/isUserTrigger.txt
Original file line number Diff line number Diff line change
@@ -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.

```
Expand Down