@@ -884,77 +884,44 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
884884 }
885885 }
886886
887- if opts .HasPullRequests != nil && ! unit_model .TypePullRequests .UnitGlobalDisabled () {
888- if * opts .HasPullRequests {
889- // We do allow setting individual PR settings through the API, so
890- // we get the config settings and then set them
891- // if those settings were provided in the opts.
887+ if ! unit_model .TypePullRequests .UnitGlobalDisabled () {
888+ mustDeletePullRequestUnit := opts .HasPullRequests != nil && ! * opts .HasPullRequests
889+ mustInsertPullRequestUnit := opts .HasPullRequests != nil && * opts .HasPullRequests
890+ if mustDeletePullRequestUnit {
891+ deleteUnitTypes = append (deleteUnitTypes , unit_model .TypePullRequests )
892+ } else {
893+ // We do allow setting individual PR settings through the API,
894+ // so we get the config settings and then set them if those settings were provided in the opts.
892895 unit , err := repo .GetUnit (ctx , unit_model .TypePullRequests )
893- var config * repo_model.PullRequestsConfig
894- if err != nil {
895- // Unit type doesn't exist so we make a new config file with default values
896- config = & repo_model.PullRequestsConfig {
897- IgnoreWhitespaceConflicts : false ,
898- AllowMerge : true ,
899- AllowRebase : true ,
900- AllowRebaseMerge : true ,
901- AllowSquash : true ,
902- AllowFastForwardOnly : true ,
903- AllowManualMerge : true ,
904- AutodetectManualMerge : false ,
905- AllowRebaseUpdate : true ,
906- DefaultDeleteBranchAfterMerge : false ,
907- DefaultMergeStyle : repo_model .MergeStyleMerge ,
908- DefaultAllowMaintainerEdit : false ,
909- }
910- } else {
911- config = unit .PullRequestsConfig ()
912- }
913-
914- if opts .IgnoreWhitespaceConflicts != nil {
915- config .IgnoreWhitespaceConflicts = * opts .IgnoreWhitespaceConflicts
916- }
917- if opts .AllowMerge != nil {
918- config .AllowMerge = * opts .AllowMerge
919- }
920- if opts .AllowRebase != nil {
921- config .AllowRebase = * opts .AllowRebase
922- }
923- if opts .AllowRebaseMerge != nil {
924- config .AllowRebaseMerge = * opts .AllowRebaseMerge
925- }
926- if opts .AllowSquash != nil {
927- config .AllowSquash = * opts .AllowSquash
928- }
929- if opts .AllowFastForwardOnly != nil {
930- config .AllowFastForwardOnly = * opts .AllowFastForwardOnly
931- }
932- if opts .AllowManualMerge != nil {
933- config .AllowManualMerge = * opts .AllowManualMerge
934- }
935- if opts .AutodetectManualMerge != nil {
936- config .AutodetectManualMerge = * opts .AutodetectManualMerge
937- }
938- if opts .AllowRebaseUpdate != nil {
939- config .AllowRebaseUpdate = * opts .AllowRebaseUpdate
940- }
941- if opts .DefaultDeleteBranchAfterMerge != nil {
942- config .DefaultDeleteBranchAfterMerge = * opts .DefaultDeleteBranchAfterMerge
943- }
944- if opts .DefaultMergeStyle != nil {
945- config .DefaultMergeStyle = repo_model .MergeStyle (* opts .DefaultMergeStyle )
896+ if err != nil && ! errors .Is (err , util .ErrNotExist ) {
897+ return err
946898 }
947- if opts .DefaultAllowMaintainerEdit != nil {
948- config .DefaultAllowMaintainerEdit = * opts .DefaultAllowMaintainerEdit
899+ if unit == nil {
900+ // Unit doesn't exist yet but is being enabled, create with defaults
901+ unit = new (repo_model.DefaultPullRequestsUnit (repo.ID ))
949902 }
950903
951- units = append (units , repo_model.RepoUnit {
952- RepoID : repo .ID ,
953- Type : unit_model .TypePullRequests ,
954- Config : config ,
955- })
956- } else {
957- deleteUnitTypes = append (deleteUnitTypes , unit_model .TypePullRequests )
904+ changed := new (false )
905+ config := unit .PullRequestsConfig ()
906+ optional .AssignPtrValue (changed , & config .IgnoreWhitespaceConflicts , opts .IgnoreWhitespaceConflicts )
907+ optional .AssignPtrValue (changed , & config .AllowMerge , opts .AllowMerge )
908+ optional .AssignPtrValue (changed , & config .AllowRebase , opts .AllowRebase )
909+ optional .AssignPtrValue (changed , & config .AllowRebaseMerge , opts .AllowRebaseMerge )
910+ optional .AssignPtrValue (changed , & config .AllowSquash , opts .AllowSquash )
911+ optional .AssignPtrValue (changed , & config .AllowFastForwardOnly , opts .AllowFastForwardOnly )
912+ optional .AssignPtrValue (changed , & config .AllowManualMerge , opts .AllowManualMerge )
913+ optional .AssignPtrValue (changed , & config .AutodetectManualMerge , opts .AutodetectManualMerge )
914+ optional .AssignPtrValue (changed , & config .AllowRebaseUpdate , opts .AllowRebaseUpdate )
915+ optional .AssignPtrValue (changed , & config .DefaultDeleteBranchAfterMerge , opts .DefaultDeleteBranchAfterMerge )
916+ optional .AssignPtrValue (changed , & config .DefaultAllowMaintainerEdit , opts .DefaultAllowMaintainerEdit )
917+ optional .AssignPtrString (changed , & config .DefaultMergeStyle , opts .DefaultMergeStyle )
918+ if * changed || mustInsertPullRequestUnit {
919+ units = append (units , repo_model.RepoUnit {
920+ RepoID : repo .ID ,
921+ Type : unit_model .TypePullRequests ,
922+ Config : config ,
923+ })
924+ }
958925 }
959926 }
960927
0 commit comments