Skip to content

Commit

Permalink
fix: use main-SNAPSHOT as best-effort fallback for jitpack (#1843)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen authored Nov 16, 2024
1 parent ada4ae8 commit 2c867b8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 47 deletions.
9 changes: 6 additions & 3 deletions docs/modules/ROOT/pages/dependencies.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ name of the module by appending `#name-of-module` to the URL.
And finally if the link you provide is to a specific branch of the project then you need to append
`#:SNAPSHOT` to the URL. (If you have both a branch and a module name then use `#name-of-module:SNAPSHOT`)

Note: if no branch specified `main-SNAPSHOT` will be used and if you list `main` or `master` they will both
be treated as if you want the snapshot version.

.Examples of links and their resulting locator:
|===
|Link | Locator
|https://github.com/jbangdev/jbang
|com.github.jbangdev:jbang:HEAD-SNAPSHOT
|com.github.jbangdev:jbang:main-SNAPSHOT

|https://github.com/jbangdev/jbang/tree/v1.2.3
|com.github.jbangdev:jbang:v1.2.3
Expand All @@ -137,13 +140,13 @@ And finally if the link you provide is to a specific branch of the project then
|com.github.jbangdev:jbang:f1f34b031d

|https://github.com/jbangdev/jbang#mymodule
|com.github.jbangdev.jbang:mymodule:HEAD-SNAPSHOT
|com.github.jbangdev.jbang:mymodule:main-SNAPSHOT

|https://github.com/jbangdev/jbang/tree/mybranch#:SNAPSHOT
|com.github.jbangdev:jbang:mybranch-SNAPSHOT

|https://github.com/jbangdev/jbang/tree/mybranch#mymodule:SNAPSHOT
|com.github.jbangdev.jbang.mymodule:mybranch-SNAPSHOT
|com.github.jbangdev.jbang: mymodule:mybranch-SNAPSHOT
|===

== Offline mode
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/jbang/dependencies/JitPackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ public static String ensureGAV(String ref) {
// Override GAV coords with values from the #part of the URL
Pgamv pgamv = coords.get().module(module);
if ((snapshot != null && pgamv.version != null) ||
(!hash.endsWith(":") && "master".equals(pgamv.version))) {
(!hash.endsWith(":") && ("master".equals(pgamv.version))
|| "main".equals(pgamv.version))) {
pgamv = pgamv.version(pgamv.version + "-SNAPSHOT");
}
ref = pgamv.toGav();
} else {
if ("master".equals(coords.get().version)) {
ref = coords.get().version("master" + "-SNAPSHOT").toGav();
} else if ("main".equals(coords.get().version)) {
ref = coords.get().version("main" + "-SNAPSHOT").toGav();
} else {
ref = coords.get().toGav();
}
Expand Down Expand Up @@ -110,7 +113,8 @@ Pgamv version(String version) {
String toGav() {
String v;
if (version == null) {
v = "-SNAPSHOT"; // using HEAD as no longer possible to know what default branch is called.
v = "main-SNAPSHOT"; // using HEAD as no longer possible to know what default branch is called.
// thus for now we default to assume 'main' as default.
} else if (POSSIBLE_SHA1_PATTERN.matcher(version).matches()) {
v = version.substring(0, 10);
} else {
Expand Down
109 changes: 67 additions & 42 deletions src/test/java/dev/jbang/dependencies/TestJitPack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jbang.dependencies;

import static dev.jbang.dependencies.JitPackUtil.ensureGAV;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
Expand All @@ -10,141 +11,165 @@ public class TestJitPack extends BaseTest {

@Test
void testExtractGithubUrlDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang"),
"com.github.jbangdev:jbang:-SNAPSHOT");
assertEquals(ensureGAV("https://github.com/jbangdev/jbang"),
"com.github.jbangdev:jbang:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/main"),
"com.github.jbangdev:jbang:main-SNAPSHOT");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master"),
"com.github.jbangdev:jbang:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master/foo"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master/foo"),
"com.github.jbangdev.jbang:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/v0.20.0"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/v0.20.0"),
"com.github.jbangdev:jbang:v0.20.0");

assertEquals(
JitPackUtil.ensureGAV(
ensureGAV(
"https://github.com/jbangdev/jbang/commit/90dfcbf354fc0838f08eea0680bf736b7c069b4e"),
"com.github.jbangdev:jbang:90dfcbf354");

}

@Test
void testDocumentedLocators() {
assertEquals(ensureGAV("https://github.com/jbangdev/jbang"),
"com.github.jbangdev:jbang:main-SNAPSHOT");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/v1.2.3"),
"com.github.jbangdev:jbang:v1.2.3");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/f1f34b031d2163e0cdc6f9a3725b59f47129c923"),
"com.github.jbangdev:jbang:f1f34b031d");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang#mymodule"),
"com.github.jbangdev.jbang:mymodule:main-SNAPSHOT");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/mybranch#:SNAPSHOT"),
"com.github.jbangdev:jbang:mybranch-SNAPSHOT");

assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/mybranch#mymodule:SNAPSHOT"),
"com.github.jbangdev.jbang:mymodule:mybranch-SNAPSHOT");
}

