Search in sources :

Example 1 with CodeCoverageMetrics

use of com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics in project phabricator-jenkins-plugin by uber.

the class PhabricatorNotifier method perform.

@Override
public final boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException {
    EnvVars environment = build.getEnvironment(listener);
    Logger logger = new Logger(listener.getLogger());
    final String branch = environment.get("GIT_BRANCH");
    final String gitUrl = environment.get("GIT_URL");
    final UberallsClient uberallsClient = getUberallsClient(logger, gitUrl, branch);
    final boolean needsDecoration = build.getActions(PhabricatorPostbuildAction.class).size() == 0;
    final String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);
    final String phid = environment.get(PhabricatorPlugin.PHID_FIELD);
    final boolean isDifferential = !CommonUtils.isBlank(diffID);
    InterruptedBuildAction action = build.getAction(InterruptedBuildAction.class);
    if (action != null) {
        List<CauseOfInterruption> causes = action.getCauses();
        for (CauseOfInterruption cause : causes) {
            if (cause instanceof PhabricatorCauseOfInterruption) {
                logger.warn(ABORT_TAG, "Skipping notification step since this build was interrupted" + " by a newer build with the same differential revision");
                return true;
            }
        }
    }
    CoverageProvider coverageProvider;
    // So only skip build result processing if both are blank (e.g. master runs to update coverage data)
    if (CommonUtils.isBlank(phid) && !isDifferential) {
        if (needsDecoration) {
            build.addAction(PhabricatorPostbuildAction.createShortText(branch, null));
        }
        coverageProvider = getCoverageProvider(build, listener, Collections.<String>emptySet());
        CodeCoverageMetrics coverageResult = null;
        if (coverageProvider != null) {
            coverageResult = coverageProvider.getMetrics();
        }
        NonDifferentialBuildTask nonDifferentialBuildTask = new NonDifferentialBuildTask(logger, uberallsClient, coverageResult, uberallsEnabled, environment.get("GIT_COMMIT"));
        // Ignore the result.
        nonDifferentialBuildTask.run();
        return true;
    }
    ConduitAPIClient conduitClient;
    try {
        conduitClient = getConduitClient(build.getParent());
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, e.getMessage());
        return false;
    }
    final String buildUrl = environment.get("BUILD_URL");
    if (!isDifferential) {
        // Process harbormaster for non-differential builds
        Task.Result result = new NonDifferentialHarbormasterTask(logger, phid, conduitClient, build.getResult(), buildUrl).run();
        return result == Task.Result.SUCCESS;
    }
    DifferentialClient diffClient = new DifferentialClient(diffID, conduitClient);
    Differential diff;
    try {
        diff = new Differential(diffClient.fetchDiff());
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, "Unable to fetch differential from Conduit API");
        logger.warn(CONDUIT_TAG, e.getMessage());
        return true;
    }
    if (needsDecoration) {
        diff.decorate(build, this.getPhabricatorURL(build.getParent()));
    }
    Set<String> includeFileNames = new HashSet<String>();
    for (String file : diff.getChangedFiles()) {
        includeFileNames.add(FilenameUtils.getName(file));
    }
    coverageProvider = getCoverageProvider(build, listener, includeFileNames);
    CodeCoverageMetrics coverageResult = null;
    if (coverageProvider != null) {
        coverageResult = coverageProvider.getMetrics();
    }
    BuildResultProcessor resultProcessor = new BuildResultProcessor(logger, build, diff, diffClient, environment.get(PhabricatorPlugin.PHID_FIELD), coverageResult, buildUrl, preserveFormatting, coverageCheckSettings);
    if (uberallsEnabled) {
        boolean passBuildOnUberalls = resultProcessor.processParentCoverage(uberallsClient);
        if (!passBuildOnUberalls) {
            build.setResult(Result.FAILURE);
        }
    }
    // Add in comments about the build result
    resultProcessor.processBuildResult(commentOnSuccess, commentWithConsoleLinkOnFailure);
    // Process unit tests results to send to Harbormaster
    resultProcessor.processUnitResults(getUnitProvider(build, listener));
    // Read coverage data to send to Harbormaster
    resultProcessor.processCoverage(coverageProvider);
    if (processLint) {
        // Read lint results to send to Harbormaster
        resultProcessor.processLintResults(lintFile, lintFileSize);
    }
    // Fail the build if we can't report to Harbormaster
    if (!resultProcessor.processHarbormaster()) {
        return false;
    }
    resultProcessor.processRemoteComment(commentFile, commentSize);
    resultProcessor.sendComment(commentWithConsoleLinkOnFailure);
    return true;
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) NonDifferentialHarbormasterTask(com.uber.jenkins.phabricator.tasks.NonDifferentialHarbormasterTask) Task(com.uber.jenkins.phabricator.tasks.Task) NonDifferentialBuildTask(com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask) NonDifferentialHarbormasterTask(com.uber.jenkins.phabricator.tasks.NonDifferentialHarbormasterTask) CauseOfInterruption(jenkins.model.CauseOfInterruption) Differential(com.uber.jenkins.phabricator.conduit.Differential) UberallsClient(com.uber.jenkins.phabricator.uberalls.UberallsClient) ConduitAPIException(com.uber.jenkins.phabricator.conduit.ConduitAPIException) DifferentialClient(com.uber.jenkins.phabricator.conduit.DifferentialClient) ConduitAPIClient(com.uber.jenkins.phabricator.conduit.ConduitAPIClient) Logger(com.uber.jenkins.phabricator.utils.Logger) EnvVars(hudson.EnvVars) CoverageProvider(com.uber.jenkins.phabricator.coverage.CoverageProvider) NonDifferentialBuildTask(com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) HashSet(java.util.HashSet)

