Skip to content
Permalink
Browse files
[FIXED JENKINS-19432] Option unstable if warnings
Add option : if warnings on compilation, the build will be unstable.
  • Loading branch information
damienfinck committed Aug 31, 2013
1 parent f3881a0 commit 7b85926b3588552f441ba1243bf5d6ac8f401778
@@ -9,6 +9,7 @@
import hudson.util.QuotedStringTokenizer;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
@@ -26,6 +27,7 @@ public class MsBuildBuilder extends Builder {
private final boolean buildVariablesAsProperties;
private transient boolean continueOnBuilFailure;
private final boolean continueOnBuildFailure;
private final boolean unstableIfWarnings;

/**
* When this builder is created in the project configuration step,
@@ -36,15 +38,17 @@ public class MsBuildBuilder extends Builder {
* @param cmdLineArgs Whitespace separated list of command line arguments
* @param buildVariablesAsProperties If true, pass build variables as properties to MSBuild
* @param continueOnBuildFailure If true, job will continue dispite of MSBuild build failure
* @param unstableIfWarnings If true, job will be unstable if there are warnings
*/
@DataBoundConstructor
@SuppressWarnings("unused")
public MsBuildBuilder(String msBuildName, String msBuildFile, String cmdLineArgs, boolean buildVariablesAsProperties, boolean continueOnBuildFailure) {
public MsBuildBuilder(String msBuildName, String msBuildFile, String cmdLineArgs, boolean buildVariablesAsProperties, boolean continueOnBuildFailure, boolean unstableIfWarnings) {
this.msBuildName = msBuildName;
this.msBuildFile = msBuildFile;
this.cmdLineArgs = cmdLineArgs;
this.buildVariablesAsProperties = buildVariablesAsProperties;
this.continueOnBuildFailure = continueOnBuildFailure;
this.unstableIfWarnings = unstableIfWarnings;
}

@SuppressWarnings("unused")
@@ -72,6 +76,11 @@ public boolean getContinueOnBuildFailure() {
return continueOnBuildFailure;
}

@SuppressWarnings("unused")
public boolean getUnstableIfWarnings() {
return unstableIfWarnings;
}

public MsBuildInstallation getMsBuild() {
DescriptorImpl descriptor = (DescriptorImpl)getDescriptor();
for (MsBuildInstallation i : descriptor.getInstallations()) {
@@ -165,7 +174,16 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

try {
listener.getLogger().println(String.format("Executing the command %s from %s", args.toStringWithQuote(), pwd));
int r = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(pwd).join();
// Parser to find the number of Warnings/Errors
MsBuildConsoleParser mbcp = new MsBuildConsoleParser(listener.getLogger(), build.getCharset());
// Launch the msbuild.exe
int r = launcher.launch().cmds(args).envs(env).stdout(mbcp).pwd(pwd).join();
// Check the number of warnings
if (unstableIfWarnings && mbcp.getNumberOfWarnings() > 0) {
listener.getLogger().println("> Set build UNSTABLE because there are warnings.");
build.setResult(Result.UNSTABLE);
}
// Return the result of the compilation
return continueOnBuildFailure ? true : (r == 0);
} catch (IOException e) {
Util.displayIOException(e, listener);
@@ -0,0 +1,76 @@
package hudson.plugins.msbuild;

import hudson.console.LineTransformationOutputStream;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Parser to find the number of Warnings/Errors of MsBuild compilation
*
* @author Damien Finck
*/
public class MsBuildConsoleParser extends LineTransformationOutputStream {
private final OutputStream out;
private final Charset charset;

private int numberOfWarnings = -1;
private int numberOfErrors = -1;

public MsBuildConsoleParser(OutputStream out, Charset charset) {
this.out = out;
this.charset = charset;
}

public int getNumberOfWarnings() {
return numberOfWarnings;
}

public int getNumberOfErrors() {
return numberOfErrors;
}

@Override
protected void eol(byte[] b, int len) throws IOException {
String line = charset.decode(ByteBuffer.wrap(b, 0, len)).toString();

// trim off CR/LF from the end
line = trimEOL(line);

Pattern patternWarnings = Pattern.compile(".*\\d+\\sWarning\\(s\\).*");
Pattern patternErrors = Pattern.compile(".*\\d+\\sError\\(s\\).*");

Matcher mWarnings = patternWarnings.matcher(line);
Matcher mErrors = patternErrors.matcher(line);

if (mWarnings.matches()) { // Match the number of warnings
String[] part = line.split(" ");
try {
numberOfWarnings = Integer.parseInt(part[4]);
} catch (NumberFormatException e) {

}
}
else if (mErrors.matches()) { // Match the number of errors
String[] part = line.split(" ");
try {
numberOfErrors = Integer.parseInt(part[4]);
} catch (NumberFormatException e) {

}
}

// Write to output
out.write(b,0,len);
}

@Override
public void close() throws IOException {
super.close();
out.close();
}
}
@@ -21,6 +21,10 @@
<f:entry title="Continue Job on build Failure" help="${rootURL}/../plugin/msbuild/help-continueOnBuildFailure.html">
<f:checkbox name="msBuildBuilder.continueOnBuildFailure" value="${instance.continueOnBuildFailure}"
checked="${instance.continueOnBuildFailure}" default="false" />
</f:entry>
</f:entry>
<f:entry title="If warnings set the build to Unstable" help="${rootURL}/../plugin/msbuild/help-unstableIfWarnings.html">
<f:checkbox name="msBuildBuilder.unstableIfWarnings" value="${instance.unstableIfWarnings}"
checked="${instance.unstableIfWarnings}" default="false" />
</f:entry>
</f:advanced>
</j:jelly>
@@ -0,0 +1,5 @@
<div>
<p>
If set to true and warnings on compilation, the build will be unstable.
</p>
</div>

0 comments on commit 7b85926

Please sign in to comment.