Skip to content

Commit 28d9dbf

Browse files
committed
Fixed #209
Return TestReport.NO_TEST_REPORT etc.
1 parent 2d33bb1 commit 28d9dbf

File tree

7 files changed

+352
-50
lines changed

7 files changed

+352
-50
lines changed

ReleaseNotes.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,53 @@
1616

1717
### API Changes
1818

19+
20+
* [Fixed issue 209][issue-209]
21+
22+
Consider Returning null from the "getTestReport()" of Build.java class for builds that Never run.
23+
24+
The type `BUILD_HAS_NEVER_RUN` has been enhanced to return `TestReport.NO_TEST_REPORT` if you
25+
call `getTestReport()` and return `BuildWithDetails.BUILD_HAS_NEVER_RUN` if you call `details()` on the
26+
type.
27+
28+
Furthermore `JobWithDetails` class has been enhanced with the following methods:
29+
30+
```java
31+
public class JobWithDetails ... {
32+
public boolean hasFirstBuildRun();
33+
public boolean hasLastBuildRun(;
34+
public boolean hasLastCompletedBuildRun();
35+
public boolean hasLastFailedBuildRun();
36+
public boolean hasLastStableBuildRun();
37+
public boolean hasLastSuccessfulBuildRun();
38+
public boolean hasLastUnstableBuildRun();
39+
public boolean hasLastUnsuccessfulBuildRun();
40+
}
41+
```
42+
to make checking more convenient. The following code snippet
43+
shows how you need to go before this change:
44+
45+
```java
46+
JobWithDetails job = server.getJob(jobName);
47+
if (Build.BUILD_HAS_NEVER_RUN.equals(job.getLastBuild()) {
48+
...
49+
} else {
50+
// Now we can get the TestReport
51+
job.getLastBuild().getTestReport();
52+
}
53+
```
54+
55+
This can now be simplified to the following:
56+
57+
```java
58+
JobWithDetails job = server.getJob(jobName);
59+
if (job.hasLastBuildRun()) {
60+
// Now we can get the TestReport
61+
job.getLastBuild().getTestReport();
62+
} else {
63+
}
64+
```
65+
1966
* [Fixed issue 211][issue-211]
2067

2168
Added methods to update/clear the description of a job.
@@ -787,6 +834,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
787834
[issue-202]: https://github.com/jenkinsci/java-client-api/issues/202
788835
[issue-203]: https://github.com/jenkinsci/java-client-api/issues/203
789836
[issue-207]: https://github.com/jenkinsci/java-client-api/issues/207
837+
[issue-209]: https://github.com/jenkinsci/java-client-api/issues/209
790838
[issue-211]: https://github.com/jenkinsci/java-client-api/issues/211
791839
[pull-123]: https://github.com/jenkinsci/java-client-api/pull/123
792840
[pull-149]: https://github.com/jenkinsci/java-client-api/pull/149

jenkins-client-it-docker/src/test/java/com/offbytwo/jenkins/integration/NoExecutorStartedGetJobIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void getLastFailedBuildShouldBeBUILD_HAS_NEVER_RAN() {
7979
assertThat(job.getLastFailedBuild()).isEqualTo(Build.BUILD_HAS_NEVER_RUN);
8080
}
8181

