Skip to content
Browse files

Merge pull request #17 from sferik/git-pulls

---

This was a simple refactoring to replace HTTParty with Octokit. Methods like `get_repo_visibility` and `cache_pull_info` have been greatly simplified. I did some manual testing to ensure everything is still working. Let me know if you have any feedback.
  • Loading branch information...
2 parents 80935b8 + a5110a9 commit 5f36966eea5f0e9b6c09c5c3d52deb17e91a2db7 Scott Chacon committed Feb 16, 2011
Showing with 79 additions and 69 deletions.
  1. +18 −0 .gitignore
  2. +3 −0 Gemfile
  3. +16 −19 git-pulls.gemspec
  4. +42 −50 lib/git-pulls.rb
View
18 .gitignore
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
View
3 Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
View
35 git-pulls.gemspec
@@ -1,26 +1,23 @@
$LOAD_PATH.unshift 'lib'
Gem::Specification.new do |s|
- s.name = "git-pulls"
- s.version = "0.2.1"
- s.date = Time.now.strftime('%Y-%m-%d')
- s.summary = "facilitates github pull requests"
- s.homepage = "http://github.com/schacon/git-pulls"
- s.email = "schacon@gmail.com"
- s.authors = [ "Scott Chacon" ]
- s.has_rdoc = false
+ s.name = "git-pulls"
+ s.version = "0.2.1"
+ s.date = Time.now.strftime('%Y-%m-%d')
+ s.summary = "facilitates github pull requests"
+ s.homepage = "http://github.com/schacon/git-pulls"
+ s.email = "schacon@gmail.com"
+ s.authors = ["Scott Chacon"]
+ s.has_rdoc = false
- s.files = %w( LICENSE )
- s.files += Dir.glob("lib/**/*")
- s.files += Dir.glob("bin/**/*")
+ s.files = %w( LICENSE )
+ s.files += Dir.glob("lib/**/*")
+ s.files += Dir.glob("bin/**/*")
- s.executables = %w( git-pulls )
+ s.executables = %w( git-pulls )
+ s.description = "git-pulls facilitates github pull requests."
- s.add_runtime_dependency 'json'
- s.add_runtime_dependency 'httparty'
- s.add_runtime_dependency 'launchy'
-
- s.description = <<desc
- git-pulls facilitates github pull requests.
-desc
+ s.add_runtime_dependency 'json'
+ s.add_runtime_dependency 'launchy'
+ s.add_runtime_dependency 'octokit'
end
View
92 lib/git-pulls.rb
@@ -1,8 +1,6 @@
-require 'rubygems'
require 'json'
-require 'httparty'
require 'launchy'
-require 'pp'
+require 'octokit'
class GitPulls
@@ -19,6 +17,7 @@ def self.start(args)
end
def run
+ configure
if @command && self.respond_to?(@command)
# If the cache file doesn't exist, make sure we run update
# before any other command. git-pulls will otherwise crash
@@ -34,7 +33,7 @@ def run
end
## COMMANDS ##
-
+
def help
puts "No command: #{@command}"
puts "Try: update, list, show, merge, browse"
@@ -113,7 +112,7 @@ def browse
def list
option = @args.shift
- puts "Open Pull Requests for #{@user}/#{@repo}"
+ puts "Open Pull Requests for #{@user}/#{@repo}"
pulls = get_pull_info
pulls.reverse! if option == '--reverse'
count = 0
@@ -136,20 +135,15 @@ def list
end
def update
- puts "Updating #{@user}/#{@repo}"
+ puts "Updating #{@user}/#{@repo}"
cache_pull_info
fetch_stale_forks
list
end
-
- def protocol(is_private)
- is_private ? "ssh://git@" : "git://"
- end
def fetch_stale_forks
puts "Checking for forks in need of fetching"
pulls = get_pull_info
- is_private = get_repo_visibility
repos = {}
pulls.each do |pull|
o = pull['head']['repository']['owner']
@@ -162,16 +156,18 @@ def fetch_stale_forks
end
repos.each do |repo, bool|
puts " fetching #{repo}"
- git("fetch #{protocol(is_private)}#{github_url}/#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
+ git("fetch #{protocol(get_repo_visibility)}#{github_url}/#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
end
end
- def github_url
@mkhl
mkhl added a note Feb 20, 2011

This change removes the github_url method which is still used in fetch_stale_forks, resulting in NameErrors during git pulls update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- host = git("config --get-all github.host")
- if host.size > 0
- host
- else
- 'github.com'
+ def protocol(is_private)
+ is_private ? "ssh://git@" : "git://"
+ end
+
+ def get_repo_visibility
+ # This would fail if the repository was private and user did not provide github token
+ if github_credentials_provided?
+ Octokit.repository("#{repo_info[0]}/#{repo_info[1]}").private
end
end
@@ -185,7 +181,6 @@ def not_merged?(sha)
commits.split("\n").size > 0
end
-
# DISPLAY HELPER FUNCTIONS #
def l(info, size)
@@ -200,55 +195,53 @@ def clean(info)
info.to_s.gsub("\n", ' ')
end
-
# PRIVATE REPOSITORIES ACCESS
-
- def github_username
+
+ def configure
+ Octokit.configure do |config|
+ config.login = github_login
+ config.token = github_token
+ config.endpoint = github_endpoint
+ end
+ end
+
+ def github_login
git("config --get-all github.user")
end
-
+
def github_token
git("config --get-all github.token")
end
+ def github_endpoint
+ host = git("config --get-all github.host")
+ if host.size > 0
+ host
+ else
+ 'https://github.com/'
+ end
+ end
+
# API/DATA HELPER FUNCTIONS #
-
+
def github_credentials_provided?
- if github_token.empty? && github_username.empty?
+ if github_token.empty? && github_login.empty?
return false
end
true
end
-
- def authentication_options
- if github_credentials_provided?
- options = {:basic_auth => {:username => "#{github_username}/token:#{github_token}"}}
- end
- options || {}
- end
-
+
def get_pull_info
get_data(PULLS_CACHE_FILE)['pulls']
end
- def get_repo_visibility
- # This would fail if the repository was private and user did not provide github token
- if github_credentials_provided?
- repo_path = "/repos/show/#{repo_info[0]}/#{repo_info[1]}"
- repo_response = HTTParty.get("https://#{github_url}/api/v2/json" << repo_path, authentication_options)
- repo_response['repository']['private'] if repo_response['repository']
- end
+ def get_data(file)
+ data = JSON.parse(File.read(file))
end
def cache_pull_info
- path = "/pulls/#{@user}/#{@repo}/open"
- puts "https://#{github_url}/api/v2/json"
- response = HTTParty.get("https://#{github_url}/api/v2/json" << path, authentication_options)
- save_data(response, PULLS_CACHE_FILE)
- end
-
- def get_data(file)
- data = JSON.parse(File.read(file))
+ response = Octokit.pull_requests("#{@user}/#{@repo}")
+ save_data({'pulls' => response}, PULLS_CACHE_FILE)
end
def save_data(data, file)
@@ -262,11 +255,10 @@ def pull_num(num)
data.select { |p| p['number'].to_s == num.to_s }.first
end
-
def repo_info
c = {}
config = git('config --list')
- config.split("\n").each do |line|
+ config.split("\n").each do |line|
k, v = line.split('=')
c[k] = v
end

0 comments on commit 5f36966

Please sign in to comment.
Something went wrong with that request. Please try again.