diff --git a/pkg/lockfile/parse-poetry-lock.go b/pkg/lockfile/parse-poetry-lock.go index 85874f4a53..234120d6e7 100644 --- a/pkg/lockfile/parse-poetry-lock.go +++ b/pkg/lockfile/parse-poetry-lock.go @@ -33,8 +33,14 @@ func (e PoetryLockExtractor) ShouldExtract(path string) bool { return filepath.Base(path) == "poetry.lock" } -func resolvePoetryPackageGroups(groups []string) []string { - for _, group := range groups { +func resolvePoetryPackageGroups(pkg PoetryLockPackage) []string { + // by definition an optional package cannot be in any other group, + // otherwise that would make it a required package + if pkg.Optional { + return []string{"optional"} + } + + for _, group := range pkg.Groups { // the "main" group is the default group used for "production" dependencies, // which we represent by an empty slice aka no groups if group == "main" { @@ -42,7 +48,7 @@ func resolvePoetryPackageGroups(groups []string) []string { } } - return groups + return pkg.Groups } func (e PoetryLockExtractor) Extract(f DepFile) ([]PackageDetails, error) { @@ -57,18 +63,14 @@ func (e PoetryLockExtractor) Extract(f DepFile) ([]PackageDetails, error) { packages := make([]PackageDetails, 0, len(parsedLockfile.Packages)) for _, lockPackage := range parsedLockfile.Packages { - pkgDetails := PackageDetails{ + packages = append(packages, PackageDetails{ Name: lockPackage.Name, Version: lockPackage.Version, Commit: lockPackage.Source.Commit, - DepGroups: resolvePoetryPackageGroups(lockPackage.Groups), + DepGroups: resolvePoetryPackageGroups(lockPackage), Ecosystem: PoetryEcosystem, CompareAs: PoetryEcosystem, - } - if lockPackage.Optional { - pkgDetails.DepGroups = append(pkgDetails.DepGroups, "optional") - } - packages = append(packages, pkgDetails) + }) } return packages, nil