From f4d27498bdc615605e14f783501e1d1256cd8d22 Mon Sep 17 00:00:00 2001 From: parnic Date: Sat, 27 Mar 2021 09:55:40 -0500 Subject: [PATCH] Add DefaultMergeStyle option to repository (#14789) Fixes #12293 --- models/repo.go | 2 +- models/repo_unit.go | 10 +++++++++ modules/convert/repository.go | 3 +++ modules/forms/repo_form.go | 1 + modules/structs/repo.go | 3 +++ options/locale/locale_en-US.ini | 1 + routers/api/v1/repo/repo.go | 4 ++++ routers/repo/issue.go | 5 ++++- routers/repo/setting.go | 1 + templates/repo/settings/options.tmpl | 33 ++++++++++++++++++++++++++++ templates/swagger/v1_json.tmpl | 9 ++++++++ 11 files changed, 70 insertions(+), 2 deletions(-) diff --git a/models/repo.go b/models/repo.go index 329e63cc42..c807916ddc 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1086,7 +1086,7 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO units = append(units, RepoUnit{ RepoID: repo.ID, Type: tp, - Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true}, + Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: MergeStyleMerge}, }) } else { units = append(units, RepoUnit{ diff --git a/models/repo_unit.go b/models/repo_unit.go index 7702697ffa..1d54579a6e 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -102,6 +102,7 @@ type PullRequestsConfig struct { AllowSquash bool AllowManualMerge bool AutodetectManualMerge bool + DefaultMergeStyle MergeStyle } // FromDB fills up a PullRequestsConfig from serialized format. @@ -125,6 +126,15 @@ func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool { mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge } +// GetDefaultMergeStyle returns the default merge style for this pull request +func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle { + if len(cfg.DefaultMergeStyle) != 0 { + return cfg.DefaultMergeStyle + } + + return MergeStyleMerge +} + // AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int { count := 0 diff --git a/modules/convert/repository.go b/modules/convert/repository.go index 813201ca68..63a22bb04e 100644 --- a/modules/convert/repository.go +++ b/modules/convert/repository.go @@ -71,6 +71,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) allowRebase := false allowRebaseMerge := false allowSquash := false + defaultMergeStyle := models.MergeStyleMerge if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil { config := unit.PullRequestsConfig() hasPullRequests = true @@ -79,6 +80,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) allowRebase = config.AllowRebase allowRebaseMerge = config.AllowRebaseMerge allowSquash = config.AllowSquash + defaultMergeStyle = config.GetDefaultMergeStyle() } hasProjects := false if _, err := repo.GetUnit(models.UnitTypeProjects); err == nil { @@ -139,6 +141,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) AllowRebase: allowRebase, AllowRebaseMerge: allowRebaseMerge, AllowSquash: allowSquash, + DefaultMergeStyle: string(defaultMergeStyle), AvatarURL: repo.AvatarLink(), Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, MirrorInterval: mirrorInterval, diff --git a/modules/forms/repo_form.go b/modules/forms/repo_form.go index 6c7c9bea13..d9eb06d194 100644 --- a/modules/forms/repo_form.go +++ b/modules/forms/repo_form.go @@ -140,6 +140,7 @@ type RepoSettingForm struct { PullsAllowRebaseMerge bool PullsAllowSquash bool PullsAllowManualMerge bool + PullsDefaultMergeStyle string EnableAutodetectManualMerge bool EnableTimetracker bool AllowOnlyContributorsToTrackTime bool diff --git a/modules/structs/repo.go b/modules/structs/repo.go index c47700cd00..c23bd1033f 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -89,6 +89,7 @@ type Repository struct { AllowRebase bool `json:"allow_rebase"` AllowRebaseMerge bool `json:"allow_rebase_explicit"` AllowSquash bool `json:"allow_squash_merge"` + DefaultMergeStyle string `json:"default_merge_style"` AvatarURL string `json:"avatar_url"` Internal bool `json:"internal"` MirrorInterval string `json:"mirror_interval"` @@ -171,6 +172,8 @@ type EditRepoOption struct { AllowManualMerge *bool `json:"allow_manual_merge,omitempty"` // either `true` to enable AutodetectManualMerge, or `false` to prevent it. `has_pull_requests` must be `true`, Note: In some special cases, misjudgments can occur. AutodetectManualMerge *bool `json:"autodetect_manual_merge,omitempty"` + // set to a merge style to be used by this repository: "merge", "rebase", "rebase-merge", or "squash". `has_pull_requests` must be `true`. + DefaultMergeStyle *string `json:"default_merge_style,omitempty"` // set to `true` to archive this repository. Archived *bool `json:"archived,omitempty"` // set to a string like `8h30m0s` to set the mirror interval time diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index e679e1e874..3a8783930e 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1791,6 +1791,7 @@ settings.block_on_official_review_requests_desc = Merging will not be possible w settings.block_outdated_branch = Block merge if pull request is outdated settings.block_outdated_branch_desc = Merging will not be possible when head branch is behind base branch. settings.default_branch_desc = Select a default repository branch for pull requests and code commits: +settings.default_merge_style_desc = Default merge style for pull requests: settings.choose_branch = Choose a branch… settings.no_protected_branch = There are no protected branches. settings.edit_protected_branch = Edit diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index b7ed2951ad..c422feb043 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -731,6 +731,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { AllowSquash: true, AllowManualMerge: true, AutodetectManualMerge: false, + DefaultMergeStyle: models.MergeStyleMerge, } } else { config = unit.PullRequestsConfig() @@ -757,6 +758,9 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if opts.AutodetectManualMerge != nil { config.AutodetectManualMerge = *opts.AutodetectManualMerge } + if opts.DefaultMergeStyle != nil { + config.DefaultMergeStyle = models.MergeStyle(*opts.DefaultMergeStyle) + } units = append(units, models.RepoUnit{ RepoID: repo.ID, diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 15459cd0e6..c2969ca4bd 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1484,7 +1484,10 @@ func ViewIssue(ctx *context.Context) { // Check correct values and select default if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok || !prConfig.IsMergeStyleAllowed(ms) { - if prConfig.AllowMerge { + defaultMergeStyle := prConfig.GetDefaultMergeStyle() + if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok { + ctx.Data["MergeStyle"] = defaultMergeStyle + } else if prConfig.AllowMerge { ctx.Data["MergeStyle"] = models.MergeStyleMerge } else if prConfig.AllowRebase { ctx.Data["MergeStyle"] = models.MergeStyleRebase diff --git a/routers/repo/setting.go b/routers/repo/setting.go index dc14b69b3b..8349164d4c 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -322,6 +322,7 @@ func SettingsPost(ctx *context.Context) { AllowSquash: form.PullsAllowSquash, AllowManualMerge: form.PullsAllowManualMerge, AutodetectManualMerge: form.EnableAutodetectManualMerge, + DefaultMergeStyle: models.MergeStyle(form.PullsDefaultMergeStyle), }, }) } else if !models.UnitTypePullRequests.UnitGlobalDisabled() { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index f944eb8d8d..514a2f5c0a 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -346,6 +346,39 @@ +
+

+ {{.i18n.Tr "repo.settings.default_merge_style_desc"}} +

+ +
{{end}} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d44583b816..120d020958 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13626,6 +13626,11 @@ "type": "string", "x-go-name": "DefaultBranch" }, + "default_merge_style": { + "description": "set to a merge style to be used by this repository: \"merge\", \"rebase\", \"rebase-merge\", or \"squash\". `has_pull_requests` must be `true`.", + "type": "string", + "x-go-name": "DefaultMergeStyle" + }, "description": { "description": "a short description of the repository.", "type": "string", @@ -15676,6 +15681,10 @@ "type": "string", "x-go-name": "DefaultBranch" }, + "default_merge_style": { + "type": "string", + "x-go-name": "DefaultMergeStyle" + }, "description": { "type": "string", "x-go-name": "Description"