[Refactor] Add variadic argument to repoCreate to support templates

This commit is contained in:
Colin Shum 2020-08-27 19:13:17 -04:00
parent 2886dd913f
commit 99372f0dbc
4 changed files with 8 additions and 12 deletions

View file

@ -78,7 +78,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
}
if opts.Template != "" && (opts.Homepage != "" || opts.Team != "" || !opts.EnableIssues || !opts.EnableWiki) {
return &cmdutil.FlagError{Err: errors.New(`the '--template' option is not supported with '--homepage, --team, --enable-issues or --enable-wiki'`)}
return &cmdutil.FlagError{Err: errors.New(`The '--template' option is not supported with '--homepage, --team, --enable-issues or --enable-wiki'`)}
}
return createRun(opts)
@ -173,8 +173,7 @@ func createRun(opts *CreateOptions) error {
}
}
// find template ID
// Find template repo ID
if opts.Template != "" {
httpClient, err := opts.HttpClient()
if err != nil {
@ -210,7 +209,6 @@ func createRun(opts *CreateOptions) error {
Visibility: visibility,
OwnerID: repoToCreate.RepoOwner(),
TeamID: opts.Team,
RepositoryID: opts.Template,
Description: opts.Description,
HomepageURL: opts.Homepage,
HasIssuesEnabled: opts.EnableIssues,
@ -231,7 +229,7 @@ func createRun(opts *CreateOptions) error {
}
if opts.ConfirmSubmit {
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input)
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input, opts.Template)
if err != nil {
return err
}

View file

@ -379,7 +379,7 @@ func TestRepoCreate_template(t *testing.T) {
}
if len(reg.Requests) != 3 {
t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests))
t.Fatalf("expected 3 HTTP requests, got %d", len(reg.Requests))
}
bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body)

View file

@ -14,8 +14,6 @@ type repoCreateInput struct {
HomepageURL string `json:"homepageUrl,omitempty"`
Description string `json:"description,omitempty"`
RepositoryID string `json:"repositoryId,omitempty"`
OwnerID string `json:"ownerId,omitempty"`
TeamID string `json:"teamId,omitempty"`
@ -33,7 +31,7 @@ type repoTemplateInput struct {
}
// repoCreate creates a new GitHub repository
func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*api.Repository, error) {
func repoCreate(client *http.Client, hostname string, input repoCreateInput, templateRepositoryID string) (*api.Repository, error) {
apiClient := api.NewClientFromHTTP(client)
if input.TeamID != "" {
@ -51,7 +49,7 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*a
input.OwnerID = orgID
}
if input.RepositoryID != "" {
if templateRepositoryID != "" {
var response struct {
CloneTemplateRepository struct {
Repository api.Repository
@ -70,7 +68,7 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*a
Name: input.Name,
Visibility: input.Visibility,
OwnerID: input.OwnerID,
RepositoryID: input.RepositoryID,
RepositoryID: templateRepositoryID,
}
variables := map[string]interface{}{

View file

@ -21,7 +21,7 @@ func Test_RepoCreate(t *testing.T) {
HomepageURL: "http://example.com",
}
_, err := repoCreate(httpClient, "github.com", input)
_, err := repoCreate(httpClient, "github.com", input, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}