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 support for sitemonitor-plugin into dashboard #16

Open
wants to merge 4 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
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>dashboard-view</artifactId>
<version>2.8</version>
<type>jar</type>
<optional>true</optional>
</dependency>
</dependencies>

<repositories>
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/hudson/plugins/sitemonitor/DashboardSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hudson.plugins.sitemonitor;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.TopLevelItem;
import hudson.plugins.sitemonitor.model.Result;
import hudson.plugins.view.dashboard.DashboardPortlet;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class DashboardSupport extends DashboardPortlet {
private String jobName;

public String getJobName() {
return jobName;
}

public String getText() {
StringBuilder result = new StringBuilder();
for (Result r: getResults()) {
result.append(String.format("site=%s, code=%s, status=%s", r.getSite().getUrl(), r.getResponseCode(), r.getStatus()));
}
return result.toString();
}

public List<Result> getResults() {
TopLevelItem item = Jenkins.getInstance().getItem(jobName);
AbstractProject<?, ?> project = (AbstractProject<?, ?>) item;
for (Action action: project.getLastBuild().getActions()) {
if (action instanceof SiteMonitorRootAction) {
return ((SiteMonitorRootAction) action).getResults();
}
}
return new LinkedList<Result>();
}

@DataBoundConstructor
public DashboardSupport(String name, String jobName ) {
super(name);
this.jobName = jobName;
}

public boolean showDescription() {
return new SiteMonitorDescriptor().getShowDescription();
}

public static Collection<String> getAllJobNames() {
return Jenkins.getInstance().getJobNames();
}

@Extension
public static class DescriptorImpl extends Descriptor<DashboardPortlet> {
@Override
public String getDisplayName() {
return Messages.SiteMonitor_DisplayName();
}
}
}
33 changes: 22 additions & 11 deletions src/main/java/hudson/plugins/sitemonitor/SiteMonitorDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
*/
package hudson.plugins.sitemonitor;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.plugins.sitemonitor.mapper.JsonToSiteMapper;
Expand All @@ -41,6 +32,14 @@
import hudson.util.FormValidation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* Handles the global and job configuration management.
Expand Down Expand Up @@ -77,6 +76,11 @@ public class SiteMonitorDescriptor extends BuildStepDescriptor<Publisher> {
*/
private Integer mTimeout;

/**
* Indicates that description is shown in result.
*/
private boolean mShowDescription;

/**
* Constructs {@link SiteMonitorDescriptor}.
*/
Expand Down Expand Up @@ -121,8 +125,7 @@ public final List<Integer> getSuccessResponseCodes() {
* @return the success response codes in comma-separated value format
*/
public final String getSuccessResponseCodesCsv() {

return SuccessCodeListToCvString.INSTANCE.apply(mSuccessResponseCodes);
return SuccessCodeListToCvString.INSTANCE.apply(getSuccessResponseCodes());
}

/**
Expand All @@ -135,6 +138,13 @@ public final Integer getTimeout() {
return mTimeout;
}

/**
* @return status of show description
*/
public final boolean getShowDescription() {
return mShowDescription;
}

/**
* Handles SiteMonitor configuration for each job. Changes to the values are
* repercuted to the view-model once save button is clicked
Expand Down Expand Up @@ -200,6 +210,7 @@ public final boolean configure(final StaplerRequest request,
}

mTimeout = json.getInt("timeout");
mShowDescription = json.getBoolean("showDescription");
save();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public SiteMonitorRootAction(final List<Result> results) {
mResults = results;
}

public boolean showDescription() {
return new SiteMonitorDescriptor().getShowDescription();
}

/**
* @return the text of site monitor link on the left menu on build page.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public Site apply(JSONObject json) {
if (!StringUtils.isBlank(json.getString("timeout")) && NumberUtils.isDigits(json.getString("timeout"))) {
siteBuilder.timeout(((JSONObject) json).getInt("timeout"));
}

siteBuilder.successResponseCodes(JsonToSuccessResponseList.INSTANCE.apply((JSONObject) json));
siteBuilder.description(((JSONObject) json).getString("description"));

return siteBuilder.build();
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/hudson/plugins/sitemonitor/model/Site.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class Site {
*/
private List<Integer> successResponseCodes;

private String description;

/**
* Constructs a Site with specified details.
* @param url
Expand All @@ -64,6 +66,7 @@ private Site(SiteBuilder builder) {
this.mUrl = builder.url;
this.timeout = builder.timeout;
this.successResponseCodes = builder.successResponseCodes;
this.description = builder.description;
}

/**
Expand All @@ -86,6 +89,10 @@ public Integer getTimeout() {
public List<Integer> getSuccessResponseCodes() {
return successResponseCodes;
}

public String getDescription() {
return description;
}

/**
* @return the success response codes in comma-separated value format, used by jelly
Expand All @@ -112,22 +119,27 @@ public static class SiteBuilder {
private Integer timeout;

private List<Integer> successResponseCodes;

private String description;

private SiteBuilder(String url) {
this.url = url;
}

public SiteBuilder timeout(int timeout) {
this.timeout = timeout;

return this;
}

public SiteBuilder successResponseCodes(List<Integer> successResponseCodes) {
this.successResponseCodes = successResponseCodes;

return this;
}

public SiteBuilder description(String value) {
this.description = value;
return this;
}

public Site build() {
return new Site(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<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">
<f:entry title="${%site.text.displayName}" field="name">
<f:textbox name="portlet.name" field="name" default="${descriptor.getDisplayName()}" />
</f:entry>
<j:invokeStatic var="allJobs" className="hudson.plugins.sitemonitor.DashboardSupport" method="getAllJobNames"/>
<f:entry title="${%site.text.jobName}" field="jobName">
<f:editableComboBox field="jobName" items="${allJobs}" />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
site.text.displayName=Display name
site.text.jobName=Job name
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:dp="/hudson/plugins/view/dashboard" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<dp:decorate portlet="${it}"> <!-- This is to say that this is a dashboard view portlet -->
<tr><td>
<div>${descriptor.showDescription}</div>
<div align="left">
<table class="pane sortable" id="files">
<tr>
<td class="pane-header">${%URL}</td>
<j:if test="${it.showDescription()}">
<td class="pane-header">${%Description}</td>
</j:if>
<td class="pane-header">${%Response Code}</td>
<td class="pane-header">${%Status}</td>
</tr>
<tbody>
<j:forEach var="result" items="${it.results}">
<tr>
<td class="pane"><a href="${result.site.url}">${result.site.url}</a></td>
<j:if test="${it.showDescription()}">
<td class="pane">${result.site.description}</td>
</j:if>
<td class="pane">${result.responseCode}</td>
<td class="pane">
<div tooltip="${result.note}">
<img src="${rootURL}/plugin/sitemonitor/images/${result.status.toString().toLowerCase()}.png"/>
</div>
</td>
</tr>
</j:forEach>
</tbody>
</table>
</div>
</td></tr>
</dp:decorate>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<f:entry title="${%Timeout in seconds}" help="/plugin/sitemonitor/timeout_sites.html">
<f:textbox name="timeout" value="${site.timeout}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkTimeout?value='+encode(this.value)"/>
</f:entry>
<f:entry title="${%Description}">
<f:textbox name="description" value="${site.description}"/>
</f:entry>
<f:entry>
<div style="text-align: right;">
<input type="button" value="${%Delete}" class="repeatable-delete show-if-not-only"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<f:textbox name="timeout" value="${descriptor.timeout}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkGlobalTimeout?value='+encode(this.value)"/>
</table>
</f:entry>
<f:entry title="">
<f:checkbox name="showDescription" title="${%Show description in results}" checked="${descriptor.showDescription}"/>
</f:entry>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
<table class="pane sortable" id="files">
<tr>
<td class="pane-header">${%URL}</td>
<j:if test="${it.showDescription()}">
<td class="pane-header">${%Description}</td>
</j:if>
<td class="pane-header">${%Response Code}</td>
<td class="pane-header">${%Status}</td>
</tr>
<tbody>
<j:forEach var="result" items="${it.results}">
<tr>
<td class="pane"><a href="${result.site.url}">${result.site.url}</a></td>
<j:if test="${it.showDescription()}">
<td class="pane">${result.site.description}</td>
</j:if>
<td class="pane">${result.responseCode}</td>
<td class="pane">
<div tooltip="${result.note}">
Expand Down