Skip to content

Commit

Permalink
refactor: optimize Poetry grouping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jan 7, 2025
1 parent 683de94 commit fe574fc
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/lockfile/parse-poetry-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ 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" {
return []string{}
}
}

return groups
return pkg.Groups
}

func (e PoetryLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
Expand All @@ -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
Expand Down

0 comments on commit fe574fc

Please sign in to comment.