Skip to content

Commit

Permalink
Merge pull request #17 from olivergondza/JENKINS-37174
Browse files Browse the repository at this point in the history
[JENKINS-37174] Eliminate some of the duplicate item crunching
  • Loading branch information
olivergondza committed May 29, 2017
2 parents 1e3dec5 + e7ddff1 commit c58b77a
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 92 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
Expand Up @@ -181,8 +181,7 @@ public Item doCreateItem(StaplerRequest req, StaplerResponse rsp)

@Override
public Collection<TopLevelItem> getItems() {
SortedSet<String> names = new TreeSet<String>();
List<TopLevelItem> items = new ArrayList<TopLevelItem>(names.size());
List<TopLevelItem> items = new ArrayList<TopLevelItem>();

for (SectionedViewSection section : sections) {
items.addAll(section.getItems(getOwnerItemGroup()));
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 @@ -191,8 +197,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>
Expand Up @@ -25,10 +25,11 @@ THE SOFTWARE.
<j:if test="${section.name.length() > 0}">
<h2>${section.name}</h2>
</j:if>
<j:if test="${empty(section.getItems(it.ownerItemGroup))}">
<j:set var="sectionItems" value="${section.getItems(it.ownerItemGroup)}"/>
<j:if test="${empty(sectionItems)}">
<p>No jobs in this section.</p>
</j:if>
<j:forEach var="job" items="${section.getItems(it.ownerItemGroup)}">
<j:forEach var="job" items="${sectionItems}">
<div style="margin: 0px 0px 5px 10px;">
<div style="border-left: 1px #bbb solid; border-top: 1px #bbb solid; border-right: 1px #bbb solid; background-color: #eee; margin: 0; padding: 3px;"><b>${job.displayName}</b> (<a href="${job.shortUrl}">view</a>)</div>
<div style="border: 1px #bbb solid;">
Expand All @@ -41,4 +42,4 @@ THE SOFTWARE.
</div>
</div>
</j:forEach>
</j:jelly>
</j:jelly>
Expand Up @@ -25,7 +25,8 @@ THE SOFTWARE.
<j:if test="${section.name.length() > 0}">
<h2>${section.name}</h2>
</j:if>
<j:if test="${empty(section.getItems(it.ownerItemGroup))}">
<j:set var="sectionItems" value="${section.getItems(it.ownerItemGroup)}"/>
<j:if test="${empty(sectionItems)}">
<p>No jobs in this section.</p>
</j:if>

Expand All @@ -40,11 +41,10 @@ THE SOFTWARE.
<st:nbsp/>
</th>
</tr>
<j:set var="itemGroup" value="${it.ownerItemGroup}"/>
<j:forEach var="job" items="${section.getItems(itemGroup)}">
<j:forEach var="job" items="${sectionItems}">
<j:set var="relativeLinkToJob" value="${h.getRelativeLinkTo(job)}"/>
<t:projectViewRow jobBaseUrl="${relativeLinkToJob.substring(0, relativeLinkToJob.length() - job.shortUrl.length())}"/>
</j:forEach>
</table>
</div>
</j:jelly>
</j:jelly>
Expand Up @@ -25,10 +25,11 @@ THE SOFTWARE.
<j:if test="${section.name.length() > 0}">
<h2>${section.name}</h2>
</j:if>
<j:if test="${empty(section.getItems(it.ownerItemGroup))}">
<j:set var="sectionItems" value="${section.getItems(it.ownerItemGroup)}"/>
<j:if test="${empty(sectionItems)}">
<p>No jobs in this section.</p>
</j:if>
<j:forEach var="job" items="${section.getItems(it.ownerItemGroup)}">
<j:forEach var="job" items="${sectionItems}">
<div style="float: left; margin: 0px 0px 5px 10px;">
<j:set var="jobName" value="${job.displayName}"/>
<h3><a href="${job.shortUrl}">${jobName.length() > 33 ? jobName.substring(0, 15) + "..." + jobName.substring(jobName.length() - 15) : jobName}</a></h3>
Expand All @@ -37,4 +38,4 @@ THE SOFTWARE.
</j:forEach>
</div>
</j:forEach>
</j:jelly>
</j:jelly>

0 comments on commit c58b77a

Please sign in to comment.