Skip to content

Commit 4c1e28a

Browse files
committed
Allow report directory to be specified by env var
1 parent 0078047 commit 4c1e28a

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

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

Lines changed: 11 additions & 5 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-Build.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

@@ -256,20 +259,23 @@ abstract class BaseExtractorTest extends Specification {
256259

257260
static class TestEnvironmentVars {
258261
final String jobId = UUID.randomUUID().toString()
259-
final String jobCorrelator = "Build"
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 [
271276
"GITHUB_DEPENDENCY_GRAPH_JOB_ID" : jobId,
272277
"GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR": jobCorrelator,
278+
"GITHUB_DEPENDENCY_GRAPH_REPORT_DIR" : reportDir,
273279
"GITHUB_REF" : ref,
274280
"GITHUB_SHA" : sha,
275281
"GITHUB_WORKSPACE" : workspace,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
2424
private companion object : PluginCompanionUtils() {
2525
const val ENV_DEPENDENCY_GRAPH_JOB_ID = "GITHUB_DEPENDENCY_GRAPH_JOB_ID"
2626
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"
2930

@@ -96,6 +97,8 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
9697
get() = gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_CORRELATOR)
9798
override val dependencyGraphJobId: String
9899
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
@@ -154,6 +157,7 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
154157
spec.parameters {
155158
it.dependencyGraphJobCorrelator.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_CORRELATOR))
156159
it.dependencyGraphJobId.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_JOB_ID))
160+
it.dependencyGraphReportDir.convention(gradle.loadEnvironmentVariable(ENV_DEPENDENCY_GRAPH_REPORT_DIR, ""))
157161
it.gitSha.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_SHA))
158162
it.gitRef.convention(gradle.loadEnvironmentVariable(ENV_GITHUB_REF))
159163
it.gitWorkspaceDirectory.convention(gitWorkspaceDirectory)

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class DependencyExtractor :
2525

2626
protected abstract val dependencyGraphJobCorrelator: String
2727
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
@@ -176,17 +177,23 @@ abstract class DependencyExtractor :
176177
}
177178

178179
private fun writeAndGetSnapshotFile() {
180+
val outputFile = File(getOutputDir(), "${dependencyGraphJobCorrelator}.json")
181+
val fileWriter = DependencyFileWriter(outputFile)
182+
fileWriter.writeDependencyManifest(gitHubRepositorySnapshotBuilder.build())
183+
}
184+
185+
private fun getOutputDir(): File {
186+
if (dependencyGraphReportDir.isNotEmpty()) {
187+
return File(dependencyGraphReportDir)
188+
}
189+
179190
if (rootProjectBuildDirectory == null) {
180-
LOGGER.warn("Unable to write dependency graph snapshot: target directory not known")
181-
return
191+
throw RuntimeException("Cannot determine report file location")
182192
}
183-
val outputFile = File(
193+
return File(
184194
rootProjectBuildDirectory,
185-
"reports/github-dependency-graph-plugin/github-dependency-snapshot-${dependencyGraphJobCorrelator}.json"
195+
"reports/github-dependency-graph-snapshots"
186196
)
187-
188-
val fileWriter = DependencyFileWriter(outputFile)
189-
fileWriter.writeDependencyManifest(gitHubRepositorySnapshotBuilder.build())
190197
}
191198

192199
override fun close() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class DependencyExtractorBuildService :
1313
interface Parameters : BuildServiceParameters {
1414
val dependencyGraphJobId: Property<String>
1515
val dependencyGraphJobCorrelator: Property<String>
16+
val dependencyGraphReportDir: Property<String>
1617
val gitSha: Property<String>
1718
val gitRef: Property<String>
1819
val gitWorkspaceDirectory: DirectoryProperty
@@ -22,6 +23,8 @@ abstract class DependencyExtractorBuildService :
2223
get() = parameters.dependencyGraphJobId.get()
2324
override val dependencyGraphJobCorrelator: String
2425
get() = parameters.dependencyGraphJobCorrelator.get()
26+
override val dependencyGraphReportDir: String
27+
get() = parameters.dependencyGraphReportDir.get()
2528
override val gitSha: String
2629
get() = parameters.gitSha.get()
2730
override val gitRef: String

0 commit comments

Comments
 (0)