Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #12 from recena/master
Browse files Browse the repository at this point in the history
[JENKINS-31462] GitHub Enterprise Servers validation
  • Loading branch information
recena committed Dec 4, 2015
2 parents 8aefbc7 + 2fb4d47 commit 384e7a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.69</version>
<version>1.71</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -24,12 +24,22 @@

package org.jenkinsci.plugins.github_branch_source;

import com.fasterxml.jackson.core.JsonParseException;
import hudson.Extension;
import hudson.Util;
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 java.util.logging.Logger;
import java.util.logging.Level;

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;

Expand Down Expand Up @@ -89,29 +99,37 @@ public int hashCode() {
@Extension
public static class DesciptorImpl extends Descriptor<Endpoint> {

private static final Logger LOGGER = Logger.getLogger(DesciptorImpl.class.getName());

@Override
public String getDisplayName() {
return "";
}

public FormValidation doCheckApiUrl(@QueryParameter String value) {
if (Util.fixEmptyAndTrim(value) == null) {
return FormValidation.error("You must specify the API URI");
@Restricted(NoExternalUse.class)
public FormValidation doCheckApiUri(@QueryParameter String apiUri) {
if (Util.fixEmptyAndTrim(apiUri) == null) {
return FormValidation.warning("You must specify the API URL");
}
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());
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");
}
// 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");
@Restricted(NoExternalUse.class)
public FormValidation doCheckName(@QueryParameter String name) {
if (Util.fixEmptyAndTrim(name) == null) {
return FormValidation.warning("You must specify the name");
}
return FormValidation.ok();
}
Expand Down

0 comments on commit 384e7a9

Please sign in to comment.