From b05cadb17e87952955d25c7e91e1e03fa82758c9 Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Sun, 15 Nov 2015 16:59:46 +0100 Subject: [PATCH 1/4] [JENKINS-31462] GitHub Enterprise Servers validation --- pom.xml | 3 +- .../github_branch_source/Endpoint.java | 38 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 917f4bba9..a5483ce6a 100644 --- a/pom.xml +++ b/pom.xml @@ -36,10 +36,11 @@ git 2.3 + org.jenkins-ci.plugins github-api - 1.69 + 1.71-SNAPSHOT org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java index 27b9ac277..a70d80580 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java @@ -29,7 +29,12 @@ import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.util.FormValidation; -import java.util.regex.Pattern; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.kohsuke.github.GitHub; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; @@ -94,24 +99,27 @@ public String getDisplayName() { return ""; } - public FormValidation doCheckApiUrl(@QueryParameter String value) { - if (Util.fixEmptyAndTrim(value) == null) { - return FormValidation.error("You must specify the API URI"); + public FormValidation doCheckApiUri(@QueryParameter String apiUri) { + if (Util.fixEmptyAndTrim(apiUri) == null) { + return FormValidation.warning("You must specify the API URI"); } - Pattern enterprise = Pattern.compile("^https?://(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*" - + "([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])/api/v3/?$"); - if (!enterprise.matcher(value).matches()) { - return FormValidation.warning( - "This does not look like a GitHub Enterprise API URI. Expecting something like " - + "https://[hostname or ip]/api/v3/"); + try { + URL api = new URL(apiUri); + GitHub github = GitHub.connectToEnterpriseAnonymously(api.toString()); + if (github.isApiUrlValid()) { + return FormValidation.ok(); + } + return FormValidation.warning("This does not look like a GitHub Enterprise API URI"); + } catch (MalformedURLException mue) { + return FormValidation.error("This does not look like a GitHub Enterprise API URI"); + } catch (IOException e) { + return FormValidation.error(e.getMessage()); } - // TODO validate connection - return FormValidation.ok(); } - public FormValidation doCheckApiName(@QueryParameter String value) { - if (Util.fixEmptyAndTrim(value) == null) { - return FormValidation.warning("Specifying a name is recommended"); + public FormValidation doCheckName(@QueryParameter String name) { + if (Util.fixEmptyAndTrim(name) == null) { + return FormValidation.warning("You must specify the name"); } return FormValidation.ok(); } From 7bf7ae007abf7e22bca8f52060192e9880bd2dcb Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Tue, 17 Nov 2015 12:15:20 +0100 Subject: [PATCH 2/4] [JENKINS-31462] @oleg-nenashev's comments were addresed --- .../org/jenkinsci/plugins/github_branch_source/Endpoint.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java index a70d80580..3331d722d 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java @@ -34,6 +34,8 @@ import java.net.MalformedURLException; import java.net.URL; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.github.GitHub; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; @@ -99,6 +101,7 @@ public String getDisplayName() { return ""; } + @Restricted(NoExternalUse.class) public FormValidation doCheckApiUri(@QueryParameter String apiUri) { if (Util.fixEmptyAndTrim(apiUri) == null) { return FormValidation.warning("You must specify the API URI"); @@ -117,6 +120,7 @@ public FormValidation doCheckApiUri(@QueryParameter String apiUri) { } } + @Restricted(NoExternalUse.class) public FormValidation doCheckName(@QueryParameter String name) { if (Util.fixEmptyAndTrim(name) == null) { return FormValidation.warning("You must specify the name"); From c385eeda0852d8f74e19a0f50a1158a334d948b4 Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Thu, 3 Dec 2015 11:09:44 +0100 Subject: [PATCH 3/4] [JENKINS-31462] github-api has been upgrade to 1.71 --- pom.xml | 3 +-- .../plugins/github_branch_source/Endpoint.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index a5483ce6a..c964e33e0 100644 --- a/pom.xml +++ b/pom.xml @@ -36,11 +36,10 @@ git 2.3 - org.jenkins-ci.plugins github-api - 1.71-SNAPSHOT + 1.71 org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java index 3331d722d..94b9141fd 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java @@ -31,8 +31,9 @@ import hudson.util.FormValidation; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; +import java.util.logging.Logger; +import java.util.logging.Level; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -96,6 +97,8 @@ public int hashCode() { @Extension public static class DesciptorImpl extends Descriptor { + private static final Logger LOGGER = Logger.getLogger(DesciptorImpl.class.getName()); + @Override public String getDisplayName() { return ""; @@ -104,19 +107,16 @@ public String getDisplayName() { @Restricted(NoExternalUse.class) public FormValidation doCheckApiUri(@QueryParameter String apiUri) { if (Util.fixEmptyAndTrim(apiUri) == null) { - return FormValidation.warning("You must specify the API URI"); + return FormValidation.warning("You must specify the API URL"); } try { URL api = new URL(apiUri); GitHub github = GitHub.connectToEnterpriseAnonymously(api.toString()); - if (github.isApiUrlValid()) { - return FormValidation.ok(); - } - return FormValidation.warning("This does not look like a GitHub Enterprise API URI"); - } catch (MalformedURLException mue) { - return FormValidation.error("This does not look like a GitHub Enterprise API URI"); + github.checkApiUrlValidity(); + return FormValidation.ok(); } catch (IOException e) { - return FormValidation.error(e.getMessage()); + LOGGER.log(Level.WARNING, e.getMessage()); + return FormValidation.error("This does not look like a GitHub Enterprise API URL"); } } From 2fb4d479220f3b1c801b5a848332ca65bc9511d2 Mon Sep 17 00:00:00 2001 From: Manuel Recena Date: Thu, 3 Dec 2015 20:03:38 +0100 Subject: [PATCH 4/4] [JENKINS-31462] Catching JsonParseException to avoid weird log messages --- .../jenkinsci/plugins/github_branch_source/Endpoint.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java index 94b9141fd..46dbfae3a 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Endpoint.java @@ -24,6 +24,7 @@ package org.jenkinsci.plugins.github_branch_source; +import com.fasterxml.jackson.core.JsonParseException; import hudson.Extension; import hudson.Util; import hudson.model.AbstractDescribableImpl; @@ -31,6 +32,7 @@ import hudson.util.FormValidation; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Logger; import java.util.logging.Level; @@ -114,6 +116,10 @@ public FormValidation doCheckApiUri(@QueryParameter String apiUri) { GitHub github = GitHub.connectToEnterpriseAnonymously(api.toString()); github.checkApiUrlValidity(); return FormValidation.ok(); + } catch (MalformedURLException mue) { + return FormValidation.error("This does not look like a GitHub Enterprise API URL"); + } catch (JsonParseException jpe) { + return FormValidation.error("This does not look like a GitHub Enterprise API URL"); } catch (IOException e) { LOGGER.log(Level.WARNING, e.getMessage()); return FormValidation.error("This does not look like a GitHub Enterprise API URL");