Skip to content

Commit 358f100

Browse files
authored
Merge pull request #46 from Octogonapus/fix_tests
Fix parsing of some purl types. Fix tests.
2 parents 8907843 + 2e2d03f commit 358f100

File tree

5 files changed

+284
-62
lines changed

5 files changed

+284
-62
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ jobs:
1414
go-version: ${{ matrix.go-version }}
1515
- name: Checkout code
1616
uses: actions/checkout@v2
17-
- name: Download test data
18-
run: curl -L https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json
1917
- name: Test go fmt
2018
run: test -z $(go fmt ./...)
2119
- name: Golangci-lint
@@ -30,4 +28,4 @@ jobs:
3028
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3129
run: |
3230
GO111MODULE=off go get github.com/mattn/goveralls
33-
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
31+
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
.PHONY: test clean lint
22

33
test:
4-
curl -L https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json
54
go test -v -cover ./...
65

7-
clean:
8-
find . -name "test-suite-data.json" | xargs rm -f
9-
106
lint:
117
go get -u golang.org/x/lint/golint
128
golint -set_exit_status

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ Testing using the normal ``go test`` command. Using ``make test`` will pull the
5959

6060
```
6161
$ make test
62-
curl -L https://raw.githubusercontent.com/package-url/purl-test-suite/master/test-suite-data.json -o testdata/test-suite-data.json
63-
% Total % Received % Xferd Average Speed Time Time Time Current
64-
Dload Upload Total Spent Left Speed
65-
100 7181 100 7181 0 0 1202 0 0:00:05 0:00:05 --:--:-- 1611
6662
go test -v -cover ./...
6763
=== RUN TestFromStringExamples
6864
--- PASS: TestFromStringExamples (0.00s)
6965
=== RUN TestToStringExamples
7066
--- PASS: TestToStringExamples (0.00s)
67+
=== RUN TestStringer
68+
--- PASS: TestStringer (0.00s)
69+
=== RUN TestQualifiersMapConversion
70+
--- PASS: TestQualifiersMapConversion (0.00s)
7171
PASS
72-
coverage: 94.7% of statements
73-
ok github.com/package-url/packageurl-go 0.002s
72+
coverage: 90.7% of statements
73+
ok github.com/package-url/packageurl-go 0.004s coverage: 90.7% of statements
7474
```

packageurl.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ var (
8989
TypeRPM = "rpm"
9090
// TypeSwift is pkg:swift purl
9191
TypeSwift = "swift"
92+
// TypeHuggingface is pkg:huggingface purl.
93+
TypeHuggingface = "huggingface"
94+
// TypeMLflow is pkg:mlflow purl.
95+
TypeMLFlow = "mlflow"
9296
)
9397

9498
// Qualifier represents a single key=value qualifier in the package url
@@ -283,7 +287,7 @@ func FromString(purl string) (PackageURL, error) {
283287
remainder = nextSplit[1]
284288

285289
index = strings.LastIndex(remainder, "/")
286-
name := typeAdjustName(purlType, remainder[index+1:])
290+
name := typeAdjustName(purlType, remainder[index+1:], qualifiers)
287291
version := ""
288292

289293
atIndex := strings.Index(name, "@")
@@ -292,7 +296,7 @@ func FromString(purl string) (PackageURL, error) {
292296
if err != nil {
293297
return PackageURL{}, fmt.Errorf("failed to unescape purl version: %s", err)
294298
}
295-
version = v
299+
version = typeAdjustVersion(purlType, v)
296300

297301
unecapeName, err := url.PathUnescape(name[:atIndex])
298302
if err != nil {
@@ -342,24 +346,56 @@ func FromString(purl string) (PackageURL, error) {
342346
// See https://github.com/package-url/purl-spec#known-purl-types
343347
func typeAdjustNamespace(purlType, ns string) string {
344348
switch purlType {
345-
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeRPM:
349+
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeRPM, TypeComposer:
346350
return strings.ToLower(ns)
347351
}
348352
return ns
349353
}
350354

351355
// Make any purl type-specific adjustments to the parsed name.
352356
// See https://github.com/package-url/purl-spec#known-purl-types
353-
func typeAdjustName(purlType, name string) string {
357+
func typeAdjustName(purlType, name string, qualifiers Qualifiers) string {
358+
quals := qualifiers.Map()
354359
switch purlType {
355-
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM:
360+
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeComposer:
356361
return strings.ToLower(name)
357362
case TypePyPi:
358363
return strings.ToLower(strings.ReplaceAll(name, "_", "-"))
364+
case TypeMLFlow:
365+
return adjustMlflowName(name, quals)
359366
}
360367
return name
361368
}
362369

370+
// Make any purl type-specific adjustments to the parsed version.
371+
// See https://github.com/package-url/purl-spec#known-purl-types
372+
func typeAdjustVersion(purlType, version string) string {
373+
switch purlType {
374+
case TypeHuggingface:
375+
return strings.ToLower(version)
376+
}
377+
return version
378+
}
379+
380+
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#mlflow
381+
func adjustMlflowName(name string, qualifiers map[string]string) string {
382+
if repo, ok := qualifiers["repository_url"]; ok {
383+
if strings.Contains(repo, "azureml") {
384+
// Azure ML is case-sensitive and must be kept as-is
385+
return name
386+
} else if strings.Contains(repo, "databricks") {
387+
// Databricks is case-insensitive and must be lowercased
388+
return strings.ToLower(name)
389+
} else {
390+
// Unknown repository type, keep as-is
391+
return name
392+
}
393+
} else {
394+
// No repository qualifier given, keep as-is
395+
return name
396+
}
397+
}
398+
363399
// validQualifierKey validates a qualifierKey against our QualifierKeyPattern.
364400
func validQualifierKey(key string) bool {
365401
return QualifierKeyPattern.MatchString(key)

0 commit comments

Comments
 (0)