Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuism committed Jul 11, 2020
2 parents 1697d3e + 4516a92 commit 4059286
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 47 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM scratch
EXPOSE 8080
COPY target/badge /badge
ENTRYPOINT [ "/badge", "-Djava.io.tmpdir=/", "-Dspring.profiles.active=production", "-XX:+PrintGC", "-XX:+VerboseGC", "--expert-options-all", "-Xmn32m", "-Xmx64m" ]
42 changes: 27 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
<build>
<finalName>badge</finalName>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.0.4</version>
<dependencies>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.0.6</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand All @@ -108,21 +120,6 @@
</resources>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>

<profiles>
<profile>
<id>native</id>
Expand Down Expand Up @@ -197,4 +194,19 @@
</profile>
</profiles>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>

</project>
11 changes: 10 additions & 1 deletion src/main/java/nl/appsource/badge/BadgeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import nl.appsource.badge.controller.ActuatorControllerImpl;
import nl.appsource.badge.controller.BadgeController;
import nl.appsource.badge.controller.BadgeControllerImpl;
import nl.appsource.badge.controller.BadgeStatus;
import nl.appsource.badge.expected.FixedImpl;
import nl.appsource.badge.expected.GitHubImpl;
import nl.appsource.badge.expected.GitLabImpl;
import nl.appsource.badge.expected.MyCache;
import nl.appsource.badge.expected.MyCacheImpl;
import nl.appsource.badge.model.actuator.Info;
import nl.appsource.badge.output.ShieldsIo;
import nl.appsource.badge.output.Svg;
import org.springframework.core.io.DefaultResourceLoader;
Expand Down Expand Up @@ -40,6 +44,8 @@ public class BadgeApplication {

private static final MediaType IMAGE_SVGXML = new MediaType("image", "svg+xml", UTF_8);

public static final MyCache<String, BadgeStatus> cache = new MyCacheImpl<>();

private static final Consumer<HttpHeaders> NOCACHE_HEADERS = (header) -> {
header.set(HttpHeaders.EXPIRES, "0");
header.setPragma("no-cache");
Expand Down Expand Up @@ -84,7 +90,8 @@ private static class RouterCall {
new RouterCall("/fixed/actuator/{latest}", APPLICATION_JSON, (r) -> badgeController.shieldsIoActuator(r.pathVariable("owner"), r.pathVariable("repo"), r.pathVariable("branch"), r.param("actuator_url").get())),
new RouterCall("/github/actuator/{owner}/{repo}/{branch}", APPLICATION_JSON, (r) -> badgeController.shieldsIoActuator(r.pathVariable("latest"), r.param("actuator_url").get())),
new RouterCall("/actuator/info", APPLICATION_JSON, (r) -> actuatorController.info()),
new RouterCall("/actuator/health", APPLICATION_JSON, (r) -> actuatorController.health())
new RouterCall("/actuator/health", APPLICATION_JSON, (r) -> actuatorController.health()),
new RouterCall("/actuator/cache", APPLICATION_JSON, (r) -> actuatorController.cache())
).forEach(rc -> {
router.GET(rc.pattern, (r) -> NOCACHES.get().contentType(rc.contentType).body(rc.handlerFunction.apply(r)));
router.HEAD(rc.pattern, (r) -> NOCACHES.get().contentType(rc.contentType).build());
Expand All @@ -103,6 +110,8 @@ private static class RouterCall {

public static void main(String[] args) throws IOException {

System.out.println("result: " + Info.Git.Commit.class.toString());

final ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
final Resource resource = new DefaultResourceLoader(defaultClassLoader).getResource("banner.txt");
final String banner = StreamUtils.copyToString(resource.getInputStream(), UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface ActuatorController {

String health();

String cache();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.appsource.badge.controller;

import nl.appsource.badge.BadgeApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

Expand All @@ -26,4 +27,9 @@ public String health() {
return "{\"status\": \"UP\"}";
}

@Override
public String cache() {
return "{\"size\": " + BadgeApplication.cache.size() + "}";
}

}
13 changes: 7 additions & 6 deletions src/main/java/nl/appsource/badge/expected/GitHubImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nl.appsource.badge.expected;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.appsource.badge.controller.BadgeException;
Expand Down Expand Up @@ -31,6 +30,7 @@

import static java.lang.Math.min;
import static java.util.stream.Collectors.joining;
import static nl.appsource.badge.BadgeApplication.cache;
import static nl.appsource.badge.controller.BadgeStatus.Status.ERROR;
import static nl.appsource.badge.controller.BadgeStatus.Status.LATEST;
import static nl.appsource.badge.controller.BadgeStatus.Status.OUTDATED;
Expand All @@ -55,9 +55,6 @@ public class GitHubImpl implements GitHub {

private final Environment environment;

@Getter
private final MyCache<String, BadgeStatus> cache = new MyCacheImpl<>();

private final BiFunction<HttpHeaders, String, String> safeHeaderPrint = (responseHeaders, key) ->
responseHeaders == null ? null :
key + "=" + Optional.ofNullable(responseHeaders.get(key))
Expand All @@ -71,7 +68,7 @@ public class GitHubImpl implements GitHub {

public BadgeStatus getBadgeStatus(final String owner, final String repo, final String branch, final String commit_sha) throws BadgeException {

final BadgeStatus cacheValue = getCache().getIfPresent(owner + "/" + repo + "/" + commit_sha);
final BadgeStatus cacheValue = cache.getIfPresent(getKey(owner, repo, branch, commit_sha));

if (cacheValue != null) {
return cacheValue;
Expand Down Expand Up @@ -127,7 +124,7 @@ private BadgeStatus callGitHub(final String owner, final String repo, final Stri
badgeStatus = new BadgeStatus(OUTDATED, commit_sha_short);
}

getCache().put(owner + "/" + repo + "/" + commit_sha, badgeStatus);
cache.put(getKey(owner, repo, branch, commit_sha), badgeStatus);
return badgeStatus;

} else {
Expand All @@ -149,6 +146,10 @@ private BadgeStatus callGitHub(final String owner, final String repo, final Stri

}

private String getKey(final String owner, final String repo, final String branch, final String commit_sha) {
return owner + "/" + repo + "/" + branch + "/" + commit_sha;
}

private String getToken() {

final String token = System.getenv("GITHUB_TOKEN");
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/nl/appsource/badge/expected/GitLabImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.util.Arrays.asList;
import static nl.appsource.badge.BadgeApplication.cache;
import static nl.appsource.badge.controller.BadgeStatus.Status.ERROR;
import static nl.appsource.badge.controller.BadgeStatus.Status.LATEST;
import static nl.appsource.badge.controller.BadgeStatus.Status.OUTDATED;
Expand All @@ -36,7 +37,16 @@ public class GitLabImpl implements GitLab {

@Override
public BadgeStatus getBadgeStatus(final String id, final String branch, final String commit_sha) throws BadgeException {
return callGitLab(id, branch, commit_sha);

final BadgeStatus cacheValue = cache.getIfPresent(getKey(id, branch, commit_sha));

if (cacheValue != null) {
return cacheValue;
} else {
return callGitLab(id, branch, commit_sha);
}


}

private BadgeStatus callGitLab(final String id, final String branch, final String commit_sha) throws BadgeException {
Expand All @@ -47,11 +57,9 @@ private BadgeStatus callGitLab(final String id, final String branch, final Strin

final HttpHeaders requestHeaders = new HttpHeaders();


final String token = getToken();

if (StringUtils.hasText(token)) {
log.info("Using token: " + token);
requestHeaders.add(AUTHORIZATION, "bearer " + token);
}

Expand Down Expand Up @@ -81,6 +89,7 @@ private BadgeStatus callGitLab(final String id, final String branch, final Strin
badgeStatus = new BadgeStatus(OUTDATED, commit_sha_short);
}

cache.put(getKey(id, branch, commit_sha), badgeStatus);
return badgeStatus;

} else {
Expand Down Expand Up @@ -111,6 +120,9 @@ private String getUrl() {

}

private String getKey(String id, String branch, String commit_sha) {
return id + "/" + branch + "/" + commit_sha;
}

private String getToken() {

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/nl/appsource/badge/expected/MyCache.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nl.appsource.badge.expected;

interface MyCache<K, V> {
public interface MyCache<K, V> {

V getIfPresent(K key);

void put(K key, V value);

int size();

}
45 changes: 29 additions & 16 deletions src/main/java/nl/appsource/badge/expected/MyCacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,49 @@

import lombok.extern.slf4j.Slf4j;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Slf4j
public class MyCacheImpl<K, V> implements MyCache<K, V> {

private final ConcurrentHashMap<K, V> _cache = new ConcurrentHashMap<>();

public MyCacheImpl() {
private final static long EXPIRED_IN_SECONDS = 15;

new Thread(() -> {
try {
while (true) {
Thread.sleep(60 * 1000);
_cache.clear();
}
} catch (final InterruptedException e) {
log.error("", e);
}
}).start();

}
private final ConcurrentHashMap<K, V> _cache = new ConcurrentHashMap<>();
private final ConcurrentHashMap<K, Long> _timestamp = new ConcurrentHashMap<>();

@Override
public V getIfPresent(final K key) {
synchronized (this) {

final Long expired = System.currentTimeMillis() + (EXPIRED_IN_SECONDS * 1000);

_timestamp.keySet().removeAll(
_timestamp
.entrySet()
.stream()
.filter(e -> e.getValue() < expired)
.map(Entry::getKey)
.map(_cache::remove)
.collect(Collectors.toSet()))
;
}
return _cache.get(key);
}

@Override
public void put(final K key, final V value) {
_cache.put(key, value);
synchronized (this) {
_cache.put(key, value);
_timestamp.put(key, System.currentTimeMillis());
}
}

@Override
public int size() {
synchronized (this) {
return _cache.size();
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/nl/appsource/badge/lib/Widths.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.appsource.badge.lib;

import java.util.ArrayList;
import java.util.Objects;

import static java.util.Arrays.asList;

Expand All @@ -15,7 +14,7 @@ public static int getWidthOfString(final String text) {
;
}

public static final ArrayList<Double> simpleWidths = new ArrayList<>(asList(
private static final ArrayList<Double> simpleWidths = new ArrayList<>(asList(
null
, null
, null
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nl/appsource/badge/model/actuator/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class Info {
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Git {
public static class Git {

private String branch;

@ToString
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Commit {
public static class Commit {

private String id;
private String time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GitHubResponse {
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Commit {
public static class Commit {

private String sha;
private String url;
Expand Down

0 comments on commit 4059286

Please sign in to comment.