git-pulls 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/git-pulls.rb +43 -51
  2. metadata +8 -8
data/lib/git-pulls.rb CHANGED
@@ -1,8 +1,6 @@
1
- require 'rubygems'
2
1
  require 'json'
3
- require 'httparty'
4
2
  require 'launchy'
5
- require 'pp'
3
+ require 'octokit'
6
4
 
7
5
  class GitPulls
8
6
 
@@ -19,6 +17,7 @@ class GitPulls
19
17
  end
20
18
 
21
19
  def run
20
+ configure
22
21
  if @command && self.respond_to?(@command)
23
22
  # If the cache file doesn't exist, make sure we run update
24
23
  # before any other command. git-pulls will otherwise crash
@@ -34,7 +33,7 @@ class GitPulls
34
33
  end
35
34
 
36
35
  ## COMMANDS ##
37
-
36
+
38
37
  def help
39
38
  puts "No command: #{@command}"
40
39
  puts "Try: update, list, show, merge, browse"
@@ -113,7 +112,7 @@ Usage: git pulls update
113
112
 
114
113
  def list
115
114
  option = @args.shift
116
- puts "Open Pull Requests for #{@user}/#{@repo}"
115
+ puts "Open Pull Requests for #{@user}/#{@repo}"
117
116
  pulls = get_pull_info
118
117
  pulls.reverse! if option == '--reverse'
119
118
  count = 0
@@ -136,20 +135,15 @@ Usage: git pulls update
136
135
  end
137
136
 
138
137
  def update
139
- puts "Updating #{@user}/#{@repo}"
138
+ puts "Updating #{@user}/#{@repo}"
140
139
  cache_pull_info
141
140
  fetch_stale_forks
142
141
  list
143
142
  end
144
-
145
- def protocol(is_private)
146
- is_private ? "ssh://git@" : "git://"
147
- end
148
143
 
149
144
  def fetch_stale_forks
150
145
  puts "Checking for forks in need of fetching"
151
146
  pulls = get_pull_info
152
- is_private = get_repo_visibility
153
147
  repos = {}
154
148
  pulls.each do |pull|
155
149
  o = pull['head']['repository']['owner']
@@ -162,16 +156,18 @@ Usage: git pulls update
162
156
  end
163
157
  repos.each do |repo, bool|
164
158
  puts " fetching #{repo}"
165
- git("fetch #{protocol(is_private)}#{github_url}/#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
159
+ git("fetch #{protocol(get_repo_visibility)}#{github_url}/#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
166
160
  end
167
161
  end
168
162
 
169
- def github_url
170
- host = git("config --get-all github.host")
171
- if host.size > 0
172
- host
173
- else
174
- 'github.com'
163
+ def protocol(is_private)
164
+ is_private ? "ssh://git@" : "git://"
165
+ end
166
+
167
+ def get_repo_visibility
168
+ # This would fail if the repository was private and user did not provide github token
169
+ if github_credentials_provided?
170
+ Octokit.repository("#{repo_info[0]}/#{repo_info[1]}").private
175
171
  end
176
172
  end
177
173
 
@@ -185,7 +181,6 @@ Usage: git pulls update
185
181
  commits.split("\n").size > 0
186
182
  end
187
183
 
188
-
189
184
  # DISPLAY HELPER FUNCTIONS #
190
185
 
191
186
  def l(info, size)
@@ -200,55 +195,53 @@ Usage: git pulls update
200
195
  info.to_s.gsub("\n", ' ')
201
196
  end
202
197
 
203
-
204
198
  # PRIVATE REPOSITORIES ACCESS
205
-
206
- def github_username
199
+
200
+ def configure
201
+ Octokit.configure do |config|
202
+ config.login = github_login
203
+ config.token = github_token
204
+ config.endpoint = github_endpoint
205
+ end
206
+ end
207
+
208
+ def github_login
207
209
  git("config --get-all github.user")
208
210
  end
209
-
211
+
210
212
  def github_token
211
213
  git("config --get-all github.token")
212
214
  end
213
215
 
216
+ def github_endpoint
217
+ host = git("config --get-all github.host")
218
+ if host.size > 0
219
+ host
220
+ else
221
+ 'https://github.com/'
222
+ end
223
+ end
224
+
214
225
  # API/DATA HELPER FUNCTIONS #
215
-
226
+
216
227
  def github_credentials_provided?
217
- if github_token.empty? && github_username.empty?
228
+ if github_token.empty? && github_login.empty?
218
229
  return false
219
230
  end
220
231
  true
221
232
  end
222
-
223
- def authentication_options
224
- if github_credentials_provided?
225
- options = {:basic_auth => {:username => "#{github_username}/token:#{github_token}"}}
226
- end
227
- options || {}
228
- end
229
-
233
+
230
234
  def get_pull_info
231
235
  get_data(PULLS_CACHE_FILE)['pulls']
232
236
  end
233
237
 
234
- def get_repo_visibility
235
- # This would fail if the repository was private and user did not provide github token
236
- if github_credentials_provided?
237
- repo_path = "/repos/show/#{repo_info[0]}/#{repo_info[1]}"
238
- repo_response = HTTParty.get("https://#{github_url}/api/v2/json" << repo_path, authentication_options)
239
- repo_response['repository']['private'] if repo_response['repository']
240
- end
238
+ def get_data(file)
239
+ data = JSON.parse(File.read(file))
241
240
  end
242
241
 
243
242
  def cache_pull_info
244
- path = "/pulls/#{@user}/#{@repo}/open"
245
- puts "https://#{github_url}/api/v2/json"
246
- response = HTTParty.get("https://#{github_url}/api/v2/json" << path, authentication_options)
247
- save_data(response, PULLS_CACHE_FILE)
248
- end
249
-
250
- def get_data(file)
251
- data = JSON.parse(File.read(file))
243
+ response = Octokit.pull_requests("#{@user}/#{@repo}")
244
+ save_data({'pulls' => response}, PULLS_CACHE_FILE)
252
245
  end
253
246
 
254
247
  def save_data(data, file)
@@ -262,16 +255,15 @@ Usage: git pulls update
262
255
  data.select { |p| p['number'].to_s == num.to_s }.first
263
256
  end
264
257
 
265
-
266
258
  def repo_info
267
259
  c = {}
268
260
  config = git('config --list')
269
- config.split("\n").each do |line|
261
+ config.split("\n").each do |line|
270
262
  k, v = line.split('=')
271
263
  c[k] = v
272
264
  end
273
265
  u = c['remote.origin.url']
274
- if m = /github\.com.(.*?)\/(.*?)\.git/.match(u)
266
+ if m = /github\.com.(.*?)\/(.*)\.git/.match(u)
275
267
  user = m[1]
276
268
  proj = m[2]
277
269
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-pulls
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Scott Chacon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-03 00:00:00 -05:00
18
+ date: 2011-02-16 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: httparty
36
+ name: launchy
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
@@ -47,7 +47,7 @@ dependencies:
47
47
  type: :runtime
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
- name: launchy
50
+ name: octokit
51
51
  prerelease: false
52
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
53
  none: false
@@ -60,7 +60,7 @@ dependencies:
60
60
  version: "0"
61
61
  type: :runtime
62
62
  version_requirements: *id003
63
- description: " git-pulls facilitates github pull requests.\n"
63
+ description: git-pulls facilitates github pull requests.
64
64
  email: schacon@gmail.com
65
65
  executables:
66
66
  - git-pulls