Skip to content

Commit 50023d2

Browse files
committed
Fixed #200
o Added getAllBuilds() and getAllBuilds(Range range) in MavenJobWithDetails class.
1 parent d4a4741 commit 50023d2

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

ReleaseNotes.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public class QueueItem {
8080
}
8181
```
8282

83+
* [Fixed Issue 200][issue-200]
84+
85+
Added two methods to get all builds and a range of builds
86+
so we are more in line wiht JobWithDetails.
87+
88+
```java
89+
public class MavenJobWithDetails {
90+
public List<MavenBuild> getAllBuilds();
91+
public List<MavenBuild> getAllBuilds(Range range);
92+
}
93+
```
8394

8495
## Release 0.3.6
8596

@@ -672,6 +683,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
672683
[issue-182]: https://github.com/jenkinsci/java-client-api/issues/182
673684
[issue-186]: https://github.com/jenkinsci/java-client-api/issues/186
674685
[issue-198]: https://github.com/jenkinsci/java-client-api/issues/198
686+
[issue-200]: https://github.com/jenkinsci/java-client-api/issues/200
675687
[issue-201]: https://github.com/jenkinsci/java-client-api/issues/201
676688
[issue-202]: https://github.com/jenkinsci/java-client-api/issues/202
677689
[pull-123]: https://github.com/jenkinsci/java-client-api/pull/123
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
3+
*
4+
* Distributed under the MIT license: http://opensource.org/licenses/MIT
5+
*/
6+
7+
package com.offbytwo.jenkins.model;
8+
9+
import java.util.List;
10+
11+
/**
12+
* This class is only needed to get all builds in
13+
* {@link MavenJobWithDetails#getAllBuilds()}.
14+
*
15+
* @author Karl Heinz Marbaise
16+
*
17+
* NOTE: This class is not part of any public API
18+
*/
19+
class AllMavenBuilds extends BaseModel {
20+
private List<MavenBuild> allBuilds;
21+
22+
public AllMavenBuilds() {
23+
}
24+
25+
public List<MavenBuild> getAllBuilds() {
26+
return this.allBuilds;
27+
}
28+
}

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package com.offbytwo.jenkins.model;
22

3+
import static com.google.common.collect.Lists.transform;
4+
5+
import java.io.IOException;
36
import java.util.Collections;
47
import java.util.List;
58

9+
import org.apache.http.HttpStatus;
10+
import org.apache.http.client.HttpResponseException;
11+
612
import com.google.common.base.Function;
13+
import com.google.common.base.Optional;
14+
import com.google.common.base.Predicate;
15+
import com.google.common.collect.Iterables;
716
import com.google.common.collect.Lists;
17+
import com.offbytwo.jenkins.client.util.EncodingUtils;
18+
import com.offbytwo.jenkins.helper.Range;
819

920
public class MavenJobWithDetails extends MavenJob {
1021

@@ -35,6 +46,12 @@ public boolean isBuildable() {
3546
}
3647

3748
/**
49+
* This method will give you back the builds of a particular job.<br/>
50+
* <b>Note: Jenkins limits the number of results to a maximum of 100 builds
51+
* which you will get back.</b>. In case you have more than 100 build you
52+
* won't get back all builds via this method. In such cases you need to use
53+
* {@link #getAllBuilds()}.
54+
*
3855
* @return the list of {@link MavenBuild}. In case of no builds have been
3956
* executed yet {@link Collections#emptyList()} will be returned.
4057
*/
@@ -51,6 +68,102 @@ public MavenBuild apply(MavenBuild from) {
5168
}
5269
}
5370

71+
/**
72+
* This method will give you back all builds which exists independent of the
73+
* number. You should be aware that this can be much in some cases if you
74+
* have more than 100 builds which is by default limited by Jenkins
75+
* {@link #getBuilds()}. This method limits it to particular information
76+
* which can be later used to get supplemental information about a
77+
* particular build {@link Build#details()} to reduce the amount of data
78+
* which needed to be transfered.
79+
*
80+
* @return the list of {@link Build}. In case of no builds have been
81+
* executed yet return {@link Collections#emptyList()}.
82+
* @throws IOException
83+
* In case of failure.
84+
* @see <a href="https://issues.jenkins-ci.org/browse/JENKINS-30238">Jenkins
85+
* Issue</a>
86+
*/
87+
public List<MavenBuild> getAllBuilds() throws IOException {
88+
String path = "/";
89+
90+
try {
91+
List<MavenBuild> builds = client.get(path + "job/" + EncodingUtils.encode(this.getName())
92+
+ "?tree=allBuilds[number[*],url[*],queueId[*]]", AllMavenBuilds.class).getAllBuilds();
93+
94+
if (builds == null) {
95+
return Collections.emptyList();
96+
} else {
97+
return transform(builds, new Function<MavenBuild, MavenBuild>() {
98+
@Override
99+
public MavenBuild apply(MavenBuild from) {
100+
return buildWithClient(from);
101+
}
102+
});
103+
}
104+
} catch (HttpResponseException e) {
105+
// TODO: Thinks about a better handling if the job does not exist?
106+
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
107+
// TODO: Check this if this is necessary or a good idea?
108+
109+
return null;
110+
}
111+
throw e;
112+
}
113+
114+
}
115+
116+
/**
117+
*
118+
* <ul>
119+
* <li>{M,N}: From the M-th element (inclusive) to the N-th element
120+
* (exclusive).</li>
121+
* <li>{M,}: From the M-th element (inclusive) to the end.</li>
122+
* <li>{,N}: From the first element (inclusive) to the N-th element
123+
* (exclusive). The same as {0,N}.</li>
124+
* <li>{N}: Just retrieve the N-th element. The same as {N,N+1}.</li>
125+
* </ul>
126+
*
127+
* <b>Note: At the moment there seemed to be no option to get the number of
128+
* existing builds for a job. The only option is to get all builds via
129+
* {@link #getAllBuilds()}.</b>
130+
*
131+
* @param range
132+
* {@link Range}
133+
* @return the list of {@link Build}. In case of no builds have been
134+
* executed yet return {@link Collections#emptyList()}.
135+
* @throws IOException
136+
* in case of an error.
137+
*/
138+
public List<MavenBuild> getAllBuilds(Range range) throws IOException {
139+
String path = "/" + "job/" + EncodingUtils.encode(this.getName())
140+
+ "?tree=allBuilds[number[*],url[*],queueId[*]]";
141+
142+
try {
143+
List<MavenBuild> builds = client.get(path + range.getRangeString(), AllMavenBuilds.class).getAllBuilds();
144+
145+
if (builds == null) {
146+
return Collections.emptyList();
147+
} else {
148+
return transform(builds, new Function<MavenBuild, MavenBuild>() {
149+
@Override
150+
public MavenBuild apply(MavenBuild from) {
151+
return buildWithClient(from);
152+
}
153+
});
154+
}
155+
} catch (HttpResponseException e) {
156+
// TODO: Thinks about a better handline if the job does not exist?
157+
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
158+
// TODO: Check this if this is necessary or a good idea?
159+
160+
return null;
161+
}
162+
throw e;
163+
}
164+
}
165+
166+
54167
/**
55168
* @return The firstBuild. If {@link #firstBuild} has never been run
56169
* {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
@@ -169,6 +282,21 @@ public List<Job> getUpstreamProjects() {
169282
}
170283
}
171284

285+
public MavenBuild getBuildByNumber(final int buildNumber) {
286+
287+
Predicate<MavenBuild> isMatchingBuildNumber = new Predicate<MavenBuild>() {
288+
289+
@Override
290+
public boolean apply(MavenBuild input) {
291+
return input.getNumber() == buildNumber;
292+
}
293+
};
294+
295+
Optional<MavenBuild> optionalBuild = Iterables.tryFind(builds, isMatchingBuildNumber);
296+
// TODO: Check if we could use Build#NO...instead of Null?
297+
return optionalBuild.orNull() == null ? null : buildWithClient(optionalBuild.orNull());
298+
}
299+
172300
private MavenBuild buildWithClient(MavenBuild from) {
173301
MavenBuild ret = new MavenBuild(from);
174302
ret.setClient(client);

0 commit comments

Comments
 (0)