Skip to content

Commit 47063d0

Browse files
Upgrade to apache-httpcomponents-client-5-api
1 parent 28a4b42 commit 47063d0

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

Jenkinsfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
buildPlugin(useContainerAgent: true, configurations: [
2-
[ platform: 'linux', jdk: '8' ],
32
[ platform: 'linux', jdk: '11' ],
43
[ platform: 'windows', jdk: '11' ],
54
])

pom.xml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<properties>
5454
<revision>2.2.1</revision>
5555
<changelist>-SNAPSHOT</changelist>
56-
<jenkins.version>2.319.1</jenkins.version>
56+
<jenkins.version>2.414.3</jenkins.version>
5757
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
5858
</properties>
5959

@@ -75,8 +75,8 @@
7575
<dependencies>
7676
<dependency>
7777
<groupId>io.jenkins.tools.bom</groupId>
78-
<artifactId>bom-2.319.x</artifactId>
79-
<version>1654.vcb_69d035fa_20</version>
78+
<artifactId>bom-2.414.x</artifactId>
79+
<version>2977.vdf61ecb_fb_e2d</version>
8080
<type>pom</type>
8181
<scope>import</scope>
8282
</dependency>
@@ -92,8 +92,7 @@
9292

9393
<dependency>
9494
<groupId>io.jenkins.plugins</groupId>
95-
<artifactId>commons-httpclient3-api</artifactId>
96-
<version>3.1-3</version>
95+
<artifactId>apache-httpcomponents-client-5-api</artifactId>
9796
</dependency>
9897

9998
<dependency>
@@ -107,16 +106,8 @@
107106
</dependency>
108107

109108
<dependency>
110-
<groupId>com.jayway.jsonpath</groupId>
111-
<artifactId>json-path</artifactId>
112-
<version>2.7.0</version>
113-
<exclusions>
114-
<!-- Provided by Jenkins core -->
115-
<exclusion>
116-
<groupId>org.slf4j</groupId>
117-
<artifactId>slf4j-api</artifactId>
118-
</exclusion>
119-
</exclusions>
109+
<groupId>io.jenkins.plugins</groupId>
110+
<artifactId>json-path-api</artifactId>
120111
</dependency>
121112

122113
<dependency>

src/main/java/com/github/terma/jenkins/githubprcoveragestatus/Message.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
*/
1818
package com.github.terma.jenkins.githubprcoveragestatus;
1919

20-
import org.apache.commons.httpclient.URIException;
21-
import org.apache.commons.httpclient.util.URIUtil;
20+
import org.springframework.web.util.UriUtils;
21+
22+
import java.nio.charset.StandardCharsets;
2223

