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.

109 lines
3.2 KiB

  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, assigneeIDs []int64) error {
  15. if err := TestPatch(pr); err != nil {
  16. return err
  17. }
  18. if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil {
  19. return err
  20. }
  21. for _, assigneeID := range assigneeIDs {
  22. if err := issue_service.AddAssigneeIfNotAssigned(pull, pull.Poster, assigneeID); err != nil {
  23. return err
  24. }
  25. }
  26. pr.Issue = pull
  27. pull.PullRequest = pr
  28. notification.NotifyNewPullRequest(pr)
  29. return nil
  30. }
  31. func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *models.User, branch string) error {
  32. repo, err := models.GetRepositoryByID(repoID)
  33. if err != nil {
  34. return fmt.Errorf("GetRepositoryByID: %v", err)
  35. }
  36. gitRepo, err := git.OpenRepository(repo.RepoPath())
  37. if err != nil {
  38. return fmt.Errorf("git.OpenRepository: %v", err)
  39. }
  40. go func() {
  41. err := requests.InvalidateCodeComments(doer, gitRepo, branch)
  42. if err != nil {
  43. log.Error("PullRequestList.InvalidateCodeComments: %v", err)
  44. }
  45. gitRepo.Close()
  46. }()
  47. return nil
  48. }
  49. func addHeadRepoTasks(prs []*models.PullRequest) {
  50. for _, pr := range prs {
  51. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  52. if err := pr.PushToBaseRepo(); err != nil {
  53. log.Error("PushToBaseRepo: %v", err)
  54. continue
  55. }
  56. AddToTaskQueue(pr)
  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. AddToTaskQueue(pr)
  92. }
  93. }