txgh 1.0.1 → 1.1.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: e9465db2fe6577a296e42811b797fd6c5bb3c8f0
4
- data.tar.gz: 8dd5386796713e113a14ae4289c4bbf2543b988e
3
+ metadata.gz: fe316847be46da3963313952b4861525ddf6b43f
4
+ data.tar.gz: 89c7ac7f13c1d2e165cdaae3f6b110cf499888d3
5
5
  SHA512:
6
- metadata.gz: 4b91e729d9edf9ce4a02243350fdcbc0204a5fdd396b5444a90b4c664f54c1e7af4cf4b50ac7a71bbc308c319c60a9aa7bb0ac13bd76a931f8fdcb7465bab36d
7
- data.tar.gz: e3ee849cb2c84cd12e9d8c5a9f91134b9a24ccf3379517347788be27b7c7624a96b6de586ef65d016eb2c4785d6b87b3621cf72706a2450eeca617f677fe1acd
6
+ metadata.gz: 6793b41001e8e2fb32c808f179d5de0b8a5f4fe0eb4736fc8998657204d6e0c760e5937f0bfb066eb216852efca0e6ab83c3bffdd48c576338719c3f61384094
7
+ data.tar.gz: c1ece2694a02648b5553d7bc226b1180f6d492678b4fbc015a8c507114dbc3805f3b5e98ac3de674287676f4351f35e43a60ce07a9c5b73007faad932d869416
@@ -0,0 +1,18 @@
1
+ module Txgh
2
+ module Handlers
3
+ module Github
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
+ def initialize(options = {})
10
+ end
11
+
12
+ def execute
13
+ respond_with(200, {})
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -14,6 +14,8 @@ module Txgh
14
14
  handle_push(request, logger)
15
15
  when 'delete'
16
16
  handle_delete(request, logger)
17
+ when 'ping'
18
+ handle_ping(request, logger)
17
19
  else
18
20
  handle_unexpected
19
21
  end
@@ -31,6 +33,11 @@ module Txgh
31
33
  new(request, logger).handle(klass)
32
34
  end
33
35
 
36
+ def handle_ping(request, logger)
37
+ klass = Txgh::Handlers::Github::PingHandler
38
+ new(request, logger).handle(klass)
39
+ end
40
+
34
41
  def handle_unexpected
35
42
  respond_with_error(400, 'Unexpected event type')
36
43
  end
@@ -3,6 +3,7 @@ module Txgh
3
3
  module Github
4
4
  autoload :DeleteHandler, 'txgh/handlers/github/delete_handler'
5
5
  autoload :Handler, 'txgh/handlers/github/handler'
6
+ autoload :PingHandler, 'txgh/handlers/github/ping_handler'
6
7
  autoload :PushHandler, 'txgh/handlers/github/push_handler'
7
8
  autoload :RequestHandler, 'txgh/handlers/github/request_handler'
8
9
  end
@@ -15,6 +15,9 @@ module Txgh
15
15
  # For each modified resource, get its content and update the content
16
16
  # in Transifex.
17
17
  def update_resource(tx_resource, commit_sha, categories = {})
18
+ # don't process the resource unless the project slugs are the same
19
+ return unless tx_resource.project_slug == project.name
20
+
18
21
  logger.info('process updated resource')
19
22
  tree_sha = repo.api.get_commit(repo.name, commit_sha)['commit']['tree']['sha']
20
23
  tree = repo.api.tree(repo.name, tree_sha)
@@ -45,8 +48,13 @@ module Txgh
45
48
  end
46
49
 
47
50
  def upload_diff(tx_resource, file, categories)
48
- if content = diff_content(tx_resource, file)
49
- upload_by_branch(tx_resource, content, categories)
51
+ # if uploading to master (i.e. the diff point), then upload the full resource
52
+ if Utils.branches_equal?(tx_resource.branch, repo.diff_point)
53
+ upload_whole(tx_resource, file, categories)
54
+ else
55
+ if content = diff_content(tx_resource, file)
56
+ upload_by_branch(tx_resource, content, categories)
57
+ end
50
58
  end
