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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ coverage
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in github-payload.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Corey Donohoe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # Github::Payload
2
+
3
+ ![](http://farm3.staticflickr.com/2365/1812404502_c855c797eb_o.jpg)
4
+
5
+ GitHub::Payload provides a set of helpers for formatting GitHub payloads. Historically, [github-services](https://github.com/github/github-services) is built on a bunch of scripts that massage GitHub events and sends them off to third parties. New services [need to accept the unmodifier payload](http://developer.github.com/changes/2013-2-5-changes-to-services/) in order to be accepted. This gem serves as a bunch of test data for people building things around these kinds of payloads. It also offers some simple formatting ideal for receiving the payload and passing it on to another services.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'github-payload'
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'github/payload'
17
+
18
+ event = GitHub::Payload.for(payload)
19
+ event.summary_message
20
+ event.summary_url
21
+ ```
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it, cnew Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'github/payload/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "github-payload"
8
+ spec.version = GitHub::Payload::VERSION
9
+ spec.authors = ["Corey Donohoe"]
10
+ spec.email = ["atmos@atmos.org"]
11
+ spec.description = %q{Summarize GitHub Service Payloads easily}
12
+ spec.summary = %q{Cargo Cult your GitHub service payload formats}
13
+ spec.homepage = "https://github.com/atmos/github-payload"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "pry"
22
+ spec.add_development_dependency "simplecov"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "yajl-ruby"
26
+ end
@@ -0,0 +1,14 @@
1
+ require "github/payload/version"
2
+
3
+ require "github/payload/helpers"
4
+ require "github/payload/formatter"
5
+
6
+ require "ostruct"
7
+
8
+ module GitHub
9
+ module Payload
10
+ def self.for(payload)
11
+ Formatter::Parser.new(payload)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,65 @@
1
+ module GitHub::Payload::Formatter
2
+ class Parser
3
+ attr_accessor :payload
4
+ def initialize(payload)
5
+ @payload = payload
6
+ end
7
+
8
+ def summary_message
9
+ summarizer.summary_message
10
+ end
11
+
12
+ def summary_url
13
+ summarizer.summary_url
14
+ end
15
+
16
+ def summarizer
17
+ @summarizer ||=
18
+ case payload['event']
19
+ when 'push'
20
+ PushEvent.new(payload)
21
+ when 'create'
22
+ CreateEvent.new(payload)
23
+ when 'delete'
24
+ DeleteEvent.new(payload)
25
+ when 'public'
26
+ PublicEvent.new(payload)
27
+ when 'issues'
28
+ IssuesEvent.new(payload)
29
+ when 'pull_request'
30
+ PullRequestEvent.new(payload)
31
+ when 'issue_comment'
32
+ IssueCommentEvent.new(payload)
33
+ when 'pull_request_review_comment'
34
+ warn 'unsupported payload type pull_request_review_comment'
35
+ else
36
+ puts payload['event']
37
+ end
38
+ end
39
+ end
40
+
41
+ class Event
42
+ include GitHub::Payload::Formatter
43
+
44
+ attr_accessor :payload
45
+ def initialize(payload)
46
+ @payload = payload['payload']
47
+ end
48
+
49
+ def self.objectify(hash)
50
+ struct = OpenStruct.new
51
+ hash.each do |key, value|
52
+ struct.send("#{key}=", value.is_a?(Hash) ? objectify(value) : value)
53
+ end
54
+ struct
55
+ end
56
+ end
57
+ end
58
+ require 'github/payload/formatter/create_event'
59
+ require 'github/payload/formatter/delete_event'
60
+ require 'github/payload/formatter/issue_comment_event'
61
+ require 'github/payload/formatter/issues_event'
62
+ require 'github/payload/formatter/public_event'
63
+ require 'github/payload/formatter/pull_request_event'
64
+ require 'github/payload/formatter/push_event'
65
+
@@ -0,0 +1,21 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class CreateEvent < Event
5
+ include GitHub::Payload::Helpers::Push
6
+
7
+ def sender_name
8
+ payload['sender']['login']
9
+ end
10
+
11
+ def summary_url
12
+ payload['repository']['html_url'] + "/commits/#{payload['ref']}"
13
+ end
14
+
15
+ def summary_message
16
+ "[#{repo_name}] #{sender_name} just created #{payload['ref']}."
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class DeleteEvent < Event
5
+ include GitHub::Payload::Helpers::Push
6
+
7
+ def summary_url
8
+ payload['repository']['html_url']
9
+ end
10
+
11
+ def sender_name
12
+ payload['sender']['login']
13
+ end
14
+
15
+ def summary_message
16
+ "[#{repo_name}] #{sender_name} just deleted #{payload['ref']}."
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class IssueCommentEvent < Event
5
+ include GitHub::Payload::Helpers::Push, GitHub::Payload::Helpers::Issues
6
+
7
+ def sender_name
8
+ payload['comment']['user']['login']
9
+ end
10
+
11
+ def summary_url
12
+ payload['comment']['url']
13
+ end
14
+
15
+ def summary_message
16
+ "[#{repo_name}] #{sender_name} just commented."
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class IssuesEvent < Event
5
+ include GitHub::Payload::Helpers::Issues
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class PublicEvent < Event
5
+ include GitHub::Payload::Helpers::Push
6
+
7
+ def summary_url
8
+ payload['repository']['html_url']
9
+ end
10
+
11
+ def sender_name
12
+ payload['sender']['login']
13
+ end
14
+
15
+ def summary_message
16
+ "[#{repo_name}] #{sender_name} just made the repo public."
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class PullRequestEvent < Event
5
+ include GitHub::Payload::Helpers::PullRequest
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module GitHub
2
+ module Payload
3
+ module Formatter
4
+ class PushEvent < Event
5
+ include GitHub::Payload::Helpers::Push
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module GitHub::Payload::Helpers
2
+ end
3
+
4
+ require "github/payload/helpers/meta"
5
+ require "github/payload/helpers/actions"
6
+
7
+ require "github/payload/helpers/push"
8
+ require "github/payload/helpers/issues"
9
+ require "github/payload/helpers/pull_request"
10
+
@@ -0,0 +1,15 @@
1
+ module GitHub
2
+ module Payload
3
+ module Helpers
4
+ module Actions
5
+ def action
6
+ payload['action'].to_s
7
+ end
8
+
9
+ def opened?
10
+ action == 'opened'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module GitHub
2
+ module Payload
3
+ module Helpers
4
+ module Issues
5
+ include GitHub::Payload::Helpers::Meta, GitHub::Payload::Helpers::Actions
6
+
7
+ def issue
8
+ @issue ||= self.class.objectify(payload['issue'])
9
+ end
10
+
11
+ def summary_url
12
+ issue.html_url
13
+ end
14
+
15
+ def summary_message
16
+ "[%s] %s %s issue #%d: %s." % [
17
+ repo.name,
18
+ sender.login,
19
+ action,
20
+ issue.number,
21
+ issue.title,
22
+ ]
23
+ rescue
24
+ raise_config_error "Unable to build message: #{$!.to_s}"
25
+ end
26
+
27
+ def self.sample_payload
28
+ Service::HelpersWithMeta.sample_payload.merge(
29
+ "action" => "opened",
30
+ "issue" => {
31
+ "number" => 5,
32
+ "state" => "open",
33
+ "title" => "booya",
34
+ "body" => "boom town",
35
+ "user" => { "login" => "mojombo" }
36
+ }
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module GitHub
2
+ module Payload
3
+ module Helpers
4
+ module Meta
5
+ def repo
6
+ @repo ||= self.class.objectify(payload['repository'])
7
+ end
8
+
9
+ def sender
10
+ @sender ||= self.class.objectify(payload['sender'])
11
+ end
12
+
13
+ def self.sample_payload
14
+ {
15
+ "repository" => {
16
+ "name" => "grit",
17
+ "url" => "http://github.com/mojombo/grit",
18
+ "owner" => { "login" => "mojombo" }
19
+ },
20
+ "sender" => { "login" => 'defunkt' }
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ module GitHub
2
+ module Payload
3
+ module Helpers
4
+ module PullRequest
5
+ include GitHub::Payload::Helpers::Meta, GitHub::Payload::Helpers::Actions
6
+
7
+ def pull
8
+ @pull ||= self.class.objectify(payload['pull_request'])
9
+ end
10
+
11
+ def summary_url
12
+ pull.html_url
13
+ end
14
+
15
+ def summary_message
16
+ base_ref = pull.base.label.split(':').last
17
+ head_ref = pull.head.label.split(':').last
18
+
19
+ "[%s] %s %s pull request #%d: %s (%s...%s)" % [
20
+ repo.name,
21
+ sender.login,
22
+ action,
23
+ pull.number,
24
+ pull.title,
25
+ base_ref,
26
+ head_ref != base_ref ? head_ref : pull.head.label]
27
+ end
28
+
29
+ def self.sample_payload
30
+ repo_owner = "mojombo"
31
+ repo_name = "magik"
32
+ pull_user = "foo"
33
+ pull_number = 5
34
+ HelpersWithMeta.sample_payload.merge(
35
+ "action" => "opened",
36
+ "pull_request" => {
37
+ "number" => pull_number,
38
+ "commits" => 1,
39
+ "state" => "open",
40
+ "title" => "booya",
41
+ "body" => "boom town",
42
+ "user" => { "login" => "#{pull_user}" },
43
+ "head" => {"label" => "#{pull_user}:feature"},
44
+ "base" => {"label" => "#{repo_owner}:master"},
45
+ "html_url" => "https://github.com/#{repo_owner}/#{repo_name}/pulls/#{pull_number}"
46
+ }
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end