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

Remove env dependency from uberallsclient #40

Merged
merged 2 commits into from
Jun 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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