Skip to content

Commit cde95f9

Browse files
davidsvantessonlafriks
authored andcommitted
Rewrite existing repo units if setting is not included in api body (#7763)
* Rewrite existing repo units if setting is not included in api body Signed-off-by: David Svantesson <[email protected]> * else-if on one row
1 parent 2ed21e7 commit cde95f9

File tree

1 file changed

+86
-73
lines changed

1 file changed

+86
-73
lines changed

routers/api/v1/repo/repo.go

+86-73
Original file line numberDiff line numberDiff line change
@@ -651,89 +651,102 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
651651
})
652652
}
653653

654-
if opts.HasIssues != nil {
655-
if *opts.HasIssues {
656-
// We don't currently allow setting individual issue settings through the API,
657-
// only can enable/disable issues, so when enabling issues,
658-
// we either get the existing config which means it was already enabled,
659-
// or create a new config since it doesn't exist.
660-
unit, err := repo.GetUnit(models.UnitTypeIssues)
661-
var config *models.IssuesConfig
662-
if err != nil {
663-
// Unit type doesn't exist so we make a new config file with default values
664-
config = &models.IssuesConfig{
665-
EnableTimetracker: true,
666-
AllowOnlyContributorsToTrackTime: true,
667-
EnableDependencies: true,
668-
}
669-
} else {
670-
config = unit.IssuesConfig()
654+
if opts.HasIssues == nil {
655+
// If HasIssues setting not touched, rewrite existing repo unit
656+
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
657+
units = append(units, *unit)
658+
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
659+
units = append(units, *unit)
660+
}
661+
} else if *opts.HasIssues {
662+
// We don't currently allow setting individual issue settings through the API,
663+
// only can enable/disable issues, so when enabling issues,
664+
// we either get the existing config which means it was already enabled,
665+
// or create a new config since it doesn't exist.
666+
unit, err := repo.GetUnit(models.UnitTypeIssues)
667+
var config *models.IssuesConfig
668+
if err != nil {
669+
// Unit type doesn't exist so we make a new config file with default values
670+
config = &models.IssuesConfig{
671+
EnableTimetracker: true,
672+
AllowOnlyContributorsToTrackTime: true,
673+
EnableDependencies: true,
671674
}
672-
units = append(units, models.RepoUnit{
673-
RepoID: repo.ID,
674-
Type: models.UnitTypeIssues,
675-
Config: config,
676-
})
675+
} else {
676+
config = unit.IssuesConfig()
677677
}
678+
units = append(units, models.RepoUnit{
679+
RepoID: repo.ID,
680+
Type: models.UnitTypeIssues,
681+
Config: config,
682+
})
678683
}
679684

680-
if opts.HasWiki != nil {
681-
if *opts.HasWiki {
682-
// We don't currently allow setting individual wiki settings through the API,
683-
// only can enable/disable the wiki, so when enabling the wiki,
684-
// we either get the existing config which means it was already enabled,
685-
// or create a new config since it doesn't exist.
686-
config := &models.UnitConfig{}
687-
units = append(units, models.RepoUnit{
688-
RepoID: repo.ID,
689-
Type: models.UnitTypeWiki,
690-
Config: config,
691-
})
685+
if opts.HasWiki == nil {
686+
// If HasWiki setting not touched, rewrite existing repo unit
687+
if unit, err := repo.GetUnit(models.UnitTypeWiki); err == nil {
688+
units = append(units, *unit)
689+
} else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil {
690+
units = append(units, *unit)
692691
}
692+
} else if *opts.HasWiki {
693+
// We don't currently allow setting individual wiki settings through the API,
694+
// only can enable/disable the wiki, so when enabling the wiki,
695+
// we either get the existing config which means it was already enabled,
696+
// or create a new config since it doesn't exist.
697+
config := &models.UnitConfig{}
698+
units = append(units, models.RepoUnit{
699+
RepoID: repo.ID,
700+
Type: models.UnitTypeWiki,
701+
Config: config,
702+
})
693703
}
694704

695-
if opts.HasPullRequests != nil {
696-
if *opts.HasPullRequests {
697-
// We do allow setting individual PR settings through the API, so
698-
// we get the config settings and then set them
699-
// if those settings were provided in the opts.
700-
unit, err := repo.GetUnit(models.UnitTypePullRequests)
701-
var config *models.PullRequestsConfig
702-
if err != nil {
703-
// Unit type doesn't exist so we make a new config file with default values
704-
config = &models.PullRequestsConfig{
705-
IgnoreWhitespaceConflicts: false,
706-
AllowMerge: true,
707-
AllowRebase: true,
708-
AllowRebaseMerge: true,
709-
AllowSquash: true,
710-
}
711-
} else {
712-
config = unit.PullRequestsConfig()
713-
}
714-
715-
if opts.IgnoreWhitespaceConflicts != nil {
716-
config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
717-
}
718-
if opts.AllowMerge != nil {
719-
config.AllowMerge = *opts.AllowMerge
720-
}
721-
if opts.AllowRebase != nil {
722-
config.AllowRebase = *opts.AllowRebase
723-
}
724-
if opts.AllowRebaseMerge != nil {
725-
config.AllowRebaseMerge = *opts.AllowRebaseMerge
726-
}
727-
if opts.AllowSquash != nil {
728-
config.AllowSquash = *opts.AllowSquash
705+
if opts.HasPullRequests == nil {
706+
// If HasPullRequest setting not touched, rewrite existing repo unit
707+
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
708+
units = append(units, *unit)
709+
}
710+
} else if *opts.HasPullRequests {
711+
// We do allow setting individual PR settings through the API, so
712+
// we get the config settings and then set them
713+
// if those settings were provided in the opts.
714+
unit, err := repo.GetUnit(models.UnitTypePullRequests)
715+
var config *models.PullRequestsConfig
716+
if err != nil {
717+
// Unit type doesn't exist so we make a new config file with default values
718+
config = &models.PullRequestsConfig{
719+
IgnoreWhitespaceConflicts: false,
720+
AllowMerge: true,
721+
AllowRebase: true,
722+
AllowRebaseMerge: true,
723+
AllowSquash: true,
729724
}
725+
} else {
726+
config = unit.PullRequestsConfig()
727+
}
730728

731-
units = append(units, models.RepoUnit{
732-
RepoID: repo.ID,
733-
Type: models.UnitTypePullRequests,
734-
Config: config,
735-
})
729+
if opts.IgnoreWhitespaceConflicts != nil {
730+
config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
731+
}
732+
if opts.AllowMerge != nil {
733+
config.AllowMerge = *opts.AllowMerge
734+
}
735+
if opts.AllowRebase != nil {
736+
config.AllowRebase = *opts.AllowRebase
737+
}
738+
if opts.AllowRebaseMerge != nil {
739+
config.AllowRebaseMerge = *opts.AllowRebaseMerge
740+
}
741+
if opts.AllowSquash != nil {
742+
config.AllowSquash = *opts.AllowSquash
736743
}
744+
745+
units = append(units, models.RepoUnit{
746+
RepoID: repo.ID,
747+
Type: models.UnitTypePullRequests,
748+
Config: config,
749+
})
737750
}
738751

739752
if err := models.UpdateRepositoryUnits(repo, units); err != nil {

0 commit comments

Comments
 (0)