Skip to content

Commit

Permalink
Fix forge URL in summary
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Apr 17, 2024
1 parent 62a008b commit 0fd4b17
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Fix NoSuchFileException when output parent directories are nonexistent [#342](https://github.com/nbbrd/java-console-properties/issues/342)
- Fix file ordering in MultiFileInput [#343](https://github.com/nbbrd/java-console-properties/issues/343)
- Fix forge URL in summary [#248](https://github.com/nbbrd/heylogs/issues/248)

## [0.8.0] - 2024-04-10

Expand Down
19 changes: 19 additions & 0 deletions heylogs-api/src/main/java/internal/heylogs/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import lombok.NonNull;
import nbbrd.design.DirectImpl;
import nbbrd.heylogs.spi.Forge;
import nbbrd.io.http.URLQueryBuilder;
import nbbrd.io.text.Parser;
import nbbrd.service.ServiceProvider;

import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;

@DirectImpl
@ServiceProvider
public final class GitHub implements Forge {
Expand All @@ -24,4 +29,18 @@ public final class GitHub implements Forge {
public boolean isCompareLink(@NonNull CharSequence text) {
return Parser.of(GitHubCompareLink::parse).parseValue(text).isPresent();
}

@Override
public @NonNull URL getBaseURL(@NonNull CharSequence text) {
GitHubCompareLink compareLink = GitHubCompareLink.parse(text);
try {
return URLQueryBuilder
.of(compareLink.getBase())
.path(compareLink.getOwner())
.path(compareLink.getRepo())
.build();
} catch (MalformedURLException ex) {
throw new UncheckedIOException(ex);
}
}
}
15 changes: 10 additions & 5 deletions heylogs-api/src/main/java/nbbrd/heylogs/Heylogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import nbbrd.heylogs.spi.*;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -142,7 +143,7 @@ public void formatResources(@NonNull String formatId, @NonNull Appendable append
.orElse(0L);

Optional<String> forgeURL = getLatestVersionURL(document);
Optional<String> forgeName = forgeURL.flatMap(this::getForgeName);
Forge forgeOrNull = forgeURL.flatMap(this::getForge).orElse(null);

return Summary
.builder()
Expand All @@ -151,13 +152,17 @@ public void formatResources(@NonNull String formatId, @NonNull Appendable append
.timeRange(releases.stream().map(Version::getDate).collect(toTimeRange()).orElse(TimeRange.ALL))
.compatibilities(getCompatibilities(releases))
.unreleasedChanges((int) unreleasedChanges)
.forgeName(forgeName.orElse(null))
.forgeURL(forgeURL.map(URLExtractor::urlOf).map(URLExtractor::baseOf).orElse(null))
.forgeName(forgeOrNull != null ? forgeOrNull.getForgeName() : null)
.forgeURL(forgeURL.map(x -> getBaseURL(forgeOrNull, x)).orElse(null))
.build();
}

private Optional<String> getForgeName(String url) {
return forges.stream().filter(forge -> forge.isCompareLink(url)).map(Forge::getForgeName).findFirst();
private URL getBaseURL(Forge forgeOrNull, CharSequence url) {
return forgeOrNull != null ? forgeOrNull.getBaseURL(url) : URLExtractor.baseOf(URLExtractor.urlOf(url));
}

private Optional<Forge> getForge(String url) {
return forges.stream().filter(forge -> forge.isCompareLink(url)).findFirst();
}

private static Optional<String> getLatestVersionURL(Node document) {
Expand Down
11 changes: 9 additions & 2 deletions heylogs-api/src/main/java/nbbrd/heylogs/spi/Forge.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
import nbbrd.service.ServiceDefinition;
import nbbrd.service.ServiceId;

import java.net.URL;

@ServiceDefinition(
quantifier = Quantifier.MULTIPLE
)
public interface Forge {

@ServiceId(pattern = ServiceId.KEBAB_CASE)
@NonNull String getForgeId();
@NonNull
String getForgeId();

@NonNull String getForgeName();
@NonNull
String getForgeName();

boolean isCompareLink(@NonNull CharSequence text);

@NonNull
URL getBaseURL(@NonNull CharSequence text);
}
13 changes: 13 additions & 0 deletions heylogs-api/src/test/java/internal/heylogs/github/GitHubTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package internal.heylogs.github;

import internal.heylogs.URLExtractor;
import nbbrd.heylogs.spi.Forge;
import nbbrd.heylogs.spi.ForgeLoader;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class GitHubTest {

Expand All @@ -26,4 +28,15 @@ void testIsCompareLink() {
assertThat(x.isCompareLink("")).isFalse();
assertThat(x.isCompareLink("https://github.com/nbbrd/heylogs/compare/v0.7.2...HEAD")).isTrue();
}

@Test
void testGetBaseURL() {
Forge x = new GitHub();

assertThatIllegalArgumentException()
.isThrownBy(() -> x.getBaseURL(""));

assertThat(x.getBaseURL("https://github.com/nbbrd/heylogs/compare/v0.7.2...HEAD"))
.isEqualTo(URLExtractor.urlOf("https://github.com/nbbrd/heylogs"));
}
}
4 changes: 2 additions & 2 deletions heylogs-api/src/test/java/nbbrd/heylogs/HeylogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void testScan() {
.compatibility("Semantic Versioning")
.unreleasedChanges(2)
.forgeName("GitHub")
.forgeURL(urlOf("https://github.com"))
.forgeURL(urlOf("https://github.com/olivierlacan/keep-a-changelog"))
.build()
);

Expand All @@ -105,7 +105,7 @@ void testScan() {
.timeRange(TimeRange.of(LocalDate.of(2019, 2, 15), LocalDate.of(2019, 2, 15)))
.unreleasedChanges(0)
.forgeName("GitHub")
.forgeURL(urlOf("https://github.com"))
.forgeURL(urlOf("https://github.com/olivierlacan/keep-a-changelog"))
.build()
);

Expand Down

0 comments on commit 0fd4b17

Please sign in to comment.