use of io.jenkins.blueocean.rest.model.BlueRun 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 io.jenkins.blueocean.rest.model.BlueRun in project blueocean-plugin by jenkinsci.
the class LinkResolverImpl method resolve.
@Override
public Link resolve(Object modelObject) {
if (modelObject instanceof FlowNode) {
FlowNode flowNode = (FlowNode) modelObject;
BlueRun r = resolveFlowNodeRun(flowNode);
if (PipelineNodeUtil.isParallelBranch(flowNode) || PipelineNodeUtil.isStage(flowNode)) {
// its Node
if (r != null) {
return r.getLink().rel("nodes/" + flowNode.getId());
}
} else if (flowNode instanceof StepAtomNode && !PipelineNodeUtil.isStage(flowNode)) {
if (r != null) {
return r.getLink().rel("steps/" + flowNode.getId());
}
}
} else if (modelObject instanceof BluePipelineNode || modelObject instanceof BluePipelineStep) {
return ((Resource) modelObject).getLink();
}
return null;
}
use of io.jenkins.blueocean.rest.model.BlueRun in project blueocean-plugin by jenkinsci.
the class MultiBranchPipelineImpl method getRuns.
@Override
public BlueRunContainer getRuns() {
return new BlueRunContainer() {
@Override
public Link getLink() {
return MultiBranchPipelineImpl.this.getLink().rel("runs");
}
@Override
public BlueRun get(String name) {
return null;
}
@Override
public Iterator<BlueRun> iterator() {
throw new ServiceException.NotImplementedException("Not implemented");
}
/**
* Fetches maximum up to MAX_MBP_RUNS_ROWS rows from each branch and does pagination on that.
*
* JVM property MAX_MBP_RUNS_ROWS can be used to tune this value to optimize performance for given setup
*/
@Override
public Iterator<BlueRun> iterator(int start, int limit) {
List<BlueRun> c = new ArrayList<>();
List<BluePipeline> branches;
// Check for branch filter
StaplerRequest req = Stapler.getCurrentRequest();
String branchFilter = null;
if (req != null) {
branchFilter = req.getParameter("branch");
}
if (!StringUtils.isEmpty(branchFilter)) {
BluePipeline pipeline = getBranches().get(branchFilter);
if (pipeline != null) {
branches = Collections.singletonList(pipeline);
} else {
branches = Collections.emptyList();
}
} else {
branches = Lists.newArrayList(getBranches().list());
sortBranchesByLatestRun(branches);
}
for (final BluePipeline b : branches) {
Iterator<BlueRun> it = b.getRuns().iterator(0, MAX_MBP_RUNS_ROWS);
int count = 0;
Iterators.skip(it, start);
while (it.hasNext() && count++ < limit) {
c.add(it.next());
}
}
Collections.sort(c, new Comparator<BlueRun>() {
@Override
public int compare(BlueRun o1, BlueRun o2) {
return o2.getStartTime().compareTo(o1.getStartTime());
}
});
return c.iterator();
}
private boolean retry(boolean[] retries) {
//if at least one of the branch needs retry we will retry it
for (boolean r : retries) {
if (r) {
return true;
}
}
return false;
}
private int computeLimit(boolean[] retries, int limit) {
//if at least one of the branch needs retry we will retry it
int count = 0;
for (boolean r : retries) {
if (r) {
count++;
}
}
if (count == 0) {
return 0;
}
return limit / count > 0 ? limit / count : 1;
}
private int collectRuns(List<BluePipeline> branches, List<BlueRun> runs, boolean[] retries, int remainingCount, int[] startIndexes, int[] limits) {
int count = 0;
for (int i = 0; i < branches.size(); i++) {
BluePipeline b = branches.get(i);
if (!retries[i]) {
continue;
}
Iterator<BlueRun> it = b.getRuns().iterator(startIndexes[i], limits[i]);
int lcount = 0;
while (it.hasNext() && count < remainingCount) {
lcount++;
count++;
runs.add(it.next());
}
if (lcount < limits[i]) {
//if its less than l
//iterator already exhausted so lets not retry next time
retries[i] = false;
} else {
//set the new start index for next time
startIndexes[i] = startIndexes[i] + lcount;
}
}
return count;
}
@Override
public BlueQueueItem create(StaplerRequest request) {
throw new ServiceException.NotImplementedException("This action is not supported");
}
};
}
use of io.jenkins.blueocean.rest.model.BlueRun in project blueocean-plugin by jenkinsci.
the class RunSearch method findRuns.
@SuppressWarnings("unchecked")
public static Iterable<BlueRun> findRuns(Job job, final Link parent, int start, int limit) {
final List<BlueRun> runs = new ArrayList<>();
Iterable<Job> pipelines;
if (job != null) {
pipelines = ImmutableList.of(job);
} else {
pipelines = Jenkins.getInstance().getItems(Job.class);
}
for (Job p : pipelines) {
Iterator<? extends Run> runIterator;
if (job instanceof LazyBuildMixIn.LazyLoadingJob) {
final LazyBuildMixIn lazyLoadMixin = ((LazyBuildMixIn.LazyLoadingJob) job).getLazyBuildMixIn();
runIterator = lazyLoadMixin.getRunMap().iterator();
} else {
runIterator = p.getBuilds().iterator();
}
runs.addAll(collectRuns(runIterator, parent, start, limit));
}
return runs;
}
use of io.jenkins.blueocean.rest.model.BlueRun in project blueocean-plugin by jenkinsci.
the class RunSearch method search.
@Override
public Pageable<BlueRun> search(Query q) {
String pipeline = q.param("pipeline", false);
boolean latestOnly = q.param("latestOnly", Boolean.class);
if (pipeline != null) {
TopLevelItem p = Jenkins.getActiveInstance().getItem(pipeline);
if (latestOnly) {
BlueRun r = getLatestRun((Job) p);
if (r != null) {
return Pageables.wrap(Collections.singletonList(r));
} else {
Pageables.empty();
}
}
if (p instanceof Job) {
return Pageables.wrap(findRuns((Job) p));
} else {
throw new ServiceException.BadRequestExpception(String.format("Pipeline %s not found", pipeline));
}
} else if (latestOnly) {
return Pageables.empty();
}
return Pageables.wrap(findRuns(null));
}
Aggregations