Skip to content
Open
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 @@ -200,23 +200,41 @@ private class CustomBuildScanEnhancements(serverConfig: Server, logger: Logger)
}
}

private val PullRequestRefPattern = """^(\d+)/merge$""".r

private def captureGitHubActions(implicit env: Env): BuildScan => BuildScan = {
if (!isGitHubActions) identity
else {
val serverUrl = env.envVariable[URL]("GITHUB_SERVER_URL")
val gitRepository = env.envVariable[String]("GITHUB_REPOSITORY")

val buildUrl = for {
url <- env.envVariable[URL]("GITHUB_SERVER_URL")
repository <- env.envVariable[String]("GITHUB_REPOSITORY")
url <- serverUrl
repository <- gitRepository
runId <- env.envVariable[String]("GITHUB_RUN_ID")
} yield sbt.url(s"$url/$repository/actions/runs/$runId")

val headRef = env.envVariable[String]("GITHUB_HEAD_REF").filter(_.nonEmpty)
val pullRequestUrl = for {
url <- serverUrl
repository <- gitRepository
_ <- headRef
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it required for headRef to be Some? If it's indeed required, I'd lift this out and make it a conditional for the whole expression, i.e.,

if (headRef.nonEmpty) {
  val pullRequestUrl = for { ... 
}

This because I believe it makes the intended logic a lot more apparent and easier to reason about.

refName <- env.envVariable[String]("GITHUB_REF_NAME")
prNumber <- refName match {
case PullRequestRefPattern(num) => Some(num)
case _ => None
}
} yield sbt.url(s"$url/$repository/pull/$prNumber")

val ops = Seq(
(bs: BuildScan) => bs.withValue("CI provider", "GitHub Actions"),
ifDefined(buildUrl)(_.withLink("GitHub Actions build", _)),
ifDefined(pullRequestUrl)(_.withLink("GitHub pull request", _)),
ifDefined(env.envVariable[String]("GITHUB_WORKFLOW"))(withCustomValueAndSearchLink(_, "CI workflow", _)),
ifDefined(env.envVariable[String]("GITHUB_RUN_ID"))(withCustomValueAndSearchLink(_, "CI run", _)),
ifDefined(env.envVariable[String]("GITHUB_ACTION"))(withCustomValueAndSearchLink(_, "CI step", _)),
ifDefined(env.envVariable[String]("GITHUB_JOB"))(withCustomValueAndSearchLink(_, "CI job", _)),
ifDefined(env.envVariable[String]("GITHUB_HEAD_REF").filter(_.nonEmpty))(_.withValue("PR branch", _))
ifDefined(headRef)(_.withValue("PR branch", _))
)
Function.chain(ops)
}
Expand Down
Loading