1
1
package com .offbytwo .jenkins .model ;
2
2
3
+ import static com .google .common .collect .Lists .transform ;
4
+
5
+ import java .io .IOException ;
3
6
import java .util .Collections ;
4
7
import java .util .List ;
5
8
9
+ import org .apache .http .HttpStatus ;
10
+ import org .apache .http .client .HttpResponseException ;
11
+
6
12
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 ;
7
16
import com .google .common .collect .Lists ;
17
+ import com .offbytwo .jenkins .client .util .EncodingUtils ;
18
+ import com .offbytwo .jenkins .helper .Range ;
8
19
9
20
public class MavenJobWithDetails extends MavenJob {
10
21
@@ -35,6 +46,12 @@ public boolean isBuildable() {
35
46
}
36
47
37
48
/**
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
+ *
38
55
* @return the list of {@link MavenBuild}. In case of no builds have been
39
56
* executed yet {@link Collections#emptyList()} will be returned.
40
57
*/
@@ -51,6 +68,102 @@ public MavenBuild apply(MavenBuild from) {
51
68
}
52
69
}
53
70
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
+
54
167
/**
55
168
* @return The firstBuild. If {@link #firstBuild} has never been run
56
169
* {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
@@ -169,6 +282,21 @@ public List<Job> getUpstreamProjects() {
169
282
}
170
283
}
171
284
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
+
172
300
private MavenBuild buildWithClient (MavenBuild from ) {
173
301
MavenBuild ret = new MavenBuild (from );
174
302
ret .setClient (client );
0 commit comments