Skip to content

Commit

Permalink
Fix JENKINS-28145
Browse files Browse the repository at this point in the history
Added resolution of variables to the classpath expansion.
  • Loading branch information
slide committed May 2, 2015
1 parent 412bea3 commit 1a52cc2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java
Expand Up @@ -453,7 +453,7 @@ private boolean executePresendScript(ExtendedEmailPublisherContext context, Mime
"hudson",
"hudson.model"));

cl = expandClassLoader(cl, cc);
expandClasspath(context, cc);
if (getDescriptor().isSecurityEnabled()) {
debug(context.getListener().getLogger(), "Setting up sandbox for pre-send script");
cc.addCompilationCustomizers(new SandboxTransformer());
Expand Down Expand Up @@ -501,23 +501,22 @@ private boolean executePresendScript(ExtendedEmailPublisherContext context, Mime
* @param cc
* @return the new expanded classloader
*/
private ClassLoader expandClassLoader(ClassLoader cl, CompilerConfiguration cc) {
private void expandClasspath(ExtendedEmailPublisherContext context, CompilerConfiguration cc) {
List<String> classpathList = new ArrayList<String>();

if ((classpath != null) && classpath.size() > 0) {
cl = new GroovyClassLoader(cl, cc);
for (GroovyScriptPath path : classpath) {
((GroovyClassLoader) cl).addURL(path.asURL());
classpathList.add(ContentBuilder.transformText(path.getPath(), context, getRuntimeMacros(context)));
}
}

List<GroovyScriptPath> globalClasspath = getDescriptor().getDefaultClasspath();
if ((globalClasspath != null) && (globalClasspath.size() > 0)) {
if (!(cl instanceof GroovyClassLoader)) {
cl = new GroovyClassLoader(cl, cc);
}
for (GroovyScriptPath path : globalClasspath) {
((GroovyClassLoader) cl).addURL(path.asURL());
classpathList.add(ContentBuilder.transformText(path.getPath(), context, getRuntimeMacros(context)));
}
}
return cl;
}
cc.setClasspathList(classpathList);
}

private MimeMessage createMail(ExtendedEmailPublisherContext context) throws MessagingException, IOException, InterruptedException {
Expand Down
Expand Up @@ -60,6 +60,7 @@
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockBuilder;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.recipes.WithPlugin;
import org.jvnet.mock_javamail.Mailbox;
import org.kohsuke.stapler.Stapler;
Expand Down Expand Up @@ -709,6 +710,54 @@ public void testPresendScriptModifiesToUsingGlobalExternalScript() throws Except
assertEquals(1, Mailbox.get("slide.o.mix@xxx.com").size());
}

@Test
@Issue("JENKINS-28145")
public void testPresendScriptExternalScriptWithVariablePath() throws Exception {
publisher.presendScript = "import javax.mail.Message.RecipientType\n"
+ "import ExtendedEmailPublisherTestHelper\n"
+ "msg.setRecipients(RecipientType.TO, ExtendedEmailPublisherTestHelper.to())";
Field f = ExtendedEmailPublisherDescriptor.class.getDeclaredField("defaultClasspath");
f.setAccessible(true);
List<GroovyScriptPath> classpath = new ArrayList<GroovyScriptPath>();
classpath.add(new GroovyScriptPath("${WORKSPACE}"));
f.set(publisher.getDescriptor(), classpath);
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("ExtendedEmailPublisherTestHelper.groovy").write(
"class ExtendedEmailPublisherTestHelper {\n" +
" static def to() {\n" +
" \"slide.o.mix@xxx.com\"\n" +
" }\n" +
"}", "UTF-8");
return true;
}
});

SuccessTrigger successTrigger = new SuccessTrigger(recProviders, "$DEFAULT_RECIPIENTS",
"$DEFAULT_REPLYTO", "$DEFAULT_SUBJECT", "$DEFAULT_CONTENT", "", 0, "project");
successTrigger.setEmail(new EmailType() {
{
addRecipientProvider(new RequesterRecipientProvider());
}
});
publisher.getConfiguredTriggers().add(successTrigger);

User u = User.get("kutzi");
u.setFullName("Christoph Kutzinski");
Mailer.UserProperty prop = new Mailer.UserProperty("kutzi@xxx.com");
u.addProperty(prop);

UserCause cause = new MockUserCause("kutzi");

FreeStyleBuild build = project.scheduleBuild2(0, cause).get();
j.assertBuildStatusSuccess(build);

assertEquals(0, Mailbox.get("kutzi@xxx.com").size());
assertEquals(1, Mailbox.get("slide.o.mix@xxx.com").size());
}


@Test
public void testPresendScriptNoSecurity() throws Exception {
Field f = ExtendedEmailPublisherDescriptor.class.getDeclaredField("enableSecurity");
Expand Down

0 comments on commit 1a52cc2

Please sign in to comment.