refactor submit review

This commit is contained in:
Lunny Xiao 2019-11-13 16:59:34 +08:00
parent 16a43156a8
commit 0f5de85f9a
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 197 additions and 183 deletions

View File

@ -11,8 +11,6 @@ import (
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
comment_service "code.gitea.io/gitea/services/comments"
pull_service "code.gitea.io/gitea/services/pull"
)
@ -31,64 +29,33 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
return
}
var comment *models.Comment
defer func() {
if comment != nil {
ctx.Redirect(comment.HTMLURL())
} else {
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
}
}()
signedLine := form.Line
if form.Side == "previous" {
signedLine *= -1
}
review := new(models.Review)
if form.IsReview {
var err error
// Check if the user has already a pending review for this issue
if review, err = models.GetCurrentReview(ctx.User, issue); err != nil {
if !models.IsErrReviewNotExist(err) {
ctx.ServerError("CreateCodeComment", err)
return
}
// No pending review exists
// Create a new pending review for this issue & user
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 {
review.ID = form.Reply
}
//FIXME check if line, commit and treepath exist
comment, err := comment_service.CreateCodeComment(
comment, err := pull_service.CreateCodeComment(
ctx.User,
issue.Repo,
issue,
signedLine,
form.Content,
form.TreePath,
signedLine,
review.ID,
form.IsReview,
form.Reply,
)
if err != nil {
ctx.ServerError("CreateCodeComment", err)
return
}
// Send no notification if comment is pending
if !form.IsReview || form.Reply != 0 {
notification.NotifyCreateIssueComment(ctx.User, issue.Repo, issue, comment)
}
log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
if comment != nil {
ctx.Redirect(comment.HTMLURL())
} else {
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
}
}
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
@ -105,11 +72,8 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
return
}
var review *models.Review
var err error
reviewType := form.ReviewType()
switch reviewType {
case models.ReviewTypeUnknown:
ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
@ -117,11 +81,8 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
// can not approve/reject your own PR
case models.ReviewTypeApprove, models.ReviewTypeReject:
if issue.Poster.ID == ctx.User.ID {
var translated string
if reviewType == models.ReviewTypeApprove {
translated = ctx.Tr("repo.issues.review.self.approval")
} else {
@ -134,69 +95,17 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
}
}
review, err = models.GetCurrentReview(ctx.User, issue)
if err == nil {
review.Issue = issue
if errl := review.LoadCodeComments(); errl != nil {
ctx.ServerError("LoadCodeComments", err)
return
}
}
if ((err == nil && len(review.CodeComments) == 0) ||
(err != nil && models.IsErrReviewNotExist(err))) &&
form.HasEmptyContent() {
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
return
}
_, comm, err := pull_service.SubmitReview(ctx.User, issue, reviewType, form.Content)
if err != nil {
if !models.IsErrReviewNotExist(err) {
ctx.ServerError("GetCurrentReview", err)
return
if pull_service.IsContentEmptyErr(err) {
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
} else {
ctx.ServerError("SubmitReview", err)
}
// No current review. Create a new one!
if review, err = pull_service.CreateReview(models.CreateReviewOptions{
Type: reviewType,
Issue: issue,
Reviewer: ctx.User,
Content: form.Content,
}); err != nil {
ctx.ServerError("CreateReview", err)
return
}
} else {
review.Content = form.Content
review.Type = reviewType
if err = pull_service.UpdateReview(review); err != nil {
ctx.ServerError("UpdateReview", err)
return
}
}
comm, err := models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeReview,
Doer: ctx.User,
Content: review.Content,
Issue: issue,
Repo: issue.Repo,
ReviewID: review.ID,
})
if err != nil || comm == nil {
ctx.ServerError("CreateComment", err)
return
}
if err = review.Publish(); err != nil {
ctx.ServerError("Publish", err)
return
}
pr, err := issue.GetPullRequest()
if err != nil {
ctx.ServerError("GetPullRequest", err)
return
}
notification.NotifyPullRequestReview(pr, review, comm)
ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
}

View File

