Skip to content

Commit

Permalink
Merge pull request #17 from adityacs/master
Browse files Browse the repository at this point in the history
  • Loading branch information
asimell authored Jun 30, 2017
2 parents 3379796 + 78066ee commit c81e195
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 67 deletions.
14 changes: 1 addition & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,7 @@ THE SOFTWARE.
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</dependencies>

<!-- The current maintainers of the plugin-->
<developers>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
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...");
* addPoints(pointsToWrite, sonarGen, listener);
* }
*/

SonarQubePointGenerator sonarGen = new SonarQubePointGenerator(measurementRenderer, customPrefix, build);
if (sonarGen.hasReport()) {
listener.getLogger().println("[InfluxDB Plugin] SonarQube data found. Writing to InfluxDB...");
addPoints(pointsToWrite, sonarGen, listener);
}


ChangeLogPointGenerator changeLogGen = new ChangeLogPointGenerator(measurementRenderer, customPrefix, build);
if (changeLogGen.hasReport()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -61,11 +60,11 @@ public boolean hasReport() {
if (!StringUtils.isEmpty(sonarBuildLink)) {
setSonarDetails(sonarBuildLink);
return true;
}
}
} catch (IOException e) {
//
}

return false;
}

Expand All @@ -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)
.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();
.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();
}

Expand All @@ -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("/");
Expand All @@ -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");
}

Expand Down

0 comments on commit c81e195

Please # to comment.