File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\ResultTrend.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Christoph Kutzinski
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import org.jvnet.localizer.Localizable;
27
28 import java.util.Locale;
29
30 /**
31 * Describes an {@link Result} trend by taking the comparing the result of the current and the previous build.
32 *
33 * @author kutzi
34 * @since 1.416
35 */
36 public enum ResultTrend {
37 /**
38 * Previous build was {@link Result#FAILURE} or {@link Result#UNSTABLE}
39 * and is now {@link Result#SUCCESS}.
40 */
41 FIXED(Messages._ResultTrend_Fixed()),
42 /**
43 * Build as well as previous build (if it has a previous build) are {@link Result#SUCCESS}
44 */
45 SUCCESS(Messages._ResultTrend_Success()),
46 /**
47 * Previous build was {@link Result#FAILURE} and is now 'only' {@link Result#UNSTABLE}.
48 */
49 NOW_UNSTABLE(Messages._ResultTrend_NowUnstable()),
50 /**
51 * Build as well as previous build are {@link Result#UNSTABLE}.
52 */
53 STILL_UNSTABLE(Messages._ResultTrend_StillUnstable()),
54 /**
55 * Previous build (if there is one) was {@link Result#SUCCESS} and current build is {@link Result#UNSTABLE}.
56 */
57 UNSTABLE(Messages._ResultTrend_Unstable()),
58 /**
59 * Build as well as previous build are {@link Result#FAILURE}.
60 */
61 STILL_FAILING(Messages._ResultTrend_StillFailing()),
62 /**
63 * Previous build (if there is one) was {@link Result#SUCCESS} or {@link Result#UNSTABLE}
64 * and current build is {@link Result#FAILURE}.
65 */
66 FAILURE(Messages._ResultTrend_Failure()),
67 /**
68 * Build was aborted.
69 */
70 ABORTED(Messages._ResultTrend_Aborted()),
71 /**
72 * Build didn't run (yet).
73 */
74 NOT_BUILT(Messages._ResultTrend_NotBuilt());
75
76 private final Localizable description;
77
78 private ResultTrend(Localizable description) {
79 this.description = description;
80 }
81
82 /**
83 * Returns a short human-readable description of the result.
84 */
85 public String getDescription() {
86 return this.description.toString();
87 }
88
89 /**
90 * Gets all upper case ID like token of the build status.
91 */
92 public String getID() {
93 return this.description.toString(Locale.ENGLISH).toUpperCase(Locale.ENGLISH);
94 }
95
96 /**
97 * Returns the result trend of a build.
98 *
99 * @param build the build
100 * @return the result trend
101 */
102 public static ResultTrend getResultTrend(AbstractBuild<?, ?> build) {
103 return getResultTrend((Run<?,?>)build);
104 }
105
106 /**
107 * Returns the result trend of a run.
108 *
109 * @param run the run
110 * @return the result trend
111 *
112 * @since 1.441
113 */
114 public static ResultTrend getResultTrend(Run<?, ?> run) {
115
116 Result result = run.getResult();
117
118 if (result == Result.ABORTED) {
119 return ABORTED;
120 } else if (result == Result.NOT_BUILT) {
121 return NOT_BUILT;
122 }
123
124 if (result == Result.SUCCESS) {
125 if (isFix(run)) {
126 return FIXED;
127 } else {
128 return SUCCESS;
129 }
130 }
131
132 Run<?, ?> previousBuild = getPreviousNonAbortedBuild(run);
133 if (result == Result.UNSTABLE) {
134 if (previousBuild == null) {
135 return UNSTABLE;
136 }
137
138
139 if (previousBuild.getResult() == Result.UNSTABLE) {
140 return STILL_UNSTABLE;
141 } else if (previousBuild.getResult() == Result.FAILURE) {
142 return NOW_UNSTABLE;
143 } else {
144 return UNSTABLE;
145 }
146 } else if (result == Result.FAILURE) {
147 if (previousBuild != null && previousBuild.getResult() == Result.FAILURE) {
148 return STILL_FAILING;
149 } else {
150 return FAILURE;
151 }
152 }
153
154 throw new IllegalArgumentException("Unknown result: '" + result + "' for build: " + run);
155 }
156
157 /**
158 * Returns the previous 'not aborted' build (i.e. ignores ABORTED and NOT_BUILT builds)
159 * or null.
160 */
161 private static Run<?, ?> getPreviousNonAbortedBuild(Run<?, ?> build) {
162 Run<?, ?> previousBuild = build.getPreviousBuild();
163 while (previousBuild != null) {
164 if (previousBuild.getResult() == null
165 || previousBuild.getResult() == Result.ABORTED
166 || previousBuild.getResult() == Result.NOT_BUILT) {
167
168 previousBuild = previousBuild.getPreviousBuild();
169 } else {
170 return previousBuild;
171 }
172 }
173 return previousBuild;
174 }
175
176 /**
177 * Returns true if this build represents a 'fix'.
178 * I.e. it is the first successful build after previous
179 * 'failed' and/or 'unstable' builds.
180 * Ignores 'aborted' and 'not built' builds.
181 */
182 private static boolean isFix(Run<?, ?> build) {
183 if (build.getResult() != Result.SUCCESS) {
184 return false;
185 }
186
187 Run<?, ?> previousBuild = getPreviousNonAbortedBuild(build);
188 if (previousBuild != null) {
189 return previousBuild.getResult().isWorseThan(Result.SUCCESS);
190 }
191 return false;
192 }
193 }
194