summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorDavid Svantesson <davidsvantesson@gmail.com>2020-01-17 08:34:37 +0100
committerLauris BH <lauris@nix.lv>2020-01-17 09:34:37 +0200
commit3c07d03c0388d3b86138572401281b51f2db9282 (patch)
tree06eecf8b818ee8721a5dbfdd688eb5f45e5bef51 /models
parent36943e56d66a2d711a6b0c27219ce91a3ddc020a (diff)
downloadgitea-3c07d03c0388d3b86138572401281b51f2db9282.tar.gz
gitea-3c07d03c0388d3b86138572401281b51f2db9282.zip
Add setting to set default and global disabled repository units. (#8788)
* Add possibility to global disable repo units. * Add Default Repo Unit app.ini setting. * Hide units * Hide disabled repo units * Minor fixes * Indicate disabled units in team settings. Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r--models/repo.go11
-rw-r--r--models/repo_unit.go13
-rw-r--r--models/unit.go79
-rw-r--r--models/user.go3
4 files changed, 102 insertions, 4 deletions
diff --git a/models/repo.go b/models/repo.go
index 2c9dafefc9..6c89dbcbbb 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -128,6 +128,7 @@ func loadRepoConfig() {
// NewRepoContext creates a new repository context
func NewRepoContext() {
loadRepoConfig()
+ loadUnitConfig()
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
}
@@ -393,6 +394,7 @@ func (repo *Repository) getUnits(e Engine) (err error) {
}
repo.Units, err = getUnitsByRepoID(e, repo.ID)
+ log.Trace("repo.Units: %-+v", repo.Units)
return err
}
@@ -1442,14 +1444,19 @@ func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
}
// UpdateRepositoryUnits updates a repository's units
-func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) {
+func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []UnitType) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
- if _, err = sess.Where("repo_id = ?", repo.ID).Delete(new(RepoUnit)); err != nil {
+ // Delete existing settings of units before adding again
+ for _, u := range units {
+ deleteUnitTypes = append(deleteUnitTypes, u.Type)
+ }
+
+ if _, err = sess.Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
return err
}
diff --git a/models/repo_unit.go b/models/repo_unit.go
index a6162a65e5..ec680c395e 100644
--- a/models/repo_unit.go
+++ b/models/repo_unit.go
@@ -170,5 +170,16 @@ func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
}
func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
- return units, e.Where("repo_id = ?", repoID).Find(&units)
+ var tmpUnits []*RepoUnit
+ if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
+ return nil, err
+ }
+
+ for _, u := range tmpUnits {
+ if !u.Type.UnitGlobalDisabled() {
+ units = append(units, u)
+ }
+ }
+
+ return units, nil
}
diff --git a/models/unit.go b/models/unit.go
index 9f5c8d3cbb..bd2e6b13a6 100644
--- a/models/unit.go
+++ b/models/unit.go
@@ -9,6 +9,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
)
// UnitType is Unit's Type
@@ -78,13 +79,89 @@ var (
UnitTypeWiki,
}
+ // NotAllowedDefaultRepoUnits contains units that can't be default
+ NotAllowedDefaultRepoUnits = []UnitType{
+ UnitTypeExternalWiki,
+ UnitTypeExternalTracker,
+ }
+
// MustRepoUnits contains the units could not be disabled currently
MustRepoUnits = []UnitType{
UnitTypeCode,
UnitTypeReleases,
}
+
+ // DisabledRepoUnits contains the units that have been globally disabled
+ DisabledRepoUnits = []UnitType{}
)
+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))
+ copy(DefaultRepoUnits, MustRepoUnits)
+ for _, defaultU := range setDefaultRepoUnits {
+ if !defaultU.CanBeDefault() {
+ log.Warn("Not allowed as default unit: %s", defaultU.String())
+ continue
+ }
+ // MustRepoUnits already added
+ if defaultU.CanDisable() {
+ DefaultRepoUnits = append(DefaultRepoUnits, defaultU)
+ }
+ }
+ }
+
+ DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...)
+ // Check that must units are not disabled
+ for i, disabledU := range DisabledRepoUnits {
+ if !disabledU.CanDisable() {
+ log.Warn("Not allowed to global disable unit %s", disabledU.String())
+ DisabledRepoUnits = append(DisabledRepoUnits[:i], DisabledRepoUnits[i+1:]...)
+ }
+ }
+ // Remove disabled units from default units
+ for _, disabledU := range DisabledRepoUnits {
+ for i, defaultU := range DefaultRepoUnits {
+ if defaultU == disabledU {
+ DefaultRepoUnits = append(DefaultRepoUnits[:i], DefaultRepoUnits[i+1:]...)
+ }
+ }
+ }
+}
+
+// UnitGlobalDisabled checks if unit type is global disabled
+func (u UnitType) UnitGlobalDisabled() bool {
+ for _, ud := range DisabledRepoUnits {
+ if u == ud {
+ return true
+ }
+ }
+ return false
+}
+
+// CanDisable checks if this unit type can be disabled.
+func (u *UnitType) CanDisable() bool {
+ for _, mu := range MustRepoUnits {
+ if *u == mu {
+ return false
+ }
+ }
+ return true
+}
+
+// CanBeDefault checks if the unit type can be a default repo unit
+func (u *UnitType) CanBeDefault() bool {
+ for _, nadU := range NotAllowedDefaultRepoUnits {
+ if *u == nadU {
+ return false
+ }
+ }
+ return true
+}
+
// Unit is a section of one repository
type Unit struct {
Type UnitType
@@ -96,7 +173,7 @@ type Unit struct {
// CanDisable returns if this unit could be disabled.
func (u *Unit) CanDisable() bool {
- return true
+ return u.Type.CanDisable()
}
// IsLessThan compares order of two units
diff --git a/models/user.go b/models/user.go
index d7129fb09a..4a4af3547a 100644
--- a/models/user.go
+++ b/models/user.go
@@ -622,6 +622,7 @@ func (u *User) GetRepositories(page, pageSize int) (err error) {
}
// 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) {
var ids []int64
@@ -636,6 +637,7 @@ func (u *User) GetRepositoryIDs(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) {
var ids []int64
@@ -656,6 +658,7 @@ func (u *User) GetOrgRepositoryIDs(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) {
ids, err := u.GetRepositoryIDs(units...)
if err != nil {