本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

75 lines
1.8KB

  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "code.gitea.io/gitea/models"
  8. api "code.gitea.io/gitea/modules/structs"
  9. )
  10. // CreateReview creates a new review based on opts
  11. func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) {
  12. review, err := models.CreateReview(opts)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return review, reviewHook(review)
  17. }
  18. // UpdateReview updates a review
  19. func UpdateReview(review *models.Review) error {
  20. err := models.UpdateReview(review)
  21. if err != nil {
  22. return err
  23. }
  24. return reviewHook(review)
  25. }
  26. func reviewHook(review *models.Review) error {
  27. var reviewHookType models.HookEventType
  28. switch review.Type {
  29. case models.ReviewTypeApprove:
  30. reviewHookType = models.HookEventPullRequestApproved
  31. case models.ReviewTypeComment:
  32. reviewHookType = models.HookEventPullRequestComment
  33. case models.ReviewTypeReject:
  34. reviewHookType = models.HookEventPullRequestRejected
  35. default:
  36. // unsupported review webhook type here
  37. return nil
  38. }
  39. pr := review.Issue.PullRequest
  40. if err := pr.LoadIssue(); err != nil {
  41. return err
  42. }
  43. mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
  44. if err != nil {
  45. return err
  46. }
  47. if err := models.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
  48. Action: api.HookIssueSynchronized,
  49. Index: review.Issue.Index,
  50. PullRequest: pr.APIFormat(),
  51. Repository: review.Issue.Repo.APIFormat(mode),
  52. Sender: review.Reviewer.APIFormat(),
  53. Review: &api.ReviewPayload{
  54. Type: string(reviewHookType),
  55. Content: review.Content,
  56. },
  57. }); err != nil {
  58. return err
  59. }
  60. go models.HookQueue.Add(review.Issue.Repo.ID)
  61. return nil
  62. }
上海开阖软件有限公司 沪ICP备12045867号-1