Skip to content

Commit 99b2858

Browse files
authored
Move unit into models/unit/ (#17576)
* Move unit into models/unit/ * Rename unit.UnitType as unit.Type
1 parent b6b1e71 commit 99b2858

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+556
-491
lines changed

integrations/api_repo_edit_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/models/db"
15+
unit_model "code.gitea.io/gitea/models/unit"
1516
api "code.gitea.io/gitea/modules/structs"
1617

1718
"github.com/stretchr/testify/assert"
@@ -26,15 +27,15 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
2627
hasIssues := false
2728
var internalTracker *api.InternalTracker
2829
var externalTracker *api.ExternalTracker
29-
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
30+
if unit, err := repo.GetUnit(unit_model.TypeIssues); err == nil {
3031
config := unit.IssuesConfig()
3132
hasIssues = true
3233
internalTracker = &api.InternalTracker{
3334
EnableTimeTracker: config.EnableTimetracker,
3435
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
3536
EnableIssueDependencies: config.EnableDependencies,
3637
}
37-
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
38+
} else if unit, err := repo.GetUnit(unit_model.TypeExternalTracker); err == nil {
3839
config := unit.ExternalTrackerConfig()
3940
hasIssues = true
4041
externalTracker = &api.ExternalTracker{
@@ -45,9 +46,9 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
4546
}
4647
hasWiki := false
4748
var externalWiki *api.ExternalWiki
48-
if _, err := repo.GetUnit(models.UnitTypeWiki); err == nil {
49+
if _, err := repo.GetUnit(unit_model.TypeWiki); err == nil {
4950
hasWiki = true
50-
} else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil {
51+
} else if unit, err := repo.GetUnit(unit_model.TypeExternalWiki); err == nil {
5152
hasWiki = true
5253
config := unit.ExternalWikiConfig()
5354
externalWiki = &api.ExternalWiki{
@@ -61,7 +62,7 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
6162
allowRebase := false
6263
allowRebaseMerge := false
6364
allowSquash := false
64-
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
65+
if unit, err := repo.GetUnit(unit_model.TypePullRequests); err == nil {
6566
config := unit.PullRequestsConfig()
6667
hasPullRequests = true
6768
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts

models/attachment.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path"
1111

1212
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/models/unit"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/storage"
1516
"code.gitea.io/gitea/modules/timeutil"
@@ -62,25 +63,25 @@ func (a *Attachment) DownloadURL() string {
6263
}
6364

6465
// LinkedRepository returns the linked repo if any
65-
func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
66+
func (a *Attachment) LinkedRepository() (*Repository, unit.Type, error) {
6667
if a.IssueID != 0 {
6768
iss, err := GetIssueByID(a.IssueID)
6869
if err != nil {
69-
return nil, UnitTypeIssues, err
70+
return nil, unit.TypeIssues, err
7071
}
7172
repo, err := GetRepositoryByID(iss.RepoID)
72-
unitType := UnitTypeIssues
73+
unitType := unit.TypeIssues
7374
if iss.IsPull {
74-
unitType = UnitTypePullRequests
75+
unitType = unit.TypePullRequests
7576
}
7677
return repo, unitType, err
7778
} else if a.ReleaseID != 0 {
7879
rel, err := GetReleaseByID(a.ReleaseID)
7980
if err != nil {
80-
return nil, UnitTypeReleases, err
81+
return nil, unit.TypeReleases, err
8182
}
8283
repo, err := GetRepositoryByID(rel.RepoID)
83-
return repo, UnitTypeReleases, err
84+
return repo, unit.TypeReleases, err
8485
}
8586
return nil, -1, nil
8687
}

models/attachment_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unit"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -107,11 +108,11 @@ func TestLinkedRepository(t *testing.T) {
107108
name string
108109
attachID int64
109110
expectedRepo *Repository
110-
expectedUnitType UnitType
111+
expectedUnitType unit.Type
111112
}{
112-
{"LinkedIssue", 1, &Repository{ID: 1}, UnitTypeIssues},
113-
{"LinkedComment", 3, &Repository{ID: 1}, UnitTypePullRequests},
114-
{"LinkedRelease", 9, &Repository{ID: 1}, UnitTypeReleases},
113+
{"LinkedIssue", 1, &Repository{ID: 1}, unit.TypeIssues},
114+
{"LinkedComment", 3, &Repository{ID: 1}, unit.TypePullRequests},
115+
{"LinkedRelease", 9, &Repository{ID: 1}, unit.TypeReleases},
115116
{"Notlinked", 10, nil, -1},
116117
}
117118
for _, tc := range testCases {

models/branches.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/models/db"
14+
"code.gitea.io/gitea/models/unit"
1415
"code.gitea.io/gitea/modules/base"
1516
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/timeutil"
@@ -74,7 +75,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
7475
} else if repo, err := GetRepositoryByID(protectBranch.RepoID); err != nil {
7576
log.Error("GetRepositoryByID: %v", err)
7677
return false
77-
} else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil {
78+
} else if writeAccess, err := HasAccessUnit(user, repo, unit.TypeCode, AccessModeWrite); err != nil {
7879
log.Error("HasAccessUnit: %v", err)
7980
return false
8081
} else {
@@ -102,7 +103,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
102103
func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permissionInRepo Permission) bool {
103104
if !protectBranch.EnableMergeWhitelist {
104105
// Then we need to fall back on whether the user has write permission
105-
return permissionInRepo.CanWrite(UnitTypeCode)
106+
return permissionInRepo.CanWrite(unit.TypeCode)
106107
}
107108

108109
if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
@@ -134,7 +135,7 @@ func (protectBranch *ProtectedBranch) isUserOfficialReviewer(e db.Engine, user *
134135

135136
if !protectBranch.EnableApprovalsWhitelist {
136137
// Anyone with write access is considered official reviewer
137-
writeAccess, err := hasAccessUnit(e, user, repo, UnitTypeCode, AccessModeWrite)
138+
writeAccess, err := hasAccessUnit(e, user, repo, unit.TypeCode, AccessModeWrite)
138139
if err != nil {
139140
return false, err
140141
}
@@ -454,7 +455,7 @@ func updateUserWhitelist(repo *Repository, currentWhitelist, newWhitelist []int6
454455
return nil, fmt.Errorf("GetUserRepoPermission [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
455456
}
456457

457-
if !perm.CanWrite(UnitTypeCode) {
458+
if !perm.CanWrite(unit.TypeCode) {
458459
continue // Drop invalid user ID
459460
}
460461

models/issue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"code.gitea.io/gitea/models/db"
1717
"code.gitea.io/gitea/models/issues"
18+
"code.gitea.io/gitea/models/unit"
1819
"code.gitea.io/gitea/modules/base"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/references"
@@ -2031,9 +2032,9 @@ func (issue *Issue) ResolveMentionsByVisibility(ctx context.Context, doer *User,
20312032
}
20322033
if len(teams) != 0 {
20332034
checked := make([]int64, 0, len(teams))
2034-
unittype := UnitTypeIssues
2035+
unittype := unit.TypeIssues
20352036
if issue.IsPull {
2036-
unittype = UnitTypePullRequests
2037+
unittype = unit.TypePullRequests
20372038
}
20382039
for _, team := range teams {
20392040
if team.Authorize >= AccessModeOwner {

models/issue_dependency.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package models
66

77
import (
88
"code.gitea.io/gitea/models/db"
9+
"code.gitea.io/gitea/models/unit"
910
"code.gitea.io/gitea/modules/log"
1011
"code.gitea.io/gitea/modules/setting"
1112
"code.gitea.io/gitea/modules/timeutil"
@@ -141,7 +142,7 @@ func (repo *Repository) IsDependenciesEnabled() bool {
141142
func (repo *Repository) isDependenciesEnabled(e db.Engine) bool {
142143
var u *RepoUnit
143144
var err error
144-
if u, err = repo.getUnit(e, UnitTypeIssues); err != nil {
145+
if u, err = repo.getUnit(e, unit.TypeIssues); err != nil {
145146
log.Trace("%s", err)
146147
return setting.Service.DefaultEnableDependencies
147148
}

models/lfs_lock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/models/db"
14+
"code.gitea.io/gitea/models/unit"
1415
"code.gitea.io/gitea/modules/log"
1516

1617
"xorm.io/xorm"
@@ -152,7 +153,7 @@ func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
152153
if err != nil {
153154
return err
154155
}
155-
if !perm.CanAccess(mode, UnitTypeCode) {
156+
if !perm.CanAccess(mode, unit.TypeCode) {
156157
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
157158
}
158159
return nil

models/migrations/v111.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
5656
// VisibleTypePrivate Visible only for organization's members
5757
VisibleTypePrivate int = 2
5858

59-
// UnitTypeCode is unit type code
59+
// unit.UnitTypeCode is unit type code
6060
UnitTypeCode int = 1
6161

6262
// AccessModeNone no access

models/notification.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010

1111
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/models/unit"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/timeutil"
@@ -262,10 +263,10 @@ func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificat
262263

263264
return err
264265
}
265-
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypePullRequests) {
266+
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypePullRequests) {
266267
continue
267268
}
268-
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypeIssues) {
269+
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypeIssues) {
269270
continue
270271
}
271272

models/org.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/models/unit"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
1516
"code.gitea.io/gitea/modules/storage"
@@ -202,8 +203,8 @@ func CreateOrganization(org, owner *User) (err error) {
202203
}
203204

204205
// insert units for team
205-
units := make([]TeamUnit, 0, len(AllRepoUnitTypes))
206-
for _, tp := range AllRepoUnitTypes {
206+
units := make([]TeamUnit, 0, len(unit.AllRepoUnitTypes))
207+
for _, tp := range unit.AllRepoUnitTypes {
207208
units = append(units, TeamUnit{
208209
OrgID: org.ID,
209210
TeamID: t.ID,

models/org_team.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"code.gitea.io/gitea/models/db"
15+
"code.gitea.io/gitea/models/unit"
1516
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
1718

@@ -135,7 +136,7 @@ func (t *Team) getUnits(e db.Engine) (err error) {
135136
// GetUnitNames returns the team units names
136137
func (t *Team) GetUnitNames() (res []string) {
137138
for _, u := range t.Units {
138-
res = append(res, Units[u.Type].NameKey)
139+
res = append(res, unit.Units[u.Type].NameKey)
139140
}
140141
return
141142
}
@@ -435,11 +436,11 @@ func (t *Team) RemoveRepository(repoID int64) error {
435436
}
436437

437438
// UnitEnabled returns if the team has the given unit type enabled
438-
func (t *Team) UnitEnabled(tp UnitType) bool {
439+
func (t *Team) UnitEnabled(tp unit.Type) bool {
439440
return t.unitEnabled(db.GetEngine(db.DefaultContext), tp)
440441
}
441442

442-
func (t *Team) unitEnabled(e db.Engine, tp UnitType) bool {
443+
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
443444
if err := t.getUnits(e); err != nil {
444445
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
445446
}
@@ -1029,15 +1030,15 @@ func GetTeamsWithAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, er
10291030

10301031
// TeamUnit describes all units of a repository
10311032
type TeamUnit struct {
1032-
ID int64 `xorm:"pk autoincr"`
1033-
OrgID int64 `xorm:"INDEX"`
1034-
TeamID int64 `xorm:"UNIQUE(s)"`
1035-
Type UnitType `xorm:"UNIQUE(s)"`
1033+
ID int64 `xorm:"pk autoincr"`
1034+
OrgID int64 `xorm:"INDEX"`
1035+
TeamID int64 `xorm:"UNIQUE(s)"`
1036+
Type unit.Type `xorm:"UNIQUE(s)"`
10361037
}
10371038

10381039
// Unit returns Unit
1039-
func (t *TeamUnit) Unit() Unit {
1040-
return Units[t.Type]
1040+
func (t *TeamUnit) Unit() unit.Unit {
1041+
return unit.Units[t.Type]
10411042
}
10421043

10431044
func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error) {

models/pull.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"code.gitea.io/gitea/models/db"
14+
"code.gitea.io/gitea/models/unit"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/timeutil"
@@ -236,7 +237,7 @@ func (pr *PullRequest) GetDefaultMergeMessage() string {
236237
}
237238

238239
issueReference := "#"
239-
if pr.BaseRepo.UnitEnabled(UnitTypeExternalTracker) {
240+
if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
240241
issueReference = "!"
241242
}
242243

@@ -338,7 +339,7 @@ func (pr *PullRequest) GetDefaultSquashMessage() string {
338339
log.Error("LoadBaseRepo: %v", err)
339340
return ""
340341
}
341-
if pr.BaseRepo.UnitEnabled(UnitTypeExternalTracker) {
342+
if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
342343
return fmt.Sprintf("%s (!%d)", pr.Issue.Title, pr.Issue.Index)
343344
}
344345
return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)

models/pull_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unit"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -255,7 +256,7 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) {
255256
assert.NoError(t, db.PrepareTestDatabase())
256257

257258
externalTracker := RepoUnit{
258-
Type: UnitTypeExternalTracker,
259+
Type: unit.TypeExternalTracker,
259260
Config: &ExternalTrackerConfig{
260261
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
261262
},

0 commit comments

Comments
 (0)