82+
@Test
83+
public void hasLastFailedBuildShouldBeTrue() {
84+
assertThat(job.hasLastFailedBuildRun()).isTrue();
85+
}
86+
8287
@Test
8388
public void getLastStableBuildShouldNotBeNull() throws IOException {
8489
assertThat(job.getLastStableBuild()).isNotNull();

jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,38 @@ public class Build extends BaseModel {
1414

1515
/**
1616
* This will be returned by the API in cases where no build has ever been
17-
* executed like {@link JobWithDetails#getLastBuild()} etc.
17+
* executed like {@link JobWithDetails#getLastBuild()} etc. This will also
18+
* be returned by {@link #details()} is the build has not been run.
1819
*/
19-
public static final Build BUILD_HAS_NEVER_RUN = new Build(-1, -1, "UNKNOWN");
20+
public static final Build BUILD_HAS_NEVER_RUN = new Build(-1, -1, "UNKNOWN") {
21+
@Override
22+
public TestReport getTestReport() {
23+
return TestReport.NO_TEST_REPORT;
24+
}
25+
26+
@Override
27+
public BuildWithDetails details() {
28+
// For a build which never has been run you couldn't get
29+
// details about!
30+
return BuildWithDetails.BUILD_HAS_NEVER_RUN;
31+
}
32+
};
33+
2034
/**
2135
* This will be returned by the API in cases where a build has been
2236
* cancelled.
2337
*/
24-
public static final Build BUILD_HAS_BEEN_CANCELLED = new Build(-1, -1, "CANCELLED");
38+
public static final Build BUILD_HAS_BEEN_CANCELLED = new Build(-1, -1, "CANCELLED") {
39+
@Override
40+
public TestReport getTestReport() {
41+
return TestReport.NO_TEST_REPORT;
42+
}
43+
44+
@Override
45+
public BuildWithDetails details() {
46+
return BuildWithDetails.BUILD_HAS_BEEN_CANCELLED;
47+
}
48+
};
2549

2650
private int number;
2751
private int queueId;
@@ -70,6 +94,13 @@ protected void setUrl(String url) {
7094
this.url = url;
7195
}
7296

97+
/**
98+
*
99+
* @return The information from Jenkins. In cases the build has never run
100+
* {@link #BUILD_HAS_NEVER_RUN} will be returned.
101+
* @throws IOException
102+
* in case of an error.
103+
*/
73104
public BuildWithDetails details() throws IOException {
74105
return client.get(url, BuildWithDetails.class);
75106
}

jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildResult.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@
77
package com.offbytwo.jenkins.model;
88

99
public enum BuildResult {
10-
FAILURE, UNSTABLE, REBUILDING, BUILDING, ABORTED, SUCCESS, UNKNOWN, NOT_BUILT,
10+
FAILURE, UNSTABLE, REBUILDING, BUILDING,
1111
/**
12-
* This will be the result of a job in cases where it has been
13-
* cancelled during the time in the queue.
12+
* This means a job was already running and has been aborted.
13+
*/
14+
ABORTED,
15+
/**
16+
*
17+
*/
18+
SUCCESS,
19+
/**
20+
* ?
21+
*/
22+
UNKNOWN,
23+
/**
24+
* This is returned if a job has never been built.
25+
*/
26+
NOT_BUILT,
27+
/**
28+
* This will be the result of a job in cases where it has been cancelled
29+
* during the time in the queue.
1430
*/
1531
CANCELLED
1632
}

jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,72 @@
2525
*/
2626
public class BuildWithDetails extends Build {
2727

28+
/**
29+
* This will be returned by the API in cases where the build has never run.
30+
* For example {@link Build#BUILD_HAS_NEVER_RUN}
31+
*/
32+
public static final BuildWithDetails BUILD_HAS_NEVER_RUN = new BuildWithDetails() {
33+
34+
@Override
35+
public List getActions() {
36+
return Collections.emptyList();
37+
}
38+
39+
@Override
40+
public List<Artifact> getArtifacts() {
41+
return Collections.<Artifact>emptyList();
42+
}
43+
44+
@Override
45+
public List<BuildCause> getCauses() {
46+
return Collections.<BuildCause>emptyList();
47+
}
48+
49+
@Override
50+
public List<BuildChangeSetAuthor> getCulprits() {
51+
return Collections.<BuildChangeSetAuthor>emptyList();
52+
}
53+
54+
@Override
55+
public BuildResult getResult() {
56+
return BuildResult.NOT_BUILT;
57+
}
58+
59+
};
60+
61+
/**
62+
* This will be returned by the API in cases where the build has been
63+
* cancelled. For example {@link Build#BUILD_HAS_BEEN_CANCELLED}
64+
*/
65+
public static final BuildWithDetails BUILD_HAS_BEEN_CANCELLED = new BuildWithDetails() {
66+
67+
@Override
68+
public List getActions() {
69+
return Collections.emptyList();
70+
}
71+
72+
@Override
73+
public List<Artifact> getArtifacts() {
74+
return Collections.<Artifact>emptyList();
75+
}
76+
77+
@Override
78+
public List<BuildCause> getCauses() {
79+
return Collections.<BuildCause>emptyList();
80+
}
81+
82+
@Override
83+
public List<BuildChangeSetAuthor> getCulprits() {
84+
return Collections.<BuildChangeSetAuthor>emptyList();
85+
}
86+
87+
@Override
88+
public BuildResult getResult() {
89+
return BuildResult.CANCELLED;
90+
}
91+
92+
};
93+
2894
private List actions; // TODO: Should be improved.
2995
private boolean building;
3096
private String description;
@@ -103,7 +169,9 @@ public boolean apply(Map<String, Object> action) {
103169
}
104170

105171
/**
106-
* Update <code>displayName</code> and the <code>description</code> of a build.
172+
* Update <code>displayName</code> and the <code>description</code> of a
173+
* build.
174+
*
107175
* @param displayName The new displayName which should be set.
108176
* @param description The description which should be set.
109177
* @param crumbFlag <code>true</code> or <code>false</code>.
@@ -113,14 +181,16 @@ public void updateDisplayNameAndDescription(String displayName, String descripti
113181
throws IOException {
114182
Objects.requireNonNull(displayName, "displayName is not allowed to be null.");
115183
Objects.requireNonNull(description, "description is not allowed to be null.");
116-
//TODO: Check what the "core:apply" means?
184+
// TODO: Check what the "core:apply" means?
117185
ImmutableMap<String, String> params = ImmutableMap.of("displayName", displayName, "description", description,
118186
"core:apply", "", "Submit", "Save");
119187
client.post_form(this.getUrl() + "/configSubmit?", params, crumbFlag);
120188
}
121189

122190
/**
123-
* Update <code>displayName</code> and the <code>description</code> of a build.
191+
* Update <code>displayName</code> and the <code>description</code> of a
192+
* build.
193+
*
124194
* @param displayName The new displayName which should be set.
125195
* @param description The description which should be set.
126196
* @throws IOException in case of errors.
@@ -131,21 +201,23 @@ public void updateDisplayNameAndDescription(String displayName, String descripti
131201

132202
/**
133203
* Update <code>displayName</code> of a build.
204+
*
134205
* @param displayName The new displayName which should be set.
135206
* @param crumbFlag <code>true</code> or <code>false</code>.
136207
* @throws IOException in case of errors.
137208
*/
138209
public void updateDisplayName(String displayName, boolean crumbFlag) throws IOException {
139210
Objects.requireNonNull(displayName, "displayName is not allowed to be null.");
140211
String description = getDescription() == null ? "" : getDescription();
141-
//TODO: Check what the "core:apply" means?
142-
ImmutableMap<String, String> params = ImmutableMap.of("displayName", displayName, "description",
143-
description, "core:apply", "", "Submit", "Save");
212+
// TODO: Check what the "core:apply" means?
213+
ImmutableMap<String, String> params = ImmutableMap.of("displayName", displayName, "description", description,
214+
"core:apply", "", "Submit", "Save");
144215
client.post_form(this.getUrl() + "/configSubmit?", params, crumbFlag);
145216
}
146217

147218
/**
148219
* Update <code>displayName</code> of a build.
220+
*
149221
* @param displayName The new displayName which should be set.
150222
* @throws IOException in case of errors.
151223
*/
@@ -155,21 +227,23 @@ public void updateDisplayName(String displayName) throws IOException {
155227

156228
/**
157229
* Update the <code>description</code> of a build.
230+
*
158231
* @param description The description which should be set.
159232
* @param crumbFlag <code>true</code> or <code>false</code>.
160233
* @throws IOException in case of errors.
161234
*/
162235
public void updateDescription(String description, boolean crumbFlag) throws IOException {
163236
Objects.requireNonNull(description, "description is not allowed to be null.");
164237
String displayName = getDisplayName() == null ? "" : getDisplayName();
165-
//TODO: Check what the "core:apply" means?
166-
ImmutableMap<String, String> params = ImmutableMap.of("displayName", displayName, "description",
167-
description, "core:apply", "", "Submit", "Save");
238+
// TODO: Check what the "core:apply" means?
239+
ImmutableMap<String, String> params = ImmutableMap.of("displayName", displayName, "description", description,
240+
"core:apply", "", "Submit", "Save");
168241
client.post_form(this.getUrl() + "/configSubmit?", params, crumbFlag);
169242
}
170243

171244
/**
172245
* Update the <code>description</code> of a build.
246+
*
173247
* @param description The description which should be set.
174248
* @throws IOException in case of errors.
175249
*/
@@ -278,15 +352,15 @@ public boolean apply(Map<String, Object> action) {
278352
/**
279353
* @return The console output of the build. The line separation is done by
280354
* {@code CR+LF}.
281-
* @throws IOException
282-
* in case of a failure.
355+
* @throws IOException in case of a failure.
283356
*/
284357
public String getConsoleOutputText() throws IOException {
285358
return client.get(getUrl() + "/logText/progressiveText");
286359
}
287360

288361
/**
289362
* The console output with HTML.
363+
*
290364
* @return The console output as HTML.
291365
* @throws IOException
292366
*/

0 commit comments

Comments
 (0)