@Test
void testExtractGithubUrlWithHashDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang#foo"),
"com.github.jbangdev.jbang:foo:-SNAPSHOT");
assertEquals(ensureGAV("https://github.com/jbangdev/jbang#foo"),
"com.github.jbangdev.jbang:foo:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master#foo"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master#foo"),
"com.github.jbangdev.jbang:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master#:"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master#:"),
"com.github.jbangdev:jbang:master");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master#foo:"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master#foo:"),
"com.github.jbangdev.jbang:foo:master");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/master/foo#bar"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/master/foo#bar"),
"com.github.jbangdev.jbang:bar:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#foo:SNAPSHOT"),
assertEquals(ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#foo:SNAPSHOT"),
"com.github.jbangdev.jbang:foo:somebranch-SNAPSHOT");

assertEquals("com.github.jbangdev:jbang:somebranch",
JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch"));
ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch"));

assertEquals("com.github.jbangdev.jbang:artid:somebranch",
JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#artid"));
ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#artid"));

assertEquals("com.github.jbangdev:jbang:somebranch-SNAPSHOT",
JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#:SNAPSHOT"));
ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#:SNAPSHOT"));

assertEquals("com.github.jbangdev.jbang:artid:somebranch-SNAPSHOT",
JitPackUtil.ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#artid:SNAPSHOT"));
ensureGAV("https://github.com/jbangdev/jbang/tree/somebranch#artid:SNAPSHOT"));

assertEquals(
JitPackUtil.ensureGAV(
ensureGAV(
"https://github.com/jbangdev/jbang/commit/90dfcbf354fc0838f08eea0680bf736b7c069b4e#foo"),
"com.github.jbangdev.jbang:foo:90dfcbf354");

}

@Test
void testExtractGitlabUrlDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab"),
"com.gitlab.gitlab-org:gitlab:-SNAPSHOT");
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab"),
"com.gitlab.gitlab-org:gitlab:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master"),
"com.gitlab.gitlab-org:gitlab:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master/foo"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master/foo"),
"com.gitlab.gitlab-org.gitlab:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/v12.7.9-ee"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/v12.7.9-ee"),
"com.gitlab.gitlab-org:gitlab:v12.7.9-ee");

assertEquals(
JitPackUtil.ensureGAV(
ensureGAV(
"https://gitlab.com/gitlab-org/gitlab/-/commit/120262d85822e6a3d4e04f5c84d0075c60309d97"),
"com.gitlab.gitlab-org:gitlab:120262d858");

}

@Test
void testExtractGitlabUrlWithHashDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab#foo"),
"com.gitlab.gitlab-org.gitlab:foo:-SNAPSHOT");
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab#foo"),
"com.gitlab.gitlab-org.gitlab:foo:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master#foo"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master#foo"),
"com.gitlab.gitlab-org.gitlab:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master/foo#bar"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/master/foo#bar"),
"com.gitlab.gitlab-org.gitlab:bar:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/somebranch#foo:SNAPSHOT"),
assertEquals(ensureGAV("https://gitlab.com/gitlab-org/gitlab/-/tree/somebranch#foo:SNAPSHOT"),
"com.gitlab.gitlab-org.gitlab:foo:somebranch-SNAPSHOT");

assertEquals(
JitPackUtil.ensureGAV(
ensureGAV(
"https://gitlab.com/gitlab-org/gitlab/-/commit/120262d85822e6a3d4e04f5c84d0075c60309d97#foo"),
"com.gitlab.gitlab-org.gitlab:foo:120262d858");

}

@Test
void testExtractBitbucketUrlDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler"),
"org.bitbucket.ceylon:ceylon-compiler:-SNAPSHOT");
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler"),
"org.bitbucket.ceylon:ceylon-compiler:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/"),
"org.bitbucket.ceylon:ceylon-compiler:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/foo/"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/foo/"),
"org.bitbucket.ceylon.ceylon-compiler:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/0.4/"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/0.4/"),
"org.bitbucket.ceylon:ceylon-compiler:0.4");

assertEquals(JitPackUtil.ensureGAV(
assertEquals(ensureGAV(
"https://bitbucket.org/ceylon/ceylon-compiler/commits/9a5e4667af5ae03e036dff1294b81b653be6dffc"),
"org.bitbucket.ceylon:ceylon-compiler:9a5e4667af");

}

@Test
void testExtractBitbucketUrlWithHashDependencies() {
assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler#foo"),
"org.bitbucket.ceylon.ceylon-compiler:foo:-SNAPSHOT");
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler#foo"),
"org.bitbucket.ceylon.ceylon-compiler:foo:main-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/#foo"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/#foo"),
"org.bitbucket.ceylon.ceylon-compiler:foo:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/foo/#bar"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/master/foo/#bar"),
"org.bitbucket.ceylon.ceylon-compiler:bar:master-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/somebranch/#foo:SNAPSHOT"),
assertEquals(ensureGAV("https://bitbucket.org/ceylon/ceylon-compiler/src/somebranch/#foo:SNAPSHOT"),
"org.bitbucket.ceylon.ceylon-compiler:foo:somebranch-SNAPSHOT");

assertEquals(JitPackUtil.ensureGAV(
assertEquals(ensureGAV(
"https://bitbucket.org/ceylon/ceylon-compiler/commits/9a5e4667af5ae03e036dff1294b81b653be6dffc#foo"),
"org.bitbucket.ceylon.ceylon-compiler:foo:9a5e4667af");

Expand Down

0 comments on commit 2c867b8

Please sign in to comment.