Skip to content

Commit

Permalink
Add support for config changed param
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-witt committed Nov 15, 2023
1 parent b1aa6e9 commit a652ade
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<dd-trace-java.version>0.71.0</dd-trace-java.version>
<!-- byte-buddy is needed until we bump to java 11: https://stackoverflow.com/questions/69929229/mockito-shipped-within-spring-boot-fails-to-attach-to-jvm-openjdk-11/72912290#72912290 -->
<byte-buddy.version>1.12.14</byte-buddy.version>
<configuration-as-code.version>1462.v069a_b_57ff5c8</configuration-as-code.version>
<useBeta>true</useBeta>
</properties>

Expand Down Expand Up @@ -87,6 +88,19 @@
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private static final String GLOBAL_JOB_TAGS_PROPERTY = "DATADOG_JENKINS_PLUGIN_GLOBAL_JOB_TAGS";
private static final String EMIT_SECURITY_EVENTS_PROPERTY = "DATADOG_JENKINS_PLUGIN_EMIT_SECURITY_EVENTS";
private static final String EMIT_SYSTEM_EVENTS_PROPERTY = "DATADOG_JENKINS_PLUGIN_EMIT_SYSTEM_EVENTS";
private static final String EMIT_CONFIG_CHANGE_EVENTS_PROPERTY = "DATADOG_JENKINS_PLUGIN_EMIT_CONFIG_CHANGE_EVENTS";
private static final String INCLUDE_EVENTS_PROPERTY = "DATADOG_JENKINS_PLUGIN_INCLUDE_EVENTS";
private static final String EXCLUDE_EVENTS_PROPERTY = "DATADOG_JENKINS_PLUGIN_EXCLUDE_EVENTS";
private static final String COLLECT_BUILD_LOGS_PROPERTY = "DATADOG_JENKINS_PLUGIN_COLLECT_BUILD_LOGS";
Expand All @@ -141,6 +142,7 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private static final Integer DEFAULT_TARGET_LOG_COLLECTION_PORT_VALUE = null;
private static final boolean DEFAULT_EMIT_SECURITY_EVENTS_VALUE = true;
private static final boolean DEFAULT_EMIT_SYSTEM_EVENTS_VALUE = true;
private static final boolean DEFAULT_EMIT_CONFIG_CHANGE_EVENTS_VALUE = false;
private static final boolean DEFAULT_COLLECT_BUILD_LOGS_VALUE = false;
private static final boolean DEFAULT_COLLECT_BUILD_TRACES_VALUE = false;
private static final boolean DEFAULT_RETRY_LOGS_VALUE = true;
Expand Down Expand Up @@ -170,6 +172,7 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private String excludeEvents = null;
private boolean emitSecurityEvents = DEFAULT_EMIT_SECURITY_EVENTS_VALUE;
private boolean emitSystemEvents = DEFAULT_EMIT_SYSTEM_EVENTS_VALUE;
private boolean emitConfigChangeEvents = DEFAULT_EMIT_CONFIG_CHANGE_EVENTS_VALUE;
private boolean collectBuildLogs = DEFAULT_COLLECT_BUILD_LOGS_VALUE;
private boolean collectBuildTraces = DEFAULT_COLLECT_BUILD_TRACES_VALUE;
private boolean retryLogs = DEFAULT_RETRY_LOGS_VALUE;
Expand Down Expand Up @@ -266,6 +269,11 @@ public void loadEnvVariables() {
this.globalTagFile = globalTagFileEnvVar;
}

String emitConfigChangeEventsEnvVar = System.getenv(EMIT_CONFIG_CHANGE_EVENTS_PROPERTY);
if(StringUtils.isNotBlank(emitConfigChangeEventsEnvVar)){
this.emitConfigChangeEvents = Boolean.valueOf(emitConfigChangeEventsEnvVar);
}

String globalTagsEnvVar = System.getenv(GLOBAL_TAGS_PROPERTY);
if(StringUtils.isNotBlank(globalTagsEnvVar)){
this.globalTags = globalTagsEnvVar;
Expand Down Expand Up @@ -1203,6 +1211,23 @@ public void setIncluded(final String jobs) {
this.whitelist = jobs;
}

/**
* @return - A {@link Boolean} indicating if the user has configured Datadog to emit Config Change events.
*/
public boolean isEmitConfigChangeEvents() {
return emitConfigChangeEvents;
}

/**
* Used for CasC
* accepting a comma-separated string of events.
*
* @param emitConfigChangeEvents - The checkbox status (checked/unchecked)
*/
@DataBoundSetter
public void setEmitConfigChangeEvents(boolean emitConfigChangeEvents) {
this.emitConfigChangeEvents = emitConfigChangeEvents;
}
/**
* Gets the globalTagFile set in the job configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import org.jvnet.hudson.test.JenkinsRule;

import hudson.util.Secret;
Expand All @@ -14,13 +16,26 @@
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;

import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;

import java.io.IOException;

public class DatadogGlobalConfigurationTest {

@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@Rule public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("test-config.yml")
public void TestConfigurationAsCodeCompatibility() throws Exception {
DatadogGlobalConfiguration cfg = DatadogUtilities.getDatadogGlobalDescriptor();
Assert.assertTrue(cfg.isEmitConfigChangeEvents());
}

@Test
public void testCanGetCredentialFromId() throws IOException {
CredentialsStore credentialsStore = CredentialsProvider.lookupStores(jenkinsRule).iterator().next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jenkins:
systemMessage: "Example of configuring datadog in Jenkins"

unclassified:
datadogGlobalConfiguration:
reportWith: 'http'
targetApiKey: 'test'
emitConfigChangeEvents: true

0 comments on commit a652ade

Please sign in to comment.