Skip to content

Commit 3d8fcd4

Browse files
author
gdy
committed
continue
1 parent c6a0b45 commit 3d8fcd4

File tree

11 files changed

+168
-60
lines changed

11 files changed

+168
-60
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ Pk-jenkins is a framework used to communicate with Jenkins server exposing base
77

88
# Terms
99
- Job
10-
- Alias of project in Jenkins terms
10+
- Alias of Project in Jenkins terms
11+
- Job Detailed
12+
- An user-level Job description: name, builds list etc
13+
- Job Config Xml
14+
- A Jenkins-level Job description:
15+
1116

1217
# Using pk-jenkins Api
1318
- Create IJenkinsApi implementation class providing Jenkins server url, username and password
1419
```java
1520
IJenkinsApi jenkins = new JenkinsApi("http://localhost:8080", "user", "password");
1621
```
1722
- `void createJob(String jobName, String jobConfigXML)`
18-
- Creates new job named `jobName` with provide job config xml. Use `getJobConfigXml()` to obtain one from an existing Job
23+
- Creates new job named `jobName` with provided job config xml. Use `getJobConfigXml()` to obtain one from an existing Job
1924

2025

2126

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ dependencies {
2121
compile 'commons-io:commons-io:2.4'
2222
compile 'com.google.code.gson:gson:2.8.0'
2323
compile 'org.apache.httpcomponents:httpclient:4.5.2'
24+
compile 'commons-httpclient:commons-httpclient:3.1'
25+
2426
}
2527

