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