Search in sources :

Example 1 with ItemGroup

use of hudson.model.ItemGroup in project hudson-2.x by hudson.

the class Functions method getRelativeLinkTo.

/**
     * Computes the relative path from the current page to the given item.
     */
public static String getRelativeLinkTo(Item p) {
    Map<Object, String> ancestors = new HashMap<Object, String>();
    View view = null;
    StaplerRequest request = Stapler.getCurrentRequest();
    for (Ancestor a : request.getAncestors()) {
        ancestors.put(a.getObject(), a.getRelativePath());
        if (a.getObject() instanceof View)
            view = (View) a.getObject();
    }
    String path = ancestors.get(p);
    if (path != null)
        return path;
    Item i = p;
    String url = "";
    while (true) {
        ItemGroup ig = i.getParent();
        url = i.getShortUrl() + url;
        if (ig == Hudson.getInstance()) {
            assert i instanceof TopLevelItem;
            if (view != null && view.contains((TopLevelItem) i)) {
                // if p and the current page belongs to the same view, then return a relative path
                return ancestors.get(view) + '/' + url;
            } else {
                // otherwise return a path from the root Hudson
                return request.getContextPath() + '/' + p.getUrl();
            }
        }
        path = ancestors.get(ig);
        if (path != null)
            return path + '/' + url;
        // if not, ig must have been the Hudson instance
        assert ig instanceof Item;
        i = (Item) ig;
    }
}
Also used : Item(hudson.model.Item) TopLevelItem(hudson.model.TopLevelItem) ItemGroup(hudson.model.ItemGroup) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) TopLevelItem(hudson.model.TopLevelItem) SearchableModelObject(hudson.search.SearchableModelObject) ModelObject(hudson.model.ModelObject) View(hudson.model.View) Ancestor(org.kohsuke.stapler.Ancestor)

Example 2 with ItemGroup

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

the class PipelineSearch method search.

