use of jenkins.model.Jenkins in project promoted-builds-plugin by jenkinsci.
the class ItemPathResolver method getByPath.
/**
* Gets an {@link Item} of the specified type by absolute or relative path.
* <p>
* The implementation retains the original behavior in {@link PromotedBuildParameterDefinition},
* but this method also provides a support of multi-level addressing including special markups
* for the relative addressing.
* </p>
* Effectively, the resolution order is following:
* <ul>
* <li><b>Optional</b> Legacy behavior, which can be enabled by {@link #ENABLE_LEGACY_RESOLUTION_AGAINST_ROOT}.
* If an item for the name exists on the top Jenkins level, it will be returned</li>
* <li>If the path starts with "/", a global addressing will be used</li>
* <li>If the path starts with "./" or "../", a relative addressing will be used</li>
* <li>If there is no prefix, a relative addressing will be tried. If it
* fails, the method falls back to a global one</li>
* </ul>
* For the relative and absolute addressing the engine supports "." and
* ".." markers within the path.
* The first one points to the current element, the second one - to the upper element.
* If the search cannot get a new top element (e.g. reached the root), the method returns {@code null}.
*
* @param <T> Type of the {@link Item} to be retrieved
* @param path Path string to the item.
* @param baseItem Base {@link Item} for the relative addressing. If null,
* this addressing approach will be skipped
* @param type Type of the {@link Item} to be retrieved
* @return Found {@link Item}. Null if it has not been found by all addressing modes
* or the type differs.
*/
@CheckForNull
@SuppressWarnings("unchecked")
@Restricted(NoExternalUse.class)
public static <T extends Item> T getByPath(@Nonnull String path, @CheckForNull Item baseItem, @Nonnull Class<T> type) {
final Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null) {
return null;
}
// Legacy behavior
if (isEnableLegacyResolutionAgainstRoot()) {
TopLevelItem topLevelItem = jenkins.getItem(path);
if (topLevelItem != null && type.isAssignableFrom(topLevelItem.getClass())) {
return (T) topLevelItem;
}
}
// Explicit global addressing
if (path.startsWith("/")) {
return findPath(jenkins, path.substring(1), type);
}
// Try the relative addressing if possible
if (baseItem != null) {
final ItemGroup<?> relativeRoot = baseItem instanceof ItemGroup<?> ? (ItemGroup<?>) baseItem : baseItem.getParent();
final T item = findPath(relativeRoot, path, type);
if (item != null) {
return item;
}
}
// Fallback to the default behavior (addressing from the Jenkins root)
return findPath(jenkins, path, type);
}
use of jenkins.model.Jenkins in project promoted-builds-plugin by jenkinsci.
the class JobDslPromotionProcessConverter method obtainClassOwnership.
@CheckForNull
private String obtainClassOwnership() {
if (this.classOwnership != null) {
return this.classOwnership;
}
if (pm == null) {
Jenkins j = Jenkins.getInstanceOrNull();
pm = j != null ? j.getPluginManager() : null;
}
if (pm == null) {
return null;
}
// TODO: possibly recursively scan super class to discover dependencies
PluginWrapper p = pm.whichPlugin(hudson.plugins.promoted_builds.PromotionProcess.class);
this.classOwnership = p != null ? p.getShortName() + '@' + trimVersion(p.getVersion()) : null;
return this.classOwnership;
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class BlueRunChangesetPreloader method getPipeline.
private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null) {
return null;
}
String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
try {
Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
} catch (Exception e) {
LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
return null;
}
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class FavoriteListStatePreloader method getStateJson.
@CheckForNull
@Override
public String getStateJson() {
User jenkinsUser = User.current();
if (jenkinsUser == null) {
return null;
}
FavoriteUserProperty fup = jenkinsUser.getProperty(FavoriteUserProperty.class);
if (fup == null) {
return null;
}
Set<String> favorites = fup.getAllFavorites();
if (favorites == null) {
return null;
}
final Jenkins jenkins = Jenkins.get();
final List<FavoritPreload> favoritPreloads = favorites.stream().map(name -> {
final Item item = jenkins.getItemByFullName(name);
if (item instanceof Job) {
final Job<?, ?> job = (Job<?, ?>) item;
if (job.getAction(PrimaryInstanceMetadataAction.class) != null) {
return new FavoritPreload(name, true);
}
}
return new FavoritPreload(name, false);
}).collect(Collectors.toList());
return JSONArray.fromObject(favoritPreloads).toString();
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class PipelineNodeTest method sequentialParallelStages.
@Test
@Issue("JENKINS-49050")
public void sequentialParallelStages() throws Exception {
WorkflowJob p = createWorkflowJobWithJenkinsfile(getClass(), "sequentialParallel.jenkinsfile");
Slave s = j.createOnlineSlave();
s.setNumExecutors(2);
// Run until completed
WorkflowRun run = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(run);
PipelineNodeGraphVisitor pipelineNodeGraphVisitor = new PipelineNodeGraphVisitor(run);
assertTrue(pipelineNodeGraphVisitor.isDeclarative());
List<FlowNodeWrapper> wrappers = pipelineNodeGraphVisitor.getPipelineNodes();
assertEquals(9, wrappers.size());
Optional<FlowNodeWrapper> optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getDisplayName().equals("first-sequential-stage")).findFirst();
// we ensure "multiple-stages" is parent of "first-sequential-stage"
assertTrue(optionalFlowNodeWrapper.isPresent());
assertEquals(1, optionalFlowNodeWrapper.get().edges.size());
assertEquals("second-sequential-stage", optionalFlowNodeWrapper.get().edges.get(0).getDisplayName());
final String parentId = optionalFlowNodeWrapper.get().getFirstParent().getId();
optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getId().equals(parentId)).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
assertEquals("multiple-stages", optionalFlowNodeWrapper.get().getDisplayName());
assertEquals(1, optionalFlowNodeWrapper.get().edges.size());
optionalFlowNodeWrapper.get().edges.stream().filter(nodeWrapper -> nodeWrapper.getDisplayName().equals("first-sequential-stage")).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getDisplayName().equals("other-single-stage")).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
final String otherParentId = optionalFlowNodeWrapper.get().getFirstParent().getId();
optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getId().equals(otherParentId)).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
assertEquals("parent", optionalFlowNodeWrapper.get().getDisplayName());
assertEquals(3, optionalFlowNodeWrapper.get().edges.size());
optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getDisplayName().equals("second-sequential-stage")).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
assertEquals(1, optionalFlowNodeWrapper.get().edges.size());
assertEquals("third-sequential-stage", optionalFlowNodeWrapper.get().edges.get(0).getDisplayName());
optionalFlowNodeWrapper = wrappers.stream().filter(nodeWrapper -> nodeWrapper.getDisplayName().equals("third-sequential-stage")).findFirst();
assertTrue(optionalFlowNodeWrapper.isPresent());
assertEquals(1, optionalFlowNodeWrapper.get().edges.size());
assertEquals("second-solo", optionalFlowNodeWrapper.get().edges.get(0).getDisplayName());
List<Map> nodes = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/1/nodes/", List.class);
assertEquals(9, nodes.size());
Optional<Map> firstSeqStage = nodes.stream().filter(map -> map.get("displayName").equals("first-sequential-stage")).findFirst();
assertTrue(firstSeqStage.isPresent());
String firstParentId = (String) firstSeqStage.get().get("firstParent");
Optional<Map> parentStage = nodes.stream().filter(map -> map.get("id").equals(firstParentId)).findFirst();
assertTrue(parentStage.isPresent());
assertEquals("multiple-stages", parentStage.get().get("displayName"));
// ensure no issue getting steps for each node
for (Map<String, String> node : nodes) {
String id = node.get("id");
List<Map> steps = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/1/nodes/" + id + "/steps/", List.class);
assertFalse(steps.get(0).isEmpty());
}
}
Aggregations