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.

pull.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/notification"
  11. issue_service "code.gitea.io/gitea/services/issue"
  12. )
  13. // NewPullRequest creates new pull request with labels for repository.
  14. func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) error {
  15. if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch); err != nil {
  16. return err
  17. }
  18. for _, assigneeID := range assigneeIDs {
  19. if err := issue_service.AddAssigneeIfNotAssigned(pull, pull.Poster, assigneeID); err != nil {
  20. return err
  21. }
  22. }
  23. pr.Issue = pull
  24. pull.PullRequest = pr
  25. notification.NotifyNewPullRequest(pr)
  26. return nil
  27. }
  28. func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *models.User, branch string) error {
  29. repo, err := models.GetRepositoryByID(repoID)
  30. if err != nil {
  31. return fmt.Errorf("GetRepositoryByID: %v", err)
  32. }
  33. gitRepo, err := git.OpenRepository(repo.RepoPath())
  34. if err != nil {
  35. return fmt.Errorf("git.OpenRepository: %v", err)
  36. }
  37. go func() {
  38. err := requests.InvalidateCodeComments(doer, gitRepo, branch)
  39. if err != nil {
  40. log.Error("PullRequestList.InvalidateCodeComments: %v", err)
  41. }
  42. gitRepo.Close()
  43. }()
  44. return nil
  45. }
  46. func addHeadRepoTasks(prs []*models.PullRequest) {
  47. for _, pr := range prs {
  48. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  49. if err := pr.UpdatePatch(); err != nil {
  50. log.Error("UpdatePatch: %v", err)
  51. continue
  52. } else if err := pr.PushToBaseRepo(); err != nil {
  53. log.Error("PushToBaseRepo: %v", err)
  54. continue
  55. }
  56. pr.AddToTaskQueue()
  57. }
  58. }
  59. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  60. // and generate new patch for testing as needed.
  61. func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool) {
  62. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  63. prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  64. if err != nil {
  65. log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  66. return
  67. }
  68. if isSync {
  69. requests := models.PullRequestList(prs)
  70. if err = requests.LoadAttributes(); err != nil {
  71. log.Error("PullRequestList.LoadAttributes: %v", err)
  72. }
  73. if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
  74. log.Error("checkForInvalidation: %v", invalidationErr)
  75. }
  76. if err == nil {
  77. for _, pr := range prs {
  78. pr.Issue.PullRequest = pr
  79. notification.NotifyPullRequestSynchronized(doer, pr)
  80. }
  81. }
  82. }
  83. addHeadRepoTasks(prs)
  84. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  85. prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  86. if err != nil {
  87. log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  88. return
  89. }
  90. for _, pr := range prs {
  91. pr.AddToTaskQueue()
  92. }
  93. }