2014-02-19 21:45:43 -05:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2020-01-22 23:46:46 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-02-19 21:45:43 -05:00
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-01 01:47:30 +01:00
|
|
|
"errors"
|
2014-07-26 02:28:04 -04:00
|
|
|
"fmt"
|
2021-04-05 17:30:52 +02:00
|
|
|
"net/http"
|
2023-09-07 17:37:47 +08:00
|
|
|
"slices"
|
2014-07-26 02:28:04 -04:00
|
|
|
"strings"
|
2014-03-22 13:50:50 -04:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-05-14 00:59:01 +03:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-03-29 14:29:02 +08:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-08-25 10:31:57 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-11-19 21:39:57 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-04-27 14:06:45 +08:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-23 03:18:33 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-03-29 15:23:45 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-04-08 02:59:56 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2024-12-22 23:33:19 +08:00
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2022-04-08 02:59:56 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 03:57:15 +01:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2021-04-06 20:44:05 +01:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2019-10-26 14:54:11 +08:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-12-06 15:19:28 +08:00
|
|
|
archiver_service "code.gitea.io/gitea/services/repository/archiver"
|
2024-03-06 20:17:19 +08:00
|
|
|
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
|
2014-02-19 21:45:43 -05:00
|
|
|
)
|
|
|
|
|
|
2014-06-22 23:11:12 -04:00
|
|
|
const (
|
2024-12-22 23:33:19 +08:00
|
|
|
tplCreate templates.TplName = "repo/create"
|
|
|
|
|
tplAlertDetails templates.TplName = "base/alert_details"
|
2014-06-22 23:11:12 -04:00
|
|
|
)
|
|
|
|
|
|
2019-01-18 00:01:04 +00:00
|
|
|
// MustBeNotEmpty render when a repo is a empty git dir
|
|
|
|
|
func MustBeNotEmpty(ctx *context.Context) {
|
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2016-01-07 11:07:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 19:04:25 -07:00
|
|
|
// MustBeEditable check that repo can be edited
|
|
|
|
|
func MustBeEditable(ctx *context.Context) {
|
2025-01-13 23:35:34 -08:00
|
|
|
if !ctx.Repo.Repository.CanEnableEditor() {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2017-10-29 19:04:25 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustBeAbleToUpload check that repo can be uploaded to
|
|
|
|
|
func MustBeAbleToUpload(ctx *context.Context) {
|
|
|
|
|
if !setting.Repository.Upload.Enabled {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2017-10-29 19:04:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 14:06:45 +08:00
|
|
|
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
|
|
|
|
|
}
|
2025-12-04 16:20:23 -08:00
|
|
|
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount(ctx)
|
2023-04-27 14:06:45 +08:00
|
|
|
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 17:49:20 +08:00
|
|
|
func checkContextUser(ctx *context.Context, uid int64) *user_model.User {
|
2023-09-14 19:09:32 +02:00
|
|
|
orgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, ctx.Doer.ID)
|
2015-09-07 13:58:23 -04:00
|
|
|
if err != nil {
|
2019-11-20 12:27:49 +01:00
|
|
|
ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
|
2015-08-28 18:33:09 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 06:45:31 +00:00
|
|
|
var orgsAvailable []*organization.Organization
|
2025-06-18 03:48:09 +02:00
|
|
|
for i := range orgs {
|
2025-04-08 06:45:31 +00:00
|
|
|
if ctx.Doer.CanCreateRepoIn(orgs[i].AsUser()) {
|
|
|
|
|
orgsAvailable = append(orgsAvailable, orgs[i])
|
2020-05-03 23:08:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-08 06:45:31 +00:00
|
|
|
ctx.Data["Orgs"] = orgsAvailable
|
2020-05-03 23:08:24 +02:00
|
|
|
|
2015-07-19 17:11:16 +08:00
|
|
|
// Not equal means current user is an organization.
|
2022-03-22 08:03:22 +01:00
|
|
|
if uid == ctx.Doer.ID || uid == 0 {
|
|
|
|
|
return ctx.Doer
|
2015-07-19 17:11:16 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-03 10:48:26 +08:00
|
|
|
org, err := user_model.GetUserByID(ctx, uid)
|
2021-11-24 17:49:20 +08:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2022-03-22 08:03:22 +01:00
|
|
|
return ctx.Doer
|
2015-07-19 17:11:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
ctx.ServerError("GetUserByID", fmt.Errorf("[%d]: %w", uid, err))
|
2015-07-19 17:11:16 +08:00
|
|
|
return nil
|
2015-08-28 18:33:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check ownership of organization.
|
2017-12-20 23:43:26 -08:00
|
|
|
if !org.IsOrganization() {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2015-07-19 17:11:16 +08:00
|
|
|
return nil
|
2014-11-05 23:30:04 -05:00
|
|
|
}
|
2022-03-22 08:03:22 +01:00
|
|
|
if !ctx.Doer.IsAdmin {
|
2023-10-03 12:30:41 +02:00
|
|
|
canCreate, err := organization.OrgFromUser(org).CanCreateOrgRepo(ctx, ctx.Doer.ID)
|
2017-12-20 23:43:26 -08:00
|
|
|
if err != nil {
|
2019-11-20 12:27:49 +01:00
|
|
|
ctx.ServerError("CanCreateOrgRepo", err)
|
2017-12-20 23:43:26 -08:00
|
|
|
return nil
|
2019-11-20 12:27:49 +01:00
|
|
|
} else if !canCreate {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2017-12-20 23:43:26 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-04-30 11:11:56 -04:00
|
|
|
} else {
|
|
|
|
|
ctx.Data["Orgs"] = orgs
|
2017-12-20 23:43:26 -08:00
|
|
|
}
|
2015-07-19 17:11:16 +08:00
|
|
|
return org
|
2014-11-05 23:30:04 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 06:59:56 -06:00
|
|
|
func getRepoPrivate(ctx *context.Context) bool {
|
|
|
|
|
switch strings.ToLower(setting.Repository.DefaultPrivate) {
|
|
|
|
|
case setting.RepoCreatingLastUserVisibility:
|
2022-03-22 08:03:22 +01:00
|
|
|
return ctx.Doer.LastRepoVisibility
|
2017-12-20 06:59:56 -06:00
|
|
|
case setting.RepoCreatingPrivate:
|
|
|
|
|
return true
|
|
|
|
|
case setting.RepoCreatingPublic:
|
|
|
|
|
return false
|
|
|
|
|
default:
|
2022-03-22 08:03:22 +01:00
|
|
|
return ctx.Doer.LastRepoVisibility
|
2017-12-20 06:59:56 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 12:01:19 +08:00
|
|
|
func createCommon(ctx *context.Context) {
|
2014-07-26 00:24:27 -04:00
|
|
|
ctx.Data["Title"] = ctx.Tr("new_repo")
|
2022-03-29 15:23:45 +08:00
|
|
|
ctx.Data["Gitignores"] = repo_module.Gitignores
|
2023-04-10 16:44:02 +08:00
|
|
|
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
|
2022-03-29 15:23:45 +08:00
|
|
|
ctx.Data["Licenses"] = repo_module.Licenses
|
|
|
|
|
ctx.Data["Readmes"] = repo_module.Readmes
|
2015-10-25 04:26:26 -04:00
|
|
|
ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
|
2025-04-08 06:45:31 +00:00
|
|
|
ctx.Data["CanCreateRepoInDoer"] = ctx.Doer.CanCreateRepoIn(ctx.Doer)
|
2025-03-27 14:16:17 +01:00
|
|
|
ctx.Data["MaxCreationLimitOfDoer"] = ctx.Doer.MaxCreationLimit()
|
2025-01-03 12:01:19 +08:00
|
|
|
ctx.Data["SupportedObjectFormats"] = git.DefaultFeatures().SupportedObjectFormats
|
|
|
|
|
ctx.Data["DefaultObjectFormat"] = git.Sha1ObjectFormat
|
|
|
|
|
}
|
2014-06-25 03:55:59 -04:00
|
|
|
|
2025-01-03 12:01:19 +08:00
|
|
|
// Create render creating repository page
|
|
|
|
|
func Create(ctx *context.Context) {
|
|
|
|
|
createCommon(ctx)
|
2021-07-29 09:42:15 +08:00
|
|
|
ctxUser := checkContextUser(ctx, ctx.FormInt64("org"))
|
2015-07-19 17:11:16 +08:00
|
|
|
if ctx.Written() {
|
2014-11-05 23:30:04 -05:00
|
|
|
return
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
2014-06-28 15:43:25 -04:00
|
|
|
ctx.Data["ContextUser"] = ctxUser
|
|
|
|
|
|
2025-01-03 12:01:19 +08:00
|
|
|
ctx.Data["readme"] = "Default"
|
|
|
|
|
ctx.Data["private"] = getRepoPrivate(ctx)
|
|
|
|
|
ctx.Data["default_branch"] = setting.Repository.DefaultBranch
|
2019-11-11 09:15:29 -06:00
|
|
|
ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select")
|
2025-01-03 12:01:19 +08:00
|
|
|
|
2021-07-29 09:42:15 +08:00
|
|
|
templateID := ctx.FormInt64("template_id")
|
2019-11-11 09:15:29 -06:00
|
|
|
if templateID > 0 {
|
2022-12-03 10:48:26 +08:00
|
|
|
templateRepo, err := repo_model.GetRepositoryByID(ctx, templateID)
|
2022-08-25 10:31:57 +08:00
|
|
|
if err == nil && access_model.CheckRepoUnitUser(ctx, templateRepo, ctxUser, unit.TypeCode) {
|
2019-11-11 09:15:29 -06:00
|
|
|
ctx.Data["repo_template"] = templateID
|
|
|
|
|
ctx.Data["repo_template_name"] = templateRepo.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplCreate)
|
2014-04-10 18:09:57 -04:00
|
|
|
}
|
2014-03-07 16:05:18 -05:00
|
|
|
|
2024-12-22 23:33:19 +08:00
|
|
|
func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl templates.TplName, form any) {
|
2015-08-28 18:33:09 +08:00
|
|
|
switch {
|
2021-12-12 23:48:20 +08:00
|
|
|
case repo_model.IsErrReachLimitOfRepo(err):
|
2022-01-02 02:38:07 +00:00
|
|
|
maxCreationLimit := owner.MaxCreationLimit()
|
2022-01-02 11:33:57 +08:00
|
|
|
msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit)
|
2021-12-31 08:43:03 +00:00
|
|
|
ctx.RenderWithErr(msg, tpl, form)
|
2021-12-12 23:48:20 +08:00
|
|
|
case repo_model.IsErrRepoAlreadyExist(err):
|
2015-08-28 18:33:09 +08:00
|
|
|
ctx.Data["Err_RepoName"] = true
|
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
|
2021-12-12 23:48:20 +08:00
|
|
|
case repo_model.IsErrRepoFilesAlreadyExist(err):
|
2020-09-25 05:09:23 +01:00
|
|
|
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 17:49:20 +08:00
|
|
|
case db.IsErrNameReserved(err):
|
2015-08-28 18:33:09 +08:00
|
|
|
ctx.Data["Err_RepoName"] = true
|
2021-11-24 17:49:20 +08:00
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form)
|
|
|
|
|
case db.IsErrNamePatternNotAllowed(err):
|
2015-08-28 18:33:09 +08:00
|
|
|
ctx.Data["Err_RepoName"] = true
|
2021-11-24 17:49:20 +08:00
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form)
|
2015-08-28 18:33:09 +08:00
|
|
|
default:
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError(name, err)
|
2015-08-28 18:33:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
// CreatePost response for creating repository
|
2021-01-26 23:36:53 +08:00
|
|
|
func CreatePost(ctx *context.Context) {
|
2025-01-03 12:01:19 +08:00
|
|
|
createCommon(ctx)
|
2021-04-06 20:44:05 +01:00
|
|
|
form := web.GetForm(ctx).(*forms.CreateRepoForm)
|
2021-04-07 02:26:41 -05:00
|
|
|
|
2016-11-27 14:03:59 +08:00
|
|
|
ctxUser := checkContextUser(ctx, form.UID)
|
2015-07-19 17:11:16 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
2014-07-26 02:28:04 -04:00
|
|
|
}
|
2014-07-26 00:24:27 -04:00
|
|
|
ctx.Data["ContextUser"] = ctxUser
|
|
|
|
|
|
2025-01-03 12:01:19 +08:00
|
|
|
if form.RepoTemplate > 0 {
|
|
|
|
|
templateRepo, err := repo_model.GetRepositoryByID(ctx, form.RepoTemplate)
|
|
|
|
|
if err == nil && access_model.CheckRepoUnitUser(ctx, templateRepo, ctxUser, unit.TypeCode) {
|
|
|
|
|
ctx.Data["repo_template"] = form.RepoTemplate
|
|
|
|
|
ctx.Data["repo_template_name"] = templateRepo.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 16:00:46 -04:00
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplCreate)
|
2014-03-22 16:00:46 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
var repo *repo_model.Repository
|
2019-11-11 09:15:29 -06:00
|
|
|
var err error
|
|
|
|
|
if form.RepoTemplate > 0 {
|
2024-02-28 21:40:36 +08:00
|
|
|
opts := repo_service.GenerateRepoOptions{
|
2023-07-21 12:32:47 +08:00
|
|
|
Name: form.RepoName,
|
|
|
|
|
Description: form.Description,
|
2024-05-20 08:56:45 +08:00
|
|
|
Private: form.Private || setting.Repository.ForcePrivate,
|
2023-07-21 12:32:47 +08:00
|
|
|
GitContent: form.GitContent,
|
|
|
|
|
Topics: form.Topics,
|
|
|
|
|
GitHooks: form.GitHooks,
|
|
|
|
|
Webhooks: form.Webhooks,
|
|
|
|
|
Avatar: form.Avatar,
|
|
|
|
|
IssueLabels: form.Labels,
|
|
|
|
|
ProtectedBranch: form.ProtectedBranch,
|
2019-11-11 09:15:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 06:17:51 +08:00
|
|
|
repo, err = repo_service.GenerateRepository(ctx, ctx.Doer, ctxUser, templateRepo, opts)
|
2019-11-11 09:15:29 -06:00
|
|
|
if err == nil {
|
|
|
|
|
log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(repo.Link())
|
2019-11-11 09:15:29 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-09-06 20:08:51 +08:00
|
|
|
repo, err = repo_service.CreateRepository(ctx, ctx.Doer, ctxUser, repo_service.CreateRepoOptions{
|
2023-12-17 19:56:08 +08:00
|
|
|
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,
|
|
|
|
|
IsTemplate: form.Template,
|
2024-01-16 20:54:48 +08:00
|
|
|
TrustModel: repo_model.DefaultTrustModel,
|
2023-12-17 19:56:08 +08:00
|
|
|
ObjectFormatName: form.ObjectFormatName,
|
2019-11-11 09:15:29 -06:00
|
|
|
})
|
|
|
|
|
if err == nil {
|
|
|
|
|
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(repo.Link())
|
2019-11-11 09:15:29 -06:00
|
|
|
return
|
|
|
|
|
}
|
2014-03-09 20:06:29 -04:00
|
|
|
}
|
2014-04-12 20:35:35 -04:00
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
|
2014-04-10 18:09:57 -04:00
|
|
|
}
|
2014-04-09 21:28:00 +08:00
|
|
|
|
2025-01-29 21:40:44 -08:00
|
|
|
func handleActionError(ctx *context.Context, err error) {
|
2025-03-31 22:19:32 +02:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, user_model.ErrBlockedUser):
|
2025-01-29 21:40:44 -08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.action.blocked_user"))
|
2025-03-31 22:19:32 +02:00
|
|
|
case repo_service.IsRepositoryLimitReached(err):
|
|
|
|
|
limit := err.(repo_service.LimitReachedError).Limit
|
|
|
|
|
ctx.Flash.Error(ctx.TrN(limit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", limit))
|
|
|
|
|
case errors.Is(err, util.ErrPermissionDenied):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2025-03-31 22:19:32 +02:00
|
|
|
default:
|
2025-01-29 21:40:44 -08:00
|
|
|
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.PathParam("action")), err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 23:37:30 +01:00
|
|
|
// RedirectDownload return a file based on the following infos:
|
|
|
|
|
func RedirectDownload(ctx *context.Context) {
|
|
|
|
|
var (
|
2024-06-19 06:32:45 +08:00
|
|
|
vTag = ctx.PathParam("vTag")
|
|
|
|
|
fileName = ctx.PathParam("fileName")
|
2019-01-06 23:37:30 +01:00
|
|
|
)
|
|
|
|
|
tagNames := []string{vTag}
|
|
|
|
|
curRepo := ctx.Repo.Repository
|
2024-01-15 10:19:25 +08:00
|
|
|
releases, err := db.Find[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
2024-06-15 12:20:14 +08:00
|
|
|
IncludeDrafts: ctx.Repo.CanWrite(unit.TypeReleases),
|
|
|
|
|
RepoID: curRepo.ID,
|
|
|
|
|
TagNames: tagNames,
|
2024-01-15 10:19:25 +08:00
|
|
|
})
|
2019-01-06 23:37:30 +01:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("RedirectDownload", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(releases) == 1 {
|
|
|
|
|
release := releases[0]
|
2022-05-20 22:08:52 +08:00
|
|
|
att, err := repo_model.GetAttachmentByReleaseIDFileName(ctx, release.ID, fileName)
|
2019-01-06 23:37:30 +01:00
|
|
|
if err != nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2019-01-06 23:37:30 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if att != nil {
|
2023-04-12 11:05:23 +02:00
|
|
|
ServeAttachment(ctx, att.UUID)
|
2019-01-06 23:37:30 +01:00
|
|
|
return
|
|
|
|
|
}
|
2023-09-06 13:06:04 +02:00
|
|
|
} else if len(releases) == 0 && vTag == "latest" {
|
|
|
|
|
// GitHub supports the alias "latest" for the latest release
|
|
|
|
|
// We only fetch the latest release if the tag is "latest" and no release with the tag "latest" exists
|
2023-09-25 15:17:37 +02:00
|
|
|
release, err := repo_model.GetLatestReleaseByRepoID(ctx, ctx.Repo.Repository.ID)
|
2023-09-06 13:06:04 +02:00
|
|
|
if err != nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2023-09-06 13:06:04 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
att, err := repo_model.GetAttachmentByReleaseIDFileName(ctx, release.ID, fileName)
|
|
|
|
|
if err != nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2023-09-06 13:06:04 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if att != nil {
|
|
|
|
|
ServeAttachment(ctx, att.UUID)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-01-06 23:37:30 +01:00
|
|
|
}
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2019-01-06 23:37:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-24 05:12:38 +08:00
|
|
|
// Download an archive of a repository
|
|
|
|
|
func Download(ctx *context.Context) {
|
2026-01-16 10:31:12 +01:00
|
|
|
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("*"), ctx.FormStrings("path"))
|
2021-06-24 05:12:38 +08:00
|
|
|
if err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusBadRequest, err.Error())
|
2026-01-16 10:31:12 +01:00
|
|
|
} else if errors.Is(err, util.ErrNotExist) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound, err.Error())
|
2021-11-18 03:47:35 +08:00
|
|
|
} else {
|
|
|
|
|
ctx.ServerError("archiver_service.NewRequest", err)
|
|
|
|
|
}
|
2021-06-24 05:12:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-01-16 10:31:12 +01:00
|
|
|
err = archiver_service.ServeRepoArchive(ctx.Base, aReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
|
ctx.HTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
ctx.ServerError("archiver_service.ServeRepoArchive", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-24 05:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 14:27:28 -06:00
|
|
|
// 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) {
|
2026-01-16 10:31:12 +01:00
|
|
|
paths := ctx.FormStrings("path")
|
|
|
|
|
if setting.Repository.StreamArchives || len(paths) > 0 {
|
2025-09-19 05:51:21 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
|
|
|
|
"complete": true,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-16 10:31:12 +01:00
|
|
|
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("*"), paths)
|
2021-06-24 05:12:38 +08:00
|
|
|
if err != nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusBadRequest, "invalid archive request")
|
2021-06-24 05:12:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
2020-11-07 14:27:28 -06:00
|
|
|
if aReq == nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2014-09-24 17:43:33 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-06 15:01:26 -07:00
|
|
|
archiver, err := repo_model.GetRepoArchiver(ctx, aReq.Repo.ID, aReq.Type, aReq.CommitID)
|
2021-06-24 05:12:38 +08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("archiver_service.StartArchive", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-06 15:19:28 +08:00
|
|
|
if archiver == nil || archiver.Status != repo_model.ArchiverReady {
|
2021-06-24 05:12:38 +08:00
|
|
|
if err := archiver_service.StartArchive(aReq); err != nil {
|
|
|
|
|
ctx.ServerError("archiver_service.StartArchive", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var completed bool
|
2021-12-06 15:19:28 +08:00
|
|
|
if archiver != nil && archiver.Status == repo_model.ArchiverReady {
|
2021-06-24 05:12:38 +08:00
|
|
|
completed = true
|
2014-03-22 13:50:50 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
2021-06-24 05:12:38 +08:00
|
|
|
"complete": completed,
|
2020-11-07 14:27:28 -06:00
|
|
|
})
|
2014-03-22 13:50:50 -04:00
|
|
|
}
|
2022-04-08 02:59:56 +08:00
|
|
|
|
|
|
|
|
// SearchRepo repositories via options
|
|
|
|
|
func SearchRepo(ctx *context.Context) {
|
2024-03-12 12:57:19 +08:00
|
|
|
page := ctx.FormInt("page")
|
|
|
|
|
if page <= 0 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
2025-06-02 10:33:25 -07:00
|
|
|
opts := repo_model.SearchRepoOptions{
|
2022-04-08 02:59:56 +08:00
|
|
|
ListOptions: db.ListOptions{
|
2024-03-12 12:57:19 +08:00
|
|
|
Page: page,
|
2022-04-08 02:59:56 +08:00
|
|
|
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"),
|
2024-02-29 19:52:49 +01:00
|
|
|
Collaborate: optional.None[bool](),
|
2022-04-08 02:59:56 +08:00
|
|
|
Private: ctx.IsSigned && (ctx.FormString("private") == "" || ctx.FormBool("private")),
|
2024-02-29 19:52:49 +01:00
|
|
|
Template: optional.None[bool](),
|
2022-04-08 02:59:56 +08:00
|
|
|
StarredByID: ctx.FormInt64("starredBy"),
|
|
|
|
|
IncludeDescription: ctx.FormBool("includeDesc"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.FormString("template") != "" {
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Template = optional.Some(ctx.FormBool("template"))
|
2022-04-08 02:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.FormBool("exclusive") {
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Collaborate = optional.Some(false)
|
2022-04-08 02:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mode := ctx.FormString("mode")
|
|
|
|
|
switch mode {
|
|
|
|
|
case "source":
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Fork = optional.Some(false)
|
|
|
|
|
opts.Mirror = optional.Some(false)
|
2022-04-08 02:59:56 +08:00
|
|
|
case "fork":
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Fork = optional.Some(true)
|
2022-04-08 02:59:56 +08:00
|
|
|
case "mirror":
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Mirror = optional.Some(true)
|
2022-04-08 02:59:56 +08:00
|
|
|
case "collaborative":
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Mirror = optional.Some(false)
|
|
|
|
|
opts.Collaborate = optional.Some(true)
|
2022-04-08 02:59:56 +08:00
|
|
|
case "":
|
|
|
|
|
default:
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid search mode: \"%s\"", mode))
|
2022-04-08 02:59:56 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.FormString("archived") != "" {
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.Archived = optional.Some(ctx.FormBool("archived"))
|
2022-04-08 02:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.FormString("is_private") != "" {
|
2024-02-29 19:52:49 +01:00
|
|
|
opts.IsPrivate = optional.Some(ctx.FormBool("is_private"))
|
2022-04-08 02:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sortMode := ctx.FormString("sort")
|
|
|
|
|
if len(sortMode) > 0 {
|
|
|
|
|
sortOrder := ctx.FormString("order")
|
|
|
|
|
if len(sortOrder) == 0 {
|
|
|
|
|
sortOrder = "asc"
|
|
|
|
|
}
|
2024-06-15 08:45:02 +02:00
|
|
|
if searchModeMap, ok := repo_model.OrderByMap[sortOrder]; ok {
|
2022-04-08 02:59:56 +08:00
|
|
|
if orderBy, ok := searchModeMap[sortMode]; ok {
|
|
|
|
|
opts.OrderBy = orderBy
|
|
|
|
|
} else {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort mode: \"%s\"", sortMode))
|
2022-04-08 02:59:56 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort order: \"%s\"", sortOrder))
|
2022-04-08 02:59:56 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 19:05:17 +08:00
|
|
|
// To improve performance when only the count is requested
|
|
|
|
|
if ctx.FormBool("count_only") {
|
|
|
|
|
if count, err := repo_model.CountRepository(ctx, opts); err != nil {
|
|
|
|
|
log.Error("CountRepository: %v", err)
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, nil) // frontend JS doesn't handle error response (same as below)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
|
ctx.JSONOK()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
repos, count, err := repo_model.SearchRepository(ctx, opts)
|
2022-04-08 02:59:56 +08:00
|
|
|
if err != nil {
|
2024-03-18 19:05:17 +08:00
|
|
|
log.Error("SearchRepository: %v", err)
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, nil)
|
2022-04-08 02:59:56 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 20:34:30 +00:00
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
|
|
2024-03-06 20:17:19 +08:00
|
|
|
latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos)
|
2023-07-03 09:53:05 +08:00
|
|
|
if err != nil {
|
2024-03-06 20:17:19 +08:00
|
|
|
log.Error("FindReposLastestCommitStatuses: %v", err)
|
2024-03-18 19:05:17 +08:00
|
|
|
ctx.JSON(http.StatusInternalServerError, nil)
|
2023-05-14 00:59:01 +03:00
|
|
|
return
|
|
|
|
|
}
|
2024-07-28 23:11:40 +08:00
|
|
|
if !ctx.Repo.CanRead(unit.TypeActions) {
|
|
|
|
|
git_model.CommitStatusesHideActionsURL(ctx, latestCommitStatuses)
|
|
|
|
|
}
|
2023-05-14 00:59:01 +03:00
|
|
|
|
|
|
|
|
results := make([]*repo_service.WebSearchRepository, len(repos))
|
2022-04-08 02:59:56 +08:00
|
|
|
for i, repo := range repos {
|
2023-05-14 00:59:01 +03:00
|
|
|
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,
|
2024-07-10 19:37:16 +08:00
|
|
|
HTMLURL: repo.HTMLURL(ctx),
|
2023-05-14 00:59:01 +03:00
|
|
|
Link: repo.Link(),
|
|
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
|
},
|
2024-03-06 20:17:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if latestCommitStatuses[i] != nil {
|
|
|
|
|
results[i].LatestCommitStatus = latestCommitStatuses[i]
|
|
|
|
|
results[i].LocaleLatestCommitStatus = latestCommitStatuses[i].LocaleString(ctx.Locale)
|
2022-04-08 02:59:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 00:59:01 +03:00
|
|
|
ctx.JSON(http.StatusOK, repo_service.WebSearchResults{
|
2022-04-08 02:59:56 +08:00
|
|
|
OK: true,
|
|
|
|
|
Data: results,
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-07-21 19:20:04 +08:00
|
|
|
|
|
|
|
|
type branchTagSearchResponse struct {
|
|
|
|
|
Results []string `json:"results"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetBranchesList get branches for current repo'
|
|
|
|
|
func GetBranchesList(ctx *context.Context) {
|
|
|
|
|
branchOpts := git_model.FindBranchOptions{
|
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2024-02-23 03:18:33 +01:00
|
|
|
IsDeletedBranch: optional.Some(false),
|
2024-03-22 20:53:52 +08:00
|
|
|
ListOptions: db.ListOptionsAll,
|
2023-07-21 19:20:04 +08:00
|
|
|
}
|
|
|
|
|
branches, err := git_model.FindBranchNames(ctx, branchOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp := &branchTagSearchResponse{}
|
|
|
|
|
// always put default branch on the top if it exists
|
2023-09-07 17:37:47 +08:00
|
|
|
if slices.Contains(branches, ctx.Repo.Repository.DefaultBranch) {
|
2023-07-21 19:20:04 +08:00
|
|
|
branches = util.SliceRemoveAll(branches, ctx.Repo.Repository.DefaultBranch)
|
|
|
|
|
branches = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...)
|
|
|
|
|
}
|
|
|
|
|
resp.Results = branches
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetTagList get tag list for current repo
|
|
|
|
|
func GetTagList(ctx *context.Context) {
|
|
|
|
|
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp := &branchTagSearchResponse{}
|
|
|
|
|
resp.Results = tags
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PrepareBranchList(ctx *context.Context) {
|
|
|
|
|
branchOpts := git_model.FindBranchOptions{
|
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2024-02-23 03:18:33 +01:00
|
|
|
IsDeletedBranch: optional.Some(false),
|
2024-03-22 20:53:52 +08:00
|
|
|
ListOptions: db.ListOptionsAll,
|
2023-07-21 19:20:04 +08:00
|
|
|
}
|
|
|
|
|
brs, err := git_model.FindBranchNames(ctx, branchOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetBranches", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// always put default branch on the top if it exists
|
2023-09-07 17:37:47 +08:00
|
|
|
if slices.Contains(brs, ctx.Repo.Repository.DefaultBranch) {
|
2023-07-21 19:20:04 +08:00
|
|
|
brs = util.SliceRemoveAll(brs, ctx.Repo.Repository.DefaultBranch)
|
|
|
|
|
brs = append([]string{ctx.Repo.Repository.DefaultBranch}, brs...)
|
|
|
|
|
}
|
|
|
|
|
ctx.Data["Branches"] = brs
|
|
|
|
|
}
|