Skip to content

Commit 278c342

Browse files
authored
fix(project): support setting aggregation logic (#49)
1 parent 9eef6be commit 278c342

File tree

2 files changed

+172
-3
lines changed

2 files changed

+172
-3
lines changed

project.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
"github.com/google/uuid"
1111
)
1212

13+
type CollectionLogic string
14+
15+
var (
16+
CollectionLogicNone CollectionLogic = "NONE"
17+
CollectionLogicAggregateDirectChildren CollectionLogic = "AGGREGATE_DIRECT_CHILDREN"
18+
CollectionLogicAggregateDirectChildrenWithTag CollectionLogic = "AGGREGATE_DIRECT_CHILDREN_WITH_TAG"
19+
CollectionLogicAggregateLatestVersionChildren CollectionLogic = "AGGREGATE_LATEST_VERSION_CHILDREN"
20+
)
21+
1322
type Project struct {
1423
UUID uuid.UUID `json:"uuid,omitempty"`
1524
Author string `json:"author,omitempty"`
@@ -31,6 +40,8 @@ type Project struct {
3140
ParentRef *ParentRef `json:"parent,omitempty"`
3241
LastBOMImport int `json:"lastBomImport"`
3342
ExternalReferences []ExternalReference `json:"externalReferences,omitempty"`
43+
CollectionLogic CollectionLogic `json:"collectionLogic,omitempty"`
44+
CollectionTag string `json:"collectionTag,omitempty"`
3445
}
3546

3647
type ParentRef struct {

project_test.go

Lines changed: 161 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package dtrack
22

33
import (
44
"context"
5-
"github.com/stretchr/testify/require"
5+
"fmt"
6+
"math/rand"
67
"testing"
8+
9+
"github.com/google/uuid"
10+
"github.com/stretchr/testify/require"
711
)
812

913
func TestProjectService_Clone(t *testing.T) {
@@ -27,6 +31,160 @@ func TestProjectService_Clone(t *testing.T) {
2731
require.NotEmpty(t, token)
2832
}
2933

34+
func TestProjectService_CreateWithCollection(t *testing.T) {
35+
client := setUpContainer(t, testContainerOptions{
36+
APIPermissions: []string{
37+
PermissionPortfolioManagement,
38+
PermissionTagManagement,
39+
},
40+
})
41+
42+
collectionDirectChildrenProject, err := client.Project.Create(context.Background(), Project{
43+
Name: "acme-app",
44+
Version: "1.0.0",
45+
Active: true,
46+
CollectionLogic: CollectionLogicAggregateDirectChildren,
47+
})
48+
require.NoError(t, err)
49+
50+
tag := "weewoo"
51+
err = client.Tag.Create(context.Background(), []string{tag})
52+
require.NoError(t, err)
53+
54+
collectionTags, err := client.Project.Create(context.Background(), Project{
55+
Name: "acme-app-2",
56+
Version: "1.0.0",
57+
Active: true,
58+
CollectionLogic: CollectionLogicAggregateDirectChildrenWithTag,
59+
})
60+
require.NoError(t, err)
61+
62+
cases := []struct {
63+
name string
64+
childProjects []Project
65+
projectName string
66+
parent uuid.UUID
67+
expErr error
68+
}{
69+
{
70+
name: "aggregate_collection_logic_direct_children_single_child",
71+
childProjects: []Project{
72+
{
73+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
74+
ParentRef: &ParentRef{
75+
UUID: collectionDirectChildrenProject.UUID,
76+
},
77+
},
78+
},
79+
parent: collectionDirectChildrenProject.UUID,
80+
},
81+
{
82+
name: "aggregate_collection_logic_direct_children_multiple_children",
83+
childProjects: []Project{
84+
{
85+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
86+
ParentRef: &ParentRef{
87+
UUID: collectionDirectChildrenProject.UUID,
88+
},
89+
},
90+
{
91+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
92+
ParentRef: &ParentRef{
93+
UUID: collectionDirectChildrenProject.UUID,
94+
},
95+
},
96+
{
97+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
98+
ParentRef: &ParentRef{
99+
UUID: collectionDirectChildrenProject.UUID,
100+
},
101+
},
102+
{
103+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
104+
ParentRef: &ParentRef{
105+
UUID: collectionDirectChildrenProject.UUID,
106+
},
107+
},
108+
},
109+
parent: collectionDirectChildrenProject.UUID,
110+
},
111+
{
112+
name: "aggregate_collection_logic_tags_single_child",
113+
childProjects: []Project{
114+
{
115+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
116+
Tags: []Tag{{Name: tag}},
117+
ParentRef: &ParentRef{
118+
UUID: collectionTags.UUID,
119+
},
120+
},
121+
},
122+
parent: collectionTags.UUID,
123+
},
124+
{
125+
name: "aggregate_collection_logic_tags_ multiple_children",
126+
childProjects: []Project{
127+
{
128+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
129+
Tags: []Tag{{Name: tag}},
130+
ParentRef: &ParentRef{
131+
UUID: collectionTags.UUID,
132+
},
133+
},
134+
{
135+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
136+
Tags: []Tag{{Name: tag}},
137+
ParentRef: &ParentRef{
138+
UUID: collectionTags.UUID,
139+
},
140+
},
141+
{
142+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
143+
Tags: []Tag{{Name: tag}},
144+
ParentRef: &ParentRef{
145+
UUID: collectionTags.UUID,
146+
},
147+
},
148+
{
149+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
150+
Tags: []Tag{{Name: tag}},
151+
ParentRef: &ParentRef{
152+
UUID: collectionTags.UUID,
153+
},
154+
},
155+
{
156+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
157+
Tags: []Tag{{Name: tag}},
158+
ParentRef: &ParentRef{
159+
UUID: collectionTags.UUID,
160+
},
161+
},
162+
{
163+
Name: fmt.Sprintf("child-%d", rand.Intn(100000)), //nolint:gosec
164+
Tags: []Tag{{Name: tag}},
165+
ParentRef: &ParentRef{
166+
UUID: collectionTags.UUID,
167+
},
168+
},
169+
},
170+
parent: collectionTags.UUID,
171+
},
172+
}
173+
174+
for _, tc := range cases {
175+
t.Run(tc.name, func(t *testing.T) {
176+
tc := tc
177+
t.Parallel()
178+
for _, project := range tc.childProjects {
179+
childProject, err := client.Project.Create(context.Background(), project)
180+
require.Equal(t, tc.expErr, err)
181+
require.NotEmpty(t, childProject.ParentRef)
182+
require.Equal(t, tc.parent, childProject.ParentRef.UUID)
183+
}
184+
})
185+
}
186+
}
187+
30188
func TestProjectService_Clone_v4_10(t *testing.T) {
31189
client := setUpContainer(t, testContainerOptions{
32190
Version: "4.10.1",
@@ -48,6 +206,7 @@ func TestProjectService_Clone_v4_10(t *testing.T) {
48206
require.NoError(t, err)
49207
require.Empty(t, token)
50208
}
209+
51210
func TestProjectService_Latest(t *testing.T) {
52211
client := setUpContainer(t, testContainerOptions{
53212
Version: "4.12.7",
@@ -56,7 +215,7 @@ func TestProjectService_Latest(t *testing.T) {
56215
PermissionViewPortfolio,
57216
},
58217
})
59-
var name = "acme-app"
218+
name := "acme-app"
60219
project, err := client.Project.Create(context.Background(), Project{
61220
Name: name,
62221
Version: "1.0.0",
@@ -88,5 +247,4 @@ func TestProjectService_Latest(t *testing.T) {
88247

89248
require.NoError(t, err)
90249
require.Equal(t, "2.0.0", latest.Version)
91-
92250
}

0 commit comments

Comments
 (0)