txgh-queue 1.0.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.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/lib/txgh-queue/application.rb +35 -0
  3. data/lib/txgh-queue/backends/null.rb +19 -0
  4. data/lib/txgh-queue/backends/sqs/config.rb +29 -0
  5. data/lib/txgh-queue/backends/sqs/consumer.rb +30 -0
  6. data/lib/txgh-queue/backends/sqs/history_sequence.rb +62 -0
  7. data/lib/txgh-queue/backends/sqs/job.rb +115 -0
  8. data/lib/txgh-queue/backends/sqs/message_attributes.rb +33 -0
  9. data/lib/txgh-queue/backends/sqs/producer.rb +33 -0
  10. data/lib/txgh-queue/backends/sqs/queue.rb +45 -0
  11. data/lib/txgh-queue/backends/sqs/retry_logic.rb +118 -0
  12. data/lib/txgh-queue/backends/sqs.rb +36 -0
  13. data/lib/txgh-queue/backends.rb +22 -0
  14. data/lib/txgh-queue/config.rb +48 -0
  15. data/lib/txgh-queue/error_handlers/github.rb +47 -0
  16. data/lib/txgh-queue/error_handlers/server_response.rb +22 -0
  17. data/lib/txgh-queue/error_handlers/standard_errors.rb +15 -0
  18. data/lib/txgh-queue/error_handlers/transifex.rb +23 -0
  19. data/lib/txgh-queue/error_handlers/txgh_errors.rb +27 -0
  20. data/lib/txgh-queue/error_handlers.rb +9 -0
  21. data/lib/txgh-queue/job.rb +77 -0
  22. data/lib/txgh-queue/result.rb +26 -0
  23. data/lib/txgh-queue/status.rb +51 -0
  24. data/lib/txgh-queue/supervisor.rb +76 -0
  25. data/lib/txgh-queue/version.rb +3 -0
  26. data/lib/txgh-queue/webhooks/github/request_handler.rb +34 -0
  27. data/lib/txgh-queue/webhooks/github.rb +7 -0
  28. data/lib/txgh-queue/webhooks/transifex/request_handler.rb +21 -0
  29. data/lib/txgh-queue/webhooks/transifex.rb +7 -0
  30. data/lib/txgh-queue/webhooks.rb +6 -0
  31. data/lib/txgh-queue.rb +14 -0
  32. data/spec/application_spec.rb +97 -0
  33. data/spec/backends/sqs/config_spec.rb +30 -0
  34. data/spec/backends/sqs/consumer_spec.rb +34 -0
  35. data/spec/backends/sqs/history_sequence_spec.rb +75 -0
  36. data/spec/backends/sqs/job_spec.rb +189 -0
  37. data/spec/backends/sqs/message_attributes_spec.rb +64 -0
  38. data/spec/backends/sqs/producer_spec.rb +32 -0
  39. data/spec/backends/sqs/queue_spec.rb +65 -0
  40. data/spec/backends/sqs/retry_logic_spec.rb +157 -0
  41. data/spec/backends/sqs_spec.rb +57 -0
  42. data/spec/backends_spec.rb +31 -0
  43. data/spec/config_spec.rb +15 -0
  44. data/spec/error_handlers/github_spec.rb +30 -0
  45. data/spec/error_handlers/server_response_spec.rb +36 -0
  46. data/spec/error_handlers/standard_errors_spec.rb +22 -0
  47. data/spec/error_handlers/transifex_spec.rb +43 -0
  48. data/spec/error_handlers/txgh_errors_spec.rb +25 -0
  49. data/spec/helpers/env_helpers.rb +13 -0
  50. data/spec/helpers/nil_logger.rb +10 -0
  51. data/spec/helpers/sqs/sqs_test_message.rb +47 -0
  52. data/spec/helpers/test_backend.rb +54 -0
  53. data/spec/job_spec.rb +111 -0
  54. data/spec/result_spec.rb +63 -0
  55. data/spec/spec_helper.rb +66 -0
  56. data/spec/status_spec.rb +68 -0
  57. data/spec/supervisor_spec.rb +40 -0
  58. data/spec/webhooks/github/request_handler_spec.rb +145 -0
  59. data/spec/webhooks/transifex/request_handler_spec.rb +87 -0
  60. data/txgh-queue.gemspec +24 -0
  61. metadata +172 -0
