Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from ahmetrehaseker/add-multiple-change-support
Browse files Browse the repository at this point in the history
add support for multiple changes in file
  • Loading branch information
owenrumney authored Nov 8, 2023
2 parents 8aed495 + 8ccf553 commit f2341ea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
47 changes: 26 additions & 21 deletions commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *Commenter) checkCommentRelevant(filename string, line int) bool {
for _, file := range c.files {
if relevant := func(file *commitFileInfo) bool {
if file.FileName == filename && !file.isResolvable() {
if line >= file.hunkStart && line <= file.hunkEnd {
if file.isLineInChange(line) {
return true
}
}
Expand All @@ -173,7 +173,7 @@ func (c *Commenter) getFileInfo(file string, line int) (*commitFileInfo, error)

for _, info := range c.files {
if info.FileName == file && !info.isResolvable() {
if line >= info.hunkStart && line <= info.hunkEnd {
if info.isLineInChange(line) {
return info, nil
}
}
Expand All @@ -195,7 +195,7 @@ func buildComment(file, comment string, line int, info commitFileInfo) *github.P
func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) {
var isBinary bool
patch := file.GetPatch()
hunkStart, hunkEnd, err := parseHunkPositions(patch, *file.Filename)
hunkInfos, err := parseHunkPositions(patch, *file.Filename)
if err != nil {
return nil, err
}
Expand All @@ -208,34 +208,39 @@ func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) {

return &commitFileInfo{
FileName: *file.Filename,
hunkStart: hunkStart,
hunkEnd: hunkStart + (hunkEnd - 1),
hunkInfos: hunkInfos,
sha: sha,
likelyBinary: isBinary,
}, nil
}

func parseHunkPositions(patch, filename string) (hunkStart int, hunkEnd int, err error) {
func parseHunkPositions(patch, filename string) (hi []*hunkInfo, err error) {
hunkInfos := make([]*hunkInfo, 0)
if patch != "" {
groups := patchRegex.FindAllStringSubmatch(patch, -1)
if len(groups) < 1 {
return 0, 0, fmt.Errorf("the patch details for [%s] could not be resolved", filename)
}

patchGroup := groups[0]
endPos := 2
if len(patchGroup) > 2 && patchGroup[2] == "" {
endPos = 1
return hunkInfos, fmt.Errorf("the patch details for [%s] could not be resolved", filename)
}
for _, patchGroup := range groups {
patchGroup = groups[0]
endPos := 2
if len(patchGroup) > 2 && patchGroup[2] == "" {
endPos = 1
}

hunkStart, err = strconv.Atoi(patchGroup[1])
if err != nil {
hunkStart = -1
}
hunkEnd, err = strconv.Atoi(patchGroup[endPos])
if err != nil {
hunkEnd = -1
hunkStart, err := strconv.Atoi(patchGroup[1])
if err != nil {
hunkStart = -1
}
hunkEnd, err := strconv.Atoi(patchGroup[endPos])
if err != nil {
hunkEnd = -1
}
hunkInfos = append(hunkInfos, &hunkInfo{
hunkStart: hunkStart,
hunkEnd: hunkEnd,
})
}
}
return hunkStart, hunkEnd, nil
return hunkInfos, nil
}
30 changes: 26 additions & 4 deletions commenter/commitFileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import (

type commitFileInfo struct {
FileName string
hunkStart int
hunkEnd int
hunkInfos []*hunkInfo
sha string
likelyBinary bool
}

type hunkInfo struct {
hunkStart int
hunkEnd int
}

func (hi hunkInfo) isLineInHunk(line int) bool {
return line >= hi.hunkStart && line <= hi.hunkEnd
}

func getCommitFileInfo(ghConnector *connector) ([]*commitFileInfo, error) {

prFiles, err := ghConnector.getFilesForPr()
Expand All @@ -39,8 +47,22 @@ func getCommitFileInfo(ghConnector *connector) ([]*commitFileInfo, error) {
return commitFileInfos, nil
}

func (cfi *commitFileInfo) getHunkInfo(line int) *hunkInfo {
for _, hunkInfo := range cfi.hunkInfos {
if hunkInfo.isLineInHunk(line) {
return hunkInfo
}
}
return nil
}

func (cfi *commitFileInfo) isLineInChange(line int) bool {
return cfi.getHunkInfo(line) != nil
}

func (cfi commitFileInfo) calculatePosition(line int) *int {
position := line - cfi.hunkStart
hi := cfi.getHunkInfo(line)
position := line - hi.hunkStart
return &position
}

Expand All @@ -49,5 +71,5 @@ func (cfi commitFileInfo) isBinary() bool {
}

func (cfi commitFileInfo) isResolvable() bool {
return cfi.isBinary() && cfi.hunkStart != -1 && cfi.hunkEnd != -1
return cfi.isBinary() && len(cfi.hunkInfos) == 0
}

0 comments on commit f2341ea

Please sign in to comment.