Skip to content
Permalink
Browse files

Fix JENKINS-18203

Implement support for managed files as templates.
  • Loading branch information...
slide committed Oct 29, 2013
1 parent 6fb9191 commit 9c8e0ac16c440293728a5f7343993857c5f47b00
10 pom.xml
@@ -105,13 +105,13 @@
<version>1.41</version>
<optional>true</optional>
</dependency>
<!--

<dependency>
<groupId>org.jenkins-ci.lib</groupId>
<artifactId>config-provider-model</artifactId>
<version>1.3.4</version>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.6.2</version>
</dependency>
-->

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
@@ -0,0 +1,45 @@
package hudson.plugins.emailext;

import hudson.Extension;
import org.jenkinsci.lib.configprovider.AbstractConfigProviderImpl;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ContentType;
import org.kohsuke.stapler.DataBoundConstructor;


public class JellyTemplateConfig extends Config {

@DataBoundConstructor
public JellyTemplateConfig(String id, String name, String comment, String content) {
super(id, name, comment, content);
}

@Extension
public static final class JellyTemplateConfigProvider extends AbstractConfigProviderImpl {

public JellyTemplateConfigProvider() {
//load();
}

@Override
public ContentType getContentType() {
return ContentType.DefinedType.XML;
}

@Override
public String getDisplayName() {
return "Extended Email Publisher Jelly Template ";
}

@Override
public Config newConfig() {
String id = getProviderId() + System.currentTimeMillis();
return new JellyTemplateConfig(id, "Jelly Email Template", "", "");
}

@Override
protected String getXmlFileName() {
return "email-ext-jelly-config-files.xml";
}
}
}
@@ -1,12 +1,15 @@
package hudson.plugins.emailext.plugins.content;

import hudson.ExtensionList;
import hudson.Plugin;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.JellyTemplateConfig.JellyTemplateConfigProvider;
import hudson.tasks.Mailer;
import java.io.ByteArrayInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
@@ -22,6 +25,8 @@
import java.io.IOException;
import java.io.InputStream;
import jenkins.model.Jenkins;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

@@ -74,11 +79,23 @@ private String generateMissingTemplate(String template) {
*/
private InputStream getTemplateInputStream(String templateName)
throws FileNotFoundException {

InputStream inputStream;
if(templateName.startsWith("managed:")) {
String managedTemplateName = templateName.substring(8);
inputStream = getManagedTemplate(managedTemplateName);
if(inputStream == null) {
throw new FileNotFoundException("Managed template '" + managedTemplateName + "' not found");
}
return inputStream;
}

// add .jelly if needed
if (!templateName.endsWith(".jelly")) {
templateName += ".jelly";
}
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(

inputStream = getClass().getClassLoader().getResourceAsStream(
"hudson/plugins/emailext/templates/" + templateName);

if (inputStream == null) {
@@ -89,6 +106,31 @@ private InputStream getTemplateInputStream(String templateName)

return inputStream;
}

private InputStream getManagedTemplate(String templateName) {
Plugin plugin = Jenkins.getInstance().getPlugin("config-file-provider");
InputStream stream = null;
if(plugin != null) {
Config config = null;
ConfigProvider provider = getTemplateConfigProvider();
for(Config c : provider.getAllConfigs()) {
if(c.name.equalsIgnoreCase(templateName) && provider.isResponsibleFor(c.id)) {
config = c;
break;
}
}

if(config != null) {
stream = new ByteArrayInputStream(config.content.getBytes());
}
}
return stream;
}

private ConfigProvider getTemplateConfigProvider() {
ExtensionList<ConfigProvider> providers = ConfigProvider.all();
return providers.get(JellyTemplateConfigProvider.class);
}

private String renderContent(AbstractBuild<?, ?> build, InputStream inputStream)
throws JellyException, IOException {
@@ -0,0 +1,29 @@
<!--
The MIT License
Copyright (c) 2013, Alex Earl
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.
-->



<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">
${%jelly_template_description}
</j:jelly>
@@ -0,0 +1 @@
jelly_template_description=A Jelly template used by the Extended Email Publisher plugin to generate emails.
@@ -0,0 +1,41 @@
<!--
The MIT License
Copyright (c) 2013, Alex Earl
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.
-->



<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="${%ID}" >
<f:textbox readonly="readonly" name="config.id" value="${config.id}" />
</f:entry>
<f:entry title="${%Name}">
<f:textbox name="config.name" value="${config.name}" />
</f:entry>
<f:entry title="${%Comment}">
<f:textbox name="config.comment" value="${config.comment}" />
</f:entry>
<f:entry title="${%Content}">
<f:textarea id="config.content" name="config.content" value="${config.content}" />
</f:entry>
</j:jelly>

0 comments on commit 9c8e0ac

Please sign in to comment.
You can’t perform that action at this time.