From dfd45c9b28405a1c62a10acccd3fda3ef0673aeb Mon Sep 17 00:00:00 2001 From: jmjoy Date: Tue, 28 Feb 2023 18:55:24 +0800 Subject: [PATCH 1/2] Normalize license for cargo. --- pkg/deps/cargo.go | 21 +++++++++++++++++++++ pkg/deps/cargo_test.go | 19 +++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pkg/deps/cargo.go b/pkg/deps/cargo.go index e0c1a59..3570742 100644 --- a/pkg/deps/cargo.go +++ b/pkg/deps/cargo.go @@ -23,6 +23,8 @@ import ( "os/exec" "path/filepath" "regexp" + "sort" + "strings" "github.com/apache/skywalking-eyes/internal/logger" "github.com/apache/skywalking-eyes/pkg/license" @@ -75,6 +77,10 @@ func (resolver *CargoTomlResolver) Resolve(cargoTomlFile string, config *ConfigD return err } + for i := range metadata.Packages { + metadata.Packages[i].License = normalizeLicense(metadata.Packages[i].License) + } + logger.Log.Debugln("Package size:", len(metadata.Packages)) return resolver.ResolvePackages(metadata.Packages, config, report) @@ -156,3 +162,18 @@ func (resolver *CargoTomlResolver) ResolvePackageLicense(config *ConfigDeps, pkg return nil } + +func normalizeLicense(license string) string { + segs := make(map[string]struct{}) + for _, ss := range strings.Split(license, "/") { + for _, s := range strings.Split(ss, " OR ") { + segs[s] = struct{}{} + } + } + var items []string + for seg := range segs { + items = append(items, seg) + } + sort.Strings(items) + return strings.Join(items, " OR ") +} diff --git a/pkg/deps/cargo_test.go b/pkg/deps/cargo_test.go index db401a3..469d32d 100644 --- a/pkg/deps/cargo_test.go +++ b/pkg/deps/cargo_test.go @@ -18,12 +18,13 @@ package deps_test import ( - "github.com/apache/skywalking-eyes/internal/logger" - "github.com/apache/skywalking-eyes/pkg/deps" "os" "os/exec" "path/filepath" "testing" + + "github.com/apache/skywalking-eyes/internal/logger" + "github.com/apache/skywalking-eyes/pkg/deps" ) func TestCanResolveCargo(t *testing.T) { @@ -134,7 +135,8 @@ edition = "2021" license = "Apache-2.0" [dependencies] -libc = "0.2.126" +libc = "0.2.126" # actual license: MIT OR Apache-2.0 +bitflags = "1.3.2" # actual license: MIT/Apache-2.0 ` config := deps.ConfigDeps{ @@ -145,12 +147,17 @@ libc = "0.2.126" } report := resolveTmpCargo(t, cargoToml, &config) - if len(report.Resolved) != 2 { - t.Error("len(report.Resolved) != 2") + if len(report.Resolved) != 3 { + t.Error("len(report.Resolved) != 3") } for _, result := range report.Resolved { if result.Dependency == "libc" { - if result.LicenseSpdxID != "MIT OR Apache-2.0" || result.LicenseContent == "" { + if result.LicenseSpdxID != "Apache-2.0 OR MIT" || result.LicenseContent == "" { + t.Error("Resolve dependency libc failed") + } + } + if result.Dependency == "bitflags" { + if result.LicenseSpdxID != "Apache-2.0 OR MIT" || result.LicenseContent == "" { t.Error("Resolve dependency libc failed") } } From 18392f4fcf887c309d20ca22607358231220d037 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Tue, 28 Feb 2023 19:16:58 +0800 Subject: [PATCH 2/2] Lint. --- pkg/deps/cargo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/deps/cargo.go b/pkg/deps/cargo.go index 3570742..5d900cd 100644 --- a/pkg/deps/cargo.go +++ b/pkg/deps/cargo.go @@ -163,9 +163,9 @@ func (resolver *CargoTomlResolver) ResolvePackageLicense(config *ConfigDeps, pkg return nil } -func normalizeLicense(license string) string { +func normalizeLicense(licenseStr string) string { segs := make(map[string]struct{}) - for _, ss := range strings.Split(license, "/") { + for _, ss := range strings.Split(licenseStr, "/") { for _, s := range strings.Split(ss, " OR ") { segs[s] = struct{}{} }