Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-13277] Global Environment Variables in Email notification. (#…
…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
1 parent
59e7ab6
commit f6112cb
Showing
5 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |