Skip to content

Commit 6ba675a

Browse files
authored
Make /version endpoint to return version in major.minor.patch format (#864)
1 parent 92aac8e commit 6ba675a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/main/java/org/prebid/server/handler/VersionHandler.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.prebid.server.util.ResourceUtil;
1515

1616
import java.io.IOException;
17+
import java.util.regex.Matcher;
18+
import java.util.regex.Pattern;
1719

1820
/**
1921
* Handles HTTP request for pbs project version.
@@ -22,7 +24,7 @@ public class VersionHandler implements Handler<RoutingContext> {
2224

2325
private static final Logger logger = LoggerFactory.getLogger(VersionHandler.class);
2426

25-
private static final String NOT_SET = "not-set";
27+
private static final String UNDEFINED = "undefined";
2628

2729
private final String revisionResponseBody;
2830

@@ -36,22 +38,29 @@ public static VersionHandler create(String revisionFilePath, JacksonMapper mappe
3638
revision = mapper.mapper().readValue(ResourceUtil.readFromClasspath(revisionFilePath), Revision.class);
3739
} catch (IllegalArgumentException | IOException e) {
3840
logger.error("Was not able to read revision file {0}. Reason: {1}", revisionFilePath, e.getMessage());
39-
revision = Revision.of(NOT_SET, NOT_SET);
41+
revision = Revision.EMPTY;
4042
}
4143
return new VersionHandler(createRevisionResponseBody(revision, mapper));
4244
}
4345

4446
private static String createRevisionResponseBody(Revision revision, JacksonMapper mapper) {
4547
try {
4648
return mapper.mapper().writeValueAsString(RevisionResponse.of(
47-
revision.commitHash != null ? revision.commitHash : NOT_SET,
48-
revision.pbsVersion != null ? revision.pbsVersion : NOT_SET));
49+
revision.commitHash != null ? revision.commitHash : UNDEFINED,
50+
revision.pbsVersion != null ? extractVersion(revision.pbsVersion) : UNDEFINED));
4951
} catch (JsonProcessingException e) {
5052
logger.error("/version Critical error when trying to marshal revision response: %s", e.getMessage());
5153
return null;
5254
}
5355
}
5456

57+
private static String extractVersion(String buildVersion) {
58+
final Pattern versionPattern = Pattern.compile("\\d+\\.\\d+\\.\\d");
59+
final Matcher versionMatcher = versionPattern.matcher(buildVersion);
60+
61+
return versionMatcher.lookingAt() ? versionMatcher.group() : null;
62+
}
63+
5564
/**
5665
* Responds with commit revision.
5766
*/
@@ -68,6 +77,8 @@ public void handle(RoutingContext context) {
6877
@Value
6978
private static class Revision {
7079

80+
private static final Revision EMPTY = Revision.of(null, null);
81+
7182
@JsonProperty("git.commit.id")
7283
String commitHash;
7384

src/test/java/org/prebid/server/handler/VersionHandlerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void shouldCreateRevisionWithNoSetVersionValueWhenFileWasNotFound() throw
3737
versionHandler.handle(routingContext);
3838

3939
// then
40-
verify(httpResponse).end(mapper.writeValueAsString(RevisionResponse.of("not-set", "not-set")));
40+
verify(httpResponse).end(mapper.writeValueAsString(RevisionResponse.of("undefined", "undefined")));
4141
}
4242

4343
@Test
@@ -50,7 +50,7 @@ public void handleShouldRespondWithNotSetWhenPropertyIsNotInFile() throws JsonPr
5050
versionHandler.handle(routingContext);
5151

5252
// then
53-
verify(httpResponse).end(mapper.writeValueAsString(RevisionResponse.of("not-set", "not-set")));
53+
verify(httpResponse).end(mapper.writeValueAsString(RevisionResponse.of("undefined", "undefined")));
5454
}
5555

5656
@Test
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"git.commit.id": "4df3f6192d7938ccdaac04df783c46c7e8847d08",
3-
"git.build.version": "1.41.0"
3+
"git.build.version": "1.41.0-SNAPSHOT"
44
}

0 commit comments

Comments
 (0)