Skip to content

Add correlator input for matrix based jobs #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
3 changes: 3 additions & 0 deletions sbt-plugin/src/main/contraband/input.contra
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(""),
Expand All @@ -155,7 +157,7 @@ object SubmitDependencyGraph {
val manifests = state.get(githubManifestsKey).get
DependencySnapshot(
0,
githubJob(),
githubJob(correlator),
githubSha(),
githubRef(),
detector,
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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)
}

test("decode input with onResolveFailure: warning") {
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)
}
}
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ async function run(): Promise<void> {
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')
Expand Down
Loading