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 3.9 kB

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