diff --git a/pom.xml b/pom.xml index 3ca2bd00c0..b05beb55d0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.33 + 1.34 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java @@ -85,6 +85,13 @@ + + + repo.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 9e5e970ded..4253d17e86 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -66,19 +66,22 @@ public class GitHub { private final Map orgs = new HashMap(); /*package*/ String oauthAccessToken; - private final String githubServer; + private final String apiUrl; private GitHub(String login, String apiToken, String password) { - this ("github.com", login, apiToken, password); + this ("https://api.github.com", login, apiToken, password); } /** * - * @param githubServer - * The host name of the GitHub (or GitHub enterprise) server, such as "github.com". + * @param apiUrl + * The URL of GitHub (or GitHub enterprise) API endpoint, such as "https://api.github.com" or + * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has /api/v3 in the URL. + * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. */ - private GitHub(String githubServer, String login, String apiToken, String password) { - this.githubServer = githubServer; + private GitHub(String apiUrl, String login, String apiToken, String password) { + if (apiUrl.endsWith("/")) apiUrl = apiUrl.substring(0, apiUrl.length()-1); // normalize + this.apiUrl = apiUrl; this.login = login; this.apiToken = apiToken; this.password = password; @@ -91,9 +94,9 @@ private GitHub(String githubServer, String login, String apiToken, String passwo encodedAuthorization = null; } - private GitHub (String githubServer, String oauthAccessToken) throws IOException { + private GitHub (String apiUrl, String oauthAccessToken) throws IOException { - this.githubServer = githubServer; + this.apiUrl = apiUrl; this.password = null; this.encodedAuthorization = null; @@ -118,6 +121,21 @@ public static GitHub connect() throws IOException { return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password")); } + /** + * Version that connects to GitHub Enterprise. + * + * @param apiUrl + * The URL of GitHub (or GitHub enterprise) API endpoint, such as "https://api.github.com" or + * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has /api/v3 in the URL. + * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. + */ + public static GitHub connectToEnterprise(String apiUrl, String login, String apiToken) { + // not exposing password because the login process still assumes https://github.com/ + // if we are to fix this, fix that by getting rid of createWebClient() and replace the e-mail service hook + // with GitHub API. + return new GitHub(apiUrl,login,apiToken,null); + } + public static GitHub connect(String login, String apiToken){ return new GitHub(login,apiToken,null); } @@ -153,10 +171,15 @@ public static GitHub connectAnonymously() { tailApiUrl = tailApiUrl + (tailApiUrl.indexOf('?')>=0 ?'&':'?') + "access_token=" + oauthAccessToken; } - if (tailApiUrl.startsWith("/")) - return new URL("https://api."+githubServer+tailApiUrl); - else + if (tailApiUrl.startsWith("/")) { + if ("github.com".equals(apiUrl)) {// backward compatibility + return new URL("https://api.github.com" + tailApiUrl); + } else { + return new URL(apiUrl + tailApiUrl); + } + } else { return new URL(tailApiUrl); + } } /*package*/ Requester retrieve() { @@ -227,7 +250,7 @@ public GHOrganization getOrganization(String name) throws IOException { */ public GHRepository getRepository(String name) throws IOException { String[] tokens = name.split("/"); - return getUser(tokens[0]).getRepository(tokens[1]); + return retrieve().to("/repos/" + tokens[0] + '/' + tokens[1], GHRepository.class).wrap(this); } /** diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java new file mode 100644 index 0000000000..8fbca796b3 --- /dev/null +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -0,0 +1,24 @@ +package org.kohsuke.github; + +import junit.framework.TestCase; + +/** + * Unit test for {@link GitHub}. + */ +public class GitHubTest extends TestCase { + + public void testGitHubServerWithHttp() throws Exception { + GitHub hub = GitHub.connectToEnterprise("http://enterprise.kohsuke.org/api/v3", "kohsuke", "token"); + assertEquals("http://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); + } + + public void testGitHubServerWithHttps() throws Exception { + GitHub hub = GitHub.connectToEnterprise("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token"); + assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); + } + + public void testGitHubServerWithoutServer() throws Exception { + GitHub hub = GitHub.connect("kohsuke", "token", "password"); + assertEquals("https://api.github.com/test", hub.getApiURL("/test").toString()); + } +}