@@ -0,0 +1,145 @@
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 TxghQueue::Webhooks
8
+
9
+ describe Github::RequestHandler, auto_configure: true 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
+ let(:queue_config) { {} }
21
+ let(:payload) { { repository: { full_name: repo_name } } }
22
+ let(:event) { 'push' }
23
+
24
+ it "responds with an error when a queue backend isn't configured" do
25
+ allow(handler).to receive(:authentic_request?).and_return(true)
26
+ response = handler.handle_request
27
+
28
+ expect(response.body.first[:error]).to(
29
+ eq('Internal server error: No queue backend has been configured')
30
+ )
31
+
32
+ expect(response.status).to eq(500)
33
+ end
34
+
35
+ context 'with a queue configured' do
36
+ let(:queue_config) do
37
+ {
38
+ backend: 'test',
39
+ options: {
40
+ queues: %w(test-queue)
41
+ }
42
+ }
43
+ end
44
+
45
+ let(:backend) { TxghQueue::Config.backend }
46
+ let(:producer) { backend.producer_for("github.#{event}") }
47
+
48
+ context 'push event' do
49
+ let(:event) { 'push' }
50
+ let(:payload) { GithubPayloadBuilder.push_payload(repo_name, ref).tap { |p| p.add_commit } }
51
+
52
+ it 'does not enqueue if unauthorized' do
53
+ expect { handler.handle_request }.to_not(
54
+ change { producer.enqueued_jobs.size }
55
+ )
56
+ end
57
+
58
+ it 'responds with an unauthorized status' do
59
+ response = handler.handle_request
60
+ expect(response.status).to eq(401)
61
+ end
62
+
63
+ context 'with an authentic request' do
64
+ before(:each) do
65
+ allow(handler).to receive(:authentic_request?).and_return(true)
66
+ end
67
+
68
+ it 'enqueues the job' do
69
+ expect { handler.handle_request }.to(
70
+ change { producer.enqueued_jobs.size }.from(0).to(1)
71
+ )
72
+ end
73
+
74
+ it 'enqueues with the correct parameters' do
75
+ handler.handle_request
76
+ params = producer.enqueued_jobs.first
77
+ expect(params[:payload]).to include(
78
+ event: 'push',
79
+ txgh_event: 'github.push',
80
+ ref: "refs/#{ref}",
81
+ repo_name: repo_name
82
+ )
83
+ end
84
+
85
+ it 'responds with an ok status code' do
86
+ response = handler.handle_request
87
+ expect(response.status).to eq(202)
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'delete event' do
93
+ let(:event) { 'delete' }
94
+ let(:payload) { GithubPayloadBuilder.delete_payload(repo_name, ref) }
95
+
96
+ it 'does not enqueue if unauthorized' do
97
+ expect { handler.handle_request }.to_not change { producer.enqueued_jobs.size }
98
+ end
99
+
100
+ it 'responds with an unauthorized status' do
101
+ response = handler.handle_request
102
+ expect(response.status).to eq(401)
103
+ end
104
+
105
+ context 'with an authentic request' do
106
+ before(:each) do
107
+ allow(handler).to receive(:authentic_request?).and_return(true)
108
+ end
109
+
110
+ it 'enqueues the job' do
111
+ expect { handler.handle_request }.to(
112
+ change { producer.enqueued_jobs.size }.from(0).to(1)
113
+ )
114
+ end
115
+
116
+ it 'enqueues with the correct parameters' do
117
+ handler.handle_request
118
+ params = producer.enqueued_jobs.first
119
+ expect(params[:payload]).to include(
120
+ event: 'delete',
121
+ txgh_event: 'github.delete',
122
+ ref: "refs/#{ref}",
123
+ repo_name: repo_name
124
+ )
125
+ end
126
+
127
+ it 'responds with an ok status code' do
128
+ response = handler.handle_request
129
+ expect(response.status).to eq(202)
130
+ end
131
+ end
132
+ end
133
+
134
+ context 'unrecognized event' do
135
+ let(:event) { 'foo' }
136
+
137
+ it 'responds with a 400' do
138
+ allow(handler).to receive(:authentic_request?).and_return(true)
139
+ response = handler.handle_request
140
+ expect(response.status).to eq(400)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require 'helpers/nil_logger'
3
+ require 'helpers/standard_txgh_setup'
4
+ require 'helpers/test_request'
5
+ require 'helpers/test_backend'
6
+
7
+ include TxghQueue::Webhooks
8
+
9
+ describe Transifex::RequestHandler, auto_configure: true do
10
+ include StandardTxghSetup
11
+
12
+ let(:logger) { NilLogger.new }
13
+ let(:body) { URI.encode_www_form(payload.to_a) }
14
+ let(:request) { TestRequest.new(body: body) }
15
+ let(:handler) { described_class.new(request, logger) }
16
+ let(:payload) do
17
+ { 'project' => project_name, 'resource' => resource_slug, 'language' => 'pt' }
18
+ end
19
+
20
+ describe '#handle_request' do
21
+ let(:queue_config) { {} }
22
+
23
+ it "responds with an error when a queue backend isn't configured" do
24
+ allow(handler).to receive(:authentic_request?).and_return(true)
25
+ response = handler.handle_request
26
+
27
+ expect(response.body.first[:error]).to(
28
+ eq('Internal server error: No queue backend has been configured')
29
+ )
30
+
31
+ expect(response.status).to eq(500)
32
+ end
33
+
34
+ context 'with a queue configured' do
35
+ let(:queue_config) do
36
+ {
37
+ backend: 'test',
38
+ options: {
39
+ queues: %w(test-queue)
40
+ }
41
+ }
42
+ end
43
+
44
+ let(:backend) { TxghQueue::Config.backend }
45
+ let(:producer) { backend.producer_for("transifex.hook") }
46
+
47
+ it 'does not enqueue if unauthorized' do
48
+ expect { handler.handle_request }.to_not(
49
+ change { producer.enqueued_jobs.size }
50
+ )
51
+ end
52
+
53
+ it 'responds with an unauthorized status' do
54
+ response = handler.handle_request
55
+ expect(response.status).to eq(401)
56
+ end
57
+
58
+ context 'with an authentic request' do
59
+ before(:each) do
60
+ allow(handler).to receive(:authentic_request?).and_return(true)
61
+ end
62
+
63
+ it 'enqueues the job' do
64
+ expect { handler.handle_request }.to(
65
+ change { producer.enqueued_jobs.size }.from(0).to(1)
66
+ )
67
+ end
68
+
69
+ it 'enqueues with the correct parameters' do
70
+ handler.handle_request
71
+ params = producer.enqueued_jobs.first
72
+ expect(params[:payload]).to include(
73
+ txgh_event: 'transifex.hook',
74
+ project: project_name,
75
+ resource: resource_slug,
76
+ language: 'pt'
77
+ )
78
+ end
79
+
80
+ it 'responds with an ok status code' do
81
+ response = handler.handle_request
82
+ expect(response.status).to eq(202)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,24 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'txgh-queue/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'txgh-queue'
6
+ s.version = ::TxghQueue::VERSION
7
+ s.authors = ['Cameron Dutro']
8
+ s.email = ['camertron@gmail.com']
9
+ s.homepage = 'https://github.com/lumoslabs/txgh'
10
+
11
+ s.description = s.summary = 'Queue worker for processing Txgh webhooks.'
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+
16
+ s.add_dependency 'aws-sdk', '~> 2.0'
17
+ s.add_dependency 'txgh', '~> 6.0'
18
+ s.add_dependency 'txgh-server', '~> 2.2'
19
+ s.add_dependency 'sinatra', '~> 1.4'
20
+ s.add_dependency 'sinatra-contrib', '~> 1.4'
21
+
22
+ s.require_path = 'lib'
23
+ s.files = Dir['{lib,spec}/**/*', 'README.md', 'txgh-queue.gemspec', 'LICENSE']
24
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: txgh-queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: txgh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: txgh-server
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra-contrib
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ description: Queue worker for processing Txgh webhooks.
84
+ email:
85
+ - camertron@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/txgh-queue.rb
91
+ - lib/txgh-queue/application.rb
92
+ - lib/txgh-queue/backends.rb
93
+ - lib/txgh-queue/backends/null.rb
94
+ - lib/txgh-queue/backends/sqs.rb
95
+ - lib/txgh-queue/backends/sqs/config.rb
96
+ - lib/txgh-queue/backends/sqs/consumer.rb
97
+ - lib/txgh-queue/backends/sqs/history_sequence.rb
98
+ - lib/txgh-queue/backends/sqs/job.rb
99
+ - lib/txgh-queue/backends/sqs/message_attributes.rb
100
+ - lib/txgh-queue/backends/sqs/producer.rb
101
+ - lib/txgh-queue/backends/sqs/queue.rb
102
+ - lib/txgh-queue/backends/sqs/retry_logic.rb
103
+ - lib/txgh-queue/config.rb
104
+ - lib/txgh-queue/error_handlers.rb
105
+ - lib/txgh-queue/error_handlers/github.rb
106
+ - lib/txgh-queue/error_handlers/server_response.rb
107
+ - lib/txgh-queue/error_handlers/standard_errors.rb
108
+ - lib/txgh-queue/error_handlers/transifex.rb
109
+ - lib/txgh-queue/error_handlers/txgh_errors.rb
110
+ - lib/txgh-queue/job.rb
111
+ - lib/txgh-queue/result.rb
112
+ - lib/txgh-queue/status.rb
113
+ - lib/txgh-queue/supervisor.rb
114
+ - lib/txgh-queue/version.rb
115
+ - lib/txgh-queue/webhooks.rb
116
+ - lib/txgh-queue/webhooks/github.rb
117
+ - lib/txgh-queue/webhooks/github/request_handler.rb
118
+ - lib/txgh-queue/webhooks/transifex.rb
119
+ - lib/txgh-queue/webhooks/transifex/request_handler.rb
120
+ - spec/application_spec.rb
121
+ - spec/backends/sqs/config_spec.rb
122
+ - spec/backends/sqs/consumer_spec.rb
123
+ - spec/backends/sqs/history_sequence_spec.rb
124
+ - spec/backends/sqs/job_spec.rb
125
+ - spec/backends/sqs/message_attributes_spec.rb
126
+ - spec/backends/sqs/producer_spec.rb
127
+ - spec/backends/sqs/queue_spec.rb
128
+ - spec/backends/sqs/retry_logic_spec.rb
129
+ - spec/backends/sqs_spec.rb
130
+ - spec/backends_spec.rb
131
+ - spec/config_spec.rb
132
+ - spec/error_handlers/github_spec.rb
133
+ - spec/error_handlers/server_response_spec.rb
134
+ - spec/error_handlers/standard_errors_spec.rb
135
+ - spec/error_handlers/transifex_spec.rb
136
+ - spec/error_handlers/txgh_errors_spec.rb
137
+ - spec/helpers/env_helpers.rb
138
+ - spec/helpers/nil_logger.rb
139
+ - spec/helpers/sqs/sqs_test_message.rb
140
+ - spec/helpers/test_backend.rb
141
+ - spec/job_spec.rb
142
+ - spec/result_spec.rb
143
+ - spec/spec_helper.rb
144
+ - spec/status_spec.rb
145
+ - spec/supervisor_spec.rb
146
+ - spec/webhooks/github/request_handler_spec.rb
147
+ - spec/webhooks/transifex/request_handler_spec.rb
148
+ - txgh-queue.gemspec
149
+ homepage: https://github.com/lumoslabs/txgh
150
+ licenses: []
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.5.1
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Queue worker for processing Txgh webhooks.
172
+ test_files: []