Skip to content

Commit be6bbc2

Browse files
author
gdy
committed
continue
1 parent ffbc677 commit be6bbc2

File tree

7 files changed

+201
-36
lines changed

7 files changed

+201
-36
lines changed

src/main/java/com/projectkaiser/scm/jenkins/api/IJenkinsApi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import com.projectkaiser.scm.jenkins.data.JobDetailed;
6+
import com.projectkaiser.scm.jenkins.data.QueueItem;
67

78
public interface IJenkinsApi {
89

@@ -11,6 +12,8 @@ public interface IJenkinsApi {
1112
JobDetailed getJobDetailed(String jobName);
1213

1314
Long enqueueBuild(String jobName);
15+
16+
QueueItem getBuild(Long buildId);
1417

1518
void updateJobConfigXml(String jobName, String configXml);
1619

src/main/java/com/projectkaiser/scm/jenkins/api/JenkinsApi.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import com.google.gson.Gson;
1515
import com.google.gson.GsonBuilder;
1616
import com.google.gson.reflect.TypeToken;
17+
import com.projectkaiser.scm.jenkins.api.exceptions.ESCMJenkinsException;
1718
import com.projectkaiser.scm.jenkins.api.facade.IJenkinsApiFacade;
1819
import com.projectkaiser.scm.jenkins.api.facade.JenkinsApiHttpFacade;
1920
import com.projectkaiser.scm.jenkins.data.JobDetailed;
2021
import com.projectkaiser.scm.jenkins.data.JobListElement;
22+
import com.projectkaiser.scm.jenkins.data.QueueItem;
2123

2224
public class JenkinsApi implements IJenkinsApi {
2325

@@ -33,12 +35,20 @@ public Long enqueueBuild(String jobName) {
3335
HttpResponse resp = facade.getResponsePOST(url, null);
3436
Header[] headers = resp.getHeaders("Location");
3537
if (headers == null || headers.length == 0) {
36-
return -1L;
38+
throw new ESCMJenkinsException("Failed to obtain Location header from response");
3739
}
38-
3940
String[] strs = headers[0].getValue().split("/");
4041
return Long.parseLong(strs[strs.length - 1]);
4142
}
43+
44+
@Override
45+
public QueueItem getBuild(Long buildId) {
46+
String url = String.format("queue/item/%d/api/json?pretty=true", buildId);
47+
String queueItemJson = facade.getResponseContentGET(url);
48+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
49+
QueueItem queueItem = gson.fromJson(queueItemJson, QueueItem.class);
50+
return queueItem;
51+
}
4252

4353
@Override
4454
public void copyJob(String srcName, String dstName) {
@@ -126,4 +136,6 @@ public void deleteJob(String jobName) {
126136
String url = "job/" + encodeUrl(jobName) + "/doDelete";
127137
facade.getResponsePOST(url, null);
128138
}
139+
140+
129141
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.projectkaiser.scm.jenkins.api.exceptions;
2+
3+
public class ESCMJenkinsException extends RuntimeException {
4+
5+
public ESCMJenkinsException(String string) {
6+
super(string);
7+
}
8+
9+
private static final long serialVersionUID = 5415122631779829397L;
10+
11+
}

src/main/java/com/projectkaiser/scm/jenkins/api/facade/JenkinsApiHttpFacade.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.http.impl.client.CloseableHttpClient;
1313
import org.apache.http.impl.client.HttpClientBuilder;
1414
import org.apache.http.protocol.HTTP;
15-
import org.apache.http.util.EntityUtils;
1615

1716

1817
public class JenkinsApiHttpFacade implements IJenkinsApiFacade {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.projectkaiser.scm.jenkins.data;
2+
3+
public class Executable {
4+
Long number;
5+
6+
String url;
7+
8+
public Long getNumber() {
9+
return number;
10+
}
11+
12+
public void setNumber(Long number) {
13+
this.number = number;
14+
}
15+
16+
public String getUrl() {
17+
return url;
18+
}
19+
20+
public void setUrl(String url) {
21+
this.url = url;
22+
}
23+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.projectkaiser.scm.jenkins.data;
2+
3+
public class QueueItem {
4+
// actions
5+
6+
private boolean blocked;
7+
8+
private boolean buildable;
9+
10+
private Long id;
11+
12+
private Long inQueueSince;
13+
14+
private String params;
15+
16+
private boolean stuck;
17+
18+
// task
19+
20+
private String url;
21+
22+
private String why;
23+
24+
private boolean cancelled;
25+
26+
private Executable executable;
27+
28+
public boolean isBlocked() {
29+
return blocked;
30+
}
31+
32+
public void setBlocked(boolean blocked) {
33+
this.blocked = blocked;
34+
}
35+
36+
public boolean isBuildable() {
37+
return buildable;
38+
}
39+
40+
public void setBuildable(boolean buildable) {
41+
this.buildable = buildable;
42+
}
43+
44+
public Long getId() {
45+
return id;
46+
}
47+
48+
public void setId(Long id) {
49+
this.id = id;
50+
}
51+
52+
public Long getInQueueSince() {
53+
return inQueueSince;
54+
}
55+
56+
public void setInQueueSince(Long inQueueSince) {
57+
this.inQueueSince = inQueueSince;
58+
}
59+
60+
public String getParams() {
61+
return params;
62+
}
63+
64+
public void setParams(String params) {
65+
this.params = params;
66+
}
67+
68+
public boolean isStuck() {
69+
return stuck;
70+
}
71+
72+
public void setStuck(boolean stuck) {
73+
this.stuck = stuck;
74+
}
75+
76+
public String getUrl() {
77+
return url;
78+
}
79+
80+
public void setUrl(String url) {
81+
this.url = url;
82+
}
83+
84+
public String getWhy() {
85+
return why;
86+
}
87+
88+
public void setWhy(String why) {
89+
this.why = why;
90+
}
91+
92+
public boolean isCancelled() {
93+
return cancelled;
94+
}
95+
96+
public void setCancelled(boolean cancelled) {
97+
this.cancelled = cancelled;
98+
}
99+
100+
public Executable getExecutable() {
101+
return executable;
102+
}
103+
104+
public void setExecutable(Executable executable) {
105+
this.executable = executable;
106+
}
107+
}

src/test/java/com/projectkaiser/scm/jenkins/api/JenkinsApiTest.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
56
import static org.junit.Assert.assertTrue;
67

78
import java.io.BufferedReader;
8-
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
910
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.io.InputStreamReader;
12-
import java.io.StringWriter;
13-
14-
import javax.xml.parsers.DocumentBuilder;
15-
import javax.xml.parsers.DocumentBuilderFactory;
16-
import javax.xml.parsers.ParserConfigurationException;
1713

1814
import org.custommonkey.xmlunit.XMLUnit;
1915
import org.junit.After;
@@ -22,11 +18,15 @@
2218
import org.junit.Test;
2319
import org.w3c.dom.Document;
2420
import org.w3c.dom.NodeList;
25-
import org.xml.sax.SAXException;
21+
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
22+
import org.w3c.dom.ls.DOMImplementationLS;
23+
import org.w3c.dom.ls.LSInput;
24+
import org.w3c.dom.ls.LSOutput;
25+
import org.w3c.dom.ls.LSParser;
26+
import org.w3c.dom.ls.LSSerializer;
2627

2728
import com.projectkaiser.scm.jenkins.data.JobDetailed;
28-
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
29-
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
29+
import com.projectkaiser.scm.jenkins.data.QueueItem;
3030

3131
public class JenkinsApiTest {
3232

@@ -79,20 +79,23 @@ public void testCreateJob() throws Exception {
7979
}
8080

8181
@Test
82-
public void testEnqueueBuild() throws Exception {
83-
Long jobId = api.enqueueBuild(TEST_JOB_NAME);
84-
assertTrue(jobId > 0);
82+
public void testEnqueueBuild() {
83+
Long buildId = api.enqueueBuild(TEST_JOB_NAME);
84+
assertTrue(buildId > 0);
85+
assertNotNull(api.getBuild(buildId));
8586
}
86-
8787

88-
public void JenkinsAPITest() throws ParserConfigurationException, SAXException, IOException {
89-
String config = api.getJobConfigXml("test job");
90-
api.createJob("newe4job", config);
88+
@Test
89+
public void testGetBuild() {
90+
Long buildId = api.enqueueBuild(TEST_JOB_NAME);
91+
QueueItem item = api.getBuild(buildId);
92+
assertNotNull(item);
93+
assertNotNull(item.getId());
94+
assertEquals(item.getUrl(), String.format("queue/item/%d/", item.getId()));
9195
}
92-
93-
96+
9497
@Test
95-
public void testChangeJob() throws ParserConfigurationException, SAXException, IOException {
98+
public void testChangeJob() throws Exception {
9699
Document doc = getJobDocument(TEST_JOB_NAME);
97100
NodeList nodes = doc.getElementsByTagName("disabled");
98101
nodes.item(0).setTextContent("true");
@@ -123,22 +126,29 @@ public void testDeleteJob() {
123126
}
124127
}
125128

126-
private void updateJob(String jobName, Document doc) throws IOException {
127-
OutputFormat format = new OutputFormat (doc);
128-
StringWriter stringOut = new StringWriter ();
129-
XMLSerializer serial = new XMLSerializer (stringOut,
130-
format);
131-
serial.serialize(doc);
132-
api.updateJobConfigXml(jobName, stringOut.toString());
129+
private void updateJob(String jobName, Document doc) throws Exception {
130+
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
131+
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
132+
LSSerializer serializer = impl.createLSSerializer();
133+
LSOutput output = impl.createLSOutput();
134+
output.setEncoding("UTF-8");
135+
ByteArrayOutputStream stringOut = new ByteArrayOutputStream();
136+
output.setByteStream(stringOut);
137+
serializer.write(doc, output);
138+
api.updateJobConfigXml(jobName, new String(stringOut.toByteArray(), "UTF-8"));
133139
}
134140

135-
private Document getJobDocument(String jobName) throws ParserConfigurationException, SAXException, IOException {
136-
137-
String xml = api.getJobConfigXml(jobName);
138-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
139-
DocumentBuilder db = dbf.newDocumentBuilder();
140-
ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes("UTF-8"));
141-
Document doc = db.parse(input);
141+
private Document getJobDocument(String jobName) throws Exception {
142+
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
143+
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
144+
145+
LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
146+
LSInput input = impl.createLSInput();
147+
148+
String xml = api.getJobConfigXml(jobName);
149+
input.setStringData(xml);
150+
151+
Document doc = parser.parse(input);
142152
return doc;
143153
}
144154

0 commit comments

Comments
 (0)