Skip to content

Commit 242263d

Browse files
committed
Update GitProperties date/time coercion to deal with more patterns
Update `GitProperties` to deal with updated date/time patterns that are now used by the `git-commit-id` maven plugin. Previous patterns are still supported as a fallback. Fixes gh-41109
1 parent 9cd6dfd commit 242263d

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
import java.time.Instant;
2020
import java.time.format.DateTimeFormatter;
2121
import java.time.format.DateTimeParseException;
22+
import java.util.Objects;
2223
import java.util.Properties;
24+
import java.util.Set;
25+
import java.util.function.Function;
26+
import java.util.function.Predicate;
2327

2428
import org.springframework.aot.hint.RuntimeHints;
2529
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -35,6 +39,9 @@
3539
@ImportRuntimeHints(GitPropertiesRuntimeHints.class)
3640
public class GitProperties extends InfoProperties {
3741

42+
static final Set<Coercer> coercers = Set.of(Coercer.milliseconds(),
43+
Coercer.dateTimePattern("yyyy-MM-dd'T'HH:mm:ssXXX"), Coercer.dateTimePattern("yyyy-MM-dd'T'HH:mm:ssZ"));
44+
3845
public GitProperties(Properties entries) {
3946
super(processEntries(entries));
4047
}
@@ -97,46 +104,58 @@ private static Properties processEntries(Properties properties) {
97104
private static void coercePropertyToEpoch(Properties properties, String key) {
98105
String value = properties.getProperty(key);
99106
if (value != null) {
100-
properties.setProperty(key, coerceToEpoch(value));
107+
properties.setProperty(key,
108+
coercers.stream()
109+
.map((coercer) -> coercer.apply(value))
110+
.filter(Objects::nonNull)
111+
.findFirst()
112+
.orElse(value));
101113
}
102114
}
103115

104116
/**
105-
* Attempt to convert the specified value to epoch time. Git properties information
106-
* are known to be specified either as epoch time in seconds or using a specific date
107-
* format.
108-
* @param s the value to coerce to
109-
* @return the epoch time in milliseconds or the original value if it couldn't be
110-
* converted
117+
* {@link RuntimeHintsRegistrar} for git properties.
111118
*/
112-
private static String coerceToEpoch(String s) {
113-
Long epoch = parseEpochSecond(s);
114-
if (epoch != null) {
115-
return String.valueOf(epoch);
116-
}
117-
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
118-
try {
119-
return String.valueOf(format.parse(s, Instant::from).toEpochMilli());
120-
}
121-
catch (DateTimeParseException ex) {
122-
return s;
119+
static class GitPropertiesRuntimeHints implements RuntimeHintsRegistrar {
120+
121+
@Override
122+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
123+
hints.resources().registerPattern("git.properties");
123124
}
125+
124126
}
125127

126-
private static Long parseEpochSecond(String s) {
127-
try {
128-
return Long.parseLong(s) * 1000;
129-
}
130-
catch (NumberFormatException ex) {
131-
return null;
128+
/**
129+
* Coercer used to convert a source value to epoch time.
130+
*/
131+
private record Coercer(Function<String, Long> action, Predicate<RuntimeException> ignoredExceptions) {
132+
133+
/**
134+
* Attempt to convert the specified value to epoch time.
135+
* @param value the value to coerce to
136+
* @return the epoch time in milliseconds or {@code null}
137+
*/
138+
String apply(String value) {
139+
try {
140+
Long result = this.action.apply(value);
141+
return (result != null) ? String.valueOf(result) : null;
142+
}
143+
catch (RuntimeException ex) {
144+
if (this.ignoredExceptions.test(ex)) {
145+
return null;
146+
}
147+
throw ex;
148+
}
132149
}
133-
}
134150

135-
static class GitPropertiesRuntimeHints implements RuntimeHintsRegistrar {
151+
static Coercer milliseconds() {
152+
return new Coercer((value) -> Long.parseLong(value) * 1000, NumberFormatException.class::isInstance);
153+
}
136154

137-
@Override
138-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
139-
hints.resources().registerPattern("git.properties");
155+
static Coercer dateTimePattern(String pattern) {
156+
return new Coercer(
157+
(value) -> DateTimeFormatter.ofPattern(pattern).parse(value, Instant::from).toEpochMilli(),
158+
DateTimeParseException.class::isInstance);
140159
}
141160

142161
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/info/GitPropertiesTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,23 @@ void coerceEpochSecond() {
6060
}
6161

6262
@Test
63-
void coerceDateString() {
63+
void coerceLegacyDateString() {
6464
GitProperties properties = new GitProperties(
6565
createProperties("master", "abcdefg", null, "2016-03-04T14:36:33+0100"));
6666
assertThat(properties.getCommitTime()).isNotNull();
6767
assertThat(properties.get("commit.time")).isEqualTo("1457098593000");
6868
assertThat(properties.getCommitTime().toEpochMilli()).isEqualTo(1457098593000L);
6969
}
7070

71+
@Test
72+
void coerceDateString() {
73+
GitProperties properties = new GitProperties(
74+
createProperties("master", "abcdefg", null, "2016-03-04T14:36:33+01:00"));
75+
assertThat(properties.getCommitTime()).isNotNull();
76+
assertThat(properties.get("commit.time")).isEqualTo("1457098593000");
77+
assertThat(properties.getCommitTime().toEpochMilli()).isEqualTo(1457098593000L);
78+
}
79+
7180
@Test
7281
void coerceUnsupportedFormat() {
7382
GitProperties properties = new GitProperties(

0 commit comments

Comments
 (0)