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;
}
}
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);
}
}
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;
}
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());
}
}
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;
}
Aggregations