diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 2eaef53ceb..2ef822680c 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -200,6 +200,15 @@ public static GitHub connectAnonymously() throws IOException { return new GitHubBuilder().build(); } + /** + * Connects to GitHub Enterprise anonymously. + * + * All operations that requires authentication will fail. + */ + public static GitHub connectToEnterpriseAnonymously(String apiUrl) throws IOException { + return new GitHubBuilder().withEndpoint(apiUrl).build(); + } + /** * Is this an anonymous connection * @return {@code true} if operations that require authentication will fail. @@ -442,6 +451,29 @@ public boolean isCredentialValid() throws IOException { } } + private static class GHApiInfo { + private String rate_limit_url; + + void check(String apiUrl) throws IOException { + if (rate_limit_url==null) + throw new IOException(apiUrl+" doesn't look like GitHub API URL"); + + // make sure that the URL is legitimate + new URL(rate_limit_url); + } + } + + /** + * Ensures that the API URL is valid. + * + *

+ * This method returns normally if the endpoint is reachable and verified to be GitHub API URL. + * Otherwise this method throws {@link IOException} to indicate the problem. + */ + public void checkApiUrlValidity() throws IOException { + retrieve().to("/", GHApiInfo.class).check(apiUrl); + } + /** * Search issues. */ diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 54ce6e35d7..1663d24144 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -12,6 +12,7 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -119,4 +120,10 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException { GHRateLimit rateLimit = github.getRateLimit(); assertThat(rateLimit.getResetDate(), notNullValue()); } + + @Test + public void testGitHubIsApiUrlValid() throws IOException { + GitHub github = GitHub.connectAnonymously(); + github.checkApiUrlValidity(); + } }