Skip to content

Commit

Permalink
Merge pull request #198 from mikecirioli/JENKINS-71789
Browse files Browse the repository at this point in the history
[JENKINS-71789] Enable ${variable} support for email and cc fields
  • Loading branch information
PierreBtz authored Aug 14, 2023
2 parents 8835ed6 + 70df3e1 commit 58972d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.RootElementConfigurator;
import io.jenkins.plugins.casc.SecretSourceResolver;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import io.jenkins.plugins.casc.model.Scalar;
Expand Down Expand Up @@ -56,7 +57,10 @@ public String getDisplayName() {
protected AdvisorGlobalConfiguration instance(Mapping mapping, ConfigurationContext configurationContext)
throws ConfiguratorException {
// Scalar values
final String email = (mapping.get(EMAIL_ATTR) != null ? mapping.getScalarValue(EMAIL_ATTR) : StringUtils.EMPTY);
final String email = configurationContext
.getSecretSourceResolver()
.resolve((mapping.get(EMAIL_ATTR) != null ? mapping.getScalarValue(EMAIL_ATTR) : StringUtils.EMPTY));

final boolean nagDisabled = (mapping.get(NAG_DISABLED_ATTR) != null
&& BooleanUtils.toBoolean(mapping.getScalarValue(NAG_DISABLED_ATTR)));
final boolean acceptToS = (mapping.get(ACCEPT_TOS_ATTR) != null
Expand All @@ -69,7 +73,9 @@ protected AdvisorGlobalConfiguration instance(Mapping mapping, ConfigurationCont
if (ccCN instanceof Sequence) {
Sequence s = (Sequence) ccCN;
for (CNode cNode : s) {
cc.add(new Recipient(cNode.asScalar().getValue()));
cc.add(new Recipient(configurationContext
.getSecretSourceResolver()
.resolve(cNode.asScalar().getValue())));
}
// We don't want to process it anymore because the mapping in YAML
// doesn't map the objects model (List<String> vs List<Recipient>
Expand Down Expand Up @@ -151,13 +157,15 @@ public CNode describe(AdvisorGlobalConfiguration instance, ConfigurationContext
mapping.put(ACCEPT_TOS_ATTR, attribute.describe(instance, context));
break;
case EMAIL_ATTR:
mapping.put(EMAIL_ATTR, attribute.describe(instance, context));
mapping.put(EMAIL_ATTR, String.valueOf(attribute.describe(instance, context)));
break;
case CCS_ATTR:
// We build it manually because we don't want to expose the Bean model
SecretSourceResolver r = context.getSecretSourceResolver();
Sequence ccs = new Sequence();
instance.getCcs().stream()
.map(Recipient::getEmail)
.map(r::encode)
.map(Scalar::new)
.forEach(ccs::add);
if (!ccs.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -32,6 +33,7 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.rules.ExpectedException;
import org.jvnet.hudson.test.JenkinsRule;

Expand All @@ -50,6 +52,9 @@ public class AdvisorRootConfiguratorTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Rule
public final EnvironmentVariables environment = new EnvironmentVariables();

private AdvisorRootConfigurator configurator;
private AdvisorGlobalConfiguration configuration;
private Mapping mapping;
Expand Down Expand Up @@ -159,6 +164,31 @@ public void testDescribeWithBlankEmail() throws Exception {
assertTrue(described.isEmpty());
}

@Test
public void testDescribeWithVarialbeValue() throws Exception {
List<Recipient> cc_with_var = Arrays.asList(new Recipient("${admin_cc}"), new Recipient("${admin_cc}"));
final AdvisorGlobalConfiguration c = new AdvisorGlobalConfiguration("${admin_email}", cc_with_var, EXCLUDED);
c.setAcceptToS(ACCEPT_TOS);
c.setNagDisabled(NAG_DISABLED);

Mapping described = configurator.describe(c, context).asMapping();
assertNotNull(described);
String email = described.getScalarValue(EMAIL_ATTR);

assertEquals("^${admin_email}", email);
assertTrue(
"encoded email cc not found in list",
toListValues(described.get(CCS_ATTR).asSequence()).stream().anyMatch(cc -> cc.equals("^${admin_cc}")));
}

@Test
public void testResolveWithVariableName() throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
context = new ConfigurationContext(registry);
environment.set("admin_email", "mike@is.cool.com");
assertThat(context.getSecretSourceResolver().resolve("${admin_email}"), equalTo("mike@is.cool.com"));
}

@Test
public void testDescribeWithEmptyCC() throws Exception {
final AdvisorGlobalConfiguration c =
Expand Down

0 comments on commit 58972d1

Please sign in to comment.