1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move unit into models/unit/ (#17576)

* Move unit into models/unit/

* Rename unit.UnitType as unit.Type
This commit is contained in:
Lunny Xiao
2021-11-10 03:57:58 +08:00
committed by GitHub
parent b6b1e71665
commit 99b2858e62
68 changed files with 556 additions and 491 deletions

View File

@@ -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"
@@ -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
}

View File

@@ -8,6 +8,7 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"github.com/stretchr/testify/assert"
)
@@ -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 {

View File

@@ -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"
@@ -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 {
@@ -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) {
@@ -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
}
@@ -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
}

View File

@@ -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"
@@ -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 {

View File

@@ -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"
@@ -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
}

View File

@@ -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"
@@ -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

View File

@@ -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

View File

@@ -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"
@@ -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
}

View File

@@ -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"
@@ -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,

View File

@@ -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"
@@ -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
}
@@ -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())
}
@@ -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) {

View File

@@ -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"
@@ -236,7 +237,7 @@ func (pr *PullRequest) GetDefaultMergeMessage() string {
}
issueReference := "#"
if pr.BaseRepo.UnitEnabled(UnitTypeExternalTracker) {
if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
issueReference = "!"
}
@@ -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)

View File

@@ -8,6 +8,7 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"github.com/stretchr/testify/assert"
)
@@ -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}",
},

View File

