Skip to content

Commit

Permalink
fixed issues with regex matching
Browse files Browse the repository at this point in the history
fixed error if build name has suffix after revision number
  • Loading branch information
nithint committed Dec 29, 2010
1 parent a9de579 commit ccd4833
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.389</version><!-- which version of Hudson is this plugin built against? -->
<version>1.390</version><!-- which version of Hudson is this plugin built against? -->
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -12,7 +12,7 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>hpi</packaging>

<!-- get every artifact through maven.glassfish.org, which proxies all the artifacts that we need -->
<!-- get every artifact through maven.glassfish.org, which proxies all the artifacts that we need
<repositories>
<repository>
<id>m.g.o-public</id>
Expand All @@ -26,7 +26,7 @@
<url>http://maven.glassfish.org/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>

-->
<licenses>
<license>
<name>MIT license</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package org.jvnet.hudson.tools.majorminor;

import hudson.Extension;
Expand All @@ -9,20 +6,11 @@
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.Descriptor.FormException;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tasks.Builder;
import hudson.tasks.BuildWrapper.Environment;
import hudson.util.FormValidation;

import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -33,7 +21,6 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

/**
*
* Changes the build name so that it follows a major.minor.revision format. For
Expand All @@ -50,6 +37,7 @@ public class MajorMinorBuilder extends BuildWrapper
{

private static final String REGEX_NO_MATCH_ERROR = "[MajorMinor Plugin] Previous build name doesn't match Build Name Regex.";
private static final String LOG_NOTE = "[MajorMinor Plugin] Build Name Changed to %1s.";
public static final String DEFAULT_BUILD_NAME_REGEX = "\\d+\\.\\d+\\.(\\d+)";
public static final String DEFAULT_FIRST_BUILD_NAME = "0.0.00";
private static final String SVN_REVISION_FIELD = "SVN_REVISION";
Expand All @@ -66,25 +54,19 @@ public class MajorMinorBuilder extends BuildWrapper
*/
private final String firstBuildName;

/**
* {@link MajorMinorBuilder#buildNameRegex as a {@link Pattern} object
*/
private final Pattern pattern;

@DataBoundConstructor
public MajorMinorBuilder(String buildNameRegex, String firstBuildName)
{
if (buildNameRegex == null || buildNameRegex.isEmpty())
this.buildNameRegex = DEFAULT_BUILD_NAME_REGEX;
else
{
this.buildNameRegex = buildNameRegex;

}
if (firstBuildName == null || firstBuildName.isEmpty())
this.firstBuildName = firstBuildName;
this.firstBuildName = DEFAULT_FIRST_BUILD_NAME;
else
this.firstBuildName = firstBuildName;

this.pattern = Pattern.compile(buildNameRegex);
}

public String getBuildNameRegex()
Expand Down Expand Up @@ -135,13 +117,16 @@ public Environment setUp(AbstractBuild build, Launcher launcher,
}

String nextBuildName = "";
Matcher m = pattern.matcher(prevBuildName);
Matcher m = Pattern.compile(this.getBuildNameRegex()).matcher(prevBuildName);
if (m.matches() && m.group(1) != null)
{
nextBuildName = prevBuildName.substring(0,
m.start(1))
+ revision
+ prevBuildName.substring(m.end(1) + 1);
+ revision;

// add any suffix if it exists
if(m.end(1)+1 < prevBuildName.length())
nextBuildName += prevBuildName.substring(m.end(1) + 1);
} else
{
// build name doesn't match pattern. error
Expand All @@ -155,8 +140,10 @@ public Environment setUp(AbstractBuild build, Launcher launcher,
final String newBuildName = nextBuildName;

//set build name (only available since Hudson v1.390)
//build.setDisplayName(newBuildName);
build.setDisplayName(newBuildName);

// add a note to the log that name was changed
listener.getLogger().println(String.format(LOG_NOTE, newBuildName));
return new Environment()
{
@Override
Expand Down Expand Up @@ -231,7 +218,9 @@ public FormValidation doCheckBuildNameRegex(
} catch (PatternSyntaxException e)
{
return FormValidation
.error("The given expression is not a valid pattern");
.error("The given expression is not a valid pattern. Syntax Error: " + e.getMessage() +
"Description: " + e.getDescription() +
"Error index: " + e.getIndex());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/help-buildNameRegex.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
place since last build, then the previous build name is kept and so you'll now have two builds with
the same build name.
<br />
<em>If this field is left empty, the default value <b>\\d+\\.\\d+\\.(\\d+)</b> is used</em>
<em>If this field is left empty, the default value <b>\d+\.\d+\.(\d+)</b> is used</em>
</p>
</div>

0 comments on commit ccd4833

Please sign in to comment.