aboutsummaryrefslogtreecommitdiffstats
path: root/services/pull/review.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-09-27 08:22:36 +0800
committerGitHub <noreply@github.com>2019-09-27 08:22:36 +0800
commiteb11ca68470e4ab60ed4ec31f4d170a4b36a528e (patch)
tree48347da8ba6c78d6ca30ac6ae5949fa93bb4c459 /services/pull/review.go
parentd958b9db4fa0f1910b3ca82338e3d68a70efedd9 (diff)
downloadgitea-eb11ca68470e4ab60ed4ec31f4d170a4b36a528e.tar.gz
gitea-eb11ca68470e4ab60ed4ec31f4d170a4b36a528e.zip
Extract actions on new pull request from models to pulls service and move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull (#8218)
* extract actions on new pull request from models to pulls service * improve code * move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull * fix fmt * Rename pulls.go to pull.go
Diffstat (limited to 'services/pull/review.go')
-rw-r--r--services/pull/review.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/services/pull/review.go b/services/pull/review.go
new file mode 100644
index 0000000000..3fdfaaff84
--- /dev/null
+++ b/services/pull/review.go
@@ -0,0 +1,57 @@
+// 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
+ }
+
+ var reviewHookType models.HookEventType
+
+ switch opts.Type {
+ case models.ReviewTypeApprove:
+ reviewHookType = models.HookEventPullRequestApproved
+ case models.ReviewTypeComment:
+ reviewHookType = models.HookEventPullRequestComment
+ case models.ReviewTypeReject:
+ reviewHookType = models.HookEventPullRequestRejected
+ default:
+ // unsupported review webhook type here
+ return review, nil
+ }
+
+ pr := opts.Issue.PullRequest
+
+ if err := pr.LoadIssue(); err != nil {
+ return nil, err
+ }
+
+ mode, err := models.AccessLevel(opts.Issue.Poster, opts.Issue.Repo)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := models.PrepareWebhooks(opts.Issue.Repo, reviewHookType, &api.PullRequestPayload{
+ Action: api.HookIssueSynchronized,
+ Index: opts.Issue.Index,
+ PullRequest: pr.APIFormat(),
+ Repository: opts.Issue.Repo.APIFormat(mode),
+ Sender: opts.Reviewer.APIFormat(),
+ }); err != nil {
+ return nil, err
+ }
+ go models.HookQueue.Add(opts.Issue.Repo.ID)
+
+ return review, nil
+}