diff options
author | a1012112796 <1012112796@qq.com> | 2021-03-04 11:41:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-03 22:41:23 -0500 |
commit | a5279b74b63248902a6572df5afa3745367e6cb9 (patch) | |
tree | 7f1fac744013b533bfb0580d48a27d0aae5ab04e /services | |
parent | 523efa433b61e00e7a14bd31cac315e43842d729 (diff) | |
download | gitea-a5279b74b63248902a6572df5afa3745367e6cb9.tar.gz gitea-a5279b74b63248902a6572df5afa3745367e6cb9.zip |
Make manual merge autodetection optional and add manual merge as merge method (#12543)
* Make auto check manual merge as a chooseable mod and add manual merge way on ui
as title, Before this pr, we use same way with GH to check manually merge.
It good, but in some special cases, misjudgments can occur. and it's hard
to fix this bug. So I add option to allow repo manager block "auto check manual merge"
function, Then it will have same style like gitlab(allow empty pr). and to compensate for
not being able to detect THE PR merge automatically, I added a manual approach.
Signed-off-by: a1012112796 <1012112796@qq.com>
* make swager
* api support
* ping ci
* fix TestPullCreate_EmptyChangesWithCommits
* Apply suggestions from code review
Co-authored-by: zeripath <art27@cantab.net>
* Apply review suggestions and add test
* Apply suggestions from code review
Co-authored-by: zeripath <art27@cantab.net>
* fix build
* test error message
* make fmt
* Fix indentation issues identified by @silverwind
Co-authored-by: silverwind <me@silverwind.io>
* Fix tests and make manually merged disabled error on API the same
Signed-off-by: Andrew Thornton <art27@cantab.net>
* a small nit
* fix wrong commit id error
* fix bug
* simple test
* fix test
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'services')
-rw-r--r-- | services/pull/check.go | 17 | ||||
-rw-r--r-- | services/pull/merge.go | 51 | ||||
-rw-r--r-- | services/pull/patch.go | 5 |
3 files changed, 70 insertions, 3 deletions
diff --git a/services/pull/check.go b/services/pull/check.go index 5acee8174b..3ec76de5e8 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -116,7 +116,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { if err != nil { return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v", err) } else if len(mergeCommit) < 40 { - // PR was fast-forwarded, so just use last commit of PR + // PR was maybe fast-forwarded, so just use last commit of PR mergeCommit = commitID[:40] } @@ -137,6 +137,21 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { // manuallyMerged checks if a pull request got manually merged // When a pull request got manually merged mark the pull request as merged func manuallyMerged(pr *models.PullRequest) bool { + if err := pr.LoadBaseRepo(); err != nil { + log.Error("PullRequest[%d].LoadBaseRepo: %v", pr.ID, err) + return false + } + + if unit, err := pr.BaseRepo.GetUnit(models.UnitTypePullRequests); err == nil { + config := unit.PullRequestsConfig() + if !config.AutodetectManualMerge { + return false + } + } else { + log.Error("PullRequest[%d].BaseRepo.GetUnit(models.UnitTypePullRequests): %v", pr.ID, err) + return false + } + commit, err := getMergeCommit(pr) if err != nil { log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err) diff --git a/services/pull/merge.go b/services/pull/merge.go index 9d6eadc524..bbe631f68c 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -615,3 +615,54 @@ func CheckPRReadyToMerge(pr *models.PullRequest, skipProtectedFilesCheck bool) ( return nil } + +// MergedManually mark pr as merged manually +func MergedManually(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository, commitID string) (err error) { + prUnit, err := pr.BaseRepo.GetUnit(models.UnitTypePullRequests) + if err != nil { + return + } + prConfig := prUnit.PullRequestsConfig() + + // Check if merge style is correct and allowed + if !prConfig.IsMergeStyleAllowed(models.MergeStyleManuallyMerged) { + return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: models.MergeStyleManuallyMerged} + } + + if len(commitID) < 40 { + return fmt.Errorf("Wrong commit ID") + } + + commit, err := baseGitRepo.GetCommit(commitID) + if err != nil { + if git.IsErrNotExist(err) { + return fmt.Errorf("Wrong commit ID") + } + return + } + + ok, err := baseGitRepo.IsCommitInBranch(commitID, pr.BaseBranch) + if err != nil { + return + } + if !ok { + return fmt.Errorf("Wrong commit ID") + } + + pr.MergedCommitID = commitID + pr.MergedUnix = timeutil.TimeStamp(commit.Author.When.Unix()) + pr.Status = models.PullRequestStatusManuallyMerged + pr.Merger = doer + pr.MergerID = doer.ID + + merged := false + if merged, err = pr.SetMerged(); err != nil { + return + } else if !merged { + return fmt.Errorf("SetMerged failed") + } + + notification.NotifyMergePullRequest(pr, doer) + log.Info("manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s", pr.ID, pr.BaseRepo.Name, pr.BaseBranch, commit.ID.String()) + return nil +} diff --git a/services/pull/patch.go b/services/pull/patch.go index 2692d848c4..72b459bf2c 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -80,7 +80,7 @@ func TestPatch(pr *models.PullRequest) error { pr.MergeBase = strings.TrimSpace(pr.MergeBase) // 2. Check for conflicts - if conflicts, err := checkConflicts(pr, gitRepo, tmpBasePath); err != nil || conflicts { + if conflicts, err := checkConflicts(pr, gitRepo, tmpBasePath); err != nil || conflicts || pr.Status == models.PullRequestStatusEmpty { return err } @@ -125,8 +125,9 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath // 1a. if the size of that patch is 0 - there can be no conflicts! if stat.Size() == 0 { log.Debug("PullRequest[%d]: Patch is empty - ignoring", pr.ID) - pr.Status = models.PullRequestStatusMergeable + pr.Status = models.PullRequestStatusEmpty pr.ConflictedFiles = []string{} + pr.ChangedProtectedFiles = []string{} return false, nil } |