Skip to content

Commit

Permalink
Merge pull request #40 from uber/remove_env_uberallsclient
Browse files Browse the repository at this point in the history
Remove env dependency from uberallsclient
  • Loading branch information
jjx committed Jun 29, 2015
2 parents 07efa72 + 3f9c36a commit 6d9f266
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
coverage.setOwner(build);
}

UberallsClient uberalls = new UberallsClient(getDescriptor().getUberallsURL(), environment, logger.getStream());
UberallsClient uberalls = new UberallsClient(getDescriptor().getUberallsURL(), logger,
environment.get("GIT_URL"), environment.get("GIT_BRANCH"));
final boolean needsDecoration = environment.get(PhabricatorPlugin.WRAP_KEY, null) == null;
final String conduitToken = environment.get(PhabricatorPlugin.CONDUIT_TOKEN, null);
final String arcPath = environment.get(PhabricatorPlugin.ARCANIST_PATH, "arc");
Expand All @@ -106,7 +107,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
if (!CommonUtils.isBlank(currentSHA) && codeCoverageMetrics.isValid()) {
logger.info("uberalls", "sending coverage report for " + currentSHA + " as " +
codeCoverageMetrics.toString());
uberalls.recordCoverage(currentSHA, environment.get("GIT_BRANCH"), codeCoverageMetrics);
uberalls.recordCoverage(currentSHA, codeCoverageMetrics);
} else {
logger.info("uberalls", "no line coverage available for " + currentSHA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.uber.jenkins.phabricator.CodeCoverageMetrics;
import com.uber.jenkins.phabricator.conduit.Differential;
import com.uber.jenkins.phabricator.utils.Logger;
import hudson.EnvVars;
import net.sf.json.JSON;
import net.sf.json.JSONNull;
Expand Down Expand Up @@ -50,22 +51,65 @@ public class UberallsClient {
public static final String LINE_COVERAGE_KEY = "lineCoverage";
public static final String CONDITIONAL_COVERAGE_KEY = "conditionalCoverage";

private final EnvVars environment;
private static final String TAG = "uberalls-client";

private final String baseURL;
private final PrintStream logger;
private final Logger logger;
private final String repository;
private final String branch;

public UberallsClient(String baseURL, Logger logger, String repository, String branch) {
this.baseURL = baseURL;
this.logger = logger;
this.repository = repository;
this.branch = branch;
}

public String getBaseURL() {
return this.baseURL;
}

public CodeCoverageMetrics getParentCoverage(Differential differential) {
try {
String sha = differential.getBaseCommit();
String coverageJSON = null;
if (sha != null) {
coverageJSON = getCoverage(sha);
}
JsonSlurper jsonParser = new JsonSlurper();
JSON responseJSON = jsonParser.parseText(coverageJSON);
if (responseJSON instanceof JSONNull) {
return null;
}
JSONObject coverage = (JSONObject) responseJSON;

return new CodeCoverageMetrics(
coverage.getString("sha"),
((Double) coverage.getDouble(PACKAGE_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(FILES_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(CLASSES_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(METHOD_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(LINE_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(CONDITIONAL_COVERAGE_KEY)).floatValue());
} catch (Exception e) {
e.printStackTrace(logger.getStream());
}

return null;
}

public boolean recordCoverage(String currentSHA, String branch, CodeCoverageMetrics codeCoverageMetrics) {
public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetrics) {
if (codeCoverageMetrics.isValid()) {
JSONObject params = new JSONObject();
params.put("sha", currentSHA);
params.put("sha", sha);
params.put("branch", branch);
params.put("repository", repository);
params.put(PACKAGE_COVERAGE_KEY, codeCoverageMetrics.getPackageCoveragePercent());
params.put(FILES_COVERAGE_KEY, codeCoverageMetrics.getFilesCoveragePercent());
params.put(CLASSES_COVERAGE_KEY, codeCoverageMetrics.getClassesCoveragePercent());
params.put(METHOD_COVERAGE_KEY, codeCoverageMetrics.getMethodCoveragePercent());
params.put(LINE_COVERAGE_KEY, codeCoverageMetrics.getLineCoveragePercent());
params.put(CONDITIONAL_COVERAGE_KEY, codeCoverageMetrics.getConditionalCoveragePercent());
params.put("repository", this.environment.get("GIT_URL"));

try {
HttpClient client = new HttpClient();
Expand All @@ -79,7 +123,7 @@ public boolean recordCoverage(String currentSHA, String branch, CodeCoverageMetr
int statusCode = client.executeMethod(request);

if (statusCode != HttpStatus.SC_OK) {
logger.println("[uberalls] call failed: " + request.getStatusLine());
logger.info(TAG, "Call failed: " + request.getStatusLine());
return false;
}
return true;
Expand All @@ -98,71 +142,32 @@ public boolean recordCoverage(String currentSHA, String branch, CodeCoverageMetr
return false;
}

public UberallsClient(String baseURL, EnvVars environment, PrintStream logger) {
this.baseURL = baseURL;
this.environment = environment;
this.logger = logger;
}

public String getBaseURL() {
return this.baseURL;
}

public CodeCoverageMetrics getParentCoverage(Differential differential) {
try {
String sha = differential.getBaseCommit();
String coverageJSON = null;
if (sha != null) {
coverageJSON = getCoverage(sha);
}
JsonSlurper jsonParser = new JsonSlurper();
JSON responseJSON = jsonParser.parseText(coverageJSON);
if (responseJSON instanceof JSONNull) {
return null;
}
JSONObject coverage = (JSONObject) responseJSON;

return new CodeCoverageMetrics(
coverage.getString("sha"),
((Double) coverage.getDouble(PACKAGE_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(FILES_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(CLASSES_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(METHOD_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(LINE_COVERAGE_KEY)).floatValue(),
((Double) coverage.getDouble(CONDITIONAL_COVERAGE_KEY)).floatValue());
} catch (Exception e) {
e.printStackTrace(logger);
}

return null;
}

public String getCoverage(String value) {
public String getCoverage(String sha) {
URIBuilder builder;
try {
builder = getBuilder()
.setParameter("repository", this.environment.get("GIT_URL"))
.setParameter("sha", value);
.setParameter("sha", sha)
.setParameter("repository", repository);

HttpClient client = new HttpClient();
HttpMethod request = new GetMethod(builder.build().toString());
int statusCode = client.executeMethod(request);

if (statusCode != HttpStatus.SC_OK) {
logger.println("[uberalls] call failed: " + request.getStatusLine());
logger.info(TAG, "Call failed: " + request.getStatusLine());
return null;
}
return request.getResponseBodyAsString();
} catch (URISyntaxException e) {
e.printStackTrace(logger);
e.printStackTrace(logger.getStream());
} catch (HttpResponseException e) {
if (e.getStatusCode() != 404) {
e.printStackTrace(logger);
e.printStackTrace(logger.getStream());
}
} catch (ClientProtocolException e) {
e.printStackTrace(logger);
e.printStackTrace(logger.getStream());
} catch (IOException e) {
e.printStackTrace(logger);
e.printStackTrace(logger.getStream());
}
return null;
}
Expand Down

0 comments on commit 6d9f266

Please sign in to comment.