Skip to content

Commit d0c5d88

Browse files
committed
Allow GitHub Actions to notify as pull request comments
1 parent 95e7534 commit d0c5d88

File tree

2 files changed

+173
-3
lines changed

2 files changed

+173
-3
lines changed

ci.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io/ioutil"
57
"os"
68
"regexp"
79
"strconv"
@@ -20,6 +22,20 @@ type PullRequest struct {
2022
Number int
2123
}
2224

25+
type gitHubActionsEventIssue struct {
26+
Number int `json:"number"`
27+
}
28+
29+
type gitHubActionsEventPullRequest struct {
30+
Number int `json:"number"`
31+
}
32+
33+
type gitHubActionsEventPayload struct {
34+
Issue *gitHubActionsEventIssue `json:"issue"`
35+
PullRequest *gitHubActionsEventPullRequest `json:"pull_request"`
36+
Number int `json:"number"`
37+
}
38+
2339
func circleci() (ci CI, err error) {
2440
ci.PR.Number = 0
2541
ci.PR.Revision = os.Getenv("CIRCLE_SHA1")
@@ -144,6 +160,30 @@ func githubActions() (ci CI, err error) {
144160
os.Getenv("GITHUB_RUN_ID"),
145161
)
146162
ci.PR.Revision = os.Getenv("GITHUB_SHA")
163+
164+
// Extract the pull request number from the event payload that triggered the current workflow.
165+
// See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
166+
ci.PR.Number = 0
167+
eventPath := os.Getenv("GITHUB_EVENT_PATH")
168+
if eventPath != "" {
169+
bytes, err := ioutil.ReadFile(eventPath)
170+
if err != nil {
171+
return ci, err
172+
}
173+
174+
eventPayload := gitHubActionsEventPayload{}
175+
if err := json.Unmarshal(bytes, &eventPayload); err != nil {
176+
return ci, err
177+
}
178+
179+
if eventPayload.Issue != nil {
180+
ci.PR.Number = eventPayload.Issue.Number
181+
} else if eventPayload.PullRequest != nil {
182+
ci.PR.Number = eventPayload.PullRequest.Number
183+
} else {
184+
ci.PR.Number = eventPayload.Number
185+
}
186+
}
147187
return ci, err
148188
}
149189

ci_test.go

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"os"
56
"reflect"
67
"testing"
@@ -708,11 +709,32 @@ func TestGitLabCI(t *testing.T) {
708709
}
709710
}
710711

712+
func createTempfile(content string) (*os.File, error) {
713+
tempfile, err := ioutil.TempFile("", "")
714+
if err != nil {
715+
return tempfile, err
716+
}
717+
718+
_, err = tempfile.Write([]byte(content))
719+
if err != nil {
720+
return tempfile, err
721+
}
722+
723+
err = tempfile.Close()
724+
if err != nil {
725+
return tempfile, err
726+
}
727+
728+
return tempfile, nil
729+
}
730+
711731
func TestGitHubActions(t *testing.T) {
732+
712733
envs := []string{
713734
"GITHUB_SHA",
714735
"GITHUB_REPOSITORY",
715736
"GITHUB_RUN_ID",
737+
"GITHUB_EVENT_PATH",
716738
}
717739
saveEnvs := make(map[string]string)
718740
for _, key := range envs {
@@ -727,15 +749,40 @@ func TestGitHubActions(t *testing.T) {
727749

728750
// https://help.github.com/ja/actions/configuring-and-managing-workflows/using-environment-variables
729751
testCases := []struct {
730-
fn func()
752+
fn func() func()
731753
ci CI
732754
ok bool
733755
}{
734756
{
735-
fn: func() {
757+
fn: func() func() {
758+
os.Setenv("GITHUB_SHA", "abcdefg")
759+
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
760+
os.Setenv("GITHUB_RUN_ID", "12345")
761+
os.Setenv("GITHUB_EVENT_PATH", "")
762+
763+
return func() {}
764+
},
765+
ci: CI{
766+
PR: PullRequest{
767+
Revision: "abcdefg",
768+
Number: 0,
769+
},
770+
URL: "https://github.com/mercari/tfnotify/actions/runs/12345",
771+
},
772+
ok: true,
773+
},
774+
{
775+
fn: func() func() {
736776
os.Setenv("GITHUB_SHA", "abcdefg")
737777
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
738778
os.Setenv("GITHUB_RUN_ID", "12345")
779+
780+
tempfile, _ := createTempfile("{}")
781+
os.Setenv("GITHUB_EVENT_PATH", tempfile.Name())
782+
783+
return func() {
784+
os.Remove(tempfile.Name())
785+
}
739786
},
740787
ci: CI{
741788
PR: PullRequest{
@@ -746,10 +793,93 @@ func TestGitHubActions(t *testing.T) {
746793
},
747794
ok: true,
748795
},
796+
{
797+
fn: func() func() {
798+
os.Setenv("GITHUB_SHA", "abcdefg")
799+
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
800+
os.Setenv("GITHUB_RUN_ID", "12345")
801+
802+
tempfile, _ := createTempfile(`
803+
{
804+
"issue": {
805+
"number": 123
806+
}
807+
}
808+
`)
809+
os.Setenv("GITHUB_EVENT_PATH", tempfile.Name())
810+
811+
return func() {
812+
os.Remove(tempfile.Name())
813+
}
814+
},
815+
ci: CI{
816+
PR: PullRequest{
817+
Revision: "abcdefg",
818+
Number: 123,
819+
},
820+
URL: "https://github.com/mercari/tfnotify/actions/runs/12345",
821+
},
822+
ok: true,
823+
},
824+
{
825+
fn: func() func() {
826+
os.Setenv("GITHUB_SHA", "abcdefg")
827+
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
828+
os.Setenv("GITHUB_RUN_ID", "12345")
829+
830+
tempfile, _ := createTempfile(`
831+
{
832+
"pull_request": {
833+
"number": 234
834+
}
835+
}
836+
`)
837+
os.Setenv("GITHUB_EVENT_PATH", tempfile.Name())
838+
839+
return func() {
840+
os.Remove(tempfile.Name())
841+
}
842+
},
843+
ci: CI{
844+
PR: PullRequest{
845+
Revision: "abcdefg",
846+
Number: 234,
847+
},
848+
URL: "https://github.com/mercari/tfnotify/actions/runs/12345",
849+
},
850+
ok: true,
851+
},
852+
{
853+
fn: func() func() {
854+
os.Setenv("GITHUB_SHA", "abcdefg")
855+
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
856+
os.Setenv("GITHUB_RUN_ID", "12345")
857+
858+
tempfile, _ := createTempfile(`
859+
{
860+
"number": 345
861+
}
862+
`)
863+
os.Setenv("GITHUB_EVENT_PATH", tempfile.Name())
864+
865+
return func() {
866+
os.Remove(tempfile.Name())
867+
}
868+
},
869+
ci: CI{
870+
PR: PullRequest{
871+
Revision: "abcdefg",
872+
Number: 345,
873+
},
874+
URL: "https://github.com/mercari/tfnotify/actions/runs/12345",
875+
},
876+
ok: true,
877+
},
749878
}
750879

751880
for _, testCase := range testCases {
752-
testCase.fn()
881+
teardown := testCase.fn()
882+
defer teardown()
753883
ci, err := githubActions()
754884
if !reflect.DeepEqual(ci, testCase.ci) {
755885
t.Errorf("got %q but want %q", ci, testCase.ci)

0 commit comments

Comments
 (0)