Skip to content

Commit 1f8f1ee

Browse files
committed
Fix test
1 parent 191c89c commit 1f8f1ee

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

routers/private/hook_post_receive_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ func TestHandlePullRequestMerging(t *testing.T) {
3737
PullRequestID: pr.ID,
3838
UserID: 2,
3939
}, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, []*repo_module.PushUpdateOptions{
40-
{NewCommitID: "01234567"},
40+
// assume the first commit is merged from this pull request but it's not a real world scenario
41+
{NewCommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d"},
4142
})
4243
assert.Empty(t, resp.Body.String())
4344
pr, err = issues_model.GetPullRequestByID(db.DefaultContext, pr.ID)
4445
assert.NoError(t, err)
4546
assert.True(t, pr.HasMerged)
46-
assert.Equal(t, "01234567", pr.MergedCommitID)
47+
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", pr.MergedCommitID)
4748

4849
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{ID: autoMerge.ID})
4950
}

services/issue/issue_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ func TestIssue_DeleteIssue(t *testing.T) {
3939
assert.NoError(t, err)
4040
assert.Len(t, issueIDs, 5)
4141

42-
issue := &issues_model.Issue{
43-
RepoID: 1,
44-
ID: issueIDs[2],
45-
}
42+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueIDs[2]})
4643

4744
_, err = deleteIssue(db.DefaultContext, issue)
4845
assert.NoError(t, err)

services/pull/review.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,11 @@ func FetchCodeCommentsByLine(ctx context.Context, gitRepo *git.Repository, repo
593593

594594
// LoadCodeComments loads comments into each line, so that the comments can be displayed in the diff view.
595595
// the comments' line number is recalculated based on the hunks of the diff.
596-
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 {
597-
if startCommit == nil || endCommit == nil {
596+
func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_model.Repository,
597+
diff *gitdiff.Diff, issueID int64, currentUser *user_model.User,
598+
beforeCommit, afterCommit *git.Commit, showOutdatedComments bool,
599+
) error {
600+
if beforeCommit == nil || afterCommit == nil {
598601
return errors.New("startCommit and endCommit cannot be nil")
599602
}
600603

@@ -609,9 +612,20 @@ func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_m
609612
hunksCache := make(map[string][]*git.HunkInfo)
610613
// filecomments should be sorted by created time, so that the latest comments are at the end
611614
for _, comment := range fileComments {
612-
dstCommitID := startCommit.ID.String()
615+
if comment.CommitSHA == "" {
616+
if comment.Line > 0 {
617+
comment.CommitSHA = afterCommit.ID.String()
618+
} else if comment.Line < 0 {
619+
comment.CommitSHA = beforeCommit.ID.String()
620+
} else {
621+
// If the comment has no line number, we cannot display it in the diff view
622+
continue
623+
}
624+
}
625+
626+
dstCommitID := beforeCommit.ID.String()
613627
if comment.Line > 0 {
614-
dstCommitID = endCommit.ID.String()
628+
dstCommitID = afterCommit.ID.String()
615629
}
616630

617631
if comment.CommitSHA == dstCommitID {

services/pull/review_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func TestDiff_LoadCommentsNoOutdated(t *testing.T) {
8282
gitRepo, err := gitrepo.OpenRepository(t.Context(), issue.Repo)
8383
assert.NoError(t, err)
8484
defer gitRepo.Close()
85-
startCommit, err := gitRepo.GetCommit(issue.PullRequest.MergeBase)
85+
beforeCommit, err := gitRepo.GetCommit(issue.PullRequest.MergeBase)
8686
assert.NoError(t, err)
87-
endCommit, err := gitRepo.GetCommit(issue.PullRequest.GetGitHeadRefName())
87+
afterCommit, err := gitRepo.GetCommit(issue.PullRequest.GetGitHeadRefName())
8888
assert.NoError(t, err)
8989

90-
assert.NoError(t, pull_service.LoadCodeComments(db.DefaultContext, gitRepo, issue.Repo, diff, issue.ID, user, startCommit, endCommit, false))
90+
assert.NoError(t, pull_service.LoadCodeComments(db.DefaultContext, gitRepo, issue.Repo, diff, issue.ID, user, beforeCommit, afterCommit, false))
9191
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
9292
}
9393

0 commit comments

Comments
 (0)