forked from jenkinsci/java-client-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.java
More file actions
225 lines (192 loc) · 6.49 KB
/
Build.java
File metadata and controls
225 lines (192 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
*
* Distributed under the MIT license: http://opensource.org/licenses/MIT
*/
package com.offbytwo.jenkins.model;
import java.io.IOException;
import org.apache.http.client.HttpResponseException;
public class Build extends BaseModel {
/**
* This will be returned by the API in cases where no build has ever been
* executed like {@link JobWithDetails#getLastBuild()} etc. This will also
* be returned by {@link #details()} is the build has not been run.
*/
public static final Build BUILD_HAS_NEVER_RUN = new Build(-1, -1, "UNKNOWN") {
@Override
public TestReport getTestReport() {
return TestReport.NO_TEST_REPORT;
}
@Override
public BuildWithDetails details() {
// For a build which never has been run you couldn't get
// details about!
return BuildWithDetails.BUILD_HAS_NEVER_RUN;
}
};
/**
* This will be returned by the API in cases where a build has been
* cancelled.
*/
public static final Build BUILD_HAS_BEEN_CANCELLED = new Build(-1, -1, "CANCELLED") {
@Override
public TestReport getTestReport() {
return TestReport.NO_TEST_REPORT;
}
@Override
public BuildWithDetails details() {
return BuildWithDetails.BUILD_HAS_BEEN_CANCELLED;
}
};
private int number;
private int queueId;
private String url;
private JobWithDetails jobWithDetails;
private Build(int number, int queueId, String url) {
super();
this.number = number;
this.queueId = queueId;
this.url = url;
}
public Build() {
}
public Build(Build from) {
this(from.getNumber(), from.getUrl());
}
public Build(int number, String url) {
this.number = number;
this.url = url;
}
public int getNumber() {
return number;
}
public int getQueueId() {
return queueId;
}
public String getUrl() {
return url;
}
protected void setNumber(int number) {
this.number = number;
}
protected void setQueueId(int queueId) {
this.queueId = queueId;
}
protected void setUrl(String url) {
this.url = url;
}
public Build setJobWithDetails(JobWithDetails jobWithDetails) {
this.jobWithDetails = jobWithDetails;
return this;
}
public JobWithDetails getJobWithDetails() {
return jobWithDetails;
}
/**
*
* @return The information from Jenkins. In cases the build has never run
* {@link #BUILD_HAS_NEVER_RUN} will be returned.
* @throws IOException
* in case of an error.
*/
public BuildWithDetails details() throws IOException {
BuildWithDetails buildWithDetails = client.get(url, BuildWithDetails.class);
buildWithDetails.setBuild(this);
return buildWithDetails;
}
/**
* This is to get the information about {@link TestReport}
* for a Maven Job type.
* @return {@link TestReport}
* @throws IOException in case of an error.
*/
public TestReport getTestReport() throws IOException {
return client.get(this.getUrl() + "/testReport/?depth=1", TestReport.class);
}
/**
* This is to get the information about run tests for a
* non Maven job type.
* @return {@link TestResult}
* @throws IOException in case of an error.
*/
public TestResult getTestResult() throws IOException {
return client.get(this.getUrl() + "/testReport/?depth=1", TestResult.class);
}
/*
* This Change (Bad Practice) is due to inconsistencies in Jenkins various
* versions. Jenkins changed their API from post to get and from get to post
* and so on. For example version 1.565 is supporting stop method as a post
* call, version 1.609.1 support it as a get call and version 1.609.2
* support it as a post call Thus, when a get is not allowed, an 405 error
* message is generated "Method Not Allowed" and the stop isn't executed (a
* post is required). Changed the code in order to do a post call in case a
* get is not allowed.
*/
public String Stop() throws HttpResponseException, IOException {
try {
return client.get(url + "stop");
} catch (IOException ex) {
if (((HttpResponseException) ex).getStatusCode() == 405) {
stopPost();
return "";
}
}
return "";
}
/** Stops the build which is currently in progress. This version takes in
* a crumbFlag. In some cases , an error is thrown which reads
* "No valid crumb was included in the request". This stop method is used incase
* those issues occur
*
* @param crumbFlag flag used to specify if a crumb is passed into for the request
* @return the client url
* @throws HttpResponseException in case of an error.
* @throws IOException in case of an error.
*/
public String Stop(boolean crumbFlag) throws HttpResponseException, IOException {
try {
return client.get(url + "stop");
} catch (IOException ex) {
if (((HttpResponseException) ex).getStatusCode() == 405) {
stopPost(crumbFlag);
return "";
}
}
return "";
}
private void stopPost(boolean crumbFlag) throws HttpResponseException, IOException {
client.post(url + "stop", crumbFlag);
}
private void stopPost() throws HttpResponseException, IOException {
client.post(url + "stop");
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Build other = (Build) obj;
if (number != other.number)
return false;
if (queueId != other.queueId)
return false;
if (url == null) {
if (other.url != null)
return false;
} else if (!url.equals(other.url))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + number;
result = prime * result + queueId;
result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
}
}