Skip to content

Commit

Permalink
Improve checkApiUrlValidity() method to support the private mode in G…
Browse files Browse the repository at this point in the history
…itHub Enterprise servers
  • Loading branch information
recena committed Mar 5, 2016
1 parent dbc79f8 commit ae85cf4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
Expand Down Expand Up @@ -482,7 +483,34 @@ void check(String apiUrl) throws IOException {
* Otherwise this method throws {@link IOException} to indicate the problem.
*/
public void checkApiUrlValidity() throws IOException {
retrieve().to("/", GHApiInfo.class).check(apiUrl);
try {
retrieve().to("/", GHApiInfo.class).check(apiUrl);
} catch (IOException ioe) {
if (isPrivateModeEnabled()) {
throw new IOException("GitHub Enterprise server (" + apiUrl + ") with private mode enabled");
}
throw ioe;
}
}

/**
* Ensures if a GitHub Enterprise server is configured in private mode.
*
* @return {@code true} if private mode is enabled. If it tries to use this method with GitHub, returns {@code
* false}.
*/
private boolean isPrivateModeEnabled() {
try {
HttpURLConnection connect = getConnector().connect(getApiURL("/"));
if (connect.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED
&& connect.getHeaderField("Server") != null
&& connect.getHeaderField("Server").equals("GitHub.com")) {
return true;
}
return false;
} catch (IOException e) {
return false;
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException {
@Test
public void testGitHubIsApiUrlValid() throws IOException {
GitHub github = GitHub.connectAnonymously();
github.checkApiUrlValidity();
//GitHub github = GitHub.connectToEnterpriseAnonymously("https://github.mycompany.com/api/v3/");
try {
github.checkApiUrlValidity();
} catch (IOException ioe) {
assertTrue(ioe.getMessage().contains("private mode enabled"));
}
}
}

0 comments on commit ae85cf4

Please sign in to comment.