1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

Merge pull request #206 from marcusholl/pr/transportRequestReleaseTransportRequestIdFromCommitHistory

Pr/transport request release transport request id from commit history
This commit is contained in:
Marcus Holl 2018-07-18 12:47:54 +02:00 committed by GitHub
commit 3bd07e40f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 24 deletions

View File

@ -14,6 +14,7 @@ Releases a Transport Request for a Change Document on the Solution Manager.
| `transportRequestId`| yes | | |
| `credentialsId` | yes | | |
| `endpoint` | yes | | |
| `gitTransportRequestLabel` | no | `TransportRequest\s?:` | regex pattern |
| `gitFrom` | no | `origin/master` | |
| `gitTo` | no | `HEAD` | |
| `gitChangeDocumentLabel` | no | `ChangeDocument\s?:` | regex pattern |
@ -27,6 +28,7 @@ Releases a Transport Request for a Change Document on the Solution Manager.
* `gitFrom` - The starting point for retrieving the change document id
* `gitTo` - The end point for retrieving the change document id
* `gitChangeDocumentLabel` - A pattern used for identifying lines holding the change document id.
* `gitTransportReqeustLabel` - A pattern used for identifying lines holding the transport request id.
* `gitFormat` - Specifies what part of the commit is scanned. By default the body of the commit message is scanned.
## Step configuration

View File

@ -197,4 +197,5 @@ steps:
gitFrom: 'origin/master'
gitTo: 'HEAD'
gitChangeDocumentLabel: 'ChangeDocument\s?:'
gitTransportRequestLabel: 'TransportRequest\s?:'
gitFormat: '%b'

View File

