Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,31 @@ object SubmitDependencyGraph {
"Authorization" -> s"token ${githubToken()}"
)

state.log.info(s"Submitting dependency snapshot of job $job to $snapshotUrl")
state.log.info(s"Submitting dependency snapshot of job $job to GitHub Dependency Graph API")
val result = for {
httpResp <- Try(Await.result(http.processFull(request), Duration.Inf))
snapshot <- getSnapshot(httpResp)
} yield {
state.log.info(s"Submitted successfully as $snapshotUrl/${snapshot.id}")
setGithubOutputs(
"submission-id" -> s"${snapshot.id}",
"submission-api-url" -> s"${snapshotUrl}/${snapshot.id}"
)
successMessages(snapshot.id, httpResp.bodyAsString).foreach(state.log.info(_))
setGithubOutputs(successOutputs(snapshotUrl, snapshot.id): _*)
state
}

result.get
}

private[scala] def successMessages(snapshotId: Long, responseBody: String): Seq[String] =
Seq(
s"Submitted successfully, submission id: $snapshotId",
s"GitHub submission response: $responseBody"
)

private[scala] def successOutputs(snapshotUrl: String, snapshotId: Long): Seq[(String, String)] =
Seq(
"submission-id" -> s"$snapshotId",
"submission-api-url" -> s"$snapshotUrl/$snapshotId"
)

// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
private def setGithubOutputs(outputs: (String, String)*): Unit =
for (output <- githubOutput())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.epfl.scala

import ch.epfl.scala.SubmitDependencyGraph.successMessages
import ch.epfl.scala.SubmitDependencyGraph.successOutputs
import munit.FunSuite

class SubmitDependencyGraphTests extends FunSuite {

test("successMessages shows submission id and response body") {
val body =
"""{"id":123,"created_at":"2024-05-31T00:11:22.957Z","result":"ACCEPTED","message":"The snapshot was accepted, but it is not for the default branch. It will not update dependency results for the repository."}"""

val res = successMessages(123L, body)

assertEquals(
res,
Seq(
"Submitted successfully, submission id: 123",
s"GitHub submission response: $body"
)
)
}

test("successMessages does not contain fake success url") {
val body = """{"id":123,"result":"ACCEPTED","message":"ok"}"""

val res = successMessages(123L, body).mkString("\n")

assert(!res.contains("/dependency-graph/snapshots/123"))
}

test("successOutputs preserves submission-id and url") {
val res = successOutputs(
"https://api.github.com/repos/foo/bar/dependency-graph/snapshots",
123L
)

assertEquals(
res,
Seq(
"submission-id" -> "123",
"submission-api-url" -> "https://api.github.com/repos/foo/bar/dependency-graph/snapshots/123"
)
)
}
}
Loading