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 <davidsvantesson@gmail.com> * else-if on one row
This commit is contained in:
		
							parent
							
								
									2ed21e7e09
								
							
						
					
					
						commit
						cde95f9923
					
				
					 1 changed files with 89 additions and 76 deletions
				
			
		|  | @ -651,89 +651,102 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { | |||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	if opts.HasIssues != nil { | ||||
| 		if *opts.HasIssues { | ||||
| 			// We don't currently allow setting individual issue settings through the API,
 | ||||
| 			// only can enable/disable issues, so when enabling issues,
 | ||||
| 			// we either get the existing config which means it was already enabled,
 | ||||
| 			// or create a new config since it doesn't exist.
 | ||||
| 			unit, err := repo.GetUnit(models.UnitTypeIssues) | ||||
| 			var config *models.IssuesConfig | ||||
| 			if err != nil { | ||||
| 				// Unit type doesn't exist so we make a new config file with default values
 | ||||
| 				config = &models.IssuesConfig{ | ||||
| 					EnableTimetracker:                true, | ||||
| 					AllowOnlyContributorsToTrackTime: true, | ||||
| 					EnableDependencies:               true, | ||||
| 				} | ||||
| 			} else { | ||||
| 				config = unit.IssuesConfig() | ||||
| 			} | ||||
| 			units = append(units, models.RepoUnit{ | ||||
| 				RepoID: repo.ID, | ||||
| 				Type:   models.UnitTypeIssues, | ||||
| 				Config: config, | ||||
| 			}) | ||||
| 	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 { | ||||
| 		// We don't currently allow setting individual issue settings through the API,
 | ||||
| 		// only can enable/disable issues, so when enabling issues,
 | ||||
| 		// we either get the existing config which means it was already enabled,
 | ||||
| 		// or create a new config since it doesn't exist.
 | ||||
| 		unit, err := repo.GetUnit(models.UnitTypeIssues) | ||||
| 		var config *models.IssuesConfig | ||||
| 		if err != nil { | ||||
| 			// Unit type doesn't exist so we make a new config file with default values
 | ||||
| 			config = &models.IssuesConfig{ | ||||
| 				EnableTimetracker:                true, | ||||
| 				AllowOnlyContributorsToTrackTime: true, | ||||
| 				EnableDependencies:               true, | ||||
| 			} | ||||
| 		} else { | ||||
| 			config = unit.IssuesConfig() | ||||
| 		} | ||||
| 		units = append(units, models.RepoUnit{ | ||||
| 			RepoID: repo.ID, | ||||
| 			Type:   models.UnitTypeIssues, | ||||
| 			Config: config, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	if opts.HasWiki != nil { | ||||
| 		if *opts.HasWiki { | ||||
| 			// We don't currently allow setting individual wiki settings through the API,
 | ||||
| 			// only can enable/disable the wiki, so when enabling the wiki,
 | ||||
| 			// we either get the existing config which means it was already enabled,
 | ||||
| 			// or create a new config since it doesn't exist.
 | ||||
| 			config := &models.UnitConfig{} | ||||
| 			units = append(units, models.RepoUnit{ | ||||
| 				RepoID: repo.ID, | ||||
| 				Type:   models.UnitTypeWiki, | ||||
| 				Config: config, | ||||
| 			}) | ||||
| 	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 { | ||||
| 		// We don't currently allow setting individual wiki settings through the API,
 | ||||
| 		// only can enable/disable the wiki, so when enabling the wiki,
 | ||||
| 		// we either get the existing config which means it was already enabled,
 | ||||
| 		// or create a new config since it doesn't exist.
 | ||||
| 		config := &models.UnitConfig{} | ||||
| 		units = append(units, models.RepoUnit{ | ||||
| 			RepoID: repo.ID, | ||||
| 			Type:   models.UnitTypeWiki, | ||||
| 			Config: config, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	if opts.HasPullRequests != nil { | ||||
| 		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, | ||||
| 				} | ||||
| 			} 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 | ||||
| 			} | ||||
| 
 | ||||
| 			units = append(units, models.RepoUnit{ | ||||
| 				RepoID: repo.ID, | ||||
| 				Type:   models.UnitTypePullRequests, | ||||
| 				Config: config, | ||||
| 			}) | ||||
| 	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, | ||||
| 			} | ||||
| 		} 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 | ||||
| 		} | ||||
| 
 | ||||
| 		units = append(units, models.RepoUnit{ | ||||
| 			RepoID: repo.ID, | ||||
| 			Type:   models.UnitTypePullRequests, | ||||
| 			Config: config, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	if err := models.UpdateRepositoryUnits(repo, units); err != nil { | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue