Skip to content

Commit

Permalink
refactor: optimize grouping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jan 7, 2025
1 parent bdbc629 commit 340f76b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions extractor/filesystem/language/python/poetrylock/poetrylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,26 @@ func (e Extractor) FileRequired(api filesystem.FileAPI) bool {
return filepath.Base(api.Path()) == "poetry.lock"
}

func resolvePoetryPackageGroups(groups []string) []string {
for _, group := range groups {
func resolveGroups(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"}
}

if pkg.Groups == nil {
return []string{}
}

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
}

// Extract extracts packages from poetry.lock files passed through the scan input.
Expand All @@ -95,27 +105,15 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]
Name: lockPackage.Name,
Version: lockPackage.Version,
Locations: []string{input.Path},
Metadata: osv.DepGroupMetadata{
DepGroupVals: resolveGroups(lockPackage),
},
}
if lockPackage.Source.Commit != "" {
pkgDetails.SourceCode = &extractor.SourceCodeIdentifier{
Commit: lockPackage.Source.Commit,
}
}

groups := resolvePoetryPackageGroups(lockPackage.Groups)

if groups == nil {
groups = []string{}
}

if lockPackage.Optional {
groups = append(groups, "optional")
}

pkgDetails.Metadata = osv.DepGroupMetadata{
DepGroupVals: groups,
}

packages = append(packages, pkgDetails)
}

Expand Down

0 comments on commit 340f76b

Please sign in to comment.