Skip to content

Commit

Permalink
Fixed technicalpickles#230 - Instead of relying on Github API which i…
Browse files Browse the repository at this point in the history
…s not supported

anymore, use Github API to login using basic auth and create a repo
using the API.
  • Loading branch information
emilsoman authored and L2G committed Feb 13, 2013
1 parent 9e1498b commit bb8c79f
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 56 deletions.
2 changes: 2 additions & 0 deletions Gemfile
@@ -1,6 +1,8 @@
source "http://rubygems.org"

gem "git", ">= 1.2.5"
gem "github_api", ">= 0.8.1"
gem "highline", ">= 1.6.15"

group :development do
gem "shoulda"
Expand Down
4 changes: 1 addition & 3 deletions features/step_definitions/generator_steps.rb
Expand Up @@ -56,15 +56,13 @@
@user_email = 'bar@example.com'
@user_name = 'foo'
@github_user = 'technicalpickles'
@github_token = 'zomgtoken'

require 'git'
Git.stubs(:global_config).
returns({
'user.name' => @user_name,
'user.email' => @user_email,
'github.user' => @github_user,
'github.token' => @github_token})
'github.user' => @github_user})
end

Given /^I set JEWELER_OPTS env variable to "(.*)"$/ do |val|
Expand Down
27 changes: 18 additions & 9 deletions lib/jeweler/generator.rb
@@ -1,4 +1,6 @@
require 'git'
require 'github_api'
require 'highline/import'
require 'erb'

require 'net/http'
Expand All @@ -20,10 +22,10 @@ class NoGitHubRepoNameGiven < StandardError
end
class NoGitHubUser < StandardError
end
class NoGitHubToken < StandardError
end
class GitInitFailed < StandardError
end
class GitRepoCreationFailed < StandardError
end

# Generator for creating a jeweler-enabled project
class Generator
Expand All @@ -46,7 +48,7 @@ class Generator
require_relative 'generator/yard_mixin'

attr_accessor :target_dir, :user_name, :user_email, :summary, :homepage,
:description, :project_name, :github_username, :github_token,
:description, :project_name, :github_username,
:repo, :should_create_remote_repo,
:testing_framework, :documentation_framework,
:should_use_cucumber, :should_use_bundler,
Expand Down Expand Up @@ -128,7 +130,7 @@ def run
$stdout.puts "Jeweler has prepared your gem in #{target_dir}"
if should_create_remote_repo
create_and_push_repo
$stdout.puts "Jeweler has pushed your repo to #{homepage}"
$stdout.puts "Jeweler has pushed your repo to #{git_remote}"
end
end

Expand Down Expand Up @@ -283,11 +285,18 @@ def create_version_control
end

