-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix commit uuid sent in github actions (#232)
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
Showing
5 changed files
with
1,005 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 47 additions & 6 deletions
53
src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |
Oops, something went wrong.