From c021890930bc1f1e52ac538e3eab4c2574513dfa Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 14 Aug 2019 23:32:19 +0800 Subject: [PATCH] move CreateReview to moduels/pull (#7841) --- models/review.go | 37 ------------------------ modules/pull/review.go | 57 +++++++++++++++++++++++++++++++++++++ routers/repo/pull_review.go | 4 ++- 3 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 modules/pull/review.go diff --git a/models/review.go b/models/review.go index 458d58152..e31e936bc 100644 --- a/models/review.go +++ b/models/review.go @@ -8,7 +8,6 @@ import ( "fmt" "code.gitea.io/gitea/modules/log" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "github.com/go-xorm/xorm" @@ -235,42 +234,6 @@ func createReview(e Engine, opts CreateReviewOptions) (*Review, error) { return nil, err } - var reviewHookType HookEventType - - switch opts.Type { - case ReviewTypeApprove: - reviewHookType = HookEventPullRequestApproved - case ReviewTypeComment: - reviewHookType = HookEventPullRequestComment - case ReviewTypeReject: - reviewHookType = 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 := AccessLevel(opts.Issue.Poster, opts.Issue.Repo) - if err != nil { - return nil, err - } - - if err := 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 HookQueue.Add(opts.Issue.Repo.ID) - return review, nil } diff --git a/modules/pull/review.go b/modules/pull/review.go new file mode 100644 index 000000000..3fdfaaff8 --- /dev/null +++ b/modules/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 +} diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go index 7576ebe90..cf9378304 100644 --- a/routers/repo/pull_review.go +++ b/routers/repo/pull_review.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + pull_service "code.gitea.io/gitea/modules/pull" ) // CreateCodeComment will create a code comment including an pending review if required @@ -53,7 +54,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) { } // No pending review exists // Create a new pending review for this issue & user - if review, err = models.CreateReview(models.CreateReviewOptions{ + if review, err = pull_service.CreateReview(models.CreateReviewOptions{ Type: models.ReviewTypePending, Reviewer: ctx.User, Issue: issue, @@ -61,6 +62,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) { ctx.ServerError("CreateCodeComment", err) return } + } } if review.ID == 0 {