git-review 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/git-review.rb CHANGED
@@ -7,6 +7,11 @@ require 'time'
7
7
  # tempfile is used to create a temporary file containing PR's title and body.
8
8
  # This file is going to be edited by the system editor.
9
9
  require 'tempfile'
10
+ # This file provides the OAuthHelper module which is used to create a oauth token/
11
+ require_relative 'oauth_helper'
12
+ # Setting class
13
+ require_relative 'settings'
14
+
10
15
 
11
16
  # A custom error to raise, if we know we can't go on.
12
17
  class UnprocessableState < StandardError
@@ -14,6 +19,7 @@ end
14
19
 
15
20
 
16
21
  class GitReview
22
+ include OAuthHelper
17
23
 
18
24
  ## COMMANDS ##
19
25
 
@@ -190,8 +196,8 @@ class GitReview
190
196
  update
191
197
  potential_new_request = @current_requests.find { |req| req['title'] == title }
192
198
  if potential_new_request and potential_new_request['number'] > last_request_id
193
- puts "Successfully created new request ##{potential_new_request['number']}" \
194
- "(#{File.join("https://github.com", target_repo, "pull", potential_new_request['number'].to_s)})."
199
+ puts "Successfully created new request ##{potential_new_request['number']}"
200
+ puts File.join("https://github.com", target_repo, "pull", potential_new_request['number'].to_s)
195
201
  end
196
202
  # Return to the user's original branch.
197
203
  git_call "checkout #{@original_branch}"
@@ -512,26 +518,21 @@ class GitReview
512
518
 
513
519
  # Uses Octokit to access GitHub.
514
520
  def configure_github_access
515
- if git_config['github.login'] and git_config['github.password']
521
+ if Settings.instance.oauth_token
516
522
  @github = Octokit::Client.new(
517
- :login => git_config['github.login'],
518
- :password => git_config['github.password']
523
+ :login => Settings.instance.username,
524
+ :oauth_token => Settings.instance.oauth_token
519
525
  )
520
- true
521
526
  @github.login
522
527
  else
523
- puts 'Please update your git config and provide your GitHub login and password.'
524
- puts
525
- puts ' git config --global github.login your_github_login_1234567890'
526
- puts ' git config --global github.password your_github_password_1234567890'
527
- puts
528
- false
528
+ configure_oauth
529
+ configure_github_access
529
530
  end
530
531
  end
531
532
 
532
533
 
533
534
  def debug_mode
534
- git_config['review.mode'] == 'debug'
535
+ Settings.instance.review_mode == 'debug'
535
536
  end
536
537
 
537
538
 
@@ -0,0 +1,63 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+ # Required to hide password
5
+ require 'io/console'
6
+ # Used to retrieve hostname
7
+ require 'socket'
8
+
9
+ module OAuthHelper
10
+ def configure_oauth(chosen_description = nil)
11
+ puts "Requesting a OAuth token for git-review."
12
+ puts "This procedure will grant access to your public and private repositories."
13
+ puts "You can revoke this authorization by visiting the following page: " +
14
+ "https://github.com/settings/applications"
15
+ print "Plese enter your GitHub's username: "
16
+ username = STDIN.gets.chomp
17
+ print "Plese enter your GitHub's password (it won't be stored anywhere): "
18
+ password = STDIN.noecho(&:gets).chomp
19
+ print "\n"
20
+
21
+ if chosen_description
22
+ description = chosen_description
23
+ else
24
+ description = "git-review - #{Socket.gethostname}"
25
+ puts "Please enter a descriptiont to associate to this token, it will " +
26
+ "make easier to find it inside of github's application page."
27
+ puts "Press enter to accept the proposed description"
28
+ print "Description [#{description}]:"
29
+ user_description = STDIN.gets.chomp
30
+ description = user_description.empty? ? description : user_description
31
+ end
32
+
33
+ uri = URI("https://api.github.com/authorizations")
34
+
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true
37
+
38
+ req =Net::HTTP::Post.new(uri.request_uri)
39
+ req.basic_auth username, password
40
+ req.body = {
41
+ "scopes" => ["repo"],
42
+ "note" => description
43
+ }.to_json
44
+
45
+ response = http.request req
46
+
47
+ if response.code == '401'
48
+ warn "You provided the wrong username/password, please try again."
49
+ configure_oauth(description)
50
+ elsif response.code == '201'
51
+ parser_response = JSON.parse(response.body)
52
+ settings = Settings.instance
53
+ settings.oauth_token = parser_response['token']
54
+ settings.username = username
55
+ settings.save!
56
+ puts "OAuth token successfully created"
57
+ else
58
+ warn "Something went wrong: #{response.body}"
59
+ exit 1
60
+ end
61
+ end
62
+
63
+ end
data/lib/settings.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'fileutils'
2
+ require 'singleton'
3
+ require 'yaml'
4
+
5
+ class Settings
6
+ include Singleton
7
+
8
+ def initialize
9
+ @config_file = File.join(
10
+ Dir.home,
11
+ '.git_review.yml'
12
+ )
13
+
14
+ @config = if File.exists?(@config_file)
15
+ YAML.load_file(@config_file) || {}
16
+ else
17
+ {}
18
+ end
19
+ end
20
+
21
+ def save!
22
+ File.open(@config_file, 'w') do |file|
23
+ file.write(YAML.dump(@config))
24
+ end
25
+ end
26
+
27
+ def review_mode
28
+ @config['review_mode']
29
+ end
30
+
31
+ def oauth_token
32
+ @config['oauth_token']
33
+ end
34
+
35
+ def oauth_token=(token)
36
+ @config['oauth_token'] = token
37
+ end
38
+
39
+ def username
40
+ @config['username']
41
+ end
42
+
43
+ def username=(username)
44
+ @config['username'] = username
45
+ end
46
+
47
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-review
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-08 00:00:00.000000000 Z
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: launchy
@@ -83,7 +83,9 @@ extensions: []
83
83
  extra_rdoc_files: []
84
84
  files:
85
85
  - LICENSE
86
+ - lib/settings.rb
86
87
  - lib/git-review.rb
88
+ - lib/oauth_helper.rb
87
89
  - bin/git-review
88
90
  homepage: http://github.com/b4mboo/git-review
89
91
  licenses: []