-
Notifications
You must be signed in to change notification settings - Fork 24
Fix #16: Added support for GitHub Enterprise instances #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
81a144e
8b41fd6
4f7451b
8401a04
09b04d2
bfc79f1
1fea0cc
4c8acc3
2195b9f
95b5db4
b97ade0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ | |
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <java.version>1.7</java.version> | ||
| <java.version>1.8</java.version> | ||
| <maven.compiler.source>${java.version}</maven.compiler.source> | ||
| <maven.compiler.target>${java.version}</maven.compiler.target> | ||
| </properties> | ||
|
|
@@ -201,7 +201,7 @@ | |
| <dependency> | ||
| <groupId>org.kohsuke</groupId> | ||
| <artifactId>github-api</artifactId> | ||
| <version>1.95</version> | ||
| <version>1.317</version> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.95 was using the hard-coded string URL for uploading the artifacts (upload.github.com), despite of the endpoint customization via |
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
@@ -222,11 +222,16 @@ | |
| <version>3.5.4</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From Maven 3.9.0 and onwards, plexus-utils is no longer placed to plugin classpath by default (see release notes), so I had to add this one to ensure the code can run also when using recent Maven versions, which is the case in our environment. 3.2.1 is the one which works with |
||
| <groupId>org.codehaus.plexus</groupId> | ||
| <artifactId>plexus-utils</artifactId> | ||
| <version>3.2.1</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>5.10.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -48,6 +51,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; | ||
|
|
||
| /** | ||
|
|
@@ -56,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. | ||
| */ | ||
|
|
@@ -149,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; | ||
|
|
@@ -270,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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to allow other hostnames. Alphanumeric, dots, dashes. Not perfect but better than nothing. |
||
| "([^/]+/[^/\\.]+)" + //Repository ID | ||
| "(\\.git)?" + //Optional suffix ".git" | ||
| "(/.*)?$" //Optional child project path | ||
|
|
@@ -285,13 +293,41 @@ 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()) { | ||
| 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); | ||
| } | ||
|
|
||
| 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"); | ||
| String passwordProperty = System.getProperty("password"); | ||
| String githubApiEndpoint = computeGithubApiEndpoint(project.getScm()); | ||
| 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); | ||
|
|
@@ -312,9 +348,9 @@ public GitHub createGithub(String serverId) throws MojoExecutionException, IOExc | |
| String serverPassword = server.getPassword(); | ||
| String serverAccessToken = server.getPrivateKey(); | ||
| 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"); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.7 is officially EOL by Oracle since 2022 and EOL by OpenJDK since 2020.
Also maven-plugin-api from 3.1.0 onward requires JDK 8
Besides that, it hinders the usage of JUnit 5 which makes writing parameterized tests so much easier.