txgh-server 3.1.0 → 4.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/lib/txgh-server.rb +1 -0
  3. data/lib/txgh-server/application.rb +7 -1
  4. data/lib/txgh-server/download_handler.rb +1 -1
  5. data/lib/txgh-server/gitlab_request_auth.rb +13 -0
  6. data/lib/txgh-server/triggers/handler.rb +18 -1
  7. data/lib/txgh-server/triggers/pull_handler.rb +1 -10
  8. data/lib/txgh-server/triggers/push_handler.rb +1 -10
  9. data/lib/txgh-server/version.rb +1 -1
  10. data/lib/txgh-server/webhooks.rb +2 -0
  11. data/lib/txgh-server/webhooks/git.rb +15 -0
  12. data/lib/txgh-server/webhooks/git/blank_attributes.rb +20 -0
  13. data/lib/txgh-server/webhooks/git/delete_attributes.rb +54 -0
  14. data/lib/txgh-server/webhooks/git/delete_handler.rb +45 -0
  15. data/lib/txgh-server/webhooks/git/ping_handler.rb +21 -0
  16. data/lib/txgh-server/webhooks/git/push_attributes.rb +79 -0
  17. data/lib/txgh-server/webhooks/git/push_handler.rb +98 -0
  18. data/lib/txgh-server/webhooks/git/request_handler.rb +76 -0
  19. data/lib/txgh-server/webhooks/git/status_updater.rb +15 -0
  20. data/lib/txgh-server/webhooks/github/blank_attributes.rb +1 -13
  21. data/lib/txgh-server/webhooks/github/delete_attributes.rb +1 -38
  22. data/lib/txgh-server/webhooks/github/delete_handler.rb +1 -38
  23. data/lib/txgh-server/webhooks/github/ping_handler.rb +1 -12
  24. data/lib/txgh-server/webhooks/github/push_attributes.rb +1 -51
  25. data/lib/txgh-server/webhooks/github/push_handler.rb +2 -88
  26. data/lib/txgh-server/webhooks/github/request_handler.rb +2 -47
  27. data/lib/txgh-server/webhooks/github/status_updater.rb +1 -9
  28. data/lib/txgh-server/webhooks/gitlab.rb +14 -0
  29. data/lib/txgh-server/webhooks/gitlab/blank_attributes.rb +8 -0
  30. data/lib/txgh-server/webhooks/gitlab/delete_attributes.rb +17 -0
  31. data/lib/txgh-server/webhooks/gitlab/delete_handler.rb +8 -0
  32. data/lib/txgh-server/webhooks/gitlab/push_attributes.rb +29 -0
  33. data/lib/txgh-server/webhooks/gitlab/push_handler.rb +13 -0
  34. data/lib/txgh-server/webhooks/gitlab/request_handler.rb +59 -0
  35. data/lib/txgh-server/webhooks/gitlab/status_updater.rb +37 -0
  36. data/lib/txgh-server/webhooks/transifex/request_handler.rb +1 -1
  37. data/spec/application_spec.rb +11 -11
  38. data/spec/download_handler_spec.rb +6 -8
  39. data/spec/github_request_auth_spec.rb +6 -8
  40. data/spec/gitlab_request_auth_spec.rb +26 -0
  41. data/spec/helpers/gitlab_payload_builder.rb +132 -0
  42. data/spec/integration/hooks_spec.rb +1 -1
  43. data/spec/tgz_stream_response_spec.rb +2 -4
  44. data/spec/transifex_request_auth_spec.rb +6 -8
  45. data/spec/webhooks/github/delete_attributes_spec.rb +2 -5
  46. data/spec/webhooks/github/delete_handler_spec.rb +4 -7
  47. data/spec/webhooks/github/ping_handler_spec.rb +2 -5
  48. data/spec/webhooks/github/push_attributes_spec.rb +3 -6
  49. data/spec/webhooks/github/push_handler_spec.rb +5 -8
  50. data/spec/webhooks/github/request_handler_spec.rb +8 -10
  51. data/spec/webhooks/github/status_updater_spec.rb +2 -4
  52. data/spec/webhooks/gitlab/delete_attributes_spec.rb +27 -0
  53. data/spec/webhooks/gitlab/delete_handler_spec.rb +34 -0
  54. data/spec/webhooks/gitlab/push_attributes_spec.rb +64 -0
  55. data/spec/webhooks/gitlab/push_handler_spec.rb +99 -0
  56. data/spec/webhooks/gitlab/request_handler_spec.rb +73 -0
  57. data/spec/webhooks/gitlab/status_updater_spec.rb +66 -0
  58. data/spec/webhooks/transifex/hook_handler_spec.rb +2 -5
  59. data/spec/webhooks/transifex/request_handler_spec.rb +3 -5
  60. data/spec/zip_stream_response_spec.rb +2 -4
  61. data/txgh-server.gemspec +1 -2
  62. metadata +35 -10
  63. data/spec/integration/payloads/github_postbody_l10n.json +0 -136
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+
3
+ module TxghServer
4
+ module Webhooks
5
+ module Git
6
+ class RequestHandler
7
+ include ResponseHelpers
8
+
9
+ class << self
10
+ def handle_request(request, logger)
11
+ new(request, logger).handle_request
12
+ end
13
+ end
14
+
15
+ attr_reader :request, :logger
16
+
17
+ def initialize(request, logger)
18
+ @request = request
19
+ @logger = logger
20
+ end
21
+
22
+ def handle_request
23
+ raise NotImplementedError
24
+ end
25
+
26
+ private
27
+
28
+ def attributes
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def handle_safely
33
+ if authentic_request?
34
+ yield
35
+ else
36
+ respond_with_error(401, 'Unauthorized')
37
+ end
38
+ rescue => e
39
+ respond_with_error(500, "Internal server error: #{e.message}", e)
40
+ end
41
+
42
+ def payload
43
+ @payload ||= begin
44
+ if request.params[:payload]
45
+ JSON.parse(request.params[:payload])
46
+ else
47
+ JSON.parse(request.body.read)
48
+ end
49
+ rescue JSON::ParserError
50
+ {}
51
+ end
52
+ end
53
+
54
+ def git_repo_name
55
+ raise NotImplementedError
56
+ end
57
+
58
+ def config
59
+ @config ||= Txgh::Config::KeyManager.config_from_repo(git_repo_name)
60
+ end
61
+
62
+ def repo
63
+ config.git_repo
64
+ end
65
+
66
+ def project
67
+ config.transifex_project
68
+ end
69
+
70
+ def authentic_request?
71
+ raise NotImplementedError
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,15 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
4
+ class StatusUpdater
5
+ attr_reader :project, :repo, :branch
6
+
7
+ def initialize(project, repo, branch)
8
+ @project = project
9
+ @repo = repo
10
+ @branch = branch
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,19 +1,7 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
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
4
+ class BlankAttributes < TxghServer::Webhooks::Git::BlankAttributes
17
5
  end
