Skip to content

Commit 3c8e163

Browse files
committed
resolve PR comments. Tests WIP
1 parent 7c8b686 commit 3c8e163

File tree

4 files changed

+135
-138
lines changed

4 files changed

+135
-138
lines changed

api/queries_repo.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"net/http"
910
"sort"
1011
"strings"
@@ -439,16 +440,8 @@ func InitRepoHostname(repo *Repository, hostname string) *Repository {
439440
return repo
440441
}
441442

442-
func InitRepoV3Hostname(repo *RepositoryV3, hostname string) *RepositoryV3 {
443-
repo.hostname = hostname
444-
if repo.Parent != nil {
445-
repo.Parent.hostname = hostname
446-
}
447-
return repo
448-
}
449-
450443
// RepositoryV3 is the repository result from GitHub API v3
451-
type RepositoryV3 struct {
444+
type repositoryV3 struct {
452445
NodeID string
453446
Name string
454447
CreatedAt time.Time `json:"created_at"`
@@ -457,22 +450,10 @@ type RepositoryV3 struct {
457450
}
458451
Private bool
459452
HTMLUrl string `json:"html_url"`
460-
Parent *RepositoryV3
453+
Parent *repositoryV3
461454
hostname string
462455
}
463456

464-
func (r RepositoryV3) RepoOwner() string {
465-
return r.Owner.Login
466-
}
467-
468-
func (r RepositoryV3) RepoHost() string {
469-
return r.hostname
470-
}
471-
472-
func (r RepositoryV3) RepoName() string {
473-
return r.Name
474-
}
475-
476457
// ForkRepo forks the repository on GitHub and returns the new repository
477458
func ForkRepo(client *Client, repo ghrepo.Interface, org string) (*Repository, error) {
478459
path := fmt.Sprintf("repos/%s/forks", ghrepo.FullName(repo))
@@ -488,7 +469,7 @@ func ForkRepo(client *Client, repo ghrepo.Interface, org string) (*Repository, e
488469
return nil, err
489470
}
490471

491-
result := RepositoryV3{}
472+
result := repositoryV3{}
492473
err := client.REST(repo.RepoHost(), "POST", path, body, &result)
493474
if err != nil {
494475
return nil, err
@@ -1138,3 +1119,23 @@ func ProjectNamesToPaths(client *Client, repo ghrepo.Interface, projectNames []s
11381119
}
11391120
return ProjectsToPaths(projects, projectNames)
11401121
}
1122+
1123+
func CreateRepoTransformToV4(apiClient *Client, hostname string, method string, path string, body io.Reader) (*Repository, error) {
1124+
var responsev3 repositoryV3
1125+
err := apiClient.REST(hostname, method, path, body, &responsev3)
1126+
1127+
if err != nil {
1128+
return nil, err
1129+
}
1130+
1131+
return &Repository{
1132+
Name: responsev3.Name,
1133+
CreatedAt: responsev3.CreatedAt,
1134+
Owner: RepositoryOwner{
1135+
Login: responsev3.Owner.Login,
1136+
},
1137+
hostname: responsev3.hostname,
1138+
URL: responsev3.HTMLUrl,
1139+
IsPrivate: responsev3.Private,
1140+
}, nil
1141+
}

pkg/cmd/repo/create/create.go

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
9494
opts.Name = args[0]
9595
}
9696

97+
if len(args) == 0 && (opts.GitIgnoreTemplate != "" || opts.LicenseTemplate != "") {
98+
return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are added only when a specific repository name is passed")}
99+
}
100+
97101
if !opts.IO.CanPrompt() {
98102
if opts.Name == "" {
99103
return &cmdutil.FlagError{Err: errors.New("name argument required when not running interactively")}
@@ -192,6 +196,17 @@ func createRun(opts *CreateOptions) error {
192196
if newVisibility != "" {
193197
visibility = newVisibility
194198
}
199+
200+
} else {
201+
// Go for a prompt only if visibility isn't passed
202+
if !isVisibilityPassed {
203+
newVisibility, err := getVisibility()
204+
if err != nil {
205+
return nil
206+
}
207+
visibility = newVisibility
208+
}
209+
195210
httpClient, err := opts.HttpClient()
196211
if err != nil {
197212
return err
@@ -202,21 +217,20 @@ func createRun(opts *CreateOptions) error {
202217
return err
203218
}
204219

205-
gt, lt, err := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host)
206-
if err != nil {
207-
return err
220+
if gitIgnoreTemplate == "" {
221+
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
222+
if err != nil {
223+
return err
224+
}
225+
gitIgnoreTemplate = gt
208226
}
209227

210-
gitIgnoreTemplate = gt
211-
repoLicenseTemplate = lt
212-
} else {
213-
// Go for a prompt only if visibility isn't passed
214-
if !isVisibilityPassed {
215-
newVisibility, err := getVisibility()
228+
if repoLicenseTemplate == "" {
229+
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
216230
if err != nil {
217-
return nil
231+
return err
218232
}
219-
visibility = newVisibility
233+
repoLicenseTemplate = lt
220234
}
221235
}
222236

@@ -359,109 +373,99 @@ func createRun(opts *CreateOptions) error {
359373
return nil
360374
}
361375

362-
func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string, error) {
376+
func interactiveGitIgnore(client *api.Client, hostname string) (string, error) {
363377

364-
var addBoth bool
365-
var initialQs []*survey.Question
378+
var addGitIgnore bool
379+
var addGitIgnoreSurvey []*survey.Question
366380

367-
addGitIgnoreLicense := &survey.Question{
368-
Name: "addGitIgnoreLicense",
381+
addGitIgnoreQuestion := &survey.Question{
382+
Name: "addGitIgnore",
369383
Prompt: &survey.Confirm{
370-
Message: "Would you like to add a .gitignore or a license?",
384+
Message: "Would you like to add a .gitignore?",
371385
Default: false,
372386
},
373387
}
374388

375-
initialQs = append(initialQs, addGitIgnoreLicense)
376-
err := prompt.SurveyAsk(initialQs, &addBoth)
389+
addGitIgnoreSurvey = append(addGitIgnoreSurvey, addGitIgnoreQuestion)
390+
err := prompt.SurveyAsk(addGitIgnoreSurvey, &addGitIgnore)
377391
if err != nil {
378-
return "", "", err
392+
return "", err
379393
}
380394

381-
if addBoth {
382-
var addQs []*survey.Question
395+
var wantedIgnoreTemplate string
396+
397+
if addGitIgnore {
398+
var gitIg []*survey.Question
383399

384-
gitIgnoreLicenseQuestion := &survey.Question{
385-
Name: "gitIgnoreLicense",
386-
Prompt: &survey.MultiSelect{
387-
Message: "What do you want to add?",
388-
Options: []string{".gitignore", "license"},
400+
gitIgnoretemplates, err := ListGitIgnoreTemplates(client, hostname)
401+
if err != nil {
402+
return "", err
403+
}
404+
gitIgnoreQuestion := &survey.Question{
405+
Name: "chooseGitIgnore",
406+
Prompt: &survey.Select{
407+
Message: "Choose a .gitignore template",
408+
Options: gitIgnoretemplates,
389409
},
390410
}
391-
addQs = append(addQs, gitIgnoreLicenseQuestion)
392-
393-
var answers []string
394-
err = prompt.SurveyAsk(addQs, &answers)
411+
gitIg = append(gitIg, gitIgnoreQuestion)
412+
err = prompt.SurveyAsk(gitIg, &wantedIgnoreTemplate)
395413
if err != nil {
396-
return "", "", err
414+
return "", err
397415
}
416+
}
398417

399-
wantGitIgnore, wantLicense := false, false
400-
401-
if len(answers) > 0 {
402-
if len(answers) > 1 {
403-
wantGitIgnore, wantLicense = answers[0] == ".gitignore", answers[1] == "license"
404-
} else if answers[0] == ".gitignore" {
405-
wantGitIgnore = true
406-
} else if answers[0] == "license" {
407-
wantLicense = true
408-
}
418+
return wantedIgnoreTemplate, nil
419+
}
409420

410-
qs := []*survey.Question{}
421+
func interactiveLicense(client *api.Client, hostname string) (string, error) {
422+
var addLicense bool
423+
var addLicenseSurvey []*survey.Question
424+
var wantedLicense string
411425

412-
if wantGitIgnore {
413-
gitIgnoretemplates, err := ListGitIgnoreTemplates(client, hostname)
414-
if err != nil {
415-
fmt.Println(err)
416-
}
417-
gitIgnoreQuestion := &survey.Question{
418-
Name: "repoGitIgnore",
419-
Prompt: &survey.Select{
420-
Message: "Choose a .gitignore template",
421-
Options: gitIgnoretemplates,
422-
},
423-
}
424-
qs = append(qs, gitIgnoreQuestion)
425-
}
426-
427-
licenseKey := map[string]string{}
426+
addLicenseQuestion := &survey.Question{
427+
Name: "addLicense",
428+
Prompt: &survey.Confirm{
429+
Message: "Would you like to add a license?",
430+
Default: false,
431+
},
432+
}
428433

429-
if wantLicense {
430-
licenseTemplates, err := ListLicenseTemplates(client, hostname)
431-
if err != nil {
432-
fmt.Println(err)
433-
}
434-
var licenseNames []string
435-
for _, l := range licenseTemplates {
436-
licenseNames = append(licenseNames, l.Name)
437-
licenseKey[l.Name] = l.Key
438-
}
439-
licenseQuestion := &survey.Question{
440-
Name: "repoLicense",
441-
Prompt: &survey.Select{
442-
Message: "Choose a license",
443-
Options: licenseNames,
444-
},
445-
}
446-
qs = append(qs, licenseQuestion)
447-
}
434+
addLicenseSurvey = append(addLicenseSurvey, addLicenseQuestion)
435+
err := prompt.SurveyAsk(addLicenseSurvey, &addLicense)
436+
if err != nil {
437+
return "", err
438+
}
448439

449-
templateAnswers := struct {
450-
RepoGitIgnore string
451-
RepoLicense string
452-
}{}
440+
licenseKey := map[string]string{}
453441

454-
err = prompt.SurveyAsk(qs, &templateAnswers)
455-
if err != nil {
456-
return "", "", err
457-
}
458-
return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense], nil
442+
if addLicense {
443+
licenseTemplates, err := ListLicenseTemplates(client, hostname)
444+
if err != nil {
445+
return "", err
446+
}
447+
var licenseNames []string
448+
for _, l := range licenseTemplates {
449+
licenseNames = append(licenseNames, l.Name)
450+
licenseKey[l.Name] = l.Key
451+
}
452+
var licenseQs []*survey.Question
459453

454+
licenseQuestion := &survey.Question{
455+
Name: "chooseLicense",
456+
Prompt: &survey.Select{
457+
Message: "Choose a license",
458+
Options: licenseNames,
459+
},
460460
}
461-
return "", "", nil
461+
licenseQs = append(licenseQs, licenseQuestion)
462+
err = prompt.SurveyAsk(licenseQs, &wantedLicense)
463+
if err != nil {
464+
return "", err
465+
}
466+
return licenseKey[wantedLicense], nil
462467
}
463-
464-
return "", "", nil
468+
return "", nil
465469
}
466470

467471
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {

0 commit comments

Comments
 (0)