Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-31462] GitHub Enterprise Servers validation #12

Merged
merged 4 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use catch (MalformedURLException | JsonParseException e) and avoid the duplicated message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but we are not using this feature of Java SE 7 in this plugin.

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