Skip to content

Commit 2e80cc4

Browse files
committed
Added location enrichment
And more tests
1 parent 7b7e959 commit 2e80cc4

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

lib/enrich.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,24 @@ func enrichLatestReleasePublishedAt(component cdx.Component, packageData package
9090
}
9191

9292
func enrichRepoArchived(component cdx.Component, packageData packages.Package) cdx.Component {
93-
if packageData.RepoMetadata != nil {
94-
if archived, ok := (*packageData.RepoMetadata)["archived"].(bool); ok && archived {
95-
return enrichProperty(component, "ecosystems:repository_archived", "true")
96-
}
97-
}
98-
return component
93+
if packageData.RepoMetadata != nil {
94+
if archived, ok := (*packageData.RepoMetadata)["archived"].(bool); ok && archived {
95+
return enrichProperty(component, "ecosystems:repository_archived", "true")
96+
}
97+
}
98+
return component
99+
}
100+
101+
func enrichLocation(component cdx.Component, packageData packages.Package) cdx.Component {
102+
if packageData.RepoMetadata != nil {
103+
meta := *packageData.RepoMetadata
104+
if ownerRecord, ok := meta["owner_record"].(map[string]interface{}); ok {
105+
if location, ok := ownerRecord["location"].(string); ok {
106+
return enrichProperty(component, "ecosystems:owner_location", location)
107+
}
108+
}
109+
}
110+
return component
99111
}
100112

101113
func enrichComponents(bom *cdx.BOM, enrichFuncs []func(cdx.Component, packages.Package) cdx.Component) {
@@ -135,6 +147,7 @@ func EnrichSBOM(bom *cdx.BOM) *cdx.BOM {
135147
enrichFirstReleasePublishedAt,
136148
enrichLatestReleasePublishedAt,
137149
enrichRepoArchived,
150+
enrichLocation,
138151
}
139152

140153
enrichComponents(bom, enrichFuncs)

lib/enrich_test.go

+73-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lib
33
import (
44
"net/http"
55
"testing"
6+
"time"
67

78
"github.com/snyk/parlay/ecosystems/packages"
89

@@ -41,20 +42,18 @@ func TestEnrichSBOM(t *testing.T) {
4142

4243
bom = EnrichSBOM(bom)
4344

44-
/*
45-
components = *bom.Components
46-
component := components[0]
47-
licenses := *component.Licenses
45+
components = *bom.Components
46+
component := components[0]
47+
licenses := *component.Licenses
4848

49-
comp := cdx.LicenseChoice(cdx.LicenseChoice{Expression: "BSD-3-Clause"})
49+
comp := cdx.LicenseChoice(cdx.LicenseChoice{Expression: "BSD-3-Clause"})
5050

51-
assert.Equal(t, "description", components[0].Description)
52-
assert.Equal(t, comp, licenses[0])
51+
assert.Equal(t, "description", components[0].Description)
52+
assert.Equal(t, comp, licenses[0])
5353

54-
httpmock.GetTotalCallCount()
55-
calls := httpmock.GetCallCountInfo()
56-
assert.Equal(t, len(components), calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`])
57-
*/
54+
httpmock.GetTotalCallCount()
55+
calls := httpmock.GetCallCountInfo()
56+
assert.Equal(t, len(components), calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`])
5857
}
5958

6059
func TestEnrichSBOMWithoutLicense(t *testing.T) {
@@ -202,3 +201,66 @@ func TestEnrichRegistryURLWithNonNullRegistryURL(t *testing.T) {
202201
func pointerToString(s string) *string {
203202
return &s
204203
}
204+
205+
func TestEnrichLatestReleasePublishedAt(t *testing.T) {
206+
component := cdx.Component{}
207+
packageData := packages.Package{
208+
LatestReleasePublishedAt: nil,
209+
}
210+
211+
result := enrichLatestReleasePublishedAt(component, packageData)
212+
assert.Equal(t, component, result)
213+
214+
latestReleasePublishedAt := time.Date(2023, time.May, 1, 0, 0, 0, 0, time.UTC)
215+
packageData.LatestReleasePublishedAt = &latestReleasePublishedAt
216+
expectedTimestamp := latestReleasePublishedAt.UTC().Format(time.RFC3339)
217+
result = enrichLatestReleasePublishedAt(component, packageData)
218+
219+
prop := (*result.Properties)[0]
220+
assert.Equal(t, "ecosystems:latest_release_published_at", prop.Name)
221+
assert.Equal(t, expectedTimestamp, prop.Value)
222+
}
223+
224+
func TestEnrichLocation(t *testing.T) {
225+
assert := assert.New(t)
226+
227+
// Test case 1: packageData.RepoMetadata is nil
228+
component := cdx.Component{Name: "test"}
229+
packageData := packages.Package{}
230+
result := enrichLocation(component, packageData)
231+
assert.Equal(component, result)
232+
233+
// Test case 2: packageData.RepoMetadata is not nil, but "owner_record" is missing
234+
component = cdx.Component{Name: "test"}
235+
packageData = packages.Package{RepoMetadata: &map[string]interface{}{
236+
"not_owner_record": map[string]interface{}{},
237+
}}
238+
result = enrichLocation(component, packageData)
239+
assert.Equal(component, result)
240+
241+
// Test case 3: "location" field is missing in "owner_record"
242+
component = cdx.Component{Name: "test"}
243+
packageData = packages.Package{RepoMetadata: &map[string]interface{}{
244+
"owner_record": map[string]interface{}{
245+
"not_location": "test",
246+
},
247+
}}
248+
result = enrichLocation(component, packageData)
249+
assert.Equal(component, result)
250+
251+
// Test case 4: "location" field is present in "owner_record"
252+
component = cdx.Component{Name: "test"}
253+
packageData = packages.Package{RepoMetadata: &map[string]interface{}{
254+
"owner_record": map[string]interface{}{
255+
"location": "test_location",
256+
},
257+
}}
258+
expectedComponent := cdx.Component{
259+
Name: "test",
260+
Properties: &[]cdx.Property{
261+
{Name: "ecosystems:owner_location", Value: "test_location"},
262+
},
263+
}
264+
result = enrichLocation(component, packageData)
265+
assert.Equal(expectedComponent, result)
266+
}

0 commit comments

Comments
 (0)