Skip to content

Commit 1a31624

Browse files
committed
fix(exec): support issues event on pull request
1 parent 3feabeb commit 1a31624

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ build:
88
go build -o bin/actions ./cmd
99

1010
test/integration:
11-
GITHUB_EVENT_NAME=issues GITHUB_EVENT_PATH=testdata/issues_event.json bin/actions pullvet
11+
echo aaa
12+
GITHUB_EVENT_NAME=issues GITHUB_EVENT_PATH=testdata/issues_event.json bin/actions exec -status-context milestone -- bin/actions pullvet -require-any -milestone-match 'test-v.+' label milestone/none
1213

1314
test:
1415
go test ./...

event.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ func IssueEvent() (*github.IssuesEvent, error) {
7272
return evt.(*github.IssuesEvent), nil
7373
}
7474

75+
func GetPullRequest(issue *github.IssuesEvent) (*github.PullRequest, error) {
76+
client, err := CreateInstallationTokenClient(os.Getenv("GITHUB_TOKEN"), "", "")
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
if issue.Issue.GetPullRequestLinks().GetURL() == "" {
82+
return nil, fmt.Errorf("issue %d is not a pull request", issue.Issue.GetNumber())
83+
}
84+
85+
// This can be a pull_request milestoned/demilestoned events emitted as issue event
86+
owner := issue.Repo.Owner.GetLogin()
87+
repo := issue.Repo.GetName()
88+
pull, _, err := client.PullRequests.Get(context.Background(), owner, repo, issue.Issue.GetNumber())
89+
if err != nil {
90+
return nil, err
91+
}
92+
return pull, nil
93+
}
94+
7595
func PullRequest() (*github.PullRequest, string, string, error) {
7696
var pr *github.PullRequest
7797
var owner, repo string
@@ -82,22 +102,11 @@ func PullRequest() (*github.PullRequest, string, string, error) {
82102
return nil, "", "", err
83103
}
84104

85-
client, err := CreateInstallationTokenClient(os.Getenv("GITHUB_TOKEN"), "", "")
105+
pull, err := GetPullRequest(issue)
86106
if err != nil {
87107
return nil, "", "", err
88108
}
89109

90-
if issue.Issue.GetPullRequestLinks().GetURL() == "" {
91-
return nil, "", "", fmt.Errorf("issue %d is not a pull request", issue.Issue.GetNumber())
92-
}
93-
94-
// This can be a pull_request milestoned/demilestoned events emitted as issue event
95-
owner := issue.Repo.Owner.GetLogin()
96-
repo := issue.Repo.GetName()
97-
pull, _, err := client.PullRequests.Get(context.Background(), owner, repo, issue.Issue.GetNumber())
98-
if err != nil {
99-
return nil, "", "", err
100-
}
101110
pr = pull
102111
case "pull_request":
103112
pull, err := PullRequestEvent()

pkg/exec/exec.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type Command struct {
2828
args []string
2929
}
3030

31+
type Target struct {
32+
Owner, Repo string
33+
PullRequest *github.PullRequest
34+
}
35+
3136
func New() *Command {
3237
return &Command{
3338
BaseURL: "",
@@ -62,8 +67,26 @@ func (c *Command) Run(args []string) error {
6267

6368
func (c *Command) HandleEvent(payload interface{}) error {
6469
switch e := payload.(type) {
70+
case *github.IssuesEvent:
71+
pull, err := actions.GetPullRequest(e)
72+
if err != nil {
73+
return err
74+
}
75+
target := &Target{
76+
Owner: e.Repo.Owner.GetLogin(),
77+
Repo: e.Repo.GetName(),
78+
PullRequest: pull,
79+
}
80+
return c.EnsureCheckRun(target)
6581
case *github.PullRequestEvent:
66-
return c.EnsureCheckRun(e)
82+
owner := e.Repo.Owner.GetLogin()
83+
repo := e.Repo.GetName()
84+
target := &Target{
85+
Owner: owner,
86+
Repo: repo,
87+
PullRequest: e.PullRequest,
88+
}
89+
return c.EnsureCheckRun(target)
6790
}
6891
return nil
6992
}
@@ -96,7 +119,7 @@ func (c *Command) createCheckRun(suite *github.CheckSuite, cr Run) (*github.Chec
96119
return created, err
97120
}
98121

99-
func (c *Command) EnsureCheckRun(pre *github.PullRequestEvent) error {
122+
func (c *Command) EnsureCheckRun(pre *Target) error {
100123
client, err := c.instTokenClient()
101124
if err != nil {
102125
return err
@@ -106,8 +129,8 @@ func (c *Command) EnsureCheckRun(pre *github.PullRequestEvent) error {
106129

107130
summary, text, runErr := c.runIt()
108131

109-
owner := pre.Repo.Owner.GetLogin()
110-
repo := pre.Repo.GetName()
132+
owner := pre.Owner
133+
repo := pre.Repo
111134

112135
if c.checkRunName != "" {
113136
suite, err := c.EnsureCheckSuite(pre)
@@ -212,7 +235,7 @@ func (c *Command) logCheckRun(checkRun *github.CheckRun) {
212235
log.Printf("CheckRun:\n%s", buf.String())
213236
}
214237

215-
func (c *Command) EnsureCheckSuite(pre *github.PullRequestEvent) (*github.CheckSuite, error) {
238+
func (c *Command) EnsureCheckSuite(pre *Target) (*github.CheckSuite, error) {
216239
return c.getOneOfSuitesAlreadyCreatedByGitHubActions(pre)
217240
}
218241

@@ -288,13 +311,13 @@ func (c *Command) logResponseAndError(suites *github.ListCheckSuiteResults, res
288311
return nil
289312
}
290313

291-
func (c *Command) getOneOfSuitesAlreadyCreatedByGitHubActions(pre *github.PullRequestEvent) (*github.CheckSuite, error) {
314+
func (c *Command) getOneOfSuitesAlreadyCreatedByGitHubActions(pre *Target) (*github.CheckSuite, error) {
292315
client, err := c.instTokenClient()
293316
if err != nil {
294317
return nil, fmt.Errorf("Failed to create a new installation token client: %s", err)
295318
}
296319

297-
owner, repo := pre.GetRepo().GetOwner().GetLogin(), pre.GetRepo().GetName()
320+
owner, repo := pre.Owner, pre.Repo
298321

299322
sha := pre.PullRequest.Head.GetSHA()
300323

0 commit comments

Comments
 (0)