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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4c9f1fb77b7c74f24d52f7c465388f5ede398508
4
- data.tar.gz: b85ae41e96ce548b1954d71b9deec88d1f6dc69b
2
+ SHA256:
3
+ metadata.gz: 624a1efbe56943dcd522f2f2c9bc580cc5669e3098a14df6f9d55df17fe878d9
4
+ data.tar.gz: e017f94f32be4224734a781c1b292c4bbde84543ed52ff12f3eda220071cff7f
5
5
  SHA512:
6
- metadata.gz: 31eec7e52a4d1d04fb7b6dae903afb050e8a68ebbebea4d64c13bace9e6494b1ba314c011e9bfbefa77a5c1a4889994e0f4654c671281d621d05078fb483501e
7
- data.tar.gz: 79d896e7ad7719183151f1420cdba37a175d0b4f493058abc2c490700cdc618f6849fe1d29e7a48f14ef4c61a9ea398eb5f51175713fdf3dce059d1f3dac5e0a
6
+ metadata.gz: 4f577c8c66c7492acd7754cc4f320559fa4397d5bcc190ae822c36042d4c16f37e7a696e7e1ab1e72af52dfeb159e50efbac4857aa220097fff33b1d7fdcb8a2
7
+ data.tar.gz: 4e4123765f6c42a35b57e8de274c106c571bf9255ec5e254db32c0c349afcde73024d31009cf6ab8a307abc14af0cae27027d931f3be62bbad4702d96ec44776
@@ -4,6 +4,7 @@ module TxghServer
4
4
  autoload :Application, 'txgh-server/application'
5
5
  autoload :DownloadHandler, 'txgh-server/download_handler'
6
6
  autoload :GithubRequestAuth, 'txgh-server/github_request_auth'
7
+ autoload :GitlabRequestAuth, 'txgh-server/gitlab_request_auth'
7
8
  autoload :Response, 'txgh-server/response'
8
9
  autoload :ResponseHelpers, 'txgh-server/response_helpers'
9
10
  autoload :StreamResponse, 'txgh-server/stream_response'
@@ -48,7 +48,7 @@ module TxghServer
48
48
 
49
49
  begin
50
50
  tx_config = Txgh::Config::TxManager.tx_config(
51
- config.transifex_project, config.github_repo, branch
51
+ config.transifex_project, config.git_repo, branch
52
52
  )
53
53
 
54
54
  data = tx_config.to_h
@@ -95,6 +95,12 @@ module TxghServer
95
95
  Github::RequestHandler.handle_request(request, settings.logger)
96
96
  )
97
97
  end
98
+
99
+ post '/gitlab' do
100
+ respond_with(
101
+ TxghServer::Webhooks::Gitlab::RequestHandler.handle_request(request, settings.logger)
102
+ )
103
+ end
98
104
  end
99
105
 
100
106
  class TriggerEndpoints < Sinatra::Base
@@ -9,7 +9,7 @@ module TxghServer
9
9
  def handle_request(request, logger = nil)
10
10
  handle_safely do
11
11
  config = config_from(request)
12
- project, repo = [config.transifex_project, config.github_repo]
12
+ project, repo = [config.transifex_project, config.git_repo]
13
13
  params = params_from(request)
14
14
  handler = new(project, repo, params, logger)
15
15
  handler.execute
@@ -0,0 +1,13 @@
1
+ module TxghServer
2
+ class GitlabRequestAuth
3
+ RACK_HEADER = 'HTTP_X_GITLAB_TOKEN'
4
+ GITLAB_HEADER = 'X-Gitlab-Token'
5
+
6
+ class << self
7
+ def authentic_request?(request, secret)
8
+ request_token = request.env[RACK_HEADER]
9
+ request_token == secret
10
+ end
11
+ end
12
+ end
13
+ end
@@ -27,7 +27,7 @@ module TxghServer
27
27
  def handler_for(config, request, logger)
