Skip to content

Commit

Permalink
Perform auto-injection when Command task is used to run Gradle (#58)
Browse files Browse the repository at this point in the history
* Support running Gradle via the Command Builder

* Test Gradle project run via Command builder

* Add new project to Bamboo 6.9.2 resources

* Do not attempt to delete agent if API is not supported

* Test environment variable setting logic
  • Loading branch information
c00ler authored Aug 2, 2023
1 parent 8228a20 commit 7a28b56
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 43 deletions.
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
<properties>
<bamboo.version>6.9.2</bamboo.version>
<bamboo.data.version>${bamboo.version}</bamboo.data.version>
<bamboo.product.data.path>${basedir}/src/test/resources/generated-test-resources-692-license-723.zip</bamboo.product.data.path>
<bamboo.product.data.path>
${basedir}/src/test/resources/generated-test-resources-692-license-723.zip
</bamboo.product.data.path>
<amps.version>8.11.0</amps.version>
<atlassian.spring.scanner.version>2.2.4</atlassian.spring.scanner.version>
<!-- This property ensures consistency between the key in atlassian-plugin.xml and the OSGi bundle's key. -->
Expand Down Expand Up @@ -124,6 +126,7 @@
<version>2.8.9</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -397,9 +400,19 @@

<profiles>
<profile>
<id>bamboo93OrLater</id>
<id>bamboo924OrLater</id>
<properties>
<bamboo.product.data.path>${basedir}/src/test/resources/generated-test-resources-923.zip</bamboo.product.data.path>
<bamboo.product.data.path>
${basedir}/src/test/resources/generated-test-resources-923.zip
</bamboo.product.data.path>
</properties>
</profile>
<profile>
<id>bamboo93OrLater</id> <!-- same as bamboo924OrLater, but requires Java 11 -->
<properties>
<bamboo.product.data.path>
${basedir}/src/test/resources/generated-test-resources-923.zip
</bamboo.product.data.path>
<required.java.version>[11,12)</required.java.version>
</properties>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public void apply(RuntimeTaskDefinition task, String name, String value) {

String currentEnvironment = StringUtils.trimToNull(configuration.get(environmentVariablesKey));
String additionalEnvironmentVariable = String.format("%s=%s", name, EnvironmentVariables.quoteIfNeeded(value));
String updatedEnvironment = StringUtils.join(new String[]{currentEnvironment, additionalEnvironmentVariable}, ' ');

String updatedEnvironment =
(currentEnvironment == null)
? additionalEnvironmentVariable
: StringUtils.join(new String[]{currentEnvironment, additionalEnvironmentVariable}, ' ');

configuration.put(environmentVariablesKey, updatedEnvironment);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/gradle/enterprise/bamboo/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public final class Constants {

public static final String SPACE = " ";

public static final String DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY = "environmentVariables";

private Constants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
@Component
public class DefaultEnvironmentVariableSetter extends AbstractEnvironmentVariableSetter {

private static final String ENVIRONMENT_VARIABLES_KEY = "environmentVariables";

public DefaultEnvironmentVariableSetter() {
super(ENVIRONMENT_VARIABLES_KEY);
super(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY);
}

@Override
Expand All @@ -19,6 +17,7 @@ public boolean applies(RuntimeTaskDefinition task) {

@Override
public int getOrder() {
// needs to be the last one
return LOWEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.atlassian.bamboo.process.EnvironmentVariableAccessor;
import com.atlassian.bamboo.task.runtime.RuntimeTaskDefinition;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.google.common.annotations.VisibleForTesting;
import com.gradle.enterprise.bamboo.utils.EnvironmentVariables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -18,9 +17,6 @@
@Component
public class DefaultMavenOptsSetter implements GradleEnterpriseMavenOptsSetter {

@VisibleForTesting
static final String ENVIRONMENT_VARIABLES_KEY = "environmentVariables";

private static final String MAVEN_OPTS = "MAVEN_OPTS";

private final EnvironmentVariableAccessor environmentVariableAccessor;
Expand All @@ -45,7 +41,7 @@ public int getOrder() {
public void apply(RuntimeTaskDefinition task, List<SystemProperty> systemProperties) {
Map<String, String> configuration = task.getConfiguration();

String currentEnvironment = configuration.get(ENVIRONMENT_VARIABLES_KEY);
String currentEnvironment = configuration.get(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY);

Map<String, String> environment =
new HashMap<>(environmentVariableAccessor.splitEnvironmentAssignments(currentEnvironment));
Expand All @@ -58,7 +54,7 @@ public void apply(RuntimeTaskDefinition task, List<SystemProperty> systemPropert
String updatedEnvironment =
environmentVariableAccessor.joinEnvironmentVariables(quoteValuesIfNeeded(environment));

configuration.put(ENVIRONMENT_VARIABLES_KEY, updatedEnvironment);
configuration.put(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY, updatedEnvironment);
}

private Map<String, String> quoteValuesIfNeeded(Map<String, String> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ public class GradleBuildScanInjector extends AbstractBuildScanInjector<GradleCon
private static final String HOME = "HOME";

// Commercial plugin https://bobswift.atlassian.net/wiki/spaces/BGTP/overview
private static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_KEY = "org.swift.bamboo.groovy:gradle";
private static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_WRAPPER_KEY = "org.swift.bamboo.groovy:gradlewrapper";
private static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLEW_KEY = "org.swift.bamboo.groovy:gradlew";
public static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_KEY = "org.swift.bamboo.groovy:gradle";
public static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_WRAPPER_KEY = "org.swift.bamboo.groovy:gradlewrapper";
public static final String BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLEW_KEY = "org.swift.bamboo.groovy:gradlew";

public static final String SCRIPT_PLUGIN_KEY = "com.atlassian.bamboo.plugins.scripttask:task.builder.script";
public static final String COMMAND_PLUGIN_KEY = "com.atlassian.bamboo.plugins.scripttask:task.builder.command";

// Artifactory gradle builder: https://github.com/jfrog/bamboo-artifactory-plugin/blob/master/src/main/java/org/jfrog/bamboo/task/ArtifactoryGradleTask.java
public static final String ARTIFACTORY_GRADLE_TASK_KEY_SUFFIX = "artifactoryGradleTask";

private static final Set<Predicate<String>> GRADLE_BUILDERS =
ImmutableSet.of(
eq(SCRIPT_PLUGIN_KEY),
eq(COMMAND_PLUGIN_KEY),
eq(BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_KEY),
eq(BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_WRAPPER_KEY),
eq(BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLEW_KEY),
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/gradle/enterprise/bamboo/BambooApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class BambooApi implements AutoCloseable {

private static final String BAMBOO_AGENT_URL_PATTERN = "https://packages.atlassian.com/repository/public/com/atlassian/bamboo/bamboo-agent/%1$s/bamboo-agent-%1$s.jar";

private static final Version BAMBOO_6_10_2 = new Version(6, 10, 2);

private final String bambooUrl;
private final CloseableHttpClient client;
private final Supplier<HttpClientContext> authContext;
Expand Down Expand Up @@ -185,6 +187,10 @@ public Collection<Agent> getAgents() {
}
}

public boolean deleteAgentSupported() {
return getBambooVersion().isGreaterOrEqualTo(BAMBOO_6_10_2);
}

public void deleteAgent(long agentId) {
HttpDelete request = new HttpDelete(String.format("%s/rest/api/latest/agent/%d", bambooUrl, agentId));
try (CloseableHttpResponse response = client.execute(request, authContext.get())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.gradle.enterprise.bamboo;

import com.atlassian.bamboo.task.TaskDefinition;
import com.atlassian.bamboo.task.TaskDefinitionImpl;
import com.atlassian.bamboo.task.runtime.RuntimeTaskDefinition;
import com.atlassian.bamboo.task.runtime.RuntimeTaskDefinitionImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.core.Ordered;

import java.util.Collections;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;

class DefaultEnvironmentVariableSetterTest {

private final EnvironmentVariableSetter environmentVariableSetter = new DefaultEnvironmentVariableSetter();

@Test
void orderIsSet() {
// expect
assertThat(environmentVariableSetter.getOrder(), is(equalTo(Ordered.LOWEST_PRECEDENCE)));
}

@Test
void addsNewEnvironmentVariable() {
// given
TaskDefinition taskDefinition =
new TaskDefinitionImpl(
RandomUtils.nextLong(),
RandomStringUtils.randomAscii(10),
null,
Collections.emptyMap()
);
RuntimeTaskDefinition runtimeTaskDefinition = new RuntimeTaskDefinitionImpl(taskDefinition);

// when
environmentVariableSetter.apply(runtimeTaskDefinition, "NEW", "value");

// then
Map<String, String> result = runtimeTaskDefinition.getConfiguration();

assertThat(result.size(), is(equalTo(1)));
assertThat(result,
hasEntry(
equalTo(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY),
equalTo("NEW=value"))
);
}

@ParameterizedTest
@CsvSource({
"NEW, value, OLD=value NEW=value",
"NEW, value with space, OLD=value NEW=\"value with space\"",
"NEW, \"value\" with space, OLD=value NEW='\"value\" with space'"
})
void updatesExistingEnvironmentVariable(String key, String value, String expected) {
// given
TaskDefinition taskDefinition =
new TaskDefinitionImpl(
RandomUtils.nextLong(),
RandomStringUtils.randomAscii(10),
null,
Collections.singletonMap(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY, "OLD=value")
);
RuntimeTaskDefinition runtimeTaskDefinition = new RuntimeTaskDefinitionImpl(taskDefinition);

// when
environmentVariableSetter.apply(runtimeTaskDefinition, key, value);

// then
Map<String, String> result = runtimeTaskDefinition.getConfiguration();

assertThat(result.size(), is(equalTo(1)));
assertThat(result,
hasEntry(
equalTo(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY),
equalTo(expected))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;

public class DefaultMavenOptsSetterTest {

private final GradleEnterpriseMavenOptsSetter mavenOptsSetter = new DefaultMavenOptsSetter(
new EnvironmentVariableAccessorImpl(null, null)
);
private final GradleEnterpriseMavenOptsSetter mavenOptsSetter =
new DefaultMavenOptsSetter(new EnvironmentVariableAccessorImpl(null, null));

@Test
void orderIsSet() {
Expand All @@ -32,25 +32,33 @@ void orderIsSet() {

@Test
void mavenOpsAreSet() {
// given
List<SystemProperty> systemProperties = new ArrayList<>();
systemProperties.add(new SystemProperty("maven.ext.class.path", "test/path/gradle-enterprise-maven-extension-1.15.4.jar"));
systemProperties.add(new SystemProperty("gradle.scan.uploadInBackground", "false"));
systemProperties.add(new SystemProperty("gradle.enterprise.url", "url"));

TaskDefinition taskDefinition = new TaskDefinitionImpl(
RandomUtils.nextLong(), RandomStringUtils.randomAscii(10), null, Collections.singletonMap("key", "value")
);

TaskDefinition taskDefinition =
new TaskDefinitionImpl(
RandomUtils.nextLong(),
RandomStringUtils.randomAscii(10),
null,
Collections.singletonMap("key", "value")
);
RuntimeTaskDefinition runtimeTaskDefinition = new RuntimeTaskDefinitionImpl(taskDefinition);

// when
mavenOptsSetter.apply(runtimeTaskDefinition, systemProperties);

// then
Map<String, String> configuration = runtimeTaskDefinition.getConfiguration();

assertThat(configuration.get("key"), is(equalTo("value")));
assertThat(
configuration.get(DefaultMavenOptsSetter.ENVIRONMENT_VARIABLES_KEY),
is(equalTo("MAVEN_OPTS=\"-Dmaven.ext.class.path=test/path/gradle-enterprise-maven-extension-1.15.4.jar -Dgradle.scan.uploadInBackground=false -Dgradle.enterprise.url=url\""))
assertThat(configuration.size(), is(equalTo(2)));
assertThat(configuration, hasEntry(equalTo("key"), equalTo("value")));
assertThat(configuration,
hasEntry(
equalTo(Constants.DEFAULT_TASK_ENVIRONMENT_VARIABLES_KEY),
equalTo("MAVEN_OPTS=\"-Dmaven.ext.class.path=test/path/gradle-enterprise-maven-extension-1.15.4.jar -Dgradle.scan.uploadInBackground=false -Dgradle.enterprise.url=url\""))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.gradle.enterprise.bamboo.config.UsernameAndPasswordCredentialsProvider;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collections;

Expand Down Expand Up @@ -168,8 +170,16 @@ void doesNothingIfInjectionDisabled() {
verify(buildContext, never()).getVariableContext();
}

@Test
void addsAccessKeyToContext() {
@ParameterizedTest
@ValueSource(strings = {
GradleBuildScanInjector.BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_KEY,
GradleBuildScanInjector.BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLE_WRAPPER_KEY,
GradleBuildScanInjector.BOB_SWIFT_GROOVY_TASKS_PLUGIN_GRADLEW_KEY,
GradleBuildScanInjector.SCRIPT_PLUGIN_KEY,
GradleBuildScanInjector.COMMAND_PLUGIN_KEY,
"org.jfrog.bamboo." + GradleBuildScanInjector.ARTIFACTORY_GRADLE_TASK_KEY_SUFFIX
})
void addsAccessKeyToContext(String pluginKey) {
// given
String accessKey = String.format("scans.gradle.com=%s", RandomStringUtils.randomAlphanumeric(10));
CredentialsData credentialsData = mock(CredentialsData.class);
Expand All @@ -188,7 +198,7 @@ void addsAccessKeyToContext() {

RuntimeTaskDefinition runtimeTaskDefinition = mock(RuntimeTaskDefinition.class);
when(runtimeTaskDefinition.isEnabled()).thenReturn(true);
when(runtimeTaskDefinition.getPluginKey()).thenReturn(GradleBuildScanInjector.SCRIPT_PLUGIN_KEY);
when(runtimeTaskDefinition.getPluginKey()).thenReturn(pluginKey);
when(buildContext.getRuntimeTaskDefinitions()).thenReturn(Collections.singletonList(runtimeTaskDefinition));
when(buildContext.getVariableContext()).thenReturn(variableContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public synchronized void close() throws Exception {
30, TimeUnit.SECONDS,
10, TimeUnit.SECONDS
);
bambooApi.deleteAgent(remoteAgent.id);
if (bambooApi.deleteAgentSupported()) {
bambooApi.deleteAgent(remoteAgent.id);
}
}
}

Expand Down
Loading

0 comments on commit 7a28b56

Please sign in to comment.