Search in sources :

Example 6 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 7 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 8 with Item

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

the class PipelineBranchRunStatePreloader method getFetchData.

@Override
protected FetchData getFetchData(@Nonnull BlueUrlTokenizer blueUrl) {
    //
    if (!blueUrl.hasPart(BlueUrlTokenizer.UrlPart.BRANCH) || !blueUrl.hasPart(BlueUrlTokenizer.UrlPart.PIPELINE_RUN_DETAIL_ID)) {
        // Not interested in it
        return null;
    }
    Jenkins jenkins = Jenkins.getInstance();
    String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
    String branchName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.BRANCH);
    String runId = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE_RUN_DETAIL_ID);
    Item pipelineJobItem = jenkins.getItemByFullName(pipelineFullName);
    if (pipelineJobItem instanceof MultiBranchProject) {
        try {
            MultiBranchProject pipelineMBP = (MultiBranchProject) pipelineJobItem;
            Job pipelineBranchJob = pipelineMBP.getItem(branchName);
            if (pipelineBranchJob != null) {
                Run run = pipelineBranchJob.getBuild(runId);
                if (run != null) {
                    BlueRun blueRun = AbstractRunImpl.getBlueRun(run, BluePipelineFactory.resolve(pipelineBranchJob));
                    if (blueRun != null) {
                        try {
                            return new FetchData(blueRun.getLink().getHref(), ModelObjectSerializer.toJson(blueRun));
                        } catch (IOException e) {
                            LOGGER.log(Level.FINE, String.format("Unable to preload run for pipeline '%s'. Run serialization error.", run.getUrl()), e);
                            return null;
                        }
                    } else {
                        LOGGER.log(Level.FINE, String.format("Unable to find run %s on branch named %s on pipeline named '%s'.", runId, branchName, pipelineFullName));
                        return null;
                    }
                }
            } else {
                LOGGER.log(Level.FINE, String.format("Unable to find branch named %s on pipeline named '%s'.", branchName, pipelineFullName));
                return null;
            }
        } catch (Exception e) {
            LOGGER.log(Level.FINE, String.format("Unable to find run from pipeline named '%s'.", pipelineFullName), e);
            return null;
        }
    } else {
        LOGGER.log(Level.FINE, String.format("Unable to find pipeline named '%s'.", pipelineFullName));
        return null;
    }
    // Don't preload any data on the page.
    return null;
}
Also used : Jenkins(jenkins.model.Jenkins) Item(hudson.model.Item) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) MultiBranchProject(jenkins.branch.MultiBranchProject) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) Run(hudson.model.Run) IOException(java.io.IOException) Job(hudson.model.Job) IOException(java.io.IOException)

Example 9 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)

Example 10 with Item

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

the class ItemPathResolver method findPath.

@CheckForNull
private static Item findPath(@CheckForNull ItemGroup base, @Nonnull String path) {
    @CheckForNull ItemGroup<?> pointer = base;
    final StringTokenizer t = new StringTokenizer(path, "/");
    while (pointer != null && t.hasMoreTokens()) {
        String current = t.nextToken();
        if (current.equals("..")) {
            if (pointer instanceof Item) {
                Item currentItem = (Item) pointer;
                pointer = currentItem.getParent();
            } else {
                // Cannot go upstairs
                pointer = null;
            }
        } else if (current.equals(".")) {
        // Do nothing, we stay on the same level
        } else {
            // Resolve the level beneath
            final Item item = pointer.getItem(current);
            if (!t.hasMoreTokens()) {
                // Last token => we consider it as a required item
                return item;
            }
            if (item instanceof ItemGroup<?>) {
                pointer = (ItemGroup<?>) item;
            } else {
                // Wrong path, we got to item before finishing the requested path
                pointer = null;
            }
        }
    }
    // Cannot retrieve the path => exit with null
    if (pointer instanceof Item) {
        return (Item) pointer;
    }
    return null;
}
Also used : TopLevelItem(hudson.model.TopLevelItem) Item(hudson.model.Item) StringTokenizer(java.util.StringTokenizer) ItemGroup(hudson.model.ItemGroup) CheckForNull(javax.annotation.CheckForNull) CheckForNull(javax.annotation.CheckForNull)

Aggregations

Item (hudson.model.Item)22 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)5 ItemGroup (hudson.model.ItemGroup)4 Run (hudson.model.Run)4 TopLevelItem (hudson.model.TopLevelItem)4 MultiBranchProject (jenkins.branch.MultiBranchProject)4 Jenkins (jenkins.model.Jenkins)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Nonnull (javax.annotation.Nonnull)3 StaplerRequest (org.kohsuke.stapler.StaplerRequest)3 Job (hudson.model.Job)2 ModelObject (hudson.model.ModelObject)2 ACL (hudson.security.ACL)2 CheckForNull (javax.annotation.CheckForNull)2 OrganizationFolder (jenkins.branch.OrganizationFolder)2 Authentication (org.acegisecurity.Authentication)2 JobChannelMessage (org.jenkinsci.plugins.pubsub.JobChannelMessage)2 Ancestor (org.kohsuke.stapler.Ancestor)2 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)1