28
28
  new(
29
29
  project: config.transifex_project,
30
- repo: config.github_repo,
30
+ repo: config.git_repo,
31
31
  branch: request.params.fetch('branch'),
32
32
  resource_slug: request.params.fetch('resource_slug'),
33
33
  logger: logger
@@ -45,6 +45,23 @@ module TxghServer
45
45
  @logger = options[:logger]
46
46
  end
47
47
 
48
+ private
49
+
50
+ def update_github_status
51
+ Txgh::GithubStatus.update(project, repo, branch)
52
+ rescue Octokit::UnprocessableEntity
53
+ # raised because we've tried to create too many statuses for the commit
54
+ rescue Txgh::TransifexNotFoundError
55
+ # raised if transifex resource can't be found
56
+ end
57
+
58
+ def update_gitlab_status
59
+ Txgh::GitlabStatus.update(project, repo, branch)
60
+ rescue ::Gitlab::Error::Unprocessable
61
+ # raised because we've tried to create too many statuses for the commit
62
+ rescue Txgh::TransifexNotFoundError
63
+ # raised if transifex resource can't be found
64
+ end
48
65
  end
49
66
  end
50
67
  end
@@ -7,24 +7,15 @@ module TxghServer
7
7
 
8
8
  def execute
9
9
  puller.pull_slug(resource_slug)
10
- update_github_status
10
+ repo.is_a?(Txgh::GithubRepo) ? update_github_status : update_gitlab_status
11
11
  respond_with(200, true)
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def update_github_status
17
- Txgh::GithubStatus.update(project, repo, branch)
18
- rescue Octokit::UnprocessableEntity
19
- # raised because we've tried to create too many statuses for the commit
20
- rescue Txgh::TransifexNotFoundError
21
- # raised if transifex resource can't be found
22
- end
23
-
24
16
  def puller
25
17
  @puller ||= Txgh::Puller.new(project, repo, branch)
26
18
  end
27
-
28
19
  end
29
20
  end
30
21
  end
@@ -7,24 +7,15 @@ module TxghServer
7
7
 
8
8
  def execute
9
9
  pusher.push_slug(resource_slug)
10
- update_github_status
10
+ repo.is_a?(Txgh::GithubRepo) ? update_github_status : update_gitlab_status
11
11
  respond_with(200, true)
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def update_github_status
17
- Txgh::GithubStatus.update(project, repo, branch)
18
- rescue Octokit::UnprocessableEntity
19
- # raised because we've tried to create too many statuses for the commit
20
- rescue Txgh::TransifexNotFoundError
21
- # raised if transifex resource can't be found
22
- end
23
-
24
16
  def pusher
25
17
  @pusher ||= Txgh::Pusher.new(project, repo, branch)
26
18
  end
27
-
28
19
  end
29
20
  end
30
21
  end
@@ -1,3 +1,3 @@
1
1
  module TxghServer
2
- VERSION = '3.1.0'
2
+ VERSION = '4.0.0.beta'
3
3
  end
@@ -1,6 +1,8 @@
1
1
  module TxghServer
2
2
  module Webhooks
3
+ autoload :Git, 'txgh-server/webhooks/git'
3
4
  autoload :Github, 'txgh-server/webhooks/github'
5
+ autoload :Gitlab, 'txgh-server/webhooks/gitlab'
4
6
  autoload :Transifex, 'txgh-server/webhooks/transifex'
5
7
  end
6
8
  end
@@ -0,0 +1,15 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
4
+ autoload :BlankAttributes, 'txgh-server/webhooks/git/blank_attributes'
5
+ autoload :DeleteAttributes, 'txgh-server/webhooks/git/delete_attributes'
6
+ autoload :DeleteHandler, 'txgh-server/webhooks/git/delete_handler'
7
+ autoload :Handler, 'txgh-server/webhooks/git/handler'
8
+ autoload :PingHandler, 'txgh-server/webhooks/git/ping_handler'
9
+ autoload :PushHandler, 'txgh-server/webhooks/git/push_handler'
10
+ autoload :PushAttributes, 'txgh-server/webhooks/git/push_attributes'
11
+ autoload :RequestHandler, 'txgh-server/webhooks/git/request_handler'
12
+ autoload :StatusUpdater, 'txgh-server/webhooks/git/status_updater'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
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
@@ -0,0 +1,54 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
4
+ class DeleteAttributes
5
+ ATTRIBUTES = [
6
+ :event, :repo_name, :ref, :ref_type
7
+ ]
8
+
9
+ 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
+ def repo_name(payload)
23
+ raise NotImplementedError
24
+ end
25
+
26
+ def ref(payload)
27
+ payload.fetch('ref')
28
+ end
29
+
30
+ def ref_type(payload)
31
+ raise NotImplementedError
32
+ end
33
+ 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
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,45 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
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
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
4
+ # Handles github's ping event, which is a test event fired whenever a new
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,79 @@
1
+ module TxghServer
2
+ module Webhooks
3
+ module Git
4
+ class PushAttributes
5
+ ATTRIBUTES = [
6
+ :event, :repo_name, :ref, :before, :after,
7
+ :added_files, :modified_files, :author
8
+ ]
9
+
10
+ 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
+ def repo_name(payload)
24
+ raise NotImplementedError
25
+ end
26
+
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
+ def added_files(payload)
40
+ raise NotImplementedError
41
+ end
42
+
43
+ def modified_files(payload)
44
+ extract_files(payload, 'modified')
45
+ end
46
+
47
+ def author(payload)
48
+ raise NotImplementedError
49
+ end
50
+
51
+ def extract_files(payload, state)
52
+ raise NotImplementedError
53
+ end
54
+ end
55
+
56
+ def files
57
+ @files ||= added_files + modified_files
58
+ end
59
+
60
+ attr_reader *ATTRIBUTES
61
+
62
+ def initialize(options = {})
63
+ ATTRIBUTES.each do |attr|
64
+ instance_variable_set(
65
+ "@#{attr}", options.fetch(attr) { options.fetch(attr.to_s) }
66
+ )
67
+ end
68
+ end
69
+
70
+ def to_h
71
+ ATTRIBUTES.each_with_object({}) do |attr, ret|
72
+ ret[attr] = public_send(attr)
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,98 @@
1
+ require 'set'
2
+ require 'txgh'
3
+
4
+ module TxghServer
5
+ module Webhooks
6
+ module Git
7
+ class PushHandler
8
+ include ResponseHelpers
9
+
10
+ attr_reader :project, :repo, :logger, :attributes
11
+
12
+ def initialize(project, repo, logger, attributes)
13
+ @project = project
14
+ @repo = repo
15
+ @logger = logger
16
+ @attributes = attributes
17
+ end
18
+
19
+ def execute
20
+ # Check if the branch in the hook data is the configured branch we want
21
+ logger.info("request git branch: #{branch}")
22
+ logger.info("config git branch: #{repo.git_config_branch}")
23
+
24
+ if should_process?
25
+ logger.info('found branch in git request')
26
+
27
+ pusher.push_resources(added_and_modified_resources) do |tx_resource|
28
+ # block should return categories for the passed-in resource
29
+ { 'author' => attributes.author }
30
+ end
31
+
32
+ status_updater.update_status
33
+ end
34
+
35
+ respond_with(200, true)
36
+ rescue => e
37
+ status_updater.report_error_and_update_status(e)
38
+ respond_with_error(500, "Internal server error: #{e.message}", e)
39
+ end
40
+
41
+ private
42
+
43
+ def pusher
44
+ @pusher ||= Txgh::Pusher.new(project, repo, branch)
45
+ end
46
+
47
+ def status_updater
48
+ raise NotImplementedError
49
+ end
50
+
51
+ # finds the resources that were updated in each commit
52
+ def added_and_modified_resources
53
+ @amr ||= attributes.files.each_with_object(Set.new) do |file, ret|
54
+ logger.info("processing added/modified file: #{file}")
55
+
56
+ if tx_resources.include?(file)
57
+ ret << tx_resources[file]
58
+ end
59
+ end
60
+ end
61
+
62
+ # Build an index of known Tx resources, by source file
63
+ def tx_resources
64
+ @tx_resources ||= tx_config.resources.each_with_object({}) do |resource, ret|
65
+ ret[resource.source_file] = if repo.process_all_branches?
66
+ Txgh::TxBranchResource.new(resource, branch) # maybe find instead?
67
+ else
68
+ resource
69
+ end
70
+ end
71
+ end
72
+
73
+ def tx_config
74
+ @tx_config ||= Txgh::Config::TxManager.tx_config(project, repo, branch)
75
+ end
76
+
77
+ def branch
78
+ @ref ||= attributes.ref.sub(/^refs\//, '')
79
+ end
80
+
81
+ def should_process?
82
+ should_process_branch? && should_process_commit?
83
+ end
84
+
85
+ def should_process_branch?
86
+ repo.should_process_ref?(branch)
87
+ end
88
+
89
+ def should_process_commit?
90
+ # return false if 'after' commit sha is all zeroes (indicates branch
91
+ # has been deleted)
92
+ !((attributes.after || '') =~ /\A0+\z/)
93
+ end
94
+
95
+ end
96
+ end
97
+ end
98
+ end