From 667671f0481085ef841968459f8f94653a0c819d Mon Sep 17 00:00:00 2001 From: nate smith Date: Tue, 25 Jan 2022 15:36:41 -0600 Subject: [PATCH] fix race condition, increase clarity --- pkg/cmd/repo/fork/fork.go | 19 +++++++++++-------- pkg/cmd/repo/fork/fork_test.go | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index a33bf4095..952071fb5 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -182,14 +182,6 @@ func forkRun(opts *ForkOptions) error { return fmt.Errorf("failed to fork: %w", err) } - // Rename the forked repo if ForkName is specified in opts. - if opts.ForkName != "" { - forkedRepo, err = api.RenameRepo(apiClient, forkedRepo, opts.ForkName) - if err != nil { - return err - } - } - // This is weird. There is not an efficient way to determine via the GitHub API whether or not a // given user has forked a given repo. We noticed, also, that the create fork API endpoint just // returns the fork repo data even if it already exists -- with no change in status code or @@ -211,6 +203,17 @@ func forkRun(opts *ForkOptions) error { } } + // Rename the forked repo if ForkName is specified in opts. + if opts.ForkName != "" { + forkedRepo, err = api.RenameRepo(apiClient, forkedRepo, opts.ForkName) + if err != nil { + return fmt.Errorf("could not rename fork: %w", err) + } + if connectedToTerminal { + fmt.Fprintf(stderr, "%s Renamed fork to %s\n", cs.SuccessIconWithColor(cs.Green), cs.Bold(ghrepo.FullName(forkedRepo))) + } + } + if (inParent && (!opts.Remote && !opts.PromptRemote)) || (!inParent && (!opts.Clone && !opts.PromptClone)) { return nil } diff --git a/pkg/cmd/repo/fork/fork_test.go b/pkg/cmd/repo/fork/fork_test.go index 0da745271..ac248669d 100644 --- a/pkg/cmd/repo/fork/fork_test.go +++ b/pkg/cmd/repo/fork/fork_test.go @@ -614,7 +614,7 @@ func TestRepoFork(t *testing.T) { httpmock.REST("PATCH", "repos/OWNER/REPO"), httpmock.StringResponse(renameResult)) }, - wantErrOut: "āœ“ Created fork OWNER/NEW_REPO\n", + wantErrOut: "āœ“ Created fork OWNER/REPO\nāœ“ Renamed fork to OWNER/NEW_REPO\n", }, }