Skip to content

Commit 3211387

Browse files
authored
feat(cli): add scope field to contract list JSON output (#3019)
1 parent 16ad43e commit 3211387

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

app/cli/pkg/action/workflow_contract_list.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023-2025 The Chainloop Authors.
2+
// Copyright 2023-2026 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ type WorkflowContractItem struct {
3838
Workflows []string `json:"workflows,omitempty"` // TODO: remove this field after all clients are updated
3939
WorkflowRefs []*WorkflowRef `json:"workflowRefs,omitempty"`
4040
ScopedEntity *ScopedEntity `json:"scopedEntity,omitempty"`
41+
Scope string `json:"scope"`
4142
}
4243

4344
type ScopedEntity struct {
@@ -118,6 +119,8 @@ func pbWorkflowContractItemToAction(in *pb.WorkflowContractItem) *WorkflowContra
118119
}
119120
}
120121

122+
item.Scope = item.ScopedEntity.String()
123+
121124
return item
122125
}
123126

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// Copyright 2026 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package action
17+
18+
import (
19+
"testing"
20+
21+
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
22+
"github.com/stretchr/testify/assert"
23+
"google.golang.org/protobuf/types/known/timestamppb"
24+
)
25+
26+
func TestPbWorkflowContractItemToAction(t *testing.T) {
27+
now := timestamppb.Now()
28+
29+
tests := []struct {
30+
name string
31+
input *pb.WorkflowContractItem
32+
expectedScope string
33+
}{
34+
{
35+
name: "global scope when scoped entity is nil",
36+
input: &pb.WorkflowContractItem{
37+
Id: "contract-id",
38+
Name: "my-contract",
39+
LatestRevision: 3,
40+
CreatedAt: now,
41+
LatestRevisionCreatedAt: now,
42+
},
43+
expectedScope: "global",
44+
},
45+
{
46+
name: "project scope when scoped entity is set",
47+
input: &pb.WorkflowContractItem{
48+
Id: "contract-id",
49+
Name: "my-contract",
50+
LatestRevision: 1,
51+
CreatedAt: now,
52+
LatestRevisionCreatedAt: now,
53+
ScopedEntity: &pb.ScopedEntity{
54+
Type: "project",
55+
Id: "project-id",
56+
Name: "my-project",
57+
},
58+
},
59+
expectedScope: "project/my-project",
60+
},
61+
{
62+
name: "workflow refs are converted",
63+
input: &pb.WorkflowContractItem{
64+
Id: "contract-id",
65+
Name: "my-contract",
66+
CreatedAt: now,
67+
LatestRevisionCreatedAt: now,
68+
WorkflowRefs: []*pb.WorkflowRef{
69+
{Id: "wf-1", Name: "build", ProjectName: "proj-a"},
70+
{Id: "wf-2", Name: "deploy", ProjectName: "proj-b"},
71+
},
72+
},
73+
expectedScope: "global",
74+
},
75+
}
76+
77+
for _, tc := range tests {
78+
t.Run(tc.name, func(t *testing.T) {
79+
result := pbWorkflowContractItemToAction(tc.input)
80+
81+
assert.Equal(t, tc.input.GetName(), result.Name)
82+
assert.Equal(t, tc.input.GetId(), result.ID)
83+
assert.Equal(t, int(tc.input.GetLatestRevision()), result.LatestRevision)
84+
assert.Equal(t, tc.expectedScope, result.Scope)
85+
86+
if tc.input.ScopedEntity != nil {
87+
assert.NotNil(t, result.ScopedEntity)
88+
assert.Equal(t, tc.input.ScopedEntity.Type, result.ScopedEntity.Type)
89+
assert.Equal(t, tc.input.ScopedEntity.Id, result.ScopedEntity.ID)
90+
assert.Equal(t, tc.input.ScopedEntity.Name, result.ScopedEntity.Name)
91+
} else {
92+
assert.Nil(t, result.ScopedEntity)
93+
}
94+
95+
assert.Len(t, result.WorkflowRefs, len(tc.input.WorkflowRefs))
96+
})
97+
}
98+
}

0 commit comments

Comments
 (0)