Skip to content

Commit

Permalink
Fixed JENKINS-9594
Browse files Browse the repository at this point in the history
Added action to the project that contains the email-ext as a publisher which allows the user to enter a template name, select a previous build and see the rendered template.
  • Loading branch information
slide committed May 20, 2013
1 parent a16a5bd commit 5957b9f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 7 deletions.
65 changes: 65 additions & 0 deletions src/main/java/hudson/plugins/emailext/EmailExtTemplateAction.java
@@ -0,0 +1,65 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hudson.plugins.emailext;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.content.ScriptContent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.kohsuke.stapler.bind.JavaScriptMethod;

/**
*
* @author acearl
*/
public class EmailExtTemplateAction implements Action {

private AbstractProject<?,?> project;

public EmailExtTemplateAction(AbstractProject<?,?> project) {
this.project = project;
}

public String getIconFileName() {
return "/plugin/email-ext/images/template-debugger.png";
}

public String getDisplayName() {
return Messages.EmailExtTemplateAction_DisplayName();
}

public String getUrlName() {
return "templateTest";
}

private String renderError(Exception ex) {
return "";
}

@JavaScriptMethod
public String renderTemplate(String templateFile, String buildId) {
String result;

try {
ScriptContent content = new ScriptContent();
content.template = templateFile;
AbstractBuild<?,?> build = project.getBuild(buildId);
result = content.evaluate(build, TaskListener.NULL, "SCRIPT");
} catch (Exception ex) {
result = renderError(ex);
}

return result;
}

public AbstractProject<?, ?> getProject() {
return project;
}
}
18 changes: 18 additions & 0 deletions src/main/java/hudson/plugins/emailext/EmailExtensionPlugin.java
Expand Up @@ -23,7 +23,9 @@
*/
package hudson.plugins.emailext;

import hudson.FilePath;
import hudson.Plugin;
import hudson.model.Hudson;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;
import hudson.plugins.emailext.plugins.trigger.AbortedTrigger;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
Expand All @@ -40,8 +42,13 @@
import hudson.plugins.emailext.plugins.trigger.StillUnstableTrigger;
import hudson.plugins.emailext.plugins.trigger.SuccessTrigger;
import hudson.plugins.emailext.plugins.trigger.UnstableTrigger;
import hudson.util.FormValidation;
import java.io.File;
import java.io.FileInputStream;

import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;

/**
* Entry point of a plugin.
Expand Down Expand Up @@ -79,6 +86,17 @@ private void addEmailTriggerPlugin(EmailTriggerDescriptor trigger) {
System.err.println(e.getMessage());
}
}

public FormValidation doTemplateFileCheck(@QueryParameter final String value) {
if(!StringUtils.isEmpty(value)) {
final File scriptsFolder = new File(Hudson.getInstance().getRootDir(), "email-templates");
final File scriptFile = new File(scriptsFolder, value);
if(!scriptFile.exists()) {
return FormValidation.error("The file '" + value + "' does not exist");
}
}
return FormValidation.ok();
}

static {
// Fix JENKINS-9006
Expand Down
Expand Up @@ -249,10 +249,10 @@ public void debug(PrintStream p, String format, Object... args) {
ExtendedEmailPublisher.DESCRIPTOR.debug(p, format, args);
}

// @Override
// public Collection<? extends Action> getProjectActions(AbstractProject<?,?> project) {
// return Collections.singletonList(new EmailExtTemplateAction(project));
// }
@Override
public Collection<? extends Action> getProjectActions(AbstractProject<?,?> project) {
return Collections.singletonList(new EmailExtTemplateAction(project));
}

@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
Expand Down
@@ -1,14 +1,12 @@
package hudson.plugins.emailext;

import hudson.Plugin;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.VersionNumber;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -438,7 +436,7 @@ public FormValidation doAddressCheck(@QueryParameter final String value)
return FormValidation.error(e.getMessage());
}
}

public FormValidation doRecipientListRecipientsCheck(@QueryParameter final String value)
throws IOException, ServletException {
return new EmailRecipientUtils().validateFormRecipientList(value);
Expand Down
@@ -0,0 +1,44 @@
<?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">
<l:layout xmlns:plugin="/hudson/plugins/javancss/tags">
<script type="text/javascript">
function onSubmit() {
var templateTester = <st:bind value="${it}"/>
var templateFile = document.getElementById('template_file_name').value
var buildId = document.getElementById('template_build').value
templateTester.renderTemplate(templateFile,buildId, function(t) {
document.getElementById('rendered_template').innerHTML = t.responseObject();
});
return false;
}
</script>
<st:include it="${it.project}" page="sidepanel.jelly"/>
<l:main-panel>
<h1>${it.displayName}</h1>
<p>
${%description}
</p>
<form action="" method="post" name="templateTest" onSubmit="return onSubmit();">
<table>
<f:entry title="${%Groovy Template File Name}">
<f:textbox name="template_file_name" id="template_file_name" clazz="required" checkUrl="'${rootURL}/plugin/email-ext/templateFileCheck?value='+this.value" />
</f:entry>
<f:entry title="${%Build To Test}">
<select name="template_build" id="template_build">
<j:forEach var="build" items="${it.project.builds}">
<option value="${build.id}">#${build.number} (${build.result})</option>
</j:forEach>
</select>
</f:entry>
<f:entry>
<f:submit value="${%Go}!"/>
</f:entry>
</table>
</form>
<div id="rendered_template">

</div>
</l:main-panel>
</l:layout>
</j:jelly>
@@ -0,0 +1,2 @@
description=Enter the template file name and select a build to test with then\
press the "Go!" button to render the template based on the selected build.
Expand Up @@ -4,3 +4,5 @@ ExtendedEmailPublisherDescriptor.AdminAddress=address not configured yet <nobody
MatrixTriggerMode.OnlyParent=Trigger only the parent job
MatrixTriggerMode.OnlyConfigurations=Trigger for each configuration
MatrixTriggerMode.Both=Trigger for parent and each configuration

EmailExtTemplateAction.DisplayName=Email Template Testing
Binary file added src/main/webapp/images/template-debugger.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5957b9f

Please sign in to comment.