From a2742e0d099b6d9c5d1b827ee21ecc21b7a57653 Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Thu, 8 Feb 2024 12:38:32 +0600 Subject: [PATCH] feat(gradle): add dep location support --- pkg/gradle/lockfile/parse.go | 17 +++++++++++++++-- pkg/gradle/lockfile/parse_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/gradle/lockfile/parse.go b/pkg/gradle/lockfile/parse.go index 9d535960..c0d57745 100644 --- a/pkg/gradle/lockfile/parse.go +++ b/pkg/gradle/lockfile/parse.go @@ -2,6 +2,7 @@ package lockfile import ( "bufio" + "fmt" "strings" dio "github.com/aquasecurity/go-dep-parser/pkg/io" @@ -18,7 +19,9 @@ func NewParser() types.Parser { func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) { var libs []types.Library scanner := bufio.NewScanner(r) + var lineNum int for scanner.Scan() { + lineNum++ line := strings.TrimSpace(scanner.Text()) if strings.HasPrefix(line, "#") { // skip comments continue @@ -29,9 +32,19 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er if len(dep) != 3 { // skip the last line with lists of empty configurations continue } + + name := strings.Join(dep[:2], ":") + version := strings.Split(dep[2], "=")[0] // remove classPaths libs = append(libs, types.Library{ - Name: strings.Join(dep[:2], ":"), - Version: strings.Split(dep[2], "=")[0], // remove classPaths + ID: fmt.Sprintf("%s:%s", name, version), + Name: name, + Version: version, + Locations: []types.Location{ + { + StartLine: lineNum, + EndLine: lineNum, + }, + }, }) } diff --git a/pkg/gradle/lockfile/parse_test.go b/pkg/gradle/lockfile/parse_test.go index 8528c7a5..3160d954 100644 --- a/pkg/gradle/lockfile/parse_test.go +++ b/pkg/gradle/lockfile/parse_test.go @@ -21,16 +21,37 @@ func TestParser_Parse(t *testing.T) { inputFile: "testdata/happy.lockfile", want: []types.Library{ { + ID: "cglib:cglib-nodep:2.1.2", Name: "cglib:cglib-nodep", Version: "2.1.2", + Locations: []types.Location{ + { + StartLine: 4, + EndLine: 4, + }, + }, }, { + ID: "org.springframework:spring-asm:3.1.3.RELEASE", Name: "org.springframework:spring-asm", Version: "3.1.3.RELEASE", + Locations: []types.Location{ + { + StartLine: 5, + EndLine: 5, + }, + }, }, { + ID: "org.springframework:spring-beans:5.0.5.RELEASE", Name: "org.springframework:spring-beans", Version: "5.0.5.RELEASE", + Locations: []types.Location{ + { + StartLine: 6, + EndLine: 6, + }, + }, }, }, },