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

GitUtils: Extract lines matching a pattern from a set of commits

In preparation of SOLMAN features. Here we need to get a change
document ID out of the commit message.

Start and end commit can be provided as well as a log format
and a filter condition.
This commit is contained in:
Marcus Holl 2018-05-07 15:52:23 +02:00
parent 599e8dedbc
commit 3499d4843c
2 changed files with 54 additions and 1 deletions

View File

@ -15,3 +15,17 @@ String getGitCommitIdOrNull() {
String getGitCommitId() {
return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
}
String[] extractLogLines(String filter = '',
String from = 'origin/master',
String to = 'HEAD',
String format = '%b') {
sh ( returnStdout: true,
script: """#!/bin/bash
git log --pretty=format:${format} ${from}..${to}
"""
)?.split('\n')
?.findAll { line -> line ==~ /${filter}/ }
}

View File

@ -10,9 +10,14 @@ import util.JenkinsShellCallRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertFalse
import static org.hamcrest.Matchers.equalTo
import static org.junit.Assert.assertTrue
import static org.junit.Assert.assertFalse
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.notNullValue
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertThat
class GitUtilsTest extends BasePiperTest {
@ -52,4 +57,38 @@ class GitUtilsTest extends BasePiperTest {
assertNull(gitUtils.getGitCommitIdOrNull())
}
@Test
void testExtractLogLinesWithDefaults() {
gitUtils.extractLogLines()
assertTrue(jscr.shell
.stream()
.anyMatch( { it ->
it.contains('git log --pretty=format:%b origin/master..HEAD')}))
}
@Test
void testExtractLogLinesWithCustomValues() {
gitUtils.extractLogLines('myFilter', 'HEAD~5', 'HEAD~1', '%B')
assertTrue( jscr.shell
.stream()
.anyMatch( { it ->
it.contains('git log --pretty=format:%B HEAD~5..HEAD~1')}))
}
@Test
void testExtractLogLinesFilter() {
jscr.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
String[] log = gitUtils.extractLogLines('12.*')
assertThat(log, is(notNullValue()))
assertThat(log.size(),is(equalTo(1)))
assertThat(log[0], is(equalTo('123')))
}
@Test
void testExtractLogLinesFilterNoMatch() {
jscr.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
String[] log = gitUtils.extractLogLines('xyz')
assertNotNull(log)
assertThat(log.size(),is(equalTo(0)))
}
}