Skip to content

Commit

Permalink
Merge pull request #102 from devatherock/98-date-format
Browse files Browse the repository at this point in the history
fix: Changed date format used to parse dates in API responses
  • Loading branch information
devatherock authored Jul 24, 2021
2 parents 67099bb + 8d10347 commit 0ef9394
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://shields.io">shields.io</a> 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 |
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -51,5 +61,6 @@ public class ArtifactoryProperties {
public void init() {
urlPrefix = url + "/artifactory/";
storageUrlPrefix = urlPrefix + "api/storage/";
dateParser = DateTimeFormatter.ofPattern(dateFormat);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'))))
Expand Down Expand Up @@ -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'))))
Expand Down Expand Up @@ -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'))))
Expand Down

0 comments on commit 0ef9394

Please sign in to comment.