Skip to content

Commit 349c594

Browse files
committed
Refactor: make if statements more redable and have less checks as not needed
1 parent 86aafea commit 349c594

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

routers/api/v1/repo/repo.go

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
772772
var units []repo_model.RepoUnit
773773
var deleteUnitTypes []unit_model.Type
774774

775-
currHasIssues := repo.UnitEnabled(ctx, unit_model.TypeIssues)
776-
newHasIssues := currHasIssues
777775
if opts.HasIssues != nil {
778-
newHasIssues = *opts.HasIssues
779-
}
780-
if currHasIssues || newHasIssues {
781-
if newHasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
776+
if *opts.HasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
782777
// Check that values are valid
783778
if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
784779
err := errors.New("External tracker URL not valid")
@@ -802,7 +797,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
802797
},
803798
})
804799
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues)
805-
} else if newHasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
800+
} else if *opts.HasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
806801
// Default to built-in tracker
807802
var config *repo_model.IssuesConfig
808803

@@ -829,23 +824,15 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
829824
Config: config,
830825
})
831826
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
832-
} else if !newHasIssues {
833-
if !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
834-
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
835-
}
836-
if !unit_model.TypeIssues.UnitGlobalDisabled() {
837-
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues)
838-
}
827+
} else if !*opts.HasIssues && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
828+
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
829+
} else if !*opts.HasIssues && !unit_model.TypeIssues.UnitGlobalDisabled() {
830+
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues)
839831
}
840832
}
841833

842-
currHasWiki := repo.UnitEnabled(ctx, unit_model.TypeWiki)
843-
newHasWiki := currHasWiki
844834
if opts.HasWiki != nil {
845-
newHasWiki = *opts.HasWiki
846-
}
847-
if currHasWiki || newHasWiki {
848-
if newHasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
835+
if *opts.HasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
849836
// Check that values are valid
850837
if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
851838
err := errors.New("External wiki URL not valid")
@@ -861,31 +848,23 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
861848
},
862849
})
863850
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki)
864-
} else if newHasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
851+
} else if *opts.HasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
865852
config := &repo_model.UnitConfig{}
866853
units = append(units, repo_model.RepoUnit{
867854
RepoID: repo.ID,
868855
Type: unit_model.TypeWiki,
869856
Config: config,
870857
})
871858
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
872-
} else if !newHasWiki {
873-
if !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
874-
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
875-
}
876-
if !unit_model.TypeWiki.UnitGlobalDisabled() {
877-
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki)
878-
}
859+
} else if !*opts.HasWiki && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
860+
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
861+
} else if !*opts.HasWiki && !unit_model.TypeWiki.UnitGlobalDisabled() {
862+
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki)
879863
}
880864
}
881865

882-
currHasPullRequests := repo.UnitEnabled(ctx, unit_model.TypePullRequests)
883-
newHasPullRequests := currHasPullRequests
884-
if opts.HasPullRequests != nil {
885-
newHasPullRequests = *opts.HasPullRequests
886-
}
887-
if currHasPullRequests || newHasPullRequests {
888-
if newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
866+
if opts.HasPullRequests != nil && !unit_model.TypePullRequests.UnitGlobalDisabled() {
867+
if *opts.HasPullRequests {
889868
// We do allow setting individual PR settings through the API, so
890869
// we get the config settings and then set them
891870
// if those settings were provided in the opts.
@@ -953,18 +932,13 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
953932
Type: unit_model.TypePullRequests,
954933
Config: config,
955934
})
956-
} else if !newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
935+
} else {
957936
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePullRequests)
958937
}
959938
}
960939

961-
currHasProjects := repo.UnitEnabled(ctx, unit_model.TypeProjects)
962-
newHasProjects := currHasProjects
963-
if opts.HasProjects != nil {
964-
newHasProjects = *opts.HasProjects
965-
}
966-
if currHasProjects || newHasProjects {
967-
if newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
940+
if opts.HasProjects != nil && !unit_model.TypeProjects.UnitGlobalDisabled() {
941+
if *opts.HasProjects {
968942
unit, err := repo.GetUnit(ctx, unit_model.TypeProjects)
969943
var config *repo_model.ProjectsConfig
970944
if err != nil {
@@ -984,7 +958,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
984958
Type: unit_model.TypeProjects,
985959
Config: config,
986960
})
987-
} else if !newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
961+
} else {
988962
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeProjects)
989963
}
990964
}

0 commit comments

Comments
 (0)