Skip to content

Commit 6a1e7d4

Browse files
committed
Fix migrated line; Added comment for review
1 parent e7ff3be commit 6a1e7d4

File tree

7 files changed

+62
-22
lines changed

7 files changed

+62
-22
lines changed

models/review.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ func InsertReviews(reviews []*Review) error {
417417
return err
418418
}
419419

420+
if _, err := sess.NoAutoTime().Insert(&Comment{
421+
Type: CommentTypeReview,
422+
Content: review.Content,
423+
PosterID: review.ReviewerID,
424+
OriginalAuthor: review.OriginalAuthor,
425+
OriginalAuthorID: review.OriginalAuthorID,
426+
IssueID: review.IssueID,
427+
ReviewID: review.ID,
428+
CreatedUnix: review.CreatedUnix,
429+
UpdatedUnix: review.UpdatedUnix,
430+
}); err != nil {
431+
return err
432+
}
433+
420434
for _, c := range review.Comments {
421435
c.ReviewID = review.ID
422436
}

modules/migrations/base/review.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ReviewComment struct {
3434
InReplyTo int64
3535
Content string
3636
TreePath string
37+
DiffHunk string
3738
Position int
3839
CommitID string
3940
PosterID int64

modules/migrations/gitea.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,17 +789,19 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
789789
}
790790
patchBuf := new(bytes.Buffer)
791791
if err := gitdiff.GetRawDiffForFile(g.gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, comment.TreePath, patchBuf); err != nil {
792-
return fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath)
792+
return fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath, err)
793793
}
794-
line := int64(comment.Position)
795-
patch := gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
794+
795+
_, _, line, _ := gitdiff.ParseDiffHunkString(comment.DiffHunk)
796+
797+
patch := gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
796798

797799
var c = models.Comment{
798800
Type: models.CommentTypeCode,
799801
PosterID: comment.PosterID,
800802
IssueID: issueID,
801803
Content: comment.Content,
802-
Line: line,
804+
Line: int64(line + comment.Position - 1),
803805
TreePath: comment.TreePath,
804806
CommitSHA: comment.CommitID,
805807
Patch: patch,

modules/migrations/github.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,14 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
418418

419419
// GetComments returns comments according issueNumber
420420
func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, error) {
421-
var allComments = make([]*base.Comment, 0, 100)
421+
var (
422+
allComments = make([]*base.Comment, 0, 100)
423+
created = "created"
424+
asc = "asc"
425+
)
422426
opt := &github.IssueListCommentsOptions{
423-
Sort: "created",
424-
Direction: "asc",
427+
Sort: created,
428+
Direction: asc,
425429
ListOptions: github.ListOptions{
426430
PerPage: 100,
427431
},
@@ -635,6 +639,7 @@ func convertGithubReviewComments(cs []*github.PullRequestComment) []*base.Review
635639
InReplyTo: c.GetInReplyTo(),
636640
Content: c.GetBody(),
637641
TreePath: c.GetPath(),
642+
DiffHunk: c.GetDiffHunk(),
638643
Position: c.GetPosition(),
639644
CommitID: c.GetCommitID(),
640645
PosterID: c.GetUser().GetID(),

services/gitdiff/gitdiff.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,9 @@ func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection {
149149
return DiffLineExpandSingle
150150
}
151151

152-
func getDiffLineSectionInfo(curFile *DiffFile, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
153-
var (
154-
leftLine int
155-
leftHunk int
156-
rightLine int
157-
righHunk int
158-
)
159-
ss := strings.Split(line, "@@")
152+
// ParseDiffHunkString parse the diffhunk content and return
153+
func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHunk int) {
154+
ss := strings.Split(diffhunk, "@@")
160155
ranges := strings.Split(ss[1][1:], " ")
161156
leftRange := strings.Split(ranges[0], ",")
162157
leftLine, _ = com.StrTo(leftRange[0][1:]).Int()
@@ -170,12 +165,18 @@ func getDiffLineSectionInfo(curFile *DiffFile, line string, lastLeftIdx, lastRig
170165
righHunk, _ = com.StrTo(rightRange[1]).Int()
171166
}
172167
} else {
173-
log.Warn("Parse line number failed: %v", line)
168+
log.Warn("Parse line number failed: %v", diffhunk)
174169
rightLine = leftLine
175170
righHunk = leftHunk
176171
}
172+
return
173+
}
174+
175+
func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
176+
leftLine, leftHunk, rightLine, righHunk := ParseDiffHunkString(line)
177+
177178
return &DiffLineSectionInfo{
178-
Path: curFile.Name,
179+
Path: treePath,
179180
LastLeftIdx: lastLeftIdx,
180181
LastRightIdx: lastRightIdx,
181182
LeftIdx: leftLine,
@@ -651,7 +652,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
651652
case line[0] == '@':
652653
curSection = &DiffSection{}
653654
curFile.Sections = append(curFile.Sections, curSection)
654-
lineSectionInfo := getDiffLineSectionInfo(curFile, line, leftLine-1, rightLine-1)
655+
lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1)
655656
diffLine := &DiffLine{
656657
Type: DiffLineSection,
657658
Content: line,

services/gitdiff/gitdiff_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,11 @@ func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
209209
}
210210
}
211211
}
212+
213+
func TestParseDiffHunkString(t *testing.T) {
214+
leftLine, leftHunk, rightLine, rightHunk := ParseDiffHunkString("@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER")
215+
assert.EqualValues(t, 19, leftLine)
216+
assert.EqualValues(t, 3, leftHunk)
217+
assert.EqualValues(t, 19, rightLine)
218+
assert.EqualValues(t, 5, rightHunk)
219+
}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,19 @@
319319
{{else if eq .Type 22}}
320320
<div class="event" id="{{.HashTag}}">
321321
<span class="octicon octicon-{{.Review.Type.Icon}} issue-symbol"></span>
322-
<a class="ui avatar image" href="{{.Poster.HomeLink}}">
323-
<img src="{{.Poster.RelAvatarLink}}">
324-
</a>
325-
<span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.GetDisplayName}}</a>
322+
{{if .OriginalAuthor }}
323+
{{else}}
324+
<a class="avatar"{{if gt .Poster.ID 0}} href="{{.Poster.HomeLink}}"{{end}}>
325+
<img src="{{.Poster.RelAvatarLink}}">
326+
</a>
327+
{{end}}
328+
<span class="text grey">
329+
{{if .OriginalAuthor }}
330+
<span class="text black"><i class="fa {{MigrationIcon $.Repository.GetOriginalURLHostname}}" aria-hidden="true"></i> {{ .OriginalAuthor }}</span><span class="text grey"> {{if $.Repository.OriginalURL}}</span><span class="text migrate">({{$.i18n.Tr "repo.migrated_from" $.Repository.OriginalURL $.Repository.GetOriginalURLHostname | Safe }}){{end}}</span>
331+
{{else}}
332+
<a{{if gt .Poster.ID 0}} href="{{.Poster.HomeLink}}"{{end}}>{{.Poster.GetDisplayName}}</a>
333+
{{end}}
334+
326335
{{if eq .Review.Type 1}}
327336
{{$.i18n.Tr "repo.issues.review.approve" $createdStr | Safe}}
328337
{{else if eq .Review.Type 2}}

0 commit comments

Comments
 (0)