Skip to content

Commit

Permalink
JAVA-8886 restructure plugin to be more inline with what gradle expects
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPhillips2020 committed Jan 10, 2025
1 parent f5bb67b commit 2e34d5b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.testing.Test;
import org.jetbrains.annotations.VisibleForTesting;

/**
* Gradle plugin for contrast utilities. The goals for this plugin are defined here <a
Expand All @@ -18,25 +25,124 @@ public void apply(final Project target) {
target.getExtensions().create(EXTENSION_NAME, ContrastConfigurationExtension.class);

TaskProvider<ResolveAgentTask> resolveAgentTask =
target
.getTasks()
.register(
"resolveAgent",
ResolveAgentTask.class,
task -> {
task.setAgentFile(new File(target.getProjectDir() + "/agent.jar"));
});
target.getTasks().register("resolveAgent", ResolveAgentTask.class);

resolveAgentTask.configure(
task -> {
task.setFileString("contrast.jar");
task.setAgent(
new File(
target.getLayout().getBuildDirectory().getAsFile().get().getPath(),
"contrast.jar"));
});

// lifecycle task for grouping verification tasks
TaskProvider<Task> contrastCheck = target.getTasks().register("contrastCheck");

// Create contrast Args
final Collection<String> contrastArgs =
createContrastArgs(
target.getName(),
resolveAgentTask.get().getOutputs().getFiles().getAsPath(),
extension.getAppName(),
extension.getServerName(),
extension.getAppVersion());

// for each test task...
target
.getTasks()
.register("installAgent", InstallAgentTask.class)
.configure(
task -> {
// Ensure resolve agent task runs before attempting to install the agent
task.dependsOn(resolveAgentTask);
// set input for the agent file as the output from the resolve agent task
task.getInputFile().set(resolveAgentTask.flatMap(ResolveAgentTask::getAgentFile));
.withType(Test.class)
.forEach(
testTask -> {
// tests depend on get Agent
testTask.dependsOn(resolveAgentTask);

// attach agent args
testTask.jvmArgs(contrastArgs);

// generate ContrastVerifyTestTask for each test
TaskProvider<ContrastVerifyTestTask> verifyTask =
target
.getTasks()
.register(
"contrastVerifyTest" + testTask.getName(), ContrastVerifyTestTask.class);
verifyTask.configure(
v -> {
v.dependsOn(testTask);
// set Input as testTask junit Output. I don't know how to do this yet. You can
// call testTask.getOutputs but then what?
// v.setjUnitOutput(?);
});

// configure lifecycle task to depend on this test's corresponding verifyTask
contrastCheck.configure(task -> task.dependsOn(verifyTask));
});
}

/**
* Creates the jvmArgs for running the java agent against JavaExec tasks
*
* @param projectName name of the current Gradle Project
* @param agentPath preconfigured path to an agent defined by the ContrastConfigurationExtension
* @param appName the name of the application on TS defined by the ContrastConfigurationExtension
* @param serverName the name of the server on TS defined by the ContrastConfigurationExtension
* @param appVersion the app version, if not supplied by the ContrastConfigurationExtension, then
* it is generated in {@link InstallAgentTask#computeAppVersion(String)}
* @return Set of arguments to be attached to specified tasks
*/
@VisibleForTesting
public static Collection<String> createContrastArgs(
final String projectName,
final String agentPath,
final String appName,
final String serverName,
String appVersion) {

// List to preserve ordering of arguments
final Collection<String> args = new LinkedList<>();

args.add("-javaagent:" + agentPath);

// Use gradle project name in the even the appName is not set
if ("null".equals(appName) || appName == null) {
args.add("-Dcontrast.override.appname=" + projectName);
} else {
args.add("-Dcontrast.override.appname=" + appName);
}

args.add("-Dcontrast.server=" + serverName);

if (appVersion == null) {
appVersion = computeAppVersion(appName);
}

args.add("-Dcontrast.override.appversion=" + appVersion);

return args;
}

/**
* Shamelessly stolen from the maven plugin TODO check if we still want to do this based on travis
* or circle build number
*
* @return computed AppVersion
*/
@VisibleForTesting
public static String computeAppVersion(final String appName) {
final Date currentDate = new Date();
final String travisBuildNumber = System.getenv("TRAVIS_BUILD_NUMBER");
final String circleBuildNum = System.getenv("CIRCLE_BUILD_NUM");

final String appVersionQualifier;
if (travisBuildNumber != null) {
appVersionQualifier = travisBuildNumber;
} else if (circleBuildNum != null) {
appVersionQualifier = circleBuildNum;
} else {
appVersionQualifier = new SimpleDateFormat("yyyyMMddHHmmss").format(currentDate);
}
return appName + "-" + appVersionQualifier;
}

public static final String EXTENSION_NAME = "contrastConfiguration";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.contrastsecurity.gradle.plugin;

import java.io.File;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.InputFile;

public class ContrastVerifyTestTask extends DefaultTask {

@InputFile private File jUnitResults;

public void setJUnitOutput(final File jUnitResults) {
this.jUnitResults = jUnitResults;
}
}

This file was deleted.

Loading

0 comments on commit 2e34d5b

Please sign in to comment.