mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-14 06:50:25 +00:00
When performing `git blame` to identify the commit that a line of code came from, limit the blame to the commit that is currently being viewed in the UI. Before this change, the blame always occurred on the current head of the PR, causing these problems: - When you click ➕ to load the comment form, the form that is dynamically loaded would have it's commit field pulled from the current PR head. That may not actually reflect the code that you were viewing at the time you authored the comment -- it could be a newer commit that occurred by the author while you were reviewing. - When viewing a specific commit within a PR and leaving a comment, the blame would occur from the head -- if the file was changed in a later commit and the line-of-code moved up or down, the comment would be misplaced. ## 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... - [ ] `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/12055): <!--number 12055 --><!--line 0 --><!--description d2hlbiByZXZpZXdpbmcgaW4gUFJzLCBtYWtlIGNvbW1lbnRzIHJlbGF0aXZlIHRvIHRoZSB2aXNpYmxlIGNvZGUncyBjb21taXQ=-->when reviewing in PRs, make comments relative to the visible code's commit<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12055 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
211 lines
8.8 KiB
Go
211 lines
8.8 KiB
Go
// Copyright 2024-2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
|
|
"forgejo.org/modules/translation"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestCommentRoles is a test for role labels of normal users in comment headers in PRs and issues.
|
|
func TestCommentRoles(t *testing.T) {
|
|
user := "user2"
|
|
repo := "repo1"
|
|
|
|
locale := translation.NewLocale("en-US")
|
|
authorTooltipPR := locale.TrString("repo.issues.author.tooltip.pr")
|
|
authorTooltipIssue := locale.TrString("repo.issues.author.tooltip.issue")
|
|
ownerTooltip := locale.TrString("repo.issues.role.owner_helper")
|
|
contributorTooltip := locale.TrString("repo.issues.role.contributor_helper")
|
|
newContributorTooltip := locale.TrString("repo.issues.role.first_time_contributor_helper")
|
|
|
|
// Test pulls
|
|
onApplicationRun(t, func(t *testing.T, giteaURL *url.URL) {
|
|
sessionUser1 := loginUser(t, "user1")
|
|
sessionUser2 := loginUser(t, "user2")
|
|
sessionUser11 := loginUser(t, "user11")
|
|
|
|
// Open a new PR as user2
|
|
testEditFileToNewBranch(t, sessionUser2, user, repo, "master", "comment-labels", "README.md", "test of comment labels\naline") // Owner
|
|
sessionUser2.MakeRequest(t, NewRequestWithValues(t, "POST", path.Join(user, repo, "compare", "master...comment-labels"),
|
|
map[string]string{
|
|
"title": "Pull used for testing commit labels",
|
|
},
|
|
), http.StatusOK)
|
|
|
|
// Pull number, expected to be 6
|
|
testID := "6"
|
|
|
|
// Add a few comments
|
|
// (first: Owner)
|
|
testEasyLeavePRReviewComment(t, sessionUser2, user, repo, testID, "README.md", "1", "New review comment from user2 on this line", "")
|
|
|
|
// Have to fetch reply ID for reviews
|
|
response := sessionUser2.MakeRequest(t, NewRequest(t, "GET", path.Join(user, repo, "pulls", testID)), http.StatusOK)
|
|
page := NewHTMLParser(t, response.Body)
|
|
replyID, _ := page.Find(".comment-form input[name='reply']").Attr("value")
|
|
|
|
testEasyLeavePRReviewComment(t, sessionUser1, user, repo, testID, "README.md", "1", "Reply comment from a contributor", replyID)
|
|
testEasyLeavePRComment(t, sessionUser2, user, repo, testID, "New comment from user2 on this PR") // Author, Owner
|
|
testEasyLeavePRComment(t, sessionUser1, user, repo, testID, "New comment from user1 on this PR") // Contributor
|
|
testEasyLeavePRComment(t, sessionUser11, user, repo, testID, "New comment from user11 on this PR") // First-time contributor
|
|
|
|
// Fetch the PR page
|
|
response = sessionUser2.MakeRequest(t, NewRequest(t, "GET", path.Join(user, repo, "pulls", testID)), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
reviewHeads := page.Find(".timeline .code-comment .header .comment-header-right")
|
|
assert.Equal(t, 2, reviewHeads.Length())
|
|
commentHeads := page.Find(".timeline .comment .comment-header .comment-header-right")
|
|
assert.Equal(t, 4, commentHeads.Length())
|
|
|
|
// === Review comments ===
|
|
|
|
// Test the first review comment labels
|
|
labels := reviewHeads.Eq(0).Find(".role-label")
|
|
assert.Equal(t, 2, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Author", authorTooltipPR)
|
|
testIssueCommentUserLabel(t, labels.Eq(1), "Owner", ownerTooltip)
|
|
|
|
// Test the second review comment labels
|
|
labels = reviewHeads.Eq(1).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Contributor", contributorTooltip)
|
|
|
|
//== Top comment ==
|
|
|
|
// Top comment (PR description) never shows `Author` label because it is implied
|
|
labels = commentHeads.Eq(0).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Owner", ownerTooltip)
|
|
|
|
// === Regular comments ===
|
|
|
|
// Test the first regular comment labels
|
|
labels = commentHeads.Eq(1).Find(".role-label")
|
|
assert.Equal(t, 2, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Author", authorTooltipPR)
|
|
testIssueCommentUserLabel(t, labels.Eq(1), "Owner", ownerTooltip)
|
|
|
|
// Test the second regular comment labels
|
|
labels = commentHeads.Eq(2).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Contributor", contributorTooltip)
|
|
|
|
// Test the third regular comment labels
|
|
labels = commentHeads.Eq(3).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "First-time contributor", newContributorTooltip)
|
|
})
|
|
|
|
// Test issues
|
|
onApplicationRun(t, func(t *testing.T, giteaURL *url.URL) {
|
|
sessionUser1 := loginUser(t, "user1")
|
|
sessionUser2 := loginUser(t, "user2")
|
|
sessionUser5 := loginUser(t, "user5")
|
|
|
|
// Open a new issue in the same repo
|
|
sessionUser2.MakeRequest(t, NewRequestWithValues(t, "POST", path.Join(user, repo, "issues/new"),
|
|
map[string]string{
|
|
"title": "Issue used for testing commit labels",
|
|
},
|
|
), http.StatusOK)
|
|
|
|
// Issue number, expected to be 6
|
|
testID := "6"
|
|
// Add a few comments
|
|
// (first: Owner)
|
|
testEasyLeaveIssueComment(t, sessionUser2, user, repo, testID, "New comment from user2 on this issue") // Author, Owner
|
|
testEasyLeaveIssueComment(t, sessionUser1, user, repo, testID, "New comment from user1 on this issue") // Contributor
|
|
testEasyLeaveIssueComment(t, sessionUser5, user, repo, testID, "New comment from user5 on this issue") // no labels
|
|
|
|
// Fetch the issue page
|
|
response := sessionUser2.MakeRequest(t, NewRequest(t, "GET", path.Join(user, repo, "issues", testID)), http.StatusOK)
|
|
page := NewHTMLParser(t, response.Body)
|
|
commentHeads := page.Find(".timeline .comment .comment-header .comment-header-right")
|
|
assert.Equal(t, 4, commentHeads.Length())
|
|
|
|
// Test the first comment and it's label "Owner"
|
|
labels := commentHeads.Eq(0).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Owner", ownerTooltip)
|
|
|
|
// Test the second comment and it's labels "Author" and "Owner"
|
|
labels = commentHeads.Eq(1).Find(".role-label")
|
|
assert.Equal(t, 2, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Author", authorTooltipIssue)
|
|
testIssueCommentUserLabel(t, labels.Eq(1), "Owner", ownerTooltip)
|
|
|
|
// Test the third comment and it's label "Contributor"
|
|
labels = commentHeads.Eq(2).Find(".role-label")
|
|
assert.Equal(t, 1, labels.Length())
|
|
testIssueCommentUserLabel(t, labels.Eq(0), "Contributor", contributorTooltip)
|
|
|
|
// Test the fifth comment and it's lack of labels
|
|
labels = commentHeads.Eq(3).Find(".role-label")
|
|
assert.Equal(t, 0, labels.Length())
|
|
})
|
|
}
|
|
|
|
// testIssueCommentUserLabel is used to verify properties of a user label from a comment
|
|
func testIssueCommentUserLabel(t *testing.T, label *goquery.Selection, expectedTitle, expectedTooltip string) {
|
|
t.Helper()
|
|
title := label.Text()
|
|
tooltip, exists := label.Attr("data-tooltip-content")
|
|
assert.True(t, exists)
|
|
assert.Equal(t, expectedTitle, strings.TrimSpace(title))
|
|
assert.Equal(t, expectedTooltip, strings.TrimSpace(tooltip))
|
|
}
|
|
|
|
// testEasyLeaveIssueComment is used to create a comment on an issue with minimum code and parameters
|
|
func testEasyLeaveIssueComment(t *testing.T, session *TestSession, user, repo, id, message string) {
|
|
t.Helper()
|
|
session.MakeRequest(t, NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", id, "comments"), map[string]string{
|
|
"content": message,
|
|
"status": "",
|
|
}), 200)
|
|
}
|
|
|
|
// testEasyLeaveIssueComment is used to create a comment on a pull request with minimum code and parameters
|
|
// The POST request is supposed to use "issues" in the path. The CSRF is supposed to be generated for the PR page.
|
|
func testEasyLeavePRComment(t *testing.T, session *TestSession, user, repo, id, message string) {
|
|
t.Helper()
|
|
session.MakeRequest(t, NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", id, "comments"), map[string]string{
|
|
"content": message,
|
|
"status": "",
|
|
}), 200)
|
|
}
|
|
|
|
// testEasyLeavePRReviewComment is used to add review comments to specific lines of changed files in the diff of the PR.
|
|
func testEasyLeavePRReviewComment(t *testing.T, session *TestSession, user, repo, id, file, line, message, replyID string) {
|
|
t.Helper()
|
|
req := NewRequestf(t, "GET", "/%s/%s/pulls/%s/files/reviews/new_comment", user, repo, id)
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
values := map[string]string{
|
|
"origin": doc.GetInputValueByName("origin"),
|
|
"latest_commit_id": doc.GetInputValueByName("latest_commit_id"),
|
|
"side": "proposed",
|
|
"line": line,
|
|
"path": file,
|
|
"diff_start_cid": doc.GetInputValueByName("diff_start_cid"),
|
|
"diff_end_cid": doc.GetInputValueByName("diff_end_cid"),
|
|
"diff_base_cid": doc.GetInputValueByName("diff_base_cid"),
|
|
"content": message,
|
|
"single_review": "true",
|
|
}
|
|
if len(replyID) > 0 {
|
|
values["reply"] = replyID
|
|
}
|
|
session.MakeRequest(t, NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/pulls/%s/files/reviews/comments", user, repo, id), values), http.StatusOK)
|
|
}
|