Skip to content

Commit e414bdf

Browse files
authored
handle issue comment event for pr num (#14)
1 parent c7efdf8 commit e414bdf

File tree

3 files changed

+299
-4
lines changed

3 files changed

+299
-4
lines changed

pkg/pullrequest/client.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,24 @@ func NewClient(action *githubactions.Action, gh *github.Client) (Client, error)
9090
}
9191

9292
func getPRNumber(event map[string]any) (int, error) {
93-
number, ok := event["pull_request"].(map[string]any)["number"]
94-
if !ok {
95-
return 0, errors.New("cannot get pull_request number")
93+
getNumber := func(eventName string) (int, error) {
94+
eventField, ok := event[eventName]
95+
if !ok {
96+
return 0, errors.New("incorrect event type")
97+
}
98+
99+
number, ok := eventField.(map[string]any)["number"]
100+
if !ok {
101+
return 0, errors.New("cannot get pull_request number")
102+
}
103+
return int(number.(float64)), nil
104+
}
105+
106+
num, err := getNumber("pull_request")
107+
if err != nil {
108+
return getNumber("issue")
96109
}
97-
return int(number.(float64)), nil
110+
return num, err
98111
}
99112

100113
func getRepo(action *githubactions.Action, event map[string]any) (string, string) {

pkg/pullrequest/client_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pullrequest
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/sethvargo/go-githubactions"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func Test_getPRNumber(t *testing.T) {
15+
readEvent := func(eventName string) map[string]any {
16+
eventData, err := os.ReadFile(fmt.Sprintf("../../test/events/%s.json", eventName))
17+
require.NoError(t, err)
18+
var githubContext githubactions.GitHubContext
19+
require.NoError(t, json.Unmarshal(eventData, &githubContext.Event))
20+
return githubContext.Event
21+
}
22+
23+
t.Run("pull_request_event", func(t *testing.T) {
24+
prNum, err := getPRNumber(readEvent("pull-request.edited"))
25+
assert.NoError(t, err)
26+
assert.Equal(t, 2, prNum)
27+
})
28+
29+
t.Run("issue_comment_event", func(t *testing.T) {
30+
prNum, err := getPRNumber(readEvent("issue-comment.created"))
31+
assert.NoError(t, err)
32+
assert.Equal(t, 1, prNum)
33+
})
34+
}
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
{
2+
"action": "created",
3+
"issue": {
4+
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1",
5+
"repository_url": "https://api.github.com/repos/Codertocat/Hello-World",
6+
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/labels{/name}",
7+
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/comments",
8+
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/events",
9+
"html_url": "https://github.com/Codertocat/Hello-World/issues/1",
10+
"id": 444500041,
11+
"node_id": "MDU6SXNzdWU0NDQ1MDAwNDE=",
12+
"number": 1,
13+
"title": "Spelling error in the README file",
14+
"user": {
15+
"login": "Codertocat",
16+
"id": 21031067,
17+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
18+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
19+
"gravatar_id": "",
20+
"url": "https://api.github.com/users/Codertocat",
21+
"html_url": "https://github.com/Codertocat",
22+
"followers_url": "https://api.github.com/users/Codertocat/followers",
23+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
24+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
25+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
26+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
27+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
28+
"repos_url": "https://api.github.com/users/Codertocat/repos",
29+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
30+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
31+
"type": "User",
32+
"site_admin": false
33+
},
34+
"labels": [
35+
{
36+
"id": 1362934389,
37+
"node_id": "MDU6TGFiZWwxMzYyOTM0Mzg5",
38+
"url": "https://api.github.com/repos/Codertocat/Hello-World/labels/bug",
39+
"name": "bug",
40+
"color": "d73a4a",
41+
"default": true,
42+
"description": "Something isn't working"
43+
}
44+
],
45+
"state": "open",
46+
"locked": false,
47+
"assignee": null,
48+
"assignees": [],
49+
"milestone": null,
50+
"comments": 2,
51+
"created_at": "2021-01-28T22:17:31Z",
52+
"updated_at": "2021-01-29T05:00:42Z",
53+
"closed_at": null,
54+
"author_association": "OWNER",
55+
"active_lock_reason": null,
56+
"body": "",
57+
"reactions": {
58+
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/reactions",
59+
"total_count": 0,
60+
"+1": 0,
61+
"-1": 0,
62+
"laugh": 0,
63+
"hooray": 0,
64+
"confused": 0,
65+
"heart": 0,
66+
"rocket": 0,
67+
"eyes": 0
68+
},
69+
"performed_via_github_app": null,
70+
"draft": false
71+
},
72+
"comment": {
73+
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments/492700400",
74+
"html_url": "https://github.com/Codertocat/Hello-World/issues/1#issuecomment-492700400",
75+
"issue_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1",
76+
"id": 492700400,
77+
"node_id": "MDEyOklzc3VlQ29tbWVudDQ5MjcwMDQwMA==",
78+
"user": {
79+
"login": "Codertocat",
80+
"id": 21031067,
81+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
82+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
83+
"gravatar_id": "",
84+
"url": "https://api.github.com/users/Codertocat",
85+
"html_url": "https://github.com/Codertocat",
86+
"followers_url": "https://api.github.com/users/Codertocat/followers",
87+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
88+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
89+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
90+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
91+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
92+
"repos_url": "https://api.github.com/users/Codertocat/repos",
93+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
94+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
95+
"type": "User",
96+
"site_admin": false
97+
},
98+
"created_at": "2019-05-15T15:20:21Z",
99+
"updated_at": "2019-05-15T15:20:21Z",
100+
"author_association": "OWNER",
101+
"body": "You are totally right! I'll get this fixed right away.",
102+
"reactions": {
103+
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments/492700400/reactions",
104+
"total_count": 0,
105+
"+1": 0,
106+
"-1": 0,
107+
"laugh": 0,
108+
"hooray": 0,
109+
"confused": 0,
110+
"heart": 0,
111+
"rocket": 0,
112+
"eyes": 0
113+
},
114+
"performed_via_github_app": null
115+
},
116+
"repository": {
117+
"id": 186853002,
118+
"node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDI=",
119+
"name": "Hello-World",
120+
"full_name": "Codertocat/Hello-World",
121+
"private": false,
122+
"owner": {
123+
"login": "Codertocat",
124+
"id": 21031067,
125+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
126+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
127+
"gravatar_id": "",
128+
"url": "https://api.github.com/users/Codertocat",
129+
"html_url": "https://github.com/Codertocat",
130+
"followers_url": "https://api.github.com/users/Codertocat/followers",
131+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
132+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
133+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
134+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
135+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
136+
"repos_url": "https://api.github.com/users/Codertocat/repos",
137+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
138+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
139+
"type": "User",
140+
"site_admin": false
141+
},
142+
"html_url": "https://github.com/Codertocat/Hello-World",
143+
"description": null,
144+
"fork": false,
145+
"url": "https://api.github.com/repos/Codertocat/Hello-World",
146+
"forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks",
147+
"keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}",
148+
"collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}",
149+
"teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams",
150+
"hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks",
151+
"issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}",
152+
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/events",
153+
"assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}",
154+
"branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}",
155+
"tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags",
156+
"blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}",
157+
"git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}",
158+
"git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}",
159+
"trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}",
160+
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}",
161+
"languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages",
162+
"stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers",
163+
"contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors",
164+
"subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers",
165+
"subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription",
166+
"commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}",
167+
"git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}",
168+
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}",
169+
"issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}",
170+
"contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}",
171+
"compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}",
172+
"merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges",
173+
"archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}",
174+
"downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads",
175+
"issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}",
176+
"pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}",
177+
"milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}",
178+
"notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}",
179+
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}",
180+
"releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}",
181+
"deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments",
182+
"created_at": "2019-05-15T15:19:25Z",
183+
"updated_at": "2019-05-15T15:19:27Z",
184+
"pushed_at": "2019-05-15T15:20:13Z",
185+
"git_url": "git://github.com/Codertocat/Hello-World.git",
186+
"ssh_url": "git@github.com:Codertocat/Hello-World.git",
187+
"clone_url": "https://github.com/Codertocat/Hello-World.git",
188+
"svn_url": "https://github.com/Codertocat/Hello-World",
189+
"homepage": null,
190+
"size": 0,
191+
"stargazers_count": 0,
192+
"watchers_count": 0,
193+
"language": null,
194+
"has_issues": true,
195+
"has_projects": true,
196+
"has_downloads": true,
197+
"has_wiki": true,
198+
"has_pages": true,
199+
"forks_count": 0,
200+
"mirror_url": null,
201+
"archived": false,
202+
"disabled": false,
203+
"open_issues_count": 1,
204+
"license": null,
205+
"forks": 0,
206+
"open_issues": 1,
207+
"watchers": 0,
208+
"default_branch": "master",
209+
"is_template": false,
210+
"topics": [],
211+
"visibility": "public",
212+
"web_commit_signoff_required": false
213+
},
214+
"organization": {
215+
"login": "Octocoders",
216+
"id": 38302899,
217+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5",
218+
"url": "https://api.github.com/orgs/Octocoders",
219+
"repos_url": "https://api.github.com/orgs/Octocoders/repos",
220+
"events_url": "https://api.github.com/orgs/Octocoders/events",
221+
"hooks_url": "https://api.github.com/orgs/Octocoders/hooks",
222+
"issues_url": "https://api.github.com/orgs/Octocoders/issues",
223+
"members_url": "https://api.github.com/orgs/Octocoders/members{/member}",
224+
"public_members_url": "https://api.github.com/orgs/Octocoders/public_members{/member}",
225+
"avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4",
226+
"description": ""
227+
},
228+
"sender": {
229+
"login": "Codertocat",
230+
"id": 21031067,
231+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
232+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
233+
"gravatar_id": "",
234+
"url": "https://api.github.com/users/Codertocat",
235+
"html_url": "https://github.com/Codertocat",
236+
"followers_url": "https://api.github.com/users/Codertocat/followers",
237+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
238+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
239+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
240+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
241+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
242+
"repos_url": "https://api.github.com/users/Codertocat/repos",
243+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
244+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
245+
"type": "User",
246+
"site_admin": false
247+
}
248+
}

0 commit comments

Comments
 (0)