Skip to content

Commit 06ffef7

Browse files
committed
Fixed #104
o Added consistently QueueReference as return type to all build methods of the Job class.
1 parent 9ba5525 commit 06ffef7

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

ReleaseNotes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
### API Changes
1616

17+
* [Fixed Issue 104][issue-104] all build* methods now return consistently `QueueReference`
18+
to make it possible to query for a queued build and if a build from the queue has
19+
been cancelled or to see if a build is running.
20+
21+
```java
22+
public class Job extends BaseModel {
23+
public QueueReference build();
24+
public QueueReference build(boolean crumbFlag);
25+
public QueueReference build(Map<String, String> params);
26+
public QueueReference build(Map<String, String> params, boolean crumbFlag);
27+
}
28+
```
29+
1730
* [Fixed Issue 203][issue-203] with [pull request #204][pull-204]
1831
of RainerW <[email protected]>
1932

@@ -682,6 +695,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
682695
[issue-104]: https://github.com/jenkinsci/java-client-api/issues/104
683696
[issue-111]: https://github.com/jenkinsci/java-client-api/issues/111
684697
[issue-116]: https://github.com/jenkinsci/java-client-api/issues/116
698+
[issue-104]: https://github.com/jenkinsci/java-client-api/issues/104
685699
[issue-108]: https://github.com/jenkinsci/java-client-api/issues/108
686700
[issue-113]: https://github.com/jenkinsci/java-client-api/issues/113
687701
[issue-119]: https://github.com/jenkinsci/java-client-api/issues/119

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,18 @@ public String runScript(String script) throws IOException {
714714
}
715715

716716
public Queue getQueue() throws IOException {
717+
//TODO: Check if using depth=1 is a good idea?
717718
return client.get("queue/?depth=1", Queue.class);
718719
}
719720

720721
public QueueItem getQueueItem(QueueReference ref) throws IOException {
721722
try {
722723
String url = ref.getQueueItemUrlPart();
723724
// "/queue/item/" + id
724-
QueueItem job = client.get(url, QueueItem.class);
725-
job.setClient(client);
725+
QueueItem queueItem = client.get(url, QueueItem.class);
726+
queueItem.setClient(client);
726727

727-
return job;
728+
return queueItem;
728729
} catch (HttpResponseException e) {
729730
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
730731
return null;

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ public JobWithDetails details() throws IOException {
4848
/**
4949
* Get a file from workspace.
5050
*
51-
* @param fileName The name of the file to download from workspace.
52-
* You can also access files which are in sub folders of the workspace.
51+
* @param fileName
52+
* The name of the file to download from workspace. You can also
53+
* access files which are in sub folders of the workspace.
5354
* @return The string which contains the content of the file.
5455
* @throws IOException
5556
*/
5657
public String getFileFromWorkspace(String fileName) throws IOException {
57-
InputStream is = client.getFile( URI.create( url + "/ws/" + fileName ));
58+
InputStream is = client.getFile(URI.create(url + "/ws/" + fileName));
5859
ByteArrayOutputStream result = new ByteArrayOutputStream();
5960
byte[] buffer = new byte[1024];
6061
int length;
@@ -66,30 +67,44 @@ public String getFileFromWorkspace(String fileName) throws IOException {
6667

6768
/**
6869
* Trigger a build without parameters
70+
*
71+
* @return {@link QueueReference} for further analysis of the queued build.
72+
* @throws IOException
73+
* in case of an error.
6974
*/
70-
public void build() throws IOException {
71-
client.post(url + "build");
75+
public QueueReference build() throws IOException {
76+
ExtractHeader location = client.post(url + "build", null, ExtractHeader.class, false);
77+
return new QueueReference(location.getLocation());
78+
7279
}
7380

7481
/**
7582
* Trigger a build with crumbFlag.
76-
* @param crumbFlag true or false.
77-
* @throws IOException in case of an error.
83+
*
84+
* @param crumbFlag
85+
* true or false.
86+
* @return {@link QueueReference} for further analysis of the queued build.
87+
* @throws IOException
88+
* in case of an error.
7889
*/
79-
public void build(boolean crumbFlag) throws IOException {
80-
client.post(url + "build", crumbFlag);
90+
public QueueReference build(boolean crumbFlag) throws IOException {
91+
ExtractHeader location = client.post(url + "build", null, ExtractHeader.class, crumbFlag);
92+
return new QueueReference(location.getLocation());
8193
}
8294

8395
/**
8496
* Trigger a parameterized build
8597
*
8698
* @param params
8799
* the job parameters
100+
* @return {@link QueueReference} for further analysis of the queued build.
88101
* @throws IOException
89102
*/
90-
public void build(Map<String, String> params) throws IOException {
103+
public QueueReference build(Map<String, String> params) throws IOException {
91104
String qs = join(Collections2.transform(params.entrySet(), new MapEntryToQueryStringPair()), "&");
92105
client.post(url + "buildWithParameters?" + qs);
106+
ExtractHeader location = client.post(url + "buildWithParameters?" + qs, null, ExtractHeader.class, false);
107+
return new QueueReference(location.getLocation());
93108
}
94109

95110
/**
@@ -99,6 +114,7 @@ public void build(Map<String, String> params) throws IOException {
99114
* the job parameters
100115
* @param crumbFlag
101116
* determines whether crumb flag is used
117+
* @return {@link QueueReference} for further analysis of the queued build.
102118
* @throws IOException
103119
*/
104120
public QueueReference build(Map<String, String> params, boolean crumbFlag) throws IOException {

0 commit comments

Comments
 (0)