From 3f2b4242e63e0bd42fa6dff18756c2e46b168a66 Mon Sep 17 00:00:00 2001 From: Daisuke Fujita Date: Mon, 29 Jun 2020 00:13:00 +0900 Subject: [PATCH] Add support for GitHub Actions workflow by Pull Request --- ci.go | 17 +++++++++++++++++ ci_test.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ci.go b/ci.go index e6159c6..9cf6d5d 100644 --- a/ci.go +++ b/ci.go @@ -8,6 +8,11 @@ import ( "strings" ) +var ( + // https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request + githubActionsPRRefRegexp = regexp.MustCompile(`refs/pull/\d+/merge`) +) + // CI represents a common information obtained from all CI platforms type CI struct { PR PullRequest @@ -144,5 +149,17 @@ func githubActions() (ci CI, err error) { os.Getenv("GITHUB_RUN_ID"), ) ci.PR.Revision = os.Getenv("GITHUB_SHA") + ci.PR.Number = 0 + + if githubActionsPRRefRegexp.MatchString(os.Getenv("GITHUB_REF")) { + s := strings.Split(os.Getenv("GITHUB_REF"), "/")[2] + pr, err := strconv.Atoi(s) + if err != nil { + return ci, err + } + + ci.PR.Number = pr + } + return ci, err } diff --git a/ci_test.go b/ci_test.go index 7bd29bb..b4661b1 100644 --- a/ci_test.go +++ b/ci_test.go @@ -746,6 +746,22 @@ func TestGitHubActions(t *testing.T) { }, ok: true, }, + { + fn: func() { + os.Setenv("GITHUB_SHA", "abcdefg") + os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify") + os.Setenv("GITHUB_RUN_ID", "12345") + os.Setenv("GITHUB_REF", "refs/pull/123/merge") + }, + ci: CI{ + PR: PullRequest{ + Revision: "abcdefg", + Number: 123, + }, + URL: "https://github.com/mercari/tfnotify/actions/runs/12345", + }, + ok: true, + }, } for _, testCase := range testCases {