Skip to content

Commit

Permalink
Merge pull request #77 from mercari/dtan4/support-github-actions-pr
Browse files Browse the repository at this point in the history
Add support for GitHub Actions workflow triggered by Pull Request
  • Loading branch information
KeisukeYamashita authored Apr 8, 2022
2 parents a2897c6 + bc087e4 commit 4d9ad90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 17 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4d9ad90

Please sign in to comment.