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

Read transportId from commit history: split getChangeID method

This commit is contained in:
Marcus Holl 2018-07-03 09:44:41 +02:00
parent 75a9a76205
commit 8d60a81ec5
2 changed files with 25 additions and 13 deletions

View File

@ -24,22 +24,34 @@ public class ChangeManagement implements Serializable {
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

@ -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()