mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
Merge pull request #151 from marcusholl/pr/extractLogLines
Pr/extract log lines
This commit is contained in:
commit
96f09cc221
@ -15,3 +15,17 @@ String getGitCommitIdOrNull() {
|
|||||||
String getGitCommitId() {
|
String getGitCommitId() {
|
||||||
return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
|
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}/ }
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -10,9 +10,14 @@ import util.JenkinsShellCallRule
|
|||||||
import util.Rules
|
import util.Rules
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals
|
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.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.assertNull
|
||||||
|
import static org.junit.Assert.assertThat
|
||||||
|
|
||||||
class GitUtilsTest extends BasePiperTest {
|
class GitUtilsTest extends BasePiperTest {
|
||||||
|
|
||||||
@ -52,4 +57,38 @@ class GitUtilsTest extends BasePiperTest {
|
|||||||
assertNull(gitUtils.getGitCommitIdOrNull())
|
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)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user