Skip to content

Commit 57d19e6

Browse files
committed
o getAllBuilds(), getAllBuilds(Range range) and getBuilds() will return Collections.emptyList() instead of null in case of no build has been executed before.
1 parent ec4d507 commit 57d19e6

2 files changed

Lines changed: 50 additions & 21 deletions

File tree

ReleaseNotes.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
### API Changes
66

7+
[Fixed issue 159][issue-159]
8+
9+
The following methods have been changed to return `Collections.emtpyList()` if
10+
no builds exists or have ran before.
11+
12+
```java
13+
getAllBuilds(Range range);
14+
getAllBuilds(),
15+
getBuilds()
16+
```
17+
718
[Fixed NPE][issue-147]
819

920
The JobWithDetails class contains several methods which could
@@ -407,6 +418,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
407418
[issue-154]: https://github.com/RisingOak/jenkins-client/issues/154
408419
[issue-155]: https://github.com/RisingOak/jenkins-client/issues/155
409420
[issue-157]: https://github.com/RisingOak/jenkins-client/issues/157
421+
[issue-159]: https://github.com/RisingOak/jenkins-client/issues/159
410422
[pull-123]: https://github.com/RisingOak/jenkins-client/pull/123
411423
[pull-149]: https://github.com/RisingOak/jenkins-client/pull/149
412424
[pull-158]: https://github.com/RisingOak/jenkins-client/pull/158

src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,21 @@ public boolean isInQueue() {
7575
* won't get back all builds via this method. In such cases you need to use
7676
* {@link #getAllBuilds()}.
7777
*
78-
* @return the list of {@link Build}.
78+
* @return the list of {@link Build}. In case of no builds have been
79+
* executed yet return {@link Collections#emptyList()}.
7980
*/
8081
public List<Build> getBuilds() {
81-
return transform(builds, new Function<Build, Build>() {
82-
@Override
83-
public Build apply(Build from) {
84-
return buildWithClient(from);
85-
}
86-
});
82+
if (builds == null) {
83+
return Collections.emptyList();
84+
}
85+
else {
86+
return transform(builds, new Function<Build, Build>() {
87+
@Override
88+
public Build apply(Build from) {
89+
return buildWithClient(from);
90+
}
91+
});
92+
}
8793
}
8894

8995
/**
@@ -95,7 +101,8 @@ public Build apply(Build from) {
95101
* particular build {@link Build#details()} to reduce the amount of data
96102
* which needed to be transfered.
97103
*
98-
* @return the list of {@link Build}.
104+
* @return the list of {@link Build}. In case of no builds have been
105+
* executed yet return {@link Collections#emptyList()}.
99106
* @throws IOException
100107
* In case of failure.
101108
* @see <a href="https://issues.jenkins-ci.org/browse/JENKINS-30238">Jenkins
@@ -108,12 +115,16 @@ public List<Build> getAllBuilds() throws IOException {
108115
List<Build> builds = client.get(path + "job/" + EncodingUtils.encode(this.getName())
109116
+ "?tree=allBuilds[number[*],url[*],queueId[*]]", AllBuilds.class).getAllBuilds();
110117

111-
return transform(builds, new Function<Build, Build>() {
112-
@Override
113-
public Build apply(Build from) {
114-
return buildWithClient(from);
115-
}
116-
});
118+
if (builds == null) {
119+
return Collections.emptyList();
120+
} else {
121+
return transform(builds, new Function<Build, Build>() {
122+
@Override
123+
public Build apply(Build from) {
124+
return buildWithClient(from);
125+
}
126+
});
127+
}
117128
} catch (HttpResponseException e) {
118129
// TODO: Thinks about a better handline if the job does not exist?
119130
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
@@ -142,7 +153,8 @@ public Build apply(Build from) {
142153
* {@link #getAllBuilds()}.</b>
143154
*
144155
* @param range {@link Range}
145-
* @return The list of builds defined by the given range.
156+
* @return the list of {@link Build}. In case of no builds have been
157+
* executed yet return {@link Collections#emptyList()}.
146158
* @throws IOException in case of an error.
147159
*/
148160
public List<Build> getAllBuilds(Range range) throws IOException {
@@ -152,12 +164,16 @@ public List<Build> getAllBuilds(Range range) throws IOException {
152164
try {
153165
List<Build> builds = client.get(path + range.getRangeString(), AllBuilds.class).getAllBuilds();
154166

155-
return transform(builds, new Function<Build, Build>() {
156-
@Override
157-
public Build apply(Build from) {
158-
return buildWithClient(from);
159-
}
160-
});
167+
if (builds == null) {
168+
return Collections.emptyList();
169+
} else {
170+
return transform(builds, new Function<Build, Build>() {
171+
@Override
172+
public Build apply(Build from) {
173+
return buildWithClient(from);
174+
}
175+
});
176+
}
161177
} catch (HttpResponseException e) {
162178
// TODO: Thinks about a better handline if the job does not exist?
163179
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
@@ -292,6 +308,7 @@ public boolean apply(Build input) {
292308
};
293309

294310
Optional<Build> optionalBuild = Iterables.tryFind(builds, isMatchingBuildNumber);
311+
//TODO: Check if we could use Build#NO...instead of Null?
295312
return optionalBuild.orNull() == null ? null : buildWithClient(optionalBuild.orNull());
296313
}
297314

0 commit comments

Comments
 (0)