Search in sources :

Example 1 with InterruptedBuildAction

use of jenkins.model.InterruptedBuildAction 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 InterruptedBuildAction

use of jenkins.model.InterruptedBuildAction in project workflow-job-plugin by jenkinsci.

the class WorkflowRunTest method interruptCause.

@Issue("JENKINS-41276")
@Test
public void interruptCause() throws Exception {
    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    // TODO should probably be @Whitelisted
    ScriptApproval.get().approveSignature("method org.jenkinsci.plugins.workflow.steps.FlowInterruptedException getCauses");
    // ditto
    ScriptApproval.get().approveSignature("method jenkins.model.CauseOfInterruption$UserInterruption getUser");
    p.setDefinition(new CpsFlowDefinition("@NonCPS def users(e) {e.causes*.user}; try {semaphore 'wait'} catch (e) {echo(/users=${users(e)}/); throw e}", true));
    final WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
    SemaphoreStep.waitForStart("wait/1", b1);
    ACL.impersonate(User.get("dev").impersonate(), new Runnable() {

        @Override
        public void run() {
            b1.getExecutor().doStop();
        }
    });
    r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b1));
    r.assertLogContains("users=[dev]", b1);
    InterruptedBuildAction iba = b1.getAction(InterruptedBuildAction.class);
    assertNotNull(iba);
    assertEquals(Collections.singletonList(new CauseOfInterruption.UserInterruption("dev")), iba.getCauses());
    String log = JenkinsRule.getLog(b1);
    assertEquals(log, 1, StringUtils.countMatches(log, jenkins.model.Messages.CauseOfInterruption_ShortDescription("dev")));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 3 with InterruptedBuildAction

use of jenkins.model.InterruptedBuildAction in project workflow-job-plugin by jenkinsci.

the class WorkflowRunTest method interruptWithResult.

@Test
public void interruptWithResult() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("sleep 1; semaphore 'hang'", true));
    WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
    // TODO sleeps should not be necessary but seems to randomly fail to receive interrupt otherwise
    Thread.sleep(500);
    SemaphoreStep.waitForStart("hang/1", b1);
    Thread.sleep(500);
    Executor ex = b1.getExecutor();
    assertNotNull(ex);
    ex.interrupt(Result.NOT_BUILT, new CauseOfInterruption.UserInterruption("bob"));
    r.assertBuildStatus(Result.NOT_BUILT, r.waitForCompletion(b1));
    InterruptedBuildAction iba = b1.getAction(InterruptedBuildAction.class);
    assertNotNull(iba);
    assertEquals(Collections.singletonList(new CauseOfInterruption.UserInterruption("bob")), iba.getCauses());
    Thread.sleep(500);
    WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
    assertEquals(2, b2.getNumber());
    Thread.sleep(500);
    SemaphoreStep.waitForStart("hang/2", b2);
    Thread.sleep(500);
    ex = b2.getExecutor();
    assertNotNull(ex);
    ex.interrupt();
    r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2));
    iba = b2.getAction(InterruptedBuildAction.class);
    assertNull(iba);
}
Also used : CauseOfInterruption(jenkins.model.CauseOfInterruption) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) Test(org.junit.Test)

Example 4 with InterruptedBuildAction

use of jenkins.model.InterruptedBuildAction in project workflow-cps-plugin by jenkinsci.

the class CpsThreadTest method stop.

@Test
public void stop() throws Exception {
    WorkflowJob p = r.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("unkillable()", true));
    final WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    r.waitForMessage("unkillable", b);
    ACL.impersonate(Jenkins.ANONYMOUS, new Runnable() {

        @Override
        public void run() {
            b.getExecutor().interrupt();
        }
    });
    r.waitForCompletion(b);
    r.assertBuildStatus(Result.ABORTED, b);
    InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class);
    assertNotNull(iba);
    List<CauseOfInterruption> causes = iba.getCauses();
    assertEquals(1, causes.size());
    assertEquals(CauseOfInterruption.UserInterruption.class, causes.get(0).getClass());
    // TODO JENKINS-46076 WorkflowRun.isBuilding() can go to false before .finish has completed
    r.waitForMessage("Finished: ABORTED", b);
    r.assertLogContains("never going to stop", b);
    r.assertLogNotContains("\tat ", b);
}
Also used : CauseOfInterruption(jenkins.model.CauseOfInterruption) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

InterruptedBuildAction (jenkins.model.InterruptedBuildAction)4 CauseOfInterruption (jenkins.model.CauseOfInterruption)3 Test (org.junit.Test)3 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)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 CodeCoverageMetrics (com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics)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 HashSet (java.util.HashSet)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)1 Issue (org.jvnet.hudson.test.Issue)1