Search in sources :

Example 1 with Cause

use of hudson.model.Cause in project gogs-webhook-plugin by jenkinsci.

the class GogsPayloadProcessor method triggerJobs.

public GogsResults triggerJobs(String jobName, String deliveryID) {
    SecurityContext saveCtx = ACL.impersonate(ACL.SYSTEM);
    GogsResults result = new GogsResults();
    try {
        BuildableItem project = GogsUtils.find(jobName, BuildableItem.class);
        if (project != null) {
            GogsTrigger gTrigger = null;
            Cause cause = new GogsCause(deliveryID);
            if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {
                ParameterizedJobMixIn.ParameterizedJob pJob = (ParameterizedJobMixIn.ParameterizedJob) project;
                for (Trigger trigger : pJob.getTriggers().values()) {
                    if (trigger instanceof GogsTrigger) {
                        gTrigger = (GogsTrigger) trigger;
                        break;
                    }
                }
            }
            if (gTrigger != null) {
                SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(project);
                GogsPayload gogsPayload = new GogsPayload(this.payload);
                if (item != null) {
                    item.scheduleBuild2(0, gogsPayload);
                }
            } else {
                project.scheduleBuild(0, cause);
            }
            result.setMessage(String.format("Job '%s' is executed", jobName));
        } else {
            String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
            result.setStatus(404, msg);
            LOGGER.warning(msg);
        }
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        LOGGER.severe(sw.toString());
    } finally {
        SecurityContextHolder.setContext(saveCtx);
    }
    return result;
}
Also used : ParameterizedJobMixIn(jenkins.model.ParameterizedJobMixIn) Trigger(hudson.triggers.Trigger) StringWriter(java.io.StringWriter) BuildableItem(hudson.model.BuildableItem) Cause(hudson.model.Cause) SecurityContext(org.acegisecurity.context.SecurityContext) SCMTriggerItem(jenkins.triggers.SCMTriggerItem) PrintWriter(java.io.PrintWriter)

Example 2 with Cause

use of hudson.model.Cause in project workflow-job-plugin by jenkinsci.

the class BuildTriggerTest method smokes.

