aboutsummaryrefslogtreecommitdiffstats
path: root/modules/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 /modules/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 'modules/repository')
-rw-r--r--modules/repository/collaborator.go43
-rw-r--r--modules/repository/collaborator_test.go281
-rw-r--r--modules/repository/create.go153
-rw-r--r--modules/repository/create_test.go7
-rw-r--r--modules/repository/generate.go3
-rw-r--r--modules/repository/init.go5
-rw-r--r--modules/repository/repo.go19
7 files changed, 490 insertions, 21 deletions
diff --git a/modules/repository/collaborator.go b/modules/repository/collaborator.go
new file mode 100644
index 0000000000..9d20a25890
--- /dev/null
+++ b/modules/repository/collaborator.go
@@ -0,0 +1,43 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repository
+
+import (
+ "context"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/perm"
+ access_model "code.gitea.io/gitea/models/perm/access"
+ repo_model "code.gitea.io/gitea/models/repo"
+ user_model "code.gitea.io/gitea/models/user"
+)
+
+func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User) error {
+ collaboration := &repo_model.Collaboration{
+ RepoID: repo.ID,
+ UserID: u.ID,
+ }
+
+ has, err := db.GetByBean(ctx, collaboration)
+ if err != nil {
+ return err
+ } else if has {
+ return nil
+ }
+ collaboration.Mode = perm.AccessModeWrite
+
+ if err = db.Insert(ctx, collaboration); err != nil {
+ return err
+ }
+
+ return access_model.RecalculateUserAccess(ctx, repo, u.ID)
+}
+
+// AddCollaborator adds new collaboration to a repository with default access mode.
+func AddCollaborator(repo *repo_model.Repository, u *user_model.User) error {
+ return db.WithTx(func(ctx context.Context) error {
+ return addCollaborator(ctx, repo, u)
+ })
+}
diff --git a/modules/repository/collaborator_test.go b/modules/repository/collaborator_test.go
new file mode 100644
index 0000000000..1b927aa3b6
--- /dev/null
+++ b/modules/repository/collaborator_test.go
@@ -0,0 +1,281 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repository
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/organization"
+ perm_model "code.gitea.io/gitea/models/perm"
+ access_model "code.gitea.io/gitea/models/perm/access"
+ repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unit"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestRepository_AddCollaborator(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ testSuccess := func(repoID, userID int64) {
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
+ assert.NoError(t, repo.GetOwner(db.DefaultContext))
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
+ assert.NoError(t, AddCollaborator(repo, user))
+ unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID}, &user_model.User{ID: userID})
+ }
+ testSuccess(1, 4)
+ testSuccess(1, 4)
+ testSuccess(3, 4)
+}
+
+func TestRepoPermissionPublicNonOrgRepo(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ // public non-organization repo
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
+ assert.NoError(t, repo.LoadUnits(db.DefaultContext))
+
+ // plain user
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+ perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // change to collaborator
+ assert.NoError(t, AddCollaborator(repo, user))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // collaborator
+ collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, collaborator)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // owner
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // admin
+ admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+}
+
+func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ // private non-organization repo
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
+ assert.NoError(t, repo.LoadUnits(db.DefaultContext))
+
+ // plain user
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
+ perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.False(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // change to collaborator to default write access
+ assert.NoError(t, AddCollaborator(repo, user))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // owner
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // admin
+ admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+}
+
+func TestRepoPermissionPublicOrgRepo(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ // public organization repo
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 32})
+ assert.NoError(t, repo.LoadUnits(db.DefaultContext))
+
+ // plain user
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
+ perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // change to collaborator to default write access
+ assert.NoError(t, AddCollaborator(repo, user))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // org member team owner
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // org member team tester
+ member := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, member)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ }
+ assert.True(t, perm.CanWrite(unit.TypeIssues))
+ assert.False(t, perm.CanWrite(unit.TypeCode))
+
+ // admin
+ admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+}
+
+func TestRepoPermissionPrivateOrgRepo(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ // private organization repo
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24})
+ assert.NoError(t, repo.LoadUnits(db.DefaultContext))
+
+ // plain user
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
+ perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.False(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // change to collaborator to default write access
+ assert.NoError(t, AddCollaborator(repo, user))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.False(t, perm.CanWrite(unit.Type))
+ }
+
+ // org member team owner
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // update team information and then check permission
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5})
+ err = organization.UpdateTeamUnits(team, nil)
+ assert.NoError(t, err)
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+
+ // org member team tester
+ tester := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, tester)
+ assert.NoError(t, err)
+ assert.True(t, perm.CanWrite(unit.TypeIssues))
+ assert.False(t, perm.CanWrite(unit.TypeCode))
+ assert.False(t, perm.CanRead(unit.TypeCode))
+
+ // org member team reviewer
+ reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, reviewer)
+ assert.NoError(t, err)
+ assert.False(t, perm.CanRead(unit.TypeIssues))
+ assert.False(t, perm.CanWrite(unit.TypeCode))
+ assert.True(t, perm.CanRead(unit.TypeCode))
+
+ // admin
+ admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
+ assert.NoError(t, err)
+ for _, unit := range repo.Units {
+ assert.True(t, perm.CanRead(unit.Type))
+ assert.True(t, perm.CanWrite(unit.Type))
+ }
+}
diff --git a/modules/repository/create.go b/modules/repository/create.go
index 9204d7e422..7a25323def 100644
--- a/modules/repository/create.go
+++ b/modules/repository/create.go
@@ -13,11 +13,16 @@ import (
"unicode/utf8"
"code.gitea.io/gitea/models"
+ activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
+ "code.gitea.io/gitea/models/organization"
+ "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -25,8 +30,150 @@ import (
"code.gitea.io/gitea/modules/util"
)
+// CreateRepositoryByExample creates a repository for the user/organization.
+func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository, overwriteOrAdopt bool) (err error) {
+ if err = repo_model.IsUsableRepoName(repo.Name); err != nil {
+ return err
+ }
+
+ has, err := repo_model.IsRepositoryExist(ctx, u, repo.Name)
+ if err != nil {
+ return fmt.Errorf("IsRepositoryExist: %v", err)
+ } else if has {
+ return repo_model.ErrRepoAlreadyExist{
+ Uname: u.Name,
+ Name: repo.Name,
+ }
+ }
+
+ repoPath := repo_model.RepoPath(u.Name, repo.Name)
+ isExist, err := util.IsExist(repoPath)
+ if err != nil {
+ log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
+ return err
+ }
+ if !overwriteOrAdopt && isExist {
+ log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
+ return repo_model.ErrRepoFilesAlreadyExist{
+ Uname: u.Name,
+ Name: repo.Name,
+ }
+ }
+
+ if err = db.Insert(ctx, repo); err != nil {
+ return err
+ }
+ if err = repo_model.DeleteRedirect(ctx, u.ID, repo.Name); err != nil {
+ return err
+ }
+
+ // insert units for repo
+ units := make([]repo_model.RepoUnit, 0, len(unit.DefaultRepoUnits))
+ for _, tp := range unit.DefaultRepoUnits {
+ if tp == unit.TypeIssues {
+ units = append(units, repo_model.RepoUnit{
+ RepoID: repo.ID,
+ Type: tp,
+ Config: &repo_model.IssuesConfig{
+ EnableTimetracker: setting.Service.DefaultEnableTimetracking,
+ AllowOnlyContributorsToTrackTime: setting.Service.DefaultAllowOnlyContributorsToTrackTime,
+ EnableDependencies: setting.Service.DefaultEnableDependencies,
+ },
+ })
+ } else if tp == unit.TypePullRequests {
+ units = append(units, repo_model.RepoUnit{
+ RepoID: repo.ID,
+ Type: tp,
+ Config: &repo_model.PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: repo_model.MergeStyle(setting.Repository.PullRequest.DefaultMergeStyle), AllowRebaseUpdate: true},
+ })
+ } else {
+ units = append(units, repo_model.RepoUnit{
+ RepoID: repo.ID,
+ Type: tp,
+ })
+ }
+ }
+
+ if err = db.Insert(ctx, units); err != nil {
+ return err
+ }
+
+ // Remember visibility preference.
+ u.LastRepoVisibility = repo.IsPrivate
+ if err = user_model.UpdateUserCols(ctx, u, "last_repo_visibility"); err != nil {
+ return fmt.Errorf("UpdateUserCols: %v", err)
+ }
+
+ if err = user_model.IncrUserRepoNum(ctx, u.ID); err != nil {
+ return fmt.Errorf("IncrUserRepoNum: %v", err)
+ }
+ u.NumRepos++
+
+ // Give access to all members in teams with access to all repositories.
+ if u.IsOrganization() {
+ teams, err := organization.FindOrgTeams(ctx, u.ID)
+ if err != nil {
+ return fmt.Errorf("FindOrgTeams: %v", err)
+ }
+ for _, t := range teams {
+ if t.IncludesAllRepositories {
+ if err := models.AddRepository(ctx, t, repo); err != nil {
+ return fmt.Errorf("AddRepository: %v", err)
+ }
+ }
+ }
+
+ if isAdmin, err := access_model.IsUserRepoAdmin(ctx, repo, doer); err != nil {
+ return fmt.Errorf("IsUserRepoAdmin: %v", err)
+ } else if !isAdmin {
+ // Make creator repo admin if it wasn't assigned automatically
+ if err = addCollaborator(ctx, repo, doer); err != nil {
+ return fmt.Errorf("addCollaborator: %v", err)
+ }
+ if err = repo_model.ChangeCollaborationAccessModeCtx(ctx, repo, doer.ID, perm.AccessModeAdmin); err != nil {
+ return fmt.Errorf("ChangeCollaborationAccessModeCtx: %v", err)
+ }
+ }
+ } else if err = access_model.RecalculateAccesses(ctx, repo); err != nil {
+ // Organization automatically called this in AddRepository method.
+ return fmt.Errorf("RecalculateAccesses: %v", err)
+ }
+
+ if setting.Service.AutoWatchNewRepos {
+ if err = repo_model.WatchRepo(ctx, doer.ID, repo.ID, true); err != nil {
+ return fmt.Errorf("WatchRepo: %v", err)
+ }
+ }
+
+ if err = webhook.CopyDefaultWebhooksToRepo(ctx, repo.ID); err != nil {
+ return fmt.Errorf("CopyDefaultWebhooksToRepo: %v", err)
+ }
+
+ return nil
+}
+
+// CreateRepoOptions contains the create repository options
+type CreateRepoOptions struct {
+ Name string
+ Description string
+ OriginalURL string
+ GitServiceType api.GitServiceType
+ Gitignores string
+ IssueLabels string
+ License string
+ Readme string
+ DefaultBranch string
+ IsPrivate bool
+ IsMirror bool
+ IsTemplate bool
+ AutoInit bool
+ Status repo_model.RepositoryStatus
+ TrustModel repo_model.TrustModelType
+ MirrorInterval string
+}
+
// CreateRepository creates a repository for the user/organization.
-func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
+func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_model.Repository, error) {
if !doer.IsAdmin && !u.CanCreateRepo() {
return nil, repo_model.ErrReachLimitOfRepo{
Limit: u.MaxRepoCreation,
@@ -66,7 +213,7 @@ func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (
var rollbackRepo *repo_model.Repository
if err := db.WithTx(func(ctx context.Context) error {
- if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil {
+ if err := CreateRepositoryByExample(ctx, doer, u, repo, false); err != nil {
return err
}
@@ -220,7 +367,7 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili
// If repo has become private, we need to set its actions to private.
if repo.IsPrivate {
- _, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&models.Action{
+ _, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&activities_model.Action{
IsPrivate: true,
})
if err != nil {
diff --git a/modules/repository/create_test.go b/modules/repository/create_test.go
index 39f8b11356..3040782845 100644
--- a/modules/repository/create_test.go
+++ b/modules/repository/create_test.go
@@ -9,6 +9,7 @@ import (
"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"
"code.gitea.io/gitea/models/perm"
@@ -56,7 +57,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
// Create repos.
repoIds := make([]int64, 0)
for i := 0; i < 3; i++ {
- r, err := CreateRepository(user, org.AsUser(), models.CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
+ r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
assert.NoError(t, err, "CreateRepository %d", i)
if r != nil {
repoIds = append(repoIds, r.ID)
@@ -118,7 +119,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
}
// Create repo and check teams repositories.
- r, err := CreateRepository(user, org.AsUser(), models.CreateRepoOptions{Name: "repo-last"})
+ r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: "repo-last"})
assert.NoError(t, err, "CreateRepository last")
if r != nil {
repoIds = append(repoIds, r.ID)
@@ -162,7 +163,7 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
assert.NoError(t, err)
// Check visibility of action has become private
- act := models.Action{}
+ act := activities_model.Action{}
_, err = db.GetEngine(db.DefaultContext).ID(3).Get(&act)
assert.NoError(t, err)
diff --git a/modules/repository/generate.go b/modules/repository/generate.go
index 8f7b4c885d..4d76d33993 100644
--- a/modules/repository/generate.go
+++ b/modules/repository/generate.go
@@ -15,7 +15,6 @@ import (
"strings"
"time"
- "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"
@@ -321,7 +320,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
TrustModel: templateRepo.TrustModel,
}
- if err = models.CreateRepository(ctx, doer, owner, generateRepo, false); err != nil {
+ if err = CreateRepositoryByExample(ctx, doer, owner, generateRepo, false); err != nil {
return nil, err
}
diff --git a/modules/repository/init.go b/modules/repository/init.go
index e984697cda..37ed0748b4 100644
--- a/modules/repository/init.go
+++ b/modules/repository/init.go
@@ -15,7 +15,6 @@ import (
"strings"
"time"
- "code.gitea.io/gitea/models"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@@ -214,7 +213,7 @@ func LoadRepoConfig() {
Licenses = sortedLicenses
}
-func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts models.CreateRepoOptions) error {
+func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts CreateRepoOptions) error {
commitTimeStr := time.Now().Format(time.RFC3339)
authorSig := repo.Owner.NewGitSig()
@@ -387,7 +386,7 @@ func checkInitRepository(ctx context.Context, owner, name string) (err error) {
}
// InitRepository initializes README and .gitignore if needed.
-func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts models.CreateRepoOptions) (err error) {
+func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
if err = checkInitRepository(ctx, repo.OwnerName, repo.Name); err != nil {
return err
}
diff --git a/modules/repository/repo.go b/modules/repository/repo.go
index 436045146a..48c3edf60f 100644
--- a/modules/repository/repo.go
+++ b/modules/repository/repo.go
@@ -13,7 +13,6 @@ import (
"strings"
"time"
- "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/organization"
@@ -31,8 +30,8 @@ import (
)
/*
- GitHub, GitLab, Gogs: *.wiki.git
- BitBucket: *.git/wiki
+GitHub, GitLab, Gogs: *.wiki.git
+BitBucket: *.git/wiki
*/
var commonWikiURLSuffixes = []string{".wiki.git", ".git/wiki"}
@@ -277,14 +276,14 @@ func SyncReleasesWithTags(repo *repo_model.Repository, gitRepo *git.Repository)
}
existingRelTags := make(map[string]struct{})
- opts := models.FindReleasesOptions{
+ opts := repo_model.FindReleasesOptions{
IncludeDrafts: true,
IncludeTags: true,
ListOptions: db.ListOptions{PageSize: 50},
}
for page := 1; ; page++ {
opts.Page = page
- rels, err := models.GetReleasesByRepoID(repo.ID, opts)
+ rels, err := repo_model.GetReleasesByRepoID(repo.ID, opts)
if err != nil {
return fmt.Errorf("unable to GetReleasesByRepoID in Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
}
@@ -300,7 +299,7 @@ func SyncReleasesWithTags(repo *repo_model.Repository, gitRepo *git.Repository)
return fmt.Errorf("unable to GetTagCommitID for %q in Repo[%d:%s/%s]: %w", rel.TagName, repo.ID, repo.OwnerName, repo.Name, err)
}
if git.IsErrNotExist(err) || commitID != rel.Sha1 {
- if err := models.PushUpdateDeleteTag(repo, rel.TagName); err != nil {
+ if err := repo_model.PushUpdateDeleteTag(repo, rel.TagName); err != nil {
return fmt.Errorf("unable to PushUpdateDeleteTag: %q in Repo[%d:%s/%s]: %w", rel.TagName, repo.ID, repo.OwnerName, repo.Name, err)
}
} else {
@@ -359,7 +358,7 @@ func PushUpdateAddTag(repo *repo_model.Repository, gitRepo *git.Repository, tagN
return fmt.Errorf("unable to get CommitsCount: %w", err)
}
- rel := models.Release{
+ rel := repo_model.Release{
RepoID: repo.ID,
TagName: tagName,
LowerTagName: strings.ToLower(tagName),
@@ -372,7 +371,7 @@ func PushUpdateAddTag(repo *repo_model.Repository, gitRepo *git.Repository, tagN
rel.PublisherID = author.ID
}
- return models.SaveOrUpdateTag(repo, &rel)
+ return repo_model.SaveOrUpdateTag(repo, &rel)
}
// StoreMissingLfsObjectsInRepository downloads missing LFS objects
@@ -489,14 +488,14 @@ func pullMirrorReleaseSync(repo *repo_model.Repository, gitRepo *git.Repository)
//
// clear out existing releases
//
- if _, err := db.DeleteByBean(ctx, &models.Release{RepoID: repo.ID}); err != nil {
+ if _, err := db.DeleteByBean(ctx, &repo_model.Release{RepoID: repo.ID}); err != nil {
return fmt.Errorf("unable to clear releases for pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
}
//
// make release set identical to upstream tags
//
for _, tag := range tags {
- release := models.Release{
+ release := repo_model.Release{
RepoID: repo.ID,
TagName: tag.Name,
LowerTagName: strings.ToLower(tag.Name),