diff --git a/.gitignore b/.gitignore index fea8c2b4..1978a322 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ InstalledFiles pkg *.gem .bundle -Gemfile.lock \ No newline at end of file +Gemfile.lock +.idea diff --git a/bin/gh b/bin/gh index 2f0b44dc..5dfc43fb 100755 --- a/bin/gh +++ b/bin/gh @@ -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' diff --git a/bin/github b/bin/github index 2f0b44dc..5dfc43fb 100755 --- a/bin/github +++ b/bin/github @@ -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' diff --git a/lib/commands/commands.rb b/lib/commands/commands.rb index 8ccb663e..faba4dd0 100644 --- a/lib/commands/commands.rb +++ b/lib/commands/commands.rb @@ -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 git@github.com:#{user}/#{repo}.git" + (dir ? " #{dir}" : "") else git_exec "clone git://github.com/#{user}/#{repo}.git" + (dir ? " #{dir}" : "") @@ -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"] @@ -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"] @@ -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"] @@ -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 diff --git a/lib/github/command.rb b/lib/github/command.rb index 6e9adc53..0344bcf7 100644 --- a/lib/github/command.rb +++ b/lib/github/command.rb @@ -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 @@ -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