github_webhook 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 285de73545052ef08df5f6196afbc7f8518c0623
4
- data.tar.gz: 5cb6d51070167f9b52273bfef55bbd4f2a669240
3
+ metadata.gz: ebcf435b142dbc33048434ccece05593df374855
4
+ data.tar.gz: 685a3cc522b13a99f1e21867ce0aa7c2b5777770
5
5
  SHA512:
6
- metadata.gz: 8fb9d9ab34f1938decb04a19e37229a1a0e614cea6857b419f9fabb0db2d4d979945276b7873165158433533a65510a729f18fed0311123e7a0c231c263e9de5
7
- data.tar.gz: 7e64a43a655c9773cf524dd28783a0cb3967a0518d7439a8dbdd4a75e0e59c84b79aff9eeaaa5ce2d43fca0fac95f8f0725c3084e00c73bc9a1d9c398409c25c
6
+ metadata.gz: 63c28c385cee31dd18b91f3c0e3e418d8af53ddf43eae9abf6df923934df01aea7c9e60731ff9bb8b1609dc04c449c4100943b94d34203350be9e28c50c62ef4
7
+ data.tar.gz: 980d7ca90fb8028c1ed5670cce359f25a9b846a18078b6f5129603ff378e7a41ea157626e3480adb6b0374482f67c1b9f3811215ed005a8190a424f1cd8cbcd9
data/Gemfile.lock CHANGED
@@ -15,7 +15,7 @@ GEM
15
15
  tzinfo (~> 1.1)
16
16
  diff-lcs (1.2.5)
17
17
  i18n (0.6.9)
18
- json (1.8.1)
18
+ json (1.8.3)
19
19
  minitest (5.3.5)
20
20
  rake (10.3.1)
21
21
  rspec (2.14.1)
@@ -38,3 +38,6 @@ DEPENDENCIES
38
38
  github_webhook!
39
39
  rake (~> 10.1)
40
40
  rspec (~> 2.14)
