Search in sources :

Example 1 with Item

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

the class BlueMessageEnricher method enrich.

@Override
public void enrich(@Nonnull Message message) {
    // TODO: Get organization name in generic way once multi-organization support is implemented in API
    message.set(EventProps.Jenkins.jenkins_org, OrganizationImpl.INSTANCE.getName());
    String channelName = message.getChannelName();
    if (channelName.equals(Events.JobChannel.NAME)) {
        JobChannelMessage jobChannelMessage = (JobChannelMessage) message;
        Item jobChannelItem = jobChannelMessage.getJobChannelItem();
        Link jobUrl = LinkResolver.resolveLink(jobChannelItem);
        jobChannelMessage.set(BlueEventProps.blueocean_job_rest_url, jobUrl.getHref());
        jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, jobChannelItem.getFullName());
        if (jobChannelItem instanceof WorkflowJob) {
            ItemGroup<? extends Item> parent = jobChannelItem.getParent();
            if (parent instanceof WorkflowMultiBranchProject) {
                String multiBranchProjectName = parent.getFullName();
                jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, multiBranchProjectName);
                jobChannelMessage.set(BlueEventProps.blueocean_job_branch_name, jobChannelItem.getName());
            }
        }
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) Item(hudson.model.Item) JobChannelMessage(org.jenkinsci.plugins.pubsub.JobChannelMessage) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Link(io.jenkins.blueocean.rest.hal.Link)

Example 2 with Item

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

the class BlueOceanWebURLBuilder method getTryBlueOceanURLs.

/**
     * Get the {@link TryBlueOceanURLs} instance for the {@link ModelObject}
     * associated with the current Stapler request.
     *
     * @return The {@link TryBlueOceanURLs} instance for the current classic
     * Jenkins page. The URL to the Blue Ocean homepage is returned if a more
     * appropriate URL is not found.
     */
@Nonnull
public static TryBlueOceanURLs getTryBlueOceanURLs() {
    StaplerRequest staplerRequest = Stapler.getCurrentRequest();
    List<Ancestor> list = staplerRequest.getAncestors();
    // Blue Ocean page we can link onto.
    for (int i = list.size() - 1; i >= 0; i--) {
        Ancestor ancestor = list.get(i);
        Object object = ancestor.getObject();
        if (object instanceof ModelObject) {
            String blueUrl = toBlueOceanURL((ModelObject) object);
            if (blueUrl != null) {
                if (object instanceof Item) {
                    return new TryBlueOceanURLs(blueUrl, ((Item) object).getUrl());
                } else if (object instanceof Run) {
                    return new TryBlueOceanURLs(blueUrl, ((Run) object).getUrl());
                } else {
                    return new TryBlueOceanURLs(blueUrl);
                }
            } else if (object instanceof Item) {
                return new TryBlueOceanURLs(getBlueHome(), ((Item) object).getUrl());
            }
        }
    }
    // Otherwise just return Blue Ocean home.
    return new TryBlueOceanURLs(getBlueHome());
}
Also used : Item(hudson.model.Item) ModelObject(hudson.model.ModelObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ModelObject(hudson.model.ModelObject) Run(hudson.model.Run) Ancestor(org.kohsuke.stapler.Ancestor) Nonnull(javax.annotation.Nonnull)

Example 3 with Item

use of hudson.model.Item in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinition method create.

@Override
public CpsFlowExecution create(FlowExecutionOwner owner, TaskListener listener, List<? extends Action> actions) throws Exception {
    for (Action a : actions) {
        if (a instanceof CpsFlowFactoryAction2) {
            return ((CpsFlowFactoryAction2) a).create(this, owner, actions);
        }
    }
    Queue.Executable _build = owner.getExecutable();
    if (!(_build instanceof Run)) {
        throw new IOException("can only check out SCM into a Run");
    }
    Run<?, ?> build = (Run<?, ?>) _build;
    if (isLightweight()) {
        try (SCMFileSystem fs = SCMFileSystem.of(build.getParent(), scm)) {
            if (fs != null) {
                String script = fs.child(scriptPath).contentAsString();
                listener.getLogger().println("Obtained " + scriptPath + " from " + scm.getKey());
                Queue.Executable exec = owner.getExecutable();
                FlowDurabilityHint hint = (exec instanceof Item) ? DurabilityHintProvider.suggestedFor((Item) exec) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
                return new CpsFlowExecution(script, true, owner, hint);
            } else {
                listener.getLogger().println("Lightweight checkout support not available, falling back to full checkout.");
            }
        }
    }
    FilePath dir;
    Node node = Jenkins.getActiveInstance();
    if (build.getParent() instanceof TopLevelItem) {
        FilePath baseWorkspace = node.getWorkspaceFor((TopLevelItem) build.getParent());
        if (baseWorkspace == null) {
            throw new IOException(node.getDisplayName() + " may be offline");
        }
        dir = getFilePathWithSuffix(baseWorkspace);
    } else {
        // should not happen, but just in case:
        dir = new FilePath(owner.getRootDir());
    }
    listener.getLogger().println("Checking out " + scm.getKey() + " into " + dir + " to read " + scriptPath);
    String script = null;
    Computer computer = node.toComputer();
    if (computer == null) {
        throw new IOException(node.getDisplayName() + " may be offline");
    }
    SCMStep delegate = new GenericSCMStep(scm);
    delegate.setPoll(true);
    delegate.setChangelog(true);
    FilePath acquiredDir;
    try (WorkspaceList.Lease lease = computer.getWorkspaceList().acquire(dir)) {
        for (int retryCount = Jenkins.getInstance().getScmCheckoutRetryCount(); retryCount >= 0; retryCount--) {
            try {
                delegate.checkout(build, dir, listener, node.createLauncher(listener));
                break;
            } catch (AbortException e) {
                // If so, just skip echoing it.
                if (e.getMessage() != null) {
                    listener.error(e.getMessage());
                }
            } catch (InterruptedIOException e) {
                throw e;
            } catch (IOException e) {
                // checkout error not yet reported
                // TODO 2.43+ use Functions.printStackTrace
                listener.error("Checkout failed").println(Functions.printThrowable(e).trim());
            }
            if (// all attempts failed
            retryCount == 0)
                throw new AbortException("Maximum checkout retry attempts reached, aborting");
            listener.getLogger().println("Retrying after 10 seconds");
            Thread.sleep(10000);
        }
        FilePath scriptFile = dir.child(scriptPath);
        if (!scriptFile.absolutize().getRemote().replace('\\', '/').startsWith(dir.absolutize().getRemote().replace('\\', '/') + '/')) {
            // TODO JENKINS-26838
            throw new IOException(scriptFile + " is not inside " + dir);
        }
        if (!scriptFile.exists()) {
            throw new AbortException(scriptFile + " not found");
        }
        script = scriptFile.readToString();
        acquiredDir = lease.path;
    }
    Queue.Executable queueExec = owner.getExecutable();
    FlowDurabilityHint hint = (queueExec instanceof Run) ? DurabilityHintProvider.suggestedFor(((Run) queueExec).getParent()) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
    CpsFlowExecution exec = new CpsFlowExecution(script, true, owner, hint);
    exec.flowStartNodeActions.add(new WorkspaceActionImpl(acquiredDir, null));
    return exec;
}
Also used : FilePath(hudson.FilePath) InterruptedIOException(java.io.InterruptedIOException) Action(hudson.model.Action) Node(hudson.model.Node) TopLevelItem(hudson.model.TopLevelItem) Run(hudson.model.Run) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) WorkspaceList(hudson.slaves.WorkspaceList) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) GenericSCMStep(org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) TopLevelItem(hudson.model.TopLevelItem) Item(hudson.model.Item) WorkspaceActionImpl(org.jenkinsci.plugins.workflow.support.actions.WorkspaceActionImpl) SCMFileSystem(jenkins.scm.api.SCMFileSystem) GenericSCMStep(org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep) SCMStep(org.jenkinsci.plugins.workflow.steps.scm.SCMStep) Computer(hudson.model.Computer) Queue(hudson.model.Queue) AbortException(hudson.AbortException)

