Skip to content

Commit

Permalink
fix: Fix commit uuid sent in github actions (#232)
Browse files Browse the repository at this point in the history
When using Github Actions and the on pull request trigger, the reporter was fetching an incorrect commit UUID
  • Loading branch information
Francisco Duarte authored Jun 18, 2020
1 parent 2c44843 commit 2bf4b6a
Show file tree
Hide file tree
Showing 5 changed files with 1,005 additions and 7 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ scalacOptions := Seq(
libraryDependencies ++= Seq(
"com.codacy" %% "coverage-parser" % "2.5.6",
"com.github.alexarchambault" %% "case-app" % "1.2.0",
"org.wvlet.airframe" %% "airframe-log" % "20.5.1"
"org.wvlet.airframe" %% "airframe-log" % "20.5.1",
"com.lihaoyi" %% "ujson" % "1.1.0"
)

// Test dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
package com.codacy.rules.commituuid.providers

import com.codacy.rules.commituuid.CommitUUIDProvider
import java.io.File
import java.nio.file.Files

import com.codacy.model.configuration.CommitUUID
import com.codacy.rules.commituuid.CommitUUIDProvider
import wvlet.log.LazyLogger

class GitHubActionProvider extends CommitUUIDProvider {
import scala.util.Try

class GitHubActionProvider extends CommitUUIDProvider with LazyLogger {
val name: String = "GitHub Actions"

override def validate(a: Map[String, String]): Boolean = {
a.get("CI").contains("true") && a.get("GITHUB_ACTIONS").contains("true")
override def validate(envVars: Map[String, String]): Boolean = {
envVars.get("CI").contains("true") && envVars.get("GITHUB_ACTIONS").contains("true")
}

override def getUUID(envVars: Map[String, String]): Either[String, CommitUUID] = {
// if the event is a pull request the GITHUB_SHA will have a different commit UUID (the id of a merge commit)
// https://help.github.com/en/actions/reference/events-that-trigger-workflows
// for this reason, we need to fetch it from the event details that GitHub provides
// equivalent to doing ${{github.event.pull_request.head.sha}} in a GitHub action workflow
if (envVars.get("GITHUB_EVENT_NAME").contains("pull_request")) {
getPullRequestCommit(envVars)
} else {
withErrorMessage(envVars.get("GITHUB_SHA"))
}
}

override def getUUID(a: Map[String, String]): Either[String, CommitUUID] =
withErrorMessage(a.get("GITHUB_SHA"))
private def getPullRequestCommit(envVars: Map[String, String]): Either[String, CommitUUID] = {
for {
eventPath <- envVars.get("GITHUB_EVENT_PATH").toRight("Could not find event description file path")
eventContent <- readFile(eventPath)
sha <- extractHeadSHA(eventContent)
} yield CommitUUID(sha)
}

private def readFile(path: String): Either[String, String] = {
val contentTry = Try {
val fileBytes = Files.readAllBytes(new File(path).toPath)
new String(fileBytes)
}

contentTry.toEither.left.map { ex =>
logger.error("Failed to read event file", ex)
s"Failed to read event file with error: ${ex.getMessage}"
}
}

private def extractHeadSHA(event: String) = {
val eventJson = ujson.read(event)
Try(eventJson("pull_request")("head")("sha").str).toEither.left
.map(t => s"Unable to fetch SHA from event file. Failed with error: ${t.getMessage}")
}
}
Loading

0 comments on commit 2bf4b6a

Please sign in to comment.