Skip to content

Commit

Permalink
Can work on issue_comment events as "trigger phrase"
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuniwak committed Jun 28, 2021
1 parent 196460a commit 6e5db5c
Show file tree
Hide file tree
Showing 7 changed files with 698 additions and 23 deletions.
46 changes: 37 additions & 9 deletions tool/gh-action/inputs/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"os"
)

type ReadEventPayloadFunc func(path typedpath.RawPath) (*PushOrPullRequestEventPayload, error)
type ReadEventPayloadFunc func(path typedpath.RawPath) (*EventPayload, error)

func NewReadEventPayload(logger logging.Logger) ReadEventPayloadFunc {
return func(path typedpath.RawPath) (*PushOrPullRequestEventPayload, error) {
return func(path typedpath.RawPath) (*EventPayload, error) {
payloadBytes, err := os.ReadFile(string(path))
if err != nil {
return nil, errors.Wrapf(err, "cannot read file: %q", path)
}

logger.Debug(fmt.Sprintf("event=%s", string(payloadBytes)))

var payload PushOrPullRequestEventPayload
var payload EventPayload
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return nil, errors.Wrapf(err, "cannot decode file: %q\n%s", path, string(payloadBytes))
}
Expand All @@ -30,26 +30,54 @@ func NewReadEventPayload(logger logging.Logger) ReadEventPayloadFunc {
}
}

// PushOrPullRequestEventPayload is a payload for pull request events.
// SEE: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
type PushOrPullRequestEventPayload struct {
type EventPayload struct {
// PullRequest is a payload for pull request related events.
// SEE: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
PullRequest *PullRequest `json:"pull_request,omitempty"`
Repository *Repository `json:"repository,omitempty"`

// Issue is a payload for issue related events.
// SEE: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#issue_comment
Issue *Issue `json:"issue"`

// Repository is the repository where the event occurred.
Repository *Repository `json:"repository,omitempty"`
}

// PullRequest is a payload for pull requests.
// PullRequest is a payload for GitHub pull requests.
// SEE: https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
type PullRequest struct {
Number github.PullNumber `json:"number"`
}

// Repository is a payload for repository.
// Repository is a payload for GitHub repositories.
// SEE: https://docs.github.com/en/rest/reference/repos#get-a-repository
type Repository struct {
Name github.Repo `json:"name"`
Owner User `json:"owner"`
}

type IssueNumber int

// Issue is a payload for GitHub issue related events.
// SEE: https://docs.github.com/en/rest/reference/issues#get-an-issue
type Issue struct {
Number IssueNumber `json:"number"`

// PullRequest exists if the issue is a pull request
PullRequest *IssuePullRequest `json:"pull_request,omitempty"`
}

func (i Issue) GetPullNumber() (github.PullNumber, error) {
if i.PullRequest != nil {
return github.PullNumber(i.Number), nil
}
return 0, fmt.Errorf("not a pull request: %d", i.Number)
}

type IssuePullRequest struct {
URL string `json:"url"`
}

// User is a payload for users.
type User struct {
Login github.Owner `json:"login"`
Expand Down
4 changes: 2 additions & 2 deletions tool/gh-action/inputs/event_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/DeNA/unity-meta-check/util/typedpath"
)

func StubReadEventPayload(payload *PushOrPullRequestEventPayload, err error) ReadEventPayloadFunc {
return func(_ typedpath.RawPath) (*PushOrPullRequestEventPayload, error) {
func StubReadEventPayload(payload *EventPayload, err error) ReadEventPayloadFunc {
return func(_ typedpath.RawPath) (*EventPayload, error) {
return payload, err
}
}
34 changes: 29 additions & 5 deletions tool/gh-action/inputs/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
func TestEventPayload(t *testing.T) {
cases := map[string]struct {
Path string
Expected PushOrPullRequestEventPayload
Expected EventPayload
}{
"pull request": {
Path: "./testdata/pr-event-payload-example.json",
Expected: PushOrPullRequestEventPayload{
Expected: EventPayload{
PullRequest: &PullRequest{
Number: 2,
},
Expand All @@ -27,8 +27,32 @@ func TestEventPayload(t *testing.T) {
},
"push": {
Path: "./testdata/push-event-payload-example.json",
Expected: PushOrPullRequestEventPayload{
PullRequest: nil,
Expected: EventPayload{
Repository: &Repository{
Name: "Hello-World",
Owner: User{Login: "Codertocat"},
},
},
},
"issue comment (comment to not pull request)": {
Path: "./testdata/issue-comment-to-issue-payload-example.json",
Expected: EventPayload{
Issue: &Issue{
Number: 1,
},
Repository: &Repository{
Name: "Hello-World",
Owner: User{Login: "Codertocat"},
},
},
},
"issue comment (comment to pull request)": {
Path: "./testdata/issue-comment-to-pr-payload-example.json",
Expected: EventPayload{
Issue: &Issue{
Number: 1,
PullRequest: &IssuePullRequest{URL: "https://api.github.com/repos/Codertocat/Hello-World/pulls/1"},
},
Repository: &Repository{
Name: "Hello-World",
Owner: User{Login: "Codertocat"},
Expand All @@ -45,7 +69,7 @@ func TestEventPayload(t *testing.T) {
return
}

var payload PushOrPullRequestEventPayload
var payload EventPayload
if err := json.Unmarshal(jsonBytes, &payload); err != nil {
t.Errorf("want nil, got %#v", err)
return
Expand Down
Loading

0 comments on commit 6e5db5c

Please sign in to comment.