diff options
author | Lauris BH <lauris@nix.lv> | 2018-01-05 20:56:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-05 20:56:50 +0200 |
commit | 8ac1501ad7f8b350bd9c1bf01b25f994fd3560f9 (patch) | |
tree | cf9b661996134da3e39aa6ddd2af4e13f7dbf30f /routers | |
parent | a192f3052ed9b59d1404fdcebf2b5c156d6d6969 (diff) | |
download | gitea-8ac1501ad7f8b350bd9c1bf01b25f994fd3560f9.tar.gz gitea-8ac1501ad7f8b350bd9c1bf01b25f994fd3560f9.zip |
Add Pull Request merge options - Ignore white-space for conflict checking, Rebase, Squash merge (#3188)
* Pull request options migration and UI in settings
* Add ignore whitespace functionality
* Fix settings if pull requests are disabled
* Fix migration transaction
* Merge with Rebase functionality
* UI changes and related functionality for pull request merging button
* Implement squash functionality
* Fix rebase merging
* Fix pull request merge tests
* Add squash and rebase tests
* Fix API method to reuse default message functions
* Some refactoring and small fixes
* Remove more hardcoded values from tests
* Remove unneeded check from API method
* Fix variable name and comment typo
* Fix reset commit count after PR merge
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 2 | ||||
-rw-r--r-- | routers/api/v1/repo/pull.go | 28 | ||||
-rw-r--r-- | routers/repo/issue.go | 20 | ||||
-rw-r--r-- | routers/repo/pull.go | 31 | ||||
-rw-r--r-- | routers/repo/setting.go | 7 | ||||
-rw-r--r-- | routers/routes/routes.go | 2 |
6 files changed, 83 insertions, 7 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 588a763616..8a300f9958 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -477,7 +477,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Combo("").Get(repo.GetPullRequest). Patch(reqToken(), reqRepoWriter(), bind(api.EditPullRequestOption{}), repo.EditPullRequest) m.Combo("/merge").Get(repo.IsPullRequestMerged). - Post(reqToken(), reqRepoWriter(), repo.MergePullRequest) + Post(reqToken(), reqRepoWriter(), bind(auth.MergePullRequestForm{}), repo.MergePullRequest) }) }, mustAllowPulls, context.ReferencesGitRepo()) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 50b864e323..7e011219be 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -434,7 +435,7 @@ func IsPullRequestMerged(ctx *context.APIContext) { } // MergePullRequest merges a PR given an index -func MergePullRequest(ctx *context.APIContext) { +func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { // swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/merge repository repoMergePullRequest // --- // summary: Merge a pull request @@ -497,7 +498,30 @@ func MergePullRequest(ctx *context.APIContext) { return } - if err := pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil { + if len(form.Do) == 0 { + form.Do = string(models.MergeStyleMerge) + } + + message := strings.TrimSpace(form.MergeTitleField) + if len(message) == 0 { + if models.MergeStyle(form.Do) == models.MergeStyleMerge { + message = pr.GetDefaultMergeMessage() + } + if models.MergeStyle(form.Do) == models.MergeStyleSquash { + message = pr.GetDefaultSquashMessage() + } + } + + form.MergeMessageField = strings.TrimSpace(form.MergeMessageField) + if len(form.MergeMessageField) > 0 { + message += "\n\n" + form.MergeMessageField + } + + if err := pr.Merge(ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if models.IsErrInvalidMergeStyle(err) { + ctx.Status(405) + return + } ctx.Error(500, "Merge", err) return } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 530586a143..e4aecf152e 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -727,6 +727,26 @@ func ViewIssue(ctx *context.Context) { } } + prUnit, err := repo.GetUnit(models.UnitTypePullRequests) + if err != nil { + ctx.Handle(500, "GetUnit", err) + return + } + prConfig := prUnit.PullRequestsConfig() + + // Check correct values and select default + if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok || + !prConfig.IsMergeStyleAllowed(ms) { + if prConfig.AllowMerge { + ctx.Data["MergeStyle"] = models.MergeStyleMerge + } else if prConfig.AllowRebase { + ctx.Data["MergeStyle"] = models.MergeStyleRebase + } else if prConfig.AllowSquash { + ctx.Data["MergeStyle"] = models.MergeStyleSquash + } else { + ctx.Data["MergeStyle"] = "" + } + } ctx.Data["IsPullBranchDeletable"] = canDelete && pull.HeadRepo != nil && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch) } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 6162df9651..9de8268957 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -495,7 +495,7 @@ func ViewPullFiles(ctx *context.Context) { } // MergePullRequest response for merging pull request -func MergePullRequest(ctx *context.Context) { +func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) { issue := checkPullInfo(ctx) if ctx.Written() { return @@ -514,15 +514,42 @@ func MergePullRequest(ctx *context.Context) { } return } + pr.Issue = issue if !pr.CanAutoMerge() || pr.HasMerged { ctx.Handle(404, "MergePullRequest", nil) return } + if ctx.HasError() { + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) + ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) + return + } + + message := strings.TrimSpace(form.MergeTitleField) + if len(message) == 0 { + if models.MergeStyle(form.Do) == models.MergeStyleMerge { + message = pr.GetDefaultMergeMessage() + } + if models.MergeStyle(form.Do) == models.MergeStyleSquash { + message = pr.GetDefaultSquashMessage() + } + } + + form.MergeMessageField = strings.TrimSpace(form.MergeMessageField) + if len(form.MergeMessageField) > 0 { + message += "\n\n" + form.MergeMessageField + } + pr.Issue = issue pr.Issue.Repo = ctx.Repo.Repository - if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil { + if err = pr.Merge(ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if models.IsErrInvalidMergeStyle(err) { + ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) + ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) + return + } ctx.Handle(500, "Merge", err) return } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 8cb551707c..342451b8ad 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -211,7 +211,12 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypePullRequests, - Config: new(models.UnitConfig), + Config: &models.PullRequestsConfig{ + IgnoreWhitespaceConflicts: form.PullsIgnoreWhitespace, + AllowMerge: form.PullsAllowMerge, + AllowRebase: form.PullsAllowRebase, + AllowSquash: form.PullsAllowSquash, + }, }) } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 4293cca6c9..3e00f55ebf 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -627,7 +627,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get(".diff", repo.DownloadPullDiff) m.Get("/commits", context.RepoRef(), repo.ViewPullCommits) m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles) - m.Post("/merge", reqRepoWriter, repo.MergePullRequest) + m.Post("/merge", reqRepoWriter, bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest) m.Post("/cleanup", context.RepoRef(), repo.CleanUpPullRequest) }, repo.MustAllowPulls) |