本站源代码
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.

145 line
4.5KB

  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. api "code.gitea.io/gitea/modules/structs"
  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. if err := models.NotifyWatchers(&models.Action{
  24. ActUserID: pull.Poster.ID,
  25. ActUser: pull.Poster,
  26. OpType: models.ActionCreatePullRequest,
  27. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
  28. RepoID: repo.ID,
  29. Repo: repo,
  30. IsPrivate: repo.IsPrivate,
  31. }); err != nil {
  32. log.Error("NotifyWatchers: %v", err)
  33. }
  34. pr.Issue = pull
  35. pull.PullRequest = pr
  36. mode, _ := models.AccessLevel(pull.Poster, repo)
  37. if err := models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{
  38. Action: api.HookIssueOpened,
  39. Index: pull.Index,
  40. PullRequest: pr.APIFormat(),
  41. Repository: repo.APIFormat(mode),
  42. Sender: pull.Poster.APIFormat(),
  43. }); err != nil {
  44. log.Error("PrepareWebhooks: %v", err)
  45. } else {
  46. go models.HookQueue.Add(repo.ID)
  47. }
  48. return nil
  49. }
  50. func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *models.User, branch string) error {
  51. repo, err := models.GetRepositoryByID(repoID)
  52. if err != nil {
  53. return fmt.Errorf("GetRepositoryByID: %v", err)
  54. }
  55. gitRepo, err := git.OpenRepository(repo.RepoPath())
  56. if err != nil {
  57. return fmt.Errorf("git.OpenRepository: %v", err)
  58. }
  59. go func() {
  60. err := requests.InvalidateCodeComments(doer, gitRepo, branch)
  61. if err != nil {
  62. log.Error("PullRequestList.InvalidateCodeComments: %v", err)
  63. }
  64. }()
  65. return nil
  66. }
  67. func addHeadRepoTasks(prs []*models.PullRequest) {
  68. for _, pr := range prs {
  69. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  70. if err := pr.UpdatePatch(); err != nil {
  71. log.Error("UpdatePatch: %v", err)
  72. continue
  73. } else if err := pr.PushToBaseRepo(); err != nil {
  74. log.Error("PushToBaseRepo: %v", err)
  75. continue
  76. }
  77. pr.AddToTaskQueue()
  78. }
  79. }
  80. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  81. // and generate new patch for testing as needed.
  82. func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool) {
  83. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  84. prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  85. if err != nil {
  86. log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  87. return
  88. }
  89. if isSync {
  90. requests := models.PullRequestList(prs)
  91. if err = requests.LoadAttributes(); err != nil {
  92. log.Error("PullRequestList.LoadAttributes: %v", err)
  93. }
  94. if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
  95. log.Error("checkForInvalidation: %v", invalidationErr)
  96. }
  97. if err == nil {
  98. for _, pr := range prs {
  99. pr.Issue.PullRequest = pr
  100. if err = pr.Issue.LoadAttributes(); err != nil {
  101. log.Error("LoadAttributes: %v", err)
  102. continue
  103. }
  104. if err = models.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
  105. Action: api.HookIssueSynchronized,
  106. Index: pr.Issue.Index,
  107. PullRequest: pr.Issue.PullRequest.APIFormat(),
  108. Repository: pr.Issue.Repo.APIFormat(models.AccessModeNone),
  109. Sender: doer.APIFormat(),
  110. }); err != nil {
  111. log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
  112. continue
  113. }
  114. go models.HookQueue.Add(pr.Issue.Repo.ID)
  115. }
  116. }
  117. }
  118. addHeadRepoTasks(prs)
  119. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  120. prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  121. if err != nil {
  122. log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  123. return
  124. }
  125. for _, pr := range prs {
  126. pr.AddToTaskQueue()
  127. }
  128. }
上海开阖软件有限公司 沪ICP备12045867号-1