From d91214c4e066b6e1bcf208a9123cdc9920766d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Thu, 22 Nov 2012 12:55:28 +0100 Subject: [PATCH] `create` command uses API response to assign remote URL This ensures we end up with the correct capitalization of user's login name, and gives the opportunity to the server to change the project name without having us choke on it. Fixes #263 --- features/authentication.feature | 12 +++++++++--- features/create.feature | 31 ++++++++++++++++++++++++------- lib/hub/commands.rb | 5 ++++- lib/hub/github_api.rb | 1 + 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/features/authentication.feature b/features/authentication.feature index 8275cf7b..038d5602 100644 --- a/features/authentication.feature +++ b/features/authentication.feature @@ -13,7 +13,9 @@ Feature: OAuth authentication halt 400 unless params[:scopes] == ['repo'] json :token => 'OTOKEN' } - post('/user/repos') { status 200 } + post('/user/repos') { + json :full_name => 'mislav/dotfiles' + } """ When I run `hub create` interactively When I type "mislav" @@ -36,7 +38,9 @@ Feature: OAuth authentication {:token => 'OTOKEN', :app => {:url => 'http://defunkt.io/hub/'}} ] } - post('/user/repos') { status 200 } + post('/user/repos') { + json :full_name => 'mislav/dotfiles' + } """ When I run `hub create` interactively When I type "mislav" @@ -56,7 +60,9 @@ Feature: OAuth authentication {:token => 'OTOKEN', :app => {:url => 'http://defunkt.io/hub/'}} ] } - post('/user/repos') { status 200 } + post('/user/repos') { + json :full_name => 'mislav/dotfiles' + } """ Given $GITHUB_USER is "mislav" And $GITHUB_PASSWORD is "kitty" diff --git a/features/create.feature b/features/create.feature index 960afdfc..d0546f0a 100644 --- a/features/create.feature +++ b/features/create.feature @@ -8,7 +8,7 @@ Feature: hub create """ post('/user/repos') { halt 400 if params[:private] - status 200 + json :full_name => 'mislav/dotfiles' } """ When I successfully run `hub create` @@ -20,7 +20,7 @@ Feature: hub create """ post('/user/repos') { halt 400 unless params[:private] - status 200 + json :full_name => 'mislav/dotfiles' } """ When I successfully run `hub create -p` @@ -29,7 +29,9 @@ Feature: hub create Scenario: HTTPS is preferred Given the GitHub API server: """ - post('/user/repos') { status 200 } + post('/user/repos') { + json :full_name => 'mislav/dotfiles' + } """ And HTTPS is preferred When I successfully run `hub create` @@ -38,7 +40,9 @@ Feature: hub create Scenario: Create in organization Given the GitHub API server: """ - post('/orgs/acme/repos') { status 200 } + post('/orgs/acme/repos') { + json :full_name => 'acme/dotfiles' + } """ When I successfully run `hub create acme/dotfiles` Then the url for "origin" should be "git@github.com:acme/dotfiles.git" @@ -59,7 +63,7 @@ Feature: hub create """ post('/user/repos') { halt 400 unless params[:name] == 'myconfig' - status 200 + json :full_name => 'mislav/myconfig' } """ When I successfully run `hub create myconfig` @@ -71,7 +75,7 @@ Feature: hub create post('/user/repos') { halt 400 unless params[:description] == 'mydesc' and params[:homepage] == 'http://example.com' - status 200 + json :full_name => 'mislav/dotfiles' } """ When I successfully run `hub create -d mydesc -h http://example.com` @@ -86,7 +90,9 @@ Feature: hub create Scenario: Origin remote already exists Given the GitHub API server: """ - post('/user/repos') { status 200 } + post('/user/repos') { + json :full_name => 'mislav/dotfiles' + } """ And the "origin" remote has url "git://github.com/mislav/dotfiles.git" When I successfully run `hub create` @@ -100,3 +106,14 @@ Feature: hub create When I successfully run `hub create` Then the output should contain "mislav/dotfiles already exists on github.com\n" And the url for "origin" should be "git@github.com:mislav/dotfiles.git" + + Scenario: API response changes the clone URL + Given the GitHub API server: + """ + post('/user/repos') { + json :full_name => 'Mooslav/myconfig' + } + """ + When I successfully run `hub create` + Then the url for "origin" should be "git@github.com:Mooslav/myconfig.git" + And the output should contain exactly "created repository: Mooslav/myconfig\n" diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index ab5d6241..dfc51e03 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -525,7 +525,10 @@ module Hub action = "set remote origin" else action = "created repository" - api_client.create_repo(new_project, options) unless args.noop? + unless args.noop? + repo_data = api_client.create_repo(new_project, options) + new_project = github_project(repo_data['full_name']) + end end url = new_project.git_url(:private => true, :https => https_protocol?) diff --git a/lib/hub/github_api.rb b/lib/hub/github_api.rb index 32eb3adc..8c4e1cc6 100644 --- a/lib/hub/github_api.rb +++ b/lib/hub/github_api.rb @@ -77,6 +77,7 @@ module Hub res = post "https://%s/user/repos" % api_host(project.host), params end res.error! unless res.success? + res.data end # Public: Fetch info about a pull request. -- GitLab