2014-02-20 08:15:43 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2020-01-23 05:16:46 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2014-02-20 08:15:43 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-03-01 06:17:30 +05:30
|
|
|
"errors"
|
2014-07-26 11:58:04 +05:30
|
|
|
"fmt"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2014-07-26 11:58:04 +05:30
|
|
|
"strings"
|
2023-05-14 03:29:01 +05:30
|
|
|
"sync"
|
2014-03-22 23:20:50 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-05-14 03:29:01 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-08-25 08:01:57 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-11-19 19:09:57 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
2023-04-27 11:36:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/cache"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2023-04-27 11:36:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-03-29 12:53:45 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-06-24 02:42:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2022-04-08 00:29:56 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2019-10-26 12:24:11 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-12-06 12:49:28 +05:30
|
|
|
archiver_service "code.gitea.io/gitea/services/repository/archiver"
|
2014-02-20 08:15:43 +05:30
|
|
|
)
|
|
|
|
|
2014-06-23 08:41:12 +05:30
|
|
|
const (
|
2020-10-21 05:20:10 +05:30
|
|
|
tplCreate base.TplName = "repo/create"
|
|
|
|
tplAlertDetails base.TplName = "base/alert_details"
|
2014-06-23 08:41:12 +05:30
|
|
|
)
|
|
|
|
|
2019-01-18 05:31:04 +05:30
|
|
|
// MustBeNotEmpty render when a repo is a empty git dir
|
|
|
|
func MustBeNotEmpty(ctx *context.Context) {
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
ctx.NotFound("MustBeNotEmpty", nil)
|
2016-01-07 08:37:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 07:34:25 +05:30
|
|
|
// MustBeEditable check that repo can be edited
|
|
|
|
func MustBeEditable(ctx *context.Context) {
|
|
|
|
if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("", nil)
|
2017-10-30 07:34:25 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustBeAbleToUpload check that repo can be uploaded to
|
|
|
|
func MustBeAbleToUpload(ctx *context.Context) {
|
|
|
|
if !setting.Repository.Upload.Enabled {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("", nil)
|
2017-10-30 07:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:36:45 +05:30
|
|
|
func CommitInfoCache(ctx *context.Context) {
|
|
|
|
var err error
|
|
|
|
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranchCommit", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommitsCount", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
|
|
|
ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
func checkContextUser(ctx *context.Context, uid int64) *user_model.User {
|
2022-03-29 11:59:02 +05:30
|
|
|
orgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx.Doer.ID)
|
2015-09-07 23:28:23 +05:30
|
|
|
if err != nil {
|
2019-11-20 16:57:49 +05:30
|
|
|
ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
|
2015-08-28 16:03:09 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if !ctx.Doer.IsAdmin {
|
2022-03-29 11:59:02 +05:30
|
|
|
orgsAvailable := []*organization.Organization{}
|
2020-05-04 02:38:24 +05:30
|
|
|
for i := 0; i < len(orgs); i++ {
|
|
|
|
if orgs[i].CanCreateRepo() {
|
|
|
|
orgsAvailable = append(orgsAvailable, orgs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["Orgs"] = orgsAvailable
|
|
|
|
} else {
|
|
|
|
ctx.Data["Orgs"] = orgs
|
|
|
|
}
|
|
|
|
|
2015-07-19 14:41:16 +05:30
|
|
|
// Not equal means current user is an organization.
|
2022-03-22 12:33:22 +05:30
|
|
|
if uid == ctx.Doer.ID || uid == 0 {
|
|
|
|
return ctx.Doer
|
2015-07-19 14:41:16 +05:30
|
|
|
}
|
|
|
|
|
2022-12-03 08:18:26 +05:30
|
|
|
org, err := user_model.GetUserByID(ctx, uid)
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2022-03-22 12:33:22 +05:30
|
|
|
return ctx.Doer
|
2015-07-19 14:41:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
ctx.ServerError("GetUserByID", fmt.Errorf("[%d]: %w", uid, err))
|
2015-07-19 14:41:16 +05:30
|
|
|
return nil
|
2015-08-28 16:03:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Check ownership of organization.
|
2017-12-21 13:13:26 +05:30
|
|
|
if !org.IsOrganization() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusForbidden)
|
2015-07-19 14:41:16 +05:30
|
|
|
return nil
|
2014-11-06 10:00:04 +05:30
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if !ctx.Doer.IsAdmin {
|
2022-03-29 11:59:02 +05:30
|
|
|
canCreate, err := organization.OrgFromUser(org).CanCreateOrgRepo(ctx.Doer.ID)
|
2017-12-21 13:13:26 +05:30
|
|
|
if err != nil {
|
2019-11-20 16:57:49 +05:30
|
|
|
ctx.ServerError("CanCreateOrgRepo", err)
|
2017-12-21 13:13:26 +05:30
|
|
|
return nil
|
2019-11-20 16:57:49 +05:30
|
|
|
} else if !canCreate {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusForbidden)
|
2017-12-21 13:13:26 +05:30
|
|
|
return nil
|
|
|
|
}
|
2020-04-30 20:41:56 +05:30
|
|
|
} else {
|
|
|
|
ctx.Data["Orgs"] = orgs
|
2017-12-21 13:13:26 +05:30
|
|
|
}
|
2015-07-19 14:41:16 +05:30
|
|
|
return org
|
2014-11-06 10:00:04 +05:30
|
|
|
}
|
|
|
|
|
2017-12-20 18:29:56 +05:30
|
|
|
func getRepoPrivate(ctx *context.Context) bool {
|
|
|
|
switch strings.ToLower(setting.Repository.DefaultPrivate) {
|
|
|
|
case setting.RepoCreatingLastUserVisibility:
|
2022-03-22 12:33:22 +05:30
|
|
|
return ctx.Doer.LastRepoVisibility
|
2017-12-20 18:29:56 +05:30
|
|
|
case setting.RepoCreatingPrivate:
|
|
|
|
return true
|
|
|
|
case setting.RepoCreatingPublic:
|
|
|
|
return false
|
|
|
|
default:
|
2022-03-22 12:33:22 +05:30
|
|
|
return ctx.Doer.LastRepoVisibility
|
2017-12-20 18:29:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// Create render creating repository page
|
2016-03-11 22:26:52 +05:30
|
|
|
func Create(ctx *context.Context) {
|
2014-07-26 09:54:27 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("new_repo")
|
|
|
|
|
|
|
|
// Give default value for template to render.
|
2022-03-29 12:53:45 +05:30
|
|
|
ctx.Data["Gitignores"] = repo_module.Gitignores
|
2023-04-10 14:14:02 +05:30
|
|
|
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
|
2022-03-29 12:53:45 +05:30
|
|
|
ctx.Data["Licenses"] = repo_module.Licenses
|
|
|
|
ctx.Data["Readmes"] = repo_module.Readmes
|
2015-08-28 14:14:04 +05:30
|
|
|
ctx.Data["readme"] = "Default"
|
2017-12-20 18:29:56 +05:30
|
|
|
ctx.Data["private"] = getRepoPrivate(ctx)
|
2015-10-25 13:56:26 +05:30
|
|
|
ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
|
2020-06-18 02:23:55 +05:30
|
|
|
ctx.Data["default_branch"] = setting.Repository.DefaultBranch
|
2014-06-25 13:25:59 +05:30
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
ctxUser := checkContextUser(ctx, ctx.FormInt64("org"))
|
2015-07-19 14:41:16 +05:30
|
|
|
if ctx.Written() {
|
2014-11-06 10:00:04 +05:30
|
|
|
return
|
2014-07-26 11:58:04 +05:30
|
|
|
}
|
2014-06-29 01:13:25 +05:30
|
|
|
ctx.Data["ContextUser"] = ctxUser
|
|
|
|
|
2019-11-11 20:45:29 +05:30
|
|
|
ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select")
|
2021-07-29 07:12:15 +05:30
|
|
|
templateID := ctx.FormInt64("template_id")
|
2019-11-11 20:45:29 +05:30
|
|
|
if templateID > 0 {
|
2022-12-03 08:18:26 +05:30
|
|
|
templateRepo, err := repo_model.GetRepositoryByID(ctx, templateID)
|
2022-08-25 08:01:57 +05:30
|
|
|
if err == nil && access_model.CheckRepoUnitUser(ctx, templateRepo, ctxUser, unit.TypeCode) {
|
2019-11-11 20:45:29 +05:30
|
|
|
ctx.Data["repo_template"] = templateID
|
|
|
|
ctx.Data["repo_template_name"] = templateRepo.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
ctx.Data["CanCreateRepo"] = ctx.Doer.CanCreateRepo()
|
|
|
|
ctx.Data["MaxCreationLimit"] = ctx.Doer.MaxCreationLimit()
|
2021-01-22 05:14:37 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplCreate)
|
2014-04-11 03:39:57 +05:30
|
|
|
}
|
2014-03-08 02:35:18 +05:30
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form interface{}) {
|
2015-08-28 16:03:09 +05:30
|
|
|
switch {
|
2021-12-12 21:18:20 +05:30
|
|
|
case repo_model.IsErrReachLimitOfRepo(err):
|
2022-01-02 08:08:07 +05:30
|
|
|
maxCreationLimit := owner.MaxCreationLimit()
|
2022-01-02 09:03:57 +05:30
|
|
|
msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit)
|
2021-12-31 14:13:03 +05:30
|
|
|
ctx.RenderWithErr(msg, tpl, form)
|
2021-12-12 21:18:20 +05:30
|
|
|
case repo_model.IsErrRepoAlreadyExist(err):
|
2015-08-28 16:03:09 +05:30
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
|
2021-12-12 21:18:20 +05:30
|
|
|
case repo_model.IsErrRepoFilesAlreadyExist(err):
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
switch {
|
|
|
|
case ctx.IsUserSiteAdmin() || (setting.Repository.AllowAdoptionOfUnadoptedRepositories && setting.Repository.AllowDeleteOfUnadoptedRepositories):
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.adopt_or_delete"), tpl, form)
|
|
|
|
case setting.Repository.AllowAdoptionOfUnadoptedRepositories:
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.adopt"), tpl, form)
|
|
|
|
case setting.Repository.AllowDeleteOfUnadoptedRepositories:
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.delete"), tpl, form)
|
|
|
|
default:
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist"), tpl, form)
|
|
|
|
}
|
2021-11-24 15:19:20 +05:30
|
|
|
case db.IsErrNameReserved(err):
|
2015-08-28 16:03:09 +05:30
|
|
|
ctx.Data["Err_RepoName"] = true
|
2021-11-24 15:19:20 +05:30
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form)
|
|
|
|
case db.IsErrNamePatternNotAllowed(err):
|
2015-08-28 16:03:09 +05:30
|
|
|
ctx.Data["Err_RepoName"] = true
|
2021-11-24 15:19:20 +05:30
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form)
|
2015-08-28 16:03:09 +05:30
|
|
|
default:
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError(name, err)
|
2015-08-28 16:03:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// CreatePost response for creating repository
|
2021-01-26 21:06:53 +05:30
|
|
|
func CreatePost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.CreateRepoForm)
|
2014-07-26 09:54:27 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("new_repo")
|
|
|
|
|
2022-03-29 12:53:45 +05:30
|
|
|
ctx.Data["Gitignores"] = repo_module.Gitignores
|
2023-04-10 14:14:02 +05:30
|
|
|
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
|
2022-03-29 12:53:45 +05:30
|
|
|
ctx.Data["Licenses"] = repo_module.Licenses
|
|
|
|
ctx.Data["Readmes"] = repo_module.Readmes
|
2014-02-20 08:15:43 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
ctx.Data["CanCreateRepo"] = ctx.Doer.CanCreateRepo()
|
|
|
|
ctx.Data["MaxCreationLimit"] = ctx.Doer.MaxCreationLimit()
|
2021-04-07 12:56:41 +05:30
|
|
|
|
2016-11-27 11:33:59 +05:30
|
|
|
ctxUser := checkContextUser(ctx, form.UID)
|
2015-07-19 14:41:16 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
2014-07-26 11:58:04 +05:30
|
|
|
}
|
2014-07-26 09:54:27 +05:30
|
|
|
ctx.Data["ContextUser"] = ctxUser
|
|
|
|
|
2014-03-23 01:30:46 +05:30
|
|
|
if ctx.HasError() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplCreate)
|
2014-03-23 01:30:46 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
var repo *repo_model.Repository
|
2019-11-11 20:45:29 +05:30
|
|
|
var err error
|
|
|
|
if form.RepoTemplate > 0 {
|
2022-06-06 13:31:49 +05:30
|
|
|
opts := repo_module.GenerateRepoOptions{
|
2019-11-11 20:45:29 +05:30
|
|
|
Name: form.RepoName,
|
|
|
|
Description: form.Description,
|
|
|
|
Private: form.Private,
|
|
|
|
GitContent: form.GitContent,
|
|
|
|
Topics: form.Topics,
|
2019-11-24 23:27:52 +05:30
|
|
|
GitHooks: form.GitHooks,
|
|
|
|
Webhooks: form.Webhooks,
|
2019-11-25 10:47:51 +05:30
|
|
|
Avatar: form.Avatar,
|
|
|
|
IssueLabels: form.Labels,
|
2019-11-11 20:45:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-01 03:47:51 +05:30
|
|
|
repo, err = repo_service.GenerateRepository(ctx, ctx.Doer, ctxUser, templateRepo, opts)
|
2019-11-11 20:45:29 +05:30
|
|
|
if err == nil {
|
|
|
|
log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
|
2021-11-16 23:48:25 +05:30
|
|
|
ctx.Redirect(repo.Link())
|
2019-11-11 20:45:29 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-01 03:47:51 +05:30
|
|
|
repo, err = repo_service.CreateRepository(ctx, ctx.Doer, ctxUser, repo_module.CreateRepoOptions{
|
2020-03-27 00:44:51 +05:30
|
|
|
Name: form.RepoName,
|
|
|
|
Description: form.Description,
|
|
|
|
Gitignores: form.Gitignores,
|
|
|
|
IssueLabels: form.IssueLabels,
|
|
|
|
License: form.License,
|
|
|
|
Readme: form.Readme,
|
|
|
|
IsPrivate: form.Private || setting.Repository.ForcePrivate,
|
|
|
|
DefaultBranch: form.DefaultBranch,
|
|
|
|
AutoInit: form.AutoInit,
|
2020-09-25 10:48:37 +05:30
|
|
|
IsTemplate: form.Template,
|
2021-12-10 06:57:50 +05:30
|
|
|
TrustModel: repo_model.ToTrustModel(form.TrustModel),
|
2019-11-11 20:45:29 +05:30
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
|
2021-11-16 23:48:25 +05:30
|
|
|
ctx.Redirect(repo.Link())
|
2019-11-11 20:45:29 +05:30
|
|
|
return
|
|
|
|
}
|
2014-03-10 05:36:29 +05:30
|
|
|
}
|
2014-04-13 06:05:35 +05:30
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
|
2014-04-11 03:39:57 +05:30
|
|
|
}
|
2014-04-09 18:58:00 +05:30
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// Action response for actions to a repository
|
2016-03-11 22:26:52 +05:30
|
|
|
func Action(ctx *context.Context) {
|
2014-08-11 08:41:18 +05:30
|
|
|
var err error
|
|
|
|
switch ctx.Params(":action") {
|
|
|
|
case "watch":
|
2022-05-20 19:38:52 +05:30
|
|
|
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
|
2014-08-11 08:41:18 +05:30
|
|
|
case "unwatch":
|
2022-05-20 19:38:52 +05:30
|
|
|
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
|
2014-08-11 08:41:18 +05:30
|
|
|
case "star":
|
2022-03-22 12:33:22 +05:30
|
|
|
err = repo_model.StarRepo(ctx.Doer.ID, ctx.Repo.Repository.ID, true)
|
2014-08-11 08:41:18 +05:30
|
|
|
case "unstar":
|
2022-03-22 12:33:22 +05:30
|
|
|
err = repo_model.StarRepo(ctx.Doer.ID, ctx.Repo.Repository.ID, false)
|
2021-03-01 06:17:30 +05:30
|
|
|
case "accept_transfer":
|
|
|
|
err = acceptOrRejectRepoTransfer(ctx, true)
|
|
|
|
case "reject_transfer":
|
|
|
|
err = acceptOrRejectRepoTransfer(ctx, false)
|
2016-01-07 08:37:17 +05:30
|
|
|
case "desc": // FIXME: this is not used
|
2015-02-16 16:21:56 +05:30
|
|
|
if !ctx.Repo.IsOwner() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusNotFound)
|
2014-08-11 08:41:18 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:01:13 +05:30
|
|
|
ctx.Repo.Repository.Description = ctx.FormString("desc")
|
|
|
|
ctx.Repo.Repository.Website = ctx.FormString("site")
|
2023-03-01 03:47:51 +05:30
|
|
|
err = repo_service.UpdateRepository(ctx, ctx.Repo.Repository, false)
|
2014-08-11 08:41:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
|
2014-08-11 08:41:18 +05:30
|
|
|
return
|
|
|
|
}
|
2015-07-24 02:20:05 +05:30
|
|
|
|
2021-08-11 06:01:13 +05:30
|
|
|
ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.Repo.RepoLink)
|
2014-08-11 08:41:18 +05:30
|
|
|
}
|
2014-07-26 09:54:27 +05:30
|
|
|
|
2021-03-01 06:17:30 +05:30
|
|
|
func acceptOrRejectRepoTransfer(ctx *context.Context, accept bool) error {
|
2022-12-10 08:16:31 +05:30
|
|
|
repoTransfer, err := models.GetPendingRepositoryTransfer(ctx, ctx.Repo.Repository)
|
2021-03-01 06:17:30 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := repoTransfer.LoadAttributes(ctx); err != nil {
|
2021-03-01 06:17:30 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if !repoTransfer.CanUserAcceptTransfer(ctx.Doer) {
|
2021-03-01 06:17:30 +05:30
|
|
|
return errors.New("user does not have enough permissions")
|
|
|
|
}
|
|
|
|
|
|
|
|
if accept {
|
2021-12-21 07:31:58 +05:30
|
|
|
if ctx.Repo.GitRepo != nil {
|
|
|
|
ctx.Repo.GitRepo.Close()
|
|
|
|
ctx.Repo.GitRepo = nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := repo_service.TransferOwnership(ctx, repoTransfer.Doer, repoTransfer.Recipient, ctx.Repo.Repository, repoTransfer.Teams); err != nil {
|
2021-03-01 06:17:30 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.transfer.success"))
|
|
|
|
} else {
|
|
|
|
if err := models.CancelRepositoryTransfer(ctx.Repo.Repository); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.transfer.rejected"))
|
|
|
|
}
|
|
|
|
|
2023-02-11 12:04:11 +05:30
|
|
|
ctx.Redirect(ctx.Repo.Repository.Link())
|
2021-03-01 06:17:30 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:07:30 +05:30
|
|
|
// RedirectDownload return a file based on the following infos:
|
|
|
|
func RedirectDownload(ctx *context.Context) {
|
|
|
|
var (
|
|
|
|
vTag = ctx.Params("vTag")
|
|
|
|
fileName = ctx.Params("fileName")
|
|
|
|
)
|
|
|
|
tagNames := []string{vTag}
|
|
|
|
curRepo := ctx.Repo.Repository
|
2022-08-25 08:01:57 +05:30
|
|
|
releases, err := repo_model.GetReleasesByRepoIDAndNames(ctx, curRepo.ID, tagNames)
|
2019-01-07 04:07:30 +05:30
|
|
|
if err != nil {
|
2021-11-19 19:09:57 +05:30
|
|
|
if repo_model.IsErrAttachmentNotExist(err) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusNotFound)
|
2019-01-07 04:07:30 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("RedirectDownload", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(releases) == 1 {
|
|
|
|
release := releases[0]
|
2022-05-20 19:38:52 +05:30
|
|
|
att, err := repo_model.GetAttachmentByReleaseIDFileName(ctx, release.ID, fileName)
|
2019-01-07 04:07:30 +05:30
|
|
|
if err != nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusNotFound)
|
2019-01-07 04:07:30 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
if att != nil {
|
2023-04-12 14:35:23 +05:30
|
|
|
ServeAttachment(ctx, att.UUID)
|
2019-01-07 04:07:30 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusNotFound)
|
2019-01-07 04:07:30 +05:30
|
|
|
}
|
|
|
|
|
2021-06-24 02:42:38 +05:30
|
|
|
// Download an archive of a repository
|
|
|
|
func Download(ctx *context.Context) {
|
|
|
|
uri := ctx.Params("*")
|
|
|
|
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
|
|
|
|
if err != nil {
|
2021-11-18 01:17:35 +05:30
|
|
|
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
|
|
|
|
ctx.Error(http.StatusBadRequest, err.Error())
|
2022-08-29 15:15:20 +05:30
|
|
|
} else if errors.Is(err, archiver_service.RepoRefNotFoundError{}) {
|
|
|
|
ctx.Error(http.StatusNotFound, err.Error())
|
2021-11-18 01:17:35 +05:30
|
|
|
} else {
|
|
|
|
ctx.ServerError("archiver_service.NewRequest", err)
|
|
|
|
}
|
2021-06-24 02:42:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:15:20 +05:30
|
|
|
archiver, err := aReq.Await(ctx)
|
2021-06-24 02:42:38 +05:30
|
|
|
if err != nil {
|
2022-08-29 15:15:20 +05:30
|
|
|
ctx.ServerError("archiver.Await", err)
|
2021-06-24 02:42:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:15:20 +05:30
|
|
|
download(ctx, aReq.GetArchiveName(), archiver)
|
2021-06-24 02:42:38 +05:30
|
|
|
}
|
|
|
|
|
2021-12-06 12:49:28 +05:30
|
|
|
func download(ctx *context.Context, archiveName string, archiver *repo_model.RepoArchiver) {
|
2021-06-24 02:42:38 +05:30
|
|
|
downloadName := ctx.Repo.Repository.Name + "-" + archiveName
|
|
|
|
|
2022-08-29 15:15:20 +05:30
|
|
|
rPath := archiver.RelativePath()
|
2021-06-24 02:42:38 +05:30
|
|
|
if setting.RepoArchive.ServeDirect {
|
2022-01-20 23:16:10 +05:30
|
|
|
// If we have a signed url (S3, object storage), redirect to this directly.
|
2021-06-24 02:42:38 +05:30
|
|
|
u, err := storage.RepoArchives.URL(rPath, downloadName)
|
|
|
|
if u != nil && err == nil {
|
|
|
|
ctx.Redirect(u.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// If we have matched and access to release or issue
|
2021-06-24 02:42:38 +05:30
|
|
|
fr, err := storage.RepoArchives.Open(rPath)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Open", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fr.Close()
|
2022-08-25 21:35:21 +05:30
|
|
|
|
2022-11-24 19:55:13 +05:30
|
|
|
ctx.ServeContent(fr, &context.ServeHeaderOptions{
|
|
|
|
Filename: downloadName,
|
|
|
|
LastModified: archiver.CreatedUnix.AsLocalTime(),
|
|
|
|
})
|
2021-06-24 02:42:38 +05:30
|
|
|
}
|
|
|
|
|
[RFC] Make archival asynchronous (#11296)
* Make archival asynchronous
The prime benefit being sought here is for large archives to not
clog up the rendering process and cause unsightly proxy timeouts.
As a secondary benefit, archive-in-progress is moved out of the
way into a /tmp file so that new archival requests for the same
commit will not get fulfilled based on an archive that isn't yet
finished.
This asynchronous system is fairly primitive; request comes in, we'll
spawn off a new goroutine to handle it, then we'll mark it as done.
Status requests will see if the file exists in the final location,
and report the archival as done when it exists.
Fixes #11265
* Archive links: drop initial delay to three-quarters of a second
Some, or perhaps even most, archives will not take all that long to archive.
The archive process starts as soon as the download button is initially
clicked, so in theory they could be done quite quickly. Drop the initial
delay down to three-quarters of a second to make it more responsive in the
common case of the archive being quickly created.
* archiver: restructure a little bit to facilitate testing
This introduces two sync.Cond pointers to the archiver package. If they're
non-nil when we go to process a request, we'll wait until signalled (at all)
to proceed. The tests will then create the sync.Cond so that it can signal
at-will and sanity-check the state of the queue at different phases.
The author believes that nil-checking these two sync.Cond pointers on every
archive processing will introduce minimal overhead with no impact on
maintainability.
* gofmt nit: no space around binary + operator
* services: archiver: appease golangci-lint, lock queueMutex
Locking/unlocking the queueMutex is allowed, but not required, for
Cond.Signal() and Cond.Broadcast(). The magic at play here is just a little
too much for golangci-lint, as we take the address of queueMutex and this is
mostly used in archiver.go; the variable still gets flagged as unused.
* archiver: tests: fix several timing nits
Once we've signaled a cond var, it may take some small amount of time for
the goroutines released to hit the spot we're wanting them to be at. Give
them an appropriate amount of time.
* archiver: tests: no underscore in var name, ungh
* archiver: tests: Test* is run in a separate context than TestMain
We must setup the mutex/cond variables at the beginning of any test that's
going to use it, or else these will be nil when the test is actually ran.
* archiver: tests: hopefully final tweak
Things got shuffled around such that we carefully build up and release
requests from the queue, so we can validate the state of the queue at each
step. Fix some assertions that no longer hold true as fallout.
* repo: Download: restore some semblance of previous behavior
When archival was made async, the GET endpoint was only useful if a previous
POST had initiated the download. This commit restores the previous behavior,
to an extent; we'll now submit the archive request there and return a
"202 Accepted" to indicate that it's processing if we didn't manage to
complete the request within ~2 seconds of submission.
This lets a client directly GET the archive, and gives them some indication
that they may attempt to GET it again at a later time.
* archiver: tests: simplify a bit further
We don't need to risk failure and use time.ParseDuration to get 2 *
time.Second.
else if isn't really necessary if the conditions are simple enough and lead
to the same result.
* archiver: tests: resolve potential source of flakiness
Increase all timeouts to 10 seconds; these aren't hard-coded sleeps, so
there's no guarantee we'll actually take that long. If we need longer to
not have a false-positive, then so be it.
While here, various assert.{Not,}Equal arguments are flipped around so that
the wording in error output reflects reality, where the expected argument is
second and actual third.
* archiver: setup infrastructure for notifying consumers of completion
This API will *not* allow consumers to subscribe to specific requests being
completed, just *any* request being completed. The caller is responsible for
determining if their request is satisfied and waiting again if needed.
* repo: archive: make GET endpoint synchronous again
If the request isn't complete, this endpoint will now submit the request and
wait for completion using the new API. This may still be susceptible to
timeouts for larger repos, but other endpoints now exist that the web
interface will use to negotiate its way through larger archive processes.
* archiver: tests: amend test to include WaitForCompletion()
This is a trivial one, so go ahead and include it.
* archiver: tests: fix test by calling NewContext()
The mutex is otherwise uninitialized, so we need to ensure that we're
actually initializing it if we plan to test it.
* archiver: tests: integrate new WaitForCompletion a little better
We can use this to wait for archives to come in, rather than spinning and
hoping with a timeout.
* archiver: tests: combine numQueued declaration with next-instruction assignment
* routers: repo: reap unused archiving flag from DownloadStatus()
This had some planned usage before, indicating whether this request
initiated the archival process or not. After several rounds of refactoring,
this use was deemed not necessary for much of anything and got boiled down
to !complete in all cases.
* services: archiver: restructure to use a channel
We now offer two forms of waiting for a request:
- WaitForCompletion: wait for completion with no timeout
- TimedWaitForCompletion: wait for completion with timeout
In both cases, we wait for the given request's cchan to close; in the latter
case, we do so with the caller-provided timeout. This completely removes the
need for busy-wait loops in Download/InitiateDownload, as it's fairly clean
to wait on a channel with timeout.
* services: archiver: use defer to unlock now that we can
This previously carried the lock into the goroutine, but an intermediate
step just added the request to archiveInProgress outside of the new
goroutine and removed the need for the goroutine to start out with it.
* Revert "archiver: tests: combine numQueued declaration with next-instruction assignment"
This reverts commit bcc52140238e16680f2e05e448e9be51372afdf5.
Revert "archiver: tests: integrate new WaitForCompletion a little better"
This reverts commit 9fc8bedb5667d24d3a3c7843dc28a229efffb1e6.
Revert "archiver: tests: fix test by calling NewContext()"
This reverts commit 709c35685eaaf261ebbb7d3420e3376a4ee8e7f2.
Revert "archiver: tests: amend test to include WaitForCompletion()"
This reverts commit 75261f56bc05d1fa8ff7e81dcbc0ccd93fdc9d50.
* archiver: tests: first attempt at WaitForCompletion() tests
* archiver: tests: slight improvement, less busy-loop
Just wait for the requests to complete in order, instead of busy-waiting
with a timeout. This is slightly less fragile.
While here, reverse the arguments of a nearby assert.Equal() so that
expected/actual are correct in any test output.
* archiver: address lint nits
* services: archiver: only close the channel once
* services: archiver: use a struct{} for the wait channel
This makes it obvious that the channel is only being used as a signal,
rather than anything useful being piped through it.
* archiver: tests: fix expectations
Move the close of the channel into doArchive() itself; notably, before these
goroutines move on to waiting on the Release cond.
The tests are adjusted to reflect that we can't WaitForCompletion() after
they've already completed, as WaitForCompletion() doesn't indicate that
they've been released from the queue yet.
* archiver: tests: set cchan to nil for comparison
* archiver: move ctx.Error's back into the route handlers
We shouldn't be setting this in a service, we should just be validating the
request that we were handed.
* services: archiver: use regex to match a hash
This makes sure we don't try and use refName as a hash when it's clearly not
one, e.g. heads/pull/foo.
* routers: repo: remove the weird /archive/status endpoint
We don't need to do this anymore, we can just continue POSTing to the
archive/* endpoint until we're told the download's complete. This avoids a
potential naming conflict, where a ref could start with "status/"
* archiver: tests: bump reasonable timeout to 15s
* archiver: tests: actually release timedReq
* archiver: tests: run through inFlight instead of manually checking
While we're here, add a test for manually re-processing an archive that's
already been complete. Re-open the channel and mark it incomplete, so that
doArchive can just mark it complete again.
* initArchiveLinks: prevent default behavior from clicking
* archiver: alias gitea's context, golang context import pending
* archiver: simplify logic, just reconstruct slices
While the previous logic was perhaps slightly more efficient, the
new variant's readability is much improved.
* archiver: don't block shutdown on waiting for archive
The technique established launches a goroutine to do the wait,
which will close a wait channel upon termination. For the timeout
case, we also send back a value indicating whether the timeout was
hit or not.
The timeouts are expected to be relatively small, but still a multi-
second delay to shutdown due to this could be unfortunate.
* archiver: simplify shutdown logic
We can just grab the shutdown channel from the graceful manager instead of
constructing a channel to halt the caller and/or pass a result back.
* Style issues
* Fix mis-merge
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2020-11-08 01:57:28 +05:30
|
|
|
// InitiateDownload will enqueue an archival request, as needed. It may submit
|
|
|
|
// a request that's already in-progress, but the archiver service will just
|
|
|
|
// kind of drop it on the floor if this is the case.
|
|
|
|
func InitiateDownload(ctx *context.Context) {
|
|
|
|
uri := ctx.Params("*")
|
2021-06-24 02:42:38 +05:30
|
|
|
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("archiver_service.NewRequest", err)
|
|
|
|
return
|
|
|
|
}
|
[RFC] Make archival asynchronous (#11296)
* Make archival asynchronous
The prime benefit being sought here is for large archives to not
clog up the rendering process and cause unsightly proxy timeouts.
As a secondary benefit, archive-in-progress is moved out of the
way into a /tmp file so that new archival requests for the same
commit will not get fulfilled based on an archive that isn't yet
finished.
This asynchronous system is fairly primitive; request comes in, we'll
spawn off a new goroutine to handle it, then we'll mark it as done.
Status requests will see if the file exists in the final location,
and report the archival as done when it exists.
Fixes #11265
* Archive links: drop initial delay to three-quarters of a second
Some, or perhaps even most, archives will not take all that long to archive.
The archive process starts as soon as the download button is initially
clicked, so in theory they could be done quite quickly. Drop the initial
delay down to three-quarters of a second to make it more responsive in the
common case of the archive being quickly created.
* archiver: restructure a little bit to facilitate testing
This introduces two sync.Cond pointers to the archiver package. If they're
non-nil when we go to process a request, we'll wait until signalled (at all)
to proceed. The tests will then create the sync.Cond so that it can signal
at-will and sanity-check the state of the queue at different phases.
The author believes that nil-checking these two sync.Cond pointers on every
archive processing will introduce minimal overhead with no impact on
maintainability.
* gofmt nit: no space around binary + operator
* services: archiver: appease golangci-lint, lock queueMutex
Locking/unlocking the queueMutex is allowed, but not required, for
Cond.Signal() and Cond.Broadcast(). The magic at play here is just a little
too much for golangci-lint, as we take the address of queueMutex and this is
mostly used in archiver.go; the variable still gets flagged as unused.
* archiver: tests: fix several timing nits
Once we've signaled a cond var, it may take some small amount of time for
the goroutines released to hit the spot we're wanting them to be at. Give
them an appropriate amount of time.
* archiver: tests: no underscore in var name, ungh
* archiver: tests: Test* is run in a separate context than TestMain
We must setup the mutex/cond variables at the beginning of any test that's
going to use it, or else these will be nil when the test is actually ran.
* archiver: tests: hopefully final tweak
Things got shuffled around such that we carefully build up and release
requests from the queue, so we can validate the state of the queue at each
step. Fix some assertions that no longer hold true as fallout.
* repo: Download: restore some semblance of previous behavior
When archival was made async, the GET endpoint was only useful if a previous
POST had initiated the download. This commit restores the previous behavior,
to an extent; we'll now submit the archive request there and return a
"202 Accepted" to indicate that it's processing if we didn't manage to
complete the request within ~2 seconds of submission.
This lets a client directly GET the archive, and gives them some indication
that they may attempt to GET it again at a later time.
* archiver: tests: simplify a bit further
We don't need to risk failure and use time.ParseDuration to get 2 *
time.Second.
else if isn't really necessary if the conditions are simple enough and lead
to the same result.
* archiver: tests: resolve potential source of flakiness
Increase all timeouts to 10 seconds; these aren't hard-coded sleeps, so
there's no guarantee we'll actually take that long. If we need longer to
not have a false-positive, then so be it.
While here, various assert.{Not,}Equal arguments are flipped around so that
the wording in error output reflects reality, where the expected argument is
second and actual third.
* archiver: setup infrastructure for notifying consumers of completion
This API will *not* allow consumers to subscribe to specific requests being
completed, just *any* request being completed. The caller is responsible for
determining if their request is satisfied and waiting again if needed.
* repo: archive: make GET endpoint synchronous again
If the request isn't complete, this endpoint will now submit the request and
wait for completion using the new API. This may still be susceptible to
timeouts for larger repos, but other endpoints now exist that the web
interface will use to negotiate its way through larger archive processes.
* archiver: tests: amend test to include WaitForCompletion()
This is a trivial one, so go ahead and include it.
* archiver: tests: fix test by calling NewContext()
The mutex is otherwise uninitialized, so we need to ensure that we're
actually initializing it if we plan to test it.
* archiver: tests: integrate new WaitForCompletion a little better
We can use this to wait for archives to come in, rather than spinning and
hoping with a timeout.
* archiver: tests: combine numQueued declaration with next-instruction assignment
* routers: repo: reap unused archiving flag from DownloadStatus()
This had some planned usage before, indicating whether this request
initiated the archival process or not. After several rounds of refactoring,
this use was deemed not necessary for much of anything and got boiled down
to !complete in all cases.
* services: archiver: restructure to use a channel
We now offer two forms of waiting for a request:
- WaitForCompletion: wait for completion with no timeout
- TimedWaitForCompletion: wait for completion with timeout
In both cases, we wait for the given request's cchan to close; in the latter
case, we do so with the caller-provided timeout. This completely removes the
need for busy-wait loops in Download/InitiateDownload, as it's fairly clean
to wait on a channel with timeout.
* services: archiver: use defer to unlock now that we can
This previously carried the lock into the goroutine, but an intermediate
step just added the request to archiveInProgress outside of the new
goroutine and removed the need for the goroutine to start out with it.
* Revert "archiver: tests: combine numQueued declaration with next-instruction assignment"
This reverts commit bcc52140238e16680f2e05e448e9be51372afdf5.
Revert "archiver: tests: integrate new WaitForCompletion a little better"
This reverts commit 9fc8bedb5667d24d3a3c7843dc28a229efffb1e6.
Revert "archiver: tests: fix test by calling NewContext()"
This reverts commit 709c35685eaaf261ebbb7d3420e3376a4ee8e7f2.
Revert "archiver: tests: amend test to include WaitForCompletion()"
This reverts commit 75261f56bc05d1fa8ff7e81dcbc0ccd93fdc9d50.
* archiver: tests: first attempt at WaitForCompletion() tests
* archiver: tests: slight improvement, less busy-loop
Just wait for the requests to complete in order, instead of busy-waiting
with a timeout. This is slightly less fragile.
While here, reverse the arguments of a nearby assert.Equal() so that
expected/actual are correct in any test output.
* archiver: address lint nits
* services: archiver: only close the channel once
* services: archiver: use a struct{} for the wait channel
This makes it obvious that the channel is only being used as a signal,
rather than anything useful being piped through it.
* archiver: tests: fix expectations
Move the close of the channel into doArchive() itself; notably, before these
goroutines move on to waiting on the Release cond.
The tests are adjusted to reflect that we can't WaitForCompletion() after
they've already completed, as WaitForCompletion() doesn't indicate that
they've been released from the queue yet.
* archiver: tests: set cchan to nil for comparison
* archiver: move ctx.Error's back into the route handlers
We shouldn't be setting this in a service, we should just be validating the
request that we were handed.
* services: archiver: use regex to match a hash
This makes sure we don't try and use refName as a hash when it's clearly not
one, e.g. heads/pull/foo.
* routers: repo: remove the weird /archive/status endpoint
We don't need to do this anymore, we can just continue POSTing to the
archive/* endpoint until we're told the download's complete. This avoids a
potential naming conflict, where a ref could start with "status/"
* archiver: tests: bump reasonable timeout to 15s
* archiver: tests: actually release timedReq
* archiver: tests: run through inFlight instead of manually checking
While we're here, add a test for manually re-processing an archive that's
already been complete. Re-open the channel and mark it incomplete, so that
doArchive can just mark it complete again.
* initArchiveLinks: prevent default behavior from clicking
* archiver: alias gitea's context, golang context import pending
* archiver: simplify logic, just reconstruct slices
While the previous logic was perhaps slightly more efficient, the
new variant's readability is much improved.
* archiver: don't block shutdown on waiting for archive
The technique established launches a goroutine to do the wait,
which will close a wait channel upon termination. For the timeout
case, we also send back a value indicating whether the timeout was
hit or not.
The timeouts are expected to be relatively small, but still a multi-
second delay to shutdown due to this could be unfortunate.
* archiver: simplify shutdown logic
We can just grab the shutdown channel from the graceful manager instead of
constructing a channel to halt the caller and/or pass a result back.
* Style issues
* Fix mis-merge
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2020-11-08 01:57:28 +05:30
|
|
|
if aReq == nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusNotFound)
|
2014-09-25 03:13:33 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:52:54 +05:30
|
|
|
archiver, err := repo_model.GetRepoArchiver(ctx, aReq.RepoID, aReq.Type, aReq.CommitID)
|
2021-06-24 02:42:38 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("archiver_service.StartArchive", err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-06 12:49:28 +05:30
|
|
|
if archiver == nil || archiver.Status != repo_model.ArchiverReady {
|
2021-06-24 02:42:38 +05:30
|
|
|
if err := archiver_service.StartArchive(aReq); err != nil {
|
|
|
|
ctx.ServerError("archiver_service.StartArchive", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var completed bool
|
2021-12-06 12:49:28 +05:30
|
|
|
if archiver != nil && archiver.Status == repo_model.ArchiverReady {
|
2021-06-24 02:42:38 +05:30
|
|
|
completed = true
|
2014-03-22 23:20:50 +05:30
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2021-06-24 02:42:38 +05:30
|
|
|
"complete": completed,
|
[RFC] Make archival asynchronous (#11296)
* Make archival asynchronous
The prime benefit being sought here is for large archives to not
clog up the rendering process and cause unsightly proxy timeouts.
As a secondary benefit, archive-in-progress is moved out of the
way into a /tmp file so that new archival requests for the same
commit will not get fulfilled based on an archive that isn't yet
finished.
This asynchronous system is fairly primitive; request comes in, we'll
spawn off a new goroutine to handle it, then we'll mark it as done.
Status requests will see if the file exists in the final location,
and report the archival as done when it exists.
Fixes #11265
* Archive links: drop initial delay to three-quarters of a second
Some, or perhaps even most, archives will not take all that long to archive.
The archive process starts as soon as the download button is initially
clicked, so in theory they could be done quite quickly. Drop the initial
delay down to three-quarters of a second to make it more responsive in the
common case of the archive being quickly created.
* archiver: restructure a little bit to facilitate testing
This introduces two sync.Cond pointers to the archiver package. If they're
non-nil when we go to process a request, we'll wait until signalled (at all)
to proceed. The tests will then create the sync.Cond so that it can signal
at-will and sanity-check the state of the queue at different phases.
The author believes that nil-checking these two sync.Cond pointers on every
archive processing will introduce minimal overhead with no impact on
maintainability.
* gofmt nit: no space around binary + operator
* services: archiver: appease golangci-lint, lock queueMutex
Locking/unlocking the queueMutex is allowed, but not required, for
Cond.Signal() and Cond.Broadcast(). The magic at play here is just a little
too much for golangci-lint, as we take the address of queueMutex and this is
mostly used in archiver.go; the variable still gets flagged as unused.
* archiver: tests: fix several timing nits
Once we've signaled a cond var, it may take some small amount of time for
the goroutines released to hit the spot we're wanting them to be at. Give
them an appropriate amount of time.
* archiver: tests: no underscore in var name, ungh
* archiver: tests: Test* is run in a separate context than TestMain
We must setup the mutex/cond variables at the beginning of any test that's
going to use it, or else these will be nil when the test is actually ran.
* archiver: tests: hopefully final tweak
Things got shuffled around such that we carefully build up and release
requests from the queue, so we can validate the state of the queue at each
step. Fix some assertions that no longer hold true as fallout.
* repo: Download: restore some semblance of previous behavior
When archival was made async, the GET endpoint was only useful if a previous
POST had initiated the download. This commit restores the previous behavior,
to an extent; we'll now submit the archive request there and return a
"202 Accepted" to indicate that it's processing if we didn't manage to
complete the request within ~2 seconds of submission.
This lets a client directly GET the archive, and gives them some indication
that they may attempt to GET it again at a later time.
* archiver: tests: simplify a bit further
We don't need to risk failure and use time.ParseDuration to get 2 *
time.Second.
else if isn't really necessary if the conditions are simple enough and lead
to the same result.
* archiver: tests: resolve potential source of flakiness
Increase all timeouts to 10 seconds; these aren't hard-coded sleeps, so
there's no guarantee we'll actually take that long. If we need longer to
not have a false-positive, then so be it.
While here, various assert.{Not,}Equal arguments are flipped around so that
the wording in error output reflects reality, where the expected argument is
second and actual third.
* archiver: setup infrastructure for notifying consumers of completion
This API will *not* allow consumers to subscribe to specific requests being
completed, just *any* request being completed. The caller is responsible for
determining if their request is satisfied and waiting again if needed.
* repo: archive: make GET endpoint synchronous again
If the request isn't complete, this endpoint will now submit the request and
wait for completion using the new API. This may still be susceptible to
timeouts for larger repos, but other endpoints now exist that the web
interface will use to negotiate its way through larger archive processes.
* archiver: tests: amend test to include WaitForCompletion()
This is a trivial one, so go ahead and include it.
* archiver: tests: fix test by calling NewContext()
The mutex is otherwise uninitialized, so we need to ensure that we're
actually initializing it if we plan to test it.
* archiver: tests: integrate new WaitForCompletion a little better
We can use this to wait for archives to come in, rather than spinning and
hoping with a timeout.
* archiver: tests: combine numQueued declaration with next-instruction assignment
* routers: repo: reap unused archiving flag from DownloadStatus()
This had some planned usage before, indicating whether this request
initiated the archival process or not. After several rounds of refactoring,
this use was deemed not necessary for much of anything and got boiled down
to !complete in all cases.
* services: archiver: restructure to use a channel
We now offer two forms of waiting for a request:
- WaitForCompletion: wait for completion with no timeout
- TimedWaitForCompletion: wait for completion with timeout
In both cases, we wait for the given request's cchan to close; in the latter
case, we do so with the caller-provided timeout. This completely removes the
need for busy-wait loops in Download/InitiateDownload, as it's fairly clean
to wait on a channel with timeout.
* services: archiver: use defer to unlock now that we can
This previously carried the lock into the goroutine, but an intermediate
step just added the request to archiveInProgress outside of the new
goroutine and removed the need for the goroutine to start out with it.
* Revert "archiver: tests: combine numQueued declaration with next-instruction assignment"
This reverts commit bcc52140238e16680f2e05e448e9be51372afdf5.
Revert "archiver: tests: integrate new WaitForCompletion a little better"
This reverts commit 9fc8bedb5667d24d3a3c7843dc28a229efffb1e6.
Revert "archiver: tests: fix test by calling NewContext()"
This reverts commit 709c35685eaaf261ebbb7d3420e3376a4ee8e7f2.
Revert "archiver: tests: amend test to include WaitForCompletion()"
This reverts commit 75261f56bc05d1fa8ff7e81dcbc0ccd93fdc9d50.
* archiver: tests: first attempt at WaitForCompletion() tests
* archiver: tests: slight improvement, less busy-loop
Just wait for the requests to complete in order, instead of busy-waiting
with a timeout. This is slightly less fragile.
While here, reverse the arguments of a nearby assert.Equal() so that
expected/actual are correct in any test output.
* archiver: address lint nits
* services: archiver: only close the channel once
* services: archiver: use a struct{} for the wait channel
This makes it obvious that the channel is only being used as a signal,
rather than anything useful being piped through it.
* archiver: tests: fix expectations
Move the close of the channel into doArchive() itself; notably, before these
goroutines move on to waiting on the Release cond.
The tests are adjusted to reflect that we can't WaitForCompletion() after
they've already completed, as WaitForCompletion() doesn't indicate that
they've been released from the queue yet.
* archiver: tests: set cchan to nil for comparison
* archiver: move ctx.Error's back into the route handlers
We shouldn't be setting this in a service, we should just be validating the
request that we were handed.
* services: archiver: use regex to match a hash
This makes sure we don't try and use refName as a hash when it's clearly not
one, e.g. heads/pull/foo.
* routers: repo: remove the weird /archive/status endpoint
We don't need to do this anymore, we can just continue POSTing to the
archive/* endpoint until we're told the download's complete. This avoids a
potential naming conflict, where a ref could start with "status/"
* archiver: tests: bump reasonable timeout to 15s
* archiver: tests: actually release timedReq
* archiver: tests: run through inFlight instead of manually checking
While we're here, add a test for manually re-processing an archive that's
already been complete. Re-open the channel and mark it incomplete, so that
doArchive can just mark it complete again.
* initArchiveLinks: prevent default behavior from clicking
* archiver: alias gitea's context, golang context import pending
* archiver: simplify logic, just reconstruct slices
While the previous logic was perhaps slightly more efficient, the
new variant's readability is much improved.
* archiver: don't block shutdown on waiting for archive
The technique established launches a goroutine to do the wait,
which will close a wait channel upon termination. For the timeout
case, we also send back a value indicating whether the timeout was
hit or not.
The timeouts are expected to be relatively small, but still a multi-
second delay to shutdown due to this could be unfortunate.
* archiver: simplify shutdown logic
We can just grab the shutdown channel from the graceful manager instead of
constructing a channel to halt the caller and/or pass a result back.
* Style issues
* Fix mis-merge
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2020-11-08 01:57:28 +05:30
|
|
|
})
|
2014-03-22 23:20:50 +05:30
|
|
|
}
|
2022-04-08 00:29:56 +05:30
|
|
|
|
|
|
|
// SearchRepo repositories via options
|
|
|
|
func SearchRepo(ctx *context.Context) {
|
2022-06-06 13:31:49 +05:30
|
|
|
opts := &repo_model.SearchRepoOptions{
|
2022-04-08 00:29:56 +05:30
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
Page: ctx.FormInt("page"),
|
|
|
|
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
|
|
|
|
},
|
|
|
|
Actor: ctx.Doer,
|
|
|
|
Keyword: ctx.FormTrim("q"),
|
|
|
|
OwnerID: ctx.FormInt64("uid"),
|
|
|
|
PriorityOwnerID: ctx.FormInt64("priority_owner_id"),
|
|
|
|
TeamID: ctx.FormInt64("team_id"),
|
|
|
|
TopicOnly: ctx.FormBool("topic"),
|
|
|
|
Collaborate: util.OptionalBoolNone,
|
|
|
|
Private: ctx.IsSigned && (ctx.FormString("private") == "" || ctx.FormBool("private")),
|
|
|
|
Template: util.OptionalBoolNone,
|
|
|
|
StarredByID: ctx.FormInt64("starredBy"),
|
|
|
|
IncludeDescription: ctx.FormBool("includeDesc"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.FormString("template") != "" {
|
|
|
|
opts.Template = util.OptionalBoolOf(ctx.FormBool("template"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.FormBool("exclusive") {
|
|
|
|
opts.Collaborate = util.OptionalBoolFalse
|
|
|
|
}
|
|
|
|
|
|
|
|
mode := ctx.FormString("mode")
|
|
|
|
switch mode {
|
|
|
|
case "source":
|
|
|
|
opts.Fork = util.OptionalBoolFalse
|
|
|
|
opts.Mirror = util.OptionalBoolFalse
|
|
|
|
case "fork":
|
|
|
|
opts.Fork = util.OptionalBoolTrue
|
|
|
|
case "mirror":
|
|
|
|
opts.Mirror = util.OptionalBoolTrue
|
|
|
|
case "collaborative":
|
|
|
|
opts.Mirror = util.OptionalBoolFalse
|
|
|
|
opts.Collaborate = util.OptionalBoolTrue
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid search mode: \"%s\"", mode))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.FormString("archived") != "" {
|
|
|
|
opts.Archived = util.OptionalBoolOf(ctx.FormBool("archived"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.FormString("is_private") != "" {
|
|
|
|
opts.IsPrivate = util.OptionalBoolOf(ctx.FormBool("is_private"))
|
|
|
|
}
|
|
|
|
|
|
|
|
sortMode := ctx.FormString("sort")
|
|
|
|
if len(sortMode) > 0 {
|
|
|
|
sortOrder := ctx.FormString("order")
|
|
|
|
if len(sortOrder) == 0 {
|
|
|
|
sortOrder = "asc"
|
|
|
|
}
|
2023-05-08 15:06:54 +05:30
|
|
|
if searchModeMap, ok := repo_model.SearchOrderByMap[sortOrder]; ok {
|
2022-04-08 00:29:56 +05:30
|
|
|
if orderBy, ok := searchModeMap[sortMode]; ok {
|
|
|
|
opts.OrderBy = orderBy
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort mode: \"%s\"", sortMode))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort order: \"%s\"", sortOrder))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-11-19 13:42:33 +05:30
|
|
|
repos, count, err := repo_model.SearchRepository(ctx, opts)
|
2022-04-08 00:29:56 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.JSON(http.StatusInternalServerError, api.SearchError{
|
|
|
|
OK: false,
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-27 02:04:30 +05:30
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
|
|
|
|
// To improve performance when only the count is requested
|
|
|
|
if ctx.FormBool("count_only") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-14 03:29:01 +05:30
|
|
|
// collect the latest commit of each repo
|
|
|
|
repoIDsToLatestCommitSHAs := make(map[int64]string)
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(repos))
|
|
|
|
for _, repo := range repos {
|
|
|
|
go func(repo *repo_model.Repository) {
|
|
|
|
defer wg.Done()
|
|
|
|
commitID, err := repo_service.GetBranchCommitID(ctx, repo, repo.DefaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
repoIDsToLatestCommitSHAs[repo.ID] = commitID
|
|
|
|
}(repo)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// call the database O(1) times to get the commit statuses for all repos
|
|
|
|
repoToItsLatestCommitStatuses, err := git_model.GetLatestCommitStatusForPairs(ctx, repoIDsToLatestCommitSHAs, db.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetLatestCommitStatusForPairs: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]*repo_service.WebSearchRepository, len(repos))
|
2022-04-08 00:29:56 +05:30
|
|
|
for i, repo := range repos {
|
2023-05-14 03:29:01 +05:30
|
|
|
results[i] = &repo_service.WebSearchRepository{
|
|
|
|
Repository: &api.Repository{
|
|
|
|
ID: repo.ID,
|
|
|
|
FullName: repo.FullName(),
|
|
|
|
Fork: repo.IsFork,
|
|
|
|
Private: repo.IsPrivate,
|
|
|
|
Template: repo.IsTemplate,
|
|
|
|
Mirror: repo.IsMirror,
|
|
|
|
Stars: repo.NumStars,
|
|
|
|
HTMLURL: repo.HTMLURL(),
|
|
|
|
Link: repo.Link(),
|
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
},
|
|
|
|
LatestCommitStatus: git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID]),
|
2022-04-08 00:29:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 03:29:01 +05:30
|
|
|
ctx.JSON(http.StatusOK, repo_service.WebSearchResults{
|
2022-04-08 00:29:56 +05:30
|
|
|
OK: true,
|
|
|
|
Data: results,
|
|
|
|
})
|
|
|
|
}
|