Skip to content

Commit

Permalink
[FIXED JENKINS-37174] Do not enumerate section items repeatedly while…
Browse files Browse the repository at this point in the history
… loading the view
  • Loading branch information
olivergondza committed May 29, 2017
1 parent a0e46f0 commit e7ddff1
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 80 deletions.
16 changes: 15 additions & 1 deletion pom.xml
@@ -1,6 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.580</version>
Expand Down
104 changes: 55 additions & 49 deletions src/main/java/hudson/plugins/sectioned_view/SectionedViewSection.java
Expand Up @@ -43,6 +43,7 @@
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import jenkins.model.Jenkins;
import org.kohsuke.stapler.Stapler;
Expand All @@ -65,41 +66,46 @@ public SectionedViewSection(String name, Width width, Positioning alignment) {

private String name;

/**
* Include regex string.
*/
String includeRegex;
/**
* Include regex string.
*/
String includeRegex;

/**
* Compiled include pattern from the includeRegex string.
*/
transient Pattern includePattern;
/**
* Compiled include pattern from the includeRegex string.
*/
transient Pattern includePattern;

private Width width;
private Width width;

private Positioning alignment;
private Positioning alignment;

transient String css;
transient String css;

/**
* Returns all the registered {@link SectionedViewSection} descriptors.
*/
public static DescriptorExtensionList<SectionedViewSection, SectionedViewSectionDescriptor> all() {
return Hudson.getInstance().<SectionedViewSection, SectionedViewSectionDescriptor>getDescriptorList(SectionedViewSection.class);
return Hudson.getInstance().<SectionedViewSection, SectionedViewSectionDescriptor>getDescriptorList(SectionedViewSection.class);
}

public String getName() {
return name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}

public String getIncludeRegex() {
return includeRegex;
}

public void setIncludeRegex(String regex) throws PatternSyntaxException {
includeRegex = regex;
includePattern = Pattern.compile(regex);
}

public String getIncludeRegex() {
return includeRegex;
}

public Iterable<ViewJobFilter> getJobFilters() {
return jobFilters;
}
Expand All @@ -112,7 +118,7 @@ public boolean hasJobFilterExtensions() {
return getDescriptor().hasJobFilterExtensions();
}

public Width getWidth() {
public Width getWidth() {
return width;
}

Expand All @@ -129,31 +135,31 @@ public void setAlignment(Positioning alignment) {
}

public boolean contains(TopLevelItem item, ItemGroup<? extends TopLevelItem> itemGroup) {
return jobNames.contains(item.getRelativeNameFrom(itemGroup));
}

protected Object readResolve() {
if (includeRegex != null)
includePattern = Pattern.compile(includeRegex);
if (width == null)
width = Width.FULL;
if (alignment == null)
alignment = Positioning.CENTER;
determineCss();
return jobNames.contains(item.getRelativeNameFrom(itemGroup));
}

protected Object readResolve() {
if (includeRegex != null)
setIncludeRegex(includeRegex);
if (width == null)
width = Width.FULL;
if (alignment == null)
alignment = Positioning.CENTER;
determineCss();
initJobFilters();
return this;
}

private void determineCss() {
final StringBuffer css = new StringBuffer();
css.append(alignment.getCss());
if (width == Width.THIRD && alignment == Positioning.CENTER) {
css.append("width: 34%; "); // the center part takes 34% so 33% + 34% + 33% = 100%
}
else {
css.append(width.getCss());
}
return this;
}

private void determineCss() {
final StringBuffer css = new StringBuffer();

css.append(alignment.getCss());
if (width == Width.THIRD && alignment == Positioning.CENTER) {
css.append("width: 34%; "); // the center part takes 34% so 33% + 34% + 33% = 100%
}
else {
css.append(width.getCss());
}
if (width == Width.FULL) {
css.append("clear: both; ");
} else if (alignment == Positioning.LEFT) {
Expand Down Expand Up @@ -190,8 +196,8 @@ public Collection<TopLevelItem> getItems(ItemGroup<? extends TopLevelItem> itemG
items = jobFilter.filter(items, allItems, null);
}
return items;
}
}

protected void initJobFilters() {
if (jobFilters != null) {
return;
Expand Down
Expand Up @@ -48,45 +48,44 @@

public abstract class SectionedViewSectionDescriptor extends Descriptor<SectionedViewSection> {

protected SectionedViewSectionDescriptor(Class<? extends SectionedViewSection> clazz) {
super(clazz);
}
protected SectionedViewSectionDescriptor(Class<? extends SectionedViewSection> clazz) {
super(clazz);
}

protected SectionedViewSectionDescriptor() {
}
protected SectionedViewSectionDescriptor() {
}

public boolean hasJobFilterExtensions() {
return !ViewJobFilter.all().isEmpty();
}

@Override
public SectionedViewSection newInstance(StaplerRequest req, JSONObject formData) throws FormException {
SectionedViewSection section = (SectionedViewSection)req.bindJSON(getClass().getDeclaringClass(), formData);

if (formData.get("useincluderegex") != null) {
JSONObject merp = formData.getJSONObject("useincluderegex");
section.includeRegex = Util.nullify(merp.getString("includeRegex"));
try {
section.includePattern = Pattern.compile(section.includeRegex);
} catch (PatternSyntaxException e) {
throw new FormException("Regular expression is invalid: " + e.getMessage(), e, "includeRegex");
}
} else {
section.includeRegex = null;
section.includePattern = null;
}

section.jobNames.clear();
@Override
public SectionedViewSection newInstance(StaplerRequest req, JSONObject formData) throws FormException {
SectionedViewSection section = (SectionedViewSection)req.bindJSON(getClass().getDeclaringClass(), formData);

if (formData.get("useincluderegex") != null) {
JSONObject merp = formData.getJSONObject("useincluderegex");
try {
section.setIncludeRegex(Util.nullify(merp.getString("includeRegex")));
} catch (PatternSyntaxException e) {
throw new FormException("Regular expression is invalid: " + e.getMessage(), e, "includeRegex");
}
} else {
section.includeRegex = null;
section.includePattern = null;
}

section.jobNames.clear();
ItemGroup<?> group = req.findAncestorObject(ItemGroup.class);
if (group == null) {
group = Jenkins.getInstance();
}

for (TopLevelItem item : Items.getAllItems(group, TopLevelItem.class)) {
String escapedName = item.getRelativeNameFrom(group).replaceAll("\\.", "_");
if (formData.containsKey(escapedName) && formData.getBoolean(escapedName))
section.jobNames.add(item.getRelativeNameFrom(group));
}
String escapedName = item.getRelativeNameFrom(group).replaceAll("\\.", "_");
if (formData.containsKey(escapedName) && formData.getBoolean(escapedName))
section.jobNames.add(item.getRelativeNameFrom(group));
}

if (section.jobFilters == null) {
section.jobFilters = new DescribableList<ViewJobFilter,Descriptor<ViewJobFilter>>(Saveable.NOOP);
Expand All @@ -96,9 +95,9 @@ public SectionedViewSection newInstance(StaplerRequest req, JSONObject formData)
} catch (IOException e) {
throw new FormException("Error rebuilding list of view job filters.", e, "jobFilters");
}
return section;
}

return section;
}

/**
* Checks if the include regular expression is valid.
Expand Down
60 changes: 60 additions & 0 deletions src/main/resources/hudson/model/View/index.jelly
@@ -0,0 +1,60 @@
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<!--
Overriding https://github.com/jenkinsci/jenkins/blob/master/core/src/main/resources/hudson/model/View/index.jelly.
The changes are kept minimal for easier updates. https://issues.jenkins-ci.org/browse/JENKINS-37174
-->
<?jelly escape-by-default='true'?>
<st:compress xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:layout title="${it.class.name=='hudson.model.AllView' ? '%Dashboard' : it.viewName}${not empty it.ownerItemGroup.fullDisplayName?' ['+it.ownerItemGroup.fullDisplayName+']':''}" norefresh="${!it.automaticRefreshEnabled}">
<j:set var="view" value="${it}"/> <!-- expose view to the scripts we include from owner -->
<st:include page="sidepanel.jelly" />
<l:main-panel>
<st:include page="view-index-top.jelly" it="${it.owner}" optional="true">
<!-- allow the owner to take over the top section, but we also need the default to be backward compatible -->
<div id="view-message">
<div id="systemmessage">
<j:out value="${app.systemMessage!=null ? app.markupFormatter.translate(app.systemMessage) : ''}" />
</div>
<t:editableDescription permission="${it.CONFIGURE}"/>
</div>
</st:include>

<j:if test="${view.class.name!='hudson.plugins.sectioned_view.SectionedView'}">
<!-- Do not call this as it causes all sections to filter items for no reason. -->
<j:set var="items" value="${it.items}"/>
</j:if>
<st:include page="main.jelly" />
</l:main-panel>
<l:header>
<!-- for screen resolution detection -->
<l:yui module="cookie" />
<script>
YAHOO.util.Cookie.set("screenResolution", screen.width+"x"+screen.height);
</script>
</l:header>
</l:layout>
</st:compress>
60 changes: 60 additions & 0 deletions src/test/java/hudson/plugins/sectioned_view/SectionedViewTest.java
@@ -0,0 +1,60 @@
package hudson.plugins.sectioned_view;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.util.DescribableList;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class SectionedViewTest {

public @Rule JenkinsRule j = new JenkinsRule();

@Test @Issue("JENKINS-37174")
public void doNotEnumerateItemsRepeatedly() throws Exception {
j.createFreeStyleProject("show");
j.createFreeStyleProject("hide");

SectionedView sw = new SectionedView("sw");
j.jenkins.addView(sw);
DescribableList<SectionedViewSection, Descriptor<SectionedViewSection>> sections = (DescribableList<SectionedViewSection, Descriptor<SectionedViewSection>>) sw.getSections();

JobGraphsSection jgs = spy(new JobGraphsSection("jgs", SectionedViewSection.Width.THIRD, SectionedViewSection.Positioning.LEFT));
ListViewSection lvs = spy(new ListViewSection("lvs", SectionedViewSection.Width.THIRD, SectionedViewSection.Positioning.CENTER));
TestResultViewSection trvs = spy(new TestResultViewSection("trvs", SectionedViewSection.Width.THIRD, SectionedViewSection.Positioning.RIGHT));
sections.add(jgs);
sections.add(lvs);
sections.add(trvs);
for (SectionedViewSection section : sections) {
section.setIncludeRegex(".*show.*");
}

JenkinsRule.WebClient wc = j.createWebClient();
String content = wc.getPage(j.jenkins).getWebResponse().getContentAsString();
assertThat(content, containsString("show"));
assertThat(content, containsString("hide"));

content = wc.getPage(sw).getWebResponse().getContentAsString();

assertThat(content, containsString("show"));
assertThat(content, not(containsString("hide")));
assertThat(content, containsString("jgs"));
assertThat(content, containsString("lvs"));
assertThat(content, containsString("trvs"));

verify(jgs, times(1)).getItems(any(ItemGroup.class));
verify(lvs, times(1)).getItems(any(ItemGroup.class));
verify(trvs, times(1)).getItems(any(ItemGroup.class));
}
}

0 comments on commit e7ddff1

Please sign in to comment.