Skip to content

Commit 6c1a31f

Browse files
adelowojonasfranz
authored andcommitted
User shouldn't be able to approve or reject his/her own PR (#4729)
* Make sure author cannot reject/approve their own PR * Disable buttons in templates too * Remove unneccessary if check since the switch below catches it * Fix IsOwner check * Update template and remove new template variable * Add alert template and redirect to diff page on review failure * Redirect to files diff as a little update to #4632
1 parent fa93857 commit 6c1a31f

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ issues.dependency.add_error_dep_not_exist = Dependency does not exist.
814814
issues.dependency.add_error_dep_exists = Dependency already exists.
815815
issues.dependency.add_error_cannot_create_circular = You cannot create a dependency with two issues blocking each other.
816816
issues.dependency.add_error_dep_not_same_repo = Both issues must be in the same repository.
817+
issues.review.self.approval = You cannot approve your own pull request.
818+
issues.review.self.rejection = You cannot request changes on your own pull request.
817819
issues.review.approve = "approved these changes %s"
818820
issues.review.comment = "reviewed %s"
819821
issues.review.content.empty = You need to leave a comment indicating the requested change(s).

routers/repo/pull_review.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,34 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
103103
var err error
104104

105105
reviewType := form.ReviewType()
106-
if reviewType == models.ReviewTypeUnknown {
106+
107+
switch reviewType {
108+
case models.ReviewTypeUnknown:
107109
ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
108110
return
111+
112+
// can not approve/reject your own PR
113+
case models.ReviewTypeApprove, models.ReviewTypeReject:
114+
115+
if issue.Poster.ID == ctx.User.ID {
116+
117+
var translated string
118+
119+
if reviewType == models.ReviewTypeApprove {
120+
translated = ctx.Tr("repo.issues.review.self.approval")
121+
} else {
122+
translated = ctx.Tr("repo.issues.review.self.rejection")
123+
}
124+
125+
ctx.Flash.Error(translated)
126+
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
127+
return
128+
}
109129
}
110130

111131
if form.HasEmptyContent() {
112132
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
113-
ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index))
133+
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
114134
return
115135
}
116136

templates/repo/diff/new_review.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
placeholder="{{$.i18n.Tr "repo.diff.review.placeholder"}}"></textarea>
1717
</div>
1818
<div class="ui divider"></div>
19-
<button type="submit" name="type" value="approve"
19+
<button type="submit" name="type" value="approve" {{ if and $.IsSigned ($.Issue.IsPoster $.SignedUser.ID) }} disabled {{ end }}
2020
class="ui submit green tiny button btn-submit">{{$.i18n.Tr "repo.diff.review.approve"}}</button>
2121
<button type="submit" name="type" value="comment"
22-
class="ui submit tiny basic button btn-submit">{{$.i18n.Tr "repo.diff.review.comment"}}</button>
23-
<button type="submit" name="type" value="reject"
22+
class="ui submit tiny basic button btn-submit">{{$.i18n.Tr "repo.diff.review.comment"}}</button>
23+
<button type="submit" name="type" value="reject" {{ if and $.IsSigned ($.Issue.IsPoster $.SignedUser.ID) }} disabled {{ end }}
2424
class="ui submit red tiny button btn-submit">{{$.i18n.Tr "repo.diff.review.reject"}}</button>
2525
</form>
2626
</div>

templates/repo/pulls/files.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<div class="ui divider"></div>
1212
{{template "repo/issue/view_title" .}}
1313
{{template "repo/pulls/tab_menu" .}}
14+
{{template "base/alert" .}}
1415
<div class="ui bottom attached tab pull segment active">
1516
{{template "repo/diff/box" .}}
1617
</div>

0 commit comments

Comments
 (0)