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