@ -5,15 +5,8 @@
package comments
import (
"bytes"
"fmt"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
)
// CreateIssueComment creates a plain issue comment.
@ -35,60 +28,6 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
return comment, nil
}
// CreateCodeComment creates a plain code comment at the specified line / path
func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
var commitID, patch string
pr, err := models.GetPullRequestByIssueID(issue.ID)
if err != nil {
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
}
if err := pr.GetBaseRepo(); err != nil {
return nil, fmt.Errorf("GetHeadRepo: %v", err)
}
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()
// FIXME validate treePath
// Get latest commit referencing the commented line
// No need for get commit for base branch changes
if line > 0 {
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
if err == nil {
commitID = commit.ID.String()
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
}
}
// Only fetch diff if comment is review comment
if reviewID != 0 {
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
}
patchBuf := new(bytes.Buffer)
if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
}
patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
}
return models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
})
}
// UpdateComment updates information of comment.
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
if err := models.UpdateComment(c, doer); err != nil {

View File

@ -6,32 +6,198 @@
package pull
import (
"bytes"
"fmt"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
)
// CreateReview creates a new review based on opts
func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) {
review, err := models.CreateReview(opts)
// CreateCodeComment creates a comment on the code line
func CreateCodeComment(doer *models.User, issue *models.Issue, line int64, content string, treePath string, isReview bool, replyReviewID int64) (*models.Comment, error) {
// It's not a review, maybe a reply to a review comment or a single comment.
if !isReview {
if err := issue.LoadRepo(); err != nil {
return nil, err
}
comment, err := createCodeComment(
doer,
issue.Repo,
issue,
content,
treePath,
line,
replyReviewID,
)
if err != nil {
return nil, err
}
notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment)
return comment, nil
}
review, err := models.GetCurrentReview(doer, issue)
if err != nil {
if !models.IsErrReviewNotExist(err) {
return nil, err
}
review, err = models.CreateReview(models.CreateReviewOptions{
Type: models.ReviewTypePending,
Reviewer: doer,
Issue: issue,
})
if err != nil {
return nil, err
}
}
comment, err := createCodeComment(
doer,
issue.Repo,
issue,
content,
treePath,
line,
review.ID,
)
if err != nil {
return nil, err
}
if opts.Type != models.ReviewTypePending {
notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil)
}
// NOTICE: it's a pending review, so the notifications will not be fired until user submit review.
return review, nil
return comment, nil
}
// UpdateReview updates a review
func UpdateReview(review *models.Review) error {
err := models.UpdateReview(review)
// createCodeComment creates a plain code comment at the specified line / path
func createCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
var commitID, patch string
if err := issue.LoadPullRequest(); err != nil {
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
}
pr := issue.PullRequest
if err := pr.GetBaseRepo(); err != nil {
return nil, fmt.Errorf("GetHeadRepo: %v", err)
}
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
return err
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()
// FIXME validate treePath
// Get latest commit referencing the commented line
// No need for get commit for base branch changes
if line > 0 {
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
if err == nil {
commitID = commit.ID.String()
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
}
}
notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil)
return nil
// Only fetch diff if comment is review comment
if reviewID != 0 {
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
}
patchBuf := new(bytes.Buffer)
if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
}
patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
}
return models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
})
}
type ContentEmptyErr struct {
}
func (ContentEmptyErr) Error() string {
return "Review content is empty"
}
func IsContentEmptyErr(err error) bool {
_, ok := err.(ContentEmptyErr)
return ok
}
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
func SubmitReview(doer *models.User, issue *models.Issue, reviewType models.ReviewType, content string) (*models.Review, *models.Comment, error) {
review, err := models.GetCurrentReview(doer, issue)
if err != nil {
if !models.IsErrReviewNotExist(err) {
return nil, nil, err
}
if len(strings.TrimSpace(content)) == 0 {
return nil, nil, ContentEmptyErr{}
}
// No current review. Create a new one!
review, err = models.CreateReview(models.CreateReviewOptions{
Type: reviewType,
Issue: issue,
Reviewer: doer,
Content: content,
})
if err != nil {
return nil, nil, err
}
} else {
review.Issue = issue
review.Content = content
review.Type = reviewType
if err = models.UpdateReview(review); err != nil {
return nil, nil, err
}
if err := review.LoadCodeComments(); err != nil {
return nil, nil, err
}
}
comm, err := models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeReview,
Doer: doer,
Content: review.Content,
Issue: issue,
Repo: issue.Repo,
ReviewID: review.ID,
})
if err != nil || comm == nil {
return nil, nil, err
}
if err = review.Publish(); err != nil {
return nil, nil, err
}
pr, err := issue.GetPullRequest()
if err != nil {
return nil, nil, err
}
notification.NotifyPullRequestReview(pr, review, comm)
return review, comm, nil
}