18
6
  end
19
7
  end
@@ -1,53 +1,16 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
3
  module Github
4
- class DeleteAttributes
5
- ATTRIBUTES = [
6
- :event, :repo_name, :ref, :ref_type
7
- ]
8
-
4
+ class DeleteAttributes < TxghServer::Webhooks::Git::DeleteAttributes
9
5
  class << self
10
- def from_webhook_payload(payload)
11
- new(
12
- ATTRIBUTES.each_with_object({}) do |attr, ret|
13
- ret[attr] = public_send(attr, payload)
14
- end
15
- )
16
- end
17
-
18
- def event(payload)
19
- 'delete'
20
- end
21
-
22
6
  def repo_name(payload)
23
7
  payload.fetch('repository').fetch('full_name')
24
8
  end
25
9
 
26
- def ref(payload)
27
- payload.fetch('ref')
28
- end
29
-
30
10
  def ref_type(payload)
31
11
  payload.fetch('ref_type')
32
12
  end
33
13
  end
34
-
35
- attr_reader *ATTRIBUTES
36
-
37
- def initialize(options = {})
38
- ATTRIBUTES.each do |attr|
39
- instance_variable_set(
40
- "@#{attr}", options.fetch(attr) { options.fetch(attr.to_s) }
41
- )
42
- end
43
- end
44
-
45
- def to_h
46
- ATTRIBUTES.each_with_object({}) do |attr, ret|
47
- ret[attr] = public_send(attr)
48
- end
49
- end
50
-
51
14
  end
52
15
  end
53
16
  end
@@ -1,44 +1,7 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
3
  module Github