Example 4 with Item

use of hudson.model.Item in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method createAndRunSleeperJob.

static WorkflowRun createAndRunSleeperJob(Jenkins jenkins, String jobName, FlowDurabilityHint durabilityHint) throws Exception {
    Item prev = jenkins.getItemByFullName(jobName);
    if (prev != null) {
        prev.delete();
    }
    WorkflowJob job = jenkins.createProject(WorkflowJob.class, jobName);
    CpsFlowDefinition def = new CpsFlowDefinition("node {\n " + "sleep 30 \n" + "} \n" + "echo 'I like cheese'\n", false);
    TestDurabilityHintProvider provider = Jenkins.getInstance().getExtensionList(TestDurabilityHintProvider.class).get(0);
    provider.registerHint(jobName, durabilityHint);
    job.setDefinition(def);
    WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
    // Hacky but we just need to ensure this can start up
    Thread.sleep(4000L);
    Assert.assertEquals(durabilityHint, run.getExecution().getDurabilityHint());
    Assert.assertEquals("sleep", run.getExecution().getCurrentHeads().get(0).getDisplayFunctionName());
    return run;
}
Also used : Item(hudson.model.Item) TestDurabilityHintProvider(org.jenkinsci.plugins.workflow.TestDurabilityHintProvider) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun)

Example 5 with Item

use of hudson.model.Item in project promoted-builds-plugin by jenkinsci.

the class PromotedBuildParameterDefinition method getBuilds.

/**
 * Gets a list of promoted builds for the project.
 * @return List of {@link AbstractBuild}s, which have been promoted
 * @deprecated This method retrieves the base item for relative addressing from
 * the {@link StaplerRequest}. The relative addressing may be malfunctional if
 * you use this method outside {@link StaplerRequest}s.
 * Use {@link #getRuns(hudson.model.Item)} instead
 */
@Nonnull
@Deprecated
public List getBuilds() {
    // Try to get ancestor from the object, otherwise pass null and disable the relative addressing
    final StaplerRequest currentRequest = Stapler.getCurrentRequest();
    final Item item = currentRequest != null ? currentRequest.findAncestorObject(Item.class) : null;
    return getRuns(item);
}
Also used : Item(hudson.model.Item) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Nonnull(javax.annotation.Nonnull)

Aggregations

Item (hudson.model.Item)36 ItemGroup (hudson.model.ItemGroup)7 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)7 IOException (java.io.IOException)7 Run (hudson.model.Run)6 Nonnull (javax.annotation.Nonnull)5 Job (hudson.model.Job)4 TopLevelItem (hudson.model.TopLevelItem)4 BlueOrganization (io.jenkins.blueocean.rest.model.BlueOrganization)4 ArrayList (java.util.ArrayList)4 MultiBranchProject (jenkins.branch.MultiBranchProject)4 Jenkins (jenkins.model.Jenkins)4 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)4 StaplerRequest (org.kohsuke.stapler.StaplerRequest)4 Queue (hudson.model.Queue)3 User (hudson.model.User)3 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)2 ExtensionPoint (hudson.ExtensionPoint)2 FreeStyleProject (hudson.model.FreeStyleProject)2