Skip to content

Commit 4474cbe

Browse files
Merge pull request code-check#60 from ashawley/pr-event
Improve PullRequest and IssueEvent with tests
2 parents b59734f + 8a90d10 commit 4474cbe

File tree

6 files changed

+739
-0
lines changed

6 files changed

+739
-0
lines changed

src/main/scala/codecheck/github/events/IssueEvent.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package codecheck.github.events
33
import org.json4s.JValue
44
import codecheck.github.models.AbstractJson
55
import codecheck.github.models.Issue
6+
import codecheck.github.models.IssueAction
67
import codecheck.github.models.Comment
78

89
case class IssueEvent(name: String, value: JValue) extends AbstractJson(value) with GitHubEvent {
10+
11+
lazy val action = IssueAction.fromString(get("action"))
12+
lazy val issue = Issue(value \ "issue")
913
}

src/main/scala/codecheck/github/models/Issue.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ object IssueInput {
129129
IssueInput(Some(title), body, assignee, milestone, labels, None)
130130
}
131131

132+
sealed abstract class IssueAction(val name: String) {
133+
override def toString = name
134+
}
135+
136+
object IssueAction {
137+
case object assigned extends IssueAction("assigned")
138+
case object unassigned extends IssueAction("unassigned")
139+
case object labeled extends IssueAction("labeled")
140+
case object unlabeled extends IssueAction("unlabeled")
141+
case object opened extends IssueAction("opened")
142+
case object edited extends IssueAction("edited")
143+
case object closed extends IssueAction("closed")
144+
case object reopened extends IssueAction("reopened")
145+
146+
val values = Array(
147+
assigned,
148+
unassigned,
149+
labeled,
150+
unlabeled,
151+
opened,
152+
closed,
153+
reopened
154+
)
155+
156+
def fromString(str: String) = values.filter(_.name == str).head
157+
}
158+
132159
case class Issue(value: JValue) extends AbstractJson(value) {
133160
def url = get("url")
134161
def labels_url = get("labels_url")

src/main/scala/codecheck/github/models/PullRequest.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ object PullRequestAction {
1919
case object labeled extends PullRequestAction("labeled")
2020
case object unlabeled extends PullRequestAction("unlabeled")
2121
case object opened extends PullRequestAction("opened")
22+
case object edited extends PullRequestAction("edited")
2223
case object closed extends PullRequestAction("closed")
2324
case object reopened extends PullRequestAction("reopened")
2425
case object synchronize extends PullRequestAction("synchronize")
@@ -37,10 +38,20 @@ object PullRequestAction {
3738
def fromString(str: String) = values.filter(_.name == str).head
3839
}
3940

41+
case class PullRequestRef(value: JValue) extends AbstractJson(value) {
42+
def label = get("label")
43+
def ref = get("ref")
44+
def sha = get("sha")
45+
lazy val user = User(value \ "user")
46+
lazy val repo = Repository(value \ "repo")
47+
}
48+
4049
case class PullRequest(value: JValue) extends AbstractJson(value) {
4150
def number = get("number").toLong
4251
def body = get("body")
4352
def state = get("state")
4453
def title = get("title")
54+
lazy val head = PullRequestRef(value \ "head")
55+
lazy val base = PullRequestRef(value \ "base")
4556
}
4657

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package codecheck.github
2+
package events
3+
4+
import org.scalatest.FunSpec
5+
import org.scalatest.Inside
6+
import org.scalatest.Matchers
7+
8+
class GitHubEventSpec extends FunSpec with Matchers with Inside
9+
with IssueEventJson
10+
with PullRequestEventJson {
11+
12+
describe("GitHubEvent(issue, JValue)") {
13+
val event = GitHubEvent("issue", issueEventJson)
14+
15+
it("should yield IssueEvent") {
16+
event shouldBe a [IssueEvent]
17+
}
18+
describe("IssueEvent") {
19+
inside(event) {
20+
case e @ IssueEvent(name, _) =>
21+
it("should have a name") {
22+
assert(name === "issue")
23+
}
24+
it("should have an action") {
25+
assert(e.action === models.IssueAction.opened)
26+
}
27+
it("should have an issue") {
28+
e.issue shouldBe a [models.Issue]
29+
}
30+
describe("Issue") {
31+
val issue = e.issue
32+
it("should have a number") {
33+
assert(issue.number === 2l)
34+
}
35+
it("should have a title") {
36+
assert(issue.title === "Spelling error in the README file")
37+
}
38+
it("should have a state") {
39+
assert(issue.state === "open")
40+
}
41+
it("should have a body") {
42+
val exp = "It looks like you accidently spelled 'commit' with two 't's."
43+
assert(issue.body === Some(exp))
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
describe("GitHubEvent(pull_request, JValue)") {
51+
val event = GitHubEvent("pull_request", pullRequestEventJson)
52+
53+
it("should yield PullRequestEvent") {
54+
event shouldBe a [PullRequestEvent]
55+
}
56+
describe("PullRequest") {
57+
inside(event) {
58+
case e @ PullRequestEvent(name, _) =>
59+
it("should have a name") {
60+
assert(name === "pull_request")
61+
}
62+
it("should have a number") {
63+
assert(e.number === 1l)
64+
}
65+
it("should have an action") {
66+
assert(e.action === models.PullRequestAction.opened)
67+
}
68+
it("should have a pull request") {
69+
e.pull_request shouldBe a [models.PullRequest]
70+
}
71+
describe("PullRequest") {
72+
val pr = e.pull_request
73+
it("should have a number") {
74+
assert(pr.number === 1l)
75+
}
76+
it("should have a title") {
77+
assert(pr.title === "Update the README with new information")
78+
}
79+
it("should have a state") {
80+
assert(pr.state === "open")
81+
}
82+
it("should have a body") {
83+
val exp = "This is a pretty simple change that we need to pull into master."
84+
assert(pr.body === exp)
85+
}
86+
it("should have a head") {
87+
pr.head shouldBe a [models.PullRequestRef]
88+
}
89+
describe("PullRequestRef") {
90+
val head = pr.head
91+
it("should have a label") {
92+
assert(head.label === "baxterthehacker:changes")
93+
}
94+
it("should have a ref") {
95+
assert(head.ref === "changes")
96+
}
97+
it("should have a sha") {
98+
assert(head.sha === "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c")
99+
}
100+
it("should have a user") {
101+
head.user shouldBe a [models.User]
102+
}
103+
it("should have a repo") {
104+
head.repo shouldBe a [models.Repository]
105+
}
106+
}
107+
it("should have a base") {
108+
pr.base shouldBe a [models.PullRequestRef]
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package codecheck.github.events
2+
3+
import org.json4s.jackson.JsonMethods
4+
5+
trait IssueEventJson {
6+
7+
val issueEventJson = JsonMethods.parse(
8+
"""
9+
|{
10+
| "action": "opened",
11+
| "issue": {
12+
| "url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/2",
13+
| "labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/2/labels{/name}",
14+
| "comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/2/comments",
15+
| "events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/2/events",
16+
| "html_url": "https://github.com/baxterthehacker/public-repo/issues/2",
17+
| "id": 73464126,
18+
| "number": 2,
19+
| "title": "Spelling error in the README file",
20+
| "user": {
21+
| "login": "baxterthehacker",
22+
| "id": 6752317,
23+
| "avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
24+
| "gravatar_id": "",
25+
| "url": "https://api.github.com/users/baxterthehacker",
26+
| "html_url": "https://github.com/baxterthehacker",
27+
| "followers_url": "https://api.github.com/users/baxterthehacker/followers",
28+
| "following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
29+
| "gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
30+
| "starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
31+
| "subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
32+
| "organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
33+
| "repos_url": "https://api.github.com/users/baxterthehacker/repos",
34+
| "events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
35+
| "received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
36+
| "type": "User",
37+
| "site_admin": false
38+
| },
39+
| "labels": [
40+
| {
41+
| "url": "https://api.github.com/repos/baxterthehacker/public-repo/labels/bug",
42+
| "name": "bug",
43+
| "color": "fc2929"
44+
| }
45+
| ],
46+
| "state": "open",
47+
| "locked": false,
48+
| "assignee": null,
49+
| "milestone": null,
50+
| "comments": 0,
51+
| "created_at": "2015-05-05T23:40:28Z",
52+
| "updated_at": "2015-05-05T23:40:28Z",
53+
| "closed_at": null,
54+
| "body": "It looks like you accidently spelled 'commit' with two 't's."
55+
| },
56+
| "repository": {
57+
| "id": 35129377,
58+
| "name": "public-repo",
59+
| "full_name": "baxterthehacker/public-repo",
60+
| "owner": {
61+
| "login": "baxterthehacker",
62+
| "id": 6752317,
63+
| "avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
64+
| "gravatar_id": "",
65+
| "url": "https://api.github.com/users/baxterthehacker",
66+
| "html_url": "https://github.com/baxterthehacker",
67+
| "followers_url": "https://api.github.com/users/baxterthehacker/followers",
68+
| "following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
69+
| "gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
70+
| "starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
71+
| "subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
72+
| "organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
73+
| "repos_url": "https://api.github.com/users/baxterthehacker/repos",
74+
| "events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
75+
| "received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
76+
| "type": "User",
77+
| "site_admin": false
78+
| },
79+
| "private": false,
80+
| "html_url": "https://github.com/baxterthehacker/public-repo",
81+
| "description": "",
82+
| "fork": false,
83+
| "url": "https://api.github.com/repos/baxterthehacker/public-repo",
84+
| "forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
85+
| "keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
86+
| "collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
87+
| "teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
88+
| "hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
89+
| "issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
90+
| "events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
91+
| "assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
92+
| "branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
93+
| "tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
94+
| "blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
95+
| "git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
96+
| "git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
97+
| "trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
98+
| "statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
99+
| "languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
100+
| "stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
101+
| "contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
102+
| "subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
103+
| "subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
104+
| "commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
105+
| "git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
106+
| "comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
107+
| "issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
108+
| "contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
109+
| "compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
110+
| "merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
111+
| "archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
112+
| "downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
113+
| "issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
114+
| "pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
115+
| "milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
116+
| "notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
117+
| "labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
118+
| "releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
119+
| "created_at": "2015-05-05T23:40:12Z",
120+
| "updated_at": "2015-05-05T23:40:12Z",
121+
| "pushed_at": "2015-05-05T23:40:27Z",
122+
| "git_url": "git://github.com/baxterthehacker/public-repo.git",
123+
| "ssh_url": "[email protected]:baxterthehacker/public-repo.git",
124+
| "clone_url": "https://github.com/baxterthehacker/public-repo.git",
125+
| "svn_url": "https://github.com/baxterthehacker/public-repo",
126+
| "homepage": null,
127+
| "size": 0,
128+
| "stargazers_count": 0,
129+
| "watchers_count": 0,
130+
| "language": null,
131+
| "has_issues": true,
132+
| "has_downloads": true,
133+
| "has_wiki": true,
134+
| "has_pages": true,
135+
| "forks_count": 0,
136+
| "mirror_url": null,
137+
| "open_issues_count": 2,
138+
| "forks": 0,
139+
| "open_issues": 2,
140+
| "watchers": 0,
141+
| "default_branch": "master"
142+
| },
143+
| "sender": {
144+
| "login": "baxterthehacker",
145+
| "id": 6752317,
146+
| "avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
147+
| "gravatar_id": "",
148+
| "url": "https://api.github.com/users/baxterthehacker",
149+
| "html_url": "https://github.com/baxterthehacker",
150+
| "followers_url": "https://api.github.com/users/baxterthehacker/followers",
151+
| "following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
152+
| "gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
153+
| "starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
154+
| "subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
155+
| "organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
156+
| "repos_url": "https://api.github.com/users/baxterthehacker/repos",
157+
| "events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
158+
| "received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
159+
| "type": "User",
160+
| "site_admin": false
161+
| }
162+
|}""".stripMargin)
163+
}

0 commit comments

Comments
 (0)