Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae85cf4

Browse filesBrowse files
committed
Improve checkApiUrlValidity() method to support the private mode in GitHub Enterprise servers
1 parent dbc79f8 commit ae85cf4
Copy full SHA for ae85cf4

File tree

Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.io.InputStreamReader;
3333
import java.io.Reader;
34+
import java.net.HttpURLConnection;
3435
import java.net.MalformedURLException;
3536
import java.net.URL;
3637
import java.text.ParseException;
@@ -482,7 +483,34 @@ void check(String apiUrl) throws IOException {
482483
* Otherwise this method throws {@link IOException} to indicate the problem.
483484
*/
484485
public void checkApiUrlValidity() throws IOException {
485-
retrieve().to("/", GHApiInfo.class).check(apiUrl);
486+
try {
487+
retrieve().to("/", GHApiInfo.class).check(apiUrl);
488+
} catch (IOException ioe) {
489+
if (isPrivateModeEnabled()) {
490+
throw new IOException("GitHub Enterprise server (" + apiUrl + ") with private mode enabled");
491+
}
492+
throw ioe;
493+
}
494+
}
495+
496+
/**
497+
* Ensures if a GitHub Enterprise server is configured in private mode.
498+
*
499+
* @return {@code true} if private mode is enabled. If it tries to use this method with GitHub, returns {@code
500+
* false}.
501+
*/
502+
private boolean isPrivateModeEnabled() {
503+
try {
504+
HttpURLConnection connect = getConnector().connect(getApiURL("/"));
505+
if (connect.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED
506+
&& connect.getHeaderField("Server") != null
507+
&& connect.getHeaderField("Server").equals("GitHub.com")) {
508+
return true;
509+
}
510+
return false;
511+
} catch (IOException e) {
512+
return false;
513+
}
486514
}
487515

488516
/**

‎src/test/java/org/kohsuke/github/GitHubTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GitHubTest.java
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException {
124124
@Test
125125
public void testGitHubIsApiUrlValid() throws IOException {
126126
GitHub github = GitHub.connectAnonymously();
127-
github.checkApiUrlValidity();
127+
//GitHub github = GitHub.connectToEnterpriseAnonymously("https://github.mycompany.com/api/v3/");
128+
try {
129+
github.checkApiUrlValidity();
130+
} catch (IOException ioe) {
131+
assertTrue(ioe.getMessage().contains("private mode enabled"));
132+
}
128133
}
129134
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.