[repo/create] Create local repo dir with non tty. (#2671)

This addresses issue #2587.
This commit is contained in:
xhqr 2021-01-05 23:40:52 +01:00 committed by GitHub
parent 86eb264277
commit 8b5c5896f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 18 deletions

View file

@ -273,22 +273,26 @@ func createRun(opts *CreateOptions) error {
if isTTY {
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.SuccessIcon(), remoteURL)
}
} else if opts.IO.CanPrompt() {
doSetup := createLocalDirectory
if !doSetup {
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &doSetup)
if err != nil {
return err
} else {
if opts.IO.CanPrompt() {
if !createLocalDirectory {
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &createLocalDirectory)
if err != nil {
return err
}
}
}
if doSetup {
if createLocalDirectory {
path := repo.Name
gitInit, err := git.GitCommand("init", path)
if err != nil {
return err
}
gitInit.Stdout = stdout
isTTY := opts.IO.IsStdoutTTY()
if isTTY {
gitInit.Stdout = stdout
}
gitInit.Stderr = stderr
err = run.PrepareCmd(gitInit).Run()
if err != nil {
@ -304,8 +308,9 @@ func createRun(opts *CreateOptions) error {
if err != nil {
return err
}
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
if isTTY {
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
}
}
}

View file

@ -3,6 +3,7 @@ package create
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os/exec"
@ -20,10 +21,10 @@ import (
"github.com/stretchr/testify/assert"
)
func runCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) {
func runCommand(httpClient *http.Client, cli string, isTTY bool) (*test.CmdOut, error) {
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(true)
io.SetStdinTTY(true)
io.SetStdoutTTY(isTTY)
io.SetStdinTTY(isTTY)
fac := &cmdutil.Factory{
IOStreams: io,
HttpClient: func() (*http.Client, error) {
@ -106,7 +107,7 @@ func TestRepoCreate(t *testing.T) {
},
})
output, err := runCommand(httpClient, "REPO")
output, err := runCommand(httpClient, "REPO", true)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}
@ -143,6 +144,83 @@ func TestRepoCreate(t *testing.T) {
}
}
func TestRepoCreate_outsideGitWorkDir(t *testing.T) {
reg := &httpmock.Registry{}
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 seenCmds []*exec.Cmd
cmdOutputs := []test.OutputStub{
{
Error: errors.New("Not a git repository"),
},
{},
{},
}
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
if len(cmdOutputs) == 0 {
t.Fatal("Too many calls to git command")
}
out := cmdOutputs[0]
cmdOutputs = cmdOutputs[1:]
seenCmds = append(seenCmds, cmd)
return &out
})
defer restoreCmd()
output, err := runCommand(httpClient, "REPO --private --confirm", false)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}
assert.Equal(t, "https://github.com/OWNER/REPO\n", output.String())
assert.Equal(t, "", output.Stderr())
if len(seenCmds) != 3 {
t.Fatal("expected three commands to run")
}
assert.Equal(t, "git rev-parse --show-toplevel", strings.Join(seenCmds[0].Args, " "))
assert.Equal(t, "git init REPO", strings.Join(seenCmds[1].Args, " "))
assert.Equal(t, "git -C REPO remote add origin https://github.com/OWNER/REPO.git", strings.Join(seenCmds[2].Args, " "))
var reqBody struct {
Query string
Variables struct {
Input map[string]interface{}
}
}
if len(reg.Requests) != 1 {
t.Fatalf("expected 1 HTTP request, got %d", len(reg.Requests))
}
bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].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 _, ownerSet := reqBody.Variables.Input["ownerId"]; ownerSet {
t.Error("expected ownerId not to be set")
}
}
func TestRepoCreate_org(t *testing.T) {
reg := &httpmock.Registry{}
reg.Register(
@ -188,7 +266,7 @@ func TestRepoCreate_org(t *testing.T) {
},
})
output, err := runCommand(httpClient, "ORG/REPO")
output, err := runCommand(httpClient, "ORG/REPO", true)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}
@ -270,7 +348,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
},
})
output, err := runCommand(httpClient, "ORG/REPO --team monkeys")
output, err := runCommand(httpClient, "ORG/REPO --team monkeys", true)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}
@ -353,7 +431,7 @@ func TestRepoCreate_template(t *testing.T) {
},
})
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'")
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'", true)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}
@ -441,7 +519,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
},
})
output, err := runCommand(httpClient, "")
output, err := runCommand(httpClient, "", true)
if err != nil {
t.Errorf("error running command `repo create`: %v", err)
}