Respect interactive repo name input on create
This commit is contained in:
parent
e930122a0c
commit
458d5cb42f
2 changed files with 108 additions and 20 deletions
|
|
@ -111,29 +111,17 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
|
||||
func createRun(opts *CreateOptions) error {
|
||||
projectDir, projectDirErr := git.ToplevelDir()
|
||||
|
||||
var repoToCreate ghrepo.Interface
|
||||
|
||||
isNameAnArg := false
|
||||
isDescEmpty := opts.Description == ""
|
||||
isVisibilityPassed := false
|
||||
|
||||
if opts.Name != "" {
|
||||
isNameAnArg = true
|
||||
if strings.Contains(opts.Name, "/") {
|
||||
var err error
|
||||
repoToCreate, err = ghrepo.FromFullName(opts.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("argument error: %w", err)
|
||||
}
|
||||
} else {
|
||||
repoToCreate = ghrepo.New("", opts.Name)
|
||||
}
|
||||
} else {
|
||||
if projectDirErr != nil {
|
||||
return projectDirErr
|
||||
}
|
||||
repoToCreate = ghrepo.New("", path.Base(projectDir))
|
||||
opts.Name = path.Base(projectDir)
|
||||
}
|
||||
|
||||
enabledFlagCount := 0
|
||||
|
|
@ -159,7 +147,7 @@ func createRun(opts *CreateOptions) error {
|
|||
|
||||
// Trigger interactive prompt if name is not passed
|
||||
if !isNameAnArg {
|
||||
newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, repoToCreate.RepoName())
|
||||
newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, opts.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -183,6 +171,18 @@ func createRun(opts *CreateOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
var repoToCreate ghrepo.Interface
|
||||
|
||||
if strings.Contains(opts.Name, "/") {
|
||||
var err error
|
||||
repoToCreate, err = ghrepo.FromFullName(opts.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("argument error: %w", err)
|
||||
}
|
||||
} else {
|
||||
repoToCreate = ghrepo.New("", opts.Name)
|
||||
}
|
||||
|
||||
// Find template repo ID
|
||||
if opts.Template != "" {
|
||||
httpClient, err := opts.HttpClient()
|
||||
|
|
@ -303,6 +303,7 @@ func createRun(opts *CreateOptions) error {
|
|||
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", utils.GreenCheck(), path)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintln(opts.IO.Out, "Discarding...")
|
||||
|
|
@ -312,14 +313,14 @@ func createRun(opts *CreateOptions) error {
|
|||
func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) {
|
||||
qs := []*survey.Question{}
|
||||
|
||||
repoOwnerQuestion := &survey.Question{
|
||||
Name: "repoOwner",
|
||||
repoNameQuestion := &survey.Question{
|
||||
Name: "repoName",
|
||||
Prompt: &survey.Input{
|
||||
Message: "Repository name",
|
||||
Default: repoName,
|
||||
},
|
||||
}
|
||||
qs = append(qs, repoOwnerQuestion)
|
||||
qs = append(qs, repoNameQuestion)
|
||||
|
||||
if isDescEmpty {
|
||||
repoDescriptionQuestion := &survey.Question{
|
||||
|
|
@ -344,7 +345,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
|
|||
}
|
||||
|
||||
answers := struct {
|
||||
RepoOwner string
|
||||
RepoName string
|
||||
RepoDescription string
|
||||
RepoVisibility string
|
||||
}{}
|
||||
|
|
@ -355,8 +356,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
|
|||
return "", "", "", err
|
||||
}
|
||||
|
||||
return answers.RepoOwner, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
|
||||
|
||||
return answers.RepoName, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
|
||||
}
|
||||
|
||||
func confirmSubmission(repoName string, repoOwner string, isConfirmFlagPassed *bool) (bool, error) {
|
||||
|
|
|
|||
|
|
@ -389,3 +389,91 @@ func TestRepoCreate_template(t *testing.T) {
|
|||
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoCreate_withoutNameArg(t *testing.T) {
|
||||
reg := &httpmock.Registry{}
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "users/OWNER"),
|
||||
httpmock.StringResponse(`{ "node_id": "OWNERID" }`))
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`mutation RepositoryCreate\b`),
|
||||
httpmock.StringResponse(`
|
||||
{ "data": { "createRepository": {
|
||||
"repository": {
|
||||
"id": "REPOID",
|
||||
"url": "https://github.com/OWNER/REPO",
|
||||
"name": "REPO",
|
||||
"owner": {
|
||||
"login": "OWNER"
|
||||
}
|
||||
}
|
||||
} } }`))
|
||||
httpClient := &http.Client{Transport: reg}
|
||||
|
||||
var seenCmd *exec.Cmd
|
||||
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
|
||||
seenCmd = cmd
|
||||
return &test.OutputStub{}
|
||||
})
|
||||
defer restoreCmd()
|
||||
|
||||
as, surveyTearDown := prompt.InitAskStubber()
|
||||
defer surveyTearDown()
|
||||
|
||||
as.Stub([]*prompt.QuestionStub{
|
||||
{
|
||||
Name: "repoName",
|
||||
Value: "OWNER/REPO",
|
||||
},
|
||||
{
|
||||
Name: "repoDescription",
|
||||
Value: "DESCRIPTION",
|
||||
},
|
||||
{
|
||||
Name: "repoVisibility",
|
||||
Value: "PRIVATE",
|
||||
},
|
||||
})
|
||||
as.Stub([]*prompt.QuestionStub{
|
||||
{
|
||||
Name: "confirmSubmit",
|
||||
Value: true,
|
||||
},
|
||||
})
|
||||
|
||||
output, err := runCommand(httpClient, "")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo create`: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "", output.String())
|
||||
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
|
||||
|
||||
if seenCmd == nil {
|
||||
t.Fatal("expected a command to run")
|
||||
}
|
||||
assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " "))
|
||||
|
||||
var reqBody struct {
|
||||
Query string
|
||||
Variables struct {
|
||||
Input map[string]interface{}
|
||||
}
|
||||
}
|
||||
|
||||
if len(reg.Requests) != 2 {
|
||||
t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests))
|
||||
}
|
||||
|
||||
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
|
||||
_ = json.Unmarshal(bodyBytes, &reqBody)
|
||||
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {
|
||||
t.Errorf("expected %q, got %q", "REPO", repoName)
|
||||
}
|
||||
if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" {
|
||||
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
|
||||
}
|
||||
if ownerId := reqBody.Variables.Input["ownerId"].(string); ownerId != "OWNERID" {
|
||||
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue