From 6e5db5ca27a0126c17e814fbdaa17b71cd3cc4e8 Mon Sep 17 00:00:00 2001 From: Kuniwak Date: Mon, 28 Jun 2021 18:13:31 +0900 Subject: [PATCH] Can work on issue_comment events as "trigger phrase" --- tool/gh-action/inputs/event.go | 46 ++- tool/gh-action/inputs/event_stub.go | 4 +- tool/gh-action/inputs/event_test.go | 34 ++- ...ssue-comment-to-issue-payload-example.json | 277 +++++++++++++++++ .../issue-comment-to-pr-payload-example.json | 283 ++++++++++++++++++ tool/gh-action/runner/options.go | 14 +- tool/gh-action/runner/options_test.go | 63 +++- 7 files changed, 698 insertions(+), 23 deletions(-) create mode 100644 tool/gh-action/inputs/testdata/issue-comment-to-issue-payload-example.json create mode 100644 tool/gh-action/inputs/testdata/issue-comment-to-pr-payload-example.json diff --git a/tool/gh-action/inputs/event.go b/tool/gh-action/inputs/event.go index 05713b7..a4befb0 100644 --- a/tool/gh-action/inputs/event.go +++ b/tool/gh-action/inputs/event.go @@ -10,10 +10,10 @@ 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) @@ -21,7 +21,7 @@ func NewReadEventPayload(logger logging.Logger) ReadEventPayloadFunc { 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)) } @@ -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"` diff --git a/tool/gh-action/inputs/event_stub.go b/tool/gh-action/inputs/event_stub.go index a942411..573983c 100644 --- a/tool/gh-action/inputs/event_stub.go +++ b/tool/gh-action/inputs/event_stub.go @@ -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 } } diff --git a/tool/gh-action/inputs/event_test.go b/tool/gh-action/inputs/event_test.go index 7956c6e..00adf1a 100644 --- a/tool/gh-action/inputs/event_test.go +++ b/tool/gh-action/inputs/event_test.go @@ -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, }, @@ -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"}, @@ -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 diff --git a/tool/gh-action/inputs/testdata/issue-comment-to-issue-payload-example.json b/tool/gh-action/inputs/testdata/issue-comment-to-issue-payload-example.json new file mode 100644 index 0000000..24de7ee --- /dev/null +++ b/tool/gh-action/inputs/testdata/issue-comment-to-issue-payload-example.json @@ -0,0 +1,277 @@ +{ + "action": "created", + "issue": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1", + "repository_url": "https://api.github.com/repos/Codertocat/Hello-World", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/labels{/name}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/comments", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/events", + "html_url": "https://github.com/Codertocat/Hello-World/issues/1", + "id": 444500041, + "node_id": "MDU6SXNzdWU0NDQ1MDAwNDE=", + "number": 1, + "title": "Spelling error in the README file", + "user": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "labels": [ + { + "id": 1362934389, + "node_id": "MDU6TGFiZWwxMzYyOTM0Mzg5", + "url": "https://api.github.com/repos/Codertocat/Hello-World/labels/bug", + "name": "bug", + "color": "d73a4a", + "default": true + } + ], + "state": "open", + "locked": false, + "assignee": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "assignees": [ + { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } + ], + "milestone": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/milestones/1", + "html_url": "https://github.com/Codertocat/Hello-World/milestone/1", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones/1/labels", + "id": 4317517, + "node_id": "MDk6TWlsZXN0b25lNDMxNzUxNw==", + "number": 1, + "title": "v1.0", + "description": "Add new space flight simulator", + "creator": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "open_issues": 1, + "closed_issues": 0, + "state": "closed", + "created_at": "2019-05-15T15:20:17Z", + "updated_at": "2019-05-15T15:20:18Z", + "due_on": "2019-05-23T07:00:00Z", + "closed_at": "2019-05-15T15:20:18Z" + }, + "comments": 0, + "created_at": "2019-05-15T15:20:18Z", + "updated_at": "2019-05-15T15:20:21Z", + "closed_at": null, + "author_association": "OWNER", + "body": "It looks like you accidently spelled 'commit' with two 't's." + }, + "comment": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments/492700400", + "html_url": "https://github.com/Codertocat/Hello-World/issues/1#issuecomment-492700400", + "issue_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1", + "id": 492700400, + "node_id": "MDEyOklzc3VlQ29tbWVudDQ5MjcwMDQwMA==", + "user": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "created_at": "2019-05-15T15:20:21Z", + "updated_at": "2019-05-15T15:20:21Z", + "author_association": "OWNER", + "body": "You are totally right! I'll get this fixed right away." + }, + "repository": { + "id": 186853002, + "node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDI=", + "name": "Hello-World", + "full_name": "Codertocat/Hello-World", + "private": false, + "owner": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "html_url": "https://github.com/Codertocat/Hello-World", + "description": null, + "fork": false, + "url": "https://api.github.com/repos/Codertocat/Hello-World", + "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", + "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", + "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", + "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", + "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", + "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", + "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", + "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", + "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", + "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", + "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", + "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", + "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", + "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", + "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", + "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", + "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", + "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", + "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", + "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", + "created_at": "2019-05-15T15:19:25Z", + "updated_at": "2019-05-15T15:19:27Z", + "pushed_at": "2019-05-15T15:20:13Z", + "git_url": "git://github.com/Codertocat/Hello-World.git", + "ssh_url": "git@github.com:Codertocat/Hello-World.git", + "clone_url": "https://github.com/Codertocat/Hello-World.git", + "svn_url": "https://github.com/Codertocat/Hello-World", + "homepage": null, + "size": 0, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": true, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 1, + "license": null, + "forks": 0, + "open_issues": 1, + "watchers": 0, + "default_branch": "master" + }, + "sender": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } +} \ No newline at end of file diff --git a/tool/gh-action/inputs/testdata/issue-comment-to-pr-payload-example.json b/tool/gh-action/inputs/testdata/issue-comment-to-pr-payload-example.json new file mode 100644 index 0000000..ff36436 --- /dev/null +++ b/tool/gh-action/inputs/testdata/issue-comment-to-pr-payload-example.json @@ -0,0 +1,283 @@ +{ + "action": "created", + "issue": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1", + "repository_url": "https://api.github.com/repos/Codertocat/Hello-World", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/labels{/name}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/comments", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/events", + "html_url": "https://github.com/Codertocat/Hello-World/issues/1", + "id": 444500041, + "node_id": "MDU6SXNzdWU0NDQ1MDAwNDE=", + "number": 1, + "title": "Spelling error in the README file", + "user": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "labels": [ + { + "id": 1362934389, + "node_id": "MDU6TGFiZWwxMzYyOTM0Mzg5", + "url": "https://api.github.com/repos/Codertocat/Hello-World/labels/bug", + "name": "bug", + "color": "d73a4a", + "default": true + } + ], + "state": "open", + "locked": false, + "assignee": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "assignees": [ + { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } + ], + "milestone": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/milestones/1", + "html_url": "https://github.com/Codertocat/Hello-World/milestone/1", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones/1/labels", + "id": 4317517, + "node_id": "MDk6TWlsZXN0b25lNDMxNzUxNw==", + "number": 1, + "title": "v1.0", + "description": "Add new space flight simulator", + "creator": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "open_issues": 1, + "closed_issues": 0, + "state": "closed", + "created_at": "2019-05-15T15:20:17Z", + "updated_at": "2019-05-15T15:20:18Z", + "due_on": "2019-05-23T07:00:00Z", + "closed_at": "2019-05-15T15:20:18Z" + }, + "comments": 0, + "created_at": "2019-05-15T15:20:18Z", + "updated_at": "2019-05-15T15:20:21Z", + "closed_at": null, + "author_association": "OWNER", + "pull_request": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/pulls/1", + "html_url": "https://github.com/Codertocat/Hello-World/pull/1", + "diff_url": "https://github.com/Codertocat/Hello-World/pull/1.diff", + "patch_url": "https://github.com/Codertocat/Hello-World/pull/1.patch" + }, + "body": "It looks like you accidently spelled 'commit' with two 't's." + }, + "comment": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments/492700400", + "html_url": "https://github.com/Codertocat/Hello-World/issues/1#issuecomment-492700400", + "issue_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1", + "id": 492700400, + "node_id": "MDEyOklzc3VlQ29tbWVudDQ5MjcwMDQwMA==", + "user": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "created_at": "2019-05-15T15:20:21Z", + "updated_at": "2019-05-15T15:20:21Z", + "author_association": "OWNER", + "body": "You are totally right! I'll get this fixed right away." + }, + "repository": { + "id": 186853002, + "node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDI=", + "name": "Hello-World", + "full_name": "Codertocat/Hello-World", + "private": false, + "owner": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "html_url": "https://github.com/Codertocat/Hello-World", + "description": null, + "fork": false, + "url": "https://api.github.com/repos/Codertocat/Hello-World", + "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", + "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", + "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", + "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", + "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", + "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", + "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", + "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", + "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", + "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", + "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", + "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", + "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", + "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", + "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", + "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", + "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", + "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", + "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", + "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", + "created_at": "2019-05-15T15:19:25Z", + "updated_at": "2019-05-15T15:19:27Z", + "pushed_at": "2019-05-15T15:20:13Z", + "git_url": "git://github.com/Codertocat/Hello-World.git", + "ssh_url": "git@github.com:Codertocat/Hello-World.git", + "clone_url": "https://github.com/Codertocat/Hello-World.git", + "svn_url": "https://github.com/Codertocat/Hello-World", + "homepage": null, + "size": 0, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": true, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 1, + "license": null, + "forks": 0, + "open_issues": 1, + "watchers": 0, + "default_branch": "master" + }, + "sender": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } +} \ No newline at end of file diff --git a/tool/gh-action/runner/options.go b/tool/gh-action/runner/options.go index 19834a9..bbc6f6f 100644 --- a/tool/gh-action/runner/options.go +++ b/tool/gh-action/runner/options.go @@ -94,8 +94,18 @@ func NewValidateFunc( return nil, err } + var pullNumber prcomment.PullNumber if eventPayload.PullRequest == nil { - return nil, fmt.Errorf("pull request comment can work only if triggered by pull request events, but triggered a not pull request event") + if eventPayload.Issue == nil { + return nil, fmt.Errorf("pull request comment can work only if triggered by pull_request events or issue_comment events, but it was not triggered by these event") + } + + pullNumber, err = eventPayload.Issue.GetPullNumber() + if err != nil { + return nil, errors.Wrap(err, "pull request comment can work by issue_comment events if the issue is a pull request, but the issue is not a pull request") + } + } else { + pullNumber = eventPayload.PullRequest.Number } var tmpl *l10n.Template @@ -126,7 +136,7 @@ func NewValidateFunc( APIEndpoint: apiEndpoint, Owner: eventPayload.Repository.Owner.Login, Repo: eventPayload.Repository.Name, - PullNumber: eventPayload.PullRequest.Number, + PullNumber: pullNumber, } } diff --git a/tool/gh-action/runner/options_test.go b/tool/gh-action/runner/options_test.go index e21b6f2..690a004 100644 --- a/tool/gh-action/runner/options_test.go +++ b/tool/gh-action/runner/options_test.go @@ -39,7 +39,7 @@ func TestNewValidateFunc(t *testing.T) { Env inputs.ActionEnv DetectedTargetType checker.TargetType BuiltIgnoredGlobs []globs.Glob - ReadPayload *inputs.PushOrPullRequestEventPayload + ReadPayload *inputs.EventPayload ReadTmpl *l10n.Template Expected *Options }{ @@ -205,7 +205,7 @@ func TestNewValidateFunc(t *testing.T) { EnableAutofix: false, }, }, - "enable pr-comment with lang": { + "enable pr-comment with lang triggered by a pull_request event": { Inputs: inputs.Inputs{ LogLevel: "INFO", TargetPath: typedpath.NewRootRawPath("path", "to", "project"), @@ -222,7 +222,7 @@ func TestNewValidateFunc(t *testing.T) { }, DetectedTargetType: checker.TargetTypeIsUnityProjectRootDirectory, BuiltIgnoredGlobs: []globs.Glob{}, - ReadPayload: &inputs.PushOrPullRequestEventPayload{ + ReadPayload: &inputs.EventPayload{ PullRequest: &inputs.PullRequest{Number: 2}, Repository: &inputs.Repository{ Name: "Hello-World", @@ -256,7 +256,7 @@ func TestNewValidateFunc(t *testing.T) { }, }, }, - "enable pr-comment with a template file": { + "enable pr-comment with a template file triggered by a pull_request event": { Inputs: inputs.Inputs{ LogLevel: "INFO", TargetPath: typedpath.NewRootRawPath("path", "to", "project"), @@ -274,7 +274,7 @@ func TestNewValidateFunc(t *testing.T) { DetectedTargetType: checker.TargetTypeIsUnityProjectRootDirectory, BuiltIgnoredGlobs: []globs.Glob{}, ReadTmpl: tmpl, - ReadPayload: &inputs.PushOrPullRequestEventPayload{ + ReadPayload: &inputs.EventPayload{ PullRequest: &inputs.PullRequest{Number: 2}, Repository: &inputs.Repository{ Name: "Hello-World", @@ -308,6 +308,59 @@ func TestNewValidateFunc(t *testing.T) { }, }, }, + "enable pr-comment with lang triggered by an issue_comment event": { + Inputs: inputs.Inputs{ + LogLevel: "INFO", + TargetPath: typedpath.NewRootRawPath("path", "to", "project"), + TargetType: "auto-detect", + EnablePRComment: true, + PRCommentLang: "en", + }, + Env: inputs.ActionEnv{ + GitHubToken: "T0K3N", + EventPath: typedpath.NewRootRawPath("github", "workflow", "event.json"), + Workspace: typedpath.NewRootRawPath("github", "workspace"), + APIURL: "https://api.github.com", + }, + DetectedTargetType: checker.TargetTypeIsUnityProjectRootDirectory, + BuiltIgnoredGlobs: []globs.Glob{}, + ReadPayload: &inputs.EventPayload{ + Issue: &inputs.Issue{ + Number: 123, + PullRequest: &inputs.IssuePullRequest{URL: "https://api.github.com/Codertocat/Hello-World/pulls/123"}, + }, + Repository: &inputs.Repository{ + Name: "Hello-World", + Owner: inputs.User{Login: "Codertocat"}, + }, + }, + Expected: &Options{ + RootDirAbs: typedpath.NewRootRawPath("path", "to", "project"), + CheckerOpts: &checker.Options{ + IgnoreCase: false, + IgnoreSubmodulesAndNested: false, + // NOTE: same as the value of DetectedTargetType. + TargetType: checker.TargetTypeIsUnityProjectRootDirectory, + }, + FilterOpts: &resultfilter.Options{ + IgnoreDangling: false, + // NOTE: same as the value of BuiltIgnoredGlobs. + IgnoredGlobs: []globs.Glob{}, + IgnoreCase: false, + }, + EnableJUnit: false, + EnablePRComment: true, + PRCommentOpts: &github.Options{ + Tmpl: &l10n.En, + SendIfSuccess: false, + Token: "T0K3N", + APIEndpoint: githubComAPIEndpoint, + Owner: "Codertocat", + Repo: "Hello-World", + PullNumber: 123, + }, + }, + }, "enable autofix": { RootDirRel: ".", Inputs: inputs.Inputs{