def create_and_push_repo
Net::HTTP.post_form URI.parse('http://github.com/api/v2/yaml/repos/create'),
'login' => github_username,
'token' => github_token,
'description' => summary,
'name' => project_name
puts "Please provide your Github password to create the Github repository"
begin
login = github_username
password = ask("Password: ") { |q| q.echo = false }
github = Github.new(login: login.strip, password: password.strip)
github.repos.create(name: project_name, description: summary)
rescue Github::Error::Unauthorized
puts "Wrong login/password! Please try again"
retry
rescue Github::Error::UnprocessableEntity
raise GitRepoCreationFailed, "Can't create that repo. Does it already exist?"
end
# TODO do a HEAD request to see when it's ready?
@repo.push('origin')
end
Expand Down
5 changes: 1 addition & 4 deletions lib/jeweler/generator/application.rb
Expand Up @@ -35,10 +35,7 @@ def run!(*arguments)
$stderr.puts %Q{No user.email found in ~/.gitconfig. Please tell git about yourself (see http://help.github.com/git-email-settings/ for details). For example: git config --global user.email mad.vooo@gmail.com}
return 1
rescue Jeweler::NoGitHubUser
$stderr.puts %Q{No github.user found in ~/.gitconfig. Please tell git about your GitHub account (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt}
return 1
rescue Jeweler::NoGitHubToken
$stderr.puts %Q{No github.token found in ~/.gitconfig. Please tell git about your GitHub account (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.token 6ef8395fecf207165f1a82178ae1b984}
$stderr.puts %Q{Please specify --github-username or set github.user in ~/.gitconfig (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt}
return 1
rescue Jeweler::FileInTheWay
$stderr.puts "The directory #{options[:project_name]} already exists. Maybe move it out of the way before continuing?"
Expand Down
7 changes: 0 additions & 7 deletions lib/jeweler/generator/github_mixin.rb
Expand Up @@ -3,18 +3,11 @@ class Generator
module GithubMixin
def self.extended(generator)
generator.github_username = generator.options[:github_username]
generator.github_token = generator.options[:github_token]
generator.should_create_remote_repo = generator.options[:create_repo]

unless generator.github_username
raise NoGitHubUser
end

if generator.should_create_remote_repo
unless generator.github_token
raise NoGitHubToken
end
end
end

def git_remote
Expand Down
5 changes: 0 additions & 5 deletions lib/jeweler/generator/options.rb
Expand Up @@ -19,7 +19,6 @@ def initialize(args)
self[:user_name] = ENV['GIT_AUTHOR_NAME'] || ENV['GIT_COMMITTER_NAME'] || git_config['user.name']
self[:user_email] = ENV['GIT_AUTHOR_EMAIL'] || ENV['GIT_COMMITTER_EMAIL'] || git_config['user.email']
self[:github_username] = git_config['github.user']
self[:github_token] = git_config['github.token']

require 'optparse'
@opts = OptionParser.new do |o|
Expand Down Expand Up @@ -114,10 +113,6 @@ def initialize(args)
self[:github_username] = github_username
end

o.on('--github-token [GITHUB_TOKEN]', "GitHub token to use for interacting with the GitHub API") do |github_token|
self[:github_token] = github_token
end

o.on('--git-remote [GIT_REMOTE]', 'URI to set the git origin remote to') do |git_remote|
self[:git_remote] = git_remote
end
Expand Down
11 changes: 0 additions & 11 deletions test/jeweler/generator/test_options.rb
Expand Up @@ -46,11 +46,6 @@ def self.for_options(*options)
should "use github username from git config" do
assert_equal @github_user, @options[:github_username]
end

should "use github token from git config" do
assert_equal @github_token, @options[:github_token]
end

should "use user name from git config" do
assert_equal @git_name, @options[:user_name]
end
Expand Down Expand Up @@ -190,12 +185,6 @@ def self.for_options(*options)
end
end

for_options '--github-token', 'mygithubtoken' do
should "set github token" do
assert_equal 'mygithubtoken', @options[:github_token]
end
end

for_options '--bundler' do
should "use bundler" do
assert @options[:use_bundler]
Expand Down
1 change: 0 additions & 1 deletion test/jeweler/test_generator.rb
Expand Up @@ -7,7 +7,6 @@ def build_generator(testing_framework = :shoulda, options = {})
:user_name => 'John Doe',
:user_email => 'john@example.com',
:github_username => 'johndoe',
:github_token => 'yyz',
:documentation_framework => :rdoc
}.merge(options)

Expand Down
14 changes: 0 additions & 14 deletions test/jeweler/test_generator_initialization.rb
Expand Up @@ -52,25 +52,11 @@ def setup
end
end
end

context "without github token set" do
setup do
stub_git_config
end

should 'raise NoGitHubToken if creating repo' do
assert_raise Jeweler::NoGitHubToken do
Jeweler::Generator.new(:project_name => @project_name, :user_name => @git_name, :user_email => @git_email, :github_username => @github_user, :create_repo => true, :testing_framework => :shoulda, :documentation_framework => :rdoc)
end
end
end

def build_generator(options = {})
defaults = { :project_name => @project_name,
:user_name => @git_name,
:user_email => @git_email,
:github_username => @github_user,
:github_token => @github_token,
:testing_framework => :shoulda,
:documentation_framework => :rdoc }

Expand Down
3 changes: 1 addition & 2 deletions test/test_helper.rb
Expand Up @@ -162,10 +162,9 @@ def set_default_git_config
@git_name = 'foo'
@git_email = 'bar@example.com'
@github_user = 'technicalpickles'
@github_token = 'zomgtoken'
end

def valid_git_config
{ 'user.name' => @git_name, 'user.email' => @git_email, 'github.user' => @github_user, 'github.token' => @github_token }
{ 'user.name' => @git_name, 'user.email' => @git_email, 'github.user' => @github_user }
end
end

0 comments on commit bb8c79f

Please sign in to comment.