Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace HTTParty with Octokit (Faraday) #17

Merged
7 commits merged into from Feb 16, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .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
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
35 changes: 16 additions & 19 deletions git-pulls.gemspec
@@ -1,26 +1,23 @@
$LOAD_PATH.unshift 'lib' $LOAD_PATH.unshift 'lib'


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "git-pulls" s.name = "git-pulls"
s.version = "0.2.1" s.version = "0.2.1"
s.date = Time.now.strftime('%Y-%m-%d') s.date = Time.now.strftime('%Y-%m-%d')
s.summary = "facilitates github pull requests" s.summary = "facilitates github pull requests"
s.homepage = "http://github.com/schacon/git-pulls" s.homepage = "http://github.com/schacon/git-pulls"
s.email = "schacon@gmail.com" s.email = "schacon@gmail.com"
s.authors = [ "Scott Chacon" ] s.authors = ["Scott Chacon"]
s.has_rdoc = false s.has_rdoc = false


s.files = %w( LICENSE ) s.files = %w( LICENSE )
s.files += Dir.glob("lib/**/*") s.files += Dir.glob("lib/**/*")
s.files += Dir.glob("bin/**/*") 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 'json'
s.add_runtime_dependency 'httparty' s.add_runtime_dependency 'launchy'
s.add_runtime_dependency 'launchy' s.add_runtime_dependency 'octokit'

s.description = <<desc
git-pulls facilitates github pull requests.
desc
end end
92 changes: 42 additions & 50 deletions lib/git-pulls.rb
@@ -1,8 +1,6 @@
require 'rubygems'
require 'json' require 'json'
require 'httparty'
require 'launchy' require 'launchy'
require 'pp' require 'octokit'


class GitPulls class GitPulls


Expand All @@ -19,6 +17,7 @@ def self.start(args)
end end


def run def run
configure
if @command && self.respond_to?(@command) if @command && self.respond_to?(@command)
# If the cache file doesn't exist, make sure we run update # If the cache file doesn't exist, make sure we run update
# before any other command. git-pulls will otherwise crash # before any other command. git-pulls will otherwise crash
Expand All @@ -34,7 +33,7 @@ def run
end end


## COMMANDS ## ## COMMANDS ##

def help def help
puts "No command: #{@command}" puts "No command: #{@command}"
puts "Try: update, list, show, merge, browse" puts "Try: update, list, show, merge, browse"
Expand Down Expand Up @@ -113,7 +112,7 @@ def browse


def list def list
option = @args.shift option = @args.shift
puts "Open Pull Requests for #{@user}/#{@repo}" puts "Open Pull Requests for #{@user}/#{@repo}"
pulls = get_pull_info pulls = get_pull_info
pulls.reverse! if option == '--reverse' pulls.reverse! if option == '--reverse'
count = 0 count = 0
Expand All @@ -136,20 +135,15 @@ def list
end end


def update def update
puts "Updating #{@user}/#{@repo}" puts "Updating #{@user}/#{@repo}"
cache_pull_info cache_pull_info
fetch_stale_forks fetch_stale_forks
list list
end end

def protocol(is_private)
is_private ? "ssh://git@" : "git://"
end


def fetch_stale_forks def fetch_stale_forks
puts "Checking for forks in need of fetching" puts "Checking for forks in need of fetching"
pulls = get_pull_info pulls = get_pull_info
is_private = get_repo_visibility
repos = {} repos = {}
pulls.each do |pull| pulls.each do |pull|
o = pull['head']['repository']['owner'] o = pull['head']['repository']['owner']
Expand All @@ -162,16 +156,18 @@ def fetch_stale_forks
end end
repos.each do |repo, bool| repos.each do |repo, bool|
puts " fetching #{repo}" 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
end end


def github_url def protocol(is_private)
host = git("config --get-all github.host") is_private ? "ssh://git@" : "git://"
if host.size > 0 end
host
else def get_repo_visibility
'github.com' # 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
end end


Expand All @@ -185,7 +181,6 @@ def not_merged?(sha)
commits.split("\n").size > 0 commits.split("\n").size > 0
end end



# DISPLAY HELPER FUNCTIONS # # DISPLAY HELPER FUNCTIONS #


def l(info, size) def l(info, size)
Expand All @@ -200,55 +195,53 @@ def clean(info)
info.to_s.gsub("\n", ' ') info.to_s.gsub("\n", ' ')
end end



# PRIVATE REPOSITORIES ACCESS # 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") git("config --get-all github.user")
end end

def github_token def github_token
git("config --get-all github.token") git("config --get-all github.token")
end 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 # # API/DATA HELPER FUNCTIONS #

def github_credentials_provided? def github_credentials_provided?
if github_token.empty? && github_username.empty? if github_token.empty? && github_login.empty?
return false return false
end end
true true
end end


def authentication_options
if github_credentials_provided?
options = {:basic_auth => {:username => "#{github_username}/token:#{github_token}"}}
end
options || {}
end

def get_pull_info def get_pull_info
get_data(PULLS_CACHE_FILE)['pulls'] get_data(PULLS_CACHE_FILE)['pulls']
end end


def get_repo_visibility def get_data(file)
# This would fail if the repository was private and user did not provide github token data = JSON.parse(File.read(file))
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
end end


def cache_pull_info def cache_pull_info
path = "/pulls/#{@user}/#{@repo}/open" response = Octokit.pull_requests("#{@user}/#{@repo}")
puts "https://#{github_url}/api/v2/json" save_data({'pulls' => response}, PULLS_CACHE_FILE)
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))
end end


def save_data(data, file) def save_data(data, file)
Expand All @@ -262,11 +255,10 @@ def pull_num(num)
data.select { |p| p['number'].to_s == num.to_s }.first data.select { |p| p['number'].to_s == num.to_s }.first
end end



def repo_info def repo_info
c = {} c = {}
config = git('config --list') config = git('config --list')
config.split("\n").each do |line| config.split("\n").each do |line|
k, v = line.split('=') k, v = line.split('=')
c[k] = v c[k] = v
end end
Expand Down