mirror of
https://github.com/go-gitea/gitea
synced 2025-07-23 02:38:35 +00:00
Some repository refactors (#17950)
* some repository refactors * remove unnecessary code * Fix test * Remove unnecessary banner
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
@@ -50,7 +51,7 @@ func ListForks(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
forks, err := models.GetForks(ctx.Repo.Repository, utils.GetListOptions(ctx))
|
||||
forks, err := repo_model.GetForks(ctx.Repo.Repository, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetForks", err)
|
||||
return
|
||||
@@ -125,7 +126,7 @@ func CreateFork(ctx *context.APIContext) {
|
||||
forker = org.AsUser()
|
||||
}
|
||||
|
||||
fork, err := repo_service.ForkRepository(ctx.User, forker, models.ForkRepoOptions{
|
||||
fork, err := repo_service.ForkRepository(ctx.User, forker, repo_service.ForkRepoOptions{
|
||||
BaseRepo: repo,
|
||||
Name: repo.Name,
|
||||
Description: repo.Description,
|
||||
|
@@ -214,15 +214,15 @@ func Migrate(ctx *context.APIContext) {
|
||||
|
||||
func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, remoteAddr string, err error) {
|
||||
switch {
|
||||
case models.IsErrRepoAlreadyExist(err):
|
||||
case repo_model.IsErrRepoAlreadyExist(err):
|
||||
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
|
||||
case models.IsErrRepoFilesAlreadyExist(err):
|
||||
case repo_model.IsErrRepoFilesAlreadyExist(err):
|
||||
ctx.Error(http.StatusConflict, "", "Files already exist for this repository. Adopt them or delete them.")
|
||||
case migrations.IsRateLimitError(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit addressed rate limitation.")
|
||||
case migrations.IsTwoFactorAuthError(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit required two factors authentication.")
|
||||
case models.IsErrReachLimitOfRepo(err):
|
||||
case repo_model.IsErrReachLimitOfRepo(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
|
||||
case db.IsErrNameReserved(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' is reserved.", err.(db.ErrNameReserved).Name))
|
||||
|
@@ -956,7 +956,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
|
||||
}
|
||||
|
||||
// Check if current user has fork of repository or in the same repository.
|
||||
headRepo := models.GetForkedRepo(headUser.ID, baseRepo.ID)
|
||||
headRepo := repo_model.GetForkedRepo(headUser.ID, baseRepo.ID)
|
||||
if headRepo == nil && !isSameRepo {
|
||||
log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
|
||||
ctx.NotFound("GetForkedRepo")
|
||||
|
@@ -261,7 +261,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
|
||||
IsTemplate: opt.Template,
|
||||
})
|
||||
if err != nil {
|
||||
if models.IsErrRepoAlreadyExist(err) {
|
||||
if repo_model.IsErrRepoAlreadyExist(err) {
|
||||
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
|
||||
} else if db.IsErrNameReserved(err) ||
|
||||
db.IsErrNamePatternNotAllowed(err) {
|
||||
@@ -410,7 +410,7 @@ func Generate(ctx *context.APIContext) {
|
||||
|
||||
repo, err := repo_service.GenerateRepository(ctx.User, ctxUser, ctx.Repo.Repository, opts)
|
||||
if err != nil {
|
||||
if models.IsErrRepoAlreadyExist(err) {
|
||||
if repo_model.IsErrRepoAlreadyExist(err) {
|
||||
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
|
||||
} else if db.IsErrNameReserved(err) ||
|
||||
db.IsErrNamePatternNotAllowed(err) {
|
||||
@@ -650,7 +650,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
|
||||
if repo.LowerName != strings.ToLower(newRepoName) {
|
||||
if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil {
|
||||
switch {
|
||||
case models.IsErrRepoAlreadyExist(err):
|
||||
case repo_model.IsErrRepoAlreadyExist(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err)
|
||||
case db.IsErrNameReserved(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err)
|
||||
@@ -911,7 +911,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
|
||||
if err := repo_model.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
|
||||
return err
|
||||
}
|
||||
@@ -931,14 +931,14 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
|
||||
return err
|
||||
}
|
||||
if *opts.Archived {
|
||||
if err := models.SetArchiveRepoState(repo, *opts.Archived); err != nil {
|
||||
if err := repo_model.SetArchiveRepoState(repo, *opts.Archived); err != nil {
|
||||
log.Error("Tried to archive a repo: %s", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
|
||||
return err
|
||||
}
|
||||
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
|
||||
} else {
|
||||
if err := models.SetArchiveRepoState(repo, *opts.Archived); err != nil {
|
||||
if err := repo_model.SetArchiveRepoState(repo, *opts.Archived); err != nil {
|
||||
log.Error("Tried to un-archive a repo: %s", err)
|
||||
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
|
||||
return err
|
||||
|
@@ -7,7 +7,7 @@ package repo
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
@@ -44,7 +44,7 @@ func ListStargazers(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
|
||||
stargazers, err := models.GetStargazers(ctx.Repo.Repository, utils.GetListOptions(ctx))
|
||||
stargazers, err := repo_model.GetStargazers(ctx.Repo.Repository, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetStargazers", err)
|
||||
return
|
||||
|
@@ -7,7 +7,7 @@ package repo
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
@@ -44,9 +44,9 @@ func ListSubscribers(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
|
||||
subscribers, err := models.GetRepoWatchers(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
|
||||
subscribers, err := repo_model.GetRepoWatchers(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetWatchers", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetRepoWatchers", err)
|
||||
return
|
||||
}
|
||||
users := make([]*api.User, len(subscribers))
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
@@ -47,12 +47,12 @@ func ListTopics(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/TopicNames"
|
||||
|
||||
opts := &models.FindTopicOptions{
|
||||
opts := &repo_model.FindTopicOptions{
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
}
|
||||
|
||||
topics, total, err := models.FindTopics(opts)
|
||||
topics, total, err := repo_model.FindTopics(opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
@@ -99,7 +99,7 @@ func UpdateTopics(ctx *context.APIContext) {
|
||||
|
||||
form := web.GetForm(ctx).(*api.RepoTopicOptions)
|
||||
topicNames := form.Topics
|
||||
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
|
||||
validTopics, invalidTopics := repo_model.SanitizeAndValidateTopics(topicNames)
|
||||
|
||||
if len(validTopics) > 25 {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
@@ -117,7 +117,7 @@ func UpdateTopics(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
|
||||
err := repo_model.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
|
||||
if err != nil {
|
||||
log.Error("SaveTopics failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
@@ -158,7 +158,7 @@ func AddTopic(ctx *context.APIContext) {
|
||||
|
||||
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
||||
|
||||
if !models.ValidateTopic(topicName) {
|
||||
if !repo_model.ValidateTopic(topicName) {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
"invalidTopics": topicName,
|
||||
"message": "Topic name is invalid",
|
||||
@@ -167,7 +167,7 @@ func AddTopic(ctx *context.APIContext) {
|
||||
}
|
||||
|
||||
// Prevent adding more topics than allowed to repo
|
||||
count, err := models.CountTopics(&models.FindTopicOptions{
|
||||
count, err := repo_model.CountTopics(&repo_model.FindTopicOptions{
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -182,7 +182,7 @@ func AddTopic(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
|
||||
_, err = repo_model.AddTopic(ctx.Repo.Repository.ID, topicName)
|
||||
if err != nil {
|
||||
log.Error("AddTopic failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
@@ -223,7 +223,7 @@ func DeleteTopic(ctx *context.APIContext) {
|
||||
|
||||
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
||||
|
||||
if !models.ValidateTopic(topicName) {
|
||||
if !repo_model.ValidateTopic(topicName) {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
"invalidTopics": topicName,
|
||||
"message": "Topic name is invalid",
|
||||
@@ -231,7 +231,7 @@ func DeleteTopic(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName)
|
||||
topic, err := repo_model.DeleteTopic(ctx.Repo.Repository.ID, topicName)
|
||||
if err != nil {
|
||||
log.Error("DeleteTopic failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
@@ -272,12 +272,12 @@ func TopicSearch(ctx *context.APIContext) {
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
opts := &models.FindTopicOptions{
|
||||
opts := &repo_model.FindTopicOptions{
|
||||
Keyword: ctx.FormString("q"),
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
}
|
||||
|
||||
topics, total, err := models.FindTopics(opts)
|
||||
topics, total, err := repo_model.FindTopics(opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
|
@@ -104,7 +104,7 @@ func Transfer(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
if models.IsErrRepoAlreadyExist(err) {
|
||||
if repo_model.IsErrRepoAlreadyExist(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "CreatePendingRepositoryTransfer", err)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user