@@ -23,6 +23,7 @@ import (
"unicode/utf8"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
@@ -128,7 +129,7 @@ func loadRepoConfig() {
// NewRepoContext creates a new repository context
func NewRepoContext() {
loadRepoConfig()
loadUnitConfig()
unit.LoadUnitConfig()
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
}
@@ -353,11 +354,11 @@ func (repo *Repository) getUnits(e db.Engine) (err error) {
}
// CheckUnitUser check whether user could visit the unit of this repository
func (repo *Repository) CheckUnitUser(user *User, unitType UnitType) bool {
func (repo *Repository) CheckUnitUser(user *User, unitType unit.Type) bool {
return repo.checkUnitUser(db.GetEngine(db.DefaultContext), user, unitType)
}
func (repo *Repository) checkUnitUser(e db.Engine, user *User, unitType UnitType) bool {
func (repo *Repository) checkUnitUser(e db.Engine, user *User, unitType unit.Type) bool {
if user.IsAdmin {
return true
}
@@ -371,7 +372,7 @@ func (repo *Repository) checkUnitUser(e db.Engine, user *User, unitType UnitType
}
// UnitEnabled if this repository has the given unit enabled
func (repo *Repository) UnitEnabled(tp UnitType) bool {
func (repo *Repository) UnitEnabled(tp unit.Type) bool {
if err := repo.getUnits(db.GetEngine(db.DefaultContext)); err != nil {
log.Warn("Error loading repository (ID: %d) units: %s", repo.ID, err.Error())
}
@@ -385,7 +386,7 @@ func (repo *Repository) UnitEnabled(tp UnitType) bool {
// ErrUnitTypeNotExist represents a "UnitTypeNotExist" kind of error.
type ErrUnitTypeNotExist struct {
UT UnitType
UT unit.Type
}
// IsErrUnitTypeNotExist checks if an error is a ErrUnitNotExist.
@@ -399,28 +400,28 @@ func (err ErrUnitTypeNotExist) Error() string {
}
// MustGetUnit always returns a RepoUnit object
func (repo *Repository) MustGetUnit(tp UnitType) *RepoUnit {
func (repo *Repository) MustGetUnit(tp unit.Type) *RepoUnit {
ru, err := repo.GetUnit(tp)
if err == nil {
return ru
}
if tp == UnitTypeExternalWiki {
if tp == unit.TypeExternalWiki {
return &RepoUnit{
Type: tp,
Config: new(ExternalWikiConfig),
}
} else if tp == UnitTypeExternalTracker {
} else if tp == unit.TypeExternalTracker {
return &RepoUnit{
Type: tp,
Config: new(ExternalTrackerConfig),
}
} else if tp == UnitTypePullRequests {
} else if tp == unit.TypePullRequests {
return &RepoUnit{
Type: tp,
Config: new(PullRequestsConfig),
}
} else if tp == UnitTypeIssues {
} else if tp == unit.TypeIssues {
return &RepoUnit{
Type: tp,
Config: new(IssuesConfig),
@@ -433,11 +434,11 @@ func (repo *Repository) MustGetUnit(tp UnitType) *RepoUnit {
}
// GetUnit returns a RepoUnit object
func (repo *Repository) GetUnit(tp UnitType) (*RepoUnit, error) {
func (repo *Repository) GetUnit(tp unit.Type) (*RepoUnit, error) {
return repo.getUnit(db.GetEngine(db.DefaultContext), tp)
}
func (repo *Repository) getUnit(e db.Engine, tp UnitType) (*RepoUnit, error) {
func (repo *Repository) getUnit(e db.Engine, tp unit.Type) (*RepoUnit, error) {
if err := repo.getUnits(e); err != nil {
return nil, err
}
@@ -484,7 +485,7 @@ func (repo *Repository) ComposeMetas() map[string]string {
"mode": "comment",
}
unit, err := repo.GetUnit(UnitTypeExternalTracker)
unit, err := repo.GetUnit(unit.TypeExternalTracker)
if err == nil {
metas["format"] = unit.ExternalTrackerConfig().ExternalTrackerFormat
switch unit.ExternalTrackerConfig().ExternalTrackerStyle {
@@ -802,7 +803,7 @@ func (repo *Repository) CanEnablePulls() bool {
// AllowsPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
func (repo *Repository) AllowsPulls() bool {
return repo.CanEnablePulls() && repo.UnitEnabled(UnitTypePullRequests)
return repo.CanEnablePulls() && repo.UnitEnabled(unit.TypePullRequests)
}
// CanEnableEditor returns true if repository meets the requirements of web editor.
@@ -1076,9 +1077,9 @@ func CreateRepository(ctx context.Context, doer, u *User, repo *Repository, over
}
// insert units for repo
units := make([]RepoUnit, 0, len(DefaultRepoUnits))
for _, tp := range DefaultRepoUnits {
if tp == UnitTypeIssues {
units := make([]RepoUnit, 0, len(unit.DefaultRepoUnits))
for _, tp := range unit.DefaultRepoUnits {
if tp == unit.TypeIssues {
units = append(units, RepoUnit{
RepoID: repo.ID,
Type: tp,
@@ -1088,7 +1089,7 @@ func CreateRepository(ctx context.Context, doer, u *User, repo *Repository, over
EnableDependencies: setting.Service.DefaultEnableDependencies,
},
})
} else if tp == UnitTypePullRequests {
} else if tp == unit.TypePullRequests {
units = append(units, RepoUnit{
RepoID: repo.ID,
Type: tp,
@@ -1406,7 +1407,7 @@ func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
}
// UpdateRepositoryUnits updates a repository's units
func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []UnitType) (err error) {
func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) {
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err = sess.Begin(); err != nil {

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
@@ -276,7 +277,7 @@ func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) {
teamMember, err := db.GetEngine(db.DefaultContext).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
Where("team_repo.repo_id = ?", repo.ID).
And("team_unit.`type` = ?", UnitTypeCode).
And("team_unit.`type` = ?", unit.TypeCode).
And("team_user.uid = ?", userID).Table("team_user").Exist(&TeamUser{})
if err != nil {
return false, err

View File

@@ -4,7 +4,10 @@
package models
import "code.gitea.io/gitea/modules/setting"
import (
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/setting"
)
// ___________.__ ___________ __
// \__ ___/|__| _____ ___\__ ___/___________ ____ | | __ ___________
@@ -27,7 +30,7 @@ func (repo *Repository) IsTimetrackerEnabled() bool {
var u *RepoUnit
var err error
if u, err = repo.GetUnit(UnitTypeIssues); err != nil {
if u, err = repo.GetUnit(unit.TypeIssues); err != nil {
return setting.Service.DefaultEnableTimetracking
}
return u.IssuesConfig().EnableTimetracker
@@ -37,7 +40,7 @@ func (repo *Repository) IsTimetrackerEnabled() bool {
func (repo *Repository) AllowOnlyContributorsToTrackTime() bool {
var u *RepoUnit
var err error
if u, err = repo.GetUnit(UnitTypeIssues); err != nil {
if u, err = repo.GetUnit(unit.TypeIssues); err != nil {
return setting.Service.DefaultAllowOnlyContributorsToTrackTime
}
return u.IssuesConfig().AllowOnlyContributorsToTrackTime

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
)
@@ -15,7 +16,7 @@ import (
type Permission struct {
AccessMode AccessMode
Units []*RepoUnit
UnitsMode map[UnitType]AccessMode
UnitsMode map[unit.Type]AccessMode
}
// IsOwner returns true if current user is the owner of repository.
@@ -37,7 +38,7 @@ func (p *Permission) HasAccess() bool {
}
// UnitAccessMode returns current user accessmode to the specify unit of the repository
func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
func (p *Permission) UnitAccessMode(unitType unit.Type) AccessMode {
if p.UnitsMode == nil {
for _, u := range p.Units {
if u.Type == unitType {
@@ -50,12 +51,12 @@ func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
}
// CanAccess returns true if user has mode access to the unit of the repository
func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
func (p *Permission) CanAccess(mode AccessMode, unitType unit.Type) bool {
return p.UnitAccessMode(unitType) >= mode
}
// CanAccessAny returns true if user has mode access to any of the units of the repository
func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...unit.Type) bool {
for _, u := range unitTypes {
if p.CanAccess(mode, u) {
return true
@@ -65,12 +66,12 @@ func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
}
// CanRead returns true if user could read to this unit
func (p *Permission) CanRead(unitType UnitType) bool {
func (p *Permission) CanRead(unitType unit.Type) bool {
return p.CanAccess(AccessModeRead, unitType)
}
// CanReadAny returns true if user has read access to any of the units of the repository
func (p *Permission) CanReadAny(unitTypes ...UnitType) bool {
func (p *Permission) CanReadAny(unitTypes ...unit.Type) bool {
return p.CanAccessAny(AccessModeRead, unitTypes...)
}
@@ -78,13 +79,13 @@ func (p *Permission) CanReadAny(unitTypes ...UnitType) bool {
// returns true if isPull is false and user could read to issues
func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
if isPull {
return p.CanRead(UnitTypePullRequests)
return p.CanRead(unit.TypePullRequests)
}
return p.CanRead(UnitTypeIssues)
return p.CanRead(unit.TypeIssues)
}
// CanWrite returns true if user could write to this unit
func (p *Permission) CanWrite(unitType UnitType) bool {
func (p *Permission) CanWrite(unitType unit.Type) bool {
return p.CanAccess(AccessModeWrite, unitType)
}
@@ -92,9 +93,9 @@ func (p *Permission) CanWrite(unitType UnitType) bool {
// returns true if isPull is false and user could write to issues
func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
if isPull {
return p.CanWrite(UnitTypePullRequests)
return p.CanWrite(unit.TypePullRequests)
}
return p.CanWrite(UnitTypeIssues)
return p.CanWrite(unit.TypeIssues)
}
// ColorFormat writes a colored string for these Permissions
@@ -215,7 +216,7 @@ func getUserRepoPermission(e db.Engine, repo *Repository, user *User) (perm Perm
return
}
perm.UnitsMode = make(map[UnitType]AccessMode)
perm.UnitsMode = make(map[unit.Type]AccessMode)
// Collaborators on organization
if isCollaborator {
@@ -330,16 +331,16 @@ func isUserRepoAdmin(e db.Engine, repo *Repository, user *User) (bool, error) {
// AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
// user does not have access.
func AccessLevel(user *User, repo *Repository) (AccessMode, error) {
return accessLevelUnit(db.GetEngine(db.DefaultContext), user, repo, UnitTypeCode)
return accessLevelUnit(db.GetEngine(db.DefaultContext), user, repo, unit.TypeCode)
}
// AccessLevelUnit returns the Access a user has to a repository's. Will return NoneAccess if the
// user does not have access.
func AccessLevelUnit(user *User, repo *Repository, unitType UnitType) (AccessMode, error) {
func AccessLevelUnit(user *User, repo *Repository, unitType unit.Type) (AccessMode, error) {
return accessLevelUnit(db.GetEngine(db.DefaultContext), user, repo, unitType)
}
func accessLevelUnit(e db.Engine, user *User, repo *Repository, unitType UnitType) (AccessMode, error) {
func accessLevelUnit(e db.Engine, user *User, repo *Repository, unitType unit.Type) (AccessMode, error) {
perm, err := getUserRepoPermission(e, repo, user)
if err != nil {
return AccessModeNone, err
@@ -347,13 +348,13 @@ func accessLevelUnit(e db.Engine, user *User, repo *Repository, unitType UnitTyp
return perm.UnitAccessMode(unitType), nil
}
func hasAccessUnit(e db.Engine, user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
func hasAccessUnit(e db.Engine, user *User, repo *Repository, unitType unit.Type, testMode AccessMode) (bool, error) {
mode, err := accessLevelUnit(e, user, repo, unitType)
return testMode <= mode, err
}
// HasAccessUnit returns true if user has testMode to the unit of the repository
func HasAccessUnit(user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
func HasAccessUnit(user *User, repo *Repository, unitType unit.Type, testMode AccessMode) (bool, error) {
return hasAccessUnit(db.GetEngine(db.DefaultContext), user, repo, unitType, testMode)
}
@@ -372,7 +373,7 @@ func canBeAssigned(e db.Engine, user *User, repo *Repository, _ bool) (bool, err
if err != nil {
return false, err
}
return perm.CanAccessAny(AccessModeWrite, UnitTypeCode, UnitTypeIssues, UnitTypePullRequests), nil
return perm.CanAccessAny(AccessModeWrite, unit.TypeCode, unit.TypeIssues, unit.TypePullRequests), nil
}
func hasAccess(e db.Engine, userID int64, repo *Repository) (bool, error) {
@@ -397,7 +398,7 @@ func HasAccess(userID int64, repo *Repository) (bool, error) {
}
// FilterOutRepoIdsWithoutUnitAccess filter out repos where user has no access to repositories
func FilterOutRepoIdsWithoutUnitAccess(u *User, repoIDs []int64, units ...UnitType) ([]int64, error) {
func FilterOutRepoIdsWithoutUnitAccess(u *User, repoIDs []int64, units ...unit.Type) ([]int64, error) {
i := 0
for _, rID := range repoIDs {
repo, err := GetRepositoryByID(rID)

View File

@@ -8,6 +8,7 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"github.com/stretchr/testify/assert"
)
@@ -165,8 +166,8 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) {
for _, unit := range repo.Units {
assert.True(t, perm.CanRead(unit.Type))
}
assert.True(t, perm.CanWrite(UnitTypeIssues))
assert.False(t, perm.CanWrite(UnitTypeCode))
assert.True(t, perm.CanWrite(unit.TypeIssues))
assert.False(t, perm.CanWrite(unit.TypeCode))
// admin
admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
@@ -235,17 +236,17 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) {
tester := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
perm, err = GetUserRepoPermission(repo, tester)
assert.NoError(t, err)
assert.True(t, perm.CanWrite(UnitTypeIssues))
assert.False(t, perm.CanWrite(UnitTypeCode))
assert.False(t, perm.CanRead(UnitTypeCode))
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 := db.AssertExistsAndLoadBean(t, &User{ID: 20}).(*User)
perm, err = GetUserRepoPermission(repo, reviewer)
assert.NoError(t, err)
assert.False(t, perm.CanRead(UnitTypeIssues))
assert.False(t, perm.CanWrite(UnitTypeCode))
assert.True(t, perm.CanRead(UnitTypeCode))
assert.False(t, perm.CanRead(unit.TypeIssues))
assert.False(t, perm.CanWrite(unit.TypeCode))
assert.True(t, perm.CanRead(unit.TypeCode))
// admin
admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)

View File

@@ -13,6 +13,7 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/markup"
"github.com/stretchr/testify/assert"
@@ -32,7 +33,7 @@ func TestMetas(t *testing.T) {
assert.Equal(t, "testOwner", metas["user"])
externalTracker := RepoUnit{
Type: UnitTypeExternalTracker,
Type: unit.TypeExternalTracker,
Config: &ExternalTrackerConfig{
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
},

View File

@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/timeutil"
@@ -20,7 +21,7 @@ import (
type RepoUnit struct {
ID int64
RepoID int64 `xorm:"INDEX(s)"`
Type UnitType `xorm:"INDEX(s)"`
Type unit.Type `xorm:"INDEX(s)"`
Config convert.Conversion `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
}
@@ -154,16 +155,16 @@ func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
switch colName {
case "type":
switch UnitType(login.Cell2Int64(val)) {
case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects:
switch unit.Type(login.Cell2Int64(val)) {
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects:
r.Config = new(UnitConfig)
case UnitTypeExternalWiki:
case unit.TypeExternalWiki:
r.Config = new(ExternalWikiConfig)
case UnitTypeExternalTracker:
case unit.TypeExternalTracker:
r.Config = new(ExternalTrackerConfig)
case UnitTypePullRequests:
case unit.TypePullRequests:
r.Config = new(PullRequestsConfig)
case UnitTypeIssues:
case unit.TypeIssues:
r.Config = new(IssuesConfig)
default:
panic(fmt.Sprintf("unrecognized repo unit type: %v", *val))
@@ -172,36 +173,36 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
}
// Unit returns Unit
func (r *RepoUnit) Unit() Unit {
return Units[r.Type]
func (r *RepoUnit) Unit() unit.Unit {
return unit.Units[r.Type]
}
// CodeConfig returns config for UnitTypeCode
// CodeConfig returns config for unit.TypeCode
func (r *RepoUnit) CodeConfig() *UnitConfig {
return r.Config.(*UnitConfig)
}
// PullRequestsConfig returns config for UnitTypePullRequests
// PullRequestsConfig returns config for unit.TypePullRequests
func (r *RepoUnit) PullRequestsConfig() *PullRequestsConfig {
return r.Config.(*PullRequestsConfig)
}
// ReleasesConfig returns config for UnitTypeReleases
// ReleasesConfig returns config for unit.TypeReleases
func (r *RepoUnit) ReleasesConfig() *UnitConfig {
return r.Config.(*UnitConfig)
}
// ExternalWikiConfig returns config for UnitTypeExternalWiki
// ExternalWikiConfig returns config for unit.TypeExternalWiki
func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
return r.Config.(*ExternalWikiConfig)
}
// IssuesConfig returns config for UnitTypeIssues
// IssuesConfig returns config for unit.TypeIssues
func (r *RepoUnit) IssuesConfig() *IssuesConfig {
return r.Config.(*IssuesConfig)
}
// ExternalTrackerConfig returns config for UnitTypeExternalTracker
// ExternalTrackerConfig returns config for unit.TypeExternalTracker
func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
return r.Config.(*ExternalTrackerConfig)
}

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
)
@@ -245,9 +246,9 @@ func notifyWatchers(e db.Engine, actions ...*Action) error {
permPR[i] = false
continue
}
permCode[i] = perm.CanRead(UnitTypeCode)
permIssue[i] = perm.CanRead(UnitTypeIssues)
permPR[i] = perm.CanRead(UnitTypePullRequests)
permCode[i] = perm.CanRead(unit.TypeCode)
permIssue[i] = perm.CanRead(unit.TypeIssues)
permPR[i] = perm.CanRead(unit.TypePullRequests)
}
}

View File

@@ -9,6 +9,7 @@ import (
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/timeutil"
@@ -897,7 +898,7 @@ func CanMarkConversation(issue *Issue, doer *User) (permResult bool, err error)
return false, err
}
permResult = perm.CanAccess(AccessModeWrite, UnitTypePullRequests)
permResult = perm.CanAccess(AccessModeWrite, unit.TypePullRequests)
if !permResult {
if permResult, err = IsOfficialReviewer(issue, doer); err != nil {
return false, err

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
package unit
import (
"fmt"
@@ -12,50 +12,50 @@ import (
"code.gitea.io/gitea/modules/setting"
)
// UnitType is Unit's Type
type UnitType int
// Type is Unit's Type
type Type int
// Enumerate all the unit types
const (
UnitTypeCode UnitType = iota + 1 // 1 code
UnitTypeIssues // 2 issues
UnitTypePullRequests // 3 PRs
UnitTypeReleases // 4 Releases
UnitTypeWiki // 5 Wiki
UnitTypeExternalWiki // 6 ExternalWiki
UnitTypeExternalTracker // 7 ExternalTracker
UnitTypeProjects // 8 Kanban board
TypeCode Type = iota + 1 // 1 code
TypeIssues // 2 issues
TypePullRequests // 3 PRs
TypeReleases // 4 Releases
TypeWiki // 5 Wiki
TypeExternalWiki // 6 ExternalWiki
TypeExternalTracker // 7 ExternalTracker
TypeProjects // 8 Kanban board
)
// Value returns integer value for unit type
func (u UnitType) Value() int {
func (u Type) Value() int {
return int(u)
}
func (u UnitType) String() string {
func (u Type) String() string {
switch u {
case UnitTypeCode:
return "UnitTypeCode"
case UnitTypeIssues:
return "UnitTypeIssues"
case UnitTypePullRequests:
return "UnitTypePullRequests"
case UnitTypeReleases:
return "UnitTypeReleases"
case UnitTypeWiki:
return "UnitTypeWiki"
case UnitTypeExternalWiki:
return "UnitTypeExternalWiki"
case UnitTypeExternalTracker:
return "UnitTypeExternalTracker"
case UnitTypeProjects:
return "UnitTypeProjects"
case TypeCode:
return "TypeCode"
case TypeIssues:
return "TypeIssues"
case TypePullRequests:
return "TypePullRequests"
case TypeReleases:
return "TypeReleases"
case TypeWiki:
return "TypeWiki"
case TypeExternalWiki:
return "TypeExternalWiki"
case TypeExternalTracker:
return "TypeExternalTracker"
case TypeProjects:
return "TypeProjects"
}
return fmt.Sprintf("Unknown UnitType %d", u)
return fmt.Sprintf("Unknown Type %d", u)
}
// ColorFormat provides a ColorFormatted version of this UnitType
func (u UnitType) ColorFormat(s fmt.State) {
// ColorFormat provides a ColorFormatted version of this Type
func (u Type) ColorFormat(s fmt.State) {
log.ColorFprintf(s, "%d:%s",
log.NewColoredIDValue(u),
u)
@@ -63,49 +63,50 @@ func (u UnitType) ColorFormat(s fmt.State) {
var (
// AllRepoUnitTypes contains all the unit types
AllRepoUnitTypes = []UnitType{
UnitTypeCode,
UnitTypeIssues,
UnitTypePullRequests,
UnitTypeReleases,
UnitTypeWiki,
UnitTypeExternalWiki,
UnitTypeExternalTracker,
UnitTypeProjects,
AllRepoUnitTypes = []Type{
TypeCode,
TypeIssues,
TypePullRequests,
TypeReleases,
TypeWiki,
TypeExternalWiki,
TypeExternalTracker,
TypeProjects,
}
// DefaultRepoUnits contains the default unit types
DefaultRepoUnits = []UnitType{
UnitTypeCode,
UnitTypeIssues,
UnitTypePullRequests,
UnitTypeReleases,
UnitTypeWiki,
UnitTypeProjects,
DefaultRepoUnits = []Type{
TypeCode,
TypeIssues,
TypePullRequests,
TypeReleases,
TypeWiki,
TypeProjects,
}
// NotAllowedDefaultRepoUnits contains units that can't be default
NotAllowedDefaultRepoUnits = []UnitType{
UnitTypeExternalWiki,
UnitTypeExternalTracker,
NotAllowedDefaultRepoUnits = []Type{
TypeExternalWiki,
TypeExternalTracker,
}
// MustRepoUnits contains the units could not be disabled currently
MustRepoUnits = []UnitType{
UnitTypeCode,
UnitTypeReleases,
MustRepoUnits = []Type{
TypeCode,
TypeReleases,
}
// DisabledRepoUnits contains the units that have been globally disabled
DisabledRepoUnits = []UnitType{}
DisabledRepoUnits = []Type{}
)
func loadUnitConfig() {
// LoadUnitConfig load units from settings
func LoadUnitConfig() {
setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
// Default repo units set if setting is not empty
if len(setDefaultRepoUnits) > 0 {
// MustRepoUnits required as default
DefaultRepoUnits = make([]UnitType, len(MustRepoUnits))
DefaultRepoUnits = make([]Type, len(MustRepoUnits))
copy(DefaultRepoUnits, MustRepoUnits)
for _, defaultU := range setDefaultRepoUnits {
if !defaultU.CanBeDefault() {
@@ -138,7 +139,7 @@ func loadUnitConfig() {
}
// UnitGlobalDisabled checks if unit type is global disabled
func (u UnitType) UnitGlobalDisabled() bool {
func (u Type) UnitGlobalDisabled() bool {
for _, ud := range DisabledRepoUnits {
if u == ud {
return true
@@ -148,7 +149,7 @@ func (u UnitType) UnitGlobalDisabled() bool {
}
// CanDisable checks if this unit type can be disabled.
func (u *UnitType) CanDisable() bool {
func (u *Type) CanDisable() bool {
for _, mu := range MustRepoUnits {
if *u == mu {
return false
@@ -158,7 +159,7 @@ func (u *UnitType) CanDisable() bool {
}
// CanBeDefault checks if the unit type can be a default repo unit
func (u *UnitType) CanBeDefault() bool {
func (u *Type) CanBeDefault() bool {
for _, nadU := range NotAllowedDefaultRepoUnits {
if *u == nadU {
return false
@@ -169,7 +170,7 @@ func (u *UnitType) CanBeDefault() bool {
// Unit is a section of one repository
type Unit struct {
Type UnitType
Type Type
NameKey string
URI string
DescKey string
@@ -183,7 +184,7 @@ func (u *Unit) CanDisable() bool {
// IsLessThan compares order of two units
func (u Unit) IsLessThan(unit Unit) bool {
if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
if (u.Type == TypeExternalTracker || u.Type == TypeExternalWiki) && unit.Type != TypeExternalTracker && unit.Type != TypeExternalWiki {
return false
}
return u.Idx < unit.Idx
@@ -192,7 +193,7 @@ func (u Unit) IsLessThan(unit Unit) bool {
// Enumerate all the units
var (
UnitCode = Unit{
UnitTypeCode,
TypeCode,
"repo.code",
"/",
"repo.code.desc",
@@ -200,7 +201,7 @@ var (
}
UnitIssues = Unit{
UnitTypeIssues,
TypeIssues,
"repo.issues",
"/issues",
"repo.issues.desc",
@@ -208,7 +209,7 @@ var (
}
UnitExternalTracker = Unit{
UnitTypeExternalTracker,
TypeExternalTracker,
"repo.ext_issues",
"/issues",
"repo.ext_issues.desc",
@@ -216,7 +217,7 @@ var (
}
UnitPullRequests = Unit{
UnitTypePullRequests,
TypePullRequests,
"repo.pulls",
"/pulls",
"repo.pulls.desc",
@@ -224,7 +225,7 @@ var (
}
UnitReleases = Unit{
UnitTypeReleases,
TypeReleases,
"repo.releases",
"/releases",
"repo.releases.desc",
@@ -232,7 +233,7 @@ var (
}
UnitWiki = Unit{
UnitTypeWiki,
TypeWiki,
"repo.wiki",
"/wiki",
"repo.wiki.desc",
@@ -240,7 +241,7 @@ var (
}
UnitExternalWiki = Unit{
UnitTypeExternalWiki,
TypeExternalWiki,
"repo.ext_wiki",
"/wiki",
"repo.ext_wiki.desc",
@@ -248,7 +249,7 @@ var (
}
UnitProjects = Unit{
UnitTypeProjects,
TypeProjects,
"repo.projects",
"/projects",
"repo.projects.desc",
@@ -256,20 +257,20 @@ var (
}
// Units contains all the units
Units = map[UnitType]Unit{
UnitTypeCode: UnitCode,
UnitTypeIssues: UnitIssues,
UnitTypeExternalTracker: UnitExternalTracker,
UnitTypePullRequests: UnitPullRequests,
UnitTypeReleases: UnitReleases,
UnitTypeWiki: UnitWiki,
UnitTypeExternalWiki: UnitExternalWiki,
UnitTypeProjects: UnitProjects,
Units = map[Type]Unit{
TypeCode: UnitCode,
TypeIssues: UnitIssues,
TypeExternalTracker: UnitExternalTracker,
TypePullRequests: UnitPullRequests,
TypeReleases: UnitReleases,
TypeWiki: UnitWiki,
TypeExternalWiki: UnitExternalWiki,
TypeProjects: UnitProjects,
}
)
// FindUnitTypes give the unit key name and return unit
func FindUnitTypes(nameKeys ...string) (res []UnitType) {
func FindUnitTypes(nameKeys ...string) (res []Type) {
for _, key := range nameKeys {
for t, u := range Units {
if strings.EqualFold(key, u.NameKey) {

View File

@@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
@@ -559,7 +560,7 @@ func (u *User) GetRepositories(listOpts db.ListOptions, names ...string) (err er
// GetRepositoryIDs returns repositories IDs where user owned and has unittypes
// Caller shall check that units is not globally disabled
func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetRepositoryIDs(units ...unit.Type) ([]int64, error) {
var ids []int64
sess := db.GetEngine(db.DefaultContext).Table("repository").Cols("repository.id")
@@ -574,7 +575,7 @@ func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) {
// GetActiveRepositoryIDs returns non-archived repositories IDs where user owned and has unittypes
// Caller shall check that units is not globally disabled
func (u *User) GetActiveRepositoryIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetActiveRepositoryIDs(units ...unit.Type) ([]int64, error) {
var ids []int64
sess := db.GetEngine(db.DefaultContext).Table("repository").Cols("repository.id")
@@ -591,7 +592,7 @@ func (u *User) GetActiveRepositoryIDs(units ...UnitType) ([]int64, error) {
// GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes
// Caller shall check that units is not globally disabled
func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetOrgRepositoryIDs(units ...unit.Type) ([]int64, error) {
var ids []int64
if err := db.GetEngine(db.DefaultContext).Table("repository").
@@ -612,7 +613,7 @@ func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
// GetActiveOrgRepositoryIDs returns non-archived repositories IDs where user's team owned and has unittypes
// Caller shall check that units is not globally disabled
func (u *User) GetActiveOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetActiveOrgRepositoryIDs(units ...unit.Type) ([]int64, error) {
var ids []int64
if err := db.GetEngine(db.DefaultContext).Table("repository").
@@ -634,7 +635,7 @@ func (u *User) GetActiveOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
// GetAccessRepoIDs returns all repositories IDs where user's or user is a team member organizations
// Caller shall check that units is not globally disabled
func (u *User) GetAccessRepoIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetAccessRepoIDs(units ...unit.Type) ([]int64, error) {
ids, err := u.GetRepositoryIDs(units...)
if err != nil {
return nil, err
@@ -648,7 +649,7 @@ func (u *User) GetAccessRepoIDs(units ...UnitType) ([]int64, error) {
// GetActiveAccessRepoIDs returns all non-archived repositories IDs where user's or user is a team member organizations
// Caller shall check that units is not globally disabled
func (u *User) GetActiveAccessRepoIDs(units ...UnitType) ([]int64, error) {
func (u *User) GetActiveAccessRepoIDs(units ...unit.Type) ([]int64, error) {
ids, err := u.GetActiveRepositoryIDs(units...)
if err != nil {
return nil, err