From 8ccf553ff8875a428a61d275f9500ca2fa058fa6 Mon Sep 17 00:00:00 2001 From: Ahmet Reha SEKER Date: Tue, 30 May 2023 13:21:02 +0300 Subject: [PATCH] add support for multiple changes in file --- commenter/commenter.go | 47 ++++++++++++++++++++----------------- commenter/commitFileInfo.go | 30 +++++++++++++++++++---- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/commenter/commenter.go b/commenter/commenter.go index 109ff13..f733a20 100644 --- a/commenter/commenter.go +++ b/commenter/commenter.go @@ -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 } } @@ -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 } } @@ -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 } @@ -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 } diff --git a/commenter/commitFileInfo.go b/commenter/commitFileInfo.go index 0ee08af..1b41a19 100644 --- a/commenter/commitFileInfo.go +++ b/commenter/commitFileInfo.go @@ -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() @@ -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 } @@ -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 }