Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 7b85926
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/main/java/hudson/plugins/msbuild/MsBuildBuilder.java
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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")
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/hudson/plugins/msbuild/MsBuildConsoleParser.java
@@ -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();
}
}
Expand Up @@ -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>
5 changes: 5 additions & 0 deletions src/main/webapp/help-UnstableIfWarnings.html
@@ -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.