Skip to content

Commit

Permalink
Move unit into models/unit/ (go-gitea#17576)
Browse files Browse the repository at this point in the history
* Move unit into models/unit/

* Rename unit.UnitType as unit.Type
  • Loading branch information
lunny authored and Stelios Malathouras committed Mar 28, 2022
1 parent f4a55d7 commit ecce7c9
Show file tree
Hide file tree
Showing 68 changed files with 556 additions and 491 deletions.
11 changes: 6 additions & 5 deletions integrations/api_repo_edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
unit_model "code.gitea.io/gitea/models/unit"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
Expand All @@ -26,15 +27,15 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
hasIssues := false
var internalTracker *api.InternalTracker
var externalTracker *api.ExternalTracker
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
if unit, err := repo.GetUnit(unit_model.TypeIssues); err == nil {
config := unit.IssuesConfig()
hasIssues = true
internalTracker = &api.InternalTracker{
EnableTimeTracker: config.EnableTimetracker,
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
EnableIssueDependencies: config.EnableDependencies,
}
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
} else if unit, err := repo.GetUnit(unit_model.TypeExternalTracker); err == nil {
config := unit.ExternalTrackerConfig()
hasIssues = true
externalTracker = &api.ExternalTracker{
Expand All @@ -45,9 +46,9 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
}
hasWiki := false
var externalWiki *api.ExternalWiki
if _, err := repo.GetUnit(models.UnitTypeWiki); err == nil {
if _, err := repo.GetUnit(unit_model.TypeWiki); err == nil {
hasWiki = true
} else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil {
} else if unit, err := repo.GetUnit(unit_model.TypeExternalWiki); err == nil {
hasWiki = true
config := unit.ExternalWikiConfig()
externalWiki = &api.ExternalWiki{
Expand All @@ -61,7 +62,7 @@ func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
allowRebase := false
allowRebaseMerge := false
allowSquash := false
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
if unit, err := repo.GetUnit(unit_model.TypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
Expand Down
13 changes: 7 additions & 6 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -62,25 +63,25 @@ func (a *Attachment) DownloadURL() string {
}

// LinkedRepository returns the linked repo if any
func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
func (a *Attachment) LinkedRepository() (*Repository, unit.Type, error) {
if a.IssueID != 0 {
iss, err := GetIssueByID(a.IssueID)
if err != nil {
return nil, UnitTypeIssues, err
return nil, unit.TypeIssues, err
}
repo, err := GetRepositoryByID(iss.RepoID)
unitType := UnitTypeIssues
unitType := unit.TypeIssues
if iss.IsPull {
unitType = UnitTypePullRequests
unitType = unit.TypePullRequests
}
return repo, unitType, err
} else if a.ReleaseID != 0 {
rel, err := GetReleaseByID(a.ReleaseID)
if err != nil {
return nil, UnitTypeReleases, err
return nil, unit.TypeReleases, err
}
repo, err := GetRepositoryByID(rel.RepoID)
return repo, UnitTypeReleases, err
return repo, unit.TypeReleases, err
}
return nil, -1, nil
}
Expand Down
9 changes: 5 additions & 4 deletions models/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -107,11 +108,11 @@ func TestLinkedRepository(t *testing.T) {
name string
attachID int64
expectedRepo *Repository
expectedUnitType UnitType
expectedUnitType unit.Type
}{
{"LinkedIssue", 1, &Repository{ID: 1}, UnitTypeIssues},
{"LinkedComment", 3, &Repository{ID: 1}, UnitTypePullRequests},
{"LinkedRelease", 9, &Repository{ID: 1}, UnitTypeReleases},
{"LinkedIssue", 1, &Repository{ID: 1}, unit.TypeIssues},
{"LinkedComment", 3, &Repository{ID: 1}, unit.TypePullRequests},
{"LinkedRelease", 9, &Repository{ID: 1}, unit.TypeReleases},
{"Notlinked", 10, nil, -1},
}
for _, tc := range testCases {
Expand Down
9 changes: 5 additions & 4 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
} else if repo, err := GetRepositoryByID(protectBranch.RepoID); err != nil {
log.Error("GetRepositoryByID: %v", err)
return false
} else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil {
} else if writeAccess, err := HasAccessUnit(user, repo, unit.TypeCode, AccessModeWrite); err != nil {
log.Error("HasAccessUnit: %v", err)
return false
} else {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permissionInRepo Permission) bool {
if !protectBranch.EnableMergeWhitelist {
// Then we need to fall back on whether the user has write permission
return permissionInRepo.CanWrite(UnitTypeCode)
return permissionInRepo.CanWrite(unit.TypeCode)
}

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

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

if !perm.CanWrite(UnitTypeCode) {
if !perm.CanWrite(unit.TypeCode) {
continue // Drop invalid user ID
}

Expand Down
5 changes: 3 additions & 2 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/references"
Expand Down Expand Up @@ -2031,9 +2032,9 @@ func (issue *Issue) ResolveMentionsByVisibility(ctx context.Context, doer *User,
}
if len(teams) != 0 {
checked := make([]int64, 0, len(teams))
unittype := UnitTypeIssues
unittype := unit.TypeIssues
if issue.IsPull {
unittype = UnitTypePullRequests
unittype = unit.TypePullRequests
}
for _, team := range teams {
if team.Authorize >= AccessModeOwner {
Expand Down
3 changes: 2 additions & 1 deletion models/issue_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package models

import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (repo *Repository) IsDependenciesEnabled() bool {
func (repo *Repository) isDependenciesEnabled(e db.Engine) bool {
var u *RepoUnit
var err error
if u, err = repo.getUnit(e, UnitTypeIssues); err != nil {
if u, err = repo.getUnit(e, unit.TypeIssues); err != nil {
log.Trace("%s", err)
return setting.Service.DefaultEnableDependencies
}
Expand Down
3 changes: 2 additions & 1 deletion models/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"

"xorm.io/xorm"
Expand Down Expand Up @@ -152,7 +153,7 @@ func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
if err != nil {
return err
}
if !perm.CanAccess(mode, UnitTypeCode) {
if !perm.CanAccess(mode, unit.TypeCode) {
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v111.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
// VisibleTypePrivate Visible only for organization's members
VisibleTypePrivate int = 2

// UnitTypeCode is unit type code
// unit.UnitTypeCode is unit type code
UnitTypeCode int = 1

// AccessModeNone no access
Expand Down
5 changes: 3 additions & 2 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -262,10 +263,10 @@ func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificat

return err
}
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypePullRequests) {
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypePullRequests) {
continue
}
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypeIssues) {
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypeIssues) {
continue
}

Expand Down
5 changes: 3 additions & 2 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
Expand Down Expand Up @@ -202,8 +203,8 @@ func CreateOrganization(org, owner *User) (err error) {
}

// insert units for team
units := make([]TeamUnit, 0, len(AllRepoUnitTypes))
for _, tp := range AllRepoUnitTypes {
units := make([]TeamUnit, 0, len(unit.AllRepoUnitTypes))
for _, tp := range unit.AllRepoUnitTypes {
units = append(units, TeamUnit{
OrgID: org.ID,
TeamID: t.ID,
Expand Down
19 changes: 10 additions & 9 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

Expand Down Expand Up @@ -135,7 +136,7 @@ func (t *Team) getUnits(e db.Engine) (err error) {
// GetUnitNames returns the team units names
func (t *Team) GetUnitNames() (res []string) {
for _, u := range t.Units {
res = append(res, Units[u.Type].NameKey)
res = append(res, unit.Units[u.Type].NameKey)
}
return
}
Expand Down Expand Up @@ -435,11 +436,11 @@ func (t *Team) RemoveRepository(repoID int64) error {
}

// UnitEnabled returns if the team has the given unit type enabled
func (t *Team) UnitEnabled(tp UnitType) bool {
func (t *Team) UnitEnabled(tp unit.Type) bool {
return t.unitEnabled(db.GetEngine(db.DefaultContext), tp)
}

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

// TeamUnit describes all units of a repository
type TeamUnit struct {
ID int64 `xorm:"pk autoincr"`
OrgID int64 `xorm:"INDEX"`
TeamID int64 `xorm:"UNIQUE(s)"`
Type UnitType `xorm:"UNIQUE(s)"`
ID int64 `xorm:"pk autoincr"`
OrgID int64 `xorm:"INDEX"`
TeamID int64 `xorm:"UNIQUE(s)"`
Type unit.Type `xorm:"UNIQUE(s)"`
}

// Unit returns Unit
func (t *TeamUnit) Unit() Unit {
return Units[t.Type]
func (t *TeamUnit) Unit() unit.Unit {
return unit.Units[t.Type]
}

func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error) {
Expand Down
5 changes: 3 additions & 2 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -236,7 +237,7 @@ func (pr *PullRequest) GetDefaultMergeMessage() string {
}

issueReference := "#"
if pr.BaseRepo.UnitEnabled(UnitTypeExternalTracker) {
if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
issueReference = "!"
}

Expand Down Expand Up @@ -338,7 +339,7 @@ func (pr *PullRequest) GetDefaultSquashMessage() string {
log.Error("LoadBaseRepo: %v", err)
return ""
}
if pr.BaseRepo.UnitEnabled(UnitTypeExternalTracker) {
if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
return fmt.Sprintf("%s (!%d)", pr.Issue.Title, pr.Issue.Index)
}
return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)
Expand Down
3 changes: 2 additions & 1 deletion models/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"github.com/stretchr/testify/assert"
)

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

externalTracker := RepoUnit{
Type: UnitTypeExternalTracker,
Type: unit.TypeExternalTracker,
Config: &ExternalTrackerConfig{
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
},
Expand Down
Loading

0 comments on commit ecce7c9

Please sign in to comment.