thegarage-gitx 2.10.2 → 2.11.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2e6a260e0bd7f13a9f8b2370fc820c12f38305d
4
- data.tar.gz: 4f3cbe2290e5028707095755d9c724eca482e52b
3
+ metadata.gz: 8d556d4767bbd711d406930510b698a1811e063c
4
+ data.tar.gz: 4b71eb5f631cf5b9d134ab842df9136321ca2601
5
5
  SHA512:
6
- metadata.gz: ebb0db12d2acb6f3524e106441ea11cf5c9894871d86e0a2f762f4bceaf64007ab32ec5709ee21117dc5ea18dc7bc8ad5bb4b9051a95305c1e809dc30b70d3de
7
- data.tar.gz: 6e3271a045068dadaaf5c015c4afd7ef447c5ac821305de953750435d0cb88e8d03257877cb9644f121b26f081ab6f9f07de586c8ba343da457ac5fd6c922cf8
6
+ metadata.gz: 3baa98bde329c5ef4d7aa54610aa5393ef4899d7ed28e4558bad31df5d135f918983974947acea1f0a2f3db56d57506f2cf3696aaf7c44fae7095f9e9c90e0b0
7
+ data.tar.gz: 1d16107432222b84b7942a9be5af7be1875c15bd794432221667daaf0a8738155b740ab671cb13d9ea0488b9edcec0e35b5b069dd9fd757b6addbf412fec9348
data/.gitignore CHANGED
@@ -23,3 +23,6 @@ pkg
23
23
 
24
24
  ## bundler
25
25
  Gemfile.lock
26
+
27
+ ## RSpec temp files
28
+ spec/tmp
@@ -1,8 +1,11 @@
1
1
  require 'octokit'
2
+ require 'fileutils'
3
+ require 'yaml'
2
4
 
3
5
  module Thegarage
4
6
  module Gitx
5
7
  module Github
8
+ GLOBAL_CONFIG_FILE = '~/.config/gitx/github.yml'
6
9
  REVIEW_CONTEXT = 'peer_review'
7
10
  CLIENT_URL = 'https://github.com/thegarage/thegarage-gitx'
8
11
  PULL_REQUEST_FOOTER = <<-EOS.dedent
@@ -79,42 +82,41 @@ module Thegarage
79
82
  body.gsub(PULL_REQUEST_FOOTER, '').chomp.strip
80
83
  end
81
84
 
82
- # token is cached in local git config for future use
85
+ # authorization token used for github API calls
86
+ # the token is cached on the filesystem for future use
83
87
  # @return [String] auth token stored in git (current repo, user config or installed global settings)
84
88
  # @see http://developer.github.com/v3/oauth/#scopes
85
89
  # @see http://developer.github.com/v3/#user-agent-required
86
90
  def authorization_token
87
- auth_token = repo.config['thegarage.gitx.githubauthtoken']
88
- return auth_token unless auth_token.to_s.blank?
89
-
90
- auth_token = create_authorization
91
- repo.config['thegarage.gitx.githubauthtoken'] = auth_token
91
+ auth_token = global_config['token']
92
+ auth_token ||= begin
93
+ new_token = create_authorization
94
+ save_global_config('token' => new_token)
95
+ new_token
96
+ end
92
97
  auth_token
93
98
  end
94
99
 
95
100
  def create_authorization
96
- password = ask("Github password for #{username}: ", :echo => false)
97
- say ''
101
+ password = ask_without_echo("Github password for #{username}: ")
98
102
  client = Octokit::Client.new(login: username, password: password)
99
- response = client.create_authorization(authorization_request_options)
103
+ options = {
104
+ :scopes => ['repo'],
105
+ :note => github_client_name,
106
+ :note_url => CLIENT_URL
107
+ }
108
+ two_factor_auth_token = ask_without_echo("Github two factor authorization token (if enabled): ")
109
+ options[:headers] = {'X-GitHub-OTP' => two_factor_auth_token} if two_factor_auth_token
110
+ response = client.create_authorization(options)
100
111
  response.token
