diff --git a/Jenkinsfile b/Jenkinsfile index 515e5f6..ef43264 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,4 @@ buildPlugin(useContainerAgent: true, configurations: [ - [ platform: 'linux', jdk: '8' ], [ platform: 'linux', jdk: '11' ], [ platform: 'windows', jdk: '11' ], ]) diff --git a/pom.xml b/pom.xml index 14401bf..93a7135 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,7 @@ 2.2.1 -SNAPSHOT - 2.319.1 + 2.414.3 jenkinsci/${project.artifactId}-plugin @@ -75,8 +75,8 @@ io.jenkins.tools.bom - bom-2.319.x - 1654.vcb_69d035fa_20 + bom-2.414.x + 2977.vdf61ecb_fb_e2d pom import @@ -92,8 +92,7 @@ io.jenkins.plugins - commons-httpclient3-api - 3.1-3 + apache-httpcomponents-client-5-api @@ -107,16 +106,8 @@ - com.jayway.jsonpath - json-path - 2.7.0 - - - - org.slf4j - slf4j-api - - + io.jenkins.plugins + json-path-api diff --git a/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/Message.java b/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/Message.java index 6c2cd11..21c023a 100644 --- a/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/Message.java +++ b/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/Message.java @@ -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 { @@ -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) { diff --git a/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/SonarMasterCoverageRepository.java b/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/SonarMasterCoverageRepository.java index a92319f..7801201 100644 --- a/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/SonarMasterCoverageRepository.java +++ b/src/main/java/com/github/terma/jenkins/githubprcoveragestatus/SonarMasterCoverageRepository.java @@ -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 { @@ -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(); } } @@ -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 sonarProjects = objectMapper.readValue(method.getResponseBodyAsStream(), new TypeReference>() { + final String searchUri = sonarUrl + SONAR_SEARCH_PROJECTS_API_PATH + "?search=" + repoName; + try (final CloseableHttpResponse response = executeGetRequest(searchUri)) { + final List sonarProjects = objectMapper.readValue(EntityUtils.toString(response.getEntity()), new TypeReference>() { }); if (sonarProjects.isEmpty()) { @@ -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) { diff --git a/src/test/java/com/github/terma/jenkins/githubprcoveragestatus/MessageTest.java b/src/test/java/com/github/terma/jenkins/githubprcoveragestatus/MessageTest.java index 642e1ac..9e735fd 100644 --- a/src/test/java/com/github/terma/jenkins/githubprcoveragestatus/MessageTest.java +++ b/src/test/java/com/github/terma/jenkins/githubprcoveragestatus/MessageTest.java @@ -58,7 +58,7 @@ 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( @@ -66,7 +66,7 @@ public void forCommentWithShieldIo() { 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)); }