From d3ac6c474becf37587863ea2cb48d17653600d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 31 Aug 2018 13:00:04 +0200 Subject: [PATCH] Fix `hub create` in place of a renamed repo If you're trying to create `owner/foo` repo and there is an existing redirect from `owner/foo` to a differently name repo, don't consider that one as an existing repo and proceed with creating `owner/foo`. --- commands/create.go | 19 +++++++++++++----- features/authentication.feature | 4 +++- features/create.feature | 34 ++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/commands/create.go b/commands/create.go index 08725aae..a5d38625 100644 --- a/commands/create.go +++ b/commands/create.go @@ -110,15 +110,24 @@ func create(command *Command, args *Args) { gh := github.NewClient(project.Host) repo, err := gh.Repository(project) - if err == nil { - if !repo.Private && flagCreatePrivate { - err = fmt.Errorf("Repository '%s' already exists and is public", repo.FullName) - utils.Check(err) + foundProject := github.NewProject(repo.FullName, "", project.Host) + if foundProject.SameAs(project) { + if !repo.Private && flagCreatePrivate { + err = fmt.Errorf("Repository '%s' already exists and is public", repo.FullName) + utils.Check(err) + } else { + ui.Errorln("Existing repository detected. Updating git remote") + project = foundProject + } } else { - ui.Errorln("Existing repository detected. Updating git remote") + repo = nil } } else { + repo = nil + } + + if repo == nil { if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) utils.Check(err) diff --git a/features/authentication.feature b/features/authentication.feature index bfe65796..afb4bec3 100644 --- a/features/authentication.feature +++ b/features/authentication.feature @@ -358,7 +358,9 @@ Feature: OAuth authentication get('/user') { json :login => 'mislav' } - get('/repos/mislav/dotfiles') { status 200 } + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles' + } """ When I run `hub create` interactively When I type "mislav@example.com" diff --git a/features/create.feature b/features/create.feature index 017d9dac..2048092e 100644 --- a/features/create.feature +++ b/features/create.feature @@ -140,7 +140,9 @@ Feature: hub create Scenario: GitHub repo already exists Given the GitHub API server: """ - get('/repos/mislav/dotfiles') { status 200 } + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles' + } """ When I successfully run `hub create` Then the output should contain "Existing repository detected. Updating git remote\n" @@ -170,6 +172,36 @@ Feature: hub create When I successfully run `hub create -p` Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Scenario: Renamed GitHub repo already exists + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + redirect 'https://api.github.com/repositories/12345', 301 + } + get('/repositories/12345') { + json :full_name => 'mislav/DOTfiles' + } + """ + When I successfully run `hub create` + And the url for "origin" should be "git@github.com:mislav/DOTfiles.git" + + Scenario: Renamed GitHub repo is unrelated + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + redirect 'https://api.github.com/repositories/12345', 301 + } + get('/repositories/12345') { + json :full_name => 'mislav/old-dotfiles' + } + post('/user/repos') { + status 201 + json :full_name => 'mislav/mydotfiles' + } + """ + When I successfully run `hub create` + And the url for "origin" should be "git@github.com:mislav/mydotfiles.git" + Scenario: API response changes the clone URL Given the GitHub API server: """ -- GitLab