Skip to content

Commit 1809680

Browse files
authored
Merge pull request #40 from gradle/dd/job-correlator
Add configuration options for github-actions integration
2 parents cccf5a0 + b5dbc3b commit 1809680

File tree

12 files changed

+99
-108
lines changed

12 files changed

+99
-108
lines changed

.github/workflows/gradle.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ jobs:
6969
uses: gradle/gradle-build-action@v2
7070

7171
- name: Self Test :plugin
72-
run: ./plugin-self-test
72+
run: ./plugin-self-test GitHubDependencyGraphPlugin_generateDependencyGraph
7373
env:
7474
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
75+
GITHUB_DEPENDENCY_GRAPH_JOB_ID: ${{ github.run_id }}
76+
GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR: "plugin-self-test"
7577

7678
- name: Save plugin JSON report
7779
uses: actions/upload-artifact@v3
7880
with:
7981
name: plugin-json
80-
path: build/reports/github-dependency-graph-plugin/github-dependency-snapshot.json
82+
path: build/reports/github-dependency-graph-snapshots/plugin-self-test.json
8183
if-no-files-found: error
8284

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ apply plugin: GitHubDependencyGraphPlugin
2424
```
2525

2626
This causes 2 separate plugins to be applied, that can be used independently:
27-
- `GitHubDependencyExtractorPlugin` collects all dependencies that are resolved during a build execution and writes these to a file. The output file can be found at `<root>/build/reports/github-depenency-graph-gradle-plugin/github-dependency-snapshot.json`.
27+
- `GitHubDependencyExtractorPlugin` collects all dependencies that are resolved during a build execution and writes these to a file. The output file can be found at `<root>/build/reports/github-depenency-graph-snapshots/<job-correlator>.json`.
2828
- `ForceDependencyResolutionPlugin` creates a `GitHubDependencyGraphPlugin_generateDependencyGraph` task that will attempt to resolve all dependencies for a Gradle build, by simply invoking `dependencies` on all projects.
2929

30+
### Required environment variables
31+
32+
The following environment variables configure the snapshot generated by the `GitHubDependencyExtractorPlugin`. See the [GitHub Dependency Submission API docs](https://docs.github.com/en/rest/dependency-graph/dependency-submission?apiVersion=2022-11-28) for details:
33+
- `GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR`: Sets the `job.correlator` value for the dependency submission
34+
- `GITHUB_DEPENDENCY_GRAPH_JOB_ID`: Sets the `job.id` value for the dependency submission
35+
- `GITHUB_REF`: Sets the `ref` value for the dependency submission
36+
- `GITHUB_SHA`: Sets the `sha` value for the dependency submission
37+
- `GITHUB_WORKSPACE`: Sets the root directory of the github repository
38+
- `GITHUB_DEPENDENCY_GRAPH_REPORT_DIR` (optional): Specifies where the dependency graph report will be generated
39+
3040
## Building/Testing
3141

3242
To build and test this plugin, run the following task:

plugin-self-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ else
1616
-H "Authorization: Bearer $GITHUB_TOKEN"\
1717
-H "X-GitHub-Api-Version: 2022-11-28" \
1818
https://api.github.com/repos/gradle/github-dependency-graph-gradle-plugin/dependency-graph/snapshots \
19-
-d @build/reports/github-dependency-graph-plugin/github-dependency-snapshot.json
19+
-d @build/reports/github-dependency-graph-snapshots/plugin-self-test.json
2020
fi

plugin-self-test-local

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

3-
export GITHUB_JOB="Build"
4-
export GITHUB_RUN_NUMBER="42"
3+
export GITHUB_DEPENDENCY_GRAPH_JOB_ID="42"
4+
export GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR="plugin-self-test"
55
export GITHUB_REF="refs/heads/main"
66
export GITHUB_SHA=$( git rev-parse HEAD )
77
export GITHUB_WORKSPACE=$( pwd )

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/BaseExtractorTest.groovy

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ abstract class BaseExtractorTest extends Specification {
4949

5050
SimpleGradleExecuter createExecuter() {
5151
// Create a new JsonManifestLoader for each invocation of the executer
52-
File manifestFile =
53-
testDirectory.file("build/reports/github-dependency-graph-plugin/github-dependency-snapshot.json")
52+
File manifestFile = reportDir.file("dummy-job-correlator.json")
5453
loader = new JsonRepositorySnapshotLoader(manifestFile)
5554
return createExecuter(testGradleVersion)
5655
}
@@ -73,6 +72,10 @@ abstract class BaseExtractorTest extends Specification {
7372
return new TestFile(testDir)
7473
}
7574

75+
TestFile getReportDir() {
76+
return getTestDirectory().file("reports")
77+
}
78+
7679
TestFile file(Object... path) {
7780
if (path.length == 1 && path[0] instanceof TestFile) {
7881
return path[0] as TestFile
@@ -108,7 +111,7 @@ abstract class BaseExtractorTest extends Specification {
108111
}
109112

110113
protected void establishEnvironmentVariables() {
111-
environmentVars = new TestEnvironmentVars(testDirectory)
114+
environmentVars = new TestEnvironmentVars(testDirectory, reportDir)
112115
getExecuter().withEnvironmentVars(environmentVars.asEnvironmentMap())
113116
}
114117

@@ -134,8 +137,8 @@ abstract class BaseExtractorTest extends Specification {
134137
assert json.sha == environmentVars.sha
135138
assert json.ref == environmentVars.ref
136139
def job = json.job as Map
137-
assert job.correlator == environmentVars.job
138-
assert job.id == environmentVars.runNumber
140+
assert job.correlator == environmentVars.jobCorrelator
141+
assert job.id == environmentVars.jobId
139142
def detector = json.detector as Map
140143
assert detector.name.contains("Gradle")
141144
assert detector.version != null
@@ -255,25 +258,28 @@ abstract class BaseExtractorTest extends Specification {
255258
}
256259

257260
static class TestEnvironmentVars {
258-
final String job = "Build_" + System.currentTimeMillis()
259-
final String runNumber = UUID.randomUUID().toString()
261+
final String jobId = UUID.randomUUID().toString()
262+
final String jobCorrelator = "dummy-job-correlator"
260263
final String ref = "refs/head/feature/test" + UUID.randomUUID().toString()
261264
final String sha = fakeSha()
262265
final String workspace
266+
final String reportDir
263267
final String gitHubToken = UUID.randomUUID().toString()
264268

265-
TestEnvironmentVars(TestFile testDirectory) {
269+
TestEnvironmentVars(TestFile testDirectory, TestFile reportDir) {
266270
workspace = testDirectory.absolutePath
271+
this.reportDir = reportDir.absolutePath
267272
}
268273

269274
Map<String, String> asEnvironmentMap() {
270275
return [
271-
"GITHUB_JOB" : job,
272-
"GITHUB_RUN_NUMBER": runNumber,
273-
"GITHUB_REF" : ref,
274-
"GITHUB_SHA" : sha,
275-
"GITHUB_WORKSPACE" : workspace,
276-
"GITHUB_TOKEN" : gitHubToken
276+
"GITHUB_DEPENDENCY_GRAPH_JOB_ID" : jobId,
277+
"GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR": jobCorrelator,
278+
"GITHUB_DEPENDENCY_GRAPH_REPORT_DIR" : reportDir,
279+
"GITHUB_REF" : ref,
280+
"GITHUB_SHA" : sha,
281+
"GITHUB_WORKSPACE" : workspace,
282+
"GITHUB_TOKEN" : gitHubToken
277283
]
278284
}
279285

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/SingleProjectDependencyExtractorTest.groovy

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ class SingleProjectDependencyExtractorTest extends BaseExtractorTest {
3232
"""
3333
}
3434

35-
def "includes build path in job correlator"() {
36-
when:
37-
executer.withArgument("-Dorg.gradle.github.env.GRADLE_BUILD_PATH=foo/bar")
38-
run()
39-
40-
then:
41-
def json = jsonRepositorySnapshot()
42-
def job = json.job as Map
43-
job.correlator == environmentVars.job + ":foo/bar"
44-
}
45-
4635
def "extracts implementation and test dependencies for a java project"() {
4736
given:
4837
buildFile << """

plugin/src/main/kotlin/org/gradle/github/dependencygraph/GitHubDependencyExtractorPlugin.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import java.nio.file.Paths
2222
*/
2323
class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
2424
private companion object : PluginCompanionUtils() {
25-
const val ENV_GITHUB_JOB = "GITHUB_JOB"
26-
const val ENV_GITHUB_RUN_NUMBER = "GITHUB_RUN_NUMBER"
25+
const val ENV_DEPENDENCY_GRAPH_JOB_ID = "GITHUB_DEPENDENCY_GRAPH_JOB_ID"
26+
const val ENV_DEPENDENCY_GRAPH_JOB_CORRELATOR = "GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR"
27+
const val ENV_DEPENDENCY_GRAPH_REPORT_DIR = "GITHUB_DEPENDENCY_GRAPH_REPORT_DIR"
2728
const val ENV_GITHUB_REF = "GITHUB_REF"
2829
const val ENV_GITHUB_SHA = "GITHUB_SHA"
29-
const val ENV_GRADLE_BUILD_PATH = "GRADLE_BUILD_PATH"
3030

3131
/**
3232
* Environment variable should be set to the workspace directory that the Git repository is checked out in.
33+
* This is used to determine relative path to build files referenced in the dependency graph.
3334
*/
3435
const val ENV_GITHUB_WORKSPACE = "GITHUB_WORKSPACE"
3536
}
@@ -50,7 +51,7 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
5051
gradle.rootProject { project ->
5152
dependencyExtractorProvider
5253
.get()
53-
.setRootProjectBuildDirectory(project.buildDir)
54+
.rootProjectBuildDirectory = project.buildDir
5455
}
5556

5657
// Register the service to listen for Build Events
@@ -92,18 +93,18 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
9293
// Create a constant value that the provider will always return.
9394
// IE. Memoize the value
9495
val constantDependencyExtractor = object : DependencyExtractor() {
95-
override val gitHubJobName: String
96-
get() = gradle.loadEnvironmentVariable(ENV_GITHUB_JOB)
97-
override val gitHubRunNumber: String
98-
get() = gradle.loadEnvironmentVariable(ENV_GITHUB_RUN_NUMBER)
96+
override val dependencyGraphJobCorrelator: String
97+
get() = gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_CORRELATOR)
98+
override val dependencyGraphJobId: String
99+
get() = gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_ID)
100+
override val dependencyGraphReportDir: String
101+
get() = gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_REPORT_DIR, "")
99102
override val gitSha: String
100103
get() = gradle.loadEnvironmentVariable(ENV_GITHUB_SHA)
101104
override val gitRef: String
102105
get() = gradle.loadEnvironmentVariable(ENV_GITHUB_REF)
103106
override val gitWorkspaceDirectory: Path
104107
get() = gitWorkspaceDirectory
105-
override val gradleBuildPath: String
106-
get() = gradle.loadEnvironmentVariable(ENV_GRADLE_BUILD_PATH, "")
107108
}
108109
return providerFactory.provider { constantDependencyExtractor }
109110
}
@@ -154,12 +155,12 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
154155
DependencyExtractorBuildService::class.java
155156
) { spec ->
156157
spec.parameters {
157-
it.gitHubJobName.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_JOB))
158-
it.gitHubRunNumber.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_RUN_NUMBER))
158+
it.dependencyGraphJobCorrelator.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_CORRELATOR))
159+
it.dependencyGraphJobId.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_ID))
160+
it.dependencyGraphReportDir.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_REPORT_DIR, ""))
159161
it.gitSha.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_SHA))
160162
it.gitRef.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_REF))
161163
it.gitWorkspaceDirectory.convention(gitWorkspaceDirectory)
162-
it.gradleBuildPath.convention(gradle.loadEnvironmentVariable(ENV_GRADLE_BUILD_PATH, ""))
163164
}
164165
}
165166
}

plugin/src/main/kotlin/org/gradle/github/dependencygraph/internal/DependencyExtractor.kt

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
55
import org.gradle.api.artifacts.result.ResolvedDependencyResult
66
import org.gradle.api.internal.artifacts.DefaultProjectComponentIdentifier
77
import org.gradle.api.internal.artifacts.configurations.ResolveConfigurationDependenciesBuildOperationType
8+
import org.gradle.api.logging.Logging
89
import org.gradle.github.GitHubDependencyGraphPlugin
9-
import org.gradle.github.dependencygraph.GitHubDependencyExtractorPlugin
1010
import org.gradle.github.dependencygraph.internal.model.ComponentCoordinates
1111
import org.gradle.github.dependencygraph.internal.model.ResolvedComponent
1212
import org.gradle.github.dependencygraph.internal.model.ResolvedConfiguration
@@ -23,27 +23,22 @@ abstract class DependencyExtractor :
2323
BuildOperationListener,
2424
AutoCloseable {
2525

26-
protected abstract val gitHubJobName: String
27-
protected abstract val gitHubRunNumber: String
26+
protected abstract val dependencyGraphJobCorrelator: String
27+
protected abstract val dependencyGraphJobId: String
28+
protected abstract val dependencyGraphReportDir: String
2829
protected abstract val gitSha: String
2930
protected abstract val gitRef: String
3031
protected abstract val gitWorkspaceDirectory: Path
31-
protected abstract val gradleBuildPath: String
3232

33-
/**
34-
* Can't use this as a proper input:
35-
* https://github.com/gradle/gradle/issues/19562
36-
*/
37-
private var fileWriter: DependencyFileWriter = DependencyFileWriter.create()
33+
var rootProjectBuildDirectory: File? = null
3834

3935
private val gitHubRepositorySnapshotBuilder by lazy {
4036
GitHubRepositorySnapshotBuilder(
41-
gitHubJobName = gitHubJobName,
42-
gitHubRunNumber = gitHubRunNumber,
37+
dependencyGraphJobCorrelator = dependencyGraphJobCorrelator,
38+
dependencyGraphJobId = dependencyGraphJobId,
4339
gitSha = gitSha,
4440
gitRef = gitRef,
45-
gitWorkspaceDirectory = gitWorkspaceDirectory,
46-
gradleBuildPath = gradleBuildPath
41+
gitWorkspaceDirectory = gitWorkspaceDirectory
4742
)
4843
}
4944

@@ -53,10 +48,6 @@ abstract class DependencyExtractor :
5348
println("Creating: DependencyExtractorService")
5449
}
5550

56-
internal fun setRootProjectBuildDirectory(rootProjectBuildDirectory: File) {
57-
fileWriter = DependencyFileWriter.create(rootProjectBuildDirectory)
58-
}
59-
6051
override fun started(buildOperation: BuildOperationDescriptor, startEvent: OperationStartEvent) {
6152
// This method will never be called when registered in a `BuildServiceRegistry` (ie. Gradle 6.1 & higher)
6253
// No-op
@@ -185,8 +176,25 @@ abstract class DependencyExtractor :
185176
}
186177
}
187178

188-
private fun writeAndGetSnapshotFile(): File =
179+
private fun writeAndGetSnapshotFile() {
180+
val outputFile = File(getOutputDir(), "${dependencyGraphJobCorrelator}.json")
181+
val fileWriter = DependencyFileWriter(outputFile)
189182
fileWriter.writeDependencyManifest(gitHubRepositorySnapshotBuilder.build())
183+
}
184+
185+
private fun getOutputDir(): File {
186+
if (dependencyGraphReportDir.isNotEmpty()) {
187+
return File(dependencyGraphReportDir)
188+
}
189+
190+
if (rootProjectBuildDirectory == null) {
191+
throw RuntimeException("Cannot determine report file location")
192+
}
193+
return File(
194+
rootProjectBuildDirectory,
195+
"reports/github-dependency-graph-snapshots"
196+
)
197+
}
190198

191199
override fun close() {
192200
if (thrownExceptions.isNotEmpty()) {
@@ -206,6 +214,9 @@ abstract class DependencyExtractor :
206214
)
207215
}
208216
}
217+
companion object {
218+
private val LOGGER = Logging.getLogger(DependencyExtractor::class.java)
219+
}
209220
}
210221

211222
private inline fun <reified D, reified R> handleBuildOperationTypeRaw(

plugin/src/main/kotlin/org/gradle/github/dependencygraph/internal/DependencyExtractorBuildService.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ abstract class DependencyExtractorBuildService :
1111
BuildService<DependencyExtractorBuildService.Parameters> {
1212

1313
interface Parameters : BuildServiceParameters {
14-
val gitHubJobName: Property<String>
15-
val gitHubRunNumber: Property<String>
14+
val dependencyGraphJobId: Property<String>
15+
val dependencyGraphJobCorrelator: Property<String>
16+
val dependencyGraphReportDir: Property<String>
1617
val gitSha: Property<String>
1718
val gitRef: Property<String>
1819
val gitWorkspaceDirectory: DirectoryProperty
19-
val gradleBuildPath: Property<String>
2020
}
2121

22-
override val gitHubJobName: String
23-
get() = parameters.gitHubJobName.get()
24-
override val gitHubRunNumber: String
25-
get() = parameters.gitHubRunNumber.get()
22+
override val dependencyGraphJobId: String
23+
get() = parameters.dependencyGraphJobId.get()
24+
override val dependencyGraphJobCorrelator: String
25+
get() = parameters.dependencyGraphJobCorrelator.get()
26+
override val dependencyGraphReportDir: String
27+
get() = parameters.dependencyGraphReportDir.get()
2628
override val gitSha: String
2729
get() = parameters.gitSha.get()
2830
override val gitRef: String
2931
get() = parameters.gitRef.get()
3032
override val gitWorkspaceDirectory: Path
3133
get() = parameters.gitWorkspaceDirectory.get().asFile.toPath()
32-
override val gradleBuildPath: String
33-
get() = parameters.gradleBuildPath.get()
3434
}

plugin/src/main/kotlin/org/gradle/github/dependencygraph/internal/DependencyFileWriter.kt

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ import org.gradle.api.logging.Logging
44
import org.gradle.github.dependencygraph.internal.json.GitHubRepositorySnapshot
55
import java.io.File
66

7-
class DependencyFileWriter
8-
private constructor(
9-
private val manifestFile: File,
10-
private val loggerWarning: () -> Unit
11-
) {
12-
7+
class DependencyFileWriter(val manifestFile: File) {
138
private var writtenFile: Boolean = false
149

1510
fun writeDependencyManifest(graph: GitHubRepositorySnapshot): File {
1611
if (writtenFile) {
1712
return manifestFile
1813
}
19-
loggerWarning()
2014
manifestFile.parentFile.mkdirs()
2115
manifestFile.writeText(JacksonJsonSerializer.serializeToJson(graph))
2216
writtenFile = true
@@ -26,28 +20,5 @@ private constructor(
2620

2721
companion object {
2822
private val LOGGER = Logging.getLogger(DependencyFileWriter::class.java)
29-
30-
fun create(buildDirectory: File): DependencyFileWriter =
31-
create(buildDirectory) {
32-
// No-op
33-
}
34-
35-
fun create(): DependencyFileWriter =
36-
create(File(".")) {
37-
LOGGER.warn(
38-
"[WARNING] Something went wrong configuring the GithubDependencyExtractorPlugin. " +
39-
"Using JVM working directory as root of build"
40-
)
41-
}
42-
43-
private fun create(buildDirectory: File, loggerWarning: () -> Unit): DependencyFileWriter {
44-
return DependencyFileWriter(
45-
File(
46-
buildDirectory,
47-
"reports/github-dependency-graph-plugin/github-dependency-snapshot.json"
48-
),
49-
loggerWarning
50-
)
51-
}
5223
}
5324
}

0 commit comments

Comments
 (0)