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

update.go 2.5 kB

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>
5 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>
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // Update updates pull request with base branch.
  12. func Update(pull *models.PullRequest, doer *models.User, message string) error {
  13. //use merge functions but switch repo's and branch's
  14. pr := &models.PullRequest{
  15. HeadRepoID: pull.BaseRepoID,
  16. BaseRepoID: pull.HeadRepoID,
  17. HeadBranch: pull.BaseBranch,
  18. BaseBranch: pull.HeadBranch,
  19. }
  20. if err := pr.LoadHeadRepo(); err != nil {
  21. log.Error("LoadHeadRepo: %v", err)
  22. return fmt.Errorf("LoadHeadRepo: %v", err)
  23. } else if err = pr.LoadBaseRepo(); err != nil {
  24. log.Error("LoadBaseRepo: %v", err)
  25. return fmt.Errorf("LoadBaseRepo: %v", err)
  26. }
  27. diffCount, err := GetDiverging(pull)
  28. if err != nil {
  29. return err
  30. } else if diffCount.Behind == 0 {
  31. return fmt.Errorf("HeadBranch of PR %d is up to date", pull.Index)
  32. }
  33. _, err = rawMerge(pr, doer, models.MergeStyleMerge, message)
  34. defer func() {
  35. go AddTestPullRequestTask(doer, pr.HeadRepo.ID, pr.HeadBranch, false, "", "")
  36. }()
  37. return err
  38. }
  39. // IsUserAllowedToUpdate check if user is allowed to update PR with given permissions and branch protections
  40. func IsUserAllowedToUpdate(pull *models.PullRequest, user *models.User) (bool, error) {
  41. headRepoPerm, err := models.GetUserRepoPermission(pull.HeadRepo, user)
  42. if err != nil {
  43. return false, err
  44. }
  45. pr := &models.PullRequest{
  46. HeadRepoID: pull.BaseRepoID,
  47. BaseRepoID: pull.HeadRepoID,
  48. HeadBranch: pull.BaseBranch,
  49. BaseBranch: pull.HeadBranch,
  50. }
  51. return IsUserAllowedToMerge(pr, headRepoPerm, user)
  52. }
  53. // GetDiverging determines how many commits a PR is ahead or behind the PR base branch
  54. func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) {
  55. log.Trace("GetDiverging[%d]: compare commits", pr.ID)
  56. if err := pr.LoadBaseRepo(); err != nil {
  57. return nil, err
  58. }
  59. if err := pr.LoadHeadRepo(); err != nil {
  60. return nil, err
  61. }
  62. tmpRepo, err := createTemporaryRepo(pr)
  63. if err != nil {
  64. log.Error("CreateTemporaryPath: %v", err)
  65. return nil, err
  66. }
  67. defer func() {
  68. if err := models.RemoveTemporaryPath(tmpRepo); err != nil {
  69. log.Error("Merge: RemoveTemporaryPath: %s", err)
  70. }
  71. }()
  72. diff, err := git.GetDivergingCommits(tmpRepo, "base", "tracking")
  73. return &diff, err
  74. }