51
59
  end
52
60
 
@@ -6,10 +6,6 @@ module Txgh
6
6
  Txgh::Handlers::Response.new(status, body, e)
7
7
  end
8
8
 
9
- def respond_with_success(status, body)
10
- respond_with(status, data(body))
11
- end
12
-
13
9
  def respond_with_error(status, message, e = nil)
14
10
  respond_with(status, error(message), e)
15
11
  end
@@ -137,6 +137,13 @@ module Txgh
137
137
  JSON.parse(response.body)
138
138
  end
139
139
 
140
+ def get_stats(project_slug, resource_slug)
141
+ url = "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/stats/"
142
+ response = connection.get(url)
143
+ raise_error!(response)
144
+ JSON.parse(response.body)
145
+ end
146
+
140
147
  private
141
148
 
142
149
  def get_content_io(tx_resource, content)
@@ -62,5 +62,9 @@ module Txgh
62
62
  'name' => translation_file
63
63
  }
64
64
  end
65
+
66
+ def branch
67
+ nil
68
+ end
65
69
  end
66
70
  end
data/lib/txgh/utils.rb CHANGED
@@ -15,6 +15,10 @@ module Txgh
15
15
  end
16
16
  end
17
17
 
18
+ def branches_equal?(first, second)
19
+ absolute_branch(first) == absolute_branch(second)
20
+ end
21
+
18
22
  def is_tag?(ref)
19
23
  ref.include?('tags/')
20
24
  end
data/lib/txgh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Txgh
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ include Txgh
4
+ include Txgh::Handlers::Github
5
+
6
+ describe PingHandler do
7
+ let(:handler) do
8
+ PingHandler.new
9
+ end
10
+
11
+ it 'responds with a 200 success' do
12
+ response = handler.execute
13
+ expect(response.status).to eq(200)
14
+ expect(response.body).to eq({})
15
+ end
16
+ end
@@ -179,7 +179,7 @@ describe ResourceDownloader do
179
179
  it "works even if the resource doesn't exist in transifex" do
