Skip to content

Commit

Permalink
handle issue comment event for pr num (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ authored Feb 23, 2023
1 parent c7efdf8 commit e414bdf
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/pullrequest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,24 @@ func NewClient(action *githubactions.Action, gh *github.Client) (Client, error)
}

func getPRNumber(event map[string]any) (int, error) {
number, ok := event["pull_request"].(map[string]any)["number"]
if !ok {
return 0, errors.New("cannot get pull_request number")
getNumber := func(eventName string) (int, error) {
eventField, ok := event[eventName]
if !ok {
return 0, errors.New("incorrect event type")
}

number, ok := eventField.(map[string]any)["number"]
if !ok {
return 0, errors.New("cannot get pull_request number")
}
return int(number.(float64)), nil
}

num, err := getNumber("pull_request")
if err != nil {
return getNumber("issue")
}
return int(number.(float64)), nil
return num, err
}

func getRepo(action *githubactions.Action, event map[string]any) (string, string) {
Expand Down
34 changes: 34 additions & 0 deletions pkg/pullrequest/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pullrequest

import (
"encoding/json"
"fmt"
"os"
"testing"

"github.com/sethvargo/go-githubactions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_getPRNumber(t *testing.T) {
readEvent := func(eventName string) map[string]any {
eventData, err := os.ReadFile(fmt.Sprintf("../../test/events/%s.json", eventName))
require.NoError(t, err)
var githubContext githubactions.GitHubContext
require.NoError(t, json.Unmarshal(eventData, &githubContext.Event))
return githubContext.Event
}

t.Run("pull_request_event", func(t *testing.T) {
prNum, err := getPRNumber(readEvent("pull-request.edited"))
assert.NoError(t, err)
assert.Equal(t, 2, prNum)
})

t.Run("issue_comment_event", func(t *testing.T) {
prNum, err := getPRNumber(readEvent("issue-comment.created"))
assert.NoError(t, err)
assert.Equal(t, 1, prNum)
})
}
248 changes: 248 additions & 0 deletions test/events/issue-comment.created.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"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,
"description": "Something isn't working"
}
],
"state": "open",
"locked": false,
"assignee": null,
"assignees": [],
"milestone": null,
"comments": 2,
"created_at": "2021-01-28T22:17:31Z",
"updated_at": "2021-01-29T05:00:42Z",
"closed_at": null,
"author_association": "OWNER",
"active_lock_reason": null,
"body": "",
"reactions": {
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"performed_via_github_app": null,
"draft": false
},
"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.",
"reactions": {
"url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments/492700400/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"performed_via_github_app": null
},
"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",
"is_template": false,
"topics": [],
"visibility": "public",
"web_commit_signoff_required": false
},
"organization": {
"login": "Octocoders",
"id": 38302899,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5",
"url": "https://api.github.com/orgs/Octocoders",
"repos_url": "https://api.github.com/orgs/Octocoders/repos",
"events_url": "https://api.github.com/orgs/Octocoders/events",
"hooks_url": "https://api.github.com/orgs/Octocoders/hooks",
"issues_url": "https://api.github.com/orgs/Octocoders/issues",
"members_url": "https://api.github.com/orgs/Octocoders/members{/member}",
"public_members_url": "https://api.github.com/orgs/Octocoders/public_members{/member}",
"avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4",
"description": ""
},
"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
}
}

0 comments on commit e414bdf

Please sign in to comment.