Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move CreateReview to modules/pull #7841

Merged
merged 3 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
57 changes: 57 additions & 0 deletions modules/pull/review.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion routers/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,14 +54,15 @@ 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,
}); err != nil {
ctx.ServerError("CreateCodeComment", err)
return
}

}
}
if review.ID == 0 {
Expand Down