Skip to content

Commit

Permalink
[JENKINS-18171] Allow more fine-grained skipping of version-number.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bagira80 committed Nov 17, 2017
1 parent 2cc0ccb commit 668c9e6
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 57 deletions.
Expand Up @@ -10,7 +10,7 @@
public abstract class AbstractBuildNumberGenerator implements BuildNumberGenerator {

@Override
public int getNextNumber(Run build, EnvVars vars, Run prevBuild, boolean skipFailedBuilds, String override) {
public int getNextNumber(Run build, EnvVars vars, Run prevBuild, Result worstResultForIncrement, String override) {
int nextNumber = 1;

// Attempt an override
Expand All @@ -19,13 +19,11 @@ public int getNextNumber(Run build, EnvVars vars, Run prevBuild, boolean skipFai
// If no override, start from the previous build
} else if (prevBuild != null) {
int increment = 1;
// if we're skipping version numbers on failed builds and the last build failed...
if (skipFailedBuilds) {
Result result = prevBuild.getResult();
if (result != null && ! result.equals(Result.SUCCESS)) {
// don't increment
increment = 0;
}
// we're skipping version numbers if the last build's result was worse than required...
Result result = prevBuild.getResult();
if (result != null && result.isWorseThan(worstResultForIncrement)) {
// don't increment
increment = 0;
}

nextNumber = resolveValue(build, prevBuild, increment);
Expand Down
@@ -1,11 +1,12 @@
package org.jvnet.hudson.tools.versionnumber;

import hudson.EnvVars;
import hudson.model.Result;
import hudson.model.Run;

public interface BuildNumberGenerator {

int getNextNumber(Run build, EnvVars vars, Run prevBuild, boolean skipFailedBuilds, String override);
int getNextNumber(Run build, EnvVars vars, Run prevBuild, Result worstResultForIncrement, String override);

int resolveValue(Run build, Run previousBuild, int increment);

Expand Down
Expand Up @@ -25,6 +25,7 @@
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tasks.Builder;
import hudson.util.ListBoxModel;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;

Expand Down Expand Up @@ -58,8 +59,6 @@ public class VersionNumberBuilder extends BuildWrapper {
/** Use Java 7 MethodHandles to get my class for logger. */
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());

private static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";

private final String versionNumberString;
private final Date projectStartDate;
private final String environmentVariableName;
Expand All @@ -71,7 +70,9 @@ public class VersionNumberBuilder extends BuildWrapper {
private String oBuildsThisYear;
private String oBuildsAllTime;

private boolean skipFailedBuilds;
private String worstResultForIncrement = null;
@Deprecated
private boolean skipFailedBuilds = false;
private boolean useAsBuildDisplayName;

public VersionNumberBuilder(String versionNumberString,
Expand All @@ -86,7 +87,27 @@ public VersionNumberBuilder(String versionNumberString,
boolean skipFailedBuilds) {
this(versionNumberString, projectStartDate, environmentVariableName,
environmentPrefixVariable, buildsToday, buildsThisWeek, buildsThisMonth,
buildsThisYear, buildsAllTime, skipFailedBuilds, false);
buildsThisYear, buildsAllTime,
(skipFailedBuilds) ? VersionNumberCommon.WORST_RESULT_SUCCESS : VersionNumberCommon.WORST_RESULT_NOT_BUILT,
false);
}

public VersionNumberBuilder(String versionNumberString,
String projectStartDate,
String environmentVariableName,
String environmentPrefixVariable,
String buildsToday,
String buildsThisWeek,
String buildsThisMonth,
String buildsThisYear,
String buildsAllTime,
boolean skipFailedBuilds,
boolean useAsBuildDisplayName) {
this(versionNumberString, projectStartDate, environmentVariableName,
environmentPrefixVariable, buildsToday, buildsThisWeek, buildsThisMonth,
buildsThisYear, buildsAllTime,
(skipFailedBuilds) ? VersionNumberCommon.WORST_RESULT_SUCCESS : VersionNumberCommon.WORST_RESULT_NOT_BUILT,
useAsBuildDisplayName);
}

@DataBoundConstructor
Expand All @@ -99,13 +120,13 @@ public VersionNumberBuilder(String versionNumberString,
String buildsThisMonth,
String buildsThisYear,
String buildsAllTime,
boolean skipFailedBuilds,
String worstResultForIncrement,
boolean useAsBuildDisplayName) {
this.versionNumberString = versionNumberString;
this.projectStartDate = VersionNumberCommon.parseDate(projectStartDate);
this.environmentVariableName = environmentVariableName;
this.environmentPrefixVariable = environmentPrefixVariable;
this.skipFailedBuilds = skipFailedBuilds;
this.worstResultForIncrement = worstResultForIncrement;
this.useAsBuildDisplayName = useAsBuildDisplayName;

this.oBuildsToday = VersionNumberCommon.makeValid(buildsToday);
Expand Down Expand Up @@ -135,8 +156,31 @@ public String getBuildsAllTime() {
return this.oBuildsAllTime;
}

public boolean getSkipFailedBuilds() {
return this.skipFailedBuilds;
public Result getWorstResultForIncrement() {
// For compatibility-reasons during transition from old plugin (<= 1.8.1) to newer plugin.
if (this.skipFailedBuilds) {
LOGGER.warning("At least in one project VersionNumber plugin still uses the old config-variable 'skipFailedBuilds'. Make sure to update and safe the job-configs to update that behavior.");
this.skipFailedBuilds = false;
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_SUCCESS;
}
if (this.worstResultForIncrement == null) {
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_NOT_BUILT;
}
switch (this.worstResultForIncrement) {
case VersionNumberCommon.WORST_RESULT_NOT_BUILT:
return Result.NOT_BUILT;
case VersionNumberCommon.WORST_RESULT_ABORTED:
return Result.ABORTED;
case VersionNumberCommon.WORST_RESULT_FAILURE:
return Result.FAILURE;
case VersionNumberCommon.WORST_RESULT_UNSTABLE:
return Result.UNSTABLE;
case VersionNumberCommon.WORST_RESULT_SUCCESS:
return Result.SUCCESS;
default:
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_SUCCESS;
return Result.SUCCESS;
}
}

public boolean getUseAsBuildDisplayName() {
Expand All @@ -151,7 +195,7 @@ public String getVersionNumberString() {
}

public String getProjectStartDate() {
final DateFormat defaultDateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT_PATTERN);
final DateFormat defaultDateFormat = new SimpleDateFormat(VersionNumberCommon.DEFAULT_DATE_FORMAT_PATTERN);
return defaultDateFormat.format(projectStartDate);
}
public String getEnvironmentVariableName() {
Expand Down Expand Up @@ -194,7 +238,7 @@ private VersionNumberBuildInfo incBuild(Run build, BuildListener listener) throw
EnvVars enVars = build.getEnvironment(listener);
Run prevBuild = getPreviousBuildWithVersionNumber(build, listener);
VersionNumberBuildInfo incBuildInfo = VersionNumberCommon.incBuild(build, enVars, prevBuild,
this.skipFailedBuilds,
this.getWorstResultForIncrement(),
this.oBuildsToday,
this.oBuildsThisWeek,
this.oBuildsThisMonth,
Expand Down Expand Up @@ -325,7 +369,7 @@ public DescriptorImpl() {
}

/**
* Performs on-the-fly validation of the form field 'name'.
* Performs on-the-fly validation of the form field 'environmentVariableName'.
*
* @param value
* This receives the current value of the field.
Expand All @@ -338,7 +382,7 @@ public FormValidation doCheckEnvironmentVariableName(@QueryParameter final Strin
}

/**
* Performs on-the-fly validation of the form field 'name'.
* Performs on-the-fly validation of the form field 'versionNumberString'.
*
* @param value
* This receives the current value of the field.
Expand All @@ -354,7 +398,7 @@ public FormValidation doCheckVersionNumberString(@QueryParameter final String va
}

/**
* Performs on-the-fly validation of the form field 'name'.
* Performs on-the-fly validation of the form field 'projectStartDate'.
*
* @param value
* This receives the current value of the field.
Expand All @@ -366,7 +410,17 @@ public FormValidation doCheckProjectStartDate(@QueryParameter final String value
return FormValidation.ok();
}
}


public ListBoxModel doFillWorstResultForIncrementItems() {
ListBoxModel items = new ListBoxModel();
items.add(VersionNumberCommon.WORST_RESULT_SUCCESS);
items.add(VersionNumberCommon.WORST_RESULT_UNSTABLE);
items.add(VersionNumberCommon.WORST_RESULT_FAILURE);
items.add(VersionNumberCommon.WORST_RESULT_ABORTED);
items.add(VersionNumberCommon.WORST_RESULT_NOT_BUILT);
return items;
}

/**
* This human readable name is used in the configuration screen.
*/
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.lang.invoke.MethodHandles;

import hudson.EnvVars;
import hudson.model.Result;
import hudson.model.Run;

/**
Expand All @@ -19,20 +20,26 @@ public class VersionNumberCommon {
/** Use Java 7 MethodHandles to get my class for logger. */
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());

private static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
public static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";

public static final String WORST_RESULT_SUCCESS = "SUCCESS";
public static final String WORST_RESULT_UNSTABLE = "UNSTABLE";
public static final String WORST_RESULT_FAILURE = "FAILURE";
public static final String WORST_RESULT_ABORTED = "ABORTED";
public static final String WORST_RESULT_NOT_BUILT = "NOT_BUILT";

// Pattern: ${VAR_NAME} or $VAR_NAME
public static final String ENV_VAR_PATTERN = "^(?:\\$\\{(\\w+)\\})|(?:\\$(\\w+))$";

public static VersionNumberBuildInfo incBuild(Run build, EnvVars vars,
Run prevBuild, boolean skipFailedBuilds, String overrideBuildsToday, String overrideBuildsThisWeek,
Run prevBuild, Result worstResultForIncrement, String overrideBuildsToday, String overrideBuildsThisWeek,
String overrideBuildsThisMonth, String overrideBuildsThisYear, String overrideBuildsAllTime) {
int buildsToday = new BuildsTodayGenerator().getNextNumber(build, vars, prevBuild, skipFailedBuilds, overrideBuildsToday);
int buildsThisWeek = new BuildsThisWeekGenerator().getNextNumber(build, vars, prevBuild, skipFailedBuilds, overrideBuildsThisWeek);
int buildsThisMonth = new BuildsThisMonthGenerator().getNextNumber(build, vars, prevBuild, skipFailedBuilds, overrideBuildsThisMonth);
int buildsThisYear = new BuildsThisYearGenerator().getNextNumber(build, vars, prevBuild, skipFailedBuilds, overrideBuildsThisYear);
int buildsAllTime = new BuildsAllTimeGenerator().getNextNumber(build, vars, prevBuild, skipFailedBuilds, overrideBuildsAllTime);

int buildsToday = new BuildsTodayGenerator().getNextNumber(build, vars, prevBuild, worstResultForIncrement, overrideBuildsToday);
int buildsThisWeek = new BuildsThisWeekGenerator().getNextNumber(build, vars, prevBuild, worstResultForIncrement, overrideBuildsThisWeek);
int buildsThisMonth = new BuildsThisMonthGenerator().getNextNumber(build, vars, prevBuild, worstResultForIncrement, overrideBuildsThisMonth);
int buildsThisYear = new BuildsThisYearGenerator().getNextNumber(build, vars, prevBuild, worstResultForIncrement, overrideBuildsThisYear);
int buildsAllTime = new BuildsAllTimeGenerator().getNextNumber(build, vars, prevBuild, worstResultForIncrement, overrideBuildsAllTime);

return new VersionNumberBuildInfo(buildsToday, buildsThisWeek, buildsThisMonth, buildsThisYear, buildsAllTime);
}
Expand Down
Expand Up @@ -35,10 +35,14 @@
import org.kohsuke.stapler.DataBoundSetter;

import hudson.Extension;
import hudson.model.Result;
import hudson.model.Run;
import hudson.EnvVars;
import hudson.util.ListBoxModel;

import java.util.Date;
import java.util.logging.Logger;
import java.lang.invoke.MethodHandles;

/**
* Returns the version number according to the
Expand All @@ -51,12 +55,19 @@
* </pre>
*/
public class VersionNumberStep extends AbstractStepImpl {

/** Use Java 7 MethodHandles to get my class for logger. */
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());

public final String versionNumberString;

@DataBoundSetter
@Deprecated
public boolean skipFailedBuilds = false;

@DataBoundSetter
public String worstResultForIncrement = null;

@DataBoundSetter
public String versionPrefix = null;

Expand Down Expand Up @@ -101,6 +112,33 @@ public String getVersionPrefix() {
return null;
}

public Result getWorstResultForIncrement() {
// For compatibility-reasons during transition from old plugin (<= 1.8.1) to newer plugin.
if (this.skipFailedBuilds) {
LOGGER.warning("At least in one project VersionNumber plugin still uses the old config-variable 'skipFailedBuilds'. Make sure to update and safe the job-configs to update that behavior.");
this.skipFailedBuilds = false;
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_SUCCESS;
}
if (this.worstResultForIncrement == null) {
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_NOT_BUILT;
}
switch (this.worstResultForIncrement) {
case VersionNumberCommon.WORST_RESULT_NOT_BUILT:
return Result.NOT_BUILT;
case VersionNumberCommon.WORST_RESULT_ABORTED:
return Result.ABORTED;
case VersionNumberCommon.WORST_RESULT_FAILURE:
return Result.FAILURE;
case VersionNumberCommon.WORST_RESULT_UNSTABLE:
return Result.UNSTABLE;
case VersionNumberCommon.WORST_RESULT_SUCCESS:
return Result.SUCCESS;
default:
this.worstResultForIncrement = VersionNumberCommon.WORST_RESULT_SUCCESS;
return Result.SUCCESS;
}
}

@Extension
public static final class DescriptorImpl extends AbstractStepDescriptorImpl {

Expand All @@ -116,6 +154,16 @@ public DescriptorImpl() {
return "Determine the correct version number";
}

public ListBoxModel doFillWorstResultForIncrementItems() {
ListBoxModel items = new ListBoxModel();
items.add(VersionNumberCommon.WORST_RESULT_SUCCESS);
items.add(VersionNumberCommon.WORST_RESULT_UNSTABLE);
items.add(VersionNumberCommon.WORST_RESULT_FAILURE);
items.add(VersionNumberCommon.WORST_RESULT_ABORTED);
items.add(VersionNumberCommon.WORST_RESULT_NOT_BUILT);
return items;
}

}

public static class Execution extends AbstractSynchronousStepExecution<String> {
Expand All @@ -129,7 +177,8 @@ protected String run() throws Exception {
if (step.versionNumberString != null) {
try {
Run prevBuild = VersionNumberCommon.getPreviousBuildWithVersionNumber(run, step.versionPrefix);
VersionNumberBuildInfo info = VersionNumberCommon.incBuild(run, env, prevBuild, step.skipFailedBuilds,
VersionNumberBuildInfo info = VersionNumberCommon.incBuild(run, env, prevBuild,
step.getWorstResultForIncrement(),
step.overrideBuildsToday,
step.overrideBuildsThisWeek,
step.overrideBuildsThisMonth,
Expand All @@ -145,7 +194,7 @@ protected String run() throws Exception {
// If a version prefix is specified, it is forced to be prefixed.
// Otherwise the version prefix does not function correctly - even in freestyle jobs.
// In freestlye jobs it is assumed that the user reuses the version prefix
// within the version number string, but this assumtion is not documented.
// within the version number string, but this assumption is not documented.
// Hence, it might yield to errors, and therefore in pipeline steps, we
// force the version prefix to be prefixed.
if (step.versionPrefix != null) {
Expand All @@ -159,8 +208,8 @@ protected String run() throws Exception {
return "";
}

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;

}

}
}
Expand Up @@ -9,9 +9,9 @@
<f:entry title="Prefix Variable" help="/plugin/versionnumber/help-environmentPrefixVariable.html">
<f:textbox field="environmentPrefixVariable" />
</f:entry>
<f:entry title="Failed Builds" help="/plugin/versionnumber/help-reuseVersionNumbers.html">
<f:checkbox name="skipFailedBuilds" checked="${instance.skipFailedBuilds}" />
Don't increment builds today / this week / this month / this year / all time after a failed build.
<f:entry title="Skip Builds worse than" field="worstResultForIncrement" help="/plugin/versionnumber/help-skipBuildsWorseThan.html">
<f:select/>
Don't increment builds today / this week / this month / this year / all time after a build-run with a result worse than the selected one.
</f:entry>
<f:entry title="Build Display Name" help="/plugin/versionnumber/help-useAsBuildDisplayName.html">
<f:checkbox name="useAsBuildDisplayName" checked="${instance.useAsBuildDisplayName}" />
Expand Down
Expand Up @@ -10,7 +10,8 @@
<f:entry title="${%Project Start Date}" field="projectStartDate">
<f:textbox/>
</f:entry>
<f:entry title="${%Skip Failed Builds}" field="skipFailedBuilds">
<f:checkbox/>
</f:entry>
<f:entry title="${%Skip Builds worse than}" field="worstResultForIncrement">
<f:select/>
Don't increment builds today / this week / this month / this year / all time after a build-run with a result worse than the selected one.
</f:entry>
</j:jelly>

0 comments on commit 668c9e6

Please sign in to comment.