2628
task sourcesJar(type: Jar, dependsOn: classes) {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22

33
import java.util.List;
44

5+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJExists;
6+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJNotFound;
57
import com.projectkaiser.scm.jenkins.data.JobDetailed;
68
import com.projectkaiser.scm.jenkins.data.QueueItem;
79

810
public interface IJenkinsApi {
911

10-
void createJob(String jobName, String jobConfigXML);
12+
void createJob(String jobName, String jobConfigXML) throws EPKJExists;
1113

12-
JobDetailed getJobDetailed(String jobName);
14+
JobDetailed getJobDetailed(String jobName) throws EPKJNotFound;
1315

14-
Long enqueueBuild(String jobName);
16+
Long enqueueBuild(String jobName) throws EPKJNotFound;
1517

16-
QueueItem getBuild(Long buildId);
18+
QueueItem getBuild(Long buildId) throws EPKJNotFound;
1719

18-
void updateJobConfigXml(String jobName, String configXml);
20+
void updateJobConfigXml(String jobName, String configXml) throws EPKJNotFound;
1921

20-
void copyJob(String srcName, String dstName);
22+
void copyJob(String srcName, String dstName) throws EPKJNotFound, EPKJExists;
2123

2224
List<String> getJobsList();
2325

24-
String getJobConfigXml(String jobName);
26+
String getJobConfigXml(String jobName) throws EPKJNotFound;
2527

26-
void deleteJob(String jobName);
28+
void deleteJob(String jobName) throws EPKJNotFound;
2729

2830
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
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;
17+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJExists;
18+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJNotFound;
19+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJenkinsException;
1820
import com.projectkaiser.scm.jenkins.api.facade.IJenkinsApiFacade;
1921
import com.projectkaiser.scm.jenkins.api.facade.JenkinsApiHttpFacade;
2022
import com.projectkaiser.scm.jenkins.data.JobDetailed;
@@ -30,19 +32,19 @@ public JenkinsApi(String baseAddress, String user, String password) {
3032
}
3133

3234
@Override
33-
public Long enqueueBuild(String jobName) {
35+
public Long enqueueBuild(String jobName) throws EPKJNotFound {
3436
String url = "job/" + encodeUrl(jobName) + "/build";
3537
HttpResponse resp = facade.getResponsePOST(url, null);
3638
Header[] headers = resp.getHeaders("Location");
3739
if (headers == null || headers.length == 0) {
38-
throw new ESCMJenkinsException("Failed to obtain Location header from response");
40+
throw new EPKJenkinsException("Failed to obtain Location header from response");
3941
}
4042
String[] strs = headers[0].getValue().split("/");
4143
return Long.parseLong(strs[strs.length - 1]);
4244
}
4345

4446
@Override
45-
public QueueItem getBuild(Long buildId) {
47+
public QueueItem getBuild(Long buildId) throws EPKJNotFound {
4648
String url = String.format("queue/item/%d/api/json?pretty=true", buildId);
4749
String queueItemJson = facade.getResponseContentGET(url);
4850
Gson gson = new GsonBuilder().setPrettyPrinting().create();
@@ -51,7 +53,7 @@ public QueueItem getBuild(Long buildId) {
5153
}
5254

5355
@Override
54-
public void copyJob(String srcName, String dstName) {
56+
public void copyJob(String srcName, String dstName) throws EPKJNotFound, EPKJExists {
5557
String url = "createItem";
5658
Map<String, String> q = new HashMap<String, String>();
5759
q.put("name", dstName);
@@ -62,7 +64,7 @@ public void copyJob(String srcName, String dstName) {
6264
}
6365

6466
@Override
65-
public void createJob(String jobName, String jobConfigXML) {
67+
public void createJob(String jobName, String jobConfigXML) throws EPKJExists {
6668
String url = "createItem";
6769
Map<String, String> q = new HashMap<String, String>();
6870
q.put("name", jobName);
@@ -111,19 +113,19 @@ public List<String> getJobsList() {
111113
}
112114

113115
@Override
114-
public String getJobConfigXml(String jobName) {
116+
public String getJobConfigXml(String jobName) throws EPKJNotFound {
115117
String url = "job/" + encodeUrl(jobName) + "/config.xml";
116118
return facade.getResponseContentGET(url);
117119
}
118120

119121
@Override
120-
public void updateJobConfigXml(String jobName, String configXml) {
122+
public void updateJobConfigXml(String jobName, String configXml) throws EPKJNotFound {
121123
String url = "job/" + encodeUrl(jobName) + "/config.xml";
122124
facade.getResponsePOST(url, configXml);
123125
}
124126

125127
@Override
126-
public JobDetailed getJobDetailed(String jobName) {
128+
public JobDetailed getJobDetailed(String jobName) throws EPKJNotFound {
127129
String url = "job/" + encodeUrl(jobName) + "/api/json?pretty=true";
128130
String json = facade.getResponseContentGET(url);
129131
Gson gson = new GsonBuilder().setPrettyPrinting().create();
@@ -132,7 +134,7 @@ public JobDetailed getJobDetailed(String jobName) {
132134
}
133135

134136
@Override
135-
public void deleteJob(String jobName) {
137+
public void deleteJob(String jobName) throws EPKJNotFound {
136138
String url = "job/" + encodeUrl(jobName) + "/doDelete";
137139
facade.getResponsePOST(url, null);
138140
}
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 EPKJExists extends EPKJenkinsServerException {
4+
5+
public EPKJExists(Integer code, String errorMessage) {
6+
super(code, errorMessage);
7+
}
8+
9+
private static final long serialVersionUID = 7637144484632267911L;
10+
11+
}
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 EPKJNotFound extends EPKJenkinsServerException {
4+
5+
public EPKJNotFound(Integer code, String errorMes) {
6+
super(code, errorMes);
7+
}
8+
9+
private static final long serialVersionUID = 2478559349028227084L;
10+
11+
}

src/main/java/com/projectkaiser/scm/jenkins/api/exceptions/ESCMJenkinsException.java renamed to src/main/java/com/projectkaiser/scm/jenkins/api/exceptions/EPKJenkinsException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.projectkaiser.scm.jenkins.api.exceptions;
22

3-
public class ESCMJenkinsException extends RuntimeException {
3+
public class EPKJenkinsException extends RuntimeException {
44

5-
public ESCMJenkinsException(String string) {
5+
public EPKJenkinsException(String string) {
66
super(string);
77
}
88

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.projectkaiser.scm.jenkins.api.exceptions;
2+
3+
import org.apache.commons.httpclient.HttpStatus;
4+
5+
public class EPKJenkinsServerException extends EPKJenkinsException {
6+
7+
private static final long serialVersionUID = 8612747334003389372L;
8+
private String errorMessage;
9+
private Integer code;
10+
11+
public String getErrorMessage() {
12+
return errorMessage;
13+
}
14+
15+
public Integer getCode() {
16+
return code;
17+
}
18+
19+
public EPKJenkinsServerException(Integer code, String errorMessage) {
20+
super(HttpStatus.getStatusText(code));
21+
this.code = code;
22+
this.errorMessage = errorMessage;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return String.format("%s (%d: %s)", errorMessage, code, super.getMessage());
28+
}
29+
}

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
import org.apache.http.impl.client.HttpClientBuilder;
1414
import org.apache.http.protocol.HTTP;
1515

16+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJExists;
17+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJNotFound;
18+
import com.projectkaiser.scm.jenkins.api.exceptions.EPKJenkinsServerException;
19+
1620

1721
public class JenkinsApiHttpFacade implements IJenkinsApiFacade {
1822

23+
private static final String JOB_EXISTS_MESSAGE = "A job already exists with the name";
24+
private static final CharSequence NO_SUCH_JOB_MESSAGE = "No such job";
1925
private CloseableHttpClient client;
2026
private String baseAddress;
2127
private String user;
@@ -65,11 +71,7 @@ public JenkinsApiHttpFacade(String baseAddress, String user, String password) {
6571
}
6672

6773
public static boolean isEmptyString(String s) {
68-
if (null == s)
69-
return true;
70-
if (s.length() == 0)
71-
return true;
72-
return false;
74+
return s == null || s.isEmpty();
7375
}
7476

7577
private HttpResponse getResponse (HttpRequestBase request) {
@@ -78,18 +80,18 @@ private HttpResponse getResponse (HttpRequestBase request) {
7880
String basicAuth = "Basic " + new String(Base64.encodeBase64(userpass.getBytes()));
7981
request.setHeader("Authorization", basicAuth);
8082
}
81-
83+
84+
HttpResponse response;
8285
try {
83-
HttpResponse response = client.execute(request);
84-
try {
85-
processResponse(response);
86-
return response;
87-
} finally {
88-
request.releaseConnection();
89-
}
86+
response = client.execute(request);
9087
} catch (Exception e) {
9188
throw new RuntimeException(e);
89+
} finally {
90+
request.releaseConnection();
9291
}
92+
93+
processResponse(response);
94+
return response;
9395
}
9496

9597
private String responseToString(HttpResponse response) {
@@ -125,22 +127,19 @@ public HttpResponse getResponsePOST(String url, String entity) {
125127
private void processResponse(HttpResponse response) {
126128
int code = response.getStatusLine().getStatusCode();
127129
if (code < HttpStatus.SC_OK || code >= HttpStatus.SC_BAD_REQUEST) {
128-
String errorMes;
130+
if (code == HttpStatus.SC_NOT_FOUND) {
131+
throw new EPKJNotFound(code, "Resource not found");
132+
}
129133
if (response.containsHeader("X-Error")) {
130-
errorMes = response.getLastHeader("X-Error").getValue();
131-
} else if (response.containsHeader(HTTP.CONTENT_TYPE) &&
132-
response.getLastHeader(HTTP.CONTENT_TYPE).getValue().contains(("text/html"))) {
133-
try {
134-
errorMes = IOUtils.toString(response.getEntity().getContent());
135-
} catch(Exception e) {
136-
errorMes = "Failed to read response fom Jenkins server: " + e.getMessage();
134+
String errorMes = response.getLastHeader("X-Error").getValue();
135+
if (errorMes.contains(JOB_EXISTS_MESSAGE)) {
136+
throw new EPKJExists(code, errorMes);
137+
} else if(errorMes.contains(NO_SUCH_JOB_MESSAGE)) {
138+
throw new EPKJNotFound(code, errorMes);
137139
}
138140
} else {
139-
errorMes = "Unknown error";
141+
throw new EPKJenkinsServerException(code, "Server error");
140142
}
141-
throw new RuntimeException(
142-
String.format("Jenkins server returned code %d: %s", code, errorMes));
143143
}
144144
}
145-
146145
}

src/main/java/com/projectkaiser/scm/jenkins/data/QueueItem.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.projectkaiser.scm.jenkins.data;
22

33
public class QueueItem {
4-
// actions
5-
64
private boolean blocked;
75

86
private boolean buildable;
@@ -15,8 +13,6 @@ public class QueueItem {
1513

1614
private boolean stuck;
1715

18-
// task
19-
2016
private String url;
2117

2218
private String why;

0 commit comments

Comments
 (0)