remove unnecessary code

This commit is contained in:
Lunny Xiao 2019-11-13 17:34:57 +08:00
parent 0f5de85f9a
commit 7735d70d66
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
5 changed files with 115 additions and 123 deletions

View File

@ -5,14 +5,12 @@
package models
import (
"fmt"
"strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder"
"xorm.io/core"
"xorm.io/xorm"
)
// ReviewType defines the sort of feedback a review gives
@ -86,6 +84,11 @@ func (r *Review) loadReviewer(e Engine) (err error) {
return
}
// LoadReviewer loads reviewer
func (r *Review) LoadReviewer() error {
return r.loadReviewer(x)
}
func (r *Review) loadAttributes(e Engine) (err error) {
if err = r.loadReviewer(e); err != nil {
return
@ -101,54 +104,6 @@ func (r *Review) LoadAttributes() error {
return r.loadAttributes(x)
}
// Publish will send notifications / actions to participants for all code comments; parts are concurrent
func (r *Review) Publish() error {
return r.publish(x)
}
func (r *Review) publish(e *xorm.Engine) error {
if r.Type == ReviewTypePending || r.Type == ReviewTypeUnknown {
return fmt.Errorf("review cannot be published if type is pending or unknown")
}
if r.Issue == nil {
if err := r.loadIssue(e); err != nil {
return err
}
}
if err := r.Issue.loadRepo(e); err != nil {
return err
}
if len(r.CodeComments) == 0 {
if err := r.loadCodeComments(e); err != nil {
return err
}
}
for _, lines := range r.CodeComments {
for _, comments := range lines {
for _, comment := range comments {
go func(en *xorm.Engine, review *Review, comm *Comment) {
sess := en.NewSession()
defer sess.Close()
opts := &CreateCommentOptions{
Doer: comm.Poster,
Issue: review.Issue,
Repo: review.Issue.Repo,
Type: comm.Type,
Content: comm.Content,
}
if err := updateCommentInfos(sess, opts, comm); err != nil {
log.Warn("updateCommentInfos: %v", err)
}
if err := sendCreateCommentAction(sess, opts, comm); err != nil {
log.Warn("sendCreateCommentAction: %v", err)
}
}(e, r, comment)
}
}
}
return nil
}
func getReviewByID(e Engine, id int64) (*Review, error) {
review := new(Review)
if has, err := e.ID(id).Get(review); err != nil {
@ -271,12 +226,73 @@ func GetCurrentReview(reviewer *User, issue *Issue) (*Review, error) {
return getCurrentReview(x, reviewer, issue)
}
// UpdateReview will update all cols of the given review in db
func UpdateReview(r *Review) error {
if _, err := x.ID(r.ID).AllCols().Update(r); err != nil {
return err
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 *User, issue *Issue, reviewType ReviewType, content string) (*Review, *Comment, error) {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return nil, nil, err
}
return nil
review, err := getCurrentReview(sess, doer, issue)
if err != nil {
if !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 = createReview(sess, 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 := sess.ID(review.ID).Cols("content, typpe").Update(review); err != nil {
return nil, nil, err
}
}
comm, err := createComment(sess, &CreateCommentOptions{
Type: CommentTypeReview,
Doer: doer,
Content: review.Content,
Issue: issue,
Repo: issue.Repo,
ReviewID: review.ID,
})
if err != nil || comm == nil {
return nil, nil, err
}
comm.Review = review
if err := updateIssueCols(sess, review.Issue, "updated_unix"); err != nil {
return nil, nil, err
}
return review, comm, sess.Commit()
}
// PullReviewersWithType represents the type used to display a review overview

View File

@ -98,14 +98,6 @@ func TestCreateReview(t *testing.T) {
AssertExistsAndLoadBean(t, &Review{Content: "New Review"})
}
func TestUpdateReview(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
review.Content = "Updated Review"
assert.NoError(t, UpdateReview(review))
AssertExistsAndLoadBean(t, &Review{ID: 1, Content: "Updated Review"})
}
func TestGetReviewersByPullID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

View File

@ -6,6 +6,7 @@ package action
import (
"fmt"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
@ -117,3 +118,41 @@ func (a *actionNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *
log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
}
}
func (a *actionNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
if err := review.LoadReviewer(); err != nil {
log.Error("LoadReviewer '%d/%d': %v", review.ID, review.ReviewerID, err)
return
}
var content = comment.Content
if err := review.LoadCodeComments(); err != nil {
log.Error("LoadCodeComments '%d/%d': %v", review.Reviewer.ID, review.ID, err)
return
}
for _, lines := range review.CodeComments {
for _, comments := range lines {
for _, comment := range comments {
// TODO: how to handle???
/*if err := sendCreateCommentAction(sess, opts, comm); err != nil {
log.Warn("sendCreateCommentAction: %v", err)
}*/
content += "\n" + comment.Content
}
}
}
if err := models.NotifyWatchers(&models.Action{
ActUserID: review.Reviewer.ID,
ActUser: review.Reviewer,
Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(content, "\n")[0]),
OpType: models.ActionCommentIssue,
RepoID: review.Issue.RepoID,
Repo: review.Issue.Repo,
IsPrivate: review.Issue.Repo.IsPrivate,
Comment: comment,
CommentID: comment.ID,
}); err != nil {
log.Error("notify watchers '%d/%d': %v", review.Reviewer.ID, review.Issue.RepoID, err)
}
}

View File

@ -76,7 +76,7 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
reviewType := form.ReviewType()
switch reviewType {
case models.ReviewTypeUnknown:
ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
ctx.ServerError("ReviewType", fmt.Errorf("unknown ReviewType: %s", form.Type))
return
// can not approve/reject your own PR
@ -97,13 +97,12 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
_, comm, err := pull_service.SubmitReview(ctx.User, issue, reviewType, form.Content)
if err != nil {
if pull_service.IsContentEmptyErr(err) {
if models.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)
}
return
}

View File

@ -131,68 +131,14 @@ func createCodeComment(doer *models.User, repo *models.Repository, issue *models
})
}
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)
review, comm, err := models.SubmitReview(doer, issue, reviewType, content)
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
}
// publish review
pr, err := issue.GetPullRequest()
if err != nil {
return nil, nil, err