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