@Override
public Pageable<BluePipeline> search(Query q) {
    String s = q.param(EXCLUDED_FROM_FLATTENING_PARAM);
    ItemGroup orgItemGroup = findItemGroup(q);
    List<Class> excludeList = new ArrayList<>();
    if (s != null) {
        for (String s1 : s.split(",")) {
            Class c = null;
            try {
                c = Class.forName(s1);
            } catch (ClassNotFoundException e) {
                try {
                    // TODO: There should be better ways to find a class from a plugin.
                    Plugin p = Jenkins.getInstance().getPlugin("blueocean-pipeline-api-impl");
                    if (p != null) {
                        c = p.getWrapper().classLoader.loadClass(s1);
                    } else {
                        logger.error("blueocean-pipeline-api-impl plugin not found!");
                    }
                } catch (ClassNotFoundException e1) {
                    logger.error(e.getMessage(), e1);
                }
            // ignored, give other OmniSearch implementations chance, they might handle it
            // throw new ServiceException.BadRequestException(String.format("%s parameter has invalid value: %s", EXCLUDED_FROM_FLATTENING_PARAM, s1), e);
            }
            if (c != null) {
                excludeList.add(c);
            }
        }
    }
    Collection<Item> items = new ArrayList<>();
    if (!excludeList.isEmpty()) {
        for (Item item : getAllItems(orgItemGroup)) {
            if (!exclude(item.getParent(), excludeList)) {
                items.add(item);
            }
        }
    } else {
        items = getAllItems(orgItemGroup);
    }
    items = ContainerFilter.filter(items);
    BlueOrganization org = OrganizationFactory.getInstance().getContainingOrg(orgItemGroup);
    if (org == null) {
        throw new ServiceException.UnexpectedErrorException("Could not find organization");
    }
    final Iterator<BluePipeline> pipelineIterator = new PipelineContainerImpl(org, orgItemGroup, org).getPipelines(items);
    final List<BluePipeline> pipelines = new ArrayList<>();
    String pipeline = q.param(getType());
    if (pipeline == null) {
        return Pageables.wrap(new Iterable<BluePipeline>() {

            @Override
            public Iterator<BluePipeline> iterator() {
                return pipelineIterator;
            }
        });
    } else {
        GlobMatcher matcher = pipeline.contains("*") ? new GlobMatcher(pipeline) : null;
        if (matcher != null) {
            while (pipelineIterator.hasNext()) {
                BluePipeline p = pipelineIterator.next();
                String decodedName = null;
                try {
                    decodedName = URLDecoder.decode(p.getFullDisplayName(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new UnexpectedErrorException("Could not decode '" + p.getFullDisplayName() + "'", e);
                }
                if (matcher.matches(decodedName)) {
                    pipelines.add(p);
                }
            }
        } else {
            while (pipelineIterator.hasNext()) {
                BluePipeline p = pipelineIterator.next();
                if (pipeline.equals(p.getFullDisplayName())) {
                    pipelines.add(p);
                }
            }
        }
        return Pageables.wrap(pipelines);
    }
}
Also used : GlobMatcher(io.jenkins.blueocean.service.embedded.util.GlobMatcher) BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) ArrayList(java.util.ArrayList) UnexpectedErrorException(io.jenkins.blueocean.commons.ServiceException.UnexpectedErrorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Item(hudson.model.Item) ItemGroup(hudson.model.ItemGroup) Iterator(java.util.Iterator) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) Plugin(hudson.Plugin)

Example 3 with ItemGroup

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

the class PipelineSearch method findItemGroup.

/**
 * If the search restricts the scope to a specific org (aka ItemGroup), return that, or else
 * the default scope, which is {@link Jenkins}.
 */
private ItemGroup findItemGroup(Query q) {
    String org = q.param(ORGANIZATION_PARAM);
    if (org == null)
        return Jenkins.getInstance();
    ItemGroup group = OrganizationFactory.getItemGroup(org);
    if (group == null) {
        throw new ServiceException.BadRequestException(String.format("Organization %s not found. Query parameter %s value: %s is invalid. ", org, ORGANIZATION_PARAM, org));
    }
    return group;
}
Also used : ItemGroup(hudson.model.ItemGroup)

Example 4 with ItemGroup

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

the class ContainerFilterTest method testPagedFilter.

@Test
public void testPagedFilter() throws IOException {
    StaplerRequest request = mock(StaplerRequest.class);
    when(request.getParameter("filter")).thenReturn("itemgroup-only");
    mockStatic(Stapler.class);
    when(Stapler.getCurrentRequest()).thenReturn(request);
    List<Item> items = new ArrayList<>();
    MockFolder folder = j.createFolder("folder");
    for (int i = 0; i < 50; i++) {
        FreeStyleProject job = folder.createProject(FreeStyleProject.class, "job" + i);
        items.add(folder.createProject(MockFolder.class, "subFolder" + i));
        items.add(job);
    }
    assertEquals(100, items.size());
    // there are total 50 folders in items, we want 25 of them starting 25th ending at 49th.
    Collection<Item> jobs = ContainerFilter.filter(items, 25, 25);
    assertEquals(25, jobs.size());
    int i = 25;
    for (Item item : jobs) {
        assertTrue(item instanceof ItemGroup);
        assertEquals("subFolder" + i++, item.getName());
    }
}
Also used : Item(hudson.model.Item) ItemGroup(hudson.model.ItemGroup) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ArrayList(java.util.ArrayList) MockFolder(org.jvnet.hudson.test.MockFolder) FreeStyleProject(hudson.model.FreeStyleProject) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ItemGroup

use of hudson.model.ItemGroup 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

ItemGroup (hudson.model.ItemGroup)9 Item (hudson.model.Item)7 TopLevelItem (hudson.model.TopLevelItem)2 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)2 ArrayList (java.util.ArrayList)2 StaplerRequest (org.kohsuke.stapler.StaplerRequest)2 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)1 Plugin (hudson.Plugin)1 FreeStyleProject (hudson.model.FreeStyleProject)1 Job (hudson.model.Job)1 ModelObject (hudson.model.ModelObject)1 Run (hudson.model.Run)1 TopLevelItemDescriptor (hudson.model.TopLevelItemDescriptor)1 View (hudson.model.View)1 SearchableModelObject (hudson.search.SearchableModelObject)1 ACL (hudson.security.ACL)1 UnexpectedErrorException (io.jenkins.blueocean.commons.ServiceException.UnexpectedErrorException)1 BranchImpl (io.jenkins.blueocean.rest.impl.pipeline.BranchImpl)1 BlueOrganization (io.jenkins.blueocean.rest.model.BlueOrganization)1 Resource (io.jenkins.blueocean.rest.model.Resource)1