Skip to content

Commit

Permalink
Add support for GitHub Actions workflow by Pull Request
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed Jun 28, 2020
1 parent c8c8bae commit 3f2b424
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3f2b424

Please sign in to comment.