From a371892409621e50a9d9c5c11d8045cfc1396d34 Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Sun, 15 Nov 2015 16:36:27 +0100 Subject: [PATCH 1/3] Added a new method to validate the GitHub API URL --- pom.xml | 3 +-- .../java/org/kohsuke/github/GHApiInfo.java | 14 ++++++++++ src/main/java/org/kohsuke/github/GitHub.java | 26 +++++++++++++++++++ .../java/org/kohsuke/github/GitHubTest.java | 7 +++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHApiInfo.java diff --git a/pom.xml b/pom.xml index 60299712b2..02b788b6a4 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ UTF-8 - 3.0.1 + 3.0.2 true @@ -52,7 +52,6 @@ ${findbugs-maven-plugin.version} true - true ${findbugs-maven-plugin.failOnError} diff --git a/src/main/java/org/kohsuke/github/GHApiInfo.java b/src/main/java/org/kohsuke/github/GHApiInfo.java new file mode 100644 index 0000000000..131e0de647 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHApiInfo.java @@ -0,0 +1,14 @@ +package org.kohsuke.github; + +/** + * Represents information about Github API. + */ +public class GHApiInfo { + + private String rate_limit_url; + + public String getRateLimitUrl() { + return rate_limit_url; + } + +} diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 9e638bb4aa..1ca5ed968e 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -204,6 +204,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. @@ -446,6 +455,23 @@ public boolean isCredentialValid() throws IOException { } } + /** + * Ensures that the API URL is valid. + */ + public boolean isApiUrlValid() throws IOException { + try { + GHApiInfo apiInfo = retrieve().to("/", GHApiInfo.class); + try { + new URL(apiInfo.getRateLimitUrl()); + return true; + } catch (MalformedURLException mue) { + return false; + } + } catch (IOException e) { + return false; + } + } + /** * Search issues. */ diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 54ce6e35d7..5c5ee982fe 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(); + assertTrue(github.isApiUrlValid()); + } } From acbafee02afcb4f46f6f379c2dbe65742c140c63 Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Mon, 23 Nov 2015 10:25:41 +0100 Subject: [PATCH 2/3] Removed unrelated changes. @KostyaSha's suggestion --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 02b788b6a4..60299712b2 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ UTF-8 - 3.0.2 + 3.0.1 true @@ -52,6 +52,7 @@ ${findbugs-maven-plugin.version} true + true ${findbugs-maven-plugin.failOnError} From 0f45d03c51975eac7c96731537a1cffc78cf6dcc Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 1 Dec 2015 16:01:01 +0100 Subject: [PATCH 3/3] Reworked this change a bit. - GHApiInfo need not be public because it's not publicly exposed. - Throwing an exception is better IMO as it allows richer error message, including the differentiation between unreachable host name vs wrong URL, and reporting the API endpoint URL that was actually tried. --- .../java/org/kohsuke/github/GHApiInfo.java | 14 --------- src/main/java/org/kohsuke/github/GitHub.java | 30 +++++++++++-------- .../java/org/kohsuke/github/GitHubTest.java | 2 +- 3 files changed, 19 insertions(+), 27 deletions(-) delete mode 100644 src/main/java/org/kohsuke/github/GHApiInfo.java diff --git a/src/main/java/org/kohsuke/github/GHApiInfo.java b/src/main/java/org/kohsuke/github/GHApiInfo.java deleted file mode 100644 index 131e0de647..0000000000 --- a/src/main/java/org/kohsuke/github/GHApiInfo.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.kohsuke.github; - -/** - * Represents information about Github API. - */ -public class GHApiInfo { - - private String rate_limit_url; - - public String getRateLimitUrl() { - return rate_limit_url; - } - -} diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 1ca5ed968e..6b434fe2a5 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -455,21 +455,27 @@ 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 boolean isApiUrlValid() throws IOException { - try { - GHApiInfo apiInfo = retrieve().to("/", GHApiInfo.class); - try { - new URL(apiInfo.getRateLimitUrl()); - return true; - } catch (MalformedURLException mue) { - return false; - } - } catch (IOException e) { - return false; - } + public void checkApiUrlValidity() throws IOException { + retrieve().to("/", GHApiInfo.class).check(apiUrl); } /** diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 5c5ee982fe..1663d24144 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -124,6 +124,6 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException { @Test public void testGitHubIsApiUrlValid() throws IOException { GitHub github = GitHub.connectAnonymously(); - assertTrue(github.isApiUrlValid()); + github.checkApiUrlValidity(); } }