Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to set default and global disabled repository units. #8788

Merged
merged 16 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hide disabled repo units
  • Loading branch information
davidsvantesson committed Jan 12, 2020
commit ddd01f6a21b6db0a3319f94977be0f073e29a44a
51 changes: 8 additions & 43 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,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
}

Expand Down Expand Up @@ -814,47 +815,6 @@ func (repo *Repository) CanUserDelete(user *User) (bool, error) {
return false, nil
}

// allowEnableUnit check if unit can't be enabled due to being global disabled
func (repo *Repository) allowEnableUnit(ut UnitType, isAdmin bool) bool {
if isAdmin {
// Site admin can activate even if global disabled
return true
}
if repo.UnitEnabled(ut) {
// If unit already active, it can be kept active
return true
}
if ut.UnitGlobalDisabled() {
return false
}
return true
}

// AllowEnableInternalWiki returns true if the enabling internal wiki can be allowed (not global disabled)
func (repo *Repository) AllowEnableInternalWiki(isAdmin bool) bool {
return repo.allowEnableUnit(UnitTypeWiki, isAdmin)
}

// AllowEnableExternalWiki returns true if the enabling external wiki can be allowed (not global disabled)
func (repo *Repository) AllowEnableExternalWiki(isAdmin bool) bool {
return repo.allowEnableUnit(UnitTypeExternalWiki, isAdmin)
}

// AllowEnableInternalIssues returns true if the enabling internal issues can be allowed (not global disabled)
func (repo *Repository) AllowEnableInternalIssues(isAdmin bool) bool {
return repo.allowEnableUnit(UnitTypeIssues, isAdmin)
}

// AllowEnableExternalTracker returns true if the enabling internal wiki can be allowed (not global disabled)
func (repo *Repository) AllowEnableExternalTracker(isAdmin bool) bool {
return repo.allowEnableUnit(UnitTypeExternalTracker, isAdmin)
}

// AllowEnablePulls returns true if the enabling internal wiki can be allowed (not global disabled and possible for repo)
func (repo *Repository) AllowEnablePulls(isAdmin bool) bool {
return repo.CanEnablePulls() && repo.allowEnableUnit(UnitTypePullRequests, isAdmin)
}

// CanEnablePulls returns true if repository meets the requirements of accepting pulls.
func (repo *Repository) CanEnablePulls() bool {
return !repo.IsMirror && !repo.IsEmpty
Expand Down Expand Up @@ -1777,14 +1737,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
}

Expand Down
13 changes: 12 additions & 1 deletion models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 2 additions & 7 deletions models/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func loadUnitConfig() {
}
}

// IsUnitGlobalDisabled checks if unit type is global disabled
func IsUnitGlobalDisabled(u UnitType) bool {
// UnitGlobalDisabled checks if unit type is global disabled
func (u UnitType) UnitGlobalDisabled() bool {
for _, ud := range DisabledRepoUnits {
if u == ud {
return true
Expand All @@ -142,11 +142,6 @@ func IsUnitGlobalDisabled(u UnitType) bool {
return false
}

// UnitGlobalDisabled checks if unit type is global disabled
func (u *UnitType) UnitGlobalDisabled() bool {
return IsUnitGlobalDisabled(*u)
}

// CanDisable checks if this unit type can be disabled.
func (u *UnitType) CanDisable() bool {
for _, mu := range MustRepoUnits {
Expand Down
140 changes: 67 additions & 73 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,25 +726,10 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
repo := ctx.Repo.Repository

var units []models.RepoUnit
var deleteUnitTypes []models.UnitType

for _, tp := range models.MustRepoUnits {
units = append(units, models.RepoUnit{
RepoID: repo.ID,
Type: tp,
Config: new(models.UnitConfig),
})
}

if opts.HasIssues == nil {
// If HasIssues setting not touched, rewrite existing repo unit
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
units = append(units, *unit)
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
units = append(units, *unit)
}
} else if *opts.HasIssues {
if opts.ExternalTracker != nil && repo.AllowEnableExternalTracker(ctx.User.IsAdmin) {

if opts.HasIssues != nil {
if *opts.HasIssues && opts.ExternalTracker != nil && !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
// Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
err := fmt.Errorf("External tracker URL not valid")
Expand All @@ -766,7 +751,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle,
},
})
} else if repo.AllowEnableInternalIssues(ctx.User.IsAdmin) {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
} else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() {
// Default to built-in tracker
var config *models.IssuesConfig

Expand All @@ -792,19 +778,19 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
Type: models.UnitTypeIssues,
Config: config,
})
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
} else if !*opts.HasIssues {
if !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
}
if !models.UnitTypeIssues.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
}
}
}