101
112
  rescue Octokit::ClientError => e
102
113
  say "Error creating authorization: #{e.message}", :red
103
114
  retry
104
115
  end
105
116
 
106
- def authorization_request_options
107
- timestamp = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S%z')
108
- client_name = "The Garage Git eXtensions - #{github_slug} #{timestamp}"
109
- options = {
110
- :scopes => ['repo'],
111
- :note => client_name,
112
- :note_url => CLIENT_URL
113
- }
114
- two_factor_auth_token = ask("Github two factor authorization token (if enabled): ", :echo => false)
115
- say ''
116
- options[:headers] = {'X-GitHub-OTP' => two_factor_auth_token} if two_factor_auth_token
117
- options
117
+ def github_client_name
118
+ timestamp = Time.now.utc.strftime('%FT%R:%S%z')
119
+ client_name = "The Garage Git eXtensions #{timestamp}"
118
120
  end
119
121
 
120
122
  def github_client
@@ -142,6 +144,34 @@ module Thegarage
142
144
  def github_organization
143
145
  github_slug.split('/').first
144
146
  end
147
+
148
+ def global_config_file
149
+ File.expand_path(GLOBAL_CONFIG_FILE)
150
+ end
151
+
152
+ def global_config
153
+ @config ||= begin
154
+ File.exists?(global_config_file) ? YAML.load_file(global_config_file) : {}
155
+ end
156
+ end
157
+
158
+ def save_global_config(options)
159
+ config_dir = File.dirname(global_config_file)
160
+ ::FileUtils.mkdir_p(config_dir, mode: 0700) unless File.exists?(config_dir)
161
+
162
+ @config = global_config.merge(options)
163
+ File.open(global_config_file, "a+") do |file|
164
+ file.truncate(0)
165
+ file.write(@config.to_yaml)
166
+ end
167
+ File.chmod(0600, global_config_file)
168
+ end
169
+
170
+ def ask_without_echo(message)
171
+ value = ask(message, echo: false)
172
+ say ''
173
+ value
174
+ end
145
175
  end
146
176
  end
147
177
  end
@@ -1,5 +1,5 @@
1
1
  module Thegarage
2
2
  module Gitx
3
- VERSION = '2.10.2'
3
+ VERSION = '2.11.0'
4
4
  end
5
5
  end
@@ -0,0 +1,21 @@
1
+ # helper for reading global config file
2
+ module GlobalConfig
3
+ def global_config_file
4
+ config_dir = File.join(__dir__, '../tmp/.config/gitx')
5
+ config_file = File.join(config_dir, 'github.yml')
6
+ FileUtils.mkdir_p(config_dir) unless File.exists?(config_dir)
7
+ config_file
8
+ end
9
+ def global_config
10
+ YAML.load_file(global_config_file)
11
+ end
12
+ end
13
+
14
+ RSpec.configure do |config|
15
+ config.include GlobalConfig
16
+
17
+ config.before do
18
+ tmp_dir = File.join(__dir__, '../tmp')
19
+ FileUtils.rm_rf(tmp_dir)
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ # Swap home directory to current project spec/tmp
2
+ # to allow for writing files + cleanup
3
+ RSpec.configure do |config|
4
+ config.before do
5
+ @old_home = ENV['HOME']
6
+ ENV['HOME'] = File.join(__dir__, '../tmp')
7
+ end
8
+ config.after do
9
+ ENV['HOME'] = @old_home
10
+ end
11
+ end
@@ -209,7 +209,7 @@ describe Thegarage::Gitx::Cli::ReviewCommand do
209
209
  end.to raise_error(/Github user not configured/)
210
210
  end
211
211
  end
212
- context 'when config.authorization_token is nil' do
212
+ context 'when global config token is nil' do
213
213
  let(:repo_config) do
