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.

pull.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 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. "os"
  8. "path"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. issue_service "code.gitea.io/gitea/services/issue"
  14. )
  15. // NewPullRequest creates new pull request with labels for repository.
  16. func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, assigneeIDs []int64) error {
  17. if err := TestPatch(pr); err != nil {
  18. return err
  19. }
  20. if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil {
  21. return err
  22. }
  23. for _, assigneeID := range assigneeIDs {
  24. if err := issue_service.AddAssigneeIfNotAssigned(pull, pull.Poster, assigneeID); err != nil {
  25. return err
  26. }
  27. }
  28. pr.Issue = pull
  29. pull.PullRequest = pr
  30. if err := PushToBaseRepo(pr); err != nil {
  31. return err
  32. }
  33. notification.NotifyNewPullRequest(pr)
  34. return nil
  35. }
  36. func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *models.User, branch string) error {
  37. repo, err := models.GetRepositoryByID(repoID)
  38. if err != nil {
  39. return fmt.Errorf("GetRepositoryByID: %v", err)
  40. }
  41. gitRepo, err := git.OpenRepository(repo.RepoPath())
  42. if err != nil {
  43. return fmt.Errorf("git.OpenRepository: %v", err)
  44. }
  45. go func() {
  46. err := requests.InvalidateCodeComments(doer, gitRepo, branch)
  47. if err != nil {
  48. log.Error("PullRequestList.InvalidateCodeComments: %v", err)
  49. }
  50. gitRepo.Close()
  51. }()
  52. return nil
  53. }
  54. func addHeadRepoTasks(prs []*models.PullRequest) {
  55. for _, pr := range prs {
  56. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  57. if err := PushToBaseRepo(pr); err != nil {
  58. log.Error("PushToBaseRepo: %v", err)
  59. continue
  60. }
  61. AddToTaskQueue(pr)
  62. }
  63. }
  64. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  65. // and generate new patch for testing as needed.
  66. func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool) {
  67. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  68. prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  69. if err != nil {
  70. log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  71. return
  72. }
  73. if isSync {
  74. requests := models.PullRequestList(prs)
  75. if err = requests.LoadAttributes(); err != nil {
  76. log.Error("PullRequestList.LoadAttributes: %v", err)
  77. }
  78. if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
  79. log.Error("checkForInvalidation: %v", invalidationErr)
  80. }
  81. if err == nil {
  82. for _, pr := range prs {
  83. pr.Issue.PullRequest = pr
  84. notification.NotifyPullRequestSynchronized(doer, pr)
  85. }
  86. }
  87. }
  88. addHeadRepoTasks(prs)
  89. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  90. prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  91. if err != nil {
  92. log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  93. return
  94. }
  95. for _, pr := range prs {
  96. AddToTaskQueue(pr)
  97. }
  98. }
  99. // PushToBaseRepo pushes commits from branches of head repository to
  100. // corresponding branches of base repository.
  101. // FIXME: Only push branches that are actually updates?
  102. func PushToBaseRepo(pr *models.PullRequest) (err error) {
  103. log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
  104. headRepoPath := pr.HeadRepo.RepoPath()
  105. headGitRepo, err := git.OpenRepository(headRepoPath)
  106. if err != nil {
  107. return fmt.Errorf("OpenRepository: %v", err)
  108. }
  109. defer headGitRepo.Close()
  110. tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
  111. if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
  112. return fmt.Errorf("headGitRepo.AddRemote: %v", err)
  113. }
  114. // Make sure to remove the remote even if the push fails
  115. defer func() {
  116. if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
  117. log.Error("PushToBaseRepo: RemoveRemote: %s", err)
  118. }
  119. }()
  120. headFile := pr.GetGitRefName()
  121. // Remove head in case there is a conflict.
  122. file := path.Join(pr.BaseRepo.RepoPath(), headFile)
  123. _ = os.Remove(file)
  124. if err = git.Push(headRepoPath, git.PushOptions{
  125. Remote: tmpRemoteName,
  126. Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
  127. Force: true,
  128. }); err != nil {
  129. return fmt.Errorf("Push: %v", err)
  130. }
  131. return nil
  132. }