Skip to content

Commit

Permalink
GradleEnterprise -> Develocity
Browse files Browse the repository at this point in the history
  • Loading branch information
blindpirate committed Apr 23, 2024
1 parent 2a46beb commit 11f64e2
Show file tree
Hide file tree
Showing 26 changed files with 538 additions and 381 deletions.
20 changes: 10 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

rootProject.group = "com.github.gradle"
rootProject.group = "io.github.gradle"
rootProject.version = "0.9.1"
rootProject.version = "0.10.0"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -19,13 +19,13 @@ repositories {
}

dependencies {
val gradleEnterprisePluginVersion = "3.16.2"
val junit5Version = "5.6.2"
val jacksonVersion = "2.10.3"
val mockitoExtensionVersion = "3.3.3"
val develocityPluginVersion = "3.17.2"
val junit5Version = "5.10.2"
val jacksonVersion = "2.17.0"
val mockitoExtensionVersion = "5.11.0"

compileOnly("com.gradle:gradle-enterprise-gradle-plugin:${gradleEnterprisePluginVersion}")
testImplementation("com.gradle:gradle-enterprise-gradle-plugin:${gradleEnterprisePluginVersion}")
compileOnly("com.gradle:develocity-gradle-plugin:${develocityPluginVersion}")
testImplementation("com.gradle:gradle-enterprise-gradle-plugin:${develocityPluginVersion}")
implementation(gradleApi())

testImplementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion")
Expand All @@ -45,9 +45,9 @@ extensions.configure<ExtraPropertiesExtension>("ext") {
gradlePlugin {
plugins.create("conventionsPlugin") {
id = "io.github.gradle.gradle-enterprise-conventions-plugin"
implementationClass = "com.gradle.enterprise.conventions.GradleEnterpriseConventionsPlugin"
displayName = "Gradle Enterprise Conventions Plugin"
description = "Gradle Enterprise Conventions Plugin"
implementationClass = "com.gradle.enterprise.conventions.DevelocityConventionsPlugin"
displayName = "Develocity Conventions Plugin"
description = "Develocity Conventions Plugin"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.gradle.enterprise.conventions;

import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.DevelocityPlugin;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import com.gradle.enterprise.conventions.customvalueprovider.BuildCacheCustomValueProvider;
import com.gradle.enterprise.conventions.customvalueprovider.BuildScanCustomValueProvider;
import com.gradle.enterprise.conventions.customvalueprovider.CITagProvider;
import com.gradle.enterprise.conventions.customvalueprovider.DevelocityConventions;
import com.gradle.enterprise.conventions.customvalueprovider.GitInformationCustomValueProvider;
import com.gradle.enterprise.conventions.customvalueprovider.GradleEnterpriseConventions;
import com.gradle.enterprise.conventions.customvalueprovider.LocalBuildCustomValueProvider;
import com.gradle.enterprise.conventions.customvalueprovider.WatchFilesystemCustomValueProvider;
import com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension;
import com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin;
import com.gradle.enterprise.gradleplugin.internal.extension.BuildScanExtensionInternal;
import com.gradle.scan.plugin.BuildScanCaptureSettings;
import com.gradle.scan.plugin.BuildScanExtension;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.initialization.Settings;
Expand All @@ -27,8 +25,8 @@
import static com.gradle.enterprise.conventions.customvalueprovider.CIBuildCustomValueProvider.TeamCityCustomValueProvider;
import static com.gradle.enterprise.conventions.customvalueprovider.CIBuildCustomValueProvider.TravisCustomValueProvider;

public abstract class GradleEnterpriseConventionsPlugin implements Plugin<Settings> {
private List<BuildScanCustomValueProvider> createBuildScanCustomValueProviders(GradleEnterpriseConventions conventions) {
public abstract class DevelocityConventionsPlugin implements Plugin<Settings> {
private List<BuildScanCustomValueProvider> createBuildScanCustomValueProviders(DevelocityConventions conventions) {
return Arrays.asList(
new BuildCacheCustomValueProvider(conventions),
new WatchFilesystemCustomValueProvider(conventions),
Expand All @@ -47,11 +45,11 @@ private List<BuildScanCustomValueProvider> createBuildScanCustomValueProviders(G

@Override
public void apply(Settings settings) {
settings.getPlugins().withType(GradleEnterprisePlugin.class, p -> {
GradleEnterpriseConventions conventions = new GradleEnterpriseConventions(getProviderFactory());
settings.getPlugins().withType(DevelocityPlugin.class, p -> {
DevelocityConventions conventions = new DevelocityConventions(getProviderFactory());
if (settings.getGradle().getStartParameter().isBuildCacheEnabled()) {
GradleEnterpriseExtension ge = settings.getExtensions().getByType(GradleEnterpriseExtension.class);
settings.buildCache(new BuildCacheConfigureAction(conventions, ge));
DevelocityConfiguration dv = settings.getExtensions().getByType(DevelocityConfiguration.class);
settings.buildCache(new BuildCacheConfigureAction(conventions, dv));
}
if (!settings.getGradle().getStartParameter().isNoBuildScan() && !containsPropertiesTask(settings)) {
configureBuildScan(settings, conventions);
Expand All @@ -66,51 +64,36 @@ private boolean containsPropertiesTask(Settings settings) {
|| settings.getGradle().getStartParameter().getTaskNames().stream().anyMatch(it -> it.endsWith(":properties"));
}

private void configureBuildScan(Settings settings, GradleEnterpriseConventions conventions) {
BuildScanExtension buildScan = settings.getExtensions().getByType(GradleEnterpriseExtension.class).getBuildScan();

private void configureBuildScan(Settings settings, DevelocityConventions conventions) {
DevelocityConfiguration dv = settings.getExtensions().getByType(DevelocityConfiguration.class);
BuildScanConfiguration buildScan = dv.getBuildScan();

// This means `-DagreePublicBuildScanTermOfService=yes` is present
if (conventions.getGradleEnterpriseServerUrl() == null) {
buildScan.setTermsOfServiceUrl("https://gradle.com/terms-of-service");
buildScan.setTermsOfServiceAgree("yes");
if (conventions.getDevelocityServerUrl() == null) {
buildScan.getTermsOfUseAgree().set("yes");
buildScan.getTermsOfUseUrl().set("https://gradle.com/terms-of-service");
} else {
buildScan.setServer(conventions.getGradleEnterpriseServerUrl());
publishIfAuthenticated(buildScan);
dv.getServer().set(conventions.getDevelocityServerUrl());
buildScan.publishing(PublishingConfigurationAction.PUBLISH_IF_AUTHENTICATED);
}
buildScan.capture(new Action<BuildScanCaptureSettings>() {
@Override
public void execute(BuildScanCaptureSettings buildScanCaptureSettings) {
buildScanCaptureSettings.setTaskInputFiles(true);
}
});
buildScan.capture(buildScanCaptureConfiguration -> buildScanCaptureConfiguration.getFileFingerprints().set(true));
configurePublishStrategy(conventions, buildScan);
try {
buildScan.setUploadInBackground(!conventions.isCiServer());
} catch (NoSuchMethodError e) {
// GE Plugin version < 3.3. Continue
}
buildScan.getUploadInBackground().set(!conventions.isCiServer());

createBuildScanCustomValueProviders(conventions).stream()
.filter(BuildScanCustomValueProvider::isEnabled)
.forEach(it -> it.accept(settings, buildScan));
}

private void publishIfAuthenticated(BuildScanExtension buildScan) {
try {
((BuildScanExtensionInternal)buildScan).publishIfAuthenticated();
} catch (ClassCastException e) {
throw new IllegalStateException("Could not call publishIfAuthenticated()", e);
}
}

private void configurePublishStrategy(GradleEnterpriseConventions conventions, BuildScanExtension buildScan) {
private void configurePublishStrategy(DevelocityConventions conventions, BuildScanConfiguration buildScan) {
String strategy = conventions.getSystemProperty("publishStrategy", "publishAlways");
switch (strategy) {
case "publishAlways":
buildScan.publishAlways();
buildScan.publishing(PublishingConfigurationAction.PUBLISH_ALWAYS);
break;
case "publishOnFailure":
buildScan.publishOnFailure();
buildScan.publishing(PublishingConfigurationAction.PUBLISH_ON_FAILURE);
break;
default:
throw new IllegalStateException("Unknown strategy: " + strategy);
Expand All @@ -128,12 +111,12 @@ private static class BuildCacheConfigureAction implements Action<BuildCacheConfi
private static final String GRADLE_ENTERPRISE_ACCESS_KEY = "GRADLE_ENTERPRISE_ACCESS_KEY";
private static final String GRADLE_CACHE_REMOTE_PUSH_PROPERTY_NAME = "gradle.cache.remote.push";
private static final String GRADLE_CACHE_NODE_PROPERTY_NAME = "cacheNode";
private final GradleEnterpriseConventions conventions;
private final GradleEnterpriseExtension ge;
private final DevelocityConventions conventions;
private final DevelocityConfiguration dv;

public BuildCacheConfigureAction(GradleEnterpriseConventions conventions, GradleEnterpriseExtension ge) {
public BuildCacheConfigureAction(DevelocityConventions conventions, DevelocityConfiguration dv) {
this.conventions = conventions;
this.ge = ge;
this.dv = dv;
}

@Override
Expand All @@ -143,7 +126,7 @@ public void execute(BuildCacheConfiguration buildCache) {
String develocityAccessKey = conventions.getEnvVariableThenSystemProperty(DEVELOCITY_ACCESS_KEY, DEVELOCITY_ACCESS_KEY, "");
String geAccessKey = conventions.getEnvVariableThenSystemProperty(GRADLE_ENTERPRISE_ACCESS_KEY, GRADLE_ENTERPRISE_ACCESS_KEY, "");
boolean disableLocalCache = Boolean.parseBoolean(conventions.getSystemProperty("disableLocalCache", "false"));
buildCache.remote(ge.getBuildCache(), remoteBuildCache -> {
buildCache.remote(dv.getBuildCache(), remoteBuildCache -> {
remoteBuildCache.setEnabled(true);
remoteBuildCache.setServer(remoteCacheUrl);
boolean accessKeySet = notNullOrEmpty(develocityAccessKey) || notNullOrEmpty(geAccessKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gradle.enterprise.conventions;

import com.gradle.develocity.agent.gradle.scan.BuildScanPublishingConfiguration;
import org.gradle.api.Action;

public abstract class PublishingConfigurationAction implements Action<BuildScanPublishingConfiguration> {

public static final PublishingConfigurationAction PUBLISH_IF_AUTHENTICATED = new PublishingConfigurationAction("publishWhenAuthenticated") {
@Override
public void execute(BuildScanPublishingConfiguration publishing) {
publishing.onlyIf(BuildScanPublishingConfiguration.PublishingContext::isAuthenticated);
}
};

public static final PublishingConfigurationAction PUBLISH_ON_FAILURE = new PublishingConfigurationAction("publishOnFailure") {
@Override
public void execute(BuildScanPublishingConfiguration publishing) {
publishing.onlyIf(spec -> !spec.getBuildResult().getFailures().isEmpty());
}
};
public static final PublishingConfigurationAction PUBLISH_ALWAYS = new PublishingConfigurationAction("publishOnFailure") {
@Override
public void execute(BuildScanPublishingConfiguration publishing) {
publishing.onlyIf(__ -> true);
}
};
public final String name;

public PublishingConfigurationAction(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.gradle.enterprise.conventions.customvalueprovider;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.api.initialization.Settings;

public class BuildCacheCustomValueProvider extends BuildScanCustomValueProvider {
public BuildCacheCustomValueProvider(GradleEnterpriseConventions conventions) {
public BuildCacheCustomValueProvider(DevelocityConventions conventions) {
super(conventions);
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
if (settings.getGradle().getStartParameter().isBuildCacheEnabled()) {
buildScan.tag("CACHED");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.gradle.enterprise.conventions.customvalueprovider;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.api.initialization.Settings;

import java.util.function.BiConsumer;

/**
* Used to provide custom value for Build Scan.
*/
public abstract class BuildScanCustomValueProvider implements BiConsumer<Settings, BuildScanExtension> {
private final GradleEnterpriseConventions conventions;
public abstract class BuildScanCustomValueProvider implements BiConsumer<Settings, BuildScanConfiguration> {
private final DevelocityConventions conventions;

public BuildScanCustomValueProvider(GradleEnterpriseConventions conventions) {
public BuildScanCustomValueProvider(DevelocityConventions conventions) {
this.conventions = conventions;
}

public GradleEnterpriseConventions getConventions() {
public DevelocityConventions getConventions() {
return conventions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gradle.enterprise.conventions.customvalueprovider;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.api.initialization.Settings;

import java.util.Collections;
Expand All @@ -12,7 +12,7 @@
public abstract class CIBuildCustomValueProvider extends BuildScanCustomValueProvider {
private final String markEnvVariableName;

CIBuildCustomValueProvider(String markEnvVariableName, GradleEnterpriseConventions conventions) {
CIBuildCustomValueProvider(String markEnvVariableName, DevelocityConventions conventions) {
super(conventions);
this.markEnvVariableName = markEnvVariableName;
}
Expand All @@ -23,12 +23,12 @@ public boolean isEnabled() {
}

public static class GitHubActionsCustomValueProvider extends CIBuildCustomValueProvider {
public GitHubActionsCustomValueProvider(GradleEnterpriseConventions conventions) {
public GitHubActionsCustomValueProvider(DevelocityConventions conventions) {
super("GITHUB_ACTIONS", conventions);
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
String commitId = getConventions().getEnv("GITHUB_SHA");
String repo = getConventions().getEnv("GITHUB_REPOSITORY");
String runId = getConventions().getEnv("GITHUB_RUN_ID");
Expand All @@ -44,38 +44,38 @@ public void accept(Settings settings, BuildScanExtension buildScan) {
}

public static class JenkinsCustomValueProvider extends CIBuildCustomValueProvider {
public JenkinsCustomValueProvider(GradleEnterpriseConventions conventions) {
public JenkinsCustomValueProvider(DevelocityConventions conventions) {
super("JENKINS_HOME", conventions);
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
buildScan.link("Jenkins Build", getConventions().getEnv("BUILD_URL"));
buildScan.value(BUILD_ID, getConventions().getEnv("BUILD_ID"));
getConventions().setCommitId(settings.getRootDir(), buildScan, getConventions().getEnv("GIT_COMMIT"));
}
}

public static class TeamCityCustomValueProvider extends CIBuildCustomValueProvider {
public TeamCityCustomValueProvider(GradleEnterpriseConventions conventions) {
public TeamCityCustomValueProvider(DevelocityConventions conventions) {
super("TEAMCITY_VERSION", conventions);
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
buildScan.link("TeamCity Build", getConventions().getEnv("BUILD_URL"));
buildScan.value(BUILD_ID, getConventions().getEnv("BUILD_ID"));
getConventions().setCommitId(settings.getRootDir(), buildScan, getConventions().getEnv("BUILD_VCS_NUMBER"));
}
}

public static class TravisCustomValueProvider extends CIBuildCustomValueProvider {
public TravisCustomValueProvider(GradleEnterpriseConventions conventions) {
public TravisCustomValueProvider(DevelocityConventions conventions) {
super("TRAVIS", conventions);
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
buildScan.link("Travis Build", getConventions().getEnv("TRAVIS_BUILD_WEB_URL"));
buildScan.value(BUILD_ID, getConventions().getEnv("TRAVIS_BUILD_ID"));
getConventions().setCommitId(settings.getRootDir(), buildScan, getConventions().getEnv("TRAVIS_COMMIT"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.gradle.enterprise.conventions.customvalueprovider;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.api.initialization.Settings;

public class CITagProvider extends BuildScanCustomValueProvider {
public CITagProvider(GradleEnterpriseConventions conventions) {
public CITagProvider(DevelocityConventions conventions) {
super(conventions);
}

Expand All @@ -14,7 +14,7 @@ public boolean isEnabled() {
}

@Override
public void accept(Settings settings, BuildScanExtension buildScan) {
public void accept(Settings settings, BuildScanConfiguration buildScan) {
buildScan.tag("CI");
}
}
Loading

0 comments on commit 11f64e2

Please sign in to comment.