Skip to content

Commit 0af41dc

Browse files
zxq1002khmarbaise
authored andcommitted
Fixed #276: Support to trigger a parameterized build with file parameters (#278)
Fixed #276 * Support to trigger a parameterized build with file parameters * Support to trigger a parameterized build with file parameters by using "triggerJobAndWaitUntilFinished" method. * fix merge error and use overload to avoid changing the contract of post method
1 parent ed4aae1 commit 0af41dc

File tree

6 files changed

+147
-8
lines changed

6 files changed

+147
-8
lines changed

jenkins-client/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
<artifactId>httpclient</artifactId>
7272
</dependency>
7373

74+
<dependency>
75+
<groupId>org.apache.httpcomponents</groupId>
76+
<artifactId>httpcore</artifactId>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.apache.httpcomponents</groupId>
81+
<artifactId>httpmime</artifactId>
82+
</dependency>
83+
7484
<dependency>
7585
<groupId>jaxen</groupId>
7686
<artifactId>jaxen</artifactId>

jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsTriggerHelper.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.offbytwo.jenkins;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.util.Map;
56

@@ -89,6 +90,49 @@ public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map<Strin
8990
return triggerJobAndWaitUntilFinished(jobName, queueRef);
9091
}
9192

93+
/**
94+
* This method will trigger a build of the given job and will wait until the
95+
* builds is ended or if the build has been cancelled.
96+
*
97+
* @param jobName The name of the job which should be triggered.
98+
* @param params the job parameters
99+
* @param fileParams the job file parameters
100+
* @param crumbFlag set to <code>true</code> or <code>false</code>.
101+
* @return In case of an cancelled job you will get
102+
* {@link BuildWithDetails#getResult()}
103+
* {@link BuildResult#CANCELLED}. So you have to check first if the
104+
* build result is {@code CANCELLED}.
105+
* @throws IOException in case of errors.
106+
* @throws InterruptedException In case of interrupts.
107+
*/
108+
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map<String, String> params,
109+
Map<String, File> fileParams,
110+
boolean crumbFlag) throws IOException, InterruptedException {
111+
JobWithDetails job = this.server.getJob(jobName);
112+
QueueReference queueRef = job.build(params, fileParams, crumbFlag);
113+
114+
return triggerJobAndWaitUntilFinished(jobName, queueRef);
115+
}
116+
117+
/**
118+
* This method will trigger a build of the given job and will wait until the
119+
* builds is ended or if the build has been cancelled.
120+
*
121+
* @param jobName The name of the job which should be triggered.
122+
* @param params the job parameters
123+
* @param fileParams the job file parameters
124+
* @return In case of an cancelled job you will get
125+
* {@link BuildWithDetails#getResult()}
126+
* {@link BuildResult#CANCELLED}. So you have to check first if the
127+
* build result is {@code CANCELLED}.
128+
* @throws IOException in case of errors.
129+
* @throws InterruptedException In case of interrupts.
130+
*/
131+
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map<String, String> params,
132+
Map<String, File> fileParams) throws IOException, InterruptedException {
133+
return triggerJobAndWaitUntilFinished(jobName, params, fileParams, false);
134+
}
135+
92136
/**
93137
* This method will trigger a build of the given job and will wait until the
94138
* builds is ended or if the build has been cancelled.

jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import net.sf.json.JSONObject;
1818
import org.apache.commons.io.IOUtils;
1919
import org.apache.commons.lang.StringUtils;
20+
import org.apache.http.Header;
21+
import org.apache.http.HttpEntity;
2022
import org.apache.http.HttpResponse;
2123
import org.apache.http.NameValuePair;
2224
import org.apache.http.auth.AuthScope;
@@ -28,6 +30,9 @@
2830
import org.apache.http.client.methods.HttpRequestBase;
2931
import org.apache.http.entity.ContentType;
3032
import org.apache.http.entity.StringEntity;
33+
import org.apache.http.entity.mime.HttpMultipartMode;
34+
import org.apache.http.entity.mime.MultipartEntityBuilder;
35+
import org.apache.http.entity.mime.content.FileBody;
3136
import org.apache.http.impl.auth.BasicScheme;
3237
import org.apache.http.impl.client.BasicCredentialsProvider;
3338
import org.apache.http.impl.client.CloseableHttpClient;
@@ -39,6 +44,7 @@
3944
import org.slf4j.Logger;
4045
import org.slf4j.LoggerFactory;
4146

47+
import java.io.File;
4248
import java.io.IOException;
4349
import java.io.InputStream;
4450
import java.net.URI;
@@ -204,14 +210,22 @@ public InputStream getFile(URI path) throws IOException {
204210
*/
205211
@Override
206212
public <R extends BaseModel, D> R post(String path, D data, Class<R> cls) throws IOException {
207-
return post(path, data, cls, true);
213+
return post(path, data, cls, null, true);
208214
}
209215

210216
/**
211217
* {@inheritDoc}
212218
*/
213219
@Override
214220
public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolean crumbFlag) throws IOException {
221+
return post(path, data, cls, null, crumbFlag);
222+
}
223+
224+
/**
225+
* {@inheritDoc}
226+
*/
227+
@Override
228+
public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, Map<String, File> fileParams, boolean crumbFlag) throws IOException {
215229
HttpPost request = new HttpPost(UrlUtils.toJsonApiUri(uri, context, path));
216230
if (crumbFlag == true) {
217231
Crumb crumb = getQuietly("/crumbIssuer", Crumb.class);
@@ -225,6 +239,21 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolea
225239
StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_JSON);
226240
request.setEntity(stringEntity);
227241
}
242+
243+
// Prepare file parameters
244+
if(fileParams != null && !(fileParams.isEmpty())) {
245+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
246+
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
247+
248+
for (Map.Entry<String, File> entry : fileParams.entrySet()) {
249+
FileBody fileBody = new FileBody(entry.getValue());
250+
builder.addPart(entry.getKey(), fileBody);
251+
}
252+
253+
HttpEntity entity = builder.build();
254+
request.setEntity(entity);
255+
}
256+
228257
HttpResponse response = client.execute(request, localContext);
229258
jenkinsVersion = ResponseUtils.getJenkinsVersion(response);
230259