if opts.HasWiki == nil {
// If HasWiki setting not touched, rewrite existing repo unit
if unit, err := repo.GetUnit(models.UnitTypeWiki); err == nil {
units = append(units, *unit)
} else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil {
units = append(units, *unit)
}
} else if *opts.HasWiki {
if opts.ExternalWiki != nil && repo.AllowEnableExternalWiki(ctx.User.IsAdmin) {

if opts.HasWiki != nil {
if *opts.HasWiki && opts.ExternalWiki != nil && !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
// Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
err := fmt.Errorf("External wiki URL not valid")
Expand All @@ -819,64 +805,72 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL,
},
})
} else if repo.AllowEnableInternalWiki(ctx.User.IsAdmin) {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
} else if *opts.HasWiki && opts.ExternalWiki == nil && !models.UnitTypeWiki.UnitGlobalDisabled() {
config := &models.UnitConfig{}
units = append(units, models.RepoUnit{
RepoID: repo.ID,
Type: models.UnitTypeWiki,
Config: config,
})
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
} else if !*opts.HasWiki {
if !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
}
if !models.UnitTypeWiki.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
}
}
}

if opts.HasPullRequests == nil {
// If HasPullRequest setting not touched, rewrite existing repo unit
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
units = append(units, *unit)
}
} else if *opts.HasPullRequests && repo.AllowEnablePulls(ctx.User.IsAdmin) {
// We do allow setting individual PR settings through the API, so
// we get the config settings and then set them
// if those settings were provided in the opts.
unit, err := repo.GetUnit(models.UnitTypePullRequests)
var config *models.PullRequestsConfig
if err != nil {
// Unit type doesn't exist so we make a new config file with default values
config = &models.PullRequestsConfig{
IgnoreWhitespaceConflicts: false,
AllowMerge: true,
AllowRebase: true,
AllowRebaseMerge: true,
AllowSquash: true,
if opts.HasPullRequests != nil {
if *opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
// We do allow setting individual PR settings through the API, so
// we get the config settings and then set them
// if those settings were provided in the opts.
unit, err := repo.GetUnit(models.UnitTypePullRequests)
var config *models.PullRequestsConfig
if err != nil {
// Unit type doesn't exist so we make a new config file with default values
config = &models.PullRequestsConfig{
IgnoreWhitespaceConflicts: false,
AllowMerge: true,
AllowRebase: true,
AllowRebaseMerge: true,
AllowSquash: true,
}
} else {
config = unit.PullRequestsConfig()
}
} else {
config = unit.PullRequestsConfig()
}

if opts.IgnoreWhitespaceConflicts != nil {
config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
}
if opts.AllowMerge != nil {
config.AllowMerge = *opts.AllowMerge
}
if opts.AllowRebase != nil {
config.AllowRebase = *opts.AllowRebase
}
if opts.AllowRebaseMerge != nil {
config.AllowRebaseMerge = *opts.AllowRebaseMerge
}
if opts.AllowSquash != nil {
config.AllowSquash = *opts.AllowSquash
}
if opts.IgnoreWhitespaceConflicts != nil {
config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
}
if opts.AllowMerge != nil {
config.AllowMerge = *opts.AllowMerge
}
if opts.AllowRebase != nil {
config.AllowRebase = *opts.AllowRebase
}
if opts.AllowRebaseMerge != nil {
config.AllowRebaseMerge = *opts.AllowRebaseMerge
}
if opts.AllowSquash != nil {
config.AllowSquash = *opts.AllowSquash
}

units = append(units, models.RepoUnit{
RepoID: repo.ID,
Type: models.UnitTypePullRequests,
Config: config,
})
units = append(units, models.RepoUnit{
RepoID: repo.ID,
Type: models.UnitTypePullRequests,
Config: config,
})
} else if !*opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests)
}
}

if err := models.UpdateRepositoryUnits(repo, units); err != nil {
if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
return err
}
Expand Down
Loading