Search in sources :

Example 1 with GlobMatcher

use of io.jenkins.blueocean.service.embedded.util.GlobMatcher 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)

Aggregations

Plugin (hudson.Plugin)1 Item (hudson.model.Item)1 ItemGroup (hudson.model.ItemGroup)1 UnexpectedErrorException (io.jenkins.blueocean.commons.ServiceException.UnexpectedErrorException)1 BlueOrganization (io.jenkins.blueocean.rest.model.BlueOrganization)1 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)1 GlobMatcher (io.jenkins.blueocean.service.embedded.util.GlobMatcher)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1