1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Cache change document id in common pipeline environment

This commit is contained in:
Marcus Holl 2018-11-06 14:36:40 +01:00
parent f757a0e1d4
commit 76be6f0b24
3 changed files with 159 additions and 19 deletions

View File

@ -43,26 +43,35 @@ public class StepHelpers {
if(changeDocumentId?.trim()) {
step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters."
} else {
step.echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." +
"Searching for pattern '${configuration.changeManagement.changeDocumentLabel}'. Searching with format '${configuration.changeManagement.git.format}'."
try {
changeDocumentId = cm.getChangeDocumentId(
configuration.changeManagement.git.from,
configuration.changeManagement.git.to,
configuration.changeManagement.changeDocumentLabel,
configuration.changeManagement.git.format
)
step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history"
} catch(ChangeManagementException ex) {
step.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}."
}
return changeDocumentId
}
changeDocumentId = step.commonPipelineEnvironment.getChangeDocumentId()
if(changeDocumentId?.trim()) {
step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from common pipeline environment."
return changeDocumentId
}
step.echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." +
"Searching for pattern '${configuration.changeManagement.changeDocumentLabel}'. Searching with format '${configuration.changeManagement.git.format}'."
try {
changeDocumentId = cm.getChangeDocumentId(
configuration.changeManagement.git.from,
configuration.changeManagement.git.to,
configuration.changeManagement.changeDocumentLabel,
configuration.changeManagement.git.format
)
step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history"
step.commonPipelineEnvironment.setChangeDocumentId(changeDocumentId)
} catch(ChangeManagementException ex) {
step.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}."
}
return changeDocumentId
}

View File

@ -0,0 +1,127 @@
package com.sap.piper.cm
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsLoggingRule
import util.Rules
class StepHelpersTest extends BasePiperTest {
// Configuration is not checked by the tests here.
// We simply assume it fits. It is the duty of the
// step related tests to ensure the configuration is valid.
def params = [changeManagement:
[git: [
from: 'HEAD~1',
to: 'HEAD',
format: '%b'
],
changeDocumentLabel: "ChangeDocument:"
]
]
private Map getChangeDocumentIdReceivedParameters = [:]
@Before
public void setup() {
getChangeDocumentIdReceivedParameters.clear()
}
JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
@Rule
public RuleChain rules = Rules.getCommonRules(this)
.around(jlr)
private ChangeManagement cm = new ChangeManagement(nullScript) {
String getChangeDocumentId(
String from,
String to,
String label,
String format
) {
getChangeDocumentIdReceivedParameters['from'] = from
getChangeDocumentIdReceivedParameters['to'] = to
getChangeDocumentIdReceivedParameters['label'] = label
getChangeDocumentIdReceivedParameters['format'] = format
return '001'
}
}
@Test
public void changeDocumentIdViaCommitHistoryTest() {
def changeDocumentId = StepHelpers.getChangeDocumentId(cm, nullScript, params)
assert changeDocumentId == '001'
assert getChangeDocumentIdReceivedParameters ==
[
from: 'HEAD~1',
to: 'HEAD',
label: 'ChangeDocument:',
format: '%b'
]
// We cache the value. Otherwise we have to retrieve it each time from the
// commit history.
assert nullScript.commonPipelineEnvironment.getChangeDocumentId() == '001'
}
@Test
public void changeDocumentIdViaCommonPipelineEnvironmentTest() {
nullScript.commonPipelineEnvironment.setChangeDocumentId('002')
def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params)
assert transportRequestId == '002'
// getChangeDocumentId gets not called on ChangeManagement util class
// in this case.
assert getChangeDocumentIdReceivedParameters == [:]
}
@Test
public void changeDocumentIdViaParametersTest() {
params << [changeDocumentId: '003']
def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params)
assert transportRequestId == '003'
// In case we get the change document id via parameters we do not cache it
// Caller knows the change document id anyway. So the caller can provide it with
// each call.
assert nullScript.commonPipelineEnvironment.getChangeDocumentId() == null
// getChangeDocumentId gets not called on ChangeManagement util class
// in this case.
assert getChangeDocumentIdReceivedParameters == [:]
}
@Test
public void changeDocumentIdNotProvidedTest() {
jlr.expect('[WARN] Cannot retrieve changeDocumentId from commit history')
def cm = new ChangeManagement(nullScript) {
String getChangeDocumentId(
String from,
String to,
String label,
String format
) {
throw new ChangeManagementException('Cannot retrieve change document ids')
}
}
def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params)
assert transportRequestId == null
}
}

View File

@ -30,6 +30,8 @@ class commonPipelineEnvironment implements Serializable {
String mtarFilePath
String changeDocumentId
def reset() {
appContainerProperties = [:]
artifactVersion = null
@ -49,6 +51,8 @@ class commonPipelineEnvironment implements Serializable {
influxCustomDataMap = [pipeline_data: [:], step_data: [:]]
mtarFilePath = null
changeDocumentId = null
}
def setAppContainerProperty(property, value) {