From bfb37de9eac70cb8b30c04708a554c1ad06e33ad Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Fri, 21 Jan 2022 16:36:20 +0000 Subject: [PATCH] handle single line new file patches --- commenter/commenter.go | 18 ++++++++++++------ commenter/commitFileInfo.go | 4 ++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/commenter/commenter.go b/commenter/commenter.go index de57357..ac59c58 100644 --- a/commenter/commenter.go +++ b/commenter/commenter.go @@ -17,7 +17,7 @@ type Commenter struct { } var ( - patchRegex = regexp.MustCompile(`^@@.*\+(\d+),(\d+).+?@@`) + patchRegex = regexp.MustCompile(`^@@.*[\+\-](\d+),(\d+).+?@@`) commitRefRegex = regexp.MustCompile(".+ref=(.+)") ) @@ -156,7 +156,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.isBinary() { + if file.FileName == filename && !file.isResolvable() { if line >= file.hunkStart && line <= file.hunkEnd { return true } @@ -172,7 +172,7 @@ func (c *Commenter) checkCommentRelevant(filename string, line int) bool { func (c *Commenter) getFileInfo(file string, line int) (*commitFileInfo, error) { for _, info := range c.files { - if info.FileName == file && !info.isBinary() { + if info.FileName == file && !info.isResolvable() { if line >= info.hunkStart && line <= info.hunkEnd { return info, nil } @@ -197,12 +197,18 @@ func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) { var isBinary bool patch := file.GetPatch() if patch != "" { - groups := patchRegex.FindAllStringSubmatch(file.GetPatch(), -1) + groups := patchRegex.FindAllStringSubmatch(patch, -1) if len(groups) < 1 { return nil, fmt.Errorf("the patch details for [%s] could not be resolved", *file.Filename) } - hunkStart, _ = strconv.Atoi(groups[0][1]) - hunkEnd, _ = strconv.Atoi(groups[0][2]) + hunkStart, err = strconv.Atoi(groups[0][1]) + if err != nil { + hunkStart = -1 + } + hunkEnd, err = strconv.Atoi(groups[0][2]) + if err != nil { + hunkEnd = -1 + } } shaGroups := commitRefRegex.FindAllStringSubmatch(file.GetContentsURL(), -1) diff --git a/commenter/commitFileInfo.go b/commenter/commitFileInfo.go index 39925e7..0ee08af 100644 --- a/commenter/commitFileInfo.go +++ b/commenter/commitFileInfo.go @@ -47,3 +47,7 @@ func (cfi commitFileInfo) calculatePosition(line int) *int { func (cfi commitFileInfo) isBinary() bool { return cfi.likelyBinary } + +func (cfi commitFileInfo) isResolvable() bool { + return cfi.isBinary() && cfi.hunkStart != -1 && cfi.hunkEnd != -1 +}