Search in sources :

Example 1 with BlueRun

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;
}
Also used : Jenkins(jenkins.model.Jenkins) Item(hudson.model.Item) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) MultiBranchProject(jenkins.branch.MultiBranchProject) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) Run(hudson.model.Run) IOException(java.io.IOException) Job(hudson.model.Job) IOException(java.io.IOException)

Example 2 with BlueRun

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;
}
Also used : BlueRun(io.jenkins.blueocean.rest.model.BlueRun) StepAtomNode(org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode) BluePipelineNode(io.jenkins.blueocean.rest.model.BluePipelineNode) BluePipelineStep(io.jenkins.blueocean.rest.model.BluePipelineStep) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 3 with BlueRun

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");
        }
    };
}
Also used : ArrayList(java.util.ArrayList) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) BlueRunContainer(io.jenkins.blueocean.rest.model.BlueRunContainer) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with BlueRun

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;
}
Also used : LazyBuildMixIn(jenkins.model.lazy.LazyBuildMixIn) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) ArrayList(java.util.ArrayList) Job(hudson.model.Job)

Example 5 with BlueRun

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));
}
Also used : BlueRun(io.jenkins.blueocean.rest.model.BlueRun) TopLevelItem(hudson.model.TopLevelItem) Job(hudson.model.Job)

Aggregations

BlueRun (io.jenkins.blueocean.rest.model.BlueRun)6 Job (hudson.model.Job)4 ArrayList (java.util.ArrayList)3 Run (hudson.model.Run)2 Item (hudson.model.Item)1 TopLevelItem (hudson.model.TopLevelItem)1 Reachable (io.jenkins.blueocean.rest.Reachable)1 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)1 BluePipelineNode (io.jenkins.blueocean.rest.model.BluePipelineNode)1 BluePipelineStep (io.jenkins.blueocean.rest.model.BluePipelineStep)1 BlueRunContainer (io.jenkins.blueocean.rest.model.BlueRunContainer)1 IOException (java.io.IOException)1 List (java.util.List)1 MultiBranchProject (jenkins.branch.MultiBranchProject)1 Jenkins (jenkins.model.Jenkins)1 LazyBuildMixIn (jenkins.model.lazy.LazyBuildMixIn)1 StepAtomNode (org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode)1 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1