From 9b03779d55626efd4e5fbade68f8d875786beac3 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Mon, 25 Mar 2024 11:13:29 +0100 Subject: [PATCH] feature: Support workflow_run GitHub Actions workflows (#492) --- .../providers/GitHubActionProvider.scala | 30 +- ... => github-action-pull-request-event.json} | 0 .../github-action-workflow-run-event.json | 355 ++++++++++++++++++ .../providers/GitHubActionProviderSpec.scala | 34 +- 4 files changed, 398 insertions(+), 21 deletions(-) rename src/test/resources/{github-action-event.json => github-action-pull-request-event.json} (100%) create mode 100644 src/test/resources/github-action-workflow-run-event.json diff --git a/src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala b/src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala index a8f8c54e..9c1713ed 100644 --- a/src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala +++ b/src/main/scala/com/codacy/rules/commituuid/providers/GitHubActionProvider.scala @@ -17,22 +17,24 @@ object GitHubActionProvider extends CommitUUIDProvider with LazyLogger { } override def getValidCommitUUID(environment: 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) + // if the event is a pull_request or a workflow_run 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 (environment.get("GITHUB_EVENT_NAME").contains("pull_request")) { - getPullRequestCommit(environment) - } else { - parseEnvironmentVariable(environment.get("GITHUB_SHA")) + environment.get("GITHUB_EVENT_NAME") match { + case Some(eventName @ ("pull_request" | "workflow_run")) => + getEventCommitSha(environment, eventName) + case _ => + parseEnvironmentVariable(environment.get("GITHUB_SHA")) } } - private def getPullRequestCommit(envVars: Map[String, String]): Either[String, CommitUUID] = { + private def getEventCommitSha(envVars: Map[String, String], eventName: 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) + sha <- extractHeadSHA(eventName = eventName, eventContent = eventContent) commitUUID <- CommitUUID.fromString(sha) } yield commitUUID } @@ -49,9 +51,15 @@ object GitHubActionProvider extends CommitUUIDProvider with LazyLogger { } } - 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}") + private def extractHeadSHA(eventName: String, eventContent: String) = { + Try { + val eventJson = ujson.read(eventContent) + eventName match { + case "workflow_run" => + eventJson(eventName)("head_sha").str + case _ => + eventJson(eventName)("head")("sha").str + } + }.toEither.left.map(t => s"Unable to fetch SHA from event file. Failed with error: ${t.getMessage}") } } diff --git a/src/test/resources/github-action-event.json b/src/test/resources/github-action-pull-request-event.json similarity index 100% rename from src/test/resources/github-action-event.json rename to src/test/resources/github-action-pull-request-event.json diff --git a/src/test/resources/github-action-workflow-run-event.json b/src/test/resources/github-action-workflow-run-event.json new file mode 100644 index 00000000..445126fa --- /dev/null +++ b/src/test/resources/github-action-workflow-run-event.json @@ -0,0 +1,355 @@ +{ + "action": "completed", + "repository": { + "allow_forking": true, + "archive_url": "https://api.github.com/repos/lolgab/example-action/{archive_format}{/ref}", + "archived": false, + "assignees_url": "https://api.github.com/repos/lolgab/example-action/assignees{/user}", + "blobs_url": "https://api.github.com/repos/lolgab/example-action/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/lolgab/example-action/branches{/branch}", + "clone_url": "https://github.com/lolgab/example-action.git", + "collaborators_url": "https://api.github.com/repos/lolgab/example-action/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/lolgab/example-action/comments{/number}", + "commits_url": "https://api.github.com/repos/lolgab/example-action/commits{/sha}", + "compare_url": "https://api.github.com/repos/lolgab/example-action/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/lolgab/example-action/contents/{+path}", + "contributors_url": "https://api.github.com/repos/lolgab/example-action/contributors", + "created_at": "2024-03-23T14:00:41Z", + "default_branch": "master", + "deployments_url": "https://api.github.com/repos/lolgab/example-action/deployments", + "description": null, + "disabled": false, + "downloads_url": "https://api.github.com/repos/lolgab/example-action/downloads", + "events_url": "https://api.github.com/repos/lolgab/example-action/events", + "fork": false, + "forks": 0, + "forks_count": 0, + "forks_url": "https://api.github.com/repos/lolgab/example-action/forks", + "full_name": "lolgab/example-action", + "git_commits_url": "https://api.github.com/repos/lolgab/example-action/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/lolgab/example-action/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/lolgab/example-action/git/tags{/sha}", + "git_url": "git://github.com/lolgab/example-action.git", + "has_discussions": false, + "has_downloads": true, + "has_issues": true, + "has_pages": false, + "has_projects": true, + "has_wiki": true, + "homepage": null, + "hooks_url": "https://api.github.com/repos/lolgab/example-action/hooks", + "html_url": "https://github.com/lolgab/example-action", + "id": 776435845, + "is_template": false, + "issue_comment_url": "https://api.github.com/repos/lolgab/example-action/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/lolgab/example-action/issues/events{/number}", + "issues_url": "https://api.github.com/repos/lolgab/example-action/issues{/number}", + "keys_url": "https://api.github.com/repos/lolgab/example-action/keys{/key_id}", + "labels_url": "https://api.github.com/repos/lolgab/example-action/labels{/name}", + "language": null, + "languages_url": "https://api.github.com/repos/lolgab/example-action/languages", + "license": null, + "merges_url": "https://api.github.com/repos/lolgab/example-action/merges", + "milestones_url": "https://api.github.com/repos/lolgab/example-action/milestones{/number}", + "mirror_url": null, + "name": "example-action", + "node_id": "R_kgDOLkd4hQ", + "notifications_url": "https://api.github.com/repos/lolgab/example-action/notifications{?since,all,participating}", + "open_issues": 0, + "open_issues_count": 0, + "owner": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "private": false, + "pulls_url": "https://api.github.com/repos/lolgab/example-action/pulls{/number}", + "pushed_at": "2024-03-23T14:18:07Z", + "releases_url": "https://api.github.com/repos/lolgab/example-action/releases{/id}", + "size": 0, + "ssh_url": "git@github.com:lolgab/example-action.git", + "stargazers_count": 0, + "stargazers_url": "https://api.github.com/repos/lolgab/example-action/stargazers", + "statuses_url": "https://api.github.com/repos/lolgab/example-action/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/lolgab/example-action/subscribers", + "subscription_url": "https://api.github.com/repos/lolgab/example-action/subscription", + "svn_url": "https://github.com/lolgab/example-action", + "tags_url": "https://api.github.com/repos/lolgab/example-action/tags", + "teams_url": "https://api.github.com/repos/lolgab/example-action/teams", + "topics": [], + "trees_url": "https://api.github.com/repos/lolgab/example-action/git/trees{/sha}", + "updated_at": "2024-03-23T14:00:41Z", + "url": "https://api.github.com/repos/lolgab/example-action", + "visibility": "public", + "watchers": 0, + "watchers_count": 0, + "web_commit_signoff_required": false + }, + "sender": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "workflow": { + "badge_url": "https://github.com/lolgab/example-action/workflows/Test/badge.svg", + "created_at": "2024-03-23T14:01:28.000Z", + "html_url": "https://github.com/lolgab/example-action/blob/master/.github/workflows/test.yml", + "id": 90872286, + "name": "Test", + "node_id": "W_kwDOLkd4hc4Fapne", + "path": ".github/workflows/test.yml", + "state": "active", + "updated_at": "2024-03-23T14:01:28.000Z", + "url": "https://api.github.com/repos/lolgab/example-action/actions/workflows/90872286" + }, + "workflow_run": { + "actor": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "artifacts_url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298/artifacts", + "cancel_url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298/cancel", + "check_suite_id": 22033177759, + "check_suite_node_id": "CS_kwDOLkd4hc8AAAAFIUecnw", + "check_suite_url": "https://api.github.com/repos/lolgab/example-action/check-suites/22033177759", + "conclusion": "success", + "created_at": "2024-03-23T14:18:39Z", + "display_title": "Test", + "event": "workflow_dispatch", + "head_branch": "master", + "head_commit": { + "author": { + "email": "lorenzolespaul@gmail.com", + "name": "Lorenzo Gabriele" + }, + "committer": { + "email": "lorenzolespaul@gmail.com", + "name": "Lorenzo Gabriele" + }, + "id": "0f33f2554d99ce8c12ab126fc4e06bd2ad663e50", + "message": ".", + "timestamp": "2024-03-23T14:18:06Z", + "tree_id": "62239c695dd78309a20e128048b7ba01672c56c0" + }, + "head_repository": { + "archive_url": "https://api.github.com/repos/lolgab/example-action/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/lolgab/example-action/assignees{/user}", + "blobs_url": "https://api.github.com/repos/lolgab/example-action/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/lolgab/example-action/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/lolgab/example-action/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/lolgab/example-action/comments{/number}", + "commits_url": "https://api.github.com/repos/lolgab/example-action/commits{/sha}", + "compare_url": "https://api.github.com/repos/lolgab/example-action/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/lolgab/example-action/contents/{+path}", + "contributors_url": "https://api.github.com/repos/lolgab/example-action/contributors", + "deployments_url": "https://api.github.com/repos/lolgab/example-action/deployments", + "description": null, + "downloads_url": "https://api.github.com/repos/lolgab/example-action/downloads", + "events_url": "https://api.github.com/repos/lolgab/example-action/events", + "fork": false, + "forks_url": "https://api.github.com/repos/lolgab/example-action/forks", + "full_name": "lolgab/example-action", + "git_commits_url": "https://api.github.com/repos/lolgab/example-action/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/lolgab/example-action/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/lolgab/example-action/git/tags{/sha}", + "hooks_url": "https://api.github.com/repos/lolgab/example-action/hooks", + "html_url": "https://github.com/lolgab/example-action", + "id": 776435845, + "issue_comment_url": "https://api.github.com/repos/lolgab/example-action/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/lolgab/example-action/issues/events{/number}", + "issues_url": "https://api.github.com/repos/lolgab/example-action/issues{/number}", + "keys_url": "https://api.github.com/repos/lolgab/example-action/keys{/key_id}", + "labels_url": "https://api.github.com/repos/lolgab/example-action/labels{/name}", + "languages_url": "https://api.github.com/repos/lolgab/example-action/languages", + "merges_url": "https://api.github.com/repos/lolgab/example-action/merges", + "milestones_url": "https://api.github.com/repos/lolgab/example-action/milestones{/number}", + "name": "example-action", + "node_id": "R_kgDOLkd4hQ", + "notifications_url": "https://api.github.com/repos/lolgab/example-action/notifications{?since,all,participating}", + "owner": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "private": false, + "pulls_url": "https://api.github.com/repos/lolgab/example-action/pulls{/number}", + "releases_url": "https://api.github.com/repos/lolgab/example-action/releases{/id}", + "stargazers_url": "https://api.github.com/repos/lolgab/example-action/stargazers", + "statuses_url": "https://api.github.com/repos/lolgab/example-action/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/lolgab/example-action/subscribers", + "subscription_url": "https://api.github.com/repos/lolgab/example-action/subscription", + "tags_url": "https://api.github.com/repos/lolgab/example-action/tags", + "teams_url": "https://api.github.com/repos/lolgab/example-action/teams", + "trees_url": "https://api.github.com/repos/lolgab/example-action/git/trees{/sha}", + "url": "https://api.github.com/repos/lolgab/example-action" + }, + "head_sha": "0f33f2554d99ce8c12ab126fc4e06bd2ad663e50", + "html_url": "https://github.com/lolgab/example-action/actions/runs/8402413298", + "id": 8402413298, + "jobs_url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298/jobs", + "logs_url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298/logs", + "name": "Test", + "node_id": "WFR_kwLOLkd4hc8AAAAB9NKm8g", + "path": ".github/workflows/test.yml", + "previous_attempt_url": null, + "pull_requests": [], + "referenced_workflows": [], + "repository": { + "archive_url": "https://api.github.com/repos/lolgab/example-action/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/lolgab/example-action/assignees{/user}", + "blobs_url": "https://api.github.com/repos/lolgab/example-action/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/lolgab/example-action/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/lolgab/example-action/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/lolgab/example-action/comments{/number}", + "commits_url": "https://api.github.com/repos/lolgab/example-action/commits{/sha}", + "compare_url": "https://api.github.com/repos/lolgab/example-action/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/lolgab/example-action/contents/{+path}", + "contributors_url": "https://api.github.com/repos/lolgab/example-action/contributors", + "deployments_url": "https://api.github.com/repos/lolgab/example-action/deployments", + "description": null, + "downloads_url": "https://api.github.com/repos/lolgab/example-action/downloads", + "events_url": "https://api.github.com/repos/lolgab/example-action/events", + "fork": false, + "forks_url": "https://api.github.com/repos/lolgab/example-action/forks", + "full_name": "lolgab/example-action", + "git_commits_url": "https://api.github.com/repos/lolgab/example-action/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/lolgab/example-action/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/lolgab/example-action/git/tags{/sha}", + "hooks_url": "https://api.github.com/repos/lolgab/example-action/hooks", + "html_url": "https://github.com/lolgab/example-action", + "id": 776435845, + "issue_comment_url": "https://api.github.com/repos/lolgab/example-action/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/lolgab/example-action/issues/events{/number}", + "issues_url": "https://api.github.com/repos/lolgab/example-action/issues{/number}", + "keys_url": "https://api.github.com/repos/lolgab/example-action/keys{/key_id}", + "labels_url": "https://api.github.com/repos/lolgab/example-action/labels{/name}", + "languages_url": "https://api.github.com/repos/lolgab/example-action/languages", + "merges_url": "https://api.github.com/repos/lolgab/example-action/merges", + "milestones_url": "https://api.github.com/repos/lolgab/example-action/milestones{/number}", + "name": "example-action", + "node_id": "R_kgDOLkd4hQ", + "notifications_url": "https://api.github.com/repos/lolgab/example-action/notifications{?since,all,participating}", + "owner": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "private": false, + "pulls_url": "https://api.github.com/repos/lolgab/example-action/pulls{/number}", + "releases_url": "https://api.github.com/repos/lolgab/example-action/releases{/id}", + "stargazers_url": "https://api.github.com/repos/lolgab/example-action/stargazers", + "statuses_url": "https://api.github.com/repos/lolgab/example-action/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/lolgab/example-action/subscribers", + "subscription_url": "https://api.github.com/repos/lolgab/example-action/subscription", + "tags_url": "https://api.github.com/repos/lolgab/example-action/tags", + "teams_url": "https://api.github.com/repos/lolgab/example-action/teams", + "trees_url": "https://api.github.com/repos/lolgab/example-action/git/trees{/sha}", + "url": "https://api.github.com/repos/lolgab/example-action" + }, + "rerun_url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298/rerun", + "run_attempt": 1, + "run_number": 5, + "run_started_at": "2024-03-23T14:18:39Z", + "status": "completed", + "triggering_actor": { + "avatar_url": "https://avatars.githubusercontent.com/u/5793054?v=4", + "events_url": "https://api.github.com/users/lolgab/events{/privacy}", + "followers_url": "https://api.github.com/users/lolgab/followers", + "following_url": "https://api.github.com/users/lolgab/following{/other_user}", + "gists_url": "https://api.github.com/users/lolgab/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/lolgab", + "id": 5793054, + "login": "lolgab", + "node_id": "MDQ6VXNlcjU3OTMwNTQ=", + "organizations_url": "https://api.github.com/users/lolgab/orgs", + "received_events_url": "https://api.github.com/users/lolgab/received_events", + "repos_url": "https://api.github.com/users/lolgab/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/lolgab/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/lolgab/subscriptions", + "type": "User", + "url": "https://api.github.com/users/lolgab" + }, + "updated_at": "2024-03-23T14:18:52Z", + "url": "https://api.github.com/repos/lolgab/example-action/actions/runs/8402413298", + "workflow_id": 90872286, + "workflow_url": "https://api.github.com/repos/lolgab/example-action/actions/workflows/90872286" + } +} \ No newline at end of file diff --git a/src/test/scala/com/codacy/rules/commituuid/providers/GitHubActionProviderSpec.scala b/src/test/scala/com/codacy/rules/commituuid/providers/GitHubActionProviderSpec.scala index 971551fc..d782ca36 100644 --- a/src/test/scala/com/codacy/rules/commituuid/providers/GitHubActionProviderSpec.scala +++ b/src/test/scala/com/codacy/rules/commituuid/providers/GitHubActionProviderSpec.scala @@ -3,8 +3,11 @@ package com.codacy.rules.commituuid.providers import org.scalatest.{EitherValues, Matchers, WordSpec} class GitHubActionProviderSpec extends WordSpec with Matchers with EitherValues { - val validCommitUuid = "480b8293f4340216b8f630053a230e2867cd5c28" - val invalidCommitUuid = "65e4436280a41c721617cba15f33555d3122907e" + val validPullRequestCommitUuid = "480b8293f4340216b8f630053a230e2867cd5c28" + val invalidPullRequestCommitUuid = "65e4436280a41c721617cba15f33555d3122907e" + + val validWorkflowRunCommitUuid = "0f33f2554d99ce8c12ab126fc4e06bd2ad663e50" + "getUUID" should { "fail" when { "no environment variables are defined" in { @@ -21,8 +24,8 @@ class GitHubActionProviderSpec extends WordSpec with Matchers with EitherValues val provider = GitHubActionProvider val envVars = Map( "GITHUB_EVENT_NAME" -> "pull_request", - "GITHUB_SHA" -> invalidCommitUuid, - "GITHUB_EVENT_PATH" -> "src/test/resources/invalid-github-action-event.json" + "GITHUB_SHA" -> invalidPullRequestCommitUuid, + "GITHUB_EVENT_PATH" -> "src/test/resources/invalid-github-action-pull-request-event.json" ) val commitUuidEither = provider.getValidCommitUUID(envVars) commitUuidEither should be('left) @@ -31,21 +34,32 @@ class GitHubActionProviderSpec extends WordSpec with Matchers with EitherValues "succeed" when { "event is push and GITHUB_SHA has value" in { val provider = GitHubActionProvider - val envVars = Map("GITHUB_EVENT_NAME" -> "push", "GITHUB_SHA" -> validCommitUuid) + val envVars = Map("GITHUB_EVENT_NAME" -> "push", "GITHUB_SHA" -> validPullRequestCommitUuid) val commitUuidEither = provider.getValidCommitUUID(envVars) commitUuidEither should be('right) - commitUuidEither.right.value.value should be(validCommitUuid) + commitUuidEither.right.value.value should be(validPullRequestCommitUuid) } - "even is pull request and json file includes needed information" in { + "event is pull_request and json file includes needed information" in { val provider = GitHubActionProvider val envVars = Map( "GITHUB_EVENT_NAME" -> "pull_request", - "GITHUB_SHA" -> invalidCommitUuid, - "GITHUB_EVENT_PATH" -> "src/test/resources/github-action-event.json" + "GITHUB_SHA" -> invalidPullRequestCommitUuid, + "GITHUB_EVENT_PATH" -> "src/test/resources/github-action-pull-request-event.json" + ) + val commitUuidEither = provider.getValidCommitUUID(envVars) + commitUuidEither should be('right) + commitUuidEither.right.value.value should be(validPullRequestCommitUuid) + } + "event is workflow_run and json file includes needed information" in { + val provider = GitHubActionProvider + val envVars = Map( + "GITHUB_EVENT_NAME" -> "workflow_run", + "GITHUB_SHA" -> invalidPullRequestCommitUuid, + "GITHUB_EVENT_PATH" -> "src/test/resources/github-action-workflow-run-event.json" ) val commitUuidEither = provider.getValidCommitUUID(envVars) commitUuidEither should be('right) - commitUuidEither.right.value.value should be(validCommitUuid) + commitUuidEither.right.value.value should be(validWorkflowRunCommitUuid) } } }