Skip to content
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

Allow creating repository from a template via the API #11665

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -93,6 +93,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 Down Expand Up @@ -122,6 +134,8 @@ type CreateRepoOption struct {
// TrustModel of the repository
// enum: default,collaborator,committer,collaboratorcommitter
TrustModel string `json:"trust_model"`
// Template contains all options to generate a repo based on a template
Template *CreateRepoTemplateOption `json:"template,omitempty"`
}

// EditRepoOption options when editing a repository's properties
Expand Down
52 changes: 39 additions & 13 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,45 @@ 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,
TrustModel: models.ToTrustModel(opt.TrustModel),
IsTemplate: opt.Template,
})

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,
TrustModel: models.ToTrustModel(opt.TrustModel),
IsTemplate: opt.Template,
})
}
if err != nil {
if models.IsErrRepoAlreadyExist(err) {
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
Expand Down