Skip to content

Commit

Permalink
[JENKINS-13277] Global Environment Variables in Email notification. (#…
Browse files Browse the repository at this point in the history
…101)

* [JENKINS-13277] Fix 'Global environment variables are not being resolved in Email Notification Recipients list for maven 2/3 projects'

* Add integration tests.
  • Loading branch information
andretadeu authored and olamy committed Aug 16, 2017
1 parent 59e7ab6 commit f6112cb
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/java/hudson/maven/MavenModuleSetBuild.java
Expand Up @@ -34,6 +34,7 @@
import hudson.maven.reporters.MavenAggregatedArtifactRecord;
import hudson.maven.reporters.MavenFingerprinter;
import hudson.maven.reporters.MavenMailer;
import hudson.maven.util.VariableExpander;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Build;
Expand Down Expand Up @@ -1087,7 +1088,8 @@ protected void post2(BuildListener listener) throws Exception {
public void cleanUp(BuildListener listener) throws Exception {
MavenMailer mailer = project.getReporters().get(MavenMailer.class);
if (mailer != null) {
new MailSender(mailer.getAllRecipients(),
final String evaluatedRecipients = new VariableExpander(getBuild(), listener).expand(mailer.getAllRecipients());
new MailSender(evaluatedRecipients,
mailer.dontNotifyEveryUnstableBuild,
mailer.sendToIndividuals).execute(MavenModuleSetBuild.this, listener);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/hudson/maven/reporters/MavenMailer.java
Expand Up @@ -29,6 +29,7 @@
import hudson.maven.MavenModule;
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.util.VariableExpander;
import hudson.model.BuildListener;
import hudson.tasks.MailSender;
import java.io.IOException;
Expand Down Expand Up @@ -71,7 +72,8 @@ public boolean end(MavenBuild build, Launcher launcher, BuildListener listener)
if (sendToIndividuals) {
LOGGER.log(Level.FINE, "would also include {0}", build.getCulprits());
}
new MailSender(getAllRecipients(),dontNotifyEveryUnstableBuild,sendToIndividuals).execute(build,listener);
final String evaluatedRecipients = new VariableExpander(build, listener).expand(getAllRecipients());
new MailSender(evaluatedRecipients,dontNotifyEveryUnstableBuild,sendToIndividuals).execute(build,listener);
}
return true;
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/hudson/maven/util/VariableExpander.java
@@ -0,0 +1,37 @@
package hudson.maven.util;

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;

import java.io.IOException;
import java.util.logging.Logger;

public final class VariableExpander {

private static final Logger LOGGER = Logger.getLogger(VariableExpander.class.getName());

private final AbstractBuild<?, ?> build;
private final TaskListener listener;

public VariableExpander(AbstractBuild<?, ?> build, TaskListener listener) {
if (build == null) {
throw new IllegalArgumentException("'build' cannot be null.");
}
if (listener == null) {
throw new IllegalArgumentException("'listener' cannot be null.");
}
this.build = build;
this.listener = listener;
}

public String expand(final String rawString) {
try {
return build.getEnvironment(listener).expand(rawString);
} catch (IOException e) {
LOGGER.fine("Cannot expand the variables in email recipients.");
} catch (InterruptedException e) {
LOGGER.fine("Cannot expand the variables in email recipients.");
}
return rawString;
}
}
72 changes: 71 additions & 1 deletion src/test/java/hudson/maven/reporters/MavenMailerTest.java
Expand Up @@ -25,16 +25,22 @@

import static org.junit.Assert.assertEquals;

import hudson.EnvVars;
import hudson.maven.MavenJenkinsRule;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Result;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.tasks.Mailer;
import hudson.tasks.Mailer.DescriptorImpl;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;

import hudson.util.DescribableList;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;

Expand All @@ -46,6 +52,8 @@
import org.jvnet.hudson.test.ToolInstallations;
import org.jvnet.mock_javamail.Mailbox;

import java.util.List;

/**
*
* @author imod (Dominik Bartholdi)
Expand All @@ -58,6 +66,8 @@ public class MavenMailerTest {
private static final String EMAIL_ADMIN = "\"me <me@sun.com>\"";
private static final String EMAIL_SOME = "some.email@domain.org";
private static final String EMAIL_OTHER = "other.email@domain.org";
private static final String ENV_EMAILS_VARIABLE = "ENV_EMAILS";
private static final String ENV_EMAILS_VALUE = "another.email@domain.org";

@Rule public JenkinsRule j = new MavenJenkinsRule();

Expand Down Expand Up @@ -217,7 +227,67 @@ public void testCiManagementNotificationModule() throws Exception {
assertContainsRecipient(EMAIL_JENKINS_CONFIGURED, message);

}


@Test
public void testEnvironmentVariableMailBeingReplaced() throws Exception {
Jenkins instance = j.getInstance();
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList =
globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);

EnvVars envVars = null;
if (envVarsNodePropertyList == null || envVarsNodePropertyList.isEmpty()) {
EnvironmentVariablesNodeProperty envVarsNodeProperty = new EnvironmentVariablesNodeProperty();
globalNodeProperties.add(envVarsNodeProperty);
envVars = envVarsNodeProperty.getEnvVars();
} else {
envVars = envVarsNodePropertyList.get(0).getEnvVars();
}
envVars.put(ENV_EMAILS_VARIABLE, ENV_EMAILS_VALUE);

JenkinsLocationConfiguration.get().setAdminAddress(EMAIL_ADMIN);
Mailbox someInbox = Mailbox.get(new InternetAddress(EMAIL_SOME));
Mailbox otherInbox = Mailbox.get(new InternetAddress(EMAIL_OTHER));
Mailbox anotherInbox = Mailbox.get(new InternetAddress(ENV_EMAILS_VALUE));
Mailbox jenkinsConfiguredInbox = Mailbox.get(new InternetAddress(EMAIL_JENKINS_CONFIGURED));
someInbox.clear();
otherInbox.clear();
anotherInbox.clear();
jenkinsConfiguredInbox.clear();

ToolInstallations.configureDefaultMaven();
MavenModuleSet mms = j.jenkins.createProject(MavenModuleSet.class, "p");
mms.setGoals("test");
mms.setScm(new ExtractResourceSCM(getClass().getResource("/hudson/maven/JENKINS-1201-module-defined.zip")));

MavenMailer mailer1 = new MavenMailer();
mailer1.recipients = EMAIL_JENKINS_CONFIGURED + " ${" + ENV_EMAILS_VARIABLE + "}";
mailer1.perModuleEmail = true;
mms.getReporters().add(mailer1);

MavenModuleSetBuild mmsb = mms.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, mmsb);

assertEquals(1, someInbox.size());
assertEquals(2, anotherInbox.size());
assertEquals(2, jenkinsConfiguredInbox.size());

Message message = otherInbox.get(0);
assertEquals(3, message.getAllRecipients().length);
assertContainsRecipient(EMAIL_OTHER, message);
assertContainsRecipient(EMAIL_JENKINS_CONFIGURED, message);

message = someInbox.get(0);
assertEquals(3, message.getAllRecipients().length);
assertContainsRecipient(EMAIL_SOME, message);
assertContainsRecipient(EMAIL_JENKINS_CONFIGURED, message);

message = anotherInbox.get(0);
assertEquals(3, message.getAllRecipients().length);
assertContainsRecipient(ENV_EMAILS_VALUE, message);
assertContainsRecipient(EMAIL_JENKINS_CONFIGURED, message);
}

@Test
@Bug(20209)
public void testRecipientsNotNullAndMavenRecipientsNull () {
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/hudson/maven/util/VariableExpanderTest.java
@@ -0,0 +1,76 @@
package hudson.maven.util;

import hudson.maven.MavenJenkinsRule;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.*;
import hudson.tasks.Maven;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.ToolInstallations;

import java.util.*;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class VariableExpanderTest {

@Rule
public JenkinsRule j = new MavenJenkinsRule();

@Parameterized.Parameters
public static Collection<Object[]> data() {
Map<String, String> envVarsMap = new HashMap<>();
envVarsMap.put("ENV_EMAILS", "user@company.com");

return Arrays.asList(new Object[][] {
new Object[]{ null, new HashMap<>(), null },
new Object[]{
"john.doe@nowhere.com",
new HashMap<>(),
"john.doe@nowhere.com"
},
new Object[]{
"Variable JOB_NAME = $JOB_NAME",
envVarsMap,
"Variable JOB_NAME = test"
},
new Object[]{
"Variable $NOT_IN_ENV_VARS = $NOT_IN_ENV_VARS",
envVarsMap,
"Variable $NOT_IN_ENV_VARS = $NOT_IN_ENV_VARS"
}
});
}

private final String rawString;
private final Map<String, String> envVarsMap;
private final String expectedString;

public VariableExpanderTest(
final String rawString,
final Map<String, String> envVarsMap,
final String expectedString) {
this.rawString = rawString;
this.envVarsMap = envVarsMap;
this.expectedString = expectedString;
}

@Test
public void expand() throws Exception {
MavenModuleSet m = j.jenkins.createProject(MavenModuleSet.class, "test");
Maven.MavenInstallation mavenInstallation = ToolInstallations.configureDefaultMaven();
m.setMaven( mavenInstallation.getName() );
m.setScm(new ExtractResourceSCM(getClass().getResource("/hudson/maven/several-modules-in-directory.zip")));
m.setGoals( "clean validate" );

MavenModuleSetBuild mmsb = j.buildAndAssertSuccess(m);
assertEquals(expectedString ,new VariableExpander(mmsb, TaskListener.NULL).expand(rawString));
}

}

0 comments on commit f6112cb

Please sign in to comment.