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.

review.go 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "code.gitea.io/gitea/modules/webhook"
  10. )
  11. // CreateReview creates a new review based on opts
  12. func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) {
  13. review, err := models.CreateReview(opts)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return review, reviewHook(review)
  18. }
  19. // UpdateReview updates a review
  20. func UpdateReview(review *models.Review) error {
  21. err := models.UpdateReview(review)
  22. if err != nil {
  23. return err
  24. }
  25. return reviewHook(review)
  26. }
  27. func reviewHook(review *models.Review) error {
  28. var reviewHookType models.HookEventType
  29. switch review.Type {
  30. case models.ReviewTypeApprove:
  31. reviewHookType = models.HookEventPullRequestApproved
  32. case models.ReviewTypeComment:
  33. reviewHookType = models.HookEventPullRequestComment
  34. case models.ReviewTypeReject:
  35. reviewHookType = models.HookEventPullRequestRejected
  36. default:
  37. // unsupported review webhook type here
  38. return nil
  39. }
  40. pr := review.Issue.PullRequest
  41. if err := pr.LoadIssue(); err != nil {
  42. return err
  43. }
  44. mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
  45. if err != nil {
  46. return err
  47. }
  48. if err := webhook.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
  49. Action: api.HookIssueSynchronized,
  50. Index: review.Issue.Index,
  51. PullRequest: pr.APIFormat(),
  52. Repository: review.Issue.Repo.APIFormat(mode),
  53. Sender: review.Reviewer.APIFormat(),
  54. Review: &api.ReviewPayload{
  55. Type: string(reviewHookType),
  56. Content: review.Content,
  57. },
  58. }); err != nil {
  59. return err
  60. }
  61. go webhook.HookQueue.Add(review.Issue.Repo.ID)
  62. return nil
  63. }