-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[JENKINS-48625] Restore binding of doCheckUrl methods and add some initial checks #841
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
Changes from 11 commits
2510bd3
7d7f670
58320c4
503e0a3
f4b0710
f99b832
a062e0b
c04b4a7
3a8d549
f298249
7293a3f
141c79f
454397e
6532b34
4a0915a
5f48308
ac1beeb
8b1433c
acebb46
d9fe71b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| package hudson.plugins.git.browser; | ||
|
|
||
| import hudson.Extension; | ||
| import hudson.Util; | ||
| import hudson.model.Descriptor; | ||
| import hudson.model.Item; | ||
| import hudson.plugins.git.GitChangeSet; | ||
| import hudson.plugins.git.GitChangeSet.Path; | ||
| import hudson.plugins.git.Messages; | ||
| import hudson.scm.EditType; | ||
| import hudson.scm.RepositoryBrowser; | ||
| import hudson.util.FormValidation; | ||
| import hudson.util.FormValidation.URLCheck; | ||
| import jenkins.model.Jenkins; | ||
| import net.sf.json.JSONObject; | ||
| import org.kohsuke.stapler.AncestorInPath; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
| import org.kohsuke.stapler.interceptor.RequirePOST; | ||
| import org.kohsuke.stapler.QueryParameter; | ||
|
|
@@ -19,6 +22,7 @@ | |
| import javax.servlet.ServletException; | ||
| import java.io.IOException; | ||
| import java.io.UnsupportedEncodingException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.net.URLEncoder; | ||
|
|
||
|
|
@@ -63,9 +67,10 @@ public String getProjectName() { | |
| return projectName; | ||
| } | ||
|
|
||
| private String encodeString(final String s) throws UnsupportedEncodingException { | ||
| private String encodeString(final String s) throws UnsupportedEncodingException { | ||
| return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20"); | ||
| } | ||
|
|
||
| @Extension | ||
| public static class ViewGitWebDescriptor extends Descriptor<RepositoryBrowser<?>> { | ||
| @Nonnull | ||
|
|
@@ -80,33 +85,47 @@ public GitBlitRepositoryBrowser newInstance(StaplerRequest req, @Nonnull JSONObj | |
| } | ||
|
|
||
| @RequirePOST | ||
| public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url) | ||
| throws IOException, ServletException { | ||
| if (url == null) // nothing entered yet | ||
| { | ||
| public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl) | ||
| throws IOException, ServletException, URISyntaxException { | ||
|
|
||
| String cleanUrl = Util.fixEmptyAndTrim(repoUrl); | ||
|
|
||
| if (cleanUrl == null) { | ||
| return FormValidation.ok(); | ||
| } | ||
| // Connect to URL and check content only if we have admin permission | ||
| if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) | ||
|
|
||
| if (project == null || !project.hasPermission(Item.CONFIGURE)) { | ||
| return FormValidation.ok(); | ||
| return new URLCheck() { | ||
| protected FormValidation check() throws IOException, ServletException { | ||
| String v = url; | ||
| if (!v.endsWith("/")) { | ||
| v += '/'; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| if (findText(open(new URL(v)), "Gitblit")) { | ||
| return FormValidation.ok(); | ||
| } else { | ||
| return FormValidation.error("This is a valid URL but it doesn't look like Gitblit"); | ||
| if (cleanUrl.contains("$")) { | ||
| // set by variable, can't validate | ||
| return FormValidation.ok(); | ||
| } | ||
| FormValidation response; | ||
| if (checkURIFormat(cleanUrl, "gitblit")) { | ||
|
||
| return new URLCheck() { | ||
| protected FormValidation check() throws IOException, ServletException { | ||
| String v = cleanUrl; | ||
| if (!v.endsWith("/")) { | ||
| v += '/'; | ||
| } | ||
rishabhBudhouliya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| if (findText(open(new URL(v)), "Gitblit")) { | ||
| return FormValidation.ok(); | ||
| } else { | ||
| return FormValidation.error("This is a valid URL but it doesn't look like Gitblit"); | ||
| } | ||
| } catch (IOException e) { | ||
| return handleIOException(v, e); | ||
| } | ||
| } catch (IOException e) { | ||
| return handleIOException(v, e); | ||
| } | ||
| } | ||
| }.check(); | ||
| }.check(); | ||
| } else { | ||
| response = FormValidation.error(Messages.invalidUrl()); | ||
| } | ||
| return response; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,26 @@ | ||
| package hudson.plugins.git.browser; | ||
|
|
||
| import hudson.Extension; | ||
| import hudson.Util; | ||
| import hudson.model.Descriptor; | ||
| import hudson.model.Item; | ||
| import hudson.plugins.git.GitChangeSet; | ||
| import hudson.plugins.git.GitChangeSet.Path; | ||
| import hudson.plugins.git.Messages; | ||
| import hudson.scm.RepositoryBrowser; | ||
| import hudson.util.FormValidation; | ||
| import hudson.util.FormValidation.URLCheck; | ||
|
|
||
| import jenkins.model.Jenkins; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.servlet.ServletException; | ||
|
|
||
| import net.sf.json.JSONObject; | ||
|
|
||
| import org.kohsuke.stapler.AncestorInPath; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
| import org.kohsuke.stapler.interceptor.RequirePOST; | ||
| import org.kohsuke.stapler.QueryParameter; | ||
|
|
@@ -70,30 +73,37 @@ public Gitiles newInstance(StaplerRequest req, @Nonnull JSONObject jsonObject) t | |
| } | ||
|
|
||
| @RequirePOST | ||
| public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url) throws IOException, ServletException { | ||
| if (url == null) // nothing entered yet | ||
| return FormValidation.ok(); | ||
| // Connect to URL and check content only if we have admin permission | ||
| if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) | ||
| public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl) | ||
| throws IOException, ServletException, URISyntaxException { | ||
|
|
||
| String cleanUrl = Util.fixEmptyAndTrim(repoUrl); | ||
| if(initialChecksAndReturnOk(project, cleanUrl)){ | ||
| return FormValidation.ok(); | ||
| return new URLCheck() { | ||
| protected FormValidation check() throws IOException, ServletException { | ||
| String v = url; | ||
| if (!v.endsWith("/")) | ||
| v += '/'; | ||
|
|
||
| try { | ||
| // gitiles has a line in main page indicating how to clone the project | ||
| if (findText(open(new URL(v)), "git clone")) { | ||
| return FormValidation.ok(); | ||
| } else { | ||
| return FormValidation.error("This is a valid URL but it doesn't look like Gitiles"); | ||
| } | ||
| FormValidation response; | ||
| if (checkURIFormat(cleanUrl, "gerrit")) { | ||
|
||
| return new URLCheck() { | ||
| protected FormValidation check() throws IOException, ServletException { | ||
| String v = cleanUrl; | ||
| if (!v.endsWith("/")) { | ||
| v += '/'; | ||
| } | ||
|
|
||
| try { | ||
| if (findText(open(new URL(v)), "git clone")) { | ||
| return FormValidation.ok(); | ||
| } else { | ||
| return FormValidation.error("This is a valid URL but it doesn't look like Gitiles"); | ||
| } | ||
| } catch (IOException e) { | ||
| return handleIOException(v, e); | ||
| } | ||
| } catch (IOException e) { | ||
| return handleIOException(v, e); | ||
| } | ||
| } | ||
| }.check(); | ||
| }.check(); | ||
| } else { | ||
| response = FormValidation.error(Messages.invalidUrl()); | ||
| } | ||
| return response; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.