From 6d463b5c2ec84f1338f7b52cf13a6d40909a9f09 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Mon, 26 Jun 2017 15:58:06 +0530 Subject: [PATCH] SonarQube httpentity issue fixed, used default java http library --- pom.xml | 20 +---- .../plugins/influxdb/InfluxDbPublisher.java | 33 ++++--- .../generators/SonarQubePointGenerator.java | 89 +++++++++---------- 3 files changed, 67 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index 295a2346..e46e9b28 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ THE SOFTWARE. influxdb https://wiki.jenkins-ci.org/display/JENKINS/InfluxDB+Plugin - 1.12-SNAPSHOT + 1.12.3-SNAPSHOT hpi InfluxDB Plugin @@ -108,7 +108,7 @@ THE SOFTWARE. org.influxdb influxdb-java - 2.2 + 2.5 @@ -146,7 +146,7 @@ THE SOFTWARE. org.jenkins-ci.plugins performance - 1.15 + 3.0 @@ -154,19 +154,7 @@ THE SOFTWARE. commons-lang3 3.0 - - - org.apache.httpcomponents - httpcore - 4.4.4 - - - - org.apache.httpcomponents - httpclient - 4.5.2 - - + diff --git a/src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java b/src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java index b4b61871..e180ad95 100644 --- a/src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java +++ b/src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java @@ -201,58 +201,69 @@ public void perform(Run build, FilePath workspace, Launcher launcher, Task // finally write to InfluxDB JenkinsBasePointGenerator jGen = new JenkinsBasePointGenerator(measurementRenderer, customPrefix, build); - pointsToWrite.addAll(Arrays.asList(jGen.generate())); + addPoints(pointsToWrite, jGen, listener); CustomDataPointGenerator cdGen = new CustomDataPointGenerator(measurementRenderer, customPrefix, build, customData); if (cdGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Custom data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(cdGen.generate())); + addPoints(pointsToWrite, cdGen, listener); } CustomDataMapPointGenerator cdmGen = new CustomDataMapPointGenerator(measurementRenderer, customPrefix, build, customDataMap); if (cdmGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Custom data map found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(cdmGen.generate())); + addPoints(pointsToWrite, cdmGen, listener); } CoberturaPointGenerator cGen = new CoberturaPointGenerator(measurementRenderer, customPrefix, build); if (cGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Cobertura data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(cGen.generate())); + addPoints(pointsToWrite, cGen, listener); } RobotFrameworkPointGenerator rfGen = new RobotFrameworkPointGenerator(measurementRenderer, customPrefix, build); if (rfGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Robot Framework data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(rfGen.generate())); + addPoints(pointsToWrite, rfGen, listener); } JacocoPointGenerator jacoGen = new JacocoPointGenerator(measurementRenderer, customPrefix, build); if (jacoGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Jacoco data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(jacoGen.generate())); + addPoints(pointsToWrite, jacoGen, listener); } PerformancePointGenerator perfGen = new PerformancePointGenerator(measurementRenderer, customPrefix, build); if (perfGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Performance data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(perfGen.generate())); + addPoints(pointsToWrite, perfGen, listener); } + SonarQubePointGenerator sonarGen = new SonarQubePointGenerator(measurementRenderer, customPrefix, build); if (sonarGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] SonarQube data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(sonarGen.generate())); - } + addPoints(pointsToWrite, sonarGen, listener); + } + ChangeLogPointGenerator changeLogGen = new ChangeLogPointGenerator(measurementRenderer, customPrefix, build); if (changeLogGen.hasReport()) { listener.getLogger().println("[InfluxDB Plugin] Git ChangeLog data found. Writing to InfluxDB..."); - pointsToWrite.addAll(Arrays.asList(changeLogGen.generate())); + addPoints(pointsToWrite, changeLogGen, listener); } writeToInflux(target, influxDB, pointsToWrite); + listener.getLogger().println("[InfluxDB Plugin] Completed."); + + } + private void addPoints(List pointsToWrite, PointGenerator generator, TaskListener listener) { + try { + pointsToWrite.addAll(Arrays.asList(generator.generate())); + } catch (Exception e) { + listener.getLogger().println("[InfluxDB Plugin] Failed to collect data. Ignoring Exception:" + e); + } } private void writeToInflux(Target target, InfluxDB influxDB, List pointsToWrite) { @@ -264,7 +275,7 @@ private void writeToInflux(Target target, InfluxDB influxDB, List pointsT .database(target.getDatabase()) .points(pointsToWrite.toArray(new Point[0])) .retentionPolicy(target.getRetentionPolicy()) - .consistency(ConsistencyLevel.ALL) + .consistency(ConsistencyLevel.ANY) .build(); influxDB.write(batchPoints); } catch (Exception e) { diff --git a/src/main/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGenerator.java b/src/main/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGenerator.java index 140a80ed..b46ed1e7 100644 --- a/src/main/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGenerator.java +++ b/src/main/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGenerator.java @@ -7,12 +7,11 @@ import java.net.URISyntaxException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import org.apache.commons.lang3.StringUtils; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import org.influxdb.dto.Point; import hudson.model.Run; @@ -22,7 +21,7 @@ import net.sf.json.JSONObject; public class SonarQubePointGenerator extends AbstractPointGenerator { - + public static final String BUILD_DISPLAY_NAME = "display_name"; public static final String SONARQUBE_LINES_OF_CODE = "lines_of_code"; public static final String SONARQUBE_COMPLEXITY = "complexity"; @@ -61,11 +60,11 @@ public boolean hasReport() { if (!StringUtils.isEmpty(sonarBuildLink)) { setSonarDetails(sonarBuildLink); return true; - } + } } catch (IOException e) { // } - + return false; } @@ -79,63 +78,58 @@ public void setSonarDetails(String sonarBuildLink) { } catch (URISyntaxException e) { // } - + } + public Point[] generate() { Point point = null; try { point = buildPoint(measurementName("sonarqube_data"), customPrefix, build) - .field(BUILD_DISPLAY_NAME, build.getDisplayName()) - .field(SONARQUBE_CRTITCAL_ISSUES, - getSonarIssues(this.SONAR_ISSUES_URL, "CRITICAL")) - .field(SONARQUBE_BLOCKER_ISSUES, - getSonarIssues(this.SONAR_ISSUES_URL, "BLOCKER")) - .field(SONARQUBE_MAJOR_ISSUES, - getSonarIssues(this.SONAR_ISSUES_URL, "MAJOR")) - .field(SONARQUBE_MINOR_ISSUES, - getSonarIssues(this.SONAR_ISSUES_URL, "MINOR")) - .field(SONARQUBE_INFO_ISSUES, - getSonarIssues(this.SONAR_ISSUES_URL, "INFO")) - .field(SONARQUBE_LINES_OF_CODE, - getLinesofCode(this.SONAR_METRICS_URL)) - .build(); + .addField(BUILD_DISPLAY_NAME, build.getDisplayName()) + .addField(SONARQUBE_CRTITCAL_ISSUES, getSonarIssues(this.SONAR_ISSUES_URL, "CRITICAL")) + .addField(SONARQUBE_BLOCKER_ISSUES, getSonarIssues(this.SONAR_ISSUES_URL, "BLOCKER")) + .addField(SONARQUBE_MAJOR_ISSUES, getSonarIssues(this.SONAR_ISSUES_URL, "MAJOR")) + .addField(SONARQUBE_MINOR_ISSUES, getSonarIssues(this.SONAR_ISSUES_URL, "MINOR")) + .addField(SONARQUBE_INFO_ISSUES, getSonarIssues(this.SONAR_ISSUES_URL, "INFO")) + .addField(SONARQUBE_LINES_OF_CODE, getLinesofCode(this.SONAR_METRICS_URL)).build(); } catch (IOException e) { - //handle + // handle } return new Point[] { point }; } - public String getResult(String request) throws IOException { - CloseableHttpClient client = null; - CloseableHttpResponse response = null; StringBuffer result = new StringBuffer(); - try { - - client = HttpClientBuilder.create().build(); - HttpGet getrequest = new HttpGet(request); - - response = client.execute(getrequest); - - BufferedReader rd; - - rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + + try + { + URL url = new URL(request); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Accept", "application/json"); + + if (conn.getResponseCode() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); + } + BufferedReader rd = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } - } catch (UnsupportedOperationException | IOException e) { - //handle - } finally { - if (response != null) { - response.close(); - } - if (client != null) { - client.close(); - } + conn.disconnect(); + + } catch (MalformedURLException e) { + + e.printStackTrace(); + + } catch (IOException e) { + + e.printStackTrace(); + } + return result.toString(); } @@ -159,7 +153,6 @@ private String getSonarProjectURLFromBuildLogs(Run build) throws IOExcepti return url; } - private String getSonarProjectName(String url) throws URISyntaxException { URI uri = new URI(url); String[] projectUrl = uri.getPath().split("/"); @@ -180,12 +173,12 @@ public int getLinesofCode(String url) throws IOException { linesofcodeCount = metricsObject.getInt("value"); } } - + return linesofcodeCount; } public int getSonarIssues(String url, String severity) throws IOException { - String output = getResult(url+severity); + String output = getResult(url + severity); return JSONObject.fromObject(output).getInt("total"); }