[x]
):We are using Gitea on a small team (5 programmers) and fairly regularly run into a case where the "Files Changed" tab on a pull request is no longer accessible with some variation on this error message:
1.12.2:
template: repo/diff/box:152:65: executing "repo/diff/box" at <0>: nil pointer evaluating *models.Review.Type
1.12.1:
template: repo/diff/section_unified:28:53: executing "repo/diff/section_unified" at <0>: nil pointer evaluating *models.Review.Type
We have sometimes been able to find the offending comment in the database and update its Review ID to the appropriate ID. This gets difficult when a lot of comments have been made on other PRs between noticing the problem and trying to fix it.
We don't have a specific reproduction case that triggers this, but in general it seems it is sometimes possible to fail to assign the appropriate review ID in a comment which then breaks the ability to review changes line-by-line on a given PR.
Nothing to show, really, since it's just an error. Loading the Files Changed tab attempts to GET /org/repo/pulls/:pull_id/files, which returns 500 and the above error as plain text.
I seem to have something similar. After commenting for a while on the Files Changed
page, this eventually displayed in my browser:
template: repo/diff/box:152:65: executing "repo/diff/box" at <0>: nil pointer evaluating *models.Review.Type
I don't recall any comment being anything special other than containing some code snippets and a weblink. Now I cannot access that part of the PR anymore :-(.
To what review ID that comment was pointing to (other PR/repo)?
To what review ID that comment was pointing to (other PR/repo)?
I don't know, I was using the Add single comment
button every time. Do you have any hint on how to get that info?
Later in the day, I decided to avoid the Add single comment
button by writing a bunch of comments in a single review submission instead. Later I got a 500 error when I tried to submit the review.
We have since downgraded 1.11.3 (the version we had before upgrading to 1.12.0/1), I've tried all the review / comment functionality and everything seems to work fine again.
We upgraded simply by pulling the new docker image and starting it on top of our pre-existing gitea database. Is it possible the database from 1.11.3 are not compatible with 1.12.0/1?
To what review ID that comment was pointing to (other PR/repo)?
For our case, the comment should have been pointing at PR 35, internal review id 192 or something. I think, based on perusing the pgsql database a bit, that the review ID is actually set to 0 on the offending comment.
I was using the
Add single comment
button
This is how most of our review comments are done as well.
Later I got a 500 error when I tried to submit the review.
We see this occasionally as well, usually when the code has changed between the time the PR was loaded and the comment was submitted, but we see it other times as well.
Is it possible the database from 1.11.3 are not compatible with 1.12.0/1?
1.12.1 was the first version we installed and we are seeing the issue, so I think it's unlikely to be a db upgrade bug.
Thanks, will try to reproduce
having the same problem as well. I added several pieces of comments with Add single comment
, then click on File Changed
tab, problem appeared.
template: repo/diff/box:152:65: executing "repo/diff/box" at <0>: nil pointer evaluating *models.Review.Type
I am using Gitea 1.12.2, too. Windows Server 2008, MySQL.
I can confirm that the issue exists on on Gitea 1.12.3. with sqlite. It worked after creation of the PR, but broke after 1 or 2 inline comments were added. So I guess there is an issue with rendering already existing comments?
Also, one user noted that his comments were not visible at all in the beginning. Not sure if this is related or reproducible.
Having the same issue, let me know if there is anything I can do to help debug this.
I initially was wondering if this was some issue with a certain number of reviews but I just added >80 reviews to a PR on my test instance and it hasn't failed (albeit this is on master/1.13) Is anyone able to confirm that the bug is not present on master/try?
I would guess that at least a temporary solution would involve monkey patching the template repo/diff/box.tmpl to prevent the NPE around line 152.
Very stable to reproduce here: https://try.gitea.io/hardi/pull-request-test/pulls/1/files
Cool that means it's definitely not just a 1.12 issue. It's really weird that I can't forcibly reproduce on my dev machine
Very stable to reproduce here: https://try.gitea.io/hardi/pull-request-test/pulls/1/files
Thank you! I'll update the original post with this link.
This is interesting - I can't replicate it on my dev box but if you look at the API you see an interesting finding:
curl -X GET "https://try.gitea.io/api/v1/repos/hardi/pull-request-test/pulls/1/reviews" -H "accept: application/json"
[
{
"id": 226,
"user": {
"id": 0,
"login": "hardi",
"full_name": "",
"email": "[email protected]",
"avatar_url": "https://try.gitea.io/user/avatar/hardi/-1",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2019-02-11T07:23:04Z",
"username": "hardi"
},
"state": "COMMENT",
"body": "make change man",
"commit_id": "",
"stale": false,
"official": false,
"comments_count": 0,
"submitted_at": "2019-02-11T07:25:41Z",
"html_url": "https://try.gitea.io/hardi/pull-request-test/pulls/1#issuecomment-9975",
"pull_request_url": "https://try.gitea.io/hardi/pull-request-test/pulls/1"
},
...
]
Vs a sort of equivalent:
curl -X GET "http://localhost/gitea/api/v1/repos/administrator/pull-request-test/pulls/1/reviews" -H "accept: application/json"
[
{
"id": 78,
"user": {
"id": 1,
"login": "administrator",
"full_name": "",
"email": "[email protected]",
"avatar_url": "http://localhost/gitea/user/avatar/administrator/-1",
"language": "en-US",
"is_admin": true,
"last_login": "2020-07-31T09:58:07+01:00",
"created": "2020-02-15T13:21:17Z",
"username": "administrator"
},
"state": "COMMENT",
"body": "",
"commit_id": "25d641639d06ce48c88ec4142a9b0f6bcb45e813",
"stale": false,
"official": false,
"comments_count": 2,
"submitted_at": "2020-08-05T14:33:27+01:00",
"html_url": "http://localhost/gitea/administrator/pull-request-test/pulls/1#issuecomment-255",
"pull_request_url": "http://localhost/gitea/administrator/pull-request-test/pulls/1"
},
...
]
Note the commit_id is empty on the broken try instance.
We got this error as well in 1.12.1. Only few persons reported this to us, maybe the amount of people having this is greater than I am aware of.
I think I've tracked this down --
To reproduce:
1: Comment on a specific line of a PR
2: Make a commit that changes the line and automatically marks the previous comments as outdated (per https://github.com/go-gitea/gitea/pull/9532)
3: Go back to files view which now doesn't show previous review
4: Leave a comment on the same line as previous
See error above
So this process causes new comments to be created with a reviewID of 0 which then triggers the NPE above because the comment will have no review loaded.
Seems to be this bit:
Will see the previous comment that has been invalidated by a commit because it only looks to see if there is an existing comment at that line.
Then here:
It leaves a comment with replyReviewID instead of trying to load the actual review and use that ID because it thinks one already exists and this is just a comment. Not sure, but replyReviewID appears to always be 0 from here:
(form.Reply)
Perhaps this:
Should check for comment.invalidated as well? Not sure how this is supposed to work but that seems to be what causes the bug. Also would probably need something to either fix what will now be broken comments or change the template to ignore them and avoid this error...
Here's a quick patch to prevent the NPE whilst we fix the underlying issue.
From ff164dabec13c9c2d10593fc5ffcd0d316018ada Mon Sep 17 00:00:00 2001
From: Andrew Thornton <[email protected]>
Date: Thu, 20 Aug 2020 17:50:35 +0100
Subject: [PATCH] Prevent NPE in template generation
Prevent the NPE in #12239 by assuming that a comment without a Review is
non-pending.
Related #12239
Signed-off-by: Andrew Thornton <[email protected]>
---
templates/repo/diff/box.tmpl | 5 ++++-
templates/repo/diff/section_unified.tmpl | 5 ++++-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl
index 7338c1ee6..397356fe6 100644
--- a/templates/repo/diff/box.tmpl
+++ b/templates/repo/diff/box.tmpl
@@ -155,7 +155,10 @@
{{if gt (len $line.Comments) 0}}
{{$resolved := (index $line.Comments 0).IsResolved}}
{{$resolveDoer := (index $line.Comments 0).ResolveDoer}}
- {{$isNotPending := (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{$isNotPending := false}}
+ {{if (index $line.Comments 0).Review}}
+ {{$isNotPending = (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{end}}
<tr class="add-code-comment">
<td class="lines-num"></td>
<td class="lines-type-marker"></td>
diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl
index 5f87a4176..2508c01fe 100644
--- a/templates/repo/diff/section_unified.tmpl
+++ b/templates/repo/diff/section_unified.tmpl
@@ -35,7 +35,10 @@
{{if gt (len $line.Comments) 0}}
{{$resolved := (index $line.Comments 0).IsResolved}}
{{$resolveDoer := (index $line.Comments 0).ResolveDoer}}
- {{$isNotPending := (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{$isNotPending := false}}
+ {{if (index $line.Comments 0).Review}}
+ {{$isNotPending = (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{end}}
<tr>
<td colspan="2" class="lines-num"></td>
<td class="add-comment-left add-comment-right" colspan="2">
--
2.25.1
Hmm, If we turn this:
to:
if !isReview && replyReviewID != 0 {
This will prevent the issue.
Current suggested patch
From 309fd941612a8d278e4520a22a32d19f03a7b75f Mon Sep 17 00:00:00 2001
From: Andrew Thornton <[email protected]>
Date: Thu, 20 Aug 2020 17:50:35 +0100
Subject: [PATCH] Prevent NPE on commenting on lines with invalidated comments
Only check for a review if we are replying to a previous review.
Prevent the NPE in #12239 by assuming that a comment without a Review is
non-pending.
Fix #12239
Signed-off-by: Andrew Thornton <[email protected]>
---
services/pull/review.go | 2 +-
templates/repo/diff/box.tmpl | 5 ++++-
templates/repo/diff/section_unified.tmpl | 5 ++++-
3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/services/pull/review.go b/services/pull/review.go
index 25e0ca858..5a77a4da1 100644
--- a/services/pull/review.go
+++ b/services/pull/review.go
@@ -29,7 +29,7 @@ func CreateCodeComment(doer *models.User, gitRepo *git.Repository, issue *models
// - Comments that are part of a review
// - Comments that reply to an existing review
- if !isReview {
+ if !isReview && replyReviewID != 0 {
// It's not part of a review; maybe a reply to a review comment or a single comment.
// Check if there are reviews for that line already; if there are, this is a reply
if existsReview, err = models.ReviewExists(issue, treePath, line); err != nil {
diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl
index 7338c1ee6..397356fe6 100644
--- a/templates/repo/diff/box.tmpl
+++ b/templates/repo/diff/box.tmpl
@@ -155,7 +155,10 @@
{{if gt (len $line.Comments) 0}}
{{$resolved := (index $line.Comments 0).IsResolved}}
{{$resolveDoer := (index $line.Comments 0).ResolveDoer}}
- {{$isNotPending := (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{$isNotPending := false}}
+ {{if (index $line.Comments 0).Review}}
+ {{$isNotPending = (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{end}}
<tr class="add-code-comment">
<td class="lines-num"></td>
<td class="lines-type-marker"></td>
diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl
index 5f87a4176..2508c01fe 100644
--- a/templates/repo/diff/section_unified.tmpl
+++ b/templates/repo/diff/section_unified.tmpl
@@ -35,7 +35,10 @@
{{if gt (len $line.Comments) 0}}
{{$resolved := (index $line.Comments 0).IsResolved}}
{{$resolveDoer := (index $line.Comments 0).ResolveDoer}}
- {{$isNotPending := (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{$isNotPending := false}}
+ {{if (index $line.Comments 0).Review}}
+ {{$isNotPending = (not (eq (index $line.Comments 0).Review.Type 0))}}
+ {{end}}
<tr>
<td colspan="2" class="lines-num"></td>
<td class="add-comment-left add-comment-right" colspan="2">
--
2.25.1
Apologies that this took so long to fix.
Most helpful comment
I think I've tracked this down --
To reproduce:
1: Comment on a specific line of a PR
2: Make a commit that changes the line and automatically marks the previous comments as outdated (per https://github.com/go-gitea/gitea/pull/9532)
3: Go back to files view which now doesn't show previous review
4: Leave a comment on the same line as previous
See error above
So this process causes new comments to be created with a reviewID of 0 which then triggers the NPE above because the comment will have no review loaded.
Seems to be this bit:
https://github.com/go-gitea/gitea/blob/74bd9691c685942798f2761607731697498ceeae/services/pull/review.go#L32-L37
Will see the previous comment that has been invalidated by a commit because it only looks to see if there is an existing comment at that line.
Then here:
https://github.com/go-gitea/gitea/blob/74bd9691c685942798f2761607731697498ceeae/services/pull/review.go#L40-L54
It leaves a comment with replyReviewID instead of trying to load the actual review and use that ID because it thinks one already exists and this is just a comment. Not sure, but replyReviewID appears to always be 0 from here:
https://github.com/go-gitea/gitea/blob/74bd9691c685942798f2761607731697498ceeae/routers/repo/pull_review.go#L38-L48
(form.Reply)
Perhaps this:
https://github.com/go-gitea/gitea/blob/74bd9691c685942798f2761607731697498ceeae/models/review.go#L265-L268
Should check for comment.invalidated as well? Not sure how this is supposed to work but that seems to be what causes the bug. Also would probably need something to either fix what will now be broken comments or change the template to ignore them and avoid this error...