From 81a144e18c69b258133ea5769505026ed4e4a63d Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 00:05:51 +0100 Subject: [PATCH 01/11] Added an option to override GitHub API endpoint. --- README.md | 1 + .../de/jutzig/github/release/plugin/UploadMojo.java | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae4f573..622a6ad 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,6 @@ Additional Parameters: * `-Dgithub.draft=true` creates the release in draft state * `-Dgithub.commitish=release/1.0.0` allows to specify a commitsh + * `-Dgithub.apiEndpoint=https://github.myorg.com/` allows to customize the root endpoint of GitHub REST API in case of private GitHub Enterprise installations. The plugin is available on Maven central diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index af42ae7..69b3d50 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -48,6 +48,7 @@ import org.kohsuke.github.GHReleaseBuilder; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GitHub; +import org.kohsuke.github.GitHubBuilder; import org.kohsuke.github.PagedIterable; /** @@ -92,6 +93,12 @@ public class UploadMojo extends AbstractMojo implements Contextualizable{ @Parameter(property = "github.draft") private Boolean draft; + /** + * GitHub REST API root endpoint. + */ + @Parameter(property = "github.apiEndpoint", defaultValue = "https://api.github.com") + private String githubApiEndpoint; + /** * The github id of the project. By default initialized from the project scm connection */ @@ -311,10 +318,11 @@ public GitHub createGithub(String serverId) throws MojoExecutionException, IOExc String serverUsername = server.getUsername(); String serverPassword = server.getPassword(); String serverAccessToken = server.getPrivateKey(); + GitHubBuilder gitHubBuilder = new GitHubBuilder().withEndpoint(githubApiEndpoint); if (StringUtils.isNotEmpty(serverUsername) && StringUtils.isNotEmpty(serverPassword)) - return GitHub.connectUsingPassword(serverUsername, serverPassword); + return gitHubBuilder.withPassword(serverUsername, serverPassword).build(); else if (StringUtils.isNotEmpty(serverAccessToken)) - return GitHub.connectUsingOAuth(serverAccessToken); + return gitHubBuilder.withOAuthToken(serverAccessToken).build(); else throw new MojoExecutionException("Configuration for server " + serverId + " has no login credentials"); } From 8b41fd69a1830f1a65b03e4a715c9a4d7116e991 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 00:05:52 +0100 Subject: [PATCH 02/11] Added compile-time dependency on plexus-utils. Maven 3.9.0 and onward doesn't include this dependency into plugins' classpath any more, see https://maven.apache.org/docs/3.9.0/release-notes.html --- pom.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7388e04..8b919fb 100644 --- a/pom.xml +++ b/pom.xml @@ -201,7 +201,7 @@ org.kohsuke github-api - 1.95 + 1.317 @@ -222,6 +222,11 @@ 3.5.4 provided + + org.codehaus.plexus + plexus-utils + 3.2.1 + junit From 4f7451bfc00648c1c6226619b86ad58a5f7c0812 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 10:38:26 +0100 Subject: [PATCH 03/11] Added an option to override GitHub API endpoint also when authenticating with username / password. --- .../java/de/jutzig/github/release/plugin/UploadMojo.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index 69b3d50..85e49a1 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -295,10 +295,11 @@ public static String computeRepositoryId(String id) { public GitHub createGithub(String serverId) throws MojoExecutionException, IOException { String usernameProperty = System.getProperty("username"); String passwordProperty = System.getProperty("password"); + GitHubBuilder gitHubBuilder = new GitHubBuilder().withEndpoint(githubApiEndpoint); if(usernameProperty!=null && passwordProperty!=null) { - getLog().debug("Using server credentials from system properties 'username' and 'password'"); - return GitHub.connectUsingPassword(usernameProperty, passwordProperty); + getLog().debug("Using server credentials from system properties 'username' and 'password'"); + return gitHubBuilder.withPassword(usernameProperty, passwordProperty).build(); } Server server = getServer(settings, serverId); @@ -318,7 +319,6 @@ public GitHub createGithub(String serverId) throws MojoExecutionException, IOExc String serverUsername = server.getUsername(); String serverPassword = server.getPassword(); String serverAccessToken = server.getPrivateKey(); - GitHubBuilder gitHubBuilder = new GitHubBuilder().withEndpoint(githubApiEndpoint); if (StringUtils.isNotEmpty(serverUsername) && StringUtils.isNotEmpty(serverPassword)) return gitHubBuilder.withPassword(serverUsername, serverPassword).build(); else if (StringUtils.isNotEmpty(serverAccessToken)) From 8401a04952fc2e35fb2ee79caeb872e2224c55c9 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 12:25:30 +0100 Subject: [PATCH 04/11] Changed to infer the GitHub API endpoint from the scm connection string. --- README.md | 5 ++- pom.xml | 8 ++-- .../github/release/plugin/UploadMojo.java | 44 ++++++++++++++++--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 622a6ad..bb2b071 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,9 @@ Additional Parameters: * `-Dgithub.draft=true` creates the release in draft state * `-Dgithub.commitish=release/1.0.0` allows to specify a commitsh - * `-Dgithub.apiEndpoint=https://github.myorg.com/` allows to customize the root endpoint of GitHub REST API in case of private GitHub Enterprise installations. The plugin is available on Maven central + +## Note on the GitHub API endpoints +The endpoint for GitHub API is inferred from `` connection string. When missing, it by default would use the public endpoint at https://api.github.com. +If you want to upload to a GitHub enterprise instance, then a respective `` connection string must be specified. diff --git a/pom.xml b/pom.xml index 8b919fb..dfb6bda 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ UTF-8 - 1.7 + 1.8 ${java.version} ${java.version} @@ -229,9 +229,9 @@ - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter + 5.10.1 test diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index 85e49a1..954b86d 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -25,11 +25,14 @@ import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.FileSet; +import org.apache.maven.model.Scm; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest; @@ -57,6 +60,7 @@ @Mojo(name = "release", defaultPhase = LifecyclePhase.DEPLOY) public class UploadMojo extends AbstractMojo implements Contextualizable{ + private static final String PUBLIC_GITUHB_API_ENDPOINT = "https://api.github.com"; /** * Server id for github access. */ @@ -93,12 +97,6 @@ public class UploadMojo extends AbstractMojo implements Contextualizable{ @Parameter(property = "github.draft") private Boolean draft; - /** - * GitHub REST API root endpoint. - */ - @Parameter(property = "github.apiEndpoint", defaultValue = "https://api.github.com") - private String githubApiEndpoint; - /** * The github id of the project. By default initialized from the project scm connection */ @@ -156,6 +154,9 @@ public class UploadMojo extends AbstractMojo implements Contextualizable{ @Parameter(defaultValue = "false") private Boolean failOnExistingRelease; + @Component + private MavenProject project; + public void execute() throws MojoExecutionException { if(releaseName==null) releaseName = tag; @@ -277,7 +278,7 @@ private GHRelease findRelease(GHRepository repository, String releaseNameToFind) */ private static final Pattern REPOSITORY_PATTERN = Pattern.compile( "^(scm:git[:|])?" + //Maven prefix for git SCM - "(https?://github\\.com/|git@github\\.com:)" + //GitHub prefix for HTTP/HTTPS/SSH/Subversion scheme + "(https?://[\\w\\d.-]+/|git@[\\w\\d.-]+:)" + //GitHub prefix for HTTP/HTTPS/SSH/Subversion scheme "([^/]+/[^/\\.]+)" + //Repository ID "(\\.git)?" + //Optional suffix ".git" "(/.*)?$" //Optional child project path @@ -292,9 +293,38 @@ public static String computeRepositoryId(String id) { } } + static String computeGithubApiEndpoint(Scm scm) { + if (scm == null || StringUtils.isEmpty(scm.getConnection())) { + return PUBLIC_GITUHB_API_ENDPOINT; + } + + Matcher matcher = REPOSITORY_PATTERN.matcher(scm.getConnection()); + if (matcher.matches()) { + String githubApiEndpoint = matcher.group(2); + if (githubApiEndpoint.startsWith("git@")) { + // According to the regex pattern above, the matched group would be in a form of git@hostname: + githubApiEndpoint = githubApiEndpoint.substring(4, githubApiEndpoint.length() - 1); + } + // Public + if (githubApiEndpoint.contains("github.com")) { + return PUBLIC_GITUHB_API_ENDPOINT; + } + + githubApiEndpoint = StringUtils.removeEnd(githubApiEndpoint, "/"); + if (!githubApiEndpoint.startsWith("http")) { + githubApiEndpoint = "https://" + githubApiEndpoint; + } + // See https://docs.github.com/en/enterprise-server@3.10/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#schema + return githubApiEndpoint + "/api/v3"; + } else { + return PUBLIC_GITUHB_API_ENDPOINT; + } + } + public GitHub createGithub(String serverId) throws MojoExecutionException, IOException { String usernameProperty = System.getProperty("username"); String passwordProperty = System.getProperty("password"); + String githubApiEndpoint = computeGithubApiEndpoint(project.getScm()); GitHubBuilder gitHubBuilder = new GitHubBuilder().withEndpoint(githubApiEndpoint); if(usernameProperty!=null && passwordProperty!=null) { From 09b04d28c70e1b7b6b995b7d927c1add4a2d243a Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 12:25:49 +0100 Subject: [PATCH 05/11] Added missing test. --- .../github/release/plugin/UploadMojoTest.java | 124 ++++++++++++------ 1 file changed, 87 insertions(+), 37 deletions(-) diff --git a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java index a1fdc39..56441a9 100644 --- a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java +++ b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java @@ -1,54 +1,49 @@ package de.jutzig.github.release.plugin; -import org.junit.Before; -import org.junit.Test; +import java.util.stream.Stream; -import java.util.HashMap; -import java.util.Map; +import org.apache.maven.model.Scm; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; -public class UploadMojoTest { +class UploadMojoTest { + @ParameterizedTest(name = "{0} should resolve to {1} repository id") + @CsvSource({ + "scm:git:https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", - private Map computeRepositoryIdData; + "scm:git:http://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|http://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "http://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", - @Before - public void setUp() throws Exception { - computeRepositoryIdData = new HashMap(); + "scm:git:git@github.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|git@github.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "git@github.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", - computeRepositoryIdData.put("scm:git:https://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("scm:git|https://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("https://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); + "scm:git:https://github.com/jutzig/github-release-plugin, jutzig/github-release-plugin", + "scm:git|https://github.com/jutzig/github-release-plugin, jutzig/github-release-plugin", + "https://github.com/jutzig/github-release-plugin, jutzig/github-release-plugin", - computeRepositoryIdData.put("scm:git:http://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("scm:git|http://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("http://github.com/jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - - computeRepositoryIdData.put("scm:git:git@github.com:jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("scm:git|git@github.com:jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("git@github.com:jutzig/github-release-plugin.git", "jutzig/github-release-plugin"); - - computeRepositoryIdData.put("scm:git:https://github.com/jutzig/github-release-plugin", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("scm:git|https://github.com/jutzig/github-release-plugin", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("https://github.com/jutzig/github-release-plugin", "jutzig/github-release-plugin"); - - computeRepositoryIdData.put("scm:git:http://github.com/jutzig/github-release-plugin.git/child", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("scm:git|http://github.com/jutzig/github-release-plugin.git/child", "jutzig/github-release-plugin"); - computeRepositoryIdData.put("http://github.com/jutzig/github-release-plugin.git/child", "jutzig/github-release-plugin"); + "scm:git:http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", + "scm:git|http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", + "http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin" + }) + void testComputeRepositoryId(String scmString, String expectedRepositoryId) { + assertEquals(expectedRepositoryId, UploadMojo.computeRepositoryId(scmString)); } - @Test - public void testComputeRepositoryId() throws Exception { - for (String source : computeRepositoryIdData.keySet()) { - String expected = computeRepositoryIdData.get(source); - assertEquals(source, expected, UploadMojo.computeRepositoryId(source)); - } + @ParameterizedTest(name = "{0} should resolve to {1} endpoiont") + @MethodSource("scmFixture") + void testGithubEndpoint(Scm scm, String expectedEndpoint) { + assertEquals(expectedEndpoint, UploadMojo.computeGithubApiEndpoint(scm)); } @Test - public void testGuessPreRelease() { + void testGuessPreRelease() { assertTrue(UploadMojo.guessPreRelease("1.0-SNAPSHOT")); assertTrue(UploadMojo.guessPreRelease("1.0-alpha")); assertTrue(UploadMojo.guessPreRelease("1.0-alpha-1")); @@ -62,4 +57,59 @@ public void testGuessPreRelease() { assertFalse(UploadMojo.guessPreRelease("1")); assertFalse(UploadMojo.guessPreRelease("1.0")); } + + private static Stream scmFixture() { + return Stream.of( + // Public GitHub + Arguments.of(mockScmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + + Arguments.of(mockScmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + + Arguments.of(mockScmWithConnectionString("scm:git:git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("scm:git|git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + + Arguments.of(mockScmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + + Arguments.of(mockScmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + Arguments.of(mockScmWithConnectionString("http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + + // GitHub Enterprise + Arguments.of(mockScmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + + Arguments.of(mockScmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + + Arguments.of(mockScmWithConnectionString("scm:git:git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("scm:git|git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + + Arguments.of(mockScmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + + Arguments.of(mockScmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + Arguments.of(mockScmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + + // Fallback to public + Arguments.of(null, "https://api.github.com") + ); + } + + private static Scm mockScmWithConnectionString(String connection) { + Scm scm = new Scm(); + scm.setConnection(connection); + return scm; + } } From bfc79f1f598acb4870ceb148fe726f807a0c170f Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 12:49:16 +0100 Subject: [PATCH 06/11] Added missing test. --- .../github/release/plugin/UploadMojoTest.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java index 56441a9..f9ae30b 100644 --- a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java +++ b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java @@ -12,6 +12,7 @@ class UploadMojoTest { @ParameterizedTest(name = "{0} should resolve to {1} repository id") @CsvSource({ + // Public "scm:git:https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", "scm:git|https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", "https://github.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", @@ -30,7 +31,28 @@ class UploadMojoTest { "scm:git:http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", "scm:git|http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", - "http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin" + "http://github.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", + + // Enterprise + "scm:git:https://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|https://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "https://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + + "scm:git:http://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|http://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "http://github.acme.com/jutzig/github-release-plugin.git, jutzig/github-release-plugin", + + "scm:git:git@github.acme.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "scm:git|git@github.acme.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", + "git@github.acme.com:jutzig/github-release-plugin.git, jutzig/github-release-plugin", + + "scm:git:https://github.acme.com/jutzig/github-release-plugin, jutzig/github-release-plugin", + "scm:git|https://github.acme.com/jutzig/github-release-plugin, jutzig/github-release-plugin", + "https://github.acme.com/jutzig/github-release-plugin, jutzig/github-release-plugin", + + "scm:git:http://github.acme.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", + "scm:git|http://github.acme.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin", + "http://github.acme.com/jutzig/github-release-plugin.git/child, jutzig/github-release-plugin" }) void testComputeRepositoryId(String scmString, String expectedRepositoryId) { assertEquals(expectedRepositoryId, UploadMojo.computeRepositoryId(scmString)); From 1fea0cc24e4a2c3e1497d7e090a3d6a87e0e213f Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 13:39:28 +0100 Subject: [PATCH 07/11] Refactored to reduce condition nesting level. --- .../github/release/plugin/UploadMojo.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index 954b86d..fce82be 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -297,29 +297,27 @@ static String computeGithubApiEndpoint(Scm scm) { if (scm == null || StringUtils.isEmpty(scm.getConnection())) { return PUBLIC_GITUHB_API_ENDPOINT; } - Matcher matcher = REPOSITORY_PATTERN.matcher(scm.getConnection()); - if (matcher.matches()) { - String githubApiEndpoint = matcher.group(2); - if (githubApiEndpoint.startsWith("git@")) { - // According to the regex pattern above, the matched group would be in a form of git@hostname: - githubApiEndpoint = githubApiEndpoint.substring(4, githubApiEndpoint.length() - 1); - } - // Public - if (githubApiEndpoint.contains("github.com")) { - return PUBLIC_GITUHB_API_ENDPOINT; - } + if (!matcher.matches()) { + return PUBLIC_GITUHB_API_ENDPOINT; + } - githubApiEndpoint = StringUtils.removeEnd(githubApiEndpoint, "/"); - if (!githubApiEndpoint.startsWith("http")) { - githubApiEndpoint = "https://" + githubApiEndpoint; - } - // See https://docs.github.com/en/enterprise-server@3.10/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#schema - return githubApiEndpoint + "/api/v3"; - } else { + String githubApiEndpoint = matcher.group(2); + if (githubApiEndpoint.startsWith("git@")) { + // According to the regex pattern above, the matched group would be in a form of git@hostname: + githubApiEndpoint = githubApiEndpoint.substring(4, githubApiEndpoint.length() - 1); + } + if (githubApiEndpoint.contains("github.com")) { return PUBLIC_GITUHB_API_ENDPOINT; } - } + + githubApiEndpoint = StringUtils.removeEnd(githubApiEndpoint, "/"); + if (!githubApiEndpoint.startsWith("http")) { + githubApiEndpoint = "https://" + githubApiEndpoint; + } + // See https://docs.github.com/en/enterprise-server@3.10/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#schema + return githubApiEndpoint + "/api/v3"; + } public GitHub createGithub(String serverId) throws MojoExecutionException, IOException { String usernameProperty = System.getProperty("username"); From 4c8acc3a0d3f37b78172e06bc72afc52be071c68 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 13:42:07 +0100 Subject: [PATCH 08/11] Fixed whitespace issue. --- .../java/de/jutzig/github/release/plugin/UploadMojo.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index fce82be..245aab8 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -298,9 +298,9 @@ static String computeGithubApiEndpoint(Scm scm) { return PUBLIC_GITUHB_API_ENDPOINT; } Matcher matcher = REPOSITORY_PATTERN.matcher(scm.getConnection()); - if (!matcher.matches()) { - return PUBLIC_GITUHB_API_ENDPOINT; - } + if (!matcher.matches()) { + return PUBLIC_GITUHB_API_ENDPOINT; + } String githubApiEndpoint = matcher.group(2); if (githubApiEndpoint.startsWith("git@")) { From 2195b9f03e714f7d10ce9931da901385fb7fb4e6 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 13:44:19 +0100 Subject: [PATCH 09/11] Further refactored to pull up all public URL fallbacks to one place. --- .../java/de/jutzig/github/release/plugin/UploadMojo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java index 245aab8..6ecf54f 100644 --- a/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java +++ b/src/main/java/de/jutzig/github/release/plugin/UploadMojo.java @@ -301,15 +301,15 @@ static String computeGithubApiEndpoint(Scm scm) { if (!matcher.matches()) { return PUBLIC_GITUHB_API_ENDPOINT; } - String githubApiEndpoint = matcher.group(2); + if (githubApiEndpoint.contains("github.com")) { + return PUBLIC_GITUHB_API_ENDPOINT; + } + if (githubApiEndpoint.startsWith("git@")) { // According to the regex pattern above, the matched group would be in a form of git@hostname: githubApiEndpoint = githubApiEndpoint.substring(4, githubApiEndpoint.length() - 1); } - if (githubApiEndpoint.contains("github.com")) { - return PUBLIC_GITUHB_API_ENDPOINT; - } githubApiEndpoint = StringUtils.removeEnd(githubApiEndpoint, "/"); if (!githubApiEndpoint.startsWith("http")) { From 95b5db4ecf7160d329a7161cd2956e0968ebfd96 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 13:46:19 +0100 Subject: [PATCH 10/11] Renamed test method. --- .../github/release/plugin/UploadMojoTest.java | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java index f9ae30b..33a066a 100644 --- a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java +++ b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java @@ -83,53 +83,53 @@ void testGuessPreRelease() { private static Stream scmFixture() { return Stream.of( // Public GitHub - Arguments.of(mockScmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("https://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("http://github.com/jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git:git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git|git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git:git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git|git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("git@github.com:jutzig/github-release-plugin.git"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git:https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git|https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("https://github.com/jutzig/github-release-plugin"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), - Arguments.of(mockScmWithConnectionString("http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git:http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("scm:git|http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), + Arguments.of(scmWithConnectionString("http://github.com/jutzig/github-release-plugin.git/child"), "https://api.github.com"), // GitHub Enterprise - Arguments.of(mockScmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git"), "http://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git:git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git|git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git:git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git|git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("git@github.acme.com:jutzig/github-release-plugin.git"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git:https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git|https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("https://github.acme.com/jutzig/github-release-plugin"), "https://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), - Arguments.of(mockScmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git:http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("scm:git|http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), + Arguments.of(scmWithConnectionString("http://github.acme.com/jutzig/github-release-plugin.git/child"), "http://github.acme.com/api/v3"), // Fallback to public Arguments.of(null, "https://api.github.com") ); } - private static Scm mockScmWithConnectionString(String connection) { + private static Scm scmWithConnectionString(String connection) { Scm scm = new Scm(); scm.setConnection(connection); return scm; From b97ade0d8790bd7ebcd18badff42acf61d3777d5 Mon Sep 17 00:00:00 2001 From: anenviousguest Date: Tue, 14 Nov 2023 13:46:53 +0100 Subject: [PATCH 11/11] Fixed typo. --- .../java/de/jutzig/github/release/plugin/UploadMojoTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java index 33a066a..305bf37 100644 --- a/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java +++ b/src/test/java/de/jutzig/github/release/plugin/UploadMojoTest.java @@ -58,7 +58,7 @@ void testComputeRepositoryId(String scmString, String expectedRepositoryId) { assertEquals(expectedRepositoryId, UploadMojo.computeRepositoryId(scmString)); } - @ParameterizedTest(name = "{0} should resolve to {1} endpoiont") + @ParameterizedTest(name = "{0} should resolve to {1} endpoint") @MethodSource("scmFixture") void testGithubEndpoint(Scm scm, String expectedEndpoint) { assertEquals(expectedEndpoint, UploadMojo.computeGithubApiEndpoint(scm));