Skip to content

Commit

Permalink
Merge pull request #19 from jenkinsci/bugfix/JENKINS-33963
Browse files Browse the repository at this point in the history
JENKINS-33963
  • Loading branch information
klimas7 committed Apr 2, 2016
2 parents 9a86765 + e4401dc commit d4585fa
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
@@ -1,28 +1,8 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.ParameterValue;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.model.AbstractProject;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.Revision;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -34,12 +14,25 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractProject;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.FetchCommand;
Expand All @@ -62,7 +55,7 @@ public class GitParameterDefinition extends ParameterDefinition implements Compa
private String type;
private String branch;
private String tagFilter;
private String branchfilter;
private String branchFilter;
private SortMode sortMode;
private String errorMessage;
private String defaultValue;
Expand All @@ -81,7 +74,7 @@ public GitParameterDefinition(String name, String type, String defaultValue, Str
this.quickFilterEnabled = quickFilterEnabled;

setTagFilter(tagFilter);
setBranchfilter(branchFilter);
setBranchFilter(branchFilter);
}

@Override
Expand Down Expand Up @@ -179,23 +172,22 @@ public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

public String getBranchfilter() {
return branchfilter;
public String getBranchFilter() {
return branchFilter;
}

public void setBranchfilter(String branchFilter) {
public void setBranchFilter(String branchFilter) {
if (StringUtils.isEmpty(StringUtils.trim(branchFilter))) {
branchFilter = "*";
branchFilter = ".*";
}

if (!"*".equals(branchFilter)) {
try {
Pattern.compile(branchFilter);
} catch (PatternSyntaxException e) {
LOGGER.log(Level.FINE, "Specified branchFilter is not a valid regex. Setting to '*'", e);
}
try {
Pattern.compile(branchFilter);
} catch (PatternSyntaxException e) {
LOGGER.log(Level.INFO, "Specified branchFilter is not a valid regex. Setting to '.*'", e.getMessage());
}
this.branchfilter = branchFilter;

this.branchFilter = branchFilter;
}

public Boolean getQuickFilterEnabled() {
Expand Down Expand Up @@ -288,7 +280,7 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC

LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch");
if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(newgit,branch);
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(newgit, branch);
List<RevisionInfo> revisions = revisionInfoFactory.getRevisions();

for (RevisionInfo revision : revisions) {
Expand All @@ -302,8 +294,9 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC

if (this.getSortMode().getIsSorting()) {
orderedTagNames = sortByName(tagSet);
if (this.getSortMode().getIsDescending())
if (this.getSortMode().getIsDescending()) {
Collections.reverse(orderedTagNames);
}
} else {
orderedTagNames = new ArrayList<String>(tagSet);
}
Expand All @@ -315,12 +308,17 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC
if (type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
time = -System.currentTimeMillis();
Set<String> branchSet = new HashSet<String>();
final boolean wildcard = "*".equals(branchfilter);
Pattern branchFilterPattern;
try {
branchFilterPattern = Pattern.compile(branchFilter);
} catch (PatternSyntaxException e) {
LOGGER.log(Level.INFO, "Specified branchFilter is not a valid regex. Setting to '.*'", e.getMessage());
errorMessage = "Specified branchFilter is not a valid regex. Setting to '.*'";
branchFilterPattern = Pattern.compile(".*");
}
for (Branch branch : newgit.getRemoteBranches()) {
// It'd be nice if we could filter on remote branches via the GitClient,
// but that's not an option.
final String branchName = branch.getName();
if (wildcard || branchName.matches(branchfilter)) {
String branchName = branch.getName();
if (branchFilterPattern.matcher(branchName).matches()) {
branchSet.add(branchName);
}
}
Expand Down Expand Up @@ -420,13 +418,11 @@ public GitSCM getProjectSCM(AbstractProject<?, ?> project) {
return null;
}

public FormValidation doCheckBranchfilter(@QueryParameter String value) {
if (!"*".equals(value)) {
try {
Pattern.compile(value); // Validate we've got a valid regex.
} catch (PatternSyntaxException e) {
return FormValidation.error("The pattern '" + value + "' does not appear to be valid: " + e.getMessage());
}
public FormValidation doCheckBranchFilter(@QueryParameter String value) {
try {
Pattern.compile(value); // Validate we've got a valid regex.
} catch (PatternSyntaxException e) {
return FormValidation.error("The pattern '" + value + "' does not appear to be valid: " + e.getMessage());
}
return FormValidation.ok();
}
Expand Down
Expand Up @@ -46,7 +46,7 @@
<f:textbox />
</f:entry>

<f:entry title="Branch filter" field="branchfilter">
<f:entry title="Branch filter" field="branchFilter">
<f:textbox />
</f:entry>

Expand Down
@@ -1,4 +1,4 @@
<div>
Regex used to filter displayed branches. If blank, the filter will default to "*".<br />
Regex used to filter displayed branches. If blank, the filter will default to ".*".<br />
Remote branches will be listed with the remote name first. E.g., "origin/master"
</div>
@@ -1,5 +1,5 @@
<div>
Select the following sorting options for tags/revision/branches/branches_or_tags
Select the following sorting options for tags/branches/branches_or_tags
<ul>
<li>none</li>
<li>descending</li>
Expand Down
Expand Up @@ -26,8 +26,6 @@
import hudson.util.ListBoxModel;
import net.sf.json.JSONObject;
import net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition.DescriptorImpl;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand Down Expand Up @@ -65,28 +63,28 @@ public void testCreateValue_StaplerRequest() {
public void testConstructorInitializesTagFilterToAsteriskWhenNull() {
GitParameterDefinition instance = new GitParameterDefinition("name","PT_REVISION","defaultValue","description","branch",null,null, SortMode.NONE,false);
assertEquals("*", instance.getTagFilter());
assertEquals("*", instance.getBranchfilter());
assertEquals(".*", instance.getBranchFilter());
}

@Test
public void testConstructorInitializesTagFilterToAsteriskWhenWhitespace() {
GitParameterDefinition instance = new GitParameterDefinition("name","PT_REVISION","defaultValue","description","branch"," "," ", SortMode.NONE,false);
assertEquals("*", instance.getTagFilter());
assertEquals("*", instance.getBranchfilter());
assertEquals(".*", instance.getBranchFilter());
}

@Test
public void testConstructorInitializesTagFilterToAsteriskWhenEmpty() {
GitParameterDefinition instance = new GitParameterDefinition("name","PT_REVISION","defaultValue","description","branch","","", SortMode.NONE,false);
assertEquals("*", instance.getTagFilter());
assertEquals("*", instance.getBranchfilter());
assertEquals(".*", instance.getBranchFilter());
}

@Test
public void testConstructorInitializesTagToGivenValueWhenNotNullOrWhitespace() {
GitParameterDefinition instance = new GitParameterDefinition("name","PT_REVISION","defaultValue","description","branch","foobar","foobar", SortMode.NONE,false);
assertEquals("foobar", instance.getTagFilter());
assertEquals("foobar", instance.getBranchfilter());
assertEquals("foobar", instance.getBranchFilter());
}


Expand Down Expand Up @@ -466,11 +464,9 @@ public void testSearchInFolders() throws Exception {
@Test
public void testBranchFilterValidation() {
final DescriptorImpl descriptor = new DescriptorImpl();
final FormValidation okPsuedoWildcard = descriptor.doCheckBranchfilter("*");
final FormValidation okWildcard = descriptor.doCheckBranchfilter("*");
final FormValidation badWildcard = descriptor.doCheckBranchfilter(".**");
final FormValidation okWildcard = descriptor.doCheckBranchFilter(".*");
final FormValidation badWildcard = descriptor.doCheckBranchFilter(".**");

assertTrue(okPsuedoWildcard.kind == Kind.OK);
assertTrue(okWildcard.kind == Kind.OK);
assertTrue(badWildcard.kind == Kind.ERROR);
}
Expand Down

0 comments on commit d4585fa

Please sign in to comment.