From d8d2249862e77614e17d40f9943b280aa286d8bc Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 29 Jun 2018 14:54:29 +0200 Subject: [PATCH 1/3] Support cmclientOpts for uploadFileToTransport --- src/com/sap/piper/cm/ChangeManagement.groovy | 5 +++-- test/groovy/TransportRequestUploadFileTest.groovy | 3 ++- vars/transportRequestUploadFile.groovy | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/com/sap/piper/cm/ChangeManagement.groovy b/src/com/sap/piper/cm/ChangeManagement.groovy index fe5b89979..75a92e115 100644 --- a/src/com/sap/piper/cm/ChangeManagement.groovy +++ b/src/com/sap/piper/cm/ChangeManagement.groovy @@ -90,13 +90,14 @@ public class ChangeManagement implements Serializable { } } - void uploadFileToTransportRequest(String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String username, String password) { + void uploadFileToTransportRequest(String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String username, String password, String cmclientOpts = '') { int rc = script.sh(returnStatus: true, script: getCMCommandLine(endpoint, username, password, 'upload-file-to-transport', ['-cID', changeId, '-tID', transportRequestId, - applicationId, filePath])) + applicationId, filePath], + cmclientOpts)) if(rc == 0) { return diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index cfaf6b60f..54250feb6 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -103,7 +103,8 @@ public class TransportRequestUploadFileTest extends BasePiperTest { String filePath, String endpoint, String username, - String password) { + String password, + String cmclientOpts) { throw new ChangeManagementException('Exception message') } } diff --git a/vars/transportRequestUploadFile.groovy b/vars/transportRequestUploadFile.groovy index 8984af844..578e30f0d 100644 --- a/vars/transportRequestUploadFile.groovy +++ b/vars/transportRequestUploadFile.groovy @@ -12,6 +12,7 @@ import hudson.AbortException @Field Set parameterKeys = [ 'changeDocumentId', + 'cmClientOpts', 'transportRequestId', 'applicationId', 'filePath', @@ -21,6 +22,7 @@ import hudson.AbortException @Field Set generalConfigurationKeys = [ 'credentialsId', + 'cmClientOpts', 'endpoint' ] @@ -62,7 +64,7 @@ def call(parameters = [:]) { usernameVariable: 'username')]) { try { - cm.uploadFileToTransportRequest(changeDocumentId, transportRequestId, applicationId, filePath, endpoint, username, password) + cm.uploadFileToTransportRequest(changeDocumentId, transportRequestId, applicationId, filePath, endpoint, username, password, configuration.cmClientOpts) } catch(ChangeManagementException ex) { throw new AbortException(ex.getMessage()) } From a7ea15ac5851a6672158d57dfc3a088af868b376 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 10 Jul 2018 15:15:54 +0200 Subject: [PATCH 2/3] transportRequestUploadFile: retrieveChangeDocumentId from commit history --- .../docs/steps/transportRequestUploadFile.md | 8 ++++ .../TransportRequestUploadFileTest.groovy | 17 ++++++-- vars/transportRequestUploadFile.groovy | 41 +++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/documentation/docs/steps/transportRequestUploadFile.md b/documentation/docs/steps/transportRequestUploadFile.md index 2f49986b1..56087acbe 100644 --- a/documentation/docs/steps/transportRequestUploadFile.md +++ b/documentation/docs/steps/transportRequestUploadFile.md @@ -16,6 +16,10 @@ Uploads a file to a Transport Request for a Change Document on the Solution Mana | `filePath` | yes | | | | `credentialsId` | yes | | | | `endpoint` | yes | | | +| `gitFrom` | no | `origin/master` | | +| `gitTo` | no | `HEAD` | | +| `gitChangeDocumentLabel` | no | `ChangeDocument\s?:` | regex pattern | +| `gitFormat` | no | `%b` | see `git log --help` | * `script` - The common script environment of the Jenkinsfile running. Typically the reference to the script calling the pipeline step is provided with the `this` parameter, as in `script: this`. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving, for example, configuration parameters. * `changeDocumentId` - The id of the change document related to the transport request to release. @@ -24,6 +28,10 @@ Uploads a file to a Transport Request for a Change Document on the Solution Mana * `filePath` - The path of the file to upload. * `credentialsId` - The credentials to connect to the Solution Manager. * `endpoint` - The address of 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. +* `gitFormat` - Specifies what part of the commit is scanned. By default the body of the commit message is scanned. ## Step configuration The following parameters can also be specified as step parameters using the global configuration file: diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index cfaf6b60f..4430a0da1 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -60,10 +60,21 @@ public class TransportRequestUploadFileTest extends BasePiperTest { @Test public void changeDocumentIdNotProvidedTest() { - thrown.expect(AbortException) - thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId').") + ChangeManagement cm = new ChangeManagement(nullScript) { + String getChangeDocumentId( + String from, + String to, + String pattern, + String format + ) { + throw new ChangeManagementException('Cannot retrieve changeId from git commits.') + } + } - jsr.step.call(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path') + thrown.expect(AbortException) + thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' or via commit history).") + + jsr.step.call(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm) } @Test diff --git a/vars/transportRequestUploadFile.groovy b/vars/transportRequestUploadFile.groovy index 8984af844..123b7e8e2 100644 --- a/vars/transportRequestUploadFile.groovy +++ b/vars/transportRequestUploadFile.groovy @@ -16,12 +16,20 @@ import hudson.AbortException 'applicationId', 'filePath', 'credentialsId', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitChangeDocumentLabel', + 'gitFormat' ] @Field Set generalConfigurationKeys = [ 'credentialsId', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitChangeDocumentLabel', + 'gitFormat' ] def call(parameters = [:]) { @@ -37,7 +45,34 @@ def call(parameters = [:]) { generalConfigurationKeys) def changeDocumentId = configuration.changeDocumentId - if(!changeDocumentId) throw new AbortException("Change document id not provided (parameter: 'changeDocumentId').") + + if(changeDocumentId?.trim()) { + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." + + } else { + + echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." + + "Searching for pattern '${configuration.gitChangeDocumentLabel}'. Searching with format '${configuration.gitFormat}'." + + try { + changeDocumentId = cm.getChangeDocumentId( + configuration.gitFrom, + configuration.gitTo, + configuration.gitChangeDocumentLabel, + configuration.gitFormat + ) + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" + + } catch(ChangeManagementException ex) { + echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." + } + } + + if(! changeDocumentId?.trim()) { + throw new AbortException("Change document id not provided (parameter: 'changeDocumentId' or via commit history).") + } def transportRequestId = configuration.transportRequestId if(!transportRequestId) throw new AbortException("Transport Request id not provided (parameter: 'transportRequestId').") From cada0e78823b105c8d1422fb5bbccea7a5a578a8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 10 Jul 2018 14:43:15 +0200 Subject: [PATCH 3/3] transportRequestCreate: retrieve changeDocumentId from commit history --- .../docs/steps/transportRequestCreate.md | 8 ++++ test/groovy/TransportRequestCreateTest.groovy | 17 ++++++-- vars/transportRequestCreate.groovy | 40 +++++++++++++++++-- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/documentation/docs/steps/transportRequestCreate.md b/documentation/docs/steps/transportRequestCreate.md index 433e3916d..321ad7fc3 100644 --- a/documentation/docs/steps/transportRequestCreate.md +++ b/documentation/docs/steps/transportRequestCreate.md @@ -14,12 +14,20 @@ Creates a Transport Request for a Change Document on the Solution Manager. | `credentialsId` | yes | | | | `endpoint` | yes | | | | `clientOpts` | no | | | +| `gitFrom` | no | `origin/master` | | +| `gitTo` | no | `HEAD` | | +| `gitChangeDocumentLabel` | no | `ChangeDocument\s?:` | regex pattern | +| `gitFormat` | no | `%b` | see `git log --help` | * `script` - The common script environment of the Jenkinsfile running. Typically the reference to the script calling the pipeline step is provided with the `this` parameter, as in `script: this`. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving, for example, configuration parameters. * `changeDocumentId` - The id of the change document to transport. * `credentialsId` - The credentials to connect to the Solution Manager. * `endpoint` - The address of the Solution Manager. * `clientOpts`- Options forwarded to JVM used by the CM client, like `JAVA_OPTS` +* `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. +* `gitFormat` - Specifies what part of the commit is scanned. By default the body of the commit message is scanned. ## Step configuration The following parameters can also be specified as step parameters using the global configuration file: diff --git a/test/groovy/TransportRequestCreateTest.groovy b/test/groovy/TransportRequestCreateTest.groovy index be6750d71..1836754d3 100644 --- a/test/groovy/TransportRequestCreateTest.groovy +++ b/test/groovy/TransportRequestCreateTest.groovy @@ -61,10 +61,21 @@ public class TransportRequestCreateTest extends BasePiperTest { @Test public void changeIdNotProvidedTest() { - thrown.expect(AbortException) - thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId').") + ChangeManagement cm = new ChangeManagement(nullScript) { + String getChangeDocumentId( + String from, + String to, + String label, + String format + ) { + throw new ChangeManagementException('Cannot retrieve changeId from git commits.') + } + } - jsr.step.call(script: nullScript, developmentSystemId: '001') + thrown.expect(AbortException) + thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' or via commit history).") + + jsr.step.call(script: nullScript, developmentSystemId: '001', cmUtils: cm) } @Test diff --git a/vars/transportRequestCreate.groovy b/vars/transportRequestCreate.groovy index f737d9d37..89992d832 100644 --- a/vars/transportRequestCreate.groovy +++ b/vars/transportRequestCreate.groovy @@ -15,13 +15,21 @@ import hudson.AbortException 'clientOpts', 'developmentSystemId', 'credentialsId', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitChangeDocumentLabel', + 'gitFormat' ] @Field Set stepConfigurationKeys = [ 'credentialsId', 'clientOpts', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitChangeDocumentLabel', + 'gitFormat' ] def call(parameters = [:]) { @@ -37,7 +45,33 @@ def call(parameters = [:]) { stepConfigurationKeys) def changeDocumentId = configuration.changeDocumentId - if(!changeDocumentId) throw new AbortException('Change document id not provided (parameter: \'changeDocumentId\').') + + if(changeDocumentId?.trim()) { + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." + + } else { + + echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." + + "Searching for pattern '${configuration.gitChangeDocumentLabel}'. Searching with format '${configuration.gitFormat}'." + + try { + changeDocumentId = cm.getChangeDocumentId( + configuration.gitFrom, + configuration.gitTo, + configuration.gitChangeDocumentLabel, + configuration.gitFormat + ) + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" + } catch(ChangeManagementException ex) { + echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." + } + } + + if(! changeDocumentId?.trim()) { + throw new AbortException("Change document id not provided (parameter: \'changeDocumentId\' or via commit history).") + } def developmentSystemId = configuration.developmentSystemId if(!developmentSystemId) throw new AbortException('Development system id not provided (parameter: \'developmentSystemId\').')