mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-04 14:36:16 +02:00
- Remove commit_comment junction table, query Comment directly via repo_id + commit_sha + type filter - Add RepoID column to Comment (migration v326) - Add reqRepoCodeWriter permission checks to all commit comment routes - Fix delete auth: use CanWrite(TypeCode) instead of IsAdmin() - Fix CanComment: gate on write access, not read - Guard LoadIssue against IssueID=0 (commit comments have no issue) - Extract duplicated markdown rendering loop in commit.go - Fix template dict keys: use "root" instead of "." - Show delete button for users with code write access - Add unit tests for create, delete, get, find operations
168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package issues_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCreateCommitComment(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
comment := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: "abc123def456",
|
|
TreePath: "README.md",
|
|
Line: 10,
|
|
Content: "looks good",
|
|
}
|
|
|
|
err := issues_model.CreateCommitComment(t.Context(), comment)
|
|
require.NoError(t, err)
|
|
assert.Greater(t, comment.ID, int64(0))
|
|
assert.Equal(t, issues_model.CommentTypeCommitComment, comment.Type)
|
|
|
|
// Verify it's in the DB
|
|
loaded, err := issues_model.GetCommitCommentByID(t.Context(), 1, comment.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "README.md", loaded.TreePath)
|
|
assert.Equal(t, int64(10), loaded.Line)
|
|
assert.Equal(t, "looks good", loaded.Content)
|
|
assert.Equal(t, int64(1), loaded.RepoID)
|
|
}
|
|
|
|
func TestGetCommitCommentByID_WrongRepo(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
comment := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: "abc123def456",
|
|
TreePath: "main.go",
|
|
Line: 5,
|
|
Content: "test",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), comment))
|
|
|
|
// Trying to load with wrong repo ID should fail
|
|
_, err := issues_model.GetCommitCommentByID(t.Context(), 999, comment.ID)
|
|
assert.True(t, db.IsErrNotExist(err))
|
|
}
|
|
|
|
func TestDeleteCommitComment(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
comment := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: "abc123def456",
|
|
TreePath: "README.md",
|
|
Line: 10,
|
|
Content: "to be deleted",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), comment))
|
|
|
|
err := issues_model.DeleteCommitComment(t.Context(), 1, comment.ID)
|
|
require.NoError(t, err)
|
|
|
|
// Verify it's gone
|
|
_, err = issues_model.GetCommitCommentByID(t.Context(), 1, comment.ID)
|
|
assert.True(t, db.IsErrNotExist(err))
|
|
}
|
|
|
|
func TestDeleteCommitComment_WrongRepo(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
comment := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: "abc123def456",
|
|
TreePath: "README.md",
|
|
Line: 10,
|
|
Content: "should not be deleted",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), comment))
|
|
|
|
// Delete with wrong repo should not actually delete
|
|
err := issues_model.DeleteCommitComment(t.Context(), 999, comment.ID)
|
|
require.NoError(t, err)
|
|
|
|
// Verify it's still there
|
|
loaded, err := issues_model.GetCommitCommentByID(t.Context(), 1, comment.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, comment.ID, loaded.ID)
|
|
}
|
|
|
|
func TestFindCommitCommentsByCommitSHA(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
sha := "deadbeef1234"
|
|
// Create two comments on same commit, different lines
|
|
for _, line := range []int64{5, -10} {
|
|
c := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: sha,
|
|
TreePath: "main.go",
|
|
Line: line,
|
|
Content: "comment",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), c))
|
|
}
|
|
|
|
comments, err := issues_model.FindCommitCommentsByCommitSHA(t.Context(), 1, sha)
|
|
require.NoError(t, err)
|
|
assert.Len(t, comments, 2)
|
|
|
|
// Different repo should return empty
|
|
comments, err = issues_model.FindCommitCommentsByCommitSHA(t.Context(), 999, sha)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, comments)
|
|
}
|
|
|
|
func TestFindCommitCommentsForDiff(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
sha := "cafebabe5678"
|
|
// Left side comment (negative line = old/previous side)
|
|
c1 := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: sha,
|
|
TreePath: "file.go",
|
|
Line: -3,
|
|
Content: "old side",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), c1))
|
|
|
|
// Right side comment (positive line = new/proposed side)
|
|
c2 := &issues_model.Comment{
|
|
PosterID: 2,
|
|
RepoID: 1,
|
|
CommitSHA: sha,
|
|
TreePath: "file.go",
|
|
Line: 7,
|
|
Content: "new side",
|
|
}
|
|
require.NoError(t, issues_model.CreateCommitComment(t.Context(), c2))
|
|
|
|
result, err := issues_model.FindCommitCommentsForDiff(t.Context(), 1, sha)
|
|
require.NoError(t, err)
|
|
|
|
fcc, ok := result["file.go"]
|
|
require.True(t, ok)
|
|
assert.Len(t, fcc.Left[3], 1)
|
|
assert.Equal(t, "old side", fcc.Left[3][0].Content)
|
|
assert.Len(t, fcc.Right[7], 1)
|
|
assert.Equal(t, "new side", fcc.Right[7][0].Content)
|
|
}
|