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

Locked out of the ability to comment on the code of a PR sometimes #12239

Closed
2 of 7 tasks
parnic-sks opened this issue Jul 13, 2020 · 18 comments · Fixed by #12549
Closed
2 of 7 tasks

Locked out of the ability to comment on the code of a PR sometimes #12239

parnic-sks opened this issue Jul 13, 2020 · 18 comments · Fixed by #12549
Labels
issue/critical This issue should be fixed ASAP. If it is a PR, the PR should be merged ASAP type/bug
Milestone

Comments

@parnic-sks
Copy link

parnic-sks commented Jul 13, 2020

Description

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.

Screenshots

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.

@aloosley
Copy link

aloosley commented Jul 14, 2020

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 :-(.


  • Gitea version (or commit ref): 1.12.1 --> Problem persisted after downgrading to 1.12.0 --> Update problem goes away when downgrading to 1.11.3 (maybe 1.11.* in general but haven't tested)
  • Operating system: Pop_OS! 20.04

@lafriks
Copy link
Member

lafriks commented Jul 14, 2020

To what review ID that comment was pointing to (other PR/repo)?

@aloosley
Copy link

aloosley commented Jul 15, 2020

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?

@parnic-sks
Copy link
Author

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.

@lafriks
Copy link
Member

lafriks commented Jul 15, 2020

Thanks, will try to reproduce

@tanrui8765
Copy link

tanrui8765 commented Jul 22, 2020

having the same problem as well. I added several pieces of comments with Add single comment, then click on File Changed tab, problem appeared.

image

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.

@svenseeberg
Copy link
Contributor

svenseeberg commented Jul 30, 2020

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.

@bendem
Copy link

bendem commented Aug 4, 2020

Having the same issue, let me know if there is anything I can do to help debug this.

@zeripath
Copy link
Contributor

zeripath commented Aug 4, 2020

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.

@singalen
Copy link

singalen commented Aug 5, 2020

Very stable to reproduce here: https://try.gitea.io/hardi/pull-request-test/pulls/1/files

@zeripath zeripath closed this as completed Aug 5, 2020
@zeripath zeripath reopened this Aug 5, 2020
@zeripath
Copy link
Contributor

zeripath commented Aug 5, 2020

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

@parnic-sks
Copy link
Author

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.

@zeripath
Copy link
Contributor

zeripath commented Aug 5, 2020

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": "testuseer@labfolder.com",
      "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": "administrator@example.com",
      "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.

@lunny lunny added the issue/critical This issue should be fixed ASAP. If it is a PR, the PR should be merged ASAP label Aug 6, 2020
@Ahaus314
Copy link

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.

@mrsdizzie
Copy link
Member

mrsdizzie commented Aug 11, 2020

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 #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:

if !isReview {
// 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 {
return nil, err
}

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:

// Comments that are replies don't require a review header to show up in the issue view
if !isReview && existsReview {
if err = issue.LoadRepo(); err != nil {
return nil, err
}
comment, err := createCodeComment(
doer,
issue.Repo,
issue,
content,
treePath,
line,
replyReviewID,
)

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:

comment, err := pull_service.CreateCodeComment(
ctx.User,
ctx.Repo.GitRepo,
issue,
signedLine,
form.Content,
form.TreePath,
form.IsReview,
form.Reply,
form.LatestCommitID,
)

(form.Reply)

Perhaps this:

gitea/models/review.go

Lines 265 to 268 in 74bd969

// ReviewExists returns whether a review exists for a particular line of code in the PR
func ReviewExists(issue *Issue, treePath string, line int64) (bool, error) {
return x.Cols("id").Exist(&Comment{IssueID: issue.ID, TreePath: treePath, Line: line, Type: CommentTypeCode})
}

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...

@zeripath
Copy link
Contributor

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 <art27@cantab.net>
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 <art27@cantab.net>
---
 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

@zeripath
Copy link
Contributor

zeripath commented Aug 20, 2020

Hmm, If we turn this:

if !isReview {

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 <art27@cantab.net>
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 <art27@cantab.net>
---
 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

zeripath added a commit to zeripath/gitea that referenced this issue Aug 20, 2020
Only check for a review if we are replying to a previous review.

Prevent the NPE in go-gitea#12239 by assuming that a comment without a Review is
non-pending.

Fix go-gitea#12239

Signed-off-by: Andrew Thornton <art27@cantab.net>
@lafriks lafriks added this to the 1.12.4 milestone Aug 20, 2020
zeripath added a commit to zeripath/gitea that referenced this issue Aug 20, 2020
Only check for a review if we are replying to a previous review.

Prevent the NPE in go-gitea#12239 by assuming that a comment without a Review is
non-pending.

Fix go-gitea#12239

Signed-off-by: Andrew Thornton <art27@cantab.net>
lafriks pushed a commit that referenced this issue Aug 21, 2020
…#12550)

* 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 <art27@cantab.net>

* Add hack around to show the broken comments

Signed-off-by: Andrew Thornton <art27@cantab.net>
lafriks pushed a commit that referenced this issue Aug 21, 2020
…gration) (#12549)

* 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 <art27@cantab.net>

* Add hack around to show the broken comments

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Add migration and remove template hacks

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath
Copy link
Contributor

Apologies that this took so long to fix.

@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/critical This issue should be fixed ASAP. If it is a PR, the PR should be merged ASAP type/bug
Projects
None yet