Skip to content

Commit

Permalink
JENKINS-13726: Github plugin should work with Guthub enterprise by al…
Browse files Browse the repository at this point in the history
…lowing for overriding the github URL.
  • Loading branch information
johnou committed Jan 5, 2013
1 parent 1ba8f2c commit 35d45ca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 35d45ca

Please sign in to comment.