From 82133c117abff108269f646227fd97b2089cf29e Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 16:31:58 -0700 Subject: [PATCH 1/9] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3ca2bd00c0..9ade7041e8 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.33 + 1.34-SNAPSHOT GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java From 1ba8f2ccbf0220ba3120f12a78fcbc2024e5ccac Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 17 Oct 2012 07:45:15 -0700 Subject: [PATCH 2/9] Update pom.xml Added definition --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index 9ade7041e8..ca90fa1be8 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,13 @@ + + + repo.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + From b66ede98c7dc2e45e56591f935da485474d592c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Br=C3=A1zdil?= Date: Sat, 20 Oct 2012 23:24:56 +0200 Subject: [PATCH 3/9] Retrieve repository directly. --- src/main/java/org/kohsuke/github/GitHub.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 9e5e970ded..d9a84d6312 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -227,7 +227,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); } /** From 35d45ca47dc6d41e22219e919399ab7b278d50a3 Mon Sep 17 00:00:00 2001 From: johnou Date: Sun, 6 Jan 2013 00:47:44 +0100 Subject: [PATCH 4/9] JENKINS-13726: Github plugin should work with Guthub enterprise by allowing for overriding the github URL. --- src/main/java/org/kohsuke/github/GitHub.java | 20 +++++++++++-- .../java/org/kohsuke/github/GitHubTest.java | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/kohsuke/github/GitHubTest.java diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 9e5e970ded..7e200ee096 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -118,6 +118,10 @@ public static GitHub connect() throws IOException { return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password")); } + public static GitHub connect(String githubServer, String login, String apiToken, String password){ + return new GitHub(githubServer,login,apiToken,password); + } + public static GitHub connect(String login, String apiToken){ return new GitHub(login,apiToken,null); } @@ -153,10 +157,20 @@ 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(githubServer)) { + return new URL("https://api." + githubServer + tailApiUrl); + } else { + // use protocol if defined otherwise default to https. + if (githubServer.matches("^(https?)://.*$")) { + return new URL(githubServer + "/api/v3" + tailApiUrl); + } else { + return new URL("https://" + githubServer + "/api/v3" + tailApiUrl); + } + } + } else { return new URL(tailApiUrl); + } } /*package*/ Requester retrieve() { 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..316ce44e9f --- /dev/null +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -0,0 +1,29 @@ +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.connect("http://enterprise.kohsuke.org", "kohsuke", "token", "password"); + assertEquals("http://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); + } + + public void testGitHubServerWithHttps() throws Exception { + GitHub hub = GitHub.connect("https://enterprise.kohsuke.org", "kohsuke", "token", "password"); + assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); + } + + public void testGitHubServerWithoutProtocol() throws Exception { + GitHub hub = GitHub.connect("enterprise.kohsuke.org", "kohsuke", "token", "password"); + 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()); + } +} From 5166202f6799abe72f5e382fed4eb805c626cc6b Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 5 Jan 2013 15:59:02 -0800 Subject: [PATCH 5/9] follow-up fix and documenting the expected value --- src/main/java/org/kohsuke/github/GitHub.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 7e200ee096..122307db12 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -69,15 +69,18 @@ public class GitHub { private final String githubServer; 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". + * The URL of GitHub (or GitHub enterprise API endpoint), such as "https://api.github.com" or + * "http://ghe.acme.com". + * 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) { + if (githubServer.endsWith("/")) githubServer=githubServer.substring(0,githubServer.length()-1); // normalize this.githubServer = githubServer; this.login = login; this.apiToken = apiToken; @@ -158,10 +161,10 @@ public static GitHub connectAnonymously() { } if (tailApiUrl.startsWith("/")) { - if ("github.com".equals(githubServer)) { + if ("github.com".equals(githubServer)) {// backward compatibility return new URL("https://api." + githubServer + tailApiUrl); } else { - // use protocol if defined otherwise default to https. + // use protocol if defined otherwise default to https for backward compatibility if (githubServer.matches("^(https?)://.*$")) { return new URL(githubServer + "/api/v3" + tailApiUrl); } else { From cbaca87bbc32ec9dbe0f087e71743ed1b682c6c2 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 5 Jan 2013 16:08:42 -0800 Subject: [PATCH 6/9] massaging this a bit to accept the full URL --- src/main/java/org/kohsuke/github/GitHub.java | 28 +++++++++---------- .../java/org/kohsuke/github/GitHubTest.java | 6 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 122307db12..05c6376607 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -66,7 +66,7 @@ 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 ("https://api.github.com", login, apiToken, password); @@ -74,14 +74,14 @@ private GitHub(String login, String apiToken, String password) { /** * - * @param githubServer - * The URL of GitHub (or GitHub enterprise API endpoint), such as "https://api.github.com" or - * "http://ghe.acme.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) { - if (githubServer.endsWith("/")) githubServer=githubServer.substring(0,githubServer.length()-1); // normalize - 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; @@ -94,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; @@ -161,14 +161,14 @@ public static GitHub connectAnonymously() { } if (tailApiUrl.startsWith("/")) { - if ("github.com".equals(githubServer)) {// backward compatibility - return new URL("https://api." + githubServer + tailApiUrl); + if ("github.com".equals(apiUrl)) {// backward compatibility + return new URL("https://api." + apiUrl + tailApiUrl); } else { // use protocol if defined otherwise default to https for backward compatibility - if (githubServer.matches("^(https?)://.*$")) { - return new URL(githubServer + "/api/v3" + tailApiUrl); + if (apiUrl.matches("^(https?)://.*$")) { + return new URL(apiUrl + tailApiUrl); } else { - return new URL("https://" + githubServer + "/api/v3" + tailApiUrl); + return new URL("https://" + apiUrl + "/api/v3" + tailApiUrl); } } } else { diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 316ce44e9f..a8535e2c2f 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -8,17 +8,17 @@ public class GitHubTest extends TestCase { public void testGitHubServerWithHttp() throws Exception { - GitHub hub = GitHub.connect("http://enterprise.kohsuke.org", "kohsuke", "token", "password"); + GitHub hub = GitHub.connect("http://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); assertEquals("http://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } public void testGitHubServerWithHttps() throws Exception { - GitHub hub = GitHub.connect("https://enterprise.kohsuke.org", "kohsuke", "token", "password"); + GitHub hub = GitHub.connect("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } public void testGitHubServerWithoutProtocol() throws Exception { - GitHub hub = GitHub.connect("enterprise.kohsuke.org", "kohsuke", "token", "password"); + GitHub hub = GitHub.connect("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } From 13158a28e12284904fd29ba829065875d514ba43 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 5 Jan 2013 17:10:24 -0800 Subject: [PATCH 7/9] turns out we never exposed the ability to specify the custom URL. So no backward compatibility provision is needed. Also in this change, I stopped exposin the password. See the code comment for more details --- src/main/java/org/kohsuke/github/GitHub.java | 24 ++++++++++++------- .../java/org/kohsuke/github/GitHubTest.java | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 05c6376607..62f483c073 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -121,8 +121,19 @@ public static GitHub connect() throws IOException { return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password")); } - public static GitHub connect(String githubServer, String login, String apiToken, String password){ - return new GitHub(githubServer,login,apiToken,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){ @@ -162,14 +173,9 @@ public static GitHub connectAnonymously() { if (tailApiUrl.startsWith("/")) { if ("github.com".equals(apiUrl)) {// backward compatibility - return new URL("https://api." + apiUrl + tailApiUrl); + return new URL("https://api.github.com" + tailApiUrl); } else { - // use protocol if defined otherwise default to https for backward compatibility - if (apiUrl.matches("^(https?)://.*$")) { - return new URL(apiUrl + tailApiUrl); - } else { - return new URL("https://" + apiUrl + "/api/v3" + tailApiUrl); - } + return new URL(apiUrl + tailApiUrl); } } else { return new URL(tailApiUrl); diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index a8535e2c2f..6a0f84ed13 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -18,7 +18,7 @@ public void testGitHubServerWithHttps() throws Exception { } public void testGitHubServerWithoutProtocol() throws Exception { - GitHub hub = GitHub.connect("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); + GitHub hub = GitHub.connect("enterprise.kohsuke.org", "kohsuke", "token", "password"); assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } From 6480dde24748fa7b1ee362b6cb8e08c25e77caac Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 5 Jan 2013 17:15:46 -0800 Subject: [PATCH 8/9] oops test failures --- src/test/java/org/kohsuke/github/GitHubTest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 6a0f84ed13..8fbca796b3 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -8,17 +8,12 @@ public class GitHubTest extends TestCase { public void testGitHubServerWithHttp() throws Exception { - GitHub hub = GitHub.connect("http://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); + 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.connect("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token", "password"); - assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); - } - - public void testGitHubServerWithoutProtocol() throws Exception { - GitHub hub = GitHub.connect("enterprise.kohsuke.org", "kohsuke", "token", "password"); + GitHub hub = GitHub.connectToEnterprise("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token"); assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } From 3dd738b0dbad14f8cfc00c3901b16c5fcb248884 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 5 Jan 2013 17:18:11 -0800 Subject: [PATCH 9/9] [maven-release-plugin] prepare release github-api-1.34 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ca90fa1be8..b05beb55d0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.34-SNAPSHOT + 1.34 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java