214
214
  {
215
215
  'remote.origin.url' => 'https://github.com/thegarage/thegarage-gitx',
@@ -227,12 +227,12 @@ describe Thegarage::Gitx::Cli::ReviewCommand do
227
227
 
228
228
  @auth_token = cli.send(:authorization_token)
229
229
  end
230
- it 'stores authorization_token in git config' do
231
- expect(repo_config).to include('thegarage.gitx.githubauthtoken' => authorization_token)
230
+ it 'stores authorization_token in global config' do
231
+ expect(global_config).to include('token' => authorization_token)
232
232
  end
233
233
  it { expect(@auth_token).to eq authorization_token }
234
234
  end
235
- context 'when config.authorization_token is nil and first request fails' do
235
+ context 'when global authorization_token is nil and first request fails' do
236
236
  let(:repo_config) do
237
237
  {
238
238
  'remote.origin.url' => 'https://github.com/thegarage/thegarage-gitx',
@@ -252,21 +252,28 @@ describe Thegarage::Gitx::Cli::ReviewCommand do
252
252
 
253
253
  @auth_token = cli.send(:authorization_token)
254
254
  end
255
- it 'stores authorization_token in git config' do
256
- expect(repo_config).to include('thegarage.gitx.githubauthtoken' => authorization_token)
255
+ it 'stores authorization_token in global config' do
256
+ expect(global_config).to include('token' => authorization_token)
257
257
  end
258
258
  it { expect(@auth_token).to eq authorization_token }
259
259
  end
260
- context 'when there is an existing authorization_token' do
260
+ context 'when the global config has an existing token' do
261
261
  let(:authorization_token) { '123981239123' }
262
262
  let(:repo_config) do
263
263
  {
264
264
  'remote.origin.url' => 'https://github.com/thegarage/thegarage-gitx',
265
- 'github.user' => 'ryan@codecrate.com',
266
- 'thegarage.gitx.githubauthtoken' => authorization_token
265
+ 'github.user' => 'ryan@codecrate.com'
266
+ }
267
+ end
268
+ let(:config) do
269
+ {
270
+ 'token' => authorization_token
267
271
  }
268
272
  end
269
273
  before do
274
+ File.open(global_config_file, 'w') do |file|
275
+ file.write(config.to_yaml)
276
+ end
270
277
  @auth_token = cli.send(:authorization_token)
271
278
  end
272
279
  it { expect(@auth_token).to eq authorization_token }
@@ -291,8 +298,8 @@ describe Thegarage::Gitx::Cli::ReviewCommand do
291
298
 
292
299
  @auth_token = cli.send(:authorization_token)
293
300
  end
294
- it 'stores authorization_token in git config' do
295
- expect(repo_config).to include('thegarage.gitx.githubauthtoken' => authorization_token)
301
+ it 'stores authorization_token in global config' do
302
+ expect(global_config).to include('token' => authorization_token)
296
303
  end
297
304
  it { expect(@auth_token).to eq authorization_token }
298
305
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thegarage-gitx
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.2
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Sonnek
@@ -279,6 +279,8 @@ files:
279
279
  - spec/fixtures/vcr_cassettes/pull_request_does_exist_with_success_status.yml
280
280
  - spec/fixtures/vcr_cassettes/pull_request_does_not_exist.yml
281
281
  - spec/spec_helper.rb
282
+ - spec/support/global_config.rb
283
+ - spec/support/home_env.rb
282
284
  - spec/support/matchers/meet_expectations_matcher.rb
283
285
  - spec/support/pry.rb
284
286
  - spec/support/stub_execution.rb
@@ -325,6 +327,8 @@ test_files:
325
327
  - spec/fixtures/vcr_cassettes/pull_request_does_exist_with_success_status.yml
326
328
  - spec/fixtures/vcr_cassettes/pull_request_does_not_exist.yml
327
329
  - spec/spec_helper.rb
330
+ - spec/support/global_config.rb
331
+ - spec/support/home_env.rb
328
332
  - spec/support/matchers/meet_expectations_matcher.rb
329
333
  - spec/support/pry.rb
330
334
  - spec/support/stub_execution.rb