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

Commit

Permalink
handle single line new file patches
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney committed Jan 21, 2022
1 parent 9f55caf commit bfb37de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 12 additions & 6 deletions commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Commenter struct {
}

var (
patchRegex = regexp.MustCompile(`^@@.*\+(\d+),(\d+).+?@@`)
patchRegex = regexp.MustCompile(`^@@.*[\+\-](\d+),(\d+).+?@@`)
commitRefRegex = regexp.MustCompile(".+ref=(.+)")
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions commenter/commitFileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit bfb37de

Please sign in to comment.