2019-08-14 09:32:19 -06:00
|
|
|
// 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 02:33:19 -06:00
|
|
|
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 09:32:19 -06:00
|
|
|
var reviewHookType models.HookEventType
|
|
|
|
|
2019-10-18 02:33:19 -06:00
|
|
|
switch review.Type {
|
2019-08-14 09:32:19 -06:00
|
|
|
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 02:33:19 -06:00
|
|
|
return nil
|
2019-08-14 09:32:19 -06:00
|
|
|
}
|
|
|
|
|
2019-10-18 02:33:19 -06:00
|
|
|
pr := review.Issue.PullRequest
|
2019-08-14 09:32:19 -06:00
|
|
|
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
2019-10-18 02:33:19 -06:00
|
|
|
return err
|
2019-08-14 09:32:19 -06:00
|
|
|
}
|
|
|
|
|
2019-10-18 02:33:19 -06:00
|
|
|
mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
|
2019-08-14 09:32:19 -06:00
|
|
|
if err != nil {
|
2019-10-18 02:33:19 -06:00
|
|
|
return err
|
2019-08-14 09:32:19 -06:00
|
|
|
}
|
2019-10-18 02:33:19 -06:00
|
|
|
if err := models.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
|
2019-08-14 09:32:19 -06:00
|
|
|
Action: api.HookIssueSynchronized,
|
2019-10-18 02:33:19 -06:00
|
|
|
Index: review.Issue.Index,
|
2019-08-14 09:32:19 -06:00
|
|
|
PullRequest: pr.APIFormat(),
|
2019-10-18 02:33:19 -06:00
|
|
|
Repository: review.Issue.Repo.APIFormat(mode),
|
|
|
|
Sender: review.Reviewer.APIFormat(),
|
2019-10-18 16:42:04 -06:00
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
2019-08-14 09:32:19 -06:00
|
|
|
}); err != nil {
|
2019-10-18 02:33:19 -06:00
|
|
|
return err
|
2019-08-14 09:32:19 -06:00
|
|
|
}
|
2019-10-18 02:33:19 -06:00
|
|
|
go models.HookQueue.Add(review.Issue.Repo.ID)
|
2019-08-14 09:32:19 -06:00
|
|
|
|
2019-10-18 02:33:19 -06:00
|
|
|
return nil
|
2019-08-14 09:32:19 -06:00
|
|
|
}
|