You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

Only check for conflicts/merging if the PR has not been merged in the interim (#10132) * Only check for merging if the PR has not been merged in the interim * fixup! Only check for merging if the PR has not been merged in the interim * Try to fix test failure * Use PR2 not PR1 in tests as PR1 merges automatically * return already merged error * enforce locking * enforce locking - fix-test * enforce locking - fix-testx2 * enforce locking - fix-testx3 * move pullrequest checking to after merge This might improve the chance that the race does not affect us but does not prevent it. * Remove minor race with getting merge commit id * fixup * move check pr after merge * Remove unnecessary prepareTestEnv - onGiteaRun does this for us * Add information about when merging occuring * fix fmt * More logging * Attempt to fix mysql * Try MySQL fix again * try again * Try again?! * Try again?! * Sigh * remove the count - perhaps that will help * next remove the update id * next remove the update id - make it updated_unix instead * On failure to merge ensure that the pr is rechecked for conflict errors * On failure to merge ensure that the pr is rechecked for conflict errors * Update models/pull.go * Update models/pull.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Only check for conflicts/merging if the PR has not been merged in the interim (#10132) * Only check for merging if the PR has not been merged in the interim * fixup! Only check for merging if the PR has not been merged in the interim * Try to fix test failure * Use PR2 not PR1 in tests as PR1 merges automatically * return already merged error * enforce locking * enforce locking - fix-test * enforce locking - fix-testx2 * enforce locking - fix-testx3 * move pullrequest checking to after merge This might improve the chance that the race does not affect us but does not prevent it. * Remove minor race with getting merge commit id * fixup * move check pr after merge * Remove unnecessary prepareTestEnv - onGiteaRun does this for us * Add information about when merging occuring * fix fmt * More logging * Attempt to fix mysql * Try MySQL fix again * try again * Try again?! * Try again?! * Sigh * remove the count - perhaps that will help * next remove the update id * next remove the update id - make it updated_unix instead * On failure to merge ensure that the pr is rechecked for conflict errors * On failure to merge ensure that the pr is rechecked for conflict errors * Update models/pull.go * Update models/pull.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package pull
  5. import (
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // Update updates pull request with base branch.
  14. func Update(pull *models.PullRequest, doer *models.User, message string) error {
  15. //use merge functions but switch repo's and branch's
  16. pr := &models.PullRequest{
  17. HeadRepoID: pull.BaseRepoID,
  18. BaseRepoID: pull.HeadRepoID,
  19. HeadBranch: pull.BaseBranch,
  20. BaseBranch: pull.HeadBranch,
  21. }
  22. if err := pr.LoadHeadRepo(); err != nil {
  23. log.Error("LoadHeadRepo: %v", err)
  24. return fmt.Errorf("LoadHeadRepo: %v", err)
  25. } else if err = pr.LoadBaseRepo(); err != nil {
  26. log.Error("LoadBaseRepo: %v", err)
  27. return fmt.Errorf("LoadBaseRepo: %v", err)
  28. }
  29. diffCount, err := GetDiverging(pull)
  30. if err != nil {
  31. return err
  32. } else if diffCount.Behind == 0 {
  33. return fmt.Errorf("HeadBranch of PR %d is up to date", pull.Index)
  34. }
  35. _, err = rawMerge(pr, doer, models.MergeStyleMerge, message)
  36. defer func() {
  37. go AddTestPullRequestTask(doer, pr.HeadRepo.ID, pr.HeadBranch, false, "", "")
  38. }()
  39. return err
  40. }
  41. // IsUserAllowedToUpdate check if user is allowed to update PR with given permissions and branch protections
  42. func IsUserAllowedToUpdate(pull *models.PullRequest, user *models.User) (bool, error) {
  43. headRepoPerm, err := models.GetUserRepoPermission(pull.HeadRepo, user)
  44. if err != nil {
  45. return false, err
  46. }
  47. pr := &models.PullRequest{
  48. HeadRepoID: pull.BaseRepoID,
  49. BaseRepoID: pull.HeadRepoID,
  50. HeadBranch: pull.BaseBranch,
  51. BaseBranch: pull.HeadBranch,
  52. }
  53. return IsUserAllowedToMerge(pr, headRepoPerm, user)
  54. }
  55. // GetDiverging determines how many commits a PR is ahead or behind the PR base branch
  56. func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) {
  57. log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
  58. if err := pr.LoadBaseRepo(); err != nil {
  59. return nil, err
  60. }
  61. if err := pr.LoadHeadRepo(); err != nil {
  62. return nil, err
  63. }
  64. headRepoPath := pr.HeadRepo.RepoPath()
  65. headGitRepo, err := git.OpenRepository(headRepoPath)
  66. if err != nil {
  67. return nil, fmt.Errorf("OpenRepository: %v", err)
  68. }
  69. defer headGitRepo.Close()
  70. if pr.IsSameRepo() {
  71. diff, err := git.GetDivergingCommits(pr.HeadRepo.RepoPath(), pr.BaseBranch, pr.HeadBranch)
  72. return &diff, err
  73. }
  74. tmpRemoteName := fmt.Sprintf("tmp-pull-%d-base", pr.ID)
  75. if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), true); err != nil {
  76. return nil, fmt.Errorf("headGitRepo.AddRemote: %v", err)
  77. }
  78. // Make sure to remove the remote even if the push fails
  79. defer func() {
  80. if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
  81. log.Error("CountDiverging: RemoveRemote: %s", err)
  82. }
  83. }()
  84. // $(git rev-list --count tmp-pull-1-base/master..feature) commits ahead of master
  85. ahead, errorAhead := checkDivergence(headRepoPath, fmt.Sprintf("%s/%s", tmpRemoteName, pr.BaseBranch), pr.HeadBranch)
  86. if errorAhead != nil {
  87. return &git.DivergeObject{}, errorAhead
  88. }
  89. // $(git rev-list --count feature..tmp-pull-1-base/master) commits behind master
  90. behind, errorBehind := checkDivergence(headRepoPath, pr.HeadBranch, fmt.Sprintf("%s/%s", tmpRemoteName, pr.BaseBranch))
  91. if errorBehind != nil {
  92. return &git.DivergeObject{}, errorBehind
  93. }
  94. return &git.DivergeObject{Ahead: ahead, Behind: behind}, nil
  95. }
  96. func checkDivergence(repoPath string, baseBranch string, targetBranch string) (int, error) {
  97. branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
  98. cmd := git.NewCommand("rev-list", "--count", branches)
  99. stdout, err := cmd.RunInDir(repoPath)
  100. if err != nil {
  101. return -1, err
  102. }
  103. outInteger, errInteger := strconv.Atoi(strings.Trim(stdout, "\n"))
  104. if errInteger != nil {
  105. return -1, errInteger
  106. }
  107. return outInteger, nil
  108. }