summaryrefslogtreecommitdiffstats
path: root/routers/api
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 /routers/api
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 'routers/api')
-rw-r--r--routers/api/v1/repo/repo.go140
1 files changed, 67 insertions, 73 deletions
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 9ae0c4af4e..a13f6ebe0d 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -757,25 +757,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 {
-
+ 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")
@@ -797,7 +782,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle,
},
})
- } else {
+ deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
+ } else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() {
// Default to built-in tracker
var config *models.IssuesConfig
@@ -823,19 +809,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 {
-
+ 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")
@@ -850,64 +836,72 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL,
},
})
- } else {
+ 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 {
- // 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
}