From 8d10347f55d1dd74b96281dfa7d57b0dba9facfb Mon Sep 17 00:00:00 2001 From: devatherock Date: Sat, 24 Jul 2021 12:19:30 -0500 Subject: [PATCH] fix: Changed date format used to parse dates in API responses --- CHANGELOG.md | 1 + README.md | 1 + .../artifactory/config/ArtifactoryProperties.java | 11 +++++++++++ .../artifactory/service/DockerBadgeService.java | 15 ++++++--------- .../controllers/VersionControllerSpec.groovy | 6 +++--- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a02695..a80ceca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Changed - [#40](https://github.com/devatherock/artifactory-badge/issues/40): To not treat `_uploads` subfolder as a docker tag +- [#98](https://github.com/devatherock/artifactory-badge/issues/98): Date format used to parse dates in API responses ### Removed - Custom environment variables with `LOGGING_LEVEL` prefix and updated documentation to use environment variables diff --git a/README.md b/README.md index 893a550..613b067 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ docker run --rm \ | ARTIFACTORY_URL | true | (None) | The JFrog artifactory URL that hosts the docker registry | | ARTIFACTORY_API_KEY | true | (None) | API key for interacting with artifactory's REST API | | ARTIFACTORY_EXCLUDED_FOLDERS | false | /_uploads | Subfolders to be not treated as docker tags | +| ARTIFACTORY_DATE_FORMAT | false | yyyy-MM-dd'T'HH:mm:ss.SSSXXX | Date format to parse dates in artifactory API responses | | ARTIFACTORY_BADGE_SHIELDS_IO_ENABLED | false | true | Indicates if shields.io should be used to generate the badge | | LOGGER_LEVELS_ROOT | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for all(framework and custom) code | | LOGGER_LEVELS_IO_GITHUB_DEVATHEROCK | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for custom code | diff --git a/src/main/java/io/github/devatherock/artifactory/config/ArtifactoryProperties.java b/src/main/java/io/github/devatherock/artifactory/config/ArtifactoryProperties.java index b4a6ba6..88c4b62 100644 --- a/src/main/java/io/github/devatherock/artifactory/config/ArtifactoryProperties.java +++ b/src/main/java/io/github/devatherock/artifactory/config/ArtifactoryProperties.java @@ -1,5 +1,6 @@ package io.github.devatherock.artifactory.config; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -36,12 +37,21 @@ public class ArtifactoryProperties { @NotBlank(message = "artifactory.api-key not specified") private String apiKey; + /** + * Date format to parse dates in artifactory API responses. Parses dates like + * {@code 2020-10-01T00:00:00.000Z} and {@code 2020-10-01T00:00:00.000-06:00} + */ + private String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + @Setter(AccessLevel.NONE) private String urlPrefix; @Setter(AccessLevel.NONE) private String storageUrlPrefix; + @Setter(AccessLevel.NONE) + private DateTimeFormatter dateParser; + /** * Subfolders to be not treated as docker tags */ @@ -51,5 +61,6 @@ public class ArtifactoryProperties { public void init() { urlPrefix = url + "/artifactory/"; storageUrlPrefix = urlPrefix + "api/storage/"; + dateParser = DateTimeFormatter.ofPattern(dateFormat); } } diff --git a/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java b/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java index 1dcbe59..c2f98c9 100644 --- a/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java +++ b/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java @@ -1,7 +1,6 @@ package io.github.devatherock.artifactory.service; import java.time.Instant; -import java.time.format.DateTimeFormatter; import java.util.regex.Pattern; import javax.inject.Singleton; @@ -47,11 +46,6 @@ public class DockerBadgeService { * Major version part of a semantic version */ private static final String VERSION_PART_MAJOR = "major"; - /** - * Formatter to parse dates like {@code 2020-10-01T00:00:00.000Z} - */ - private static final DateTimeFormatter MODIFIED_TIME_PARSER = DateTimeFormatter - .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); /** * Pattern to match versions like {@code 1}, {@code 1.2} and {@code 1.2.2} */ @@ -158,9 +152,12 @@ public String getLatestVersionBadge(String packageName, String badgeLabel, Strin ArtifactoryFolderInfo currentVersion = getArtifactoryFolderInfo(packageName + child.getUri()); if (null == latestVersion || (null != currentVersion - && Instant.from(MODIFIED_TIME_PARSER.parse(currentVersion.getLastModified())).compareTo( - Instant.from( - MODIFIED_TIME_PARSER.parse(latestVersion.getLastModified()))) > 0)) { + && Instant + .from(artifactoryConfig.getDateParser().parse(currentVersion.getLastModified())) + .compareTo( + Instant.from( + artifactoryConfig.getDateParser() + .parse(latestVersion.getLastModified()))) > 0)) { latestVersion = currentVersion; } } diff --git a/src/test/groovy/io/github/devatherock/artifactory/controllers/VersionControllerSpec.groovy b/src/test/groovy/io/github/devatherock/artifactory/controllers/VersionControllerSpec.groovy index f79d3b4..6e9e409 100644 --- a/src/test/groovy/io/github/devatherock/artifactory/controllers/VersionControllerSpec.groovy +++ b/src/test/groovy/io/github/devatherock/artifactory/controllers/VersionControllerSpec.groovy @@ -57,7 +57,7 @@ class VersionControllerSpec extends Specification { TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.0', '2020-10-08T00:00:00.000Z')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/1.1.2") .willReturn(WireMock.okJson( - TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000Z')))) + TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000-06:00')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/latest") .willReturn(WireMock.okJson( TestUtil.getFoldersResponse('/devatherock/simple-slack/latest', '2020-10-01T00:00:00.000Z')))) @@ -110,7 +110,7 @@ class VersionControllerSpec extends Specification { TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.0', '2020-10-08T00:00:00.000Z')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/1.1.2") .willReturn(WireMock.okJson( - TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000Z')))) + TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000-06:00')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/latest") .willReturn(WireMock.okJson( TestUtil.getFoldersResponse('/devatherock/simple-slack/latest', '2020-10-01T00:00:00.000Z')))) @@ -164,7 +164,7 @@ class VersionControllerSpec extends Specification { TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.0', '2020-10-08T00:00:00.000Z')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/1.1.2") .willReturn(WireMock.okJson( - TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000Z')))) + TestUtil.getFoldersResponse('/devatherock/simple-slack/1.1.2', '2020-10-15T00:00:00.000-06:00')))) WireMock.givenThat(WireMock.get("/artifactory/api/storage/${packageName}/latest") .willReturn(WireMock.okJson( TestUtil.getFoldersResponse('/devatherock/simple-slack/latest', '2020-10-01T00:00:00.000Z'))))