@Issue("JENKINS-28113")
@Test
public void smokes() throws Exception {
    BuildTrigger.DescriptorImpl d = r.jenkins.getDescriptorByType(BuildTrigger.DescriptorImpl.class);
    FreeStyleProject us = r.createProject(FreeStyleProject.class, "us");
    WorkflowJob ds = r.createProject(WorkflowJob.class, "ds");
    ds.setDefinition(new CpsFlowDefinition("", true));
    assertEquals(Collections.singletonList("ds"), d.doAutoCompleteChildProjects("d", us, r.jenkins).getValues());
    FormValidation validation = d.doCheck(us, "ds");
    assertEquals(validation.renderHtml(), FormValidation.Kind.OK, validation.kind);
    us.getPublishersList().add(new BuildTrigger("ds", Result.SUCCESS));
    r.jenkins.setQuietPeriod(0);
    FreeStyleBuild us1 = r.buildAndAssertSuccess(us);
    r.waitUntilNoActivity();
    WorkflowRun ds1 = ds.getLastBuild();
    assertNotNull("triggered", ds1);
    Cause.UpstreamCause cause = ds1.getCause(Cause.UpstreamCause.class);
    assertNotNull(cause);
    assertEquals(us1, cause.getUpstreamRun());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FormValidation(hudson.util.FormValidation) Cause(hudson.model.Cause) BuildTrigger(hudson.tasks.BuildTrigger) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 3 with Cause

use of hudson.model.Cause in project dependency-check-plugin by jenkinsci.

the class AbstractDependencyCheckBuilder method isSkip.

/**
 * Determine if the build should be skipped or not
 */
private boolean isSkip(final Run<?, ?> build, final TaskListener listener) {
    boolean skip = false;
    // Determine if the OWASP_DC_SKIP environment variable is set to true
    try {
        skip = Boolean.parseBoolean(build.getEnvironment(listener).get("OWASP_DC_SKIP"));
    } catch (Exception e) {
    /* throw it away */
    }
    // Why was this build triggered? Get the causes and find out.
    @SuppressWarnings("unchecked") final List<Cause> causes = build.getCauses();
    for (Cause cause : causes) {
        // Skip if the build is configured to skip on SCM change and the cause of the build was an SCM trigger
        if (skipOnScmChange && cause instanceof SCMTrigger.SCMTriggerCause) {
            skip = true;
        }
        // Skip if the build is configured to skip on Upstream change and the cause of the build was an Upstream trigger
        if (skipOnUpstreamChange && cause instanceof Cause.UpstreamCause) {
            skip = true;
        }
    }
    // Log a message if being skipped
    if (skip) {
        listener.getLogger().println(OUT_TAG + "Skipping Dependency-Check analysis.");
    }
    return skip;
}
Also used : SCMTrigger(hudson.triggers.SCMTrigger) Cause(hudson.model.Cause) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 4 with Cause

use of hudson.model.Cause in project sonar-scanner-jenkins by SonarSource.

the class TriggersConfig method isSkipSonar.

public String isSkipSonar(AbstractBuild<?, ?> build, BuildListener listener) throws IOException, InterruptedException {
    Result result = build.getResult();
    if (result != null && result.isWorseThan(Result.UNSTABLE)) {
        // unstable means that build completed, but there were some test failures, which is not critical for analysis
        return Messages.SonarPublisher_BadBuildStatus(result.toString());
    }
    // skip analysis by environment variable or build parameter
    if (getEnvVar() != null) {
        // check against build parameters
        String value = build.getBuildVariableResolver().resolve(getEnvVar());
        if ("true".equalsIgnoreCase(value)) {
            return Messages.Skipping_Sonar_analysis();
        }
        // check against environment variable
        EnvVars envVars = build.getEnvironment(listener);
        value = new VariableResolver.ByMap<>(envVars).resolve(getEnvVar());
        if ("true".equalsIgnoreCase(value)) {
            return Messages.Skipping_Sonar_analysis();
        }
    }
    List<Cause> causes = new ArrayList<>(build.getCauses());
    // skip analysis, when all causes from blacklist
    if (isSkipScmCause() || isSkipUpstreamCause()) {
        Iterator<Cause> iter = causes.iterator();
        while (iter.hasNext()) {
            Cause cause = iter.next();
            if ((isSkipScmCause() && SCMTrigger.SCMTriggerCause.class.isInstance(cause)) || (isSkipUpstreamCause() && Cause.UpstreamCause.class.isInstance(cause))) {
                iter.remove();
            }
        }
    }
    return causes.isEmpty() ? Messages.Skipping_Sonar_analysis() : null;
}
Also used : EnvVars(hudson.EnvVars) Cause(hudson.model.Cause) ArrayList(java.util.ArrayList) Result(hudson.model.Result)

Example 5 with Cause

use of hudson.model.Cause in project blueocean-plugin by jenkinsci.

the class DownstreamJobListener method onStarted.

@Override
public void onStarted(Run<?, ?> run, TaskListener listener) {
    for (Cause cause : run.getCauses()) {
        if (cause instanceof BuildUpstreamCause) {
            BuildUpstreamCause buildUpstreamCause = (BuildUpstreamCause) cause;
            Run triggerRun = buildUpstreamCause.getUpstreamRun();
            if (triggerRun instanceof WorkflowRun) {
                FlowExecution execution = ((WorkflowRun) triggerRun).getExecution();
                FlowNode node;
                if (execution == null) {
                    LOGGER.warning("Could not retrieve upstream FlowExecution");
                    continue;
                }
                try {
                    node = execution.getNode(buildUpstreamCause.getNodeId());
                } catch (IOException e) {
                    LOGGER.warning("Could not retrieve upstream node: " + e);
                    continue;
                }
                if (node == null) {
                    LOGGER.warning("Could not retrieve upstream node (null)");
                    continue;
                }
                // Add an action on the triggerRun node pointing to the currently executing run
                String description = run.getDescription();
                if (description == null) {
                    description = run.getFullDisplayName();
                }
                Link link = LinkResolver.resolveLink(run);
                if (link != null) {
                    try {
                        // Also add to the actual trigger node so we can find it later by step
                        node.addAction(new NodeDownstreamBuildAction(link, description));
                        node.save();
                    } catch (IOException e) {
                        LOGGER.severe("Could not persist node: " + e);
                    }
                }
            }
        }
    }
}
Also used : Cause(hudson.model.Cause) BuildUpstreamCause(org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) Run(hudson.model.Run) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) IOException(java.io.IOException) BuildUpstreamCause(org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Link(io.jenkins.blueocean.rest.hal.Link) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Aggregations

Cause (hudson.model.Cause)5 IOException (java.io.IOException)2 EnvVars (hudson.EnvVars)1 BuildableItem (hudson.model.BuildableItem)1 FreeStyleBuild (hudson.model.FreeStyleBuild)1 FreeStyleProject (hudson.model.FreeStyleProject)1 Result (hudson.model.Result)1 Run (hudson.model.Run)1 BuildTrigger (hudson.tasks.BuildTrigger)1 SCMTrigger (hudson.triggers.SCMTrigger)1 Trigger (hudson.triggers.Trigger)1 FormValidation (hudson.util.FormValidation)1 Link (io.jenkins.blueocean.rest.hal.Link)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 ParameterizedJobMixIn (jenkins.model.ParameterizedJobMixIn)1 SCMTriggerItem (jenkins.triggers.SCMTriggerItem)1 SecurityContext (org.acegisecurity.context.SecurityContext)1