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

prevent empty review comment #4632

Merged
merged 3 commits into from
Aug 7, 2018
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
8 changes: 8 additions & 0 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ func (f SubmitReviewForm) ReviewType() models.ReviewType {
}
}

// HasEmptyContent checks if the content of the review form is empty.
func (f SubmitReviewForm) HasEmptyContent() bool {
reviewType := f.ReviewType()

return (reviewType == models.ReviewTypeComment || reviewType == models.ReviewTypeReject) &&
len(strings.TrimSpace(f.Content)) == 0
}

// __________ .__
// \______ \ ____ | | ____ _____ ______ ____
// | _// __ \| | _/ __ \\__ \ / ___// __ \
Expand Down
41 changes: 41 additions & 0 deletions modules/auth/repo_form_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 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 auth

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSubmitReviewForm_IsEmpty(t *testing.T) {

cases := []struct {
form SubmitReviewForm
expected bool
}{
// Approved PR with a comment shouldn't count as empty
{SubmitReviewForm{Type: "approve", Content: "Awesome"}, false},

// Approved PR without a comment shouldn't count as empty
{SubmitReviewForm{Type: "approve", Content: ""}, false},

// Rejected PR without a comment should count as empty
{SubmitReviewForm{Type: "reject", Content: ""}, true},

// Rejected PR with a comment shouldn't count as empty
{SubmitReviewForm{Type: "reject", Content: "Awesome"}, false},

// Comment review on a PR with a comment shouldn't count as empty
{SubmitReviewForm{Type: "comment", Content: "Awesome"}, false},

// Comment review on a PR without a comment should count as empty
{SubmitReviewForm{Type: "comment", Content: ""}, true},
}

for _, v := range cases {
assert.Equal(t, v.expected, v.form.HasEmptyContent())
}
}
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ issues.dependency.add_error_cannot_create_circular = You cannot create a depende
issues.dependency.add_error_dep_not_same_repo = Both issues must be in the same repository.
issues.review.approve = "approved these changes %s"
issues.review.comment = "reviewed %s"
issues.review.content.empty = You need to leave a comment indicating the requested change(s).
issues.review.reject = "rejected these changes %s"
issues.review.pending = Pending
issues.review.review = Review
Expand Down
7 changes: 7 additions & 0 deletions routers/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
return
}

if form.HasEmptyContent() {
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index))
return
}

review, err = models.GetCurrentReview(ctx.User, issue)
if err != nil {
if !models.IsErrReviewNotExist(err) {
Expand Down