Search in sources :

Example 36 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class BrowserAndOperatingSystemAnalyticsProperties method properties.

@Override
public Map<String, Object> properties(TrackRequest req) {
    StaplerRequest httpReq = Stapler.getCurrentRequest();
    if (httpReq == null) {
        return null;
    }
    String userAgent = httpReq.getHeader("User-Agent");
    if (userAgent == null) {
        return null;
    }
    Client client = PARSER.parse(userAgent);
    String browserFamily = client.userAgent.family;
    // If we can't find the browser family then we shouldn't record anything
    if (browserFamily == null) {
        return null;
    }
    Map<String, Object> props = new HashMap<>();
    props.put("browserFamily", browserFamily);
    String browserVersionMajor = client.userAgent.major;
    // Versions are useful if they are available
    if (isNotEmpty(browserVersionMajor)) {
        props.put("browserVersionMajor", browserVersionMajor);
    }
    String browserVersionMinor = client.userAgent.minor;
    if (isNotEmpty(browserVersionMinor)) {
        props.put("browserVersionMinor", browserVersionMinor);
    }
    // If the operating system is available lets use that
    String operatingSystemFamily = client.os.family;
    if (isNotEmpty(operatingSystemFamily)) {
        props.put("osFamily", operatingSystemFamily);
        String osVersionMajor = client.os.major;
        if (isNotEmpty(osVersionMajor)) {
            props.put("osVersionMajor", osVersionMajor);
        }
        String osVersionMinor = client.os.minor;
        if (isNotEmpty(osVersionMinor)) {
            props.put("osVersionMinor", osVersionMinor);
        }
    }
    return props;
}
Also used : HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Client(ua_parser.Client)

Example 37 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class BrowserAndOperatingSystemAnalyticsPropertiesTest method setup.

private BrowserAndOperatingSystemAnalyticsProperties setup(String userAgent) {
    StaplerRequest request = mock(StaplerRequest.class);
    when(request.getHeader("User-Agent")).thenReturn(userAgent);
    mockStatic(Stapler.class);
    when(Stapler.getCurrentRequest()).thenReturn(request);
    return new BrowserAndOperatingSystemAnalyticsProperties();
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Example 38 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest 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());
    }
}
Also used : Item(hudson.model.Item) ItemGroup(hudson.model.ItemGroup) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ArrayList(java.util.ArrayList) MockFolder(org.jvnet.hudson.test.MockFolder) FreeStyleProject(hudson.model.FreeStyleProject) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 39 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest 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 40 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class ContainerFilter method filter.

/**
     * Filters the item list based on the current StaplerRequest
     */
public static <T extends Item> Collection<T> filter(Collection<T> items) {
    StaplerRequest req = Stapler.getCurrentRequest();
    if (req == null) {
        return items;
    }
    String itemFilter = req.getParameter("filter");
    if (itemFilter == null) {
        return items;
    }
    return filter(items, itemFilter.split(","));
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4