Skip to content

Commit 9b03779

Browse files
authored
feature: Support workflow_run GitHub Actions workflows (#492)
1 parent 87b0deb commit 9b03779

File tree

4 files changed

+398
-21
lines changed

4 files changed

+398
-21
lines changed

src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ object GitHubActionProvider extends CommitUUIDProvider with LazyLogger {
1717
}
1818

1919
override def getValidCommitUUID(environment: Map[String, String]): Either[String, CommitUUID] = {
20-
// if the event is a pull request the GITHUB_SHA will have a different commit UUID (the id of a merge commit)
20+
// if the event is a pull_request or a workflow_run the GITHUB_SHA will
21+
// have a different commit UUID (the id of a merge commit)
2122
// https://help.github.com/en/actions/reference/events-that-trigger-workflows
2223
// for this reason, we need to fetch it from the event details that GitHub provides
2324
// equivalent to doing ${{github.event.pull_request.head.sha}} in a GitHub action workflow
24-
if (environment.get("GITHUB_EVENT_NAME").contains("pull_request")) {
25-
getPullRequestCommit(environment)
26-
} else {
27-
parseEnvironmentVariable(environment.get("GITHUB_SHA"))
25+
environment.get("GITHUB_EVENT_NAME") match {
26+
case Some(eventName @ ("pull_request" | "workflow_run")) =>
27+
getEventCommitSha(environment, eventName)
28+
case _ =>
29+
parseEnvironmentVariable(environment.get("GITHUB_SHA"))
2830
}
2931
}
3032

31-
private def getPullRequestCommit(envVars: Map[String, String]): Either[String, CommitUUID] = {
33+
private def getEventCommitSha(envVars: Map[String, String], eventName: String): Either[String, CommitUUID] = {
3234
for {
3335
eventPath <- envVars.get("GITHUB_EVENT_PATH").toRight("Could not find event description file path")
3436
eventContent <- readFile(eventPath)
35-
sha <- extractHeadSHA(eventContent)
37+
sha <- extractHeadSHA(eventName = eventName, eventContent = eventContent)
3638
commitUUID <- CommitUUID.fromString(sha)
3739
} yield commitUUID
3840
}
@@ -49,9 +51,15 @@ object GitHubActionProvider extends CommitUUIDProvider with LazyLogger {
4951
}
5052
}
5153

52-
private def extractHeadSHA(event: String) = {
53-
val eventJson = ujson.read(event)
54-
Try(eventJson("pull_request")("head")("sha").str).toEither.left
55-
.map(t => s"Unable to fetch SHA from event file. Failed with error: ${t.getMessage}")
54+
private def extractHeadSHA(eventName: String, eventContent: String) = {
55+
Try {
56+
val eventJson = ujson.read(eventContent)
57+
eventName match {
58+
case "workflow_run" =>
59+
eventJson(eventName)("head_sha").str
60+
case _ =>
61+
eventJson(eventName)("head")("sha").str
62+
}
63+
}.toEither.left.map(t => s"Unable to fetch SHA from event file. Failed with error: ${t.getMessage}")
5664
}
5765
}

0 commit comments

Comments
 (0)