Skip to content

Commit cc7e208

Browse files
fix: allow hyphens in annotation names
Signed-off-by: wangyusheng1985 <wangyusheng1985@users.noreply.github.com>
1 parent 687c6ab commit cc7e208

7 files changed

Lines changed: 55 additions & 18 deletions

File tree

‎app/controlplane/api/gen/frontend/workflowcontract/v1/crafting_schema.ts‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/controlplane/api/gen/jsonschema/workflowcontract.v1.Annotation.jsonschema.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/controlplane/api/gen/jsonschema/workflowcontract.v1.Annotation.schema.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/controlplane/api/workflowcontract/v1/crafting_schema.pb.go‎

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/controlplane/api/workflowcontract/v1/crafting_schema.proto‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ message CraftingSchemaV2Spec {
241241
}
242242

243243
message Annotation {
244-
string name = 1 [(buf.validate.field).string.pattern = "^[\\w]+$"]; // Single word optionally separated with _
244+
string name = 1 [(buf.validate.field).string.pattern = "^[\\w-]+$"]; // Single word optionally separated with _ or -
245245
// This value can be set in the contract or provided during the attestation
246246
string value = 2;
247247
}

‎app/controlplane/api/workflowcontract/v1/crafting_schema_test.go‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
// limitations under the License.
17-
1816
package v1_test
1917

2018
import (
2119
"errors"
2220
"testing"
2321

22+
validate "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
2423
"buf.build/go/protovalidate"
2524
v1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2625
"github.com/stretchr/testify/assert"
2726
"github.com/stretchr/testify/require"
27+
"google.golang.org/protobuf/proto"
28+
"google.golang.org/protobuf/types/descriptorpb"
2829
)
2930

3031
func TestValidateAnnotations(t *testing.T) {
@@ -50,17 +51,28 @@ func TestValidateAnnotations(t *testing.T) {
5051
value: "hello_world",
5152
},
5253
{
53-
desc: "invalid key hyphen",
54-
name: "hello-world",
55-
value: "hello-world",
56-
wantErr: true,
54+
desc: "valid key hyphen",
55+
name: "hello-world",
56+
value: "hello-world",
5757
},
5858
{
5959
desc: "invalid key space",
6060
name: " hello",
6161
value: "hello-world",
6262
wantErr: true,
6363
},
64+
{
65+
desc: "invalid key punctuation",
66+
name: "hello.world",
67+
value: "hello",
68+
wantErr: true,
69+
},
70+
{
71+
desc: "invalid key slash",
72+
name: "hello/world",
73+
value: "hello",
74+
wantErr: true,
75+
},
6476
{
6577
desc: "valid key camel case",
6678
name: "helloWorld",
@@ -94,6 +106,19 @@ func TestValidateAnnotations(t *testing.T) {
94106
}
95107
}
96108

109+
func TestAnnotationNameConstraintAllowsHyphen(t *testing.T) {
110+
field := (&v1.Annotation{}).ProtoReflect().Descriptor().Fields().ByName("name")
111+
require.NotNil(t, field)
112+
113+
opts, ok := field.Options().(*descriptorpb.FieldOptions)
114+
require.True(t, ok)
115+
116+
constraints, ok := proto.GetExtension(opts, validate.E_Field).(*validate.FieldRules)
117+
require.True(t, ok)
118+
require.NotNil(t, constraints.GetString())
119+
assert.Equal(t, `^[\w-]+$`, constraints.GetString().GetPattern())
120+
}
121+
97122
func TestPolicyAttachment(t *testing.T) {
98123
testCases := []struct {
99124
desc string

‎app/controlplane/plugins/core/dependency-track/v1/extension_test.go‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,17 @@ func TestResolveProjectName(t *testing.T) {
146146
projectName: "{{.Attestation.Annotations.Version}}",
147147
want: "1.2.3",
148148
},
149+
{
150+
name: "hyphenated annotation interpolates via index",
151+
projectName: `{{ index .Material.Annotations "my-annotation" }}`,
152+
want: "hyphen-ok",
153+
},
149154
}
150155

151156
sbomAnnotation := map[string]string{
152-
"hello": "hola",
153-
"world": "mundo",
157+
"hello": "hola",
158+
"world": "mundo",
159+
"my-annotation": "hyphen-ok",
154160
}
155161

156162
attAnnotation := map[string]string{
@@ -348,8 +354,9 @@ func TestVerifyAllFilters(t *testing.T) {
348354
}
349355

350356
materialAnnotations := map[string]string{
351-
"environment": "staging",
352-
"critical": "true",
357+
"environment": "staging",
358+
"critical": "true",
359+
"release-stage": "ga",
353360
}
354361

355362
testCases := []struct {
@@ -398,6 +405,10 @@ func TestVerifyAllFilters(t *testing.T) {
398405
filter: "environment",
399406
errMsg: "invalid filter segment",
400407
},
408+
{
409+
name: "hyphenated annotation name matches",
410+
filter: "release-stage=ga",
411+
},
401412
}
402413

403414
for _, tc := range testCases {

0 commit comments

Comments
 (0)