Example 2 with CodeCoverageMetrics

use of com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics in project phabricator-jenkins-plugin by uber.

the class CommentBuilderTest method testProcessWithDecreaseButHigherThanMinNotFailingTheBuild.

@Test
public void testProcessWithDecreaseButHigherThanMinNotFailingTheBuild() {
    CodeCoverageMetrics fifteenPercentDrop = TestUtils.getCoverageResult(100.0f, 100.0f, 100.0f, 100.0f, 85.0f);
    CommentBuilder commenter = createCommenter(Result.SUCCESS, fifteenPercentDrop, false, -10.0f, 80.0f);
    boolean passCoverage = commenter.processParentCoverage(TestUtils.getDefaultCodeCoverageMetrics(), TestUtils.TEST_SHA, FAKE_BRANCH_NAME);
    String comment = commenter.getComment();
    assertTrue(passCoverage);
    assertThat(comment, containsString("decreased (-15.000%)"));
    assertFalse(comment.contains("Build failed because coverage is lower than minimum 80.0% and decreased more than allowed 10.0%."));
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with CodeCoverageMetrics

use of com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics in project phabricator-jenkins-plugin by uber.

the class CommentBuilderTest method testProcessParentWithIncreasedCoverage.

@Test
public void testProcessParentWithIncreasedCoverage() {
    CodeCoverageMetrics parent = TestUtils.getCodeCoverageMetrics(100.0f, 100.0f, 100.0f, 100.0f, 90.0f, 90.0f);
    commenter.processParentCoverage(parent, TestUtils.TEST_SHA, FAKE_BRANCH_NAME);
    String comment = commenter.getComment();
    assertTrue(comment.contains("increased (+10.000%)"));
    assertTrue("comment contains branch name", comment.contains(FAKE_BRANCH_NAME));
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with CodeCoverageMetrics

use of com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics in project phabricator-jenkins-plugin by uber.

the class UberallsClient method getParentCoverage.

public CodeCoverageMetrics getParentCoverage(String sha) {
    if (sha == null) {
        return null;
    }
    try {
        String 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(((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;
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) JsonSlurper(net.sf.json.groovy.JsonSlurper) JSONNull(net.sf.json.JSONNull) JSONObject(net.sf.json.JSONObject) JSON(net.sf.json.JSON) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 5 with CodeCoverageMetrics

use of com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics in project phabricator-jenkins-plugin by uber.

the class CommentBuilderTest method testProcessWithDecreaseFailingTheBuild.

@Test
public void testProcessWithDecreaseFailingTheBuild() {
    CodeCoverageMetrics fiftyPercentDrop = TestUtils.getCoverageResult(100.0f, 100.0f, 100.0f, 100.0f, 50.0f);
    CommentBuilder commenter = createCommenter(Result.SUCCESS, fiftyPercentDrop, false, -10.0f);
    boolean passCoverage = commenter.processParentCoverage(TestUtils.getDefaultCodeCoverageMetrics(), TestUtils.TEST_SHA, FAKE_BRANCH_NAME);
    String comment = commenter.getComment();
    assertFalse(passCoverage);
    assertThat(comment, containsString("decreased (-50.000%)"));
    assertThat(comment, containsString("Build failed because coverage is lower than minimum 100.0% and decreased more than allowed 10.0%."));
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

CodeCoverageMetrics (com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics)7 Test (org.junit.Test)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 JSONObject (net.sf.json.JSONObject)2 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)1 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)1 Differential (com.uber.jenkins.phabricator.conduit.Differential)1 DifferentialClient (com.uber.jenkins.phabricator.conduit.DifferentialClient)1 CoverageProvider (com.uber.jenkins.phabricator.coverage.CoverageProvider)1 NonDifferentialBuildTask (com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask)1 NonDifferentialHarbormasterTask (com.uber.jenkins.phabricator.tasks.NonDifferentialHarbormasterTask)1 Task (com.uber.jenkins.phabricator.tasks.Task)1 UberallsClient (com.uber.jenkins.phabricator.uberalls.UberallsClient)1 Logger (com.uber.jenkins.phabricator.utils.Logger)1 EnvVars (hudson.EnvVars)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 CauseOfInterruption (jenkins.model.CauseOfInterruption)1 InterruptedBuildAction (jenkins.model.InterruptedBuildAction)1