Skip to content

Commit

Permalink
Decouple inputs from default GitHub env vars
Browse files Browse the repository at this point in the history
Instead of reading built-in GitHub environment variables directly, the caller must
map these to environment variables defined explicitly for this plugin.
This will allow us to use modified input parameters in GitHub Actions
without overriding these built-in environment variable.
  • Loading branch information
bigdaz committed Sep 25, 2023
1 parent 0688904 commit e34f704
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ jobs:
run: ./plugin-self-test ForceDependencyResolutionPlugin_resolveAllDependencies
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_JOB_ID: ${{ github.run_id }}
GITHUB_JOB_CORRELATOR: "plugin-self-test"
GITHUB_DEPENDENCY_GRAPH_JOB_ID: ${{ github.run_id }}
GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR: "plugin-self-test"
GITHUB_DEPENDENCY_GRAPH_REF: ${{ github.ref }}
GITHUB_DEPENDENCY_GRAPH_SHA: ${{ github.sha }}
GITHUB_DEPENDENCY_GRAPH_WORKSPACE: ${{ github.workspace }}

- name: Save plugin JSON report
uses: actions/upload-artifact@v3
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ This causes 2 separate plugins to be applied, that can be used independently:
### Required environment variables

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:
- `GITHUB_JOB_CORRELATOR`: Sets the `job.correlator` value for the dependency submission
- `GITHUB_JOB_ID`: Sets the `job.id` value for the dependency submission
- `GITHUB_REF`: Sets the `ref` value for the dependency submission
- `GITHUB_SHA`: Sets the `sha` value for the dependency submission
- `GITHUB_WORKSPACE`: Sets the root directory of the github repository
- `GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR`: Sets the `job.correlator` value for the dependency submission
- `GITHUB_DEPENDENCY_GRAPH_JOB_ID`: Sets the `job.id` value for the dependency submission
- `GITHUB_DEPENDENCY_GRAPH_REF`: Sets the `ref` value for the commit that generated the dependency graph
- `GITHUB_DEPENDENCY_GRAPH_SHA`: Sets the `sha` value for the commit that generated the dependency graph
- `GITHUB_DEPENDENCY_GRAPH_WORKSPACE`: Sets the root directory of the github repository
- `DEPENDENCY_GRAPH_REPORT_DIR` (optional): Specifies where the dependency graph report will be generated

Each of these values can also be provided via a system property.
Expand Down
10 changes: 5 additions & 5 deletions plugin-self-test-local
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh

export GITHUB_JOB_ID="42"
export GITHUB_JOB_CORRELATOR="plugin-self-test"
export GITHUB_REF="refs/heads/main"
export GITHUB_SHA=$( git rev-parse HEAD )
export GITHUB_WORKSPACE=$( pwd )
export GITHUB_DEPENDENCY_GRAPH_JOB_ID="42"
export GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR="plugin-self-test"
export GITHUB_DEPENDENCY_GRAPH_REF="refs/heads/main"
export GITHUB_DEPENDENCY_GRAPH_SHA=$( git rev-parse HEAD )
export GITHUB_DEPENDENCY_GRAPH_WORKSPACE=$( pwd )

./plugin-self-test $*
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ abstract class BaseExtractorTest extends Specification {

Map<String, String> asEnvironmentMap() {
return [
"GITHUB_JOB_ID" : jobId,
"GITHUB_JOB_CORRELATOR": jobCorrelator,
"GITHUB_DEPENDENCY_GRAPH_JOB_ID" : jobId,
"GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR": jobCorrelator,
"DEPENDENCY_GRAPH_REPORT_DIR" : reportDir,
"GITHUB_REF" : ref,
"GITHUB_SHA" : sha,
"GITHUB_WORKSPACE" : workspace,
"GITHUB_DEPENDENCY_GRAPH_REF" : ref,
"GITHUB_DEPENDENCY_GRAPH_SHA" : sha,
"GITHUB_DEPENDENCY_GRAPH_WORKSPACE" : workspace,
"GITHUB_TOKEN" : gitHubToken
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class DependencyExtractorConfigTest extends BaseExtractorTest {

when:
executer
.withArgument("-DGITHUB_JOB_CORRELATOR=TEST_CORRELATOR")
.withArgument("-DGITHUB_REF=refs/my-branch/foo")
.withArgument("-DGITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR=TEST_CORRELATOR")
.withArgument("-DGITHUB_DEPENDENCY_GRAPH_REF=refs/my-branch/foo")
run()

then:
Expand Down Expand Up @@ -81,11 +81,11 @@ class DependencyExtractorConfigTest extends BaseExtractorTest {
def "fails gracefully if configuration values not set"() {
when:
def envVars = environmentVars.asEnvironmentMap()
envVars.remove("GITHUB_JOB_CORRELATOR")
envVars.remove("GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR")
executer.withEnvironmentVars(envVars)
def result = executer.runWithFailure()

then:
result.output.contains("'GITHUB_JOB_CORRELATOR' must be set")
result.output.contains("'GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR' must be set")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import org.gradle.dependencygraph.util.PluginParameters
import java.nio.file.Path
import java.nio.file.Paths

const val PARAM_JOB_ID = "GITHUB_JOB_ID"
const val PARAM_JOB_CORRELATOR = "GITHUB_JOB_CORRELATOR"
const val PARAM_GITHUB_REF = "GITHUB_REF"
const val PARAM_GITHUB_SHA = "GITHUB_SHA"
const val PARAM_JOB_ID = "GITHUB_DEPENDENCY_GRAPH_JOB_ID"
const val PARAM_JOB_CORRELATOR = "GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR"
const val PARAM_GITHUB_REF = "GITHUB_DEPENDENCY_GRAPH_REF"
const val PARAM_GITHUB_SHA = "GITHUB_DEPENDENCY_GRAPH_SHA"
/**
* Environment variable should be set to the workspace directory that the Git repository is checked out in.
* This is used to determine relative path to build files referenced in the dependency graph.
*/
const val PARAM_GITHUB_WORKSPACE = "GITHUB_WORKSPACE"
const val PARAM_GITHUB_WORKSPACE = "GITHUB_DEPENDENCY_GRAPH_WORKSPACE"

class GitHubSnapshotParams(private val pluginParameters: PluginParameters) {
val dependencyGraphJobCorrelator: String = pluginParameters.load(PARAM_JOB_CORRELATOR)
Expand Down

0 comments on commit e34f704

Please sign in to comment.