git_pr 0.0.1 → 0.0.2

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: c87ac91486e6a5273bc76ffdd29e36669010d56f
4
- data.tar.gz: 7503626a0aa947362e5f12c15c40818e3320244f
3
+ metadata.gz: e177f1696787c1c00a2f32b1434b622513690951
4
+ data.tar.gz: e8fd9ce384e2c11d7db089a587b73ecc657ada12
5
5
  SHA512:
6
- metadata.gz: 2f2710728e24a9ce6c7d5aaedfbc39cd2d948a21ed1e58f37adfc9c9b37b3024ef11d2341dc8404ea848f1f02524fe84f8ff091e675283f873cf3d26b431d6ad
7
- data.tar.gz: c5026497f74f7a8979dccf51e54375c1d00b817f7521466ad5d49b63f4f1a535aa42adbcacbad7b27f0b7c75e8e67db95c141a9aa6ede2b1e3b0e22a9562c3c9
6
+ metadata.gz: 12174ea2522aee16ccaf1e7941dea8395996558a878409c670661c57c72d96f466a9de8590ea7e969142b87f5b0668879c6417a158fb70a084da697ca15e6b65
7
+ data.tar.gz: d7ccc87a980d5e36af33f799a52d188231b8035b05a9427963392e05342a4f421673cd8b7c888dda34767b8c5282b42610bfc54d4006d407bc4568e98f8f3003
data/bin/git-pr CHANGED
@@ -11,7 +11,6 @@ require 'colorize'
11
11
  require 'git'
12
12
  require 'highline/import'
13
13
  require 'io/console'
14
- require 'netrc'
15
14
  require 'octokit'
16
15
  require 'optparse'
17
16
  require 'pp'
@@ -127,9 +126,6 @@ if command == "merge" and !options.merge.additional_arguments.empty?
127
126
  options.merge.pr = options.merge.additional_arguments.shift.to_i
128
127
  end
129
128
 
130
- AUTH_KEY_NAME = "git-merge-pull"
131
- NETRC_KEY = "#{AUTH_KEY_NAME}.api.github.com"
132
-
133
129
  def run(cmd, args = { :failure => lambda {} })
134
130
  puts cmd.green
135
131
  puts `#{cmd}`
@@ -144,62 +140,6 @@ def run_test_output_empty(cmd)
144
140
  "" == `#{cmd}`
145
141
  end
146
142
 
147
- def test_github_credentials
148
- n = Netrc.read
149
- user, oauth_token = n[NETRC_KEY]
150
- client = Octokit::Client.new :access_token => oauth_token
151
- begin
152
- client.user
153
- rescue
154
- n.delete NETRC_KEY
155
- n.save
156
- return false
157
- end
158
- return true
159
- end
160
-
161
- def prompt_for_github_credentials(args = {})
162
- user = args[:user]
163
- pass = args[:pass]
164
- needs_otp = args[:needs_otp]
165
- headers = {}
166
-
167
- unless user
168
- print "Enter your github username: "
169
- user = STDIN.gets.chomp!
170
- print "Password: "
171
- pass = STDIN.noecho(&:gets).chomp!
172
- puts "\n"
173
- end
174
-
175
- if needs_otp
176
- print "Enter an OTP code: "
177
- otp = STDIN.gets.chomp!
178
- headers = { "X-GitHub-OTP" => "#{otp}" }
179
- end
180
-
181
- client = Octokit::Client.new :login => user, :password => pass
182
- begin
183
- authorizations = client.authorizations :headers => headers
184
- auth = authorizations.find { |x| x[:app][:name].match "^#{AUTH_KEY_NAME}" }
185
- unless auth
186
- auth = client.create_authorization(:scopes => ["user", "repo"], :note => AUTH_KEY_NAME, :headers => headers)
187
- end
188
- rescue Octokit::Unauthorized
189
- puts "Invalid username or password."
190
- return false
191
- rescue Octokit::OneTimePasswordRequired
192
- # Come back through this method, prompting for an OTP
193
- return prompt_for_github_credentials :user => user, :pass => pass, :needs_otp => true
194
- end
195
-
196
- n = Netrc.read
197
- n[NETRC_KEY] = user, auth[:token]
198
- n.save
199
-
200
- return true
201
- end
202
-
203
143
  def pull_summary(pull)
204
144
  return "##{pull[:number]} from #{pull[:user][:login]}: \"#{pull[:title]}\""
205
145
  end
@@ -217,30 +157,30 @@ def query_for_pull_to_merge(pulls)
217
157
  return pull_to_merge
218
158
  end
219
159
 
220
- if not test_github_credentials and not prompt_for_github_credentials
160
+ if not GitPr::GitHub.test_credentials and not GitPr::GitHub.prompt_for_credentials
221
161
  exit -1
222
162
  end
223
163
 
164
+ GitPr::GitHub.initialize_octokit
165
+
224
166
  # Get local Git object pointed at our repo root
225
167
  git = Git.open `git rev-parse --show-toplevel`.chomp!
226
168
 
227
169
  # Find the target repository that we'll merge the pull request into
