Skip to content

Commit

Permalink
add Parse function to pkgecosystem, use in ResolvePackage (#769)
Browse files Browse the repository at this point in the history
* add Parse function to pkgecosystem, and use in downloader tool

Signed-off-by: Max Fisher <maxfisher@google.com>

* fix purl parsing issue

Signed-off-by: Max Fisher <maxfisher@google.com>

---------

Signed-off-by: Max Fisher <maxfisher@google.com>
  • Loading branch information
maxfisher-g authored Jun 20, 2023
1 parent 23f83f9 commit 461099e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
8 changes: 4 additions & 4 deletions internal/worker/resolvepackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func ResolvePkg(manager *pkgmanager.PkgManager, name, version, localPath string)
// ResolvePurl creates a Pkg object from the given purl
// See https://github.com/package-url/purl-spec
func ResolvePurl(purl packageurl.PackageURL) (*pkgmanager.Pkg, error) {
var ecosystem pkgecosystem.Ecosystem
if err := ecosystem.UnmarshalText([]byte(purl.Type)); err != nil {
return nil, fmt.Errorf("unsupported package ecosystem '%s'", purl.Type)
ecosystem, err := pkgecosystem.ParsePurlType(purl.Type)
if err != nil {
return nil, err
}

manager := pkgmanager.Manager(ecosystem)
if manager == nil {
return nil, fmt.Errorf("unsupported package ecosystem '%s'", purl.Type)
return nil, pkgecosystem.Unsupported(purl.Type)
}

// Prepend package namespace to package name, if present
Expand Down
52 changes: 44 additions & 8 deletions pkg/api/pkgecosystem/ecosystem.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The pkgecosystem package defines the open source ecosystems supported by Package Analysis.
// Package pkgecosystem defines the open source ecosystems supported by Package Analysis.
package pkgecosystem

import (
Expand All @@ -25,6 +25,12 @@ const (
// correspond to a defined ecosystem constant is passed in as a parameter.
var ErrUnsupported = errors.New("ecosystem unsupported")

// Unsupported returns a new ErrUnsupported that adds the unsupported ecosystem name
// to the error message
func Unsupported(name string) error {
return fmt.Errorf("%w: %s", ErrUnsupported, name)
}

// SupportedEcosystems is a list of all the ecosystems supported.
var SupportedEcosystems = []Ecosystem{
CratesIO,
Expand All @@ -43,14 +49,14 @@ var SupportedEcosystemsStrings = EcosystemsAsStrings(SupportedEcosystems)
// It will only succeed when unmarshaling ecosytems in SupportedEcosystems or
// empty.
func (e *Ecosystem) UnmarshalText(text []byte) error {
search := string(text)
for _, s := range append(SupportedEcosystems, None) {
if string(s) == search {
*e = s
return nil
}
ecosystem, err := Parse(string(text))

if err != nil {
return err
}
return fmt.Errorf("%w: %s", ErrUnsupported, text)

*e = ecosystem
return nil
}

// MarshalText implements the encoding.TextMarshaler interface.
Expand All @@ -71,3 +77,33 @@ func EcosystemsAsStrings(es []Ecosystem) []string {
}
return s
}

// Parse returns an Ecosystem corresponding to the given string name, or
// the None ecosystem along with an error if there is no matching Ecosystem.
// If name == "", then the None ecosystem is returned with no error.
func Parse(name string) (Ecosystem, error) {
for _, s := range append(SupportedEcosystems, None) {
if string(s) == name {
return s, nil
}
}

return None, Unsupported(name)
}

// ParsePurlType converts from a Package URL type, defined at
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst
// to an Ecosystem object
func ParsePurlType(purlType string) (Ecosystem, error) {
switch purlType {
case "cargo":
return CratesIO, nil
case "composer":
return Packagist, nil
case "gem":
return RubyGems, nil
default:
// we use the same name for NPM and PyPI as the purl type string
return Parse(purlType)
}
}

0 comments on commit 461099e

Please sign in to comment.