Skip to content

Commit 2e159c6

Browse files
Merge pull request #28 from jenkinsci/pipeline_template-can-be-last-with-no-new-line
Allow pipeline_template at end of file w/o newline
2 parents 08d9f16 + c6110eb commit 2e159c6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/finder/ConfigurationValueFinder.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ public static String findFirstConfigurationValue(String configurationContents, S
3030
if (indexOfKey == -1) {
3131
return null;
3232
}
33-
int indexAfterKey = keyToFind.length() + indexOfKey;
34-
int firstNewLine = configurationContents.indexOf("\n", indexAfterKey);
35-
String delimiterAndValueString = configurationContents.substring(indexAfterKey, firstNewLine);
36-
String valueWithoutDelimitersQuotesAndTicks = removeDelimitersQuotesAndTicks(delimiterAndValueString);
33+
String delimiterAndValue = getDelimiterAndValue(configurationContents, keyToFind, indexOfKey);
34+
String valueWithoutDelimitersQuotesAndTicks = removeDelimitersQuotesAndTicks(delimiterAndValue);
3735
if (valueWithoutDelimitersQuotesAndTicks == null) {
3836
return null;
3937
}
4038
return valueWithoutDelimitersQuotesAndTicks.trim();
4139
}
4240

41+
private static String getDelimiterAndValue(String configurationContents, String keyToFind, int indexOfKey) {
42+
int indexAfterKey = keyToFind.length() + indexOfKey;
43+
int firstNewLine = configurationContents.indexOf("\n", indexAfterKey);
44+
if (firstNewLine == -1) {
45+
return configurationContents.substring(indexAfterKey);
46+
}
47+
return configurationContents.substring(indexAfterKey, firstNewLine);
48+
}
49+
4350
/**
4451
* Remove colon or equal delimiters as well as "s and 's from the {@code inputString}
4552
* @param inputString small string which potentially contains a colon or equals sign

src/test/java/org/jenkinsci/plugins/workflow/multibranch/template/finder/ConfigurationValueFinderTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ class ConfigurationValueFinderTest extends Specification {
1717
where:
1818
firstValue | configKey | configText
1919
null | null | "test"
20+
"value" | "nonewline" | "nonewline:value"
2021
null | "test1" | null
2122
null | "test1" | "keynotfound"
2223
"hi" | "bare" | "bare:hi\n test1:second\n test1:third"
2324
"hi" | "quotes" | '"quotes":"hi"\n test1:second\n test1:third'
25+
"third" | "last" | '"quotes":"hi"\n test1:second\n last:third'
2426
"hi" | "equals_space" | 'equals_space = "hi"\ntest1 = second\ntest2 = third'
27+
"third" | "last" | 'equals_space = "hi"\ntest1 = second\nlast = third'
2528
"hi" | "equals" | 'equals="hi"\ntest1=second\ntest2=third'
29+
"third" | "last" | 'equals="hi"\ntest1=second\nlast=third'
2630
"hi" | "ticks" | '\'ticks\':\'hi\'\n test1:second\n test1:third'
2731
"hi" | "spaces" | '\'spaces\' : \'hi\' \n test1:second\n test1:third'
2832

0 commit comments

Comments
 (0)