com.scireum.AbstractGithubTagMojo Maven / Gradle / Ivy
Show all versions of github-tag-plugin Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package com.scireum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.io.CharStreams;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Base class for all goals provided by this plugin.
*
* Takes care of fetching all necessarry parameters to use the GitHub API.
*/
public abstract class AbstractGithubTagMojo extends AbstractMojo {
/**
* Detects and parses scm urls like https://github.com/OWNER/REPO.git
*/
private static final Pattern HTTP_GITHUB_URL = Pattern.compile("https?://github.com/([^/]+)/([^\\.]+)\\.git");
/**
* Detects and parses scm urls like [email protected]:OWNER/REPO.git
*/
private static final Pattern SSH_GITHUB_URL = Pattern.compile("git@github\\.com:([^/]+)/([^\\.]+)\\.git");
/**
* Contains the oauth2 token used to access the GitHub API.
*
* This should most probably be set in the settings.xml
*/
@Parameter(property = "github.global.oauth2Token")
protected String oauth2Token;
/**
* Contains the commit hash which might be tagged.
*/
@Parameter(property = "github.commitHash")
protected String commitHash;
/**
* Contains the repository owner.
*
* If empty this will be filled by parsing the SCM URL
*/
@Parameter(defaultValue = "")
protected String repositoryOwner;
/**
* Contains the repository name.
*
* If empty this will be filled by parsing the SCM URL
*/
@Parameter(defaultValue = "")
protected String repositoryName;
@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;
/**
* Fetches the current commit.
*
* @return the current commit hash
*/
protected String getCurrentCommit() {
return commitHash;
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (Strings.isNullOrEmpty(oauth2Token)) {
getLog().info("No GitHub oauth2 token (github.global.oauth2Token) is present. Skipping....");
return;
}
if (Strings.isNullOrEmpty(repositoryOwner)) {
loadConfigFromSCM();
if (Strings.isNullOrEmpty(repositoryOwner)) {
getLog().info("No GitHub repository owner is present. Skipping....");
return;
}
}
if (Strings.isNullOrEmpty(repositoryName)) {
getLog().info("No GitHub respository name is present. Skipping....");
return;
}
executeWithConfig();
}
/*
* Tries to parse repositoryOwner and repositoryName from the projects SCM URL
*/
protected void loadConfigFromSCM() {
final Scm scm = project.getScm();
if (scm != null) {
if (!Strings.isNullOrEmpty(scm.getUrl())) {
Matcher m = HTTP_GITHUB_URL.matcher(scm.getUrl());
if (m.matches()) {
repositoryOwner = m.group(1);
repositoryName = m.group(2);
} else {
m = SSH_GITHUB_URL.matcher(scm.getUrl());
if (m.matches()) {
repositoryOwner = m.group(1);
repositoryName = m.group(2);
}
}
}
}
}
/**
* Executes a call against the GitHub API
*
* @param method the request method to use
* @param uri the relative url. https://api.github.com/repos/OWNER/REPO is already given
* @param input the JSON to send to the server. Use null for GET requests.
* @return the parsed JSON from the server with state containing the HTTP status code
* @throws Exception in case of any error
*/
protected JSONObject call(String method, String uri, JSONObject input) throws Exception {
URLConnection c = new URL("https://api.github.com/repos/"
+ repositoryOwner
+ "/"
+ repositoryName
+ uri
+ "?access_token="
+ oauth2Token).openConnection();
((HttpURLConnection) c).setRequestMethod(method);
c.setDoInput(true);
if (input != null) {
c.setDoOutput(true);
String inputAsJSON = input.toJSONString();
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(c.getOutputStream(), Charsets.UTF_8))) {
writer.print(inputAsJSON);
}
}
String responseAsJSON = CharStreams.toString(new InputStreamReader(c.getInputStream(), Charsets.UTF_8));
JSONObject result = JSON.parseObject(responseAsJSON);
result.put("state", ((HttpURLConnection) c).getResponseCode());
return result;
}
/**
* Executes the goal with a valid config.
*
* @throws MojoExecutionException in case an error occurs
*/
protected abstract void executeWithConfig() throws MojoExecutionException;
}