@@ -392,15 +421,15 @@ public String post_text(String path, String textData, ContentType contentType, b
392421
*/
393422
@Override
394423
public void post(String path) throws IOException {
395-
post(path, null, null, false);
424+
post(path, null, null, null, false);
396425
}
397426

398427
/**
399428
* {@inheritDoc}
400429
*/
401430
@Override
402431
public void post(String path, boolean crumbFlag) throws IOException {
403-
post(path, null, null, crumbFlag);
432+
post(path, null, null,null, crumbFlag);
404433
}
405434

406435
/**

jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpConnection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.offbytwo.jenkins.model.BaseModel;
99
import java.io.Closeable;
10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.net.URI;
@@ -109,6 +110,21 @@ public interface JenkinsHttpConnection extends Closeable {
109110
*/
110111
<R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolean crumbFlag) throws IOException;
111112

113+
/**
114+
* Perform a POST request and parse the response to the given class
115+
*
116+
* @param path path to request, can be relative or absolute
117+
* @param data data to post
118+
* @param cls class of the response
119+
* @param fileParams file parameters
120+
* @param <R> type of the response
121+
* @param <D> type of the data
122+
* @param crumbFlag true / false.
123+
* @return an instance of the supplied class
124+
* @throws IOException in case of an error.
125+
*/
126+
<R extends BaseModel, D> R post(String path, D data, Class<R> cls, Map<String, File> fileParams, boolean crumbFlag) throws IOException;
127+
112128
/**
113129
* Perform POST request that takes no parameters and returns no response
114130
*

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.apache.commons.lang.StringUtils.join;
1010

1111
import java.io.ByteArrayOutputStream;
12+
import java.io.File;
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.net.URI;
@@ -102,27 +103,52 @@ public QueueReference build(boolean crumbFlag) throws IOException {
102103
}
103104

104105
/**
105-
* Trigger a parameterized build
106+
* Trigger a parameterized build with string parameters only
106107
*
107108
* @param params the job parameters
108109
* @return {@link QueueReference} for further analysis of the queued build.
109110
* @throws IOException in case of an error.
110111
*/
111112
public QueueReference build(Map<String, String> params) throws IOException {
112-
return build(params, false);
113+
return build(params, null,false);
113114
}
114115

115116
/**
116-
* Trigger a parameterized build
117+
* Trigger a parameterized build with string parameters only
117118
*
118119
* @param params the job parameters
119-
* @param crumbFlag determines whether crumb flag is used
120+
* @param crumbFlag true or false.
120121
* @return {@link QueueReference} for further analysis of the queued build.
121122
* @throws IOException in case of an error.
122123
*/
123124
public QueueReference build(Map<String, String> params, boolean crumbFlag) throws IOException {
125+
return build(params,null,crumbFlag);
126+
}
127+
128+
/**
129+
* Trigger a parameterized build with file parameters
130+
*
131+
* @param params the job parameters
132+
* @param fileParams the job file parameters
133+
* @return {@link QueueReference} for further analysis of the queued build.
134+
* @throws IOException in case of an error.
135+
*/
136+
public QueueReference build(Map<String, String> params, Map<String, File> fileParams) throws IOException {
137+
return build(params,fileParams,false);
138+
}
139+
140+
/**
141+
* Trigger a parameterized build with file parameters and crumbFlag
142+
*
143+
* @param params the job parameters
144+
* @param fileParams the job file parameters
145+
* @param crumbFlag determines whether crumb flag is used
146+
* @return {@link QueueReference} for further analysis of the queued build.
147+
* @throws IOException in case of an error.
148+
*/
149+
public QueueReference build(Map<String, String> params, Map<String, File> fileParams, boolean crumbFlag) throws IOException {
124150
String qs = join(Collections2.transform(params.entrySet(), new MapEntryToQueryStringPair()), "&");
125-
ExtractHeader location = client.post(url + "buildWithParameters?" + qs, null, ExtractHeader.class, crumbFlag);
151+
ExtractHeader location = client.post(url + "buildWithParameters?" + qs,null, ExtractHeader.class, fileParams, crumbFlag);
126152
return new QueueReference(location.getLocation());
127153
}
128154

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
<guava.version>17.0</guava.version>
6262
<json-lib.version>2.4</json-lib.version>
6363
<httpclient.version>4.3.6</httpclient.version>
64+
<httpcore.version>4.3.3</httpcore.version>
65+
<httpmime.version>4.3.6</httpmime.version>
6466
<jackson-databind.version>2.3.4</jackson-databind.version>
6567
</properties>
6668

@@ -148,6 +150,18 @@
148150
<version>${httpclient.version}</version>
149151
</dependency>
150152

153+
<dependency>
154+
<groupId>org.apache.httpcomponents</groupId>
155+
<artifactId>httpcore</artifactId>
156+
<version>${httpcore.version}</version>
157+
</dependency>
158+
159+
<dependency>
160+
<groupId>org.apache.httpcomponents</groupId>
161+
<artifactId>httpmime</artifactId>
162+
<version>${httpmime.version}</version>
163+
</dependency>
164+
151165
<dependency>
152166
<groupId>jaxen</groupId>
153167
<artifactId>jaxen</artifactId>

0 commit comments

Comments
 (0)