Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build triggers column #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ work
.project
.settings
*.iml

.idea/
141 changes: 141 additions & 0 deletions src/main/java/jenkins/plugins/extracolumns/BuildTriggersColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package jenkins.plugins.extracolumns;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.scm.NullSCM;
import hudson.scm.SCM;
import hudson.triggers.SCMTrigger;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.views.ListViewColumn;
import hudson.views.ListViewColumnDescriptor;
import jenkins.model.ParameterizedJobMixIn;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.List;
import java.util.Map;

public class BuildTriggersColumn extends ListViewColumn {

private int columnWidth;
private boolean forceWidth;

@DataBoundConstructor
public BuildTriggersColumn(int columnWidth, boolean forceWidth) {
this.columnWidth = columnWidth;
this.forceWidth = forceWidth;
}

public BuildTriggersColumn() {
this(80, false);
}

public String getBuildTriggers(@SuppressWarnings("rawtypes") Job job) {
String upstreamTriggerName = jenkins.triggers.Messages.ReverseBuildTrigger_build_after_other_projects_are_built();

StringBuilder result = new StringBuilder();

List<AbstractProject> upstreamProjects = getUpstreamProjects(job);
if (upstreamProjects != null && !upstreamProjects.isEmpty()) {
result.append(escape(upstreamTriggerName)).append(": ");
append(result, upstreamProjects);
}

Map<TriggerDescriptor, Trigger<?>> triggers = getTriggers(job);
if (triggers != null) {
boolean hasSourceCodeManagement = hasSourceCodeManagement(job);

for (Map.Entry<TriggerDescriptor, Trigger<?>> trigger : triggers.entrySet()) {
if (upstreamTriggerName.equals(trigger.getKey().getDisplayName()))
continue;

if (result.length() > 0)
result.append("<br>");

boolean isDisabled = !hasSourceCodeManagement && trigger.getValue() instanceof SCMTrigger;

if (isDisabled)
result.append("<i>[")
.append(escape(Messages.BuildTriggersColumn_Disabled()))
.append("] ");

result.append(escape(trigger.getKey().getDisplayName()));

String spec = trigger.getValue().getSpec();
if (spec != null && spec.trim().length() > 0)
result.append(": ")
.append(escape(spec));

if (isDisabled)
result.append("</i>");
}
}

return result.toString();
}

private List<AbstractProject> getUpstreamProjects(Job job) {
if (!(job instanceof AbstractProject))
return null;

AbstractProject proj = (AbstractProject) job;

return proj.getUpstreamProjects();
}

private boolean hasSourceCodeManagement(Job job) {
if (!(job instanceof AbstractProject))
return false;

AbstractProject proj = (AbstractProject) job;

SCM sourceCodeManagement = proj.getScm();
return sourceCodeManagement != null && !(sourceCodeManagement instanceof NullSCM);
}

private Map<TriggerDescriptor, Trigger<?>> getTriggers(Job aJob) {
if (!(aJob instanceof ParameterizedJobMixIn.ParameterizedJob))
return null;

ParameterizedJobMixIn.ParameterizedJob job = (ParameterizedJobMixIn.ParameterizedJob) aJob;

return job.getTriggers();
}

private void append(StringBuilder result, List<AbstractProject> upstreamTriggers) {
for (int i = 0; i < upstreamTriggers.size(); i++) {
AbstractProject up = upstreamTriggers.get(i);
if (i > 0)
result.append(", ");
result.append(escape(up.getDisplayName()));
}
}

private String escape(String txt) {
if (txt == null)
txt = "";

return Util.escape(txt.trim()).replace("\n", "<br>");
}

@Extension
public static class DescriptorImpl extends ListViewColumnDescriptor {

@Override
public boolean shownByDefault() {
return false;
}

@Override
public String getDisplayName() {
return Messages.BuildTriggersColumn_DisplayName();
}

@Override
public String getHelpFile() {
return "/plugin/extra-columns/help-buildTriggers-column.html";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
The MIT License

Copyright (c) 2012, Frederic Gurr

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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
<j:set var="widthAttribute" value="width: ${it.getColumnWidth()}px;word-wrap: break-word;white-space:normal;"/>
<td style="${it.isForceWidth() ? widthAttribute : null}">
<j:out value="${it.getBuildTriggers(job)}" />
</td>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
The MIT License

Copyright (c) 2012, Frederic Gurr

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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
<th>
${%BuildTriggersColumn.Header}
</th>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# The MIT License
#
# Copyright (c) 2012, Frederic Gurr
#
# 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.
#

BuildTriggersColumn.Header=Build triggers
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
The MIT License

Copyright (c) 2012, Frederic Gurr

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.
-->

<?jelly escape-by-default='true'?>
<j:jelly 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" xmlns:p="/lib/hudson/project">
<f:block>
<table>
<tr>
<td colspan="3">${%This column shows the build triggers.}</td>
</tr>
<f:optionalBlock field="forceWidth" title="${%Force column width}" inline="true">
<f:entry title="${%Column width}">
<f:textbox field="columnWidth" default="80" />
</f:entry>
</f:optionalBlock>
</table>
</f:block>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The MIT License
#
# Copyright (c) 2012, Frederic Gurr
#
# 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.

Force\ column\ width=Spaltenbreite festlegen
Column\ width=Spaltenbreite
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ SlaveOrLabelColumn.DisplayName=Slave Allocation
UserNameColumn.DisplayName=User Name
CronTriggerColumn.DisplayName=Periodic Build Trigger
CronTriggerColumn.ToolTipFormat=Would have last run at {0}; would next run at {1}.
BuildTriggersColumn.DisplayName=Build Triggers
BuildTriggersColumn.Disabled=Disabled
29 changes: 29 additions & 0 deletions src/main/webapp/help-buildTriggers-column.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
The MIT License

Copyright (c) 2013, Frederic Gurr

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.
-->

<div>
This column shows the build triggers.<br/>
<br/>
To force a specific column width in pixel (px) check the "Force column width" checkbox and set the desired width.<br/>
</div>