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.

107 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, 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. }()
  43. return nil
  44. }
  45. func addHeadRepoTasks(prs []*models.PullRequest) {
  46. for _, pr := range prs {
  47. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  48. if err := pr.UpdatePatch(); err != nil {
  49. log.Error("UpdatePatch: %v", err)
  50. continue
  51. } else if err := pr.PushToBaseRepo(); err != nil {
  52. log.Error("PushToBaseRepo: %v", err)
  53. continue
  54. }
  55. pr.AddToTaskQueue()
  56. }
  57. }
  58. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  59. // and generate new patch for testing as needed.
  60. func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool) {
  61. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  62. prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  63. if err != nil {
  64. log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  65. return
  66. }
  67. if isSync {
  68. requests := models.PullRequestList(prs)
  69. if err = requests.LoadAttributes(); err != nil {
  70. log.Error("PullRequestList.LoadAttributes: %v", err)
  71. }
  72. if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
  73. log.Error("checkForInvalidation: %v", invalidationErr)
  74. }
  75. if err == nil {
  76. for _, pr := range prs {
  77. pr.Issue.PullRequest = pr
  78. notification.NotifyPullRequestSynchronized(doer, pr)
  79. }
  80. }
  81. }
  82. addHeadRepoTasks(prs)
  83. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  84. prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  85. if err != nil {
  86. log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  87. return
  88. }
  89. for _, pr := range prs {
  90. pr.AddToTaskQueue()
  91. }
  92. }