diff --git a/routers/private/hook_post_receive_test.go b/routers/private/hook_post_receive_test.go index ca721b16d1b..3d70552a3d1 100644 --- a/routers/private/hook_post_receive_test.go +++ b/routers/private/hook_post_receive_test.go @@ -37,13 +37,14 @@ func TestHandlePullRequestMerging(t *testing.T) { PullRequestID: pr.ID, UserID: 2, }, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, []*repo_module.PushUpdateOptions{ - {NewCommitID: "01234567"}, + // assume the first commit is merged from this pull request but it's not a real world scenario + {NewCommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d"}, }) assert.Empty(t, resp.Body.String()) pr, err = issues_model.GetPullRequestByID(db.DefaultContext, pr.ID) assert.NoError(t, err) assert.True(t, pr.HasMerged) - assert.Equal(t, "01234567", pr.MergedCommitID) + assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", pr.MergedCommitID) unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{ID: autoMerge.ID}) } diff --git a/services/issue/issue_test.go b/services/issue/issue_test.go index bad0d65d1ed..0527192f916 100644 --- a/services/issue/issue_test.go +++ b/services/issue/issue_test.go @@ -39,10 +39,7 @@ func TestIssue_DeleteIssue(t *testing.T) { assert.NoError(t, err) assert.Len(t, issueIDs, 5) - issue := &issues_model.Issue{ - RepoID: 1, - ID: issueIDs[2], - } + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueIDs[2]}) _, err = deleteIssue(db.DefaultContext, issue) assert.NoError(t, err) diff --git a/services/pull/review.go b/services/pull/review.go index fc3fc5b9179..a66317c2581 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -593,8 +593,11 @@ func FetchCodeCommentsByLine(ctx context.Context, gitRepo *git.Repository, repo // LoadCodeComments loads comments into each line, so that the comments can be displayed in the diff view. // the comments' line number is recalculated based on the hunks of the diff. -func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_model.Repository, diff *gitdiff.Diff, issueID int64, currentUser *user_model.User, startCommit, endCommit *git.Commit, showOutdatedComments bool) error { - if startCommit == nil || endCommit == nil { +func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_model.Repository, + diff *gitdiff.Diff, issueID int64, currentUser *user_model.User, + beforeCommit, afterCommit *git.Commit, showOutdatedComments bool, +) error { + if beforeCommit == nil || afterCommit == nil { return errors.New("startCommit and endCommit cannot be nil") } @@ -609,9 +612,20 @@ func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_m hunksCache := make(map[string][]*git.HunkInfo) // filecomments should be sorted by created time, so that the latest comments are at the end for _, comment := range fileComments { - dstCommitID := startCommit.ID.String() + if comment.CommitSHA == "" { + if comment.Line > 0 { + comment.CommitSHA = afterCommit.ID.String() + } else if comment.Line < 0 { + comment.CommitSHA = beforeCommit.ID.String() + } else { + // If the comment has no line number, we cannot display it in the diff view + continue + } + } + + dstCommitID := beforeCommit.ID.String() if comment.Line > 0 { - dstCommitID = endCommit.ID.String() + dstCommitID = afterCommit.ID.String() } if comment.CommitSHA == dstCommitID { diff --git a/services/pull/review_test.go b/services/pull/review_test.go index 80cc78bcbe6..65abc0a277f 100644 --- a/services/pull/review_test.go +++ b/services/pull/review_test.go @@ -82,12 +82,12 @@ func TestDiff_LoadCommentsNoOutdated(t *testing.T) { gitRepo, err := gitrepo.OpenRepository(t.Context(), issue.Repo) assert.NoError(t, err) defer gitRepo.Close() - startCommit, err := gitRepo.GetCommit(issue.PullRequest.MergeBase) + beforeCommit, err := gitRepo.GetCommit(issue.PullRequest.MergeBase) assert.NoError(t, err) - endCommit, err := gitRepo.GetCommit(issue.PullRequest.GetGitHeadRefName()) + afterCommit, err := gitRepo.GetCommit(issue.PullRequest.GetGitHeadRefName()) assert.NoError(t, err) - assert.NoError(t, pull_service.LoadCodeComments(db.DefaultContext, gitRepo, issue.Repo, diff, issue.ID, user, startCommit, endCommit, false)) + assert.NoError(t, pull_service.LoadCodeComments(db.DefaultContext, gitRepo, issue.Repo, diff, issue.ID, user, beforeCommit, afterCommit, false)) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) }