|
| 1 | +/* |
| 2 | + * This plugin defines a set of tasks to be used in CI. These aggregate tasks support partitioning (to parallelize |
| 3 | + * jobs) with -PtaskPartitionCount and -PtaskPartition, and limiting tasks to those affected by git changes |
| 4 | + * with -PgitBaseRef. |
| 5 | + */ |
| 6 | + |
| 7 | +import datadog.gradle.plugin.ci.isAffectedBy |
| 8 | +import java.io.File |
| 9 | +import kotlin.math.abs |
| 10 | + |
| 11 | +// Set up activePartition property on all projects |
| 12 | +allprojects { |
| 13 | + extra.set("activePartition", true) |
| 14 | + |
| 15 | + val shouldUseTaskPartitions = rootProject.hasProperty("taskPartitionCount") && rootProject.hasProperty("taskPartition") |
| 16 | + if (shouldUseTaskPartitions) { |
| 17 | + val taskPartitionCount = rootProject.property("taskPartitionCount") as String |
| 18 | + val taskPartition = rootProject.property("taskPartition") as String |
| 19 | + val currentTaskPartition = abs(project.path.hashCode() % taskPartitionCount.toInt()) |
| 20 | + extra.set("activePartition", currentTaskPartition == taskPartition.toInt()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fun relativeToGitRoot(f: File): File { |
| 25 | + return rootProject.projectDir.toPath().relativize(f.absoluteFile.toPath()).toFile() |
| 26 | +} |
| 27 | + |
| 28 | +fun getChangedFiles(baseRef: String, newRef: String): List<File> { |
| 29 | + val stdout = StringBuilder() |
| 30 | + val stderr = StringBuilder() |
| 31 | + |
| 32 | + val proc = Runtime.getRuntime().exec(arrayOf("git", "diff", "--name-only", "$baseRef..$newRef")) |
| 33 | + proc.inputStream.bufferedReader().use { stdout.append(it.readText()) } |
| 34 | + proc.errorStream.bufferedReader().use { stderr.append(it.readText()) } |
| 35 | + proc.waitFor() |
| 36 | + require(proc.exitValue() == 0) { "git diff command failed, stderr: $stderr" } |
| 37 | + |
| 38 | + val out = stdout.toString().trim() |
| 39 | + if (out.isEmpty()) { |
| 40 | + return emptyList() |
| 41 | + } |
| 42 | + |
| 43 | + logger.debug("git diff output: $out") |
| 44 | + return out.split("\n").map { File(rootProject.projectDir, it.trim()) } |
| 45 | +} |
| 46 | + |
| 47 | +// Initialize git change tracking |
| 48 | +rootProject.extra.set("useGitChanges", false) |
| 49 | + |
| 50 | +if (rootProject.hasProperty("gitBaseRef")) { |
| 51 | + val baseRef = rootProject.property("gitBaseRef") as String |
| 52 | + val newRef = if (rootProject.hasProperty("gitNewRef")) { |
| 53 | + rootProject.property("gitNewRef") as String |
| 54 | + } else { |
| 55 | + "HEAD" |
| 56 | + } |
| 57 | + |
| 58 | + val changedFiles = getChangedFiles(baseRef, newRef) |
| 59 | + rootProject.extra.set("changedFiles", changedFiles) |
| 60 | + rootProject.extra.set("useGitChanges", true) |
| 61 | + |
| 62 | + val ignoredFiles = fileTree(rootProject.projectDir) { |
| 63 | + include(".gitignore", ".editorconfig") |
| 64 | + include("*.md", "**/*.md") |
| 65 | + include("gradlew", "gradlew.bat", "mvnw", "mvnw.cmd") |
| 66 | + include("NOTICE") |
| 67 | + include("static-analysis.datadog.yml") |
| 68 | + } |
| 69 | + |
| 70 | + changedFiles.forEach { f -> |
| 71 | + if (ignoredFiles.contains(f)) { |
| 72 | + logger.warn("Ignoring changed file: ${relativeToGitRoot(f)}") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + val filteredChangedFiles = changedFiles.filter { !ignoredFiles.contains(it) } |
| 77 | + rootProject.extra.set("changedFiles", filteredChangedFiles) |
| 78 | + |
| 79 | + val globalEffectFiles = fileTree(rootProject.projectDir) { |
| 80 | + include(".gitlab/**") |
| 81 | + include("build.gradle") |
| 82 | + include("gradle/**") |
| 83 | + } |
| 84 | + |
| 85 | + for (f in filteredChangedFiles) { |
| 86 | + if (globalEffectFiles.contains(f)) { |
| 87 | + logger.warn("Global effect change: ${relativeToGitRoot(f)} (no tasks will be skipped)") |
| 88 | + rootProject.extra.set("useGitChanges", false) |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (rootProject.extra.get("useGitChanges") as Boolean) { |
| 94 | + logger.warn("Git change tracking is enabled: $baseRef..$newRef") |
| 95 | + |
| 96 | + val projects = subprojects.sortedByDescending { it.projectDir.path.length } |
| 97 | + val affectedProjects = mutableMapOf<Project, MutableSet<String>>() |
| 98 | + |
| 99 | + // Path prefixes mapped to affected task names. A file not matching any of these prefixes will affect all tasks in |
| 100 | + // the project ("all" can be used a task name to explicitly state the same). Only the first matching prefix is used. |
| 101 | + val matchers = listOf( |
| 102 | + mapOf("prefix" to "src/testFixtures/", "task" to "testFixturesClasses"), |
| 103 | + mapOf("prefix" to "src/test/", "task" to "testClasses"), |
| 104 | + mapOf("prefix" to "src/jmh/", "task" to "jmhCompileGeneratedClasses") |
| 105 | + ) |
| 106 | + |
| 107 | + for (f in filteredChangedFiles) { |
| 108 | + val p = projects.find { f.toString().startsWith(it.projectDir.path + "/") } |
| 109 | + if (p == null) { |
| 110 | + logger.warn("Changed file: ${relativeToGitRoot(f)} at root project (no task will be skipped)") |
| 111 | + rootProject.extra.set("useGitChanges", false) |
| 112 | + break |
| 113 | + } |
| 114 | + |
| 115 | + // Make sure path separator is / |
| 116 | + val relPath = p.projectDir.toPath().relativize(f.toPath()).joinToString("/") |
| 117 | + val task = matchers.find { relPath.startsWith(it["prefix"]!!) }?.get("task") ?: "all" |
| 118 | + logger.warn("Changed file: ${relativeToGitRoot(f)} in project ${p.path} ($task)") |
| 119 | + affectedProjects.computeIfAbsent(p) { mutableSetOf() }.add(task) |
| 120 | + } |
| 121 | + |
| 122 | + rootProject.extra.set("affectedProjects", affectedProjects) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +tasks.register("runMuzzle") { |
| 127 | + val muzzleSubprojects = subprojects.filter { p -> |
| 128 | + val activePartition = p.extra.get("activePartition") as Boolean |
| 129 | + activePartition && p.plugins.hasPlugin("java") && p.plugins.hasPlugin("muzzle") |
| 130 | + } |
| 131 | + dependsOn(muzzleSubprojects.map { p -> "${p.path}:muzzle" }) |
| 132 | +} |
0 commit comments