Skip to content

Commit

Permalink
[FIXED JENKINS-9971] Fix NPE when validating remote for publish.
Browse files Browse the repository at this point in the history
Add new validation to GitPublisher that checks the remote name
is a valid repository name.

Also update validatation in GitSCM to response null repo name
and url parameter values.
  • Loading branch information
kevinsawicki committed Jun 14, 2011
1 parent 6513580 commit 61bd032
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
28 changes: 28 additions & 0 deletions src/main/java/hudson/plugins/git/GitPublisher.java
Expand Up @@ -28,12 +28,15 @@
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;


import org.apache.commons.lang.StringUtils;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import org.eclipse.jgit.transport.RemoteConfig;

Expand Down Expand Up @@ -402,6 +405,31 @@ public FormValidation doCheckTagName(@QueryParameter String value) {
public FormValidation doCheckBranchName(@QueryParameter String value) {
return checkFieldNotEmpty(value, "Branch Name");
}

public FormValidation doCheckRemote(
@AncestorInPath AbstractProject project, StaplerRequest req)
throws IOException, ServletException {
String remote = req.getParameter("value");
boolean isMerge = req.getParameter("isMerge") != null;

// Added isMerge because we don't want to allow empty remote names
// for tag/branch pushes.
if (remote.length() == 0 && isMerge)
return FormValidation.ok();

FormValidation validation = checkFieldNotEmpty(remote,
"Remote Name");
if (validation.kind != FormValidation.Kind.OK)
return validation;

GitSCM scm = (GitSCM) project.getScm();
if (scm.getRepositoryByName(remote) == null)
return FormValidation
.error("No remote repository configured with name '"
+ remote + "'");

return FormValidation.ok();
}

public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -1528,14 +1528,10 @@ public FormValidation doGitRemoteNameCheck(StaplerRequest req, StaplerResponse r

String[] urls = req.getParameterValues("repo.url");
String[] names = req.getParameterValues("repo.name");

names = GitUtils.fixupNames(names, urls);

for (String name : names) {
if (name.equals(mergeRemoteName)) {
return FormValidation.ok();
}
}
if (urls != null && names != null)
for (String name : GitUtils.fixupNames(names, urls))
if (name.equals(mergeRemoteName))
return FormValidation.ok();

return FormValidation.error("No remote repository configured with name '" + mergeRemoteName + "'");
}
Expand Down
Expand Up @@ -41,9 +41,7 @@
<f:entry field="targetRepoName"
title="${%Target remote name}">
<f:textbox
checkUrl="'${rootURL}/scm/GitSCM/gitRemoteNameCheck?value='+escape(this.value)
+encodeAllInputs('&amp;', this.form, 'git.repo.name')
+encodeAllInputs('&amp;', this.form, 'git.repo.url')"/>
checkUrl="'descriptorByName/GitPublisher/checkRemote?value='+escape(this.value)"/>
</f:entry>
</table>
<div align="right">
Expand All @@ -65,9 +63,7 @@
<f:entry field="targetRepoName"
title="${%Target remote name}">
<f:textbox
checkUrl="'${rootURL}/scm/GitSCM/gitRemoteNameCheck?value='+escape(this.value)
+encodeAllInputs('&amp;', this.form, 'git.repo.name')
+encodeAllInputs('&amp;', this.form, 'git.repo.url')"/>
checkUrl="'descriptorByName/GitPublisher/checkRemote?value='+escape(this.value)" />
</f:entry>
</table>
<div align="right">
Expand Down

0 comments on commit 61bd032

Please sign in to comment.