github-payload 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.md +22 -0
  4. data/README.md +25 -0
  5. data/Rakefile +10 -0
  6. data/github-payload.gemspec +26 -0
  7. data/lib/github/payload.rb +14 -0
  8. data/lib/github/payload/formatter.rb +65 -0
  9. data/lib/github/payload/formatter/create_event.rb +21 -0
  10. data/lib/github/payload/formatter/delete_event.rb +21 -0
  11. data/lib/github/payload/formatter/issue_comment_event.rb +21 -0
  12. data/lib/github/payload/formatter/issues_event.rb +9 -0
  13. data/lib/github/payload/formatter/public_event.rb +21 -0
  14. data/lib/github/payload/formatter/pull_request_event.rb +9 -0
  15. data/lib/github/payload/formatter/push_event.rb +9 -0
  16. data/lib/github/payload/helpers.rb +10 -0
  17. data/lib/github/payload/helpers/actions.rb +15 -0
  18. data/lib/github/payload/helpers/issues.rb +42 -0
  19. data/lib/github/payload/helpers/meta.rb +26 -0
  20. data/lib/github/payload/helpers/pull_request.rb +52 -0
  21. data/lib/github/payload/helpers/push.rb +230 -0
  22. data/lib/github/payload/version.rb +5 -0
  23. data/test/data_test.rb +107 -0
  24. data/test/fixtures/commit_comment_comment.json +143 -0
  25. data/test/fixtures/create_branch.json +117 -0
  26. data/test/fixtures/delete_branch.json +115 -0
  27. data/test/fixtures/issue_comment_created.json +185 -0
  28. data/test/fixtures/issues_closed.json +158 -0
  29. data/test/fixtures/issues_opened.json +158 -0
  30. data/test/fixtures/member_added.json +132 -0
  31. data/test/fixtures/public.json +113 -0
  32. data/test/fixtures/pull_request_closed.json +414 -0
  33. data/test/fixtures/pull_request_opened.json +397 -0
  34. data/test/fixtures/pull_request_review_comment_comment.json +157 -0
  35. data/test/fixtures/push_created_forced.json +75 -0
  36. data/test/fixtures/push_deleted_forced.json +49 -0
  37. data/test/fixtures/push_event.json +99 -0
  38. data/test/push_test.rb +142 -0
  39. data/test/test_helper.rb +48 -0
  40. metadata +182 -0
