Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to apache-httpcomponents-client-5-api #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
buildPlugin(useContainerAgent: true, configurations: [
[ platform: 'linux', jdk: '8' ],
[ platform: 'linux', jdk: '11' ],
[ platform: 'windows', jdk: '11' ],
])
21 changes: 6 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<properties>
<revision>2.2.1</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.319.1</jenkins.version>
<jenkins.version>2.414.3</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
</properties>

Expand All @@ -75,8 +75,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.319.x</artifactId>
<version>1654.vcb_69d035fa_20</version>
<artifactId>bom-2.414.x</artifactId>
<version>2977.vdf61ecb_fb_e2d</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -92,8 +92,7 @@

<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-httpclient3-api</artifactId>
<version>3.1-3</version>
<artifactId>apache-httpcomponents-client-5-api</artifactId>
</dependency>

<dependency>
Expand All @@ -107,16 +106,8 @@
</dependency>

<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
<exclusions>
<!-- Provided by Jenkins core -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<groupId>io.jenkins.plugins</groupId>
<artifactId>json-path-api</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package com.github.terma.jenkins.githubprcoveragestatus;

import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.springframework.web.util.UriUtils;

import java.nio.charset.StandardCharsets;

@SuppressWarnings("WeakerAccess")
class Message {
Expand Down Expand Up @@ -71,11 +72,7 @@ private String shieldIoUrl(String icon, final int yellowThreshold, final int gre
final String color = getColor(yellowThreshold, greenThreshold);
// dash should be encoded as two dash
icon = icon.replace("-", "--");
try {
return String.format(BADGE_TEMPLATE, URIUtil.encodePath(icon), color);
} catch (URIException e) {
throw new RuntimeException(e);
}
return String.format(BADGE_TEMPLATE, UriUtils.encodePathSegment(icon, StandardCharsets.UTF_8), color);
}

private String getColor(int yellowThreshold, int greenThreshold) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.BasicScheme;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.List;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static org.apache.commons.httpclient.HttpStatus.SC_BAD_REQUEST;
import static org.apache.hc.core5.http.HttpStatus.SC_BAD_REQUEST;

@SuppressWarnings("WeakerAccess")
public class SonarMasterCoverageRepository implements MasterCoverageRepository {
Expand All @@ -52,9 +58,17 @@ public SonarMasterCoverageRepository(String sonarUrl, String login, String passw
this.sonarUrl = sonarUrl;
this.login = login;
this.buildLog = buildLog;
httpClient = new HttpClient();
if (login != null) {
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(login, password));
if (this.login != null) {
try {
BasicCredentialsProvider provider = new BasicCredentialsProvider();
AuthScope scope = new AuthScope(HttpHost.create(sonarUrl));
provider.setCredentials(scope, new UsernamePasswordCredentials(login, password.toCharArray()));
this.httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
} else {
this.httpClient = HttpClients.createDefault();
}
}

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

if (sonarProjects.isEmpty()) {
Expand All @@ -108,25 +121,23 @@ private SonarProject getSonarProject(final String repoName) throws SonarProjectR
*/
private float getCoverageMeasure(SonarProject project) throws SonarCoverageMeasureRetrievalException {
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);
try {
final GetMethod method = executeGetRequest(uri);
String value = JsonUtils.findInJson(method.getResponseBodyAsString(), "component.measures[0].value");
try (final CloseableHttpResponse response = executeGetRequest(uri)) {
String value = JsonUtils.findInJson(EntityUtils.toString(response.getEntity()), "component.measures[0].value");
return Float.parseFloat(value) / 100;
} catch (Exception e) {
throw new SonarCoverageMeasureRetrievalException(String.format("failed to get coverage measure for sonar project %s - %s", project.getKey(), e.getMessage()), e);
}
}

private GetMethod executeGetRequest(String uri) throws IOException, HttpClientException {
final GetMethod method = new GetMethod(uri);
if (login != null) {
method.getHostAuthState().setAuthScheme(new BasicScheme());
}
int status = httpClient.executeMethod(method);
private CloseableHttpResponse executeGetRequest(String uri) throws IOException, HttpClientException, ParseException {
final HttpGet method = new HttpGet(uri);

CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(method);
int status = response.getCode();
if (status >= SC_BAD_REQUEST) {
throw new HttpClientException(uri, status, method.getResponseBodyAsString());
throw new HttpClientException(uri, status, EntityUtils.toString(response.getEntity()));
}
return method;
return response;
}

private void log(String format, Object... arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public void forCommentWithShieldIo() {
new Message(0, 0).forComment(buildUrl, null, 80, 90, true));

Assert.assertEquals(
"[![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)",
"[![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)",
new Message(0.5f, 0).forComment(buildUrl, null, 80, 90, true));

Assert.assertEquals(
"[![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)",
new Message(0, 0.5f).forComment(buildUrl, null, 80, 90, true));

Assert.assertEquals(
"[![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)",
"[![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)",
new Message(0.85f, 0.5f).forComment(buildUrl, null, 80, 90, true));
}

Expand Down