From 274ee49538d9c31dcac422b27ba0b499d65a471c Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Mon, 24 Jan 2022 19:29:39 +0000 Subject: [PATCH] update the regex in base commenter --- go.mod | 2 +- go.sum | 2 + .../commenter/commenter.go | 45 +++++++++++-------- vendor/modules.txt | 2 +- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 931c139..c402a98 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/aquasecurity/tfsec-github-commenter-action go 1.15 -require github.com/owenrumney/go-github-pr-commenter v0.1.1 +require github.com/owenrumney/go-github-pr-commenter v0.1.2 diff --git a/go.sum b/go.sum index 8d0e153..1945bc4 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/owenrumney/go-github-pr-commenter v0.1.0 h1:P1AUqD1O4jSFSJbRldhNl88eV github.com/owenrumney/go-github-pr-commenter v0.1.0/go.mod h1:FVpyF/OGnWrtYwe9DkRpJsIlxSPIOJstZAyGyDfC/rA= github.com/owenrumney/go-github-pr-commenter v0.1.1 h1:YRp5/jak37SyjF9UVPiHkeX4+WaRoS+pE+QkW4T5SSU= github.com/owenrumney/go-github-pr-commenter v0.1.1/go.mod h1:FVpyF/OGnWrtYwe9DkRpJsIlxSPIOJstZAyGyDfC/rA= +github.com/owenrumney/go-github-pr-commenter v0.1.2 h1:FAFRXhd62oHPyrT6zU31jkp9jIiOR4c3Qa+GwuTxtvU= +github.com/owenrumney/go-github-pr-commenter v0.1.2/go.mod h1:FVpyF/OGnWrtYwe9DkRpJsIlxSPIOJstZAyGyDfC/rA= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/vendor/github.com/owenrumney/go-github-pr-commenter/commenter/commenter.go b/vendor/github.com/owenrumney/go-github-pr-commenter/commenter/commenter.go index 5dc7ab7..109ff13 100644 --- a/vendor/github.com/owenrumney/go-github-pr-commenter/commenter/commenter.go +++ b/vendor/github.com/owenrumney/go-github-pr-commenter/commenter/commenter.go @@ -17,7 +17,7 @@ type Commenter struct { } var ( - patchRegex = regexp.MustCompile(`^@@.*[\+\-](\d+)(?>\s[\+\-](\d)|,(\d+)).+?@@`) + patchRegex = regexp.MustCompile(`^@@.*\d [\+\-](\d+),?(\d+)?.+?@@`) commitRefRegex = regexp.MustCompile(".+ref=(.+)") ) @@ -193,19 +193,39 @@ func buildComment(file, comment string, line int, info commitFileInfo) *github.P } func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) { - var hunkStart, hunkEnd int var isBinary bool patch := file.GetPatch() + hunkStart, hunkEnd, err := parseHunkPositions(patch, *file.Filename) + if err != nil { + return nil, err + } + + shaGroups := commitRefRegex.FindAllStringSubmatch(file.GetContentsURL(), -1) + if len(shaGroups) < 1 { + return nil, fmt.Errorf("the sha details for [%s] could not be resolved", *file.Filename) + } + sha := shaGroups[0][1] + + return &commitFileInfo{ + FileName: *file.Filename, + hunkStart: hunkStart, + hunkEnd: hunkStart + (hunkEnd - 1), + sha: sha, + likelyBinary: isBinary, + }, nil +} + +func parseHunkPositions(patch, filename string) (hunkStart int, hunkEnd int, err error) { if patch != "" { groups := patchRegex.FindAllStringSubmatch(patch, -1) if len(groups) < 1 { - return nil, fmt.Errorf("the patch details for [%s] could not be resolved", *file.Filename) + return 0, 0, fmt.Errorf("the patch details for [%s] could not be resolved", filename) } patchGroup := groups[0] endPos := 2 - if len(patchGroup) > 2 { - endPos = 3 + if len(patchGroup) > 2 && patchGroup[2] == "" { + endPos = 1 } hunkStart, err = strconv.Atoi(patchGroup[1]) @@ -217,18 +237,5 @@ func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) { hunkEnd = -1 } } - - shaGroups := commitRefRegex.FindAllStringSubmatch(file.GetContentsURL(), -1) - if len(shaGroups) < 1 { - return nil, fmt.Errorf("the sha details for [%s] could not be resolved", *file.Filename) - } - sha := shaGroups[0][1] - - return &commitFileInfo{ - FileName: *file.Filename, - hunkStart: hunkStart, - hunkEnd: hunkStart + (hunkEnd - 1), - sha: sha, - likelyBinary: isBinary, - }, nil + return hunkStart, hunkEnd, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 7be0f81..feb97aa 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -4,7 +4,7 @@ github.com/golang/protobuf/proto github.com/google/go-github/v32/github # github.com/google/go-querystring v1.0.0 github.com/google/go-querystring/query -# github.com/owenrumney/go-github-pr-commenter v0.1.1 +# github.com/owenrumney/go-github-pr-commenter v0.1.2 ## explicit github.com/owenrumney/go-github-pr-commenter/commenter # golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9