@@ -0,0 +1,230 @@
1
+ module GitHub
2
+ module Payload
3
+ module Helpers
4
+ module Push
5
+ def created?
6
+ payload['created'] || !!(payload['before'] =~ /0{40}/)
7
+ end
8
+
9
+ def deleted?
10
+ payload['deleted'] || !!(payload['after'] =~ /0{40}/)
11
+ end
12
+
13
+ def forced?
14
+ payload['forced']
15
+ end
16
+
17
+ def ref
18
+ payload['ref'].to_s
19
+ end
20
+
21
+ def base_ref
22
+ payload['base_ref']
23
+ end
24
+
25
+ def tag?
26
+ !!(ref =~ %r|^refs/tags/|)
27
+ end
28
+
29
+ def ref_name
30
+ payload['ref_name'] ||= ref.sub(/\Arefs\/(heads|tags)\//, '')
31
+ end
32
+ alias :tag_name :ref_name
33
+ alias :branch_name :ref_name
34
+
35
+ def base_ref_name
36
+ payload['base_ref_name'] ||= base_ref.sub(/\Arefs\/(heads|tags)\//, '')
37
+ end
38
+
39
+ def before_sha
40
+ payload['before'][0..6]
41
+ end
42
+
43
+ def after_sha
44
+ payload['after'][0..6]
45
+ end
46
+
47
+ def format_commit_message(commit)
48
+ short = commit['message'].split("\n", 2).first
49
+ "[#{repo_name}/#{branch_name}] #{short} - #{commit['author']['name']}"
50
+ end
51
+
52
+ def commit_messages
53
+ distinct_commits.map do |commit|
54
+ format_commit_message(commit)
55
+ end
56
+ end
57
+
58
+ def summary_message
59
+ message = []
60
+ message << "[#{repo_name}] #{pusher_name}"
61
+
62
+ if created?
63
+ if tag?
64
+ message << "tagged #{tag_name} at"
65
+ message << (base_ref ? base_ref_name : after_sha)
66
+ else
67
+ message << "created #{branch_name}"
68
+
69
+ if base_ref
70
+ message << "from #{base_ref_name}"
71
+ elsif distinct_commits.empty?
72
+ message << "at #{after_sha}"
73
+ end
74
+
75
+ if distinct_commits.any?
76
+ num = distinct_commits.size
77
+ message << "(+#{num} new commit#{num > 1 ? 's' : ''})"
78
+ end
79
+ end
80
+
81
+ elsif deleted?
82
+ message << "deleted #{branch_name} at #{before_sha}"
83
+
84
+ elsif forced?
85
+ message << "force-pushed #{branch_name} from #{before_sha} to #{after_sha}"
86
+
87
+ elsif commits.any? and distinct_commits.empty?
88
+ if base_ref
89
+ message << "merged #{base_ref_name} into #{branch_name}"
90
+ else
91
+ message << "fast-forwarded #{branch_name} from #{before_sha} to #{after_sha}"
92
+ end
93
+
94
+ elsif distinct_commits.any?
95
+ num = distinct_commits.size
96
+ message << "pushed #{num} new commit#{num > 1 ? 's' : ''} to #{branch_name}"
97
+
98
+ else
99
+ message << "pushed nothing"
100
+ end
101
+
102
+ message.join(' ')
103
+ end
104
+
105
+ def summary_url
106
+ if created?
107
+ if distinct_commits.empty?
108
+ branch_url
109
+ else
110
+ compare_url
111
+ end
112
+
113
+ elsif deleted?
114
+ before_sha_url
115
+
116
+ elsif forced?
117
+ branch_url
118
+
119
+ elsif distinct_commits.size == 1
120
+ distinct_commits.first['url']
121
+
122
+ else
123
+ compare_url
124
+ end
125
+ end
126
+
127
+ def repo_url
128
+ payload['repository']['url']
129
+ end
130
+
131
+ def compare_url
132
+ payload['compare']
133
+ end
134
+
135
+ def branch_url
136
+ repo_url + "/commits/#{branch_name}"
137
+ end
138
+
139
+ def before_sha_url
140
+ repo_url + "/commit/#{before_sha}"
141
+ end
142
+
143
+ def after_sha_url
144
+ repo_url + "/commit/#{after_sha}"
145
+ end
146
+
147
+ def pusher_name
148
+ payload.include?('pusher') ? payload['pusher']['name'] : "somebody"
149
+ end
150
+
151
+ def owner_name
152
+ payload['repository']['owner']['name']
153
+ end
154
+
155
+ def repo_name
156
+ payload['repository']['name']
157
+ end
158
+
159
+ def name_with_owner
160
+ File.join(owner_name, repo_name)
161
+ end
162
+
163
+ def commits
164
+ Array(payload['commits'])
165
+ end
166
+
167
+ def distinct_commits
168
+ payload['distinct_commits'] ||= Array(commits.select do |commit|
169
+ commit['distinct'] and !commit['message'].to_s.strip.empty?
170
+ end)
171
+ end
172
+
173
+ def self.sample_payload
174
+ {
175
+ "after" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
176
+ "ref" => "refs/heads/master",
177
+ "before" => "4c8124ffcf4039d292442eeccabdeca5af5c5017",
178
+ "compare" => "http://github.com/mojombo/grit/compare/4c8124f...a47fd41",
179
+
180
+ "repository" => {
181
+ "name" => "grit",
182
+ "url" => "http://github.com/mojombo/grit",
183
+ "owner" => { "name" => "mojombo", "email" => "tom@mojombo.com" }
184
+ },
185
+
186
+ "pusher" => {
187
+ "name" => "rtomayko"
188
+ },
189
+
190
+ "commits" => [
191
+ {
192
+ "distinct" => true,
193
+ "removed" => [],
194
+ "message" => "stub git call for Grit#heads test f:15 Case#1",
195
+ "added" => [],
196
+ "timestamp" => "2007-10-10T00:11:02-07:00",
197
+ "modified" => ["lib/grit/grit.rb", "test/helper.rb", "test/test_grit.rb"],
198
+ "url" => "http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325",
199
+ "author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
200
+ "id" => "06f63b43050935962f84fe54473a7c5de7977325"
201
+ },
202
+ {
203
+ "distinct" => true,
204
+ "removed" => [],
205
+ "message" => "clean up heads test f:2hrs",
206
+ "added" => [],
207
+ "timestamp" => "2007-10-10T00:18:20-07:00",
208
+ "modified" => ["test/test_grit.rb"],
209
+ "url" => "http://github.com/mojombo/grit/commit/5057e76a11abd02e83b7d3d3171c4b68d9c88480",
210
+ "author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
211
+ "id" => "5057e76a11abd02e83b7d3d3171c4b68d9c88480"
212
+ },
213
+ {
214
+ "distinct" => true,
215
+ "removed" => [],
216
+ "message" => "add more comments throughout",
217
+ "added" => [],
218
+ "timestamp" => "2007-10-10T00:50:39-07:00",
219
+ "modified" => ["lib/grit.rb", "lib/grit/commit.rb", "lib/grit/grit.rb"],
220
+ "url" => "http://github.com/mojombo/grit/commit/a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
221
+ "author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
222
+ "id" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425"
223
+ }
224
+ ]
225
+ }
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,5 @@
1
+ module GitHub
2
+ module Payload
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,107 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class DataTest < Formatter::TestCase
4
+ def test_all_events_presents
5
+ assert_equal 14, fixture_files.size
6
+
7
+ fixture_files.each do |file|
8
+ content = Yajl.load(File.read(file))
9
+ receiver = GitHub::Payload.for(content)
10
+ assert receiver.payload['event']
11
+ end
12
+ end
13
+
14
+ def test_push_summary
15
+ content = load_fixture("push_event")
16
+ receiver = GitHub::Payload.for(content)
17
+
18
+ assert_equal "[test] atmos pushed 1 new commit to master", receiver.summary_message
19
+ assert_equal "https://github.com/atmos/test/commit/12c8e95fe799c4545d932ee5245ef2a53735d1ae",
20
+ receiver.summary_url
21
+ end
22
+
23
+ def test_branch_created_summary
24
+ content = load_fixture("push_created")
25
+ receiver = GitHub::Payload.for(content)
26
+
27
+ assert_equal "[test] fakeatmos created logo from master", receiver.summary_message
28
+ assert_equal "https://github.com/atmos/test/commits/logo", receiver.summary_url
29
+ end
30
+
31
+ def test_branch_deleted_summary
32
+ content = load_fixture("push_deleted")
33
+ receiver = GitHub::Payload.for(content)
34
+
35
+ assert_equal "[test] fakeatmos deleted logo at dcbb4e7", receiver.summary_message
36
+ assert_equal "https://github.com/atmos/test/commit/dcbb4e7", receiver.summary_url
37
+ end
38
+
39
+ def test_pull_request_opened_summary
40
+ content = load_fixture("pull_request_opened")
41
+ receiver = GitHub::Payload.for(content)
42
+
43
+ assert_equal "[test] fakeatmos opened pull request #2: Update README.md (master...logo)",
44
+ receiver.summary_message
45
+ assert_equal "https://github.com/atmos/test/pull/2", receiver.summary_url
46
+ end
47
+
48
+ def test_pull_request_closed_summary
49
+ content = load_fixture("pull_request_closed")
50
+ receiver = GitHub::Payload.for(content)
51
+
52
+ assert_equal "[test] atmos closed pull request #2: Update README.md (master...logo)",
53
+ receiver.summary_message
54
+ assert_equal "https://github.com/atmos/test/pull/2", receiver.summary_url
55
+ end
56
+
57
+ def test_toggling_repo_public
58
+ content = load_fixture("public")
59
+ receiver = GitHub::Payload.for(content)
60
+
61
+ assert_equal "[test] atmos just made the repo public.", receiver.summary_message
62
+ assert_equal "https://github.com/atmos/test", receiver.summary_url
63
+ end
64
+
65
+ def test_issue_opening
66
+ content = load_fixture("issues_opened")
67
+ receiver = GitHub::Payload.for(content)
68
+
69
+ assert_equal "[test] fakeatmos opened issue #1: Images would be cool.",
70
+ receiver.summary_message
71
+ assert_equal "https://github.com/atmos/test/issues/1", receiver.summary_url
72
+ end
73
+
74
+ def test_issue_closing
75
+ content = load_fixture("issues_closed")
76
+ receiver = GitHub::Payload.for(content)
77
+
78
+ assert_equal "[test] atmos closed issue #1: Images would be cool.",
79
+ receiver.summary_message
80
+ assert_equal "https://github.com/atmos/test/issues/1", receiver.summary_url
81
+ end
82
+
83
+ def test_issue_comment_created
84
+ content = load_fixture("issue_comment_created")
85
+ receiver = GitHub::Payload.for(content)
86
+
87
+ assert_equal "[test] atmos just commented.", receiver.summary_message
88
+ assert_equal "https://api.github.com/repos/atmos/test/issues/comments/19092978",
89
+ receiver.summary_url
90
+ end
91
+
92
+ def test_created_branch
93
+ content = load_fixture("create_branch")
94
+ receiver = GitHub::Payload.for(content)
95
+
96
+ assert_equal "[test] fakeatmos just created logo.", receiver.summary_message
97
+ assert_equal "https://github.com/atmos/test/commits/logo", receiver.summary_url
98
+ end
99
+
100
+ def test_deleted_branch
101
+ content = load_fixture("delete_branch")
102
+ receiver = GitHub::Payload.for(content)
103
+
104
+ assert_equal "[test] fakeatmos just deleted logo.", receiver.summary_message
105
+ assert_equal "https://github.com/atmos/test", receiver.summary_url
106
+ end
107
+ end
@@ -0,0 +1,143 @@
1
+ {
2
+ "payload": {
3
+ "comment": {
4
+ "url": "https://api.github.com/repos/atmos/test/comments/3375062",
5
+ "html_url": "https://github.com/atmos/test/commit/dcbb4e731b9588903d1fdda5131ab6125e7bce31#commitcomment-3375062",
6
+ "id": 3375062,
7
+ "user": {
8
+ "login": "atmos",
9
+ "id": 38,
10
+ "avatar_url": "https://secure.gravatar.com/avatar/a86224d72ce21cd9f5bee6784d4b06c7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
11
+ "gravatar_id": "a86224d72ce21cd9f5bee6784d4b06c7",
12
+ "url": "https://api.github.com/users/atmos",
13
+ "html_url": "https://github.com/atmos",
14
+ "followers_url": "https://api.github.com/users/atmos/followers",
15
+ "following_url": "https://api.github.com/users/atmos/following{/other_user}",
16
+ "gists_url": "https://api.github.com/users/atmos/gists{/gist_id}",
17
+ "starred_url": "https://api.github.com/users/atmos/starred{/owner}{/repo}",
18
+ "subscriptions_url": "https://api.github.com/users/atmos/subscriptions",
19
+ "organizations_url": "https://api.github.com/users/atmos/orgs",
20
+ "repos_url": "https://api.github.com/users/atmos/repos",
21
+ "events_url": "https://api.github.com/users/atmos/events{/privacy}",
22
+ "received_events_url": "https://api.github.com/users/atmos/received_events",
23
+ "type": "User"
24
+ },
25
+ "position": null,
26
+ "line": null,
27
+ "path": null,
28
+ "commit_id": "dcbb4e731b9588903d1fdda5131ab6125e7bce31",
29
+ "created_at": "2013-06-07T07:30:32Z",
30
+ "updated_at": "2013-06-07T07:30:32Z",
31
+ "body": ":shipit:"
32
+ },
33
+ "repository": {
34
+ "id": 10544626,
35
+ "name": "test",
36
+ "full_name": "atmos/test",
37
+ "owner": {
38
+ "login": "atmos",
39
+ "id": 38,
40
+ "avatar_url": "https://secure.gravatar.com/avatar/a86224d72ce21cd9f5bee6784d4b06c7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
41
+ "gravatar_id": "a86224d72ce21cd9f5bee6784d4b06c7",
42
+ "url": "https://api.github.com/users/atmos",
43
+ "html_url": "https://github.com/atmos",
44
+ "followers_url": "https://api.github.com/users/atmos/followers",
45
+ "following_url": "https://api.github.com/users/atmos/following{/other_user}",
46
+ "gists_url": "https://api.github.com/users/atmos/gists{/gist_id}",
47
+ "starred_url": "https://api.github.com/users/atmos/starred{/owner}{/repo}",
48
+ "subscriptions_url": "https://api.github.com/users/atmos/subscriptions",
49
+ "organizations_url": "https://api.github.com/users/atmos/orgs",
50
+ "repos_url": "https://api.github.com/users/atmos/repos",
51
+ "events_url": "https://api.github.com/users/atmos/events{/privacy}",
52
+ "received_events_url": "https://api.github.com/users/atmos/received_events",
53
+ "type": "User"
54
+ },
55
+ "private": true,
56
+ "html_url": "https://github.com/atmos/test",
57
+ "description": "",
58
+ "fork": false,
59
+ "url": "https://api.github.com/repos/atmos/test",
60
+ "forks_url": "https://api.github.com/repos/atmos/test/forks",
61
+ "keys_url": "https://api.github.com/repos/atmos/test/keys{/key_id}",
62
+ "collaborators_url": "https://api.github.com/repos/atmos/test/collaborators{/collaborator}",
63
+ "teams_url": "https://api.github.com/repos/atmos/test/teams",
64
+ "hooks_url": "https://api.github.com/repos/atmos/test/hooks",
65
+ "issue_events_url": "https://api.github.com/repos/atmos/test/issues/events{/number}",
66
+ "events_url": "https://api.github.com/repos/atmos/test/events",
67
+ "assignees_url": "https://api.github.com/repos/atmos/test/assignees{/user}",
68
+ "branches_url": "https://api.github.com/repos/atmos/test/branches{/branch}",
69
+ "tags_url": "https://api.github.com/repos/atmos/test/tags",
70
+ "blobs_url": "https://api.github.com/repos/atmos/test/git/blobs{/sha}",
71
+ "git_tags_url": "https://api.github.com/repos/atmos/test/git/tags{/sha}",
72
+ "git_refs_url": "https://api.github.com/repos/atmos/test/git/refs{/sha}",
73
+ "trees_url": "https://api.github.com/repos/atmos/test/git/trees{/sha}",
74
+ "statuses_url": "https://api.github.com/repos/atmos/test/statuses/{sha}",
75
+ "languages_url": "https://api.github.com/repos/atmos/test/languages",
76
+ "stargazers_url": "https://api.github.com/repos/atmos/test/stargazers",
77
+ "contributors_url": "https://api.github.com/repos/atmos/test/contributors",
78
+ "subscribers_url": "https://api.github.com/repos/atmos/test/subscribers",
79
+ "subscription_url": "https://api.github.com/repos/atmos/test/subscription",
80
+ "commits_url": "https://api.github.com/repos/atmos/test/commits{/sha}",
81
+ "git_commits_url": "https://api.github.com/repos/atmos/test/git/commits{/sha}",
82
+ "comments_url": "https://api.github.com/repos/atmos/test/comments{/number}",
83
+ "issue_comment_url": "https://api.github.com/repos/atmos/test/issues/comments/{number}",
84
+ "contents_url": "https://api.github.com/repos/atmos/test/contents/{+path}",
85
+ "compare_url": "https://api.github.com/repos/atmos/test/compare/{base}...{head}",
86
+ "merges_url": "https://api.github.com/repos/atmos/test/merges",
87
+ "archive_url": "https://api.github.com/repos/atmos/test/{archive_format}{/ref}",
88
+ "downloads_url": "https://api.github.com/repos/atmos/test/downloads",
89
+ "issues_url": "https://api.github.com/repos/atmos/test/issues{/number}",
90
+ "pulls_url": "https://api.github.com/repos/atmos/test/pulls{/number}",
91
+ "milestones_url": "https://api.github.com/repos/atmos/test/milestones{/number}",
92
+ "notifications_url": "https://api.github.com/repos/atmos/test/notifications{?since,all,participating}",
93
+ "labels_url": "https://api.github.com/repos/atmos/test/labels{/name}",
94
+ "created_at": "2013-06-07T07:27:32Z",
95
+ "updated_at": "2013-06-07T07:29:44Z",
96
+ "pushed_at": "2013-06-07T07:29:44Z",
97
+ "git_url": "git://github.com/atmos/test.git",
98
+ "ssh_url": "git@github.com:atmos/test.git",
99
+ "clone_url": "https://github.com/atmos/test.git",
100
+ "svn_url": "https://github.com/atmos/test",
101
+ "homepage": null,
102
+ "size": 112,
103
+ "watchers_count": 0,
104
+ "language": null,
105
+ "has_issues": true,
106
+ "has_downloads": true,
107
+ "has_wiki": true,
108
+ "forks_count": 0,
109
+ "mirror_url": null,
110
+ "open_issues_count": 1,
111
+ "forks": 0,
112
+ "open_issues": 1,
113
+ "watchers": 0,
114
+ "master_branch": "master",
115
+ "default_branch": "master"
116
+ },
117
+ "sender": {
118
+ "login": "atmos",
119
+ "id": 38,
120
+ "avatar_url": "https://secure.gravatar.com/avatar/a86224d72ce21cd9f5bee6784d4b06c7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
121
+ "gravatar_id": "a86224d72ce21cd9f5bee6784d4b06c7",
122
+ "url": "https://api.github.com/users/atmos",
123
+ "html_url": "https://github.com/atmos",
124
+ "followers_url": "https://api.github.com/users/atmos/followers",
125
+ "following_url": "https://api.github.com/users/atmos/following{/other_user}",
126
+ "gists_url": "https://api.github.com/users/atmos/gists{/gist_id}",
127
+ "starred_url": "https://api.github.com/users/atmos/starred{/owner}{/repo}",
128
+ "subscriptions_url": "https://api.github.com/users/atmos/subscriptions",
129
+ "organizations_url": "https://api.github.com/users/atmos/orgs",
130
+ "repos_url": "https://api.github.com/users/atmos/repos",
131
+ "events_url": "https://api.github.com/users/atmos/events{/privacy}",
132
+ "received_events_url": "https://api.github.com/users/atmos/received_events",
133
+ "type": "User"
134
+ }
135
+ },
136
+ "event": "commit_comment",
137
+ "config": {
138
+ "url": "https://campfire-relaay.herokuapp.com",
139
+ "content_type": "json",
140
+ "secret": "563eb98bb765b5c048b9bbd1f4611b2c2beafc1c8fb3678b"
141
+ },
142
+ "guid": "229d5c00-cf44-11e2-81c1-e8a63442614c"
143
+ }