Skip to content

Commit 0df7cab

Browse files
adelowolafriks
authored andcommitted
prevent empty review comment (#4632)
* prevent empty review comment This would only require a comment for rejection and comment * add tests * add comment
1 parent 59b10e6 commit 0df7cab

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

modules/auth/repo_form.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ func (f SubmitReviewForm) ReviewType() models.ReviewType {
402402
}
403403
}
404404

405+
// HasEmptyContent checks if the content of the review form is empty.
406+
func (f SubmitReviewForm) HasEmptyContent() bool {
407+
reviewType := f.ReviewType()
408+
409+
return (reviewType == models.ReviewTypeComment || reviewType == models.ReviewTypeReject) &&
410+
len(strings.TrimSpace(f.Content)) == 0
411+
}
412+
405413
// __________ .__
406414
// \______ \ ____ | | ____ _____ ______ ____
407415
// | _// __ \| | _/ __ \\__ \ / ___// __ \

modules/auth/repo_form_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package auth
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestSubmitReviewForm_IsEmpty(t *testing.T) {
14+
15+
cases := []struct {
16+
form SubmitReviewForm
17+
expected bool
18+
}{
19+
// Approved PR with a comment shouldn't count as empty
20+
{SubmitReviewForm{Type: "approve", Content: "Awesome"}, false},
21+
22+
// Approved PR without a comment shouldn't count as empty
23+
{SubmitReviewForm{Type: "approve", Content: ""}, false},
24+
25+
// Rejected PR without a comment should count as empty
26+
{SubmitReviewForm{Type: "reject", Content: ""}, true},
27+
28+
// Rejected PR with a comment shouldn't count as empty
29+
{SubmitReviewForm{Type: "reject", Content: "Awesome"}, false},
30+
31+
// Comment review on a PR with a comment shouldn't count as empty
32+
{SubmitReviewForm{Type: "comment", Content: "Awesome"}, false},
33+
34+
// Comment review on a PR without a comment should count as empty
35+
{SubmitReviewForm{Type: "comment", Content: ""}, true},
36+
}
37+
38+
for _, v := range cases {
39+
assert.Equal(t, v.expected, v.form.HasEmptyContent())
40+
}
41+
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ issues.dependency.add_error_cannot_create_circular = You cannot create a depende
815815
issues.dependency.add_error_dep_not_same_repo = Both issues must be in the same repository.
816816
issues.review.approve = "approved these changes %s"
817817
issues.review.comment = "reviewed %s"
818+
issues.review.content.empty = You need to leave a comment indicating the requested change(s).
818819
issues.review.reject = "rejected these changes %s"
819820
issues.review.pending = Pending
820821
issues.review.review = Review

routers/repo/pull_review.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
107107
ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
108108
return
109109
}
110+
111+
if form.HasEmptyContent() {
112+
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
113+
ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index))
114+
return
115+
}
116+
110117
review, err = models.GetCurrentReview(ctx.User, issue)
111118
if err != nil {
112119
if !models.IsErrReviewNotExist(err) {

0 commit comments

Comments
 (0)