4
- class DeleteHandler
5
- include ResponseHelpers
6
-
7
- attr_reader :project, :repo, :logger, :attributes
8
-
9
- def initialize(project, repo, logger, attributes)
10
- @project = project
11
- @repo = repo
12
- @logger = logger
13
- @attributes = attributes
14
- end
15
-
16
- def execute
17
- perform_delete if should_handle_request?
18
- respond_with(200, true)
19
- end
20
-
21
- private
22
-
23
- def perform_delete
24
- deleter.delete_resources
25
- end
26
-
27
- def deleter
28
- @deleter ||= Txgh::ResourceDeleter.new(project, repo, branch)
29
- end
30
-
31
- def should_handle_request?
32
- # ref_type can be either 'branch' or 'tag' - we only care about branches
33
- attributes.ref_type == 'branch' &&
34
- repo.should_process_ref?(branch) &&
35
- project.auto_delete_resources?
36
- end
37
-
38
- def branch
39
- Txgh::Utils.absolute_branch(attributes.ref.sub(/^refs\//, ''))
40
- end
41
-
4
+ class DeleteHandler < TxghServer::Webhooks::Git::DeleteHandler
42
5
  end
43
6
  end
44
7
  end
@@ -3,18 +3,7 @@ module TxghServer
3
3
  module Github
4
4
  # Handles github's ping event, which is a test event fired whenever a new
5
5
  # webhook is set up.
6
- class PingHandler
7
- include ResponseHelpers
8
-
9
- attr_reader :logger
10
-
11
- def initialize(logger)
12
- @logger = logger
13
- end
14
-
15
- def execute
16
- respond_with(200, {})
17
- end
6
+ class PingHandler < TxghServer::Webhooks::Git::PingHandler
18
7
  end
19
8
  end
20
9
  end
@@ -1,41 +1,12 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
3
  module Github
4
- class PushAttributes
5
- ATTRIBUTES = [
6
- :event, :repo_name, :ref, :before, :after,
7
- :added_files, :modified_files, :author
8
- ]
9
-
4
+ class PushAttributes < TxghServer::Webhooks::Git::PushAttributes
10
5
  class << self
11
- def from_webhook_payload(payload)
12
- new(
13
- ATTRIBUTES.each_with_object({}) do |attr, ret|
14
- ret[attr] = public_send(attr, payload)
15
- end
16
- )
17
- end
18
-
19
- def event(payload)
20
- 'push'
21
- end
22
-
23
6
  def repo_name(payload)
24
7
  payload.fetch('repository').fetch('full_name')
25
8
  end
26
9
 
27
- def ref(payload)
28
- payload.fetch('ref')
29
- end
30
-
31
- def before(payload)
32
- payload.fetch('before')
33
- end
34
-
35
- def after(payload)
36
- payload.fetch('after')
37
- end
38
-
39
10
  def added_files(payload)
40
11
  extract_files(payload, 'added')
41
12
  end
@@ -57,27 +28,6 @@ module TxghServer
57
28
  payload.fetch('commits').flat_map { |c| c[state] }.uniq
58
29
  end
59
30
  end
60
-
61
- def files
62
- @files ||= added_files + modified_files
63
- end
64
-
65
- attr_reader *ATTRIBUTES
66
-
67
- def initialize(options = {})
68
- ATTRIBUTES.each do |attr|
69
- instance_variable_set(
70
- "@#{attr}", options.fetch(attr) { options.fetch(attr.to_s) }
71
- )
72
- end
73
- end
74
-
75
- def to_h
76
- ATTRIBUTES.each_with_object({}) do |attr, ret|
77
- ret[attr] = public_send(attr)
78
- end
79
- end
80
-
81
31
  end
82
32
  end
83
33
  end
@@ -1,98 +1,12 @@
1
- require 'set'
2
- require 'octokit'
3
- require 'txgh'
4
-
5
1
  module TxghServer
6
2
  module Webhooks
7
3
  module Github
8
- class PushHandler
9
- include ResponseHelpers
10
-
11
- attr_reader :project, :repo, :logger, :attributes
12
-
13
- def initialize(project, repo, logger, attributes)
14
- @project = project
15
- @repo = repo
16
- @logger = logger
17
- @attributes = attributes
18
- end
19
-
20
- def execute
21
- # Check if the branch in the hook data is the configured branch we want
22
- logger.info("request github branch: #{branch}")
23
- logger.info("config github branch: #{repo.github_config_branch}")
24
-
25
- if should_process?
26
- logger.info('found branch in github request')
27
-
28
- pusher.push_resources(added_and_modified_resources) do |tx_resource|
29
- # block should return categories for the passed-in resource
30
- { 'author' => attributes.author }
31
- end
32
-
33
- status_updater.update_status
34
- end
35
-
36
- respond_with(200, true)
37
- rescue => e
38
- status_updater.report_error_and_update_status(e)
39
- respond_with_error(500, "Internal server error: #{e.message}", e)
40
- end
41
-
4
+ class PushHandler < TxghServer::Webhooks::Git::PushHandler
42
5
  private
43
6
 
44
- def pusher
45
- @pusher ||= Txgh::Pusher.new(project, repo, branch)
46
- end
47
-
48
7
  def status_updater
49
- @status_updater = StatusUpdater.new(project, repo, branch)
8
+ @status_updater = TxghServer::Webhooks::Github::StatusUpdater.new(project, repo, branch)
50
9
  end
51
-
52
- # finds the resources that were updated in each commit
53
- def added_and_modified_resources
54
- @amr ||= attributes.files.each_with_object(Set.new) do |file, ret|
55
- logger.info("processing added/modified file: #{file}")
56
-
57
- if tx_resources.include?(file)
58
- ret << tx_resources[file]
59
- end
60
- end
61
- end
62
-
63
- # Build an index of known Tx resources, by source file
64
- def tx_resources
65
- @tx_resources ||= tx_config.resources.each_with_object({}) do |resource, ret|
66
- ret[resource.source_file] = if repo.process_all_branches?
67
- Txgh::TxBranchResource.new(resource, branch) # maybe find instead?
68
- else
69
- resource
70
- end
71
- end
72
- end
73
-
74
- def tx_config
75
- @tx_config ||= Txgh::Config::TxManager.tx_config(project, repo, branch)
76
- end
77
-
78
- def branch
79
- @ref ||= attributes.ref.sub(/^refs\//, '')
80
- end
81
-
82
- def should_process?
83
- should_process_branch? && should_process_commit?
84
- end
85
-
86
- def should_process_branch?
87
- repo.should_process_ref?(branch)
88
- end
89
-
90
- def should_process_commit?
91
- # return false if 'after' commit sha is all zeroes (indicates branch
92
- # has been deleted)
93
- !((attributes.after || '') =~ /\A0+\z/)
94
- end
95
-
96
10
  end
97
11
  end
98
12
  end
@@ -3,22 +3,7 @@ require 'json'
3
3
  module TxghServer
4
4
  module Webhooks
5
5
  module Github
6
- class RequestHandler
7
- include ResponseHelpers
8
-
9
- class << self
10
- def handle_request(request, logger)
11
- new(request, logger).handle_request
12
- end
13
- end
14
-
15
- attr_reader :request, :logger
16
-
17
- def initialize(request, logger)
18
- @request = request
19
- @logger = logger
20
- end
21
-
6
+ class RequestHandler < TxghServer::Webhooks::Git::RequestHandler
22
7
  def handle_request
23
8
  handle_safely do
24
9
  case github_event
@@ -47,44 +32,14 @@ module TxghServer
47
32
  end
48
33
  end
49
34
 
50
- def handle_safely
51
- if authentic_request?
52
- yield
53
- else
54
- respond_with_error(401, 'Unauthorized')
55
- end
56
- rescue => e
57
- respond_with_error(500, "Internal server error: #{e.message}", e)
58
- end
59
-
60
- def payload
61
- @payload ||= begin
62
- if request.params[:payload]
63
- JSON.parse(request.params[:payload])
64
- else
65
- JSON.parse(request.body.read)
66
- end
67
- rescue JSON::ParserError
68
- {}
69
- end
70
- end
71
-
72
35
  def github_event
73
36
  request.env['HTTP_X_GITHUB_EVENT']
74
37
  end
75
38
 
76
- def github_repo_name
39
+ def git_repo_name
77
40
  payload.fetch('repository', {})['full_name']
78
41
  end
79
42
 
80
- def config
81
- @config ||= Txgh::Config::KeyManager.config_from_repo(github_repo_name)
82
- end
83
-
84
- def repo
85
- config.github_repo
86
- end
87
-
88
43
  def project
89
44
  config.transifex_project
90
45
  end