From c9c33b5b31392ef0dbee8e1cca7f74801ec33d55 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 6 Aug 2024 11:40:14 +0200 Subject: [PATCH] Add correlator input for matrix based jobs --- README.md | 11 ++++++++++ action.yml | 6 ++++++ sbt-plugin/src/main/contraband/input.contra | 3 +++ .../ch/epfl/scala/SubmitDependencyGraph.scala | 21 +++++++------------ .../ignore-scaladoc/build.sbt | 2 +- .../dependency-manifest/ignore-test/build.sbt | 2 +- .../ch/epfl/scala/JsonProtocolTests.scala | 4 ++-- src/main.ts | 5 ++++- 8 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6daa61f..4716309 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,17 @@ A list of space-separated names of configurations to ignore. The action will not Example of configurations are `compile`, `test`, `scala-tool`, `scala-doc-tool`. +### - `correlator` (optional) + +An optional identifier to distinguish between multiple dependency snapshots of the same type. +Defaults to the concatenation of the workflow name, the job id and the action id. + +Typically you would specify the correlator in a matrix-based job like this: + +```yaml + correlator: ${{ github.job }}-${{ matrix.directory }} +``` + #### - `token` (optional) GitHub Personal Access Token (PAT). Defaults to PAT provided by Action runner. diff --git a/action.yml b/action.yml index c902c6c..05c64de 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,12 @@ inputs: Example: `test scala-doc-tool` required: false default: '' + correlator: + description: | + An optional identifier to distinguish between multiple dependency snapshots of the same type. + Defaults to the concatenation of the workflow name, the job id and the action id. + required: false + default: '' on-resolve-failure: description: | Either 'error' or 'warning'. diff --git a/sbt-plugin/src/main/contraband/input.contra b/sbt-plugin/src/main/contraband/input.contra index 77fa2a6..78cc8b9 100644 --- a/sbt-plugin/src/main/contraband/input.contra +++ b/sbt-plugin/src/main/contraband/input.contra @@ -23,4 +23,7 @@ type DependencySnapshotInput { ## - "scala-doc-tool" to ignore the scaladoc dependencies ## - "scala-tool" to ignore the compiler dependencies ignoredConfigs: [String] + + ## The job correlator of the snapshot + correlator: String } diff --git a/sbt-plugin/src/main/scala/ch/epfl/scala/SubmitDependencyGraph.scala b/sbt-plugin/src/main/scala/ch/epfl/scala/SubmitDependencyGraph.scala index d4c96b8..deb06d0 100644 --- a/sbt-plugin/src/main/scala/ch/epfl/scala/SubmitDependencyGraph.scala +++ b/sbt-plugin/src/main/scala/ch/epfl/scala/SubmitDependencyGraph.scala @@ -42,7 +42,7 @@ object SubmitDependencyGraph { private def inputParser(state: State): Parser[DependencySnapshotInput] = Parsers.any.*.map { raw => val rawString = raw.mkString - if (rawString.isEmpty) DependencySnapshotInput(None, Vector.empty, Vector.empty) + if (rawString.isEmpty) DependencySnapshotInput(None, Vector.empty, Vector.empty, Some("")) else JsonParser .parseFromString(rawString) @@ -82,7 +82,8 @@ object SubmitDependencyGraph { } private def generateInternal(state: State): State = { - val snapshot = githubDependencySnapshot(state) + val input = state.attributes(githubSnapshotInputKey) + val snapshot = githubDependencySnapshot(state, input.correlator.getOrElse("")) val snapshotJson = CompactPrinter(Converter.toJsonUnsafe(snapshot)) val snapshotJsonFile = IO.withTemporaryFile("dependency-snapshot-", ".json", keepFile = true) { file => IO.write(file, snapshotJson) @@ -103,7 +104,8 @@ object SubmitDependencyGraph { ) ) val snapshotUrl = s"${githubApiUrl()}/repos/${githubRepository()}/dependency-graph/snapshots" - val job = githubJob() + val input = state.attributes(githubSnapshotInputKey) + val job = githubJob(input.correlator.getOrElse("")) val request = Gigahorse .url(snapshotUrl) .post(snapshotJsonFile) @@ -145,7 +147,7 @@ object SubmitDependencyGraph { throw new MessageOnlyException(message) } - private def githubDependencySnapshot(state: State): DependencySnapshot = { + private def githubDependencySnapshot(state: State, correlator: String): DependencySnapshot = { val detector = DetectorMetadata( SbtGithubDependencySubmission.name, SbtGithubDependencySubmission.homepage.map(_.toString).getOrElse(""), @@ -155,7 +157,7 @@ object SubmitDependencyGraph { val manifests = state.get(githubManifestsKey).get DependencySnapshot( 0, - githubJob(), + githubJob(correlator), githubSha(), githubRef(), detector, @@ -165,8 +167,7 @@ object SubmitDependencyGraph { ) } - private def githubJob(): Job = { - val correlator = s"${githubWorkflow()}_${githubJobName()}_${githubAction()}" + private def githubJob(correlator: String): Job = { val id = githubRunId val html_url = for { @@ -181,9 +182,6 @@ object SubmitDependencyGraph { throw new MessageOnlyException(s"Missing environment variable $name. This task must run in a Github Action.") } check("GITHUB_WORKSPACE") - check("GITHUB_WORKFLOW") - check("GITHUB_JOB") - check("GITHUB_ACTION") check("GITHUB_RUN_ID") check("GITHUB_SHA") check("GITHUB_REF") @@ -194,9 +192,6 @@ object SubmitDependencyGraph { } private def githubWorkspace(): String = Properties.envOrElse("GITHUB_WORKSPACE", "") - private def githubWorkflow(): String = Properties.envOrElse("GITHUB_WORKFLOW", "") - private def githubJobName(): String = Properties.envOrElse("GITHUB_JOB", "") - private def githubAction(): String = Properties.envOrElse("GITHUB_ACTION", "") private def githubRunId(): String = Properties.envOrElse("GITHUB_RUN_ID", "") private def githubSha(): String = Properties.envOrElse("GITHUB_SHA", "") private def githubRef(): String = Properties.envOrElse("GITHUB_REF", "") diff --git a/sbt-plugin/src/sbt-test/dependency-manifest/ignore-scaladoc/build.sbt b/sbt-plugin/src/sbt-test/dependency-manifest/ignore-scaladoc/build.sbt index af7df7c..6a2d421 100644 --- a/sbt-plugin/src/sbt-test/dependency-manifest/ignore-scaladoc/build.sbt +++ b/sbt-plugin/src/sbt-test/dependency-manifest/ignore-scaladoc/build.sbt @@ -17,7 +17,7 @@ inThisBuild( ) Global / ignoreScaladoc := { - val input = DependencySnapshotInput(None, Vector.empty, ignoredConfigs = Vector("scala-doc-tool")) + val input = DependencySnapshotInput(None, Vector.empty, ignoredConfigs = Vector("scala-doc-tool"), correlator = None) StateTransform(state => state.put(githubSnapshotInputKey, input)) } diff --git a/sbt-plugin/src/sbt-test/dependency-manifest/ignore-test/build.sbt b/sbt-plugin/src/sbt-test/dependency-manifest/ignore-test/build.sbt index 1e45f6a..08f4da5 100644 --- a/sbt-plugin/src/sbt-test/dependency-manifest/ignore-test/build.sbt +++ b/sbt-plugin/src/sbt-test/dependency-manifest/ignore-test/build.sbt @@ -17,7 +17,7 @@ inThisBuild( ) Global / ignoreTestConfig := { - val input = DependencySnapshotInput(None, Vector.empty, ignoredConfigs = Vector("test")) + val input = DependencySnapshotInput(None, Vector.empty, ignoredConfigs = Vector("test"), correlator = None) StateTransform(state => state.put(githubSnapshotInputKey, input)) } diff --git a/sbt-plugin/src/test/scala/ch/epfl/scala/JsonProtocolTests.scala b/sbt-plugin/src/test/scala/ch/epfl/scala/JsonProtocolTests.scala index 6eeffea..e3fdc07 100644 --- a/sbt-plugin/src/test/scala/ch/epfl/scala/JsonProtocolTests.scala +++ b/sbt-plugin/src/test/scala/ch/epfl/scala/JsonProtocolTests.scala @@ -30,7 +30,7 @@ class JsonProtocolTests extends FunSuite { import ch.epfl.scala.JsonProtocol._ val raw = Parser.parseUnsafe("{}") val obtained = Converter.fromJson[DependencySnapshotInput](raw).get - val expected = DependencySnapshotInput(None, Vector.empty, Vector.empty) + val expected = DependencySnapshotInput(None, Vector.empty, Vector.empty, None) assertEquals(obtained, expected) } @@ -38,7 +38,7 @@ class JsonProtocolTests extends FunSuite { import ch.epfl.scala.JsonProtocol._ val raw = Parser.parseUnsafe("""{"onResolveFailure": "warning"}""") val obtained = Converter.fromJson[DependencySnapshotInput](raw).get - val expected = DependencySnapshotInput(Some(OnFailure.warning), Vector.empty, Vector.empty) + val expected = DependencySnapshotInput(Some(OnFailure.warning), Vector.empty, Vector.empty, None) assertEquals(obtained, expected) } } diff --git a/src/main.ts b/src/main.ts index 4e6a937..013aff6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,7 +48,10 @@ async function run(): Promise { return } - const input = { ignoredModules, ignoredConfigs, onResolveFailure } + const correlatorInput = core.getInput('correlator') + const correlator = correlatorInput ? correlatorInput : `${github.context.workflow}_${github.context.job}_${github.context.action}` + + const input = { ignoredModules, ignoredConfigs, onResolveFailure, correlator } if (github.context.eventName === 'pull_request') { core.info('pull request, resetting sha')