mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
fix: mark code comments as Outdated based upon line-of-code existence in current PR commit (#12054)
Currently when a commit is pushed to a branch, code comments are marked as Outdated if a `git blame` on the current commit's code returns the same commit as the `git blame` did when the comment was originally created. This implementation doesn't make sense: - It doesn't handle the case correctly where the same line of code exists unaltered in the new commit, but it has been relocated (eg. new lines entered or removed above the location). - It falsely keeps the commit valid if the line of code that the comment was made upon has been removed, if, coincidentally, the line of code that now exists at the commit came from the same source commit. For example, if the line of code that the comment was on was deleted, but the next line of code came from the same commit, the comment will be kept as valid. This PR uses the logic introduced in #12015, using a `git blame --reverse` -- the commit & line that was identified as having the comment on it is reversed, and if it still exists in the new head, then the comment is considered valid. Otherwise it is marked as outdated. Automated tests are added primarily by revising the automated tests in #12015 -- a comment in an existing test case was marked as outdated, even though it shouldn't have been. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/12054): <!--number 12054 --><!--line 0 --><!--description bWFyayBjb2RlIGNvbW1lbnRzIGFzIE91dGRhdGVkIGJhc2VkIHVwb24gbGluZS1vZi1jb2RlIGV4aXN0ZW5jZSBpbiBjdXJyZW50IFBSIGNvbW1pdA==-->mark code comments as Outdated based upon line-of-code existence in current PR commit<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12054 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
parent
160cd930ff
commit
40aa3a5c7d
4 changed files with 49 additions and 14 deletions
|
|
@ -1242,6 +1242,7 @@ func TestPullRequestCommentPlacement(t *testing.T) {
|
|||
assert.Equal(t, "proposed", comment.DiffSide())
|
||||
assert.EqualValues(t, 50, comment.Line)
|
||||
assert.Equal(t, commit1, comment.CommitSHA)
|
||||
assert.False(t, comment.Invalidated)
|
||||
|
||||
// Add a second commit to the PR which removes "Line 1" - "Line 10".
|
||||
content = strings.Replace(content, "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\n", "", 1)
|
||||
|
|
@ -1271,6 +1272,11 @@ func TestPullRequestCommentPlacement(t *testing.T) {
|
|||
}
|
||||
tester.assertFilesChangedDiff(diff1, "checking commit1 contents in full PR diff")
|
||||
tester.assertCommitDiff(commit1, diff1, "checking commit1 contents in single-commit diff")
|
||||
|
||||
// This comment can still be located in the diff, so it should not be marked as Invalidated/Outdated --
|
||||
// which is kinda guaranteed by it being loaded in the diff, but for test sanity assert specifically.
|
||||
commentReloaded := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID})
|
||||
assert.False(t, commentReloaded.Invalidated)
|
||||
})
|
||||
|
||||
t.Run("comment on line commit is rewritten and force-push'd", func(t *testing.T) {
|
||||
|
|
@ -1296,6 +1302,7 @@ func TestPullRequestCommentPlacement(t *testing.T) {
|
|||
assert.Equal(t, "proposed", comment.DiffSide())
|
||||
assert.EqualValues(t, 50, comment.Line)
|
||||
assert.Equal(t, commit1, comment.CommitSHA)
|
||||
assert.False(t, comment.Invalidated)
|
||||
|
||||
// Add a second commit to the PR which removes "Line 1" - "Line 10".
|
||||
content = strings.Replace(content, "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\n", "", 1)
|
||||
|
|
@ -1350,6 +1357,17 @@ func TestPullRequestCommentPlacement(t *testing.T) {
|
|||
{rowType: RowHasCode, code: "Line 51"},
|
||||
}
|
||||
tester.assertFilesChangedDiff(diff1, "checking commit1 contents in full PR diff")
|
||||
|
||||
// After the force push, the comment we originally left should be marked as invalidated since it can no
|
||||
// longer be resolved to a code location in the PR head. The above tests validate that it no longer appears
|
||||
// in the diff, but this will also happen because of the diff-side check for the correct location -- so
|
||||
// let's check that it's invalidated as well, indicating that it will be shown in the UI as "Outdated". This
|
||||
// usually passes on the first check but is wrapped in Eventually because the async goroutine used in the
|
||||
// pull request testing when the branch is pushed may not be immediately complete.
|
||||
assert.EventuallyWithT(t, func(t *assert.CollectT) {
|
||||
commentReloaded := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID})
|
||||
assert.True(t, commentReloaded.Invalidated)
|
||||
}, 1*time.Second, 50*time.Millisecond)
|
||||
})
|
||||
|
||||
t.Run("comment lands on blame with original line number varying from current", func(t *testing.T) {
|
||||
|
|
@ -1551,7 +1569,7 @@ func (tester *PullRequestCommentPlacementTester) withBranchCheckout(action func(
|
|||
|
||||
func (tester *PullRequestCommentPlacementTester) assertFilesChangedDiff(rowAssertions []diffTableRow, note ...string) {
|
||||
req := NewRequest(tester.t, "GET",
|
||||
fmt.Sprintf("/%s/%s/pulls/%d/files?show-outdated=true", tester.repo.OwnerName, tester.repo.Name, tester.pr.Index))
|
||||
fmt.Sprintf("/%s/%s/pulls/%d/files", tester.repo.OwnerName, tester.repo.Name, tester.pr.Index))
|
||||
resp := tester.session.MakeRequest(tester.t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(tester.t, resp.Body)
|
||||
var testNote string
|
||||
|
|
@ -1565,7 +1583,7 @@ func (tester *PullRequestCommentPlacementTester) assertFilesChangedDiff(rowAsser
|
|||
|
||||
func (tester *PullRequestCommentPlacementTester) assertCommitDiff(commitSHA string, rowAssertions []diffTableRow, note ...string) {
|
||||
req := NewRequest(tester.t, "GET",
|
||||
fmt.Sprintf("/%s/%s/pulls/%d/commits/%s?show-outdated=true", tester.repo.OwnerName, tester.repo.Name, tester.pr.Index, commitSHA))
|
||||
fmt.Sprintf("/%s/%s/pulls/%d/commits/%s", tester.repo.OwnerName, tester.repo.Name, tester.pr.Index, commitSHA))
|
||||
resp := tester.session.MakeRequest(tester.t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(tester.t, resp.Body)
|
||||
var testNote string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue