txgh-server 2.1.1 → 2.2.0

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: 08999181efe8b160394afe81270055aea22ba8df
4
- data.tar.gz: 80f4a180a3a7d20f97adb2c587bcf884f3ba85c1
3
+ metadata.gz: 0ed650a35c78fd2a4a616a5e106aceea35eae654
4
+ data.tar.gz: a8df5a3a0ab04c9e30c86444dd06c49bf5127768
5
5
  SHA512:
6
- metadata.gz: 11edfef929681456b7e0977a34a67b99e658341e82e6f6a71f18034fada792614eaf4d3547f14ee9205c6d4fc0a14fe944676c6e0cf1c8e1ca8df1bf6b4716e4
7
- data.tar.gz: ee273984933596d9363ecf632e2f85a895fc06559c6458f7244946459dac03f9b7350a603f6069fc5c825980bfb444720e08837886f4b607bc20c109945fe96c
6
+ metadata.gz: ece1db6aa35c8d561d6e8c1f8335190e23e0dc97d32ed90d4f1a83e38fdeaceef90bf86ceb46aab83c6ab13d760b999e726362df45a6a4568f619b4f5a59b403
7
+ data.tar.gz: f9bdf5680ac0306ffa52564e7b8d31733d7e60e7bac5ae250086520ab3266e4e22325bb230bf0c42b7bd063d451fd6c9806dd1f824b30b5e66175a1a691e2b61
@@ -1,6 +1,5 @@
1
1
  require 'sinatra'
2
2
  require 'sinatra/json'
3
- require 'sinatra/reloader'
4
3
  require 'sinatra/streaming'
5
4
 
6
5
  module TxghServer
@@ -37,10 +36,6 @@ module TxghServer
37
36
  set :logger, logger
38
37
  end
39
38
 
40
- def initialize(app = nil)
41
- super(app)
42
- end
43
-
44
39
  get '/health_check' do