41
+
42
+ BUNDLED WITH
43
+ 1.11.2
data/README.md CHANGED
@@ -12,7 +12,7 @@ to a [GitHub webhook](https://developer.github.com/webhooks/)
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'github_webhook'
15
+ gem 'github_webhook', '~> 0.3.0'
16
16
  ```
17
17
 
18
18
  And then execute:
@@ -35,11 +35,15 @@ Then create a new controller:
35
35
  class GithubWebhooksController < ActionController::Base
36
36
  include GithubWebhook::Processor
37
37
 
38
- def push(payload)
38
+ # Handle push event
39
+ def github_push(payload)
39
40
  # TODO: handle push webhook
40
41
  end
41
42
 
42
- private
43
+ # Handle create event
44
+ def github_create(payload)
45
+ # TODO: handle create webhook
46
+ end
43
47
 
44
48
  def webhook_secret(payload)
45
49
  ENV['GITHUB_WEBHOOK_SECRET']
@@ -48,7 +52,11 @@ end
48
52
  ```
49
53
 
50
54
  Add as many instance methods as events you want to handle in
51
- your controller. You can read the [full list of events](https://developer.github.com/v3/activity/events/types/) GitHub can notify you about.
55
+ your controller.
56
+
57
+ All events are prefixed with `github_`. So, a `push` event can be handled by `github_push(payload)`, or a `create` event can be handled by `github_create(payload)`, etc.
58
+
59
+ You can read the [full list of events](https://developer.github.com/v3/activity/events/types/) GitHub can notify you about.
52
60
 
53
61
  ## Adding the Webhook to your git repository:
54
62
 
@@ -2,18 +2,22 @@ module GithubWebhook::Processor
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- before_filter :authenticate_github_request!, :only => :create
5
+ before_filter :authenticate_github_request!, only: :create
6
+ before_filter :check_github_event!, only: :create
6
7
  end
7
8
 
8
9
  class SignatureError < StandardError; end
9
10
  class UnspecifiedWebhookSecretError < StandardError; end
11
+ class UnsupportedGithubEventError < StandardError; end
12
+
13
+ GITHUB_EVENTS_WHITELIST = %w(commit_comment create delete deployment deployment_status download follow fork fork_apply gist gollum issue_comment issues member membership page_build public pull_request pull_request_review_comment push release repository status team_add watch)
10
14
 
11
15
  def create
12
- if self.respond_to? event
13
- self.send event, json_body
16
+ if self.respond_to? event_method
17
+ self.send event_method, json_body
14
18
  head(:ok)
15
19
  else
16
- raise NoMethodError.new("GithubWebhooksController##{event} not implemented")
20
+ raise NoMethodError.new("GithubWebhooksController##{event_method} not implemented")
17
21
  end
18
22
  end
19
23
 
@@ -35,6 +39,12 @@ module GithubWebhook::Processor
35
39
  end
36
40
  end
37
41
 
42
+ def check_github_event!
43
+ unless GITHUB_EVENTS_WHITELIST.include?(request.headers['X-GitHub-Event'])
44
+ raise UnsupportedGithubEventError.new("#{request.headers['X-GitHub-Event']} is not a whiltelisted GitHub event. See https://developer.github.com/v3/activity/events/types/")
45
+ end
46
+ end
47
+
38
48
  def request_body
39
49
  @request_body ||= (
40
50
  request.body.rewind
@@ -50,7 +60,7 @@ module GithubWebhook::Processor
50
60
  @signature_header ||= request.headers['X-Hub-Signature']
51
61
  end
52
62
 
53
- def event
54
- @event ||= request.headers['X-GitHub-Event'].to_sym
63
+ def event_method
64
+ @event_method ||= "github_#{request.headers['X-GitHub-Event']}".to_sym
55
65
  end
56
66
  end
@@ -1,3 +1,3 @@
1
1
  module GithubWebhook
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -23,7 +23,7 @@ module GithubWebhook
23
23
 
24
24
  include GithubWebhook::Processor
25
25
 
26
- def push(payload)
26
+ def github_push(payload)
27
27
  @pushed = payload[:foo]
28
28
  end
29
29
  end
@@ -46,7 +46,7 @@ module GithubWebhook
46
46
 
47
47
  describe "#create" do
48
48
  it "raises an error when secret is not defined" do
49
- expect { controller_without_secret.send :authenticate_github_request! }.to raise_error
49
+ expect { controller_without_secret.send :authenticate_github_request! }.to raise_error(Processor::UnspecifiedWebhookSecretError)
50
50
  end
51
51
 
52
52
  it "calls the #push method in controller" do
@@ -55,20 +55,25 @@ module GithubWebhook
55
55
  controller.request.headers['X-GitHub-Event'] = 'push'
56
56
  controller.send :authenticate_github_request! # Manually as we don't have the before_filter logic in our Mock object
57
57
  controller.create
58
- controller.pushed.should eq "bar"
58
+ expect(controller.pushed).to eq "bar"
59
59
  end
60
60
 
61
61
  it "raises an error when signature does not match" do
62
62
  controller.request.body = StringIO.new({ :foo => "bar" }.to_json.to_s)
63
63
  controller.request.headers['X-Hub-Signature'] = "sha1=FOOBAR"
64
64
  controller.request.headers['X-GitHub-Event'] = 'push'
65
- expect { controller_without_secret.send :authenticate_github_request! }.to raise_error
65
+ expect { controller.send :authenticate_github_request! }.to raise_error(Processor::SignatureError)
66
66
  end
67
67
 
68
68
  it "raises an error when the github event method is not implemented" do
69
- controller.request.headers['X-GitHub-Event'] = 'unimplemented_event'
70
- expect { controller_without_secret.create }.to raise_error
69
+ controller.request.headers['X-GitHub-Event'] = 'deployment'
70
+ expect { controller.create }.to raise_error(NoMethodError)
71
+ end
72
+
73
+ it "raises an error when the github event is not in the whitelist" do
74
+ controller.request.headers['X-GitHub-Event'] = 'fake_event'
75
+ expect { controller.send :check_github_event! }.to raise_error(Processor::UnsupportedGithubEventError)
71
76
  end
72
77
  end
73
78
  end
74
- end
79
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github_webhook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Saunier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2016-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 2.2.2
109
+ rubygems_version: 2.5.1
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Process GitHub Webhooks in your Rails app (Controller mixin)