right_hook 0.2.5 → 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.
@@ -3,27 +3,22 @@ require 'json'
3
3
 
4
4
  module RightHook
5
5
  class App < Sinatra::Base
6
- KNOWN_EVENT_TYPES = %w(
7
- pull_request
8
- issue
9
- )
10
-
11
6
  post '/hook/:owner/:repo_name/:event_type' do
12
7
  owner = params[:owner]
13
8
  repo_name = params[:repo_name]
14
9
  event_type = params[:event_type]
15
10
  content = request.body.read
16
11
 
17
- halt 404 unless KNOWN_EVENT_TYPES.include?(event_type)
12
+ halt 404 unless Event::KNOWN_TYPES.include?(event_type)
18
13
  halt 501 unless respond_to?("on_#{event_type}")
19
14
 
20
15
  require_valid_signature(content, owner, repo_name, event_type)
21
16
 
22
17
  json = JSON.parse(content)
23
18
  case event_type
24
- when 'pull_request'
19
+ when Event::PULL_REQUEST
25
20
  on_pull_request(owner, repo_name, json['number'], json['action'], json['pull_request'])
26
- when 'issue'
21
+ when Event::ISSUE
27
22
  on_issue(owner, repo_name, json['action'], json['issue'])
28
23
  else
29
24
  halt 500
@@ -0,0 +1,24 @@
1
+ module RightHook
2
+ module Event
3
+ class << self
4
+ def github_name(event_type)
5
+ case event_type
6
+ when ISSUE
7
+ 'issues'
8
+ when PULL_REQUEST
9
+ 'pull_request'
10
+ else
11
+ raise ArgumentError, "Unrecognized event_type: #{event_type}"
12
+ end
13
+ end
14
+ end
15
+
16
+ ISSUE = 'issue'.freeze
17
+ PULL_REQUEST = 'pull_request'.freeze
18
+
19
+ KNOWN_TYPES = [
20
+ ISSUE,
21
+ PULL_REQUEST
22
+ ].freeze
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ module RightHook
4
+ class << self
5
+ def logger
6
+ @logger || Logger.new(File::NULL)
7
+ end
8
+
9
+ def logger=(logger)
10
+ @logger = logger
11
+ end
12
+ end
13
+ end
@@ -1,4 +1,6 @@
1
1
  require 'httparty'
2
+ require 'right_hook/event'
3
+ require 'right_hook/logger'
2
4
 
3
5
  module RightHook
4
6
  # Subscriber can subscribe and unsubscribe GitHub hooks to a hosted instance of a specified {RightHook::App}.
@@ -49,19 +51,24 @@ module RightHook
49
51
  oauth_token = opts.fetch(:oauth_token) { self.oauth_token }
50
52
  owner = opts.fetch(:owner) { self.owner }
51
53
  base_url = opts.fetch(:base_url) { self.base_url }
54
+ event_type = opts.fetch(:event_type) { self.event_type }
52
55
 
53
- HTTParty.post('https://api.github.com/hub',
56
+ response = HTTParty.post('https://api.github.com/hub',
54
57
  headers: {
55
58
  # http://developer.github.com/v3/#authentication
56
59
  'Authorization' => "token #{oauth_token}"
57
60
  },
58
61
  body: {
59
62
  'hub.mode' => mode,
60
- 'hub.topic' => "https://github.com/#{owner}/#{repo_name}/events/#{event_type}",
63
+ 'hub.topic' => "https://github.com/#{owner}/#{repo_name}/events/#{Event.github_name(event_type)}",
61
64
  'hub.callback' => "#{base_url}/hook/#{owner}/#{repo_name}/#{event_type}",
62
65
  'hub.secret' => secret
63
66
  }
64
- ).success?
67
+ )
68
+
69
+ RightHook.logger.warn("Failure modifying subscription: #{response.inspect}") unless response.success?
70
+
71
+ response.success?
65
72
  end
66
73
  end
67
74
  end
@@ -1,3 +1,3 @@
1
1
  module RightHook
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe RightHook::Event do
4
+ describe '.github_name' do
5
+ it 'maps all known types' do
6
+ described_class::KNOWN_TYPES.each do |type|
7
+ expect {
8
+ described_class.github_name(type)
9
+ }.not_to raise_error
10
+ end
11
+ end
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'right_hook/app'
4
4
  require 'right_hook/authenticated_client'
5
5
  require 'right_hook/authenticator'
6
6
  require 'right_hook/commenter'
7
+ require 'right_hook/event'
7
8
  require 'right_hook/subscriber'
8
9
 
9
10
  require_relative './support/spec_helpers.rb'
@@ -13,7 +13,7 @@ describe RightHook::Subscriber do
13
13
  describe '.subscribe' do
14
14
  let!(:stubbed_request) do
15
15
  stub_request(:post, 'https://api.github.com/hub').
16
- with(:body => 'hub.mode=subscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissue&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
16
+ with(:body => 'hub.mode=subscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
17
17
  :headers => {'Authorization' => 'token my_token'}
18
18
  ).to_return(:status => status_code, :body => '', :headers => {})
19
19
  end
@@ -37,12 +37,30 @@ describe RightHook::Subscriber do
37
37
  expect(stubbed_request).to have_been_requested
38
38
  end
39
39
  end
40
+
41
+ context 'When everything is overridden' do
42
+ let(:status_code) { 200 }
43
+ it 'works' do
44
+ s = described_class.new
45
+ result = s.subscribe(
46
+ repo_name: 'right_hook',
47
+ secret: 'the-secret',
48
+ oauth_token: 'my_token',
49
+ owner: 'mark-rushakoff',
50
+ base_url: 'http://example.com',
51
+ event_type: 'issue'
52
+ )
53
+ expect(result).to eq(true)
54
+
55
+ expect(stubbed_request).to have_been_requested
56
+ end
57
+ end
40
58
  end
41
59
 
42
60
  describe '.unsubscribe' do
43
61
  let!(:stubbed_request) do
44
62
  stub_request(:post, 'https://api.github.com/hub').
45
- with(:body => 'hub.mode=unsubscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissue&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
63
+ with(:body => 'hub.mode=unsubscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
46
64
  :headers => {'Authorization' => 'token my_token'}
47
65
  ).to_return(:status => status_code, :body => '', :headers => {})
48
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -174,6 +174,8 @@ files:
174
174
  - lib/right_hook/authenticated_client.rb
175
175
  - lib/right_hook/authenticator.rb
176
176
  - lib/right_hook/commenter.rb
177
+ - lib/right_hook/event.rb
178
+ - lib/right_hook/logger.rb
177
179
  - lib/right_hook/subscriber.rb
178
180
  - lib/right_hook/version.rb
179
181
  - right_hook.gemspec
@@ -184,6 +186,7 @@ files:
184
186
  - spec/authenticator_spec.rb
185
187
  - spec/captain_hook_spec.rb
186
188
  - spec/commenter_spec.rb
189
+ - spec/event_spec.rb
187
190
  - spec/spec_helper.rb
188
191
  - spec/subscriber_spec.rb
189
192
  - spec/support/spec_helpers.rb
@@ -221,6 +224,7 @@ test_files:
221
224
  - spec/authenticator_spec.rb
222
225
  - spec/captain_hook_spec.rb
223
226
  - spec/commenter_spec.rb
227
+ - spec/event_spec.rb
224
228
  - spec/spec_helper.rb
225
229
  - spec/subscriber_spec.rb
226
230
  - spec/support/spec_helpers.rb