Skip to content

Allow creating repository from a template via the API #11665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ type Repository struct {
Internal bool `json:"internal"`
}

// CreateRepoOption options when creating repository
// swagger:model
type CreateRepoTemplateOption struct {
RepoTemplate int64
GitContent bool
Topics bool
GitHooks bool
Webhooks bool
Avatar bool
Labels bool
}

// CreateRepoOption options when creating repository
// swagger:model
type CreateRepoOption struct {
Expand All @@ -116,6 +128,8 @@ type CreateRepoOption struct {
Readme string `json:"readme"`
// DefaultBranch of the repository (used when initializes and in template)
DefaultBranch string `json:"default_branch" binding:"GitRefName;MaxSize(100)"`
// AAAA
Template *CreateRepoTemplateOption
}

// EditRepoOption options when editing a repository's properties
Expand Down
48 changes: 37 additions & 11 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,43 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
if opt.AutoInit && opt.Readme == "" {
opt.Readme = "Default"
}
repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
IssueLabels: opt.IssueLabels,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
DefaultBranch: opt.DefaultBranch,
})

var err error
var repo *models.Repository

if opt.Template != nil {
templateRepo, err2 := models.GetRepositoryByID(opt.Template.RepoTemplate)
if err2 != nil {
if models.IsErrRepoNotExist(err) {
ctx.Error(http.StatusNotFound, "GetTemplateRepo", fmt.Errorf("Template Repo [%d] not found", opt.Template.RepoTemplate))
}
ctx.InternalServerError(err)
return
}
repo, err = repo_service.GenerateRepository(ctx.User, owner, templateRepo, models.GenerateRepoOptions{
Name: opt.Name,
Description: opt.Description,
Private: opt.Private,
GitContent: opt.Template.GitContent,
Topics: opt.Template.Topics,
GitHooks: opt.Template.GitHooks,
Webhooks: opt.Template.Webhooks,
Avatar: opt.Template.Avatar,
IssueLabels: opt.Template.Labels,
})
} else {
repo, err = repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
IssueLabels: opt.IssueLabels,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
DefaultBranch: opt.DefaultBranch,
})
}
if err != nil {
if models.IsErrRepoAlreadyExist(err) {
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
Expand Down