2017-02-04 18:53:46 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
2018-11-10 22:45:32 +03:00
import (
2019-04-22 23:40:51 +03:00
"fmt"
2018-11-10 22:45:32 +03:00
"strings"
2019-04-22 23:40:51 +03:00
"code.gitea.io/gitea/modules/log"
2020-01-17 10:34:37 +03:00
"code.gitea.io/gitea/modules/setting"
2018-11-10 22:45:32 +03:00
)
2017-02-04 18:53:46 +03:00
// UnitType is Unit's Type
type UnitType int
// Enumerate all the unit types
const (
UnitTypeCode UnitType = iota + 1 // 1 code
UnitTypeIssues // 2 issues
UnitTypePullRequests // 3 PRs
2017-07-17 05:04:43 +03:00
UnitTypeReleases // 4 Releases
UnitTypeWiki // 5 Wiki
UnitTypeExternalWiki // 6 ExternalWiki
UnitTypeExternalTracker // 7 ExternalTracker
2020-08-17 06:07:38 +03:00
UnitTypeProjects // 8 Kanban board
2017-02-04 18:53:46 +03:00
)
2019-04-25 12:00:34 +03:00
// Value returns integer value for unit type
func ( u UnitType ) Value ( ) int {
return int ( u )
}
2019-04-22 23:40:51 +03:00
func ( u UnitType ) 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"
2020-08-17 06:07:38 +03:00
case UnitTypeProjects :
return "UnitTypeProjects"
2019-04-22 23:40:51 +03:00
}
return fmt . Sprintf ( "Unknown UnitType %d" , u )
}
// ColorFormat provides a ColorFormatted version of this UnitType
func ( u UnitType ) ColorFormat ( s fmt . State ) {
log . ColorFprintf ( s , "%d:%s" ,
log . NewColoredIDValue ( u ) ,
u )
}
2017-05-18 17:54:24 +03:00
var (
2019-05-30 18:09:05 +03:00
// AllRepoUnitTypes contains all the unit types
AllRepoUnitTypes = [ ] UnitType {
2017-05-18 17:54:24 +03:00
UnitTypeCode ,
UnitTypeIssues ,
UnitTypePullRequests ,
UnitTypeReleases ,
UnitTypeWiki ,
UnitTypeExternalWiki ,
UnitTypeExternalTracker ,
2020-08-17 06:07:38 +03:00
UnitTypeProjects ,
2017-05-18 17:54:24 +03:00
}
2019-05-30 18:09:05 +03:00
// DefaultRepoUnits contains the default unit types
DefaultRepoUnits = [ ] UnitType {
2017-05-18 17:54:24 +03:00
UnitTypeCode ,
UnitTypeIssues ,
UnitTypePullRequests ,
UnitTypeReleases ,
UnitTypeWiki ,
2020-08-17 06:07:38 +03:00
UnitTypeProjects ,
2017-05-18 17:54:24 +03:00
}
2020-01-17 10:34:37 +03:00
// NotAllowedDefaultRepoUnits contains units that can't be default
NotAllowedDefaultRepoUnits = [ ] UnitType {
UnitTypeExternalWiki ,
UnitTypeExternalTracker ,
}
2017-07-17 05:04:43 +03:00
// MustRepoUnits contains the units could not be disabled currently
2017-05-18 17:54:24 +03:00
MustRepoUnits = [ ] UnitType {
UnitTypeCode ,
UnitTypeReleases ,
}
2020-01-17 10:34:37 +03:00
// DisabledRepoUnits contains the units that have been globally disabled
DisabledRepoUnits = [ ] UnitType { }
2017-05-18 17:54:24 +03:00
)
2020-01-17 10:34:37 +03:00
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
}
2017-07-17 05:04:43 +03:00
// Unit is a section of one repository
2017-02-04 18:53:46 +03:00
type Unit struct {
Type UnitType
NameKey string
URI string
DescKey string
Idx int
}
2017-05-18 17:54:24 +03:00
// CanDisable returns if this unit could be disabled.
func ( u * Unit ) CanDisable ( ) bool {
2020-01-17 10:34:37 +03:00
return u . Type . CanDisable ( )
2017-05-18 17:54:24 +03:00
}
2017-10-01 16:50:56 +03:00
// 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 {
return false
}
return u . Idx < unit . Idx
}
2017-02-04 18:53:46 +03:00
// Enumerate all the units
var (
UnitCode = Unit {
UnitTypeCode ,
"repo.code" ,
"/" ,
2017-05-18 17:54:24 +03:00
"repo.code.desc" ,
2017-02-04 18:53:46 +03:00
0 ,
}
UnitIssues = Unit {
UnitTypeIssues ,
"repo.issues" ,
"/issues" ,
2017-05-18 17:54:24 +03:00
"repo.issues.desc" ,
2017-02-04 18:53:46 +03:00
1 ,
}
UnitExternalTracker = Unit {
UnitTypeExternalTracker ,
2017-05-18 17:54:24 +03:00
"repo.ext_issues" ,
2017-02-04 18:53:46 +03:00
"/issues" ,
2017-05-18 17:54:24 +03:00
"repo.ext_issues.desc" ,
2017-02-04 18:53:46 +03:00
1 ,
}
UnitPullRequests = Unit {
UnitTypePullRequests ,
"repo.pulls" ,
"/pulls" ,
2017-05-18 17:54:24 +03:00
"repo.pulls.desc" ,
2017-02-04 18:53:46 +03:00
2 ,
}
UnitReleases = Unit {
UnitTypeReleases ,
"repo.releases" ,
"/releases" ,
2017-05-18 17:54:24 +03:00
"repo.releases.desc" ,
2017-07-17 05:04:43 +03:00
3 ,
2017-02-04 18:53:46 +03:00
}
UnitWiki = Unit {
UnitTypeWiki ,
"repo.wiki" ,
"/wiki" ,
2017-05-18 17:54:24 +03:00
"repo.wiki.desc" ,
2017-07-17 05:04:43 +03:00
4 ,
2017-02-04 18:53:46 +03:00
}
UnitExternalWiki = Unit {
UnitTypeExternalWiki ,
2017-05-18 17:54:24 +03:00
"repo.ext_wiki" ,
2017-02-04 18:53:46 +03:00
"/wiki" ,
2017-05-18 17:54:24 +03:00
"repo.ext_wiki.desc" ,
2017-07-17 05:04:43 +03:00
4 ,
2017-02-04 18:53:46 +03:00
}
2020-08-17 06:07:38 +03:00
UnitProjects = Unit {
UnitTypeProjects ,
"repo.projects" ,
"/projects" ,
"repo.projects.desc" ,
5 ,
}
2017-02-04 18:53:46 +03:00
// Units contains all the units
Units = map [ UnitType ] Unit {
UnitTypeCode : UnitCode ,
UnitTypeIssues : UnitIssues ,
UnitTypeExternalTracker : UnitExternalTracker ,
UnitTypePullRequests : UnitPullRequests ,
UnitTypeReleases : UnitReleases ,
UnitTypeWiki : UnitWiki ,
UnitTypeExternalWiki : UnitExternalWiki ,
2020-08-17 06:07:38 +03:00
UnitTypeProjects : UnitProjects ,
2017-02-04 18:53:46 +03:00
}
)
2018-11-10 22:45:32 +03:00
// FindUnitTypes give the unit key name and return unit
func FindUnitTypes ( nameKeys ... string ) ( res [ ] UnitType ) {
for _ , key := range nameKeys {
for t , u := range Units {
if strings . EqualFold ( key , u . NameKey ) {
res = append ( res , t )
break
}
}
}
return
}