2324
@SuppressWarnings("WeakerAccess")
2425
class Message {
@@ -71,11 +72,7 @@ private String shieldIoUrl(String icon, final int yellowThreshold, final int gre
7172
final String color = getColor(yellowThreshold, greenThreshold);
7273
// dash should be encoded as two dash
7374
icon = icon.replace("-", "--");
74-
try {
75-
return String.format(BADGE_TEMPLATE, URIUtil.encodePath(icon), color);
76-
} catch (URIException e) {
77-
throw new RuntimeException(e);
78-
}
75+
return String.format(BADGE_TEMPLATE, UriUtils.encodePathSegment(icon, StandardCharsets.UTF_8), color);
7976
}
8077

8178
private String getColor(int yellowThreshold, int greenThreshold) {

src/main/java/com/github/terma/jenkins/githubprcoveragestatus/SonarMasterCoverageRepository.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@
2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import com.fasterxml.jackson.core.type.TypeReference;
2222
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import org.apache.commons.httpclient.HttpClient;
24-
import org.apache.commons.httpclient.auth.BasicScheme;
25-
import org.apache.commons.httpclient.methods.GetMethod;
26-
import org.apache.commons.httpclient.UsernamePasswordCredentials;
27-
import org.apache.commons.httpclient.auth.AuthScope;
23+
import org.apache.hc.client5.http.auth.AuthScope;
24+
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
25+
import org.apache.hc.client5.http.classic.HttpClient;
26+
import org.apache.hc.client5.http.classic.methods.HttpGet;
27+
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
28+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
29+
import org.apache.hc.client5.http.impl.classic.HttpClients;
30+
import org.apache.hc.core5.http.HttpHost;
31+
import org.apache.hc.core5.http.ParseException;
32+
import org.apache.hc.core5.http.io.entity.EntityUtils;
2833

2934
import java.io.IOException;
3035
import java.io.PrintStream;
36+
import java.net.URISyntaxException;
3137
import java.net.URLEncoder;
3238
import java.text.MessageFormat;
3339
import java.util.List;
3440

3541
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
36-
import static org.apache.commons.httpclient.HttpStatus.SC_BAD_REQUEST;
42+
import static org.apache.hc.core5.http.HttpStatus.SC_BAD_REQUEST;
3743

3844
@SuppressWarnings("WeakerAccess")
3945
public class SonarMasterCoverageRepository implements MasterCoverageRepository {
@@ -52,9 +58,17 @@ public SonarMasterCoverageRepository(String sonarUrl, String login, String passw
5258
this.sonarUrl = sonarUrl;
5359
this.login = login;
5460
this.buildLog = buildLog;
55-
httpClient = new HttpClient();
56-
if (login != null) {
57-
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(login, password));
61+
if (this.login != null) {
62+
try {
63+
BasicCredentialsProvider provider = new BasicCredentialsProvider();
64+
AuthScope scope = new AuthScope(HttpHost.create(sonarUrl));
65+
provider.setCredentials(scope, new UsernamePasswordCredentials(login, password.toCharArray()));
66+
this.httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
67+
} catch (URISyntaxException e) {
68+
throw new RuntimeException(e);
69+
}
70+
} else {
71+
this.httpClient = HttpClients.createDefault();
5872
}
5973
}
6074

@@ -80,10 +94,9 @@ public float get(final String gitHubRepoUrl) {
8094
* @throws SonarProjectRetrievalException if no project could be found or an error occurred during retrieval
8195
*/
8296
private SonarProject getSonarProject(final String repoName) throws SonarProjectRetrievalException {
83-
try {
84-
final String searchUri = sonarUrl + SONAR_SEARCH_PROJECTS_API_PATH + "?search=" + repoName;
85-
final GetMethod method = executeGetRequest(searchUri);
86-
final List<SonarProject> sonarProjects = objectMapper.readValue(method.getResponseBodyAsStream(), new TypeReference<List<SonarProject>>() {
97+
final String searchUri = sonarUrl + SONAR_SEARCH_PROJECTS_API_PATH + "?search=" + repoName;
98+
try (final CloseableHttpResponse response = executeGetRequest(searchUri)) {
99+
final List<SonarProject> sonarProjects = objectMapper.readValue(EntityUtils.toString(response.getEntity()), new TypeReference<List<SonarProject>>() {
87100
});
88101

89102
if (sonarProjects.isEmpty()) {
@@ -108,25 +121,23 @@ private SonarProject getSonarProject(final String repoName) throws SonarProjectR
108121
*/
109122
private float getCoverageMeasure(SonarProject project) throws SonarCoverageMeasureRetrievalException {
110123
final String uri = MessageFormat.format("{0}{1}?componentKey={2}&metricKeys={3}", sonarUrl, SONAR_COMPONENT_MEASURE_API_PATH, URLEncoder.encode(project.getKey()), SONAR_OVERALL_LINE_COVERAGE_METRIC_NAME);
111-
try {
112-
final GetMethod method = executeGetRequest(uri);
113-
String value = JsonUtils.findInJson(method.getResponseBodyAsString(), "component.measures[0].value");
124+
try (final CloseableHttpResponse response = executeGetRequest(uri)) {
125+
String value = JsonUtils.findInJson(EntityUtils.toString(response.getEntity()), "component.measures[0].value");
114126
return Float.parseFloat(value) / 100;
115127
} catch (Exception e) {
116128
throw new SonarCoverageMeasureRetrievalException(String.format("failed to get coverage measure for sonar project %s - %s", project.getKey(), e.getMessage()), e);
117129
}
118130
}
119131

120-
private GetMethod executeGetRequest(String uri) throws IOException, HttpClientException {
121-
final GetMethod method = new GetMethod(uri);
122-
if (login != null) {
123-
method.getHostAuthState().setAuthScheme(new BasicScheme());
124-
}
125-
int status = httpClient.executeMethod(method);
132+
private CloseableHttpResponse executeGetRequest(String uri) throws IOException, HttpClientException, ParseException {
133+
final HttpGet method = new HttpGet(uri);
134+
135+
CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(method);
136+
int status = response.getCode();
126137
if (status >= SC_BAD_REQUEST) {
127-
throw new HttpClientException(uri, status, method.getResponseBodyAsString());
138+
throw new HttpClientException(uri, status, EntityUtils.toString(response.getEntity()));
128139
}
129-
return method;
140+
return response;
130141
}
131142

132143
private void log(String format, Object... arguments) {

src/test/java/com/github/terma/jenkins/githubprcoveragestatus/MessageTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public void forCommentWithShieldIo() {
5858
new Message(0, 0).forComment(buildUrl, null, 80, 90, true));
5959

6060
Assert.assertEquals(
61-
"[![50% (+50.0%) vs master 0%](https://img.shields.io/badge/coverage-50%25%20(%2B50.0%25)%20vs%20master%200%25-red.svg)](http://terma.com/jenkins/job/ama)",
61+
"[![50% (+50.0%) vs master 0%](https://img.shields.io/badge/coverage-50%25%20(+50.0%25)%20vs%20master%200%25-red.svg)](http://terma.com/jenkins/job/ama)",
6262
new Message(0.5f, 0).forComment(buildUrl, null, 80, 90, true));
6363

6464
Assert.assertEquals(
6565
"[![0% (-50.0%) vs master 50%](https://img.shields.io/badge/coverage-0%25%20(--50.0%25)%20vs%20master%2050%25-red.svg)](http://terma.com/jenkins/job/ama)",
6666
new Message(0, 0.5f).forComment(buildUrl, null, 80, 90, true));
6767

6868
Assert.assertEquals(
69-
"[![85% (+35.0%) vs master 50%](https://img.shields.io/badge/coverage-85%25%20(%2B35.0%25)%20vs%20master%2050%25-yellow.svg)](http://terma.com/jenkins/job/ama)",
69+
"[![85% (+35.0%) vs master 50%](https://img.shields.io/badge/coverage-85%25%20(+35.0%25)%20vs%20master%2050%25-yellow.svg)](http://terma.com/jenkins/job/ama)",
7070
new Message(0.85f, 0.5f).forComment(buildUrl, null, 80, 90, true));
7171
}
7272

0 commit comments

Comments
 (0)