@ -24,22 +24,44 @@ public class ChangeManagement implements Serializable {
String format = '%b'
) {
return getLabeledItem('ChangeDocumentId', from, to, label, format)
}
String getTransportRequestId(
String from = 'origin/master',
String to = 'HEAD',
String label = 'TransportRequest\\s?:',
String format = '%b'
) {
return getLabeledItem('ChangeDocumentId', from, to, label, format)
}
private String getLabeledItem(
String name,
String from,
String to,
String label,
String format
) {
if( ! gitUtils.insideWorkTree() ) {
throw new ChangeManagementException('Cannot retrieve change document id. Not in a git work tree. Change document id is extracted from git commit messages.')
throw new ChangeManagementException("Cannot retrieve ${name}. Not in a git work tree. ${name} is extracted from git commit messages.")
}
def changeIds = gitUtils.extractLogLines(".*${label}.*", from, to, format)
def items = gitUtils.extractLogLines(".*${label}.*", from, to, format)
.collect { line -> line?.replaceAll(label,'')?.trim() }
.unique()
changeIds.retainAll { line -> line != null && ! line.isEmpty() }
if( changeIds.size() == 0 ) {
throw new ChangeManagementException("Cannot retrieve changeId from git commits. Change id retrieved from git commit messages via pattern '${label}'.")
} else if (changeIds.size() > 1) {
throw new ChangeManagementException("Multiple ChangeIds found: ${changeIds}. Change id retrieved from git commit messages via pattern '${label}'.")
items.retainAll { line -> line != null && ! line.isEmpty() }
if( items.size() == 0 ) {
throw new ChangeManagementException("Cannot retrieve ${name} from git commits. ${name} retrieved from git commit messages via pattern '${label}'.")
} else if (items.size() > 1) {
throw new ChangeManagementException("Multiple ${name}s found: ${items}. ${name} retrieved from git commit messages via pattern '${label}'.")
}
return changeIds.get(0)
return items[0]
}
boolean isChangeInDevelopment(String changeId, String endpoint, String username, String password, String clientOpts = '') {

View File

@ -13,6 +13,7 @@ import util.JenkinsLoggingRule
import util.Rules
import hudson.AbortException
import hudson.scm.NullSCM
public class TransportRequestReleaseTest extends BasePiperTest {
@ -78,10 +79,19 @@ public class TransportRequestReleaseTest extends BasePiperTest {
@Test
public void transportRequestIdNotProvidedTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR transportRequestId")
ChangeManagement cm = new ChangeManagement(nullScript) {
String getTransportRequestId(String from,
String to,
String label,
String format) {
throw new ChangeManagementException('Cannot retrieve transportRequestId')
}
}
jsr.step.call(script: nullScript, changeDocumentId: '001')
thrown.expect(IllegalArgumentException)
thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' or via commit history).")
jsr.step.call(script: nullScript, changeDocumentId: '001', cmUtils: cm)
}
@Test

View File

@ -42,9 +42,9 @@ public class ChangeManagementTest extends BasePiperTest {
public void testRetrieveChangeDocumentIdOutsideGitWorkTreeTest() {
thrown.expect(ChangeManagementException)
thrown.expectMessage('Cannot retrieve change document id. ' +
thrown.expectMessage('Cannot retrieve ChangeDocumentId. ' +
'Not in a git work tree. ' +
'Change document id is extracted from git commit messages.')
'ChangeDocumentId is extracted from git commit messages.')
new ChangeManagement(nullScript, gitUtilsMock(false, new String[0])).getChangeDocumentId()
}
@ -53,7 +53,7 @@ public class ChangeManagementTest extends BasePiperTest {
public void testRetrieveChangeDocumentIdNothingFound() {
thrown.expect(ChangeManagementException)
thrown.expectMessage('Cannot retrieve changeId from git commits.')
thrown.expectMessage('Cannot retrieve ChangeDocumentId from git commits.')
new ChangeManagement(nullScript, gitUtilsMock(true, new String[0])).getChangeDocumentId()
}
@ -62,7 +62,7 @@ public class ChangeManagementTest extends BasePiperTest {
public void testRetrieveChangeDocumentIdReturnsArrayWithNullValue() {
thrown.expect(ChangeManagementException)
thrown.expectMessage('Cannot retrieve changeId from git commits.')
thrown.expectMessage('Cannot retrieve ChangeDocumentId from git commits.')
new ChangeManagement(nullScript, gitUtilsMock(true, (String[])[ null ])).getChangeDocumentId()
}
@ -71,7 +71,7 @@ public class ChangeManagementTest extends BasePiperTest {
public void testRetrieveChangeDocumentNotUnique() {
thrown.expect(ChangeManagementException)
thrown.expectMessage('Multiple ChangeIds found')
thrown.expectMessage('Multiple ChangeDocumentIds found')
String[] changeIds = [ 'a', 'b' ]
new ChangeManagement(nullScript, gitUtilsMock(true, changeIds)).getChangeDocumentId()

View File

@ -13,11 +13,13 @@ import hudson.AbortException
@Field Set stepConfigurationKeys = [
'credentialsId',
'cmClientOpts',
'endpoint',
'gitChangeDocumentLabel',
'gitFormat',
'gitFrom',
'gitTo'
'gitTo',
'gitTransportRequestLabel',
'gitFormat'
]
@Field Set parameterKeys = stepConfigurationKeys.plus([
@ -35,16 +37,42 @@ def call(parameters = [:]) {
ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script)
Map configuration = ConfigurationHelper
ConfigurationHelper configHelper = ConfigurationHelper
.loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, generalConfigurationKeys)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, stepConfigurationKeys)
.mixinStepConfig(script.commonPipelineEnvironment, stepConfigurationKeys)
.mixin(parameters, parameterKeys)
.withMandatoryProperty('changeDocumentId')
.withMandatoryProperty('transportRequestId')
.withMandatoryProperty('endpoint')
.use()
Map configuration = configHelper.use()
def transportRequestId = configuration.transportRequestId
if(transportRequestId?.trim()) {
echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters."
} else {
echo "[INFO] Retrieving transport request id from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." +
" Searching for pattern '${configuration.gitTransportRequestLabel}'. Searching with format '${configuration.gitFormat}'."
try {
transportRequestId = cm.getTransportRequestId(
configuration.gitFrom,
configuration.gitTo,
configuration.gitTransportRequestLabel,
configuration.gitFormat
)
echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history"
} catch(ChangeManagementException ex) {
echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}."
}
}
def changeDocumentId = configuration.changeDocumentId
@ -72,9 +100,14 @@ def call(parameters = [:]) {
}
}
if(!changeDocumentId) {
throw new AbortException("Change document id not provided (parameter: 'changeDocumentId' or via commit history).")
}
configuration = configHelper
.mixin([transportRequestId: transportRequestId?.trim() ?: null,
changeDocumentId: changeDocumentId?.trim() ?: null], ['transportRequestId', 'changeDocumentId'] as Set)
.withMandatoryProperty('transportRequestId',
"Transport request id not provided (parameter: \'transportRequestId\' or via commit history).")
.withMandatoryProperty('changeDocumentId',
"Change document id not provided (parameter: \'changeDocumentId\' or via commit history).")
.use()
echo "[INFO] Closing transport request '${configuration.transportRequestId}' for change document '${configuration.changeDocumentId}'."