45
40
  respond_with(
46
41
  Response.new(200, {})
@@ -89,14 +84,6 @@ module TxghServer
89
84
  set :logger, logger
90
85
  end
91
86
 
92
- configure :development do
93
- register Sinatra::Reloader
94
- end
95
-
96
- def initialize(app = nil)
97
- super(app)
98
- end
99
-
100
87
  post '/transifex' do
101
88
  respond_with(
102
89
  Transifex::RequestHandler.handle_request(request, settings.logger)
@@ -121,10 +108,6 @@ module TxghServer
121
108
  set :logger, logger
122
109
  end
123
110
 
124
- configure :development do
125
- register Sinatra::Reloader
126
- end
127
-
128
111
  patch '/push' do
129
112
  respond_with(
130
113
  PushHandler.handle_request(request, settings.logger)
@@ -1,3 +1,3 @@
1
1
  module TxghServer
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -0,0 +1,20 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Github
4
+ class BlankAttributes
5
+ class << self
6
+ def from_webhook_payload(payload)
7
+ new
8
+ end
9
+ end
10
+
11
+ def initialize(options = {})
12
+ end
13
+
14
+ def to_h
15
+ {}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -21,21 +21,32 @@ module TxghServer
21
21
 
22
22
  def handle_request
23
23
  handle_safely do
24
- case request.env['HTTP_X_GITHUB_EVENT']
24
+ case github_event
25
25
  when 'push'
26
- handle_push
26
+ PushHandler.new(project, repo, logger, attributes).execute
27
27
  when 'delete'
28
- handle_delete
28
+ DeleteHandler.new(project, repo, logger, attributes).execute
29
29
  when 'ping'
30
- handle_ping
30
+ PingHandler.new(logger).execute
31
31
  else
32
- handle_unexpected
32
+ respond_with_error(400, 'Unexpected event type')
33
33
  end
34
34
  end
35
35
  end
36
36
 
37
37
  private
38
38
 
39
+ def attributes
40
+ case github_event
41
+ when 'push'
42
+ PushAttributes.from_webhook_payload(payload)
43
+ when 'delete'
44
+ DeleteAttributes.from_webhook_payload(payload)
45
+ else
46
+ BlankAttributes.from_webhook_payload(payload)
47
+ end
48
+ end
49
+
39
50
  def handle_safely
40
51
  if authentic_request?
41
52
  yield
@@ -46,24 +57,6 @@ module TxghServer
46
57
  respond_with_error(500, "Internal server error: #{e.message}", e)
47
58
  end
48
59
 
49
- def handle_push
50
- attributes = PushAttributes.from_webhook_payload(payload)
51
- PushHandler.new(project, repo, logger, attributes).execute
52
- end
53
-
54
- def handle_delete
55
- attributes = DeleteAttributes.from_webhook_payload(payload)
56
- DeleteHandler.new(project, repo, logger, attributes).execute
57
- end
58
-
59
- def handle_ping
60
- PingHandler.new(logger).execute
61
- end
62
-
63
- def handle_unexpected
64
- respond_with_error(400, 'Unexpected event type')
65
- end
66
-
67
60
  def payload
68
61
  @payload ||= begin
69
62
  if request.params[:payload]
@@ -76,6 +69,10 @@ module TxghServer
76
69
  end
77
70
  end
78
71
 
72
+ def github_event
73
+ request.env['HTTP_X_GITHUB_EVENT']
74
+ end
75
+
79
76
  def github_repo_name
80
77
  payload.fetch('repository', {})['full_name']
81
78
  end
@@ -1,6 +1,7 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
3
  module Github
4
+ autoload :BlankAttributes, 'txgh-server/webhooks/github/blank_attributes'
4
5
  autoload :DeleteAttributes, 'txgh-server/webhooks/github/delete_attributes'
5
6
  autoload :DeleteHandler, 'txgh-server/webhooks/github/delete_handler'
6
7
  autoload :Handler, 'txgh-server/webhooks/github/handler'
@@ -5,11 +5,9 @@ module TxghServer
5
5
  module Transifex
6
6
  class RequestHandler
7
7
  class << self
8
-
9
8
  def handle_request(request, logger)
10
- new(request, logger).handle(TxghServer::Webhooks::Transifex::HookHandler)
9
+ new(request, logger).handle_request
11
10
  end
12
-
13
11
  end
14
12
 
15
13
  include ResponseHelpers
@@ -21,13 +19,13 @@ module TxghServer
21
19
  @logger = logger
22
20
  end
23
21
 
24
- def handle(klass)
22
+ def handle_request
25
23
  handle_safely do
26
- handler = klass.new(
24
+ handler = TxghServer::Webhooks::Transifex::HookHandler.new(
27
25
  project: config.transifex_project,
28
26
  repo: config.github_repo,
29
- resource_slug: payload['resource'],
30
- language: payload['language'],
27
+ resource_slug: payload[:resource],
28
+ language: payload[:language],
31
29
  logger: logger
32
30
  )
33
31
 
@@ -62,13 +60,16 @@ module TxghServer
62
60
  end
63
61
 
64
62
  def config
65
- @config ||= Txgh::Config::KeyManager.config_from_project(payload['project'])
63
+ @config ||= Txgh::Config::KeyManager.config_from_project(payload[:project])
66
64
  end
67
65
 
68
66
  def payload
69
67
  @payload ||= begin
70
68
  request.body.rewind
71
- Hash[URI.decode_www_form(request.body.read)]
69
+
70
+ Txgh::Utils.deep_symbolize_keys(
71
+ Hash[URI.decode_www_form(request.body.read)]
72
+ )
72
73
  end
73
74
  end
74
75
 
data/lib/txgh-server.rb CHANGED
@@ -3,20 +3,12 @@ require 'txgh'
3
3
  module TxghServer
4
4
  autoload :Application, 'txgh-server/application'
5
5
  autoload :DownloadHandler, 'txgh-server/download_handler'
6
-
7
- # @TODO: refactor/remove
8
- autoload :WebhookEndpoints, 'txgh-server/application'
9
-
10
6
  autoload :GithubRequestAuth, 'txgh-server/github_request_auth'
11
7
  autoload :Response, 'txgh-server/response'
12
8
  autoload :ResponseHelpers, 'txgh-server/response_helpers'
13
9
  autoload :StreamResponse, 'txgh-server/stream_response'
14
10
  autoload :TgzStreamResponse, 'txgh-server/tgz_stream_response'
15
11
  autoload :TransifexRequestAuth, 'txgh-server/transifex_request_auth'
16
-
17
- # @TODO: refactor/remove
18
- autoload :TriggerEndpoints, 'txgh-server/application'
19
-
20
12
  autoload :Triggers, 'txgh-server/triggers'
21
13
  autoload :Webhooks, 'txgh-server/webhooks'
22
14
  autoload :ZipStreamResponse, 'txgh-server/zip_stream_response'
@@ -0,0 +1,10 @@
1
+ class TestRequest
2
+ attr_reader :body, :params, :headers
3
+ alias_method :env, :headers
4
+
5
+ def initialize(body: , params: {}, headers: {})
6
+ @body = StringIO.new(body)
7
+ @params = params
8
+ @headers = headers
9
+ end
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ $:.push(File.join(Gem.loaded_specs['txgh'].full_gem_path, 'spec'))
3
3
  require 'pry-byebug'
4
4
  require 'rake'
5
5
  require 'rspec'
6
+ require 'txgh'
6
7
  require 'txgh-server'
7
8
  require 'vcr'
8
9
  require 'webmock'
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'helpers/github_payload_builder'
3
+ require 'helpers/nil_logger'
4
+ require 'helpers/standard_txgh_setup'
5
+ require 'helpers/test_request'
6
+
7
+ include TxghServer::Webhooks
8
+
9
+ describe Github::RequestHandler do
10
+ include StandardTxghSetup
11
+
12
+ let(:logger) { NilLogger.new }
13
+ let(:ref) { 'heads/my_ref' }
14
+ let(:headers) { { 'HTTP_X_GITHUB_EVENT' => event } }
15
+ let(:body) { payload.to_json }
16
+ let(:request) { TestRequest.new(body: body, headers: headers) }
17
+ let(:handler) { described_class.new(request, logger) }
18
+
19
+ describe '#handle_request' do
20
+ context 'push event' do
21
+ let(:event) { 'push' }
22
+ let(:payload) { GithubPayloadBuilder.push_payload(repo_name, ref).tap { |p| p.add_commit } }
23
+
24
+ it 'does not execute if unauthorized' do
25
+ expect_any_instance_of(Github::PushHandler).to_not receive(:execute)
26
+ response = handler.handle_request
27
+ expect(response.status).to eq(401)
28
+ end
29
+
30
+ context 'with an authentic request' do
31
+ before(:each) do
32
+ allow(handler).to receive(:authentic_request?).and_return(true)
33
+ end
34
+
35
+ it 'handles the request with the push handler' do
36
+ expect_any_instance_of(Github::PushHandler).to receive(:execute).and_return(:response)
37
+ expect(handler.handle_request).to eq(:response)
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'delete event' do
43
+ let(:event) { 'delete' }
44
+ let(:payload) { GithubPayloadBuilder.delete_payload(repo_name, ref) }
45
+
46
+ it 'does not execute if unauthorized' do
47
+ expect_any_instance_of(Github::DeleteHandler).to_not receive(:execute)
48
+ response = handler.handle_request
49
+ expect(response.status).to eq(401)
50
+ end
51
+
52
+ context 'with an authentic request' do
53
+ before(:each) do
54
+ allow(handler).to receive(:authentic_request?).and_return(true)
55
+ end
56
+
57
+ it 'handles the request with the delete handler' do
58
+ expect_any_instance_of(Github::DeleteHandler).to receive(:execute).and_return(:response)
59
+ expect(handler.handle_request).to eq(:response)
60
+ end
61
+ end
62
+ end
63
+
64
+ context 'unrecognized event' do
65
+ let(:event) { 'foo' }
66
+ let(:payload) { { repository: { full_name: repo_name } } }
67
+
68
+ it 'responds with a 400' do
69
+ allow(handler).to receive(:authentic_request?).and_return(true)
70
+ response = handler.handle_request
71
+ expect(response.status).to eq(400)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'helpers/nil_logger'
3
+ require 'helpers/standard_txgh_setup'
4
+ require 'helpers/test_request'
5
+
6
+ include TxghServer::Webhooks
7
+
8
+ describe Transifex::RequestHandler do
9
+ include StandardTxghSetup
10
+
11
+ let(:logger) { NilLogger.new }
12
+ let(:body) { URI.encode_www_form(payload.to_a) }
13
+ let(:request) { TestRequest.new(body: body) }
14
+ let(:handler) { described_class.new(request, logger) }
15
+ let(:payload) { { 'project' => project_name, 'resource' => resource_slug, 'language' => 'pt' } }
16
+
17
+ describe '#handle_request' do
18
+ it 'does not execute if unauthorized' do
19
+ expect_any_instance_of(Transifex::HookHandler).to_not receive(:execute)
20
+ response = handler.handle_request
21
+ expect(response.status).to eq(401)
22
+ end
23
+
24
+ context 'with an authentic request' do
25
+ before(:each) do
26
+ allow(handler).to receive(:authentic_request?).and_return(true)
27
+ end
28
+
29
+ it 'handles the request with the hook handler' do
30
+ expect_any_instance_of(Transifex::HookHandler).to receive(:execute).and_return(:response)
31
+ expect(handler.handle_request).to eq(:response)
32
+ end
33
+ end
34
+ end
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txgh-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Jackowski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-10 00:00:00.000000000 Z
12
+ date: 2016-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mime-types
@@ -112,6 +112,7 @@ files:
112
112
  - lib/txgh-server/version.rb
113
113
  - lib/txgh-server/webhooks.rb
114
114
  - lib/txgh-server/webhooks/github.rb
115
+ - lib/txgh-server/webhooks/github/blank_attributes.rb
115
116
  - lib/txgh-server/webhooks/github/delete_attributes.rb
116
117
  - lib/txgh-server/webhooks/github/delete_handler.rb
117
118
  - lib/txgh-server/webhooks/github/ping_handler.rb
@@ -127,6 +128,7 @@ files:
127
128
  - spec/github_request_auth_spec.rb
128
129
  - spec/helpers/github_payload_builder.rb
129
130
  - spec/helpers/integration_setup.rb
131
+ - spec/helpers/test_request.rb
130
132
  - spec/integration/cassettes/pull.yml
131
133
  - spec/integration/cassettes/push.yml
132
134
  - spec/integration/cassettes/transifex_hook_endpoint.yml
@@ -144,7 +146,9 @@ files:
144
146
  - spec/webhooks/github/ping_handler_spec.rb
145
147
  - spec/webhooks/github/push_attributes_spec.rb
146
148
  - spec/webhooks/github/push_handler_spec.rb
149
+ - spec/webhooks/github/request_handler_spec.rb
147
150
  - spec/webhooks/transifex/hook_handler_spec.rb
151
+ - spec/webhooks/transifex/request_handler_spec.rb
148
152
  - spec/zip_stream_response_spec.rb
149
153
  - txgh-server.gemspec
150
154
  homepage: https://github.com/lumoslabs/txgh