180
180
  allow(transifex_api).to(
181
181
  receive(:download) do |resource, language|
182
- if resource.respond_to?(:branch)
182
+ if resource.branch
183
183
  translations_for(resource, language)
184
184
  else
185
185
  raise TransifexNotFoundError
@@ -19,7 +19,7 @@ describe ResourceUpdater do
19
19
  [{ 'path' => resource.source_file, 'sha' => 'def456' }]
20
20
  end
21
21
 
22
- def phrases_for(path)
22
+ let(:translations) do
23
23
  YAML.load("|
24
24
  en:
25
25
  welcome: Hello
@@ -44,8 +44,6 @@ describe ResourceUpdater do
44
44
  )
45
45
 
46
46
  modified_files.each do |file|
47
- translations = phrases_for(file['path'])
48
-
49
47
  allow(github_api).to(
50
48
  receive(:blob).with(repo_name, file['sha']) do
51
49
  { 'content' => translations, 'encoding' => 'utf-8' }
@@ -56,8 +54,6 @@ describe ResourceUpdater do
56
54
 
57
55
  it 'correctly uploads modified files to transifex' do
58
56
  modified_files.each do |file|
59
- translations = phrases_for(file['path'])
60
-
61
57
  expect(transifex_api).to(
62
58
  receive(:create_or_update) do |resource, content|
63
59
  expect(resource.source_file).to eq(file['path'])
@@ -77,8 +73,6 @@ describe ResourceUpdater do
77
73
  allow(transifex_api).to receive(:resource_exists?).and_return(false)
78
74
 
79
75
  modified_files.each do |file|
80
- translations = phrases_for(file['path'])
81
-
82
76
  expect(transifex_api).to(
83
77
  receive(:create) do |resource, content, categories|
84
78
  expect(resource.source_file).to eq(file['path'])
@@ -95,8 +89,6 @@ describe ResourceUpdater do
95
89
  expect(transifex_api).to receive(:resource_exists?).and_return(false)
96
90
 
97
91
  modified_files.each do |file|
98
- translations = phrases_for(file['path'])
99
-
100
92
  expect(transifex_api).to(
101
93
  receive(:create) do |resource, content, categories|
102
94
  expect(categories).to include('foo:bar')
@@ -109,11 +101,16 @@ describe ResourceUpdater do
109
101
  end
110
102
 
111
103
  context 'when asked to upload diffs' do
104
+ let(:branch) { 'all' }
105
+ let(:ref) { 'heads/my_branch' }
112
106
  let(:diff_point) { 'heads/diff_point' }
113
107
  let(:resource) do
114
- TxResource.new(
115
- project_name, resource_slug, 'YAML',
116
- 'en', 'en.yml', '', 'translation_file'
108
+ TxBranchResource.new(
109
+ TxResource.new(
110
+ project_name, resource_slug, 'YAML',
111
+ 'en', 'en.yml', '', 'translation_file'
112
+ ),
113
+ ref
117
114
  )
118
115
  end
119
116
 
@@ -137,11 +134,19 @@ describe ResourceUpdater do
137
134
  receive(:upload_by_branch).with(resource, diff, anything)
138
135
  )
139
136
 
140
- allow(updater).to(
141
- receive(:categories_for).and_return({})
142
- )
143
-
144
137
  updater.update_resource(resource, commit_sha)
145
138
  end
139
+
140
+ context 'when asked to upload the diff point' do
141
+ let(:ref) { diff_point }
142
+
143
+ it 'uploads the whole resource' do
144
+ expect(updater).to(
145
+ receive(:upload_by_branch).with(resource, translations, anything)
146
+ )
147
+
148
+ updater.update_resource(resource, commit_sha)
149
+ end
150
+ end
146
151
  end
147
152
  end
@@ -342,4 +342,26 @@ describe TransifexApi do
342
342
  expect { api.get_formats }.to raise_error(TransifexApiError)
343
343
  end
344
344
  end
345
+
346
+ describe '#get_stats' do
347
+ it 'makes a request with the correct parameters' do
348
+ expect(connection).to receive(:get) do |url, payload|
349
+ expect(url).to end_with("stats/")
350
+ response
351
+ end
352
+
353
+ allow(response).to receive(:status).and_return(200)
354
+ allow(response).to receive(:body).and_return('{}')
355
+ expect(api.get_stats(project_name, resource_slug)).to eq({})
356
+ end
357
+
358
+ it 'raises an exception if the api responds with an error code' do
359
+ allow(connection).to receive(:get).and_return(response)
360
+ allow(response).to receive(:status).and_return(404)
361
+ allow(response).to receive(:body).and_return('{}')
362
+ expect { api.get_stats(project_name, resource_slug) }.to(
363
+ raise_error(TransifexApiError)
364
+ )
365
+ end
366
+ end
345
367
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txgh
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.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-04-09 00:00:00.000000000 Z
12
+ date: 2016-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abroad
@@ -194,6 +194,7 @@ files:
194
194
  - lib/txgh/handlers/github.rb
195
195
  - lib/txgh/handlers/github/delete_handler.rb
196
196
  - lib/txgh/handlers/github/handler.rb
197
+ - lib/txgh/handlers/github/ping_handler.rb
197
198
  - lib/txgh/handlers/github/push_handler.rb
198
199
  - lib/txgh/handlers/github/request_handler.rb
199
200
  - lib/txgh/handlers/response.rb
@@ -236,6 +237,7 @@ files:
236
237
  - spec/github_request_auth_spec.rb
237
238
  - spec/handlers/download_handler_spec.rb
238
239
  - spec/handlers/github/delete_handler_spec.rb
240
+ - spec/handlers/github/ping_handler_spec.rb
239
241
  - spec/handlers/github/push_handler_spec.rb
240
242
  - spec/handlers/tgz_stream_response_spec.rb
241
243
  - spec/handlers/transifex/hook_handler_spec.rb