/
BuildStatusContent.java
85 lines (76 loc) · 3.46 KB
/
BuildStatusContent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package hudson.plugins.emailext.plugins.content;
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
@EmailToken
public class BuildStatusContent extends DataBoundTokenMacro {
public static final String MACRO_NAME = "BUILD_STATUS";
@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals(MACRO_NAME);
}
@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
// Build can be "building" when the pre-build trigger is used. (and in this case there is not result set yet for the build)
// Reporting "success", "still failing", etc doesn't make sense in this case.
// When using on matrix build, the build is still in building stage when matrix aggregator end build trigger is fired, though
if ( (build.isBuilding()) && (null == build.getResult())) {
return "Building";
}
Result buildResult = build.getResult();
if (buildResult == Result.FAILURE) {
Run<?,?> prevBuild = build.getPreviousBuild();
if (prevBuild != null && (prevBuild.getResult() == Result.FAILURE)) {
return "Still Failing";
} else {
return "Failure";
}
} else if (buildResult == Result.UNSTABLE) {
Run<?,?> prevBuild = build.getPreviousBuild();
if (prevBuild != null) {
if (prevBuild.getResult() == Result.UNSTABLE) {
return "Still Unstable";
} else if (prevBuild.getResult() == Result.SUCCESS) {
return "Unstable";
} else if (prevBuild.getResult() == Result.FAILURE ||
prevBuild.getResult() == Result.ABORTED ||
prevBuild.getResult() == Result.NOT_BUILT) {
//iterate through previous builds
//(fail_or_aborted)* and then an unstable : return still unstable
//(fail_or_aborted)* and then successful : return unstable
Run<?,?> previous = prevBuild.getPreviousBuild();
while (previous != null) {
if (previous.getResult() == Result.SUCCESS) {
return "Unstable";
}
if (previous.getResult() == Result.UNSTABLE) {
return "Still unstable";
}
previous = previous.getPreviousBuild();
}
return "Unstable";
}
} else {
return "Unstable";
}
} else if (buildResult == Result.SUCCESS) {
Run<?,?> prevBuild = build.getPreviousBuild();
if (prevBuild != null && (prevBuild.getResult() == Result.UNSTABLE || prevBuild.getResult() == Result.FAILURE)) {
return "Fixed";
} else {
return "Successful";
}
} else if (buildResult == Result.NOT_BUILT) {
return "Not Built";
} else if (buildResult == Result.ABORTED) {
return "Aborted";
}
return "Unknown";
}
}