summaryrefslogtreecommitdiffstats
path: root/services/repository
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-08-25 10:31:57 +0800
committerGitHub <noreply@github.com>2022-08-25 10:31:57 +0800
commit1d8543e7db58d7c4973758e47f005c4d8bd7d7a3 (patch)
treeb60c99e2dfd69ccb998f8a0829d98d7cadcf0d6b /services/repository
parent4a4bfafa238bf48851f8c11fa3701bd42b912475 (diff)
downloadgitea-1d8543e7db58d7c4973758e47f005c4d8bd7d7a3.tar.gz
gitea-1d8543e7db58d7c4973758e47f005c4d8bd7d7a3.zip
Move some files into models' sub packages (#20262)
* Move some files into models' sub packages * Move functions * merge main branch * Fix check * fix check * Fix some tests * Fix lint * Fix lint * Revert lint changes * Fix error comments * Fix lint Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'services/repository')
-rw-r--r--services/repository/adopt.go7
-rw-r--r--services/repository/files/upload.go7
-rw-r--r--services/repository/fork.go22
-rw-r--r--services/repository/fork_test.go3
-rw-r--r--services/repository/push.go13
-rw-r--r--services/repository/repository.go6
-rw-r--r--services/repository/transfer.go5
-rw-r--r--services/repository/transfer_test.go6
8 files changed, 41 insertions, 28 deletions
diff --git a/services/repository/adopt.go b/services/repository/adopt.go
index 6d6611c705..74876d8e76 100644
--- a/services/repository/adopt.go
+++ b/services/repository/adopt.go
@@ -11,7 +11,6 @@ import (
"path/filepath"
"strings"
- "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@@ -26,7 +25,7 @@ import (
)
// AdoptRepository adopts pre-existing repository files for the user/organization.
-func AdoptRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
+func AdoptRepository(doer, u *user_model.User, opts repo_module.CreateRepoOptions) (*repo_model.Repository, error) {
if !doer.IsAdmin && !u.CanCreateRepo() {
return nil, repo_model.ErrReachLimitOfRepo{
Limit: u.MaxRepoCreation,
@@ -67,7 +66,7 @@ func AdoptRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*
}
}
- if err := models.CreateRepository(ctx, doer, u, repo, true); err != nil {
+ if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true); err != nil {
return err
}
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {
@@ -100,7 +99,7 @@ func AdoptRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*
return repo, nil
}
-func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts models.CreateRepoOptions) (err error) {
+func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts repo_module.CreateRepoOptions) (err error) {
isExist, err := util.IsExist(repoPath)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go
index ffc1f4efe9..327a2e121c 100644
--- a/services/repository/files/upload.go
+++ b/services/repository/files/upload.go
@@ -11,7 +11,6 @@ import (
"path"
"strings"
- "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@@ -32,7 +31,7 @@ type UploadRepoFileOptions struct {
}
type uploadInfo struct {
- upload *models.Upload
+ upload *repo_model.Upload
lfsMetaObject *git_model.LFSMetaObject
}
@@ -56,7 +55,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
return nil
}
- uploads, err := models.GetUploadsByUUIDs(opts.Files)
+ uploads, err := repo_model.GetUploadsByUUIDs(opts.Files)
if err != nil {
return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
}
@@ -157,7 +156,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
return err
}
- return models.DeleteUploads(uploads...)
+ return repo_model.DeleteUploads(uploads...)
}
func copyUploadedLFSFileIntoRepository(info *uploadInfo, filename2attribute2info map[string]map[string]string, t *TemporaryUploadRepository, treePath string) error {
diff --git a/services/repository/fork.go b/services/repository/fork.go
index b274585ed5..96c391e715 100644
--- a/services/repository/fork.go
+++ b/services/repository/fork.go
@@ -10,7 +10,6 @@ import (
"strings"
"time"
- "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
@@ -23,6 +22,23 @@ import (
"code.gitea.io/gitea/modules/util"
)
+// ErrForkAlreadyExist represents a "ForkAlreadyExist" kind of error.
+type ErrForkAlreadyExist struct {
+ Uname string
+ RepoName string
+ ForkName string
+}
+
+// IsErrForkAlreadyExist checks if an error is an ErrForkAlreadyExist.
+func IsErrForkAlreadyExist(err error) bool {
+ _, ok := err.(ErrForkAlreadyExist)
+ return ok
+}
+
+func (err ErrForkAlreadyExist) Error() string {
+ return fmt.Sprintf("repository is already forked by user [uname: %s, repo path: %s, fork path: %s]", err.Uname, err.RepoName, err.ForkName)
+}
+
// ForkRepoOptions contains the fork repository options
type ForkRepoOptions struct {
BaseRepo *repo_model.Repository
@@ -37,7 +53,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
return nil, err
}
if forkedRepo != nil {
- return nil, models.ErrForkAlreadyExist{
+ return nil, ErrForkAlreadyExist{
Uname: owner.Name,
RepoName: opts.BaseRepo.FullName(),
ForkName: forkedRepo.FullName(),
@@ -93,7 +109,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
}()
err = db.WithTx(func(txCtx context.Context) error {
- if err = models.CreateRepository(txCtx, doer, owner, repo, false); err != nil {
+ if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil {
return err
}
diff --git a/services/repository/fork_test.go b/services/repository/fork_test.go
index 376c5c06d9..d4ba507351 100644
--- a/services/repository/fork_test.go
+++ b/services/repository/fork_test.go
@@ -7,7 +7,6 @@ package repository
import (
"testing"
- "code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -30,5 +29,5 @@ func TestForkRepository(t *testing.T) {
})
assert.Nil(t, fork)
assert.Error(t, err)
- assert.True(t, models.IsErrForkAlreadyExist(err))
+ assert.True(t, IsErrForkAlreadyExist(err))
}
diff --git a/services/repository/push.go b/services/repository/push.go
index 65ac7b660c..f3f505aa00 100644
--- a/services/repository/push.go
+++ b/services/repository/push.go
@@ -11,7 +11,6 @@ import (
"strings"
"time"
- "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
@@ -292,7 +291,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
// PushUpdateAddDeleteTags updates a number of added and delete tags
func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
return db.WithTx(func(ctx context.Context) error {
- if err := models.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
+ if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
return err
}
return pushUpdateAddTags(ctx, repo, gitRepo, addTags)
@@ -310,16 +309,16 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
lowerTags = append(lowerTags, strings.ToLower(tag))
}
- releases, err := models.GetReleasesByRepoIDAndNames(ctx, repo.ID, lowerTags)
+ releases, err := repo_model.GetReleasesByRepoIDAndNames(ctx, repo.ID, lowerTags)
if err != nil {
return fmt.Errorf("GetReleasesByRepoIDAndNames: %v", err)
}
- relMap := make(map[string]*models.Release)
+ relMap := make(map[string]*repo_model.Release)
for _, rel := range releases {
relMap[rel.LowerTagName] = rel
}
- newReleases := make([]*models.Release, 0, len(lowerTags)-len(relMap))
+ newReleases := make([]*repo_model.Release, 0, len(lowerTags)-len(relMap))
emailToUser := make(map[string]*user_model.User)
@@ -366,7 +365,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
rel, has := relMap[lowerTag]
if !has {
- rel = &models.Release{
+ rel = &repo_model.Release{
RepoID: repo.ID,
Title: "",
TagName: tags[i],
@@ -393,7 +392,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
if rel.IsTag && author != nil {
rel.PublisherID = author.ID
}
- if err = models.UpdateRelease(ctx, rel); err != nil {
+ if err = repo_model.UpdateRelease(ctx, rel); err != nil {
return fmt.Errorf("Update: %v", err)
}
}
diff --git a/services/repository/repository.go b/services/repository/repository.go
index 4bde6879a6..d530358360 100644
--- a/services/repository/repository.go
+++ b/services/repository/repository.go
@@ -25,7 +25,7 @@ import (
)
// CreateRepository creates a repository for the user/organization.
-func CreateRepository(doer, owner *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
+func CreateRepository(doer, owner *user_model.User, opts repo_module.CreateRepoOptions) (*repo_model.Repository, error) {
repo, err := repo_module.CreateRepository(doer, owner, opts)
if err != nil {
// No need to rollback here we should do this in CreateRepository...
@@ -69,7 +69,7 @@ func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_mo
}
}
- repo, err := CreateRepository(authUser, owner, models.CreateRepoOptions{
+ repo, err := CreateRepository(authUser, owner, repo_module.CreateRepoOptions{
Name: repoName,
IsPrivate: setting.Repository.DefaultPushCreatePrivate,
})
@@ -117,7 +117,7 @@ func LinkedRepository(a *repo_model.Attachment) (*repo_model.Repository, unit.Ty
}
return repo, unitType, err
} else if a.ReleaseID != 0 {
- rel, err := models.GetReleaseByID(db.DefaultContext, a.ReleaseID)
+ rel, err := repo_model.GetReleaseByID(db.DefaultContext, a.ReleaseID)
if err != nil {
return nil, unit.TypeReleases, err
}
diff --git a/services/repository/transfer.go b/services/repository/transfer.go
index ae15383240..a0f4a7685c 100644
--- a/services/repository/transfer.go
+++ b/services/repository/transfer.go
@@ -16,6 +16,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
+ repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/sync"
)
@@ -49,7 +50,7 @@ func TransferOwnership(doer, newOwner *user_model.User, repo *repo_model.Reposit
}
for _, team := range teams {
- if err := models.AddRepository(team, newRepo); err != nil {
+ if err := models.AddRepository(db.DefaultContext, team, newRepo); err != nil {
return err
}
}
@@ -111,7 +112,7 @@ func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.R
return err
}
if !hasAccess {
- if err := models.AddCollaborator(repo, newOwner); err != nil {
+ if err := repo_module.AddCollaborator(repo, newOwner); err != nil {
return err
}
if err := repo_model.ChangeCollaborationAccessMode(repo, newOwner.ID, perm.AccessModeRead); err != nil {
diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go
index 3c929f2f7b..bf2a0ce0a2 100644
--- a/services/repository/transfer_test.go
+++ b/services/repository/transfer_test.go
@@ -8,7 +8,7 @@ import (
"sync"
"testing"
- "code.gitea.io/gitea/models"
+ activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
access_model "code.gitea.io/gitea/models/perm/access"
@@ -49,8 +49,8 @@ func TestTransferOwnership(t *testing.T) {
exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
assert.NoError(t, err)
assert.True(t, exist)
- unittest.AssertExistsAndLoadBean(t, &models.Action{
- OpType: models.ActionTransferRepo,
+ unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
+ OpType: activities_model.ActionTransferRepo,
ActUserID: 2,
RepoID: 3,
Content: "user3/repo3",