githook 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a2e2d13b18491f2527d99f809e9437936d61390
4
- data.tar.gz: 68f87cbf610aca114c7af8077947f43939deb9ad
3
+ metadata.gz: 343adffd4b44ce17f0a0986d0288d448e1d2aa42
4
+ data.tar.gz: 656d009224e57a9f31a02266e4f0bc77a0d579ac
5
5
  SHA512:
6
- metadata.gz: 3a291f330588cf5fdd0b53a281d8570244c011c907c4dbcb61e1af1853ca7e22985ff13a47a43a4b9fae9a7d01cd2d8e8a226c9def90209067d639e926a29954
7
- data.tar.gz: 80c43f28f19ccf6327fbbf254b3e51c53f8b4bd4cbce582b7d65a07c354e850a6312721ca3eb8da8d16fb45e26241e9b869bc645ce024a75bc47ef1d88f707f3
6
+ metadata.gz: 9f061b511a75ce10cded9b983ba46f62e787722cba89a45cffeb65a50b49912d72954f48382219591a79e8e547144e1bed99c42a42ae565619de14e65d9961b0
7
+ data.tar.gz: aff4ca2ded4914aacf8ce9c719cb018ad0441746511bc76b2dbaa3a9da9072986504989b1ac95b03937e0728b294791481a4f17952c554b9b828773d85fc6fea
data/lib/githook/bot.rb CHANGED
@@ -4,16 +4,16 @@ module Githook
4
4
  class Bot
5
5
  attr_accessor :github_client
6
6
 
7
+ # TODO: Grow list will grow in future changes.
8
+ SUPPORTED_ACTIONS = %w(issue_comment issues pull_request pull_request_review_comment)
9
+
7
10
  def initialize(configs = {})
8
11
  access_token = configs[:access_token]
9
12
  @logger = configs[:logger] || Logger.new(STDOUT)
10
13
 
11
14
  raise "No Github access token was provided. Your bot will be unable to perform Github actions on your behalf." unless access_token
12
15
 
13
- @github_client = Octokit::Client.new({access_token:
14
- access_token
15
- })
16
-
16
+ @github_client = Octokit::Client.new(access_token: access_token)
17
17
  @registry = {}
18
18
  end
19
19
 
@@ -33,6 +33,7 @@ module Githook
33
33
 
34
34
  @actions = []
35
35
  def when(event_action)
36
+
36
37
  @actions << event_action
37
38
 
38
39
  self
@@ -40,16 +41,24 @@ module Githook
40
41
 
41
42
  @event_type = nil
42
43
  def on(event_type)
44
+ raise "Github event '#{event}' is not supported by Githook" unless SUPPORTED_ACTIONS.include?(event_type)
45
+
43
46
  @event_type = event_type
44
47
  @actions = []
45
48
 
46
49
  self
47
50
  end
48
51
 
49
- def process(body)
50
- event_info = github_event(body)
52
+ def process(body, event)
53
+ unless SUPPORTED_ACTIONS.include?(event)
54
+ @logger.info("Github event '#{event}' is not supported by Githook")
55
+ return
56
+ end
57
+
58
+ action = body['action']
59
+ @logger.info("Detected the following GitHub event: action: #{action} event: #{event}")
51
60
 
52
- handler = (@registry["#{event_info['event']}-#{event_info['action']}"] || @registry[event_info['event']])
61
+ handler = (@registry["#{event}-#{action}"] || @registry[event])
53
62
 
54
63
  if handler
55
64
  pr_info = body['pull_request'] || body['issue']
@@ -58,17 +67,5 @@ module Githook
58
67
  handler.call(pr)
59
68
  end
60
69
  end
61
-
62
- private
63
- def github_event(body)
64
- # Normally the GitHub event is present in the X-GitHub-Event headers but sometimes
65
- # that header is absent in which case we need to brute force it.
66
- events = %w(commit_comment create delete deployment deployment_status fork gollum issue_comment issue pull_request)
67
-
68
- event = (body.keys & events).first # should only ever be one
69
-
70
- @logger.info("Detected the following GitHub event: action: #{body['action']} event: #{event}")
71
- {'action' => body['action'], 'event' => event}
72
- end
73
70
  end
74
71
  end
