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

Fetch&rebase support for pull requests #56

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ InstalledFiles
pkg
*.gem
.bundle
Gemfile.lock
Gemfile.lock
.idea
4 changes: 2 additions & 2 deletions bin/gh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
require 'pathname'
lib_dir = File.join(File.dirname(Pathname.new(__FILE__).realpath.to_s), '..', 'lib')
$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir)

require 'github'
Expand Down
4 changes: 2 additions & 2 deletions bin/github
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
require 'pathname'
lib_dir = File.join(File.dirname(Pathname.new(__FILE__).realpath.to_s), '..', 'lib')
$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir)

require 'github'
Expand Down
27 changes: 23 additions & 4 deletions lib/commands/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
end

if repo
if options[:ssh] || current_user?(user)
if options[:ssh] || current_user?(user) || in_org?(user)
git_exec "clone [email protected]:#{user}/#{repo}.git" + (dir ? " #{dir}" : "")
else
git_exec "clone git://github.com/#{user}/#{repo}.git" + (dir ? " #{dir}" : "")
Expand Down Expand Up @@ -193,7 +193,7 @@
flags :rst => 'Create README.rst'
flags :private => 'Create private repository'
command :create do |repo|
command = "curl -F 'name=#{repo}' -F 'public=#{options[:private] ? 0 : 1}' -F 'login=#{github_user}' -F 'token=#{github_token}' https://github.com/api/v2/json/repos/create"
command = "curl -F 'name=#{repo}' -F 'public=#{options[:private] ? 0 : 1}' -H 'Authorization: token #{github_token}' https://github.com/api/v2/json/repos/create"
output_json = sh command
output = JSON.parse(output_json)
if output["error"]
Expand Down Expand Up @@ -231,7 +231,7 @@

current_origin = git "config remote.origin.url"

output_json = sh "curl -F 'login=#{github_user}' -F 'token=#{github_token}' https://github.com/api/v2/json/repos/fork/#{user}/#{repo}"
output_json = sh "curl -H 'Authorization: token #{github_token}' https://github.com/api/v2/json/repos/fork/#{user}/#{repo}"
output = JSON.parse(output_json)
if output["error"]
die output["error"]
Expand Down Expand Up @@ -261,7 +261,7 @@
end
is_repo = !git("status").match(/fatal/)
raise "Not a git repository. Use 'gh create' instead" unless is_repo
command = "curl -F 'name=#{repo}' -F 'public=#{options[:private] ? 0 : 1}' -F 'login=#{github_user}' -F 'token=#{github_token}' https://github.com/api/v2/json/repos/create"
command = "curl -F 'name=#{repo}' -F 'public=#{options[:private] ? 0 : 1}' -H 'Authorization: token #{github_token}' https://github.com/api/v2/json/repos/create"
output_json = sh command
output = JSON.parse(output_json)
if output["error"]
Expand All @@ -283,3 +283,22 @@
puts "No results found"
end
end

desc "Fetch a pull request and possibly rebase or merge it to the current tip"
usage "github fetch-pull [pullRequestId] [rebase | merge]"
command :'fetch-pull' do |n,action|
user, repo = nil,nil
# figure out the user+repo name from git-remote
git("remote -v").split("\n").each do |line|
m = /git@github\.com[:\/]([^\/]+)\/(.+)\.git/.match(line)
if m
user = m[1]
repo = m[2]
end
end
die "Cannot infer repository from git-remote" unless user && repo

pgit "fetch https://github.com/#{user}/#{repo}.git refs/pull/#{n}/head:pull-#{n}"
pgit "checkout pull-#{n}"
pgit "#{action} #{tip}" if ["rebase", "merge"].include?(action)
end
26 changes: 16 additions & 10 deletions lib/github/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def github_user
end

def github_token
token = git("config --get github.token")
token = git("config --get github.oauth")
if token.empty?
request_github_credentials
token = github_token
Expand All @@ -89,17 +89,23 @@ def request_github_credentials
user = highline.ask("Username: ") while user.nil? || user.empty?

git("config --global github.user '#{user}'")
puts("Your account token is at https://github.com/account under 'Account Admin'.")
puts("Press Enter to launch page in browser.")
token = highline.ask("Token: ")
while token.strip.empty?
helper.open "https://github.com/account"
token = highline.ask("Token: ")
end
git("config --global github.token '#{token}'")
puts "We now need to ask you to give your GitHub password."
puts("We use this to generate OAuth token and store that. Password will not be persisted.")

token = highline.ask("Password: ") { |q| q.echo = false }
data = JSON.parse(`curl -s -L -u '#{github_user}:#{token}' --data-binary '{"scopes":["repo","gist"],"note":"GitHub Gem"}' -X POST https://api.github.com/authorizations`)
git("config --global github.oauth '#{data["token"]}'")
true
end


# is the current user in the given org?
def in_org?(name)
command = "curl -H 'Authorization: token #{github_token}' https://api.github.com/user/orgs"
output_json = sh command
orgs = JSON.parse(output_json)
return orgs.find {|o| o['login']==name }!=nil
end

def highline
@highline ||= HighLine.new
end
Expand Down