diff --git a/ci.go b/ci.go index 2719296..28930e1 100644 --- a/ci.go +++ b/ci.go @@ -12,6 +12,11 @@ const ( defaultCloudBuildRegion = "global" ) +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 @@ -148,6 +153,18 @@ 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 1bf7e34..6a32797 100644 --- a/ci_test.go +++ b/ci_test.go @@ -713,6 +713,7 @@ func TestGitHubActions(t *testing.T) { "GITHUB_SHA", "GITHUB_REPOSITORY", "GITHUB_RUN_ID", + "GITHUB_REF", } saveEnvs := make(map[string]string) for _, key := range envs { @@ -746,6 +747,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 {