@@ -0,0 +1,35 @@
1
+ require 'rack'
2
+
3
+ module Githook
4
+ class Server
5
+ attr_accessor :bot
6
+
7
+ GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
8
+
9
+ def initialize(bot)
10
+ @bot = bot
11
+ end
12
+
13
+ def listen(route = nil)
14
+ app = Proc.new do |env|
15
+ callback_route = route.start_with?('/') ? route : "/#{route}"
16
+ if env['PATH_INFO'] == callback_route
17
+ body = JSON.parse(env['rack.input'].gets)
18
+ @bot.process(body, env[GITHUB_EVENT_HEADER])
19
+
20
+ ['200', {'Content-Type' => 'application/json'}, ["{\"message\": \"Success\"}"]]
21
+ else
22
+ ['404', {'Content-Type' => 'application/json'}, ["{\"message\": \"Not Found\"}"]]
23
+ end
24
+ end
25
+
26
+ bot_server = Rack::Server.new(
27
+ app: app,
28
+ server: 'webrick',
29
+ Port: 3002
30
+ )
31
+
32
+ bot_server.start
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/githook.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'octokit'
2
2
  require 'githook/bot'
3
3
  require 'githook/event_objects/pull_request'
4
+ require 'githook/server'
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Githook::Server do
4
+ context '#listen' do
5
+ let(:mybot) { Githook::Bot.new(access_token: 'xxxxxxfakexxxx') }
6
+ let(:myserver) { Githook::Server.new(mybot) }
7
+
8
+ it 'calls listen' do
9
+ # expect{ myserver.listen('/payload') }.to_not raise_error
10
+ end
11
+ end
12
+ end
13
+
data/spec/githook_spec.rb CHANGED
@@ -14,7 +14,7 @@ describe Githook do
14
14
  it 'invokes the handler that matches the event type and action' do
15
15
  mybot.on('pull_request').when('opened').perform(&handler)
16
16
  expect(handler).to receive(:call)
17
- mybot.process(WEBHOOK_REQUESTS['spec/webhooks/opened_pr.json'])
17
+ mybot.process(WEBHOOK_REQUESTS['spec/webhooks/opened_pr.json'], 'pull_request')
18
18
  end
19
19
 
20
20
  it 'passes an appropriate object to the invoked block' do
@@ -25,7 +25,7 @@ describe Githook do
25
25
  end
26
26
 
27
27
  mybot.on('pull_request').when('opened').perform(&complex_handler)
28
- expect{ mybot.process(WEBHOOK_REQUESTS['spec/webhooks/opened_pr.json']) }.to_not raise_error
28
+ expect{ mybot.process(WEBHOOK_REQUESTS['spec/webhooks/opened_pr.json'], 'pull_request') }.to_not raise_error
29
29
  end
30
30
  end
31
31
 
@@ -40,6 +40,11 @@ describe Githook do
40
40
  expect(mybot.registry['pull_request']).to eq(nil)
41
41
  end
42
42
 
43
+ it 'raises an error if registering an invalid Github event' do
44
+ handler = Proc.new {|pr| true}
45
+ expect{ mybot.on('crayons').when('opened').perform(&handler) }.to raise_error
46
+ end
47
+
43
48
  it "registers handler only for the event if no action is specified" do
44
49
  handler = Proc.new {|pr| true}
45
50
  mybot.on('pull_request').perform(&handler)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: githook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael West
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  executables: []
@@ -32,9 +46,10 @@ extra_rdoc_files: []
32
46
  files:
33
47
  - lib/githook.rb
34
48
  - lib/githook/bot.rb
35
- - lib/githook/bot_server.rb
36
49
  - lib/githook/event_objects/pull_request.rb
50
+ - lib/githook/server.rb
37
51
  - lib/githook/version.rb
52
+ - spec/bot_server_spec.rb
38
53
  - spec/githook_spec.rb
39
54
  - spec/spec_helper.rb
40
55
  - spec/webhooks/README.md
@@ -67,6 +82,7 @@ specification_version: 4
67
82
  summary: A Ruby DSL for defining your very own Github bot. The lingua franca for the
68
83
  DSL is Github webhook requests
69
84
  test_files:
85
+ - spec/bot_server_spec.rb
70
86
  - spec/githook_spec.rb
71
87
  - spec/spec_helper.rb
72
88
  - spec/webhooks/README.md
@@ -1,15 +0,0 @@
1
- =begin
2
- require 'rack'
3
-
4
- module Githook
5
- def self.listen(route = nil)
6
- app = Proc.new do |env|
7
- ['200', {'Content-Type' => 'application/json'}, ["{\"message\": \"Success\"}"]]
8
- end
9
-
10
- Rack::Server.new(
11
- server: 'webrick',
12
- Port: 3002
13
- end
14
- end
15
- =end