2019-08-14 21:02:19 +05:30
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateReview creates a new review based on opts
|
|
|
|
func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) {
|
|
|
|
review, err := models.CreateReview(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-18 14:03:19 +05:30
|
|
|
return review, reviewHook(review)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateReview updates a review
|
|
|
|
func UpdateReview(review *models.Review) error {
|
|
|
|
err := models.UpdateReview(review)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reviewHook(review)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reviewHook(review *models.Review) error {
|
2019-08-14 21:02:19 +05:30
|
|
|
var reviewHookType models.HookEventType
|
|
|
|
|
2019-10-18 14:03:19 +05:30
|
|
|
switch review.Type {
|
2019-08-14 21:02:19 +05:30
|
|
|
case models.ReviewTypeApprove:
|
|
|
|
reviewHookType = models.HookEventPullRequestApproved
|
|
|
|
case models.ReviewTypeComment:
|
|
|
|
reviewHookType = models.HookEventPullRequestComment
|
|
|
|
case models.ReviewTypeReject:
|
|
|
|
reviewHookType = models.HookEventPullRequestRejected
|
|
|
|
default:
|
|
|
|
// unsupported review webhook type here
|
2019-10-18 14:03:19 +05:30
|
|
|
return nil
|
2019-08-14 21:02:19 +05:30
|
|
|
}
|
|
|
|
|
2019-10-18 14:03:19 +05:30
|
|
|
pr := review.Issue.PullRequest
|
2019-08-14 21:02:19 +05:30
|
|
|
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
2019-10-18 14:03:19 +05:30
|
|
|
return err
|
2019-08-14 21:02:19 +05:30
|
|
|
}
|
|
|
|
|
2019-10-18 14:03:19 +05:30
|
|
|
mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
|
2019-08-14 21:02:19 +05:30
|
|
|
if err != nil {
|
2019-10-18 14:03:19 +05:30
|
|
|
return err
|
2019-08-14 21:02:19 +05:30
|
|
|
}
|
2019-10-18 14:03:19 +05:30
|
|
|
if err := models.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
|
2019-08-14 21:02:19 +05:30
|
|
|
Action: api.HookIssueSynchronized,
|
2019-10-18 14:03:19 +05:30
|
|
|
Index: review.Issue.Index,
|
2019-08-14 21:02:19 +05:30
|
|
|
PullRequest: pr.APIFormat(),
|
2019-10-18 14:03:19 +05:30
|
|
|
Repository: review.Issue.Repo.APIFormat(mode),
|
|
|
|
Sender: review.Reviewer.APIFormat(),
|
2019-10-19 04:12:04 +05:30
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
2019-08-14 21:02:19 +05:30
|
|
|
}); err != nil {
|
2019-10-18 14:03:19 +05:30
|
|
|
return err
|
2019-08-14 21:02:19 +05:30
|
|
|
}
|
2019-10-18 14:03:19 +05:30
|
|
|
go models.HookQueue.Add(review.Issue.Repo.ID)
|
2019-08-14 21:02:19 +05:30
|
|
|
|
2019-10-18 14:03:19 +05:30
|
|
|
return nil
|
2019-08-14 21:02:19 +05:30
|
|
|
}
|