From 1ce45574ad3e71cd8ca7fdfc6531b399ede342be Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 24 Feb 2021 12:49:01 -0600 Subject: [PATCH 1/7] Add DefaultMergeStyle option to repository Fixes #12293 --- models/migrations/v174.go | 62 ++++++++++++++++++++++++++++ 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 | 20 +++++++++ 11 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 models/migrations/v174.go diff --git a/models/migrations/v174.go b/models/migrations/v174.go new file mode 100644 index 0000000000000..8f7860fab44cd --- /dev/null +++ b/models/migrations/v174.go @@ -0,0 +1,62 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "fmt" + + "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/xorm" +) + +func addPullRequestDefaultMergeStyle(x *xorm.Engine) error { + // RepoUnit describes all units of a repository + type RepoUnit struct { + ID int64 + RepoID int64 `xorm:"INDEX(s)"` + Type int `xorm:"INDEX(s)"` + Config map[string]interface{} `xorm:"JSON"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"` + } + + const ( + v16UnitTypeCode = iota + 1 // 1 code + v16UnitTypeIssues // 2 issues + v16UnitTypePRs // 3 PRs + v16UnitTypeCommits // 4 Commits + v16UnitTypeReleases // 5 Releases + v16UnitTypeWiki // 6 Wiki + v16UnitTypeSettings // 7 Settings + v16UnitTypeExternalWiki // 8 ExternalWiki + v16UnitTypeExternalTracker // 9 ExternalTracker + ) + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + //Updating existing issue units + units := make([]*RepoUnit, 0, 100) + if err := sess.Where("`type` = ?", v16UnitTypePRs).Find(&units); err != nil { + return fmt.Errorf("Query repo units: %v", err) + } + for _, unit := range units { + if unit.Config == nil { + unit.Config = make(map[string]interface{}) + } + defaultMergeStyle := "merge" + + if _, ok := unit.Config["DefaultMergeStyle"]; !ok { + unit.Config["DefaultMergeStyle"] = defaultMergeStyle + } + if _, err := sess.ID(unit.ID).Cols("config").Update(unit); err != nil { + return err + } + } + return sess.Commit() +} diff --git a/models/repo.go b/models/repo.go index 03259d9e347a2..101960f2cb4e6 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1076,7 +1076,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 8c2d458758a2d..437dd764a1e2d 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -93,6 +93,7 @@ type PullRequestsConfig struct { AllowRebase bool AllowRebaseMerge bool AllowSquash bool + DefaultMergeStyle MergeStyle } // FromDB fills up a PullRequestsConfig from serialized format. @@ -113,6 +114,15 @@ func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool { mergeStyle == MergeStyleSquash && cfg.AllowSquash } +// 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 813201ca6844c..63a22bb04ead6 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 48af3450f371d..4b1a51b3f3ab8 100644 --- a/modules/forms/repo_form.go +++ b/modules/forms/repo_form.go @@ -156,6 +156,7 @@ type RepoSettingForm struct { PullsAllowRebase bool PullsAllowRebaseMerge bool PullsAllowSquash bool + PullsDefaultMergeStyle string EnableTimetracker bool AllowOnlyContributorsToTrackTime bool EnableIssueDependencies bool diff --git a/modules/structs/repo.go b/modules/structs/repo.go index d588813b21883..d094ec9d9dbe8 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"` @@ -167,6 +168,8 @@ type EditRepoOption struct { AllowRebaseMerge *bool `json:"allow_rebase_explicit,omitempty"` // either `true` to allow squash-merging pull requests, or `false` to prevent squash-merging. `has_pull_requests` must be `true`. AllowSquash *bool `json:"allow_squash_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 8245df754a70e..41fca240c9a56 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1764,6 +1764,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 b84c5993e4792..b17b1271450b4 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -725,6 +725,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, + DefaultMergeStyle: models.MergeStyleMerge, } } else { config = unit.PullRequestsConfig() @@ -745,6 +746,9 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if opts.AllowSquash != nil { config.AllowSquash = *opts.AllowSquash } + 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 a9459a10ed93d..f0a2f827a9eb3 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1483,7 +1483,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 3e22e8804e5d5..4defb886e5459 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -321,6 +321,7 @@ func SettingsPost(ctx *context.Context) { AllowRebase: form.PullsAllowRebase, AllowRebaseMerge: form.PullsAllowRebaseMerge, AllowSquash: form.PullsAllowSquash, + 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 e85451ac39878..a4c9670d4825c 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -330,6 +330,26 @@ +
+

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

+ +
{{end}} From af12149a0a1a79bec8cf7404071eb481859a58fb Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 24 Feb 2021 13:39:31 -0600 Subject: [PATCH 2/7] Fix missing migration - satisfy linter --- models/migrations/migrations.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 4fc737e1bfe42..b0683a1fccc82 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -294,6 +294,8 @@ var migrations = []Migration{ NewMigration("Add sessions table for go-chi/session", addSessionTable), // v173 -> v174 NewMigration("Add time_id column to Comment", addTimeIDCommentColumn), + // v174 -> v175 + NewMigration("Add DefaultMergeStyle to pull requests", addPullRequestDefaultMergeStyle), } // GetCurrentDBVersion returns the current db version From 0de9098bf4af2b505689c2c813661b5e55c2ede9 Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 24 Feb 2021 13:40:05 -0600 Subject: [PATCH 3/7] Update to latest formatting This was originally based on the 1.13.2 branch, but it seems this has changed in master --- templates/repo/settings/options.tmpl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index a4c9670d4825c..aaee8f3a1998c 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -340,8 +340,21 @@ - -
{{$prUnit.PullRequestsConfig.DefaultMergeStyle}}
+ {{svg "octicon-triangle-down" 14 "dropdown icon"}} +
+ {{if (eq $prUnit.PullRequestsConfig.DefaultMergeStyle "merge")}} + {{.i18n.Tr "repo.pulls.merge_pull_request"}} + {{end}} + {{if (eq $prUnit.PullRequestsConfig.DefaultMergeStyle "rebase")}} + {{.i18n.Tr "repo.pulls.rebase_merge_pull_request"}} + {{end}} + {{if (eq $prUnit.PullRequestsConfig.DefaultMergeStyle "rebase-merge")}} + {{.i18n.Tr "repo.pulls.rebase_merge_commit_pull_request"}} + {{end}} + {{if (eq $prUnit.PullRequestsConfig.DefaultMergeStyle "squash")}} + {{.i18n.Tr "repo.pulls.squash_merge_pull_request"}} + {{end}} +
-

+

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

-
- + + {{end}} From 3b79b0a816a50e63585a5f4575950f3189560ad8 Mon Sep 17 00:00:00 2001 From: parnic Date: Wed, 17 Mar 2021 18:59:17 -0500 Subject: [PATCH 7/7] Fix whitespace after GitHub merge --- modules/forms/repo_form.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/forms/repo_form.go b/modules/forms/repo_form.go index 72791755c279b..d9eb06d194db5 100644 --- a/modules/forms/repo_form.go +++ b/modules/forms/repo_form.go @@ -141,7 +141,7 @@ type RepoSettingForm struct { PullsAllowSquash bool PullsAllowManualMerge bool PullsDefaultMergeStyle string - EnableAutodetectManualMerge bool + EnableAutodetectManualMerge bool EnableTimetracker bool AllowOnlyContributorsToTrackTime bool EnableIssueDependencies bool