From 74a6add4d90beb8133bcbf8ca6b43de35e0aa983 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Mon, 11 Nov 2019 09:15:29 -0600 Subject: Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser * Continue work Signed-off-by: jolheiser * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser * Fix integration tests Signed-off-by: jolheiser * Remove unused User Signed-off-by: jolheiser * Move template tests to existing repos Signed-off-by: jolheiser * Minor re-check updates and cleanup Signed-off-by: jolheiser * make fmt Signed-off-by: jolheiser * Test cleanup Signed-off-by: jolheiser * Fix optionalbool Signed-off-by: jolheiser * make fmt Signed-off-by: jolheiser * Test fixes and icon change Signed-off-by: jolheiser * Add new user and repo for tests Signed-off-by: jolheiser * Fix tests (finally) Signed-off-by: jolheiser * Update meta repo with env variables Signed-off-by: jolheiser * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser * Fix tests and coverage Signed-off-by: jolheiser * Fix swagger and JS lint Signed-off-by: jolheiser * Fix API searching for own private repos Signed-off-by: jolheiser * Change wording Signed-off-by: jolheiser * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser * Another search test fix Signed-off-by: jolheiser * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser * Add topics WIP Signed-off-by: jolheiser * Finish adding topics Signed-off-by: jolheiser * Update locale Signed-off-by: jolheiser --- routers/repo/pull.go | 43 ++++++++++++++++++------------ routers/repo/repo.go | 71 +++++++++++++++++++++++++++++++++++++++---------- routers/repo/setting.go | 1 + 3 files changed, 84 insertions(+), 31 deletions(-) (limited to 'routers/repo') diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 0eea5fcbe6..8269717e57 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -51,8 +51,8 @@ var ( } ) -func getForkRepository(ctx *context.Context) *models.Repository { - forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid")) +func getRepository(ctx *context.Context, repoID int64) *models.Repository { + repo, err := models.GetRepositoryByID(repoID) if err != nil { if models.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByID", nil) @@ -62,25 +62,33 @@ func getForkRepository(ctx *context.Context) *models.Repository { return nil } - perm, err := models.GetUserRepoPermission(forkRepo, ctx.User) + perm, err := models.GetUserRepoPermission(repo, ctx.User) if err != nil { ctx.ServerError("GetUserRepoPermission", err) return nil } - if forkRepo.IsEmpty || !perm.CanRead(models.UnitTypeCode) { - if log.IsTrace() { - if forkRepo.IsEmpty { - log.Trace("Empty fork repository %-v", forkRepo) - } else { - log.Trace("Permission Denied: User %-v cannot read %-v of forkRepo %-v\n"+ - "User in forkRepo has Permissions: %-+v", - ctx.User, - models.UnitTypeCode, - ctx.Repo, - perm) - } - } + if !perm.CanRead(models.UnitTypeCode) { + log.Trace("Permission Denied: User %-v cannot read %-v of repo %-v\n"+ + "User in repo has Permissions: %-+v", + ctx.User, + models.UnitTypeCode, + ctx.Repo, + perm) + ctx.NotFound("getRepository", nil) + return nil + } + return repo +} + +func getForkRepository(ctx *context.Context) *models.Repository { + forkRepo := getRepository(ctx, ctx.ParamsInt64(":repoid")) + if ctx.Written() { + return nil + } + + if forkRepo.IsEmpty { + log.Trace("Empty repository %-v", forkRepo) ctx.NotFound("getForkRepository", nil) return nil } @@ -90,7 +98,7 @@ func getForkRepository(ctx *context.Context) *models.Repository { ctx.Data["IsPrivate"] = forkRepo.IsPrivate canForkToUser := forkRepo.OwnerID != ctx.User.ID && !ctx.User.HasForkedRepo(forkRepo.ID) - if err = forkRepo.GetOwner(); err != nil { + if err := forkRepo.GetOwner(); err != nil { ctx.ServerError("GetOwner", err) return nil } @@ -109,6 +117,7 @@ func getForkRepository(ctx *context.Context) *models.Repository { } var traverseParentRepo = forkRepo + var err error for { if ctx.User.ID == traverseParentRepo.OwnerID { canForkToUser = false diff --git a/routers/repo/repo.go b/routers/repo/repo.go index cf1845a727..cb4e483333 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -129,6 +129,16 @@ func Create(ctx *context.Context) { } ctx.Data["ContextUser"] = ctxUser + ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select") + templateID := ctx.QueryInt64("template_id") + if templateID > 0 { + templateRepo, err := models.GetRepositoryByID(templateID) + if err == nil && templateRepo.CheckUnitUser(ctxUser.ID, ctxUser.IsAdmin, models.UnitTypeCode) { + ctx.Data["repo_template"] = templateID + ctx.Data["repo_template_name"] = templateRepo.Name + } + } + ctx.HTML(200, tplCreate) } @@ -170,20 +180,53 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) { return } - repo, err := repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ - Name: form.RepoName, - Description: form.Description, - Gitignores: form.Gitignores, - IssueLabels: form.IssueLabels, - License: form.License, - Readme: form.Readme, - IsPrivate: form.Private || setting.Repository.ForcePrivate, - AutoInit: form.AutoInit, - }) - if err == nil { - log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) - ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name) - return + var err error + if form.RepoTemplate > 0 { + opts := models.GenerateRepoOptions{ + Name: form.RepoName, + Description: form.Description, + Private: form.Private, + GitContent: form.GitContent, + Topics: form.Topics, + } + + if !opts.IsValid() { + ctx.RenderWithErr(ctx.Tr("repo.template.one_item"), tplCreate, form) + return + } + + templateRepo := getRepository(ctx, form.RepoTemplate) + if ctx.Written() { + return + } + + if !templateRepo.IsTemplate { + ctx.RenderWithErr(ctx.Tr("repo.template.invalid"), tplCreate, form) + return + } + + repo, err := repo_service.GenerateRepository(ctx.User, ctxUser, templateRepo, opts) + if err == nil { + log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) + ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name) + return + } + } else { + repo, err := repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ + Name: form.RepoName, + Description: form.Description, + Gitignores: form.Gitignores, + IssueLabels: form.IssueLabels, + License: form.License, + Readme: form.Readme, + IsPrivate: form.Private || setting.Repository.ForcePrivate, + AutoInit: form.AutoInit, + }) + if err == nil { + log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) + ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name) + return + } } handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 95cba3cbf2..f699c1d685 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -101,6 +101,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { repo.LowerName = strings.ToLower(newRepoName) repo.Description = form.Description repo.Website = form.Website + repo.IsTemplate = form.Template // Visibility of forked repository is forced sync with base repository. if repo.IsFork { -- cgit v1.2.3