228
- # TODO: could read off command line instead
229
170
  target_remote = git.remotes.find { |x| x.name == options.merge.remote }
230
171
  url_match = target_remote.url.match "^git@github.com:(.*)/(.*).git"
231
172
  organization = url_match[1]
232
173
  repository = url_match[2]
233
174
 
234
- n = Netrc.read
235
- user, oauth_token = n[NETRC_KEY]
236
- Octokit.configure do |c|
237
- c.access_token = oauth_token
238
- end
239
-
240
175
  github_repo = Octokit.repo "#{organization}/#{repository}"
241
176
  # pp github_repo
242
177
 
243
178
  pulls = Octokit.pulls "#{organization}/#{repository}/pulls"
179
+ unless pulls.length > 0
180
+ puts "No open pull requests found for #{organization}/#{repository}.".yellow
181
+ exit
182
+ end
183
+
244
184
  if options.merge.pr
245
185
  pull = pulls.find { |p| p[:number] == options.merge.pr }
246
186
  unless pull
@@ -251,6 +191,7 @@ else
251
191
  pull = query_for_pull_to_merge pulls
252
192
  end
253
193
 
194
+
254
195
  pull_number = pull[:number]
255
196
  source_branch = pull[:head][:ref]
256
197
  source_repo_ssh_url = pull[:head][:repo][:git_url]
@@ -0,0 +1,84 @@
1
+ require 'io/console'
2
+ require 'octokit'
3
+ require 'netrc'
4
+
5
+ module GitPr
6
+ module GitHub
7
+
8
+ AUTH_KEY_NAME = "git-merge-pull"
9
+ NETRC_KEY = "#{AUTH_KEY_NAME}.api.github.com"
10
+
11
+ def self.test_credentials
12
+ n = Netrc.read
13
+ user, oauth_token = n[NETRC_KEY]
14
+ client = Octokit::Client.new :access_token => oauth_token
15
+ begin
16
+ client.user
17
+ rescue
18
+ n.delete NETRC_KEY
19
+ n.save
20
+ return false
21
+ end
22
+ return true
23
+ end
24
+
25
+ def self.prompt_for_credentials(args = {})
26
+ user = args[:user]
27
+ pass = args[:pass]
28
+ needs_otp = args[:needs_otp]
29
+ headers = {}
30
+
31
+ unless user
32
+ print "Enter your github username: "
33
+ user = STDIN.gets.chomp!
34
+ print "Password: "
35
+ pass = STDIN.noecho(&:gets).chomp!
36
+ puts "\n"
37
+ end
38
+
39
+ if needs_otp
40
+ print "Enter an OTP code: "
41
+ otp = STDIN.gets.chomp!
42
+ headers = { "X-GitHub-OTP" => "#{otp}" }
43
+ end
44
+
45
+ client = Octokit::Client.new :login => user, :password => pass
46
+ begin
47
+ authorizations = client.authorizations :headers => headers
48
+ auth = authorizations.find { |x| x[:app][:name].match "^#{AUTH_KEY_NAME}" }
49
+ unless auth
50
+ auth = client.create_authorization(:scopes => ["user", "repo"], :note => AUTH_KEY_NAME, :headers => headers)
51
+ end
52
+ rescue Octokit::Unauthorized
53
+ puts "Invalid username or password."
54
+ return false
55
+ rescue Octokit::OneTimePasswordRequired
56
+ # Clients that receive OTP codes via SMS won't get one when we do a get request to client.authorizations
57
+ # We have to make a post to the authorizations endpoint to trigger the sending of the SMS code.
58
+ # https://github.com/github/hub/commit/3d29989
59
+ begin
60
+ result = client.post "authorizations"
61
+ rescue Octokit::OneTimePasswordRequired
62
+ end
63
+
64
+ # Come back through this method, prompting for an OTP
65
+ return prompt_for_credentials :user => user, :pass => pass, :needs_otp => true
66
+ end
67
+
68
+ n = Netrc.read
69
+ n[NETRC_KEY] = user, auth[:token]
70
+ n.save
71
+
72
+ return true
73
+ end
74
+
75
+ def self.initialize_octokit
76
+ n = Netrc.read
77
+ user, oauth_token = n[NETRC_KEY]
78
+ Octokit.configure do |c|
79
+ c.access_token = oauth_token
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,9 @@
1
+ module GitPr
2
+
3
+ class PullRequest
4
+ def initialize(pull)
5
+ @pull = pull
6
+ end
7
+ end
8
+
9
+ end
@@ -1,3 +1,3 @@
1
1
  module GitPr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/git_pr.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "git_pr/github"
2
+ require "git_pr/pull_request"
1
3
  require "git_pr/version"
2
4
 
3
5
  module GitPr
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_pr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Sharon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-30 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -138,6 +138,8 @@ files:
138
138
  - bin/git-pr
139
139
  - git_pr.gemspec
140
140
  - lib/git_pr.rb
141
+ - lib/git_pr/github.rb
142
+ - lib/git_pr/pull_request.rb
141
143
  - lib/git_pr/version.rb
142
144
  homepage: ''
143
145
  licenses: