diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /modules/doctor | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'modules/doctor')
-rw-r--r-- | modules/doctor/checkOldArchives.go | 4 | ||||
-rw-r--r-- | modules/doctor/fix16961.go | 41 | ||||
-rw-r--r-- | modules/doctor/fix16961_test.go | 32 | ||||
-rw-r--r-- | modules/doctor/mergebase.go | 7 | ||||
-rw-r--r-- | modules/doctor/misc.go | 15 |
5 files changed, 51 insertions, 48 deletions
diff --git a/modules/doctor/checkOldArchives.go b/modules/doctor/checkOldArchives.go index a4e2ffbd1f..0db8794080 100644 --- a/modules/doctor/checkOldArchives.go +++ b/modules/doctor/checkOldArchives.go @@ -8,7 +8,7 @@ import ( "os" "path/filepath" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" ) @@ -16,7 +16,7 @@ import ( func checkOldArchives(logger log.Logger, autofix bool) error { numRepos := 0 numReposUpdated := 0 - err := iterateRepositories(func(repo *models.Repository) error { + err := iterateRepositories(func(repo *repo_model.Repository) error { if repo.IsEmpty { return nil } diff --git a/modules/doctor/fix16961.go b/modules/doctor/fix16961.go index 2e1db834cd..4797c97ef3 100644 --- a/modules/doctor/fix16961.go +++ b/modules/doctor/fix16961.go @@ -8,9 +8,10 @@ import ( "bytes" "fmt" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" @@ -36,8 +37,8 @@ func parseBool16961(bs []byte) (bool, error) { return false, fmt.Errorf("unexpected bool format: %s", string(bs)) } -func fixUnitConfig16961(bs []byte, cfg *models.UnitConfig) (fixed bool, err error) { - err = models.JSONUnmarshalHandleDoubleEncode(bs, &cfg) +func fixUnitConfig16961(bs []byte, cfg *repo_model.UnitConfig) (fixed bool, err error) { + err = json.UnmarshalHandleDoubleEncode(bs, &cfg) if err == nil { return } @@ -50,8 +51,8 @@ func fixUnitConfig16961(bs []byte, cfg *models.UnitConfig) (fixed bool, err erro return true, nil } -func fixExternalWikiConfig16961(bs []byte, cfg *models.ExternalWikiConfig) (fixed bool, err error) { - err = models.JSONUnmarshalHandleDoubleEncode(bs, &cfg) +func fixExternalWikiConfig16961(bs []byte, cfg *repo_model.ExternalWikiConfig) (fixed bool, err error) { + err = json.UnmarshalHandleDoubleEncode(bs, &cfg) if err == nil { return } @@ -66,8 +67,8 @@ func fixExternalWikiConfig16961(bs []byte, cfg *models.ExternalWikiConfig) (fixe return true, nil } -func fixExternalTrackerConfig16961(bs []byte, cfg *models.ExternalTrackerConfig) (fixed bool, err error) { - err = models.JSONUnmarshalHandleDoubleEncode(bs, &cfg) +func fixExternalTrackerConfig16961(bs []byte, cfg *repo_model.ExternalTrackerConfig) (fixed bool, err error) { + err = json.UnmarshalHandleDoubleEncode(bs, &cfg) if err == nil { return } @@ -91,8 +92,8 @@ func fixExternalTrackerConfig16961(bs []byte, cfg *models.ExternalTrackerConfig) return true, nil } -func fixPullRequestsConfig16961(bs []byte, cfg *models.PullRequestsConfig) (fixed bool, err error) { - err = models.JSONUnmarshalHandleDoubleEncode(bs, &cfg) +func fixPullRequestsConfig16961(bs []byte, cfg *repo_model.PullRequestsConfig) (fixed bool, err error) { + err = json.UnmarshalHandleDoubleEncode(bs, &cfg) if err == nil { return } @@ -169,12 +170,12 @@ func fixPullRequestsConfig16961(bs []byte, cfg *models.PullRequestsConfig) (fixe return } - cfg.DefaultMergeStyle = models.MergeStyle(string(bytes.Join(parts[8:], []byte{' '}))) + cfg.DefaultMergeStyle = repo_model.MergeStyle(string(bytes.Join(parts[8:], []byte{' '}))) return true, nil } -func fixIssuesConfig16961(bs []byte, cfg *models.IssuesConfig) (fixed bool, err error) { - err = models.JSONUnmarshalHandleDoubleEncode(bs, &cfg) +func fixIssuesConfig16961(bs []byte, cfg *repo_model.IssuesConfig) (fixed bool, err error) { + err = json.UnmarshalHandleDoubleEncode(bs, &cfg) if err == nil { return } @@ -208,7 +209,7 @@ func fixIssuesConfig16961(bs []byte, cfg *models.IssuesConfig) (fixed bool, err return true, nil } -func fixBrokenRepoUnit16961(repoUnit *models.RepoUnit, bs []byte) (fixed bool, err error) { +func fixBrokenRepoUnit16961(repoUnit *repo_model.RepoUnit, bs []byte) (fixed bool, err error) { // Shortcut empty or null values if len(bs) == 0 { return false, nil @@ -216,33 +217,33 @@ func fixBrokenRepoUnit16961(repoUnit *models.RepoUnit, bs []byte) (fixed bool, e switch unit.Type(repoUnit.Type) { case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects: - cfg := &models.UnitConfig{} + cfg := &repo_model.UnitConfig{} repoUnit.Config = cfg if fixed, err := fixUnitConfig16961(bs, cfg); !fixed { return false, err } case unit.TypeExternalWiki: - cfg := &models.ExternalWikiConfig{} + cfg := &repo_model.ExternalWikiConfig{} repoUnit.Config = cfg if fixed, err := fixExternalWikiConfig16961(bs, cfg); !fixed { return false, err } case unit.TypeExternalTracker: - cfg := &models.ExternalTrackerConfig{} + cfg := &repo_model.ExternalTrackerConfig{} repoUnit.Config = cfg if fixed, err := fixExternalTrackerConfig16961(bs, cfg); !fixed { return false, err } case unit.TypePullRequests: - cfg := &models.PullRequestsConfig{} + cfg := &repo_model.PullRequestsConfig{} repoUnit.Config = cfg if fixed, err := fixPullRequestsConfig16961(bs, cfg); !fixed { return false, err } case unit.TypeIssues: - cfg := &models.IssuesConfig{} + cfg := &repo_model.IssuesConfig{} repoUnit.Config = cfg if fixed, err := fixIssuesConfig16961(bs, cfg); !fixed { return false, err @@ -275,7 +276,7 @@ func fixBrokenRepoUnits16961(logger log.Logger, autofix bool) error { unit := bean.(*RepoUnit) bs := unit.Config - repoUnit := &models.RepoUnit{ + repoUnit := &repo_model.RepoUnit{ ID: unit.ID, RepoID: unit.RepoID, Type: unit.Type, @@ -291,7 +292,7 @@ func fixBrokenRepoUnits16961(logger log.Logger, autofix bool) error { return nil } - return models.UpdateRepoUnit(repoUnit) + return repo_model.UpdateRepoUnit(repoUnit) }, ) diff --git a/modules/doctor/fix16961_test.go b/modules/doctor/fix16961_test.go index 986425b4d8..f5e5667c09 100644 --- a/modules/doctor/fix16961_test.go +++ b/modules/doctor/fix16961_test.go @@ -7,7 +7,7 @@ package doctor import ( "testing" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "github.com/stretchr/testify/assert" ) @@ -46,7 +46,7 @@ func Test_fixUnitConfig_16961(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotFixed, err := fixUnitConfig16961([]byte(tt.bs), &models.UnitConfig{}) + gotFixed, err := fixUnitConfig16961([]byte(tt.bs), &repo_model.UnitConfig{}) if (err != nil) != tt.wantErr { t.Errorf("fixUnitConfig_16961() error = %v, wantErr %v", err, tt.wantErr) return @@ -89,7 +89,7 @@ func Test_fixExternalWikiConfig_16961(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg := &models.ExternalWikiConfig{} + cfg := &repo_model.ExternalWikiConfig{} gotFixed, err := fixExternalWikiConfig16961([]byte(tt.bs), cfg) if (err != nil) != tt.wantErr { t.Errorf("fixExternalWikiConfig_16961() error = %v, wantErr %v", err, tt.wantErr) @@ -109,14 +109,14 @@ func Test_fixExternalTrackerConfig_16961(t *testing.T) { tests := []struct { name string bs string - expected models.ExternalTrackerConfig + expected repo_model.ExternalTrackerConfig wantFixed bool wantErr bool }{ { name: "normal", bs: `{"ExternalTrackerURL":"a","ExternalTrackerFormat":"b","ExternalTrackerStyle":"c"}`, - expected: models.ExternalTrackerConfig{ + expected: repo_model.ExternalTrackerConfig{ ExternalTrackerURL: "a", ExternalTrackerFormat: "b", ExternalTrackerStyle: "c", @@ -127,7 +127,7 @@ func Test_fixExternalTrackerConfig_16961(t *testing.T) { { name: "broken", bs: "&{a b c}", - expected: models.ExternalTrackerConfig{ + expected: repo_model.ExternalTrackerConfig{ ExternalTrackerURL: "a", ExternalTrackerFormat: "b", ExternalTrackerStyle: "c", @@ -150,7 +150,7 @@ func Test_fixExternalTrackerConfig_16961(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg := &models.ExternalTrackerConfig{} + cfg := &repo_model.ExternalTrackerConfig{} gotFixed, err := fixExternalTrackerConfig16961([]byte(tt.bs), cfg) if (err != nil) != tt.wantErr { t.Errorf("fixExternalTrackerConfig_16961() error = %v, wantErr %v", err, tt.wantErr) @@ -176,7 +176,7 @@ func Test_fixPullRequestsConfig_16961(t *testing.T) { tests := []struct { name string bs string - expected models.PullRequestsConfig + expected repo_model.PullRequestsConfig wantFixed bool wantErr bool }{ @@ -187,7 +187,7 @@ func Test_fixPullRequestsConfig_16961(t *testing.T) { { name: "broken - 1.14", bs: `&{%!s(bool=false) %!s(bool=true) %!s(bool=true) %!s(bool=true) %!s(bool=true) %!s(bool=false) %!s(bool=false)}`, - expected: models.PullRequestsConfig{ + expected: repo_model.PullRequestsConfig{ IgnoreWhitespaceConflicts: false, AllowMerge: true, AllowRebase: true, @@ -201,19 +201,19 @@ func Test_fixPullRequestsConfig_16961(t *testing.T) { { name: "broken - 1.15", bs: `&{%!s(bool=false) %!s(bool=true) %!s(bool=true) %!s(bool=true) %!s(bool=true) %!s(bool=false) %!s(bool=false) %!s(bool=false) merge}`, - expected: models.PullRequestsConfig{ + expected: repo_model.PullRequestsConfig{ AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, - DefaultMergeStyle: models.MergeStyleMerge, + DefaultMergeStyle: repo_model.MergeStyleMerge, }, wantFixed: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg := &models.PullRequestsConfig{} + cfg := &repo_model.PullRequestsConfig{} gotFixed, err := fixPullRequestsConfig16961([]byte(tt.bs), cfg) if (err != nil) != tt.wantErr { t.Errorf("fixPullRequestsConfig_16961() error = %v, wantErr %v", err, tt.wantErr) @@ -231,14 +231,14 @@ func Test_fixIssuesConfig_16961(t *testing.T) { tests := []struct { name string bs string - expected models.IssuesConfig + expected repo_model.IssuesConfig wantFixed bool wantErr bool }{ { name: "normal", bs: `{"EnableTimetracker":true,"AllowOnlyContributorsToTrackTime":true,"EnableDependencies":true}`, - expected: models.IssuesConfig{ + expected: repo_model.IssuesConfig{ EnableTimetracker: true, AllowOnlyContributorsToTrackTime: true, EnableDependencies: true, @@ -247,7 +247,7 @@ func Test_fixIssuesConfig_16961(t *testing.T) { { name: "broken", bs: `&{%!s(bool=true) %!s(bool=true) %!s(bool=true)}`, - expected: models.IssuesConfig{ + expected: repo_model.IssuesConfig{ EnableTimetracker: true, AllowOnlyContributorsToTrackTime: true, EnableDependencies: true, @@ -257,7 +257,7 @@ func Test_fixIssuesConfig_16961(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg := &models.IssuesConfig{} + cfg := &repo_model.IssuesConfig{} gotFixed, err := fixIssuesConfig16961([]byte(tt.bs), cfg) if (err != nil) != tt.wantErr { t.Errorf("fixIssuesConfig_16961() error = %v, wantErr %v", err, tt.wantErr) diff --git a/modules/doctor/mergebase.go b/modules/doctor/mergebase.go index c959da8d7f..ef78cc49d1 100644 --- a/modules/doctor/mergebase.go +++ b/modules/doctor/mergebase.go @@ -10,13 +10,14 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "xorm.io/builder" ) -func iteratePRs(repo *models.Repository, each func(*models.Repository, *models.PullRequest) error) error { +func iteratePRs(repo *repo_model.Repository, each func(*repo_model.Repository, *models.PullRequest) error) error { return db.Iterate( db.DefaultContext, new(models.PullRequest), @@ -31,9 +32,9 @@ func checkPRMergeBase(logger log.Logger, autofix bool) error { numRepos := 0 numPRs := 0 numPRsUpdated := 0 - err := iterateRepositories(func(repo *models.Repository) error { + err := iterateRepositories(func(repo *repo_model.Repository) error { numRepos++ - return iteratePRs(repo, func(repo *models.Repository, pr *models.PullRequest) error { + return iteratePRs(repo, func(repo *repo_model.Repository, pr *models.PullRequest) error { numPRs++ pr.BaseRepo = repo repoPath := repo.RepoPath() diff --git a/modules/doctor/misc.go b/modules/doctor/misc.go index 1cf8024b98..a788e5f6a9 100644 --- a/modules/doctor/misc.go +++ b/modules/doctor/misc.go @@ -13,6 +13,7 @@ import ( "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" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -25,13 +26,13 @@ import ( "xorm.io/builder" ) -func iterateRepositories(each func(*models.Repository) error) error { +func iterateRepositories(each func(*repo_model.Repository) error) error { err := db.Iterate( db.DefaultContext, - new(models.Repository), + new(repo_model.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { - return each(bean.(*models.Repository)) + return each(bean.(*repo_model.Repository)) }, ) return err @@ -48,7 +49,7 @@ func checkScriptType(logger log.Logger, autofix bool) error { } func checkHooks(logger log.Logger, autofix bool) error { - if err := iterateRepositories(func(repo *models.Repository) error { + if err := iterateRepositories(func(repo *repo_model.Repository) error { results, err := repository.CheckDelegateHooks(repo.RepoPath()) if err != nil { logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err) @@ -84,7 +85,7 @@ func checkEnablePushOptions(logger log.Logger, autofix bool) error { numRepos := 0 numNeedUpdate := 0 - if err := iterateRepositories(func(repo *models.Repository) error { + if err := iterateRepositories(func(repo *repo_model.Repository) error { numRepos++ r, err := git.OpenRepository(repo.RepoPath()) if err != nil { @@ -131,13 +132,13 @@ func checkDaemonExport(logger log.Logger, autofix bool) error { logger.Critical("Unable to create cache: %v", err) return err } - if err := iterateRepositories(func(repo *models.Repository) error { + if err := iterateRepositories(func(repo *repo_model.Repository) error { numRepos++ if owner, has := cache.Get(repo.OwnerID); has { repo.Owner = owner.(*user_model.User) } else { - if err := repo.GetOwner(); err != nil { + if err := repo.GetOwner(db.DefaultContext); err != nil { return err } cache.Add(repo.OwnerID, repo.Owner) |