MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / getGroupMaterialsToAdd

Function getGroupMaterialsToAdd

app/cli/pkg/action/attestation_init.go:428–469  ·  view source on GitHub ↗

merge existing materials with group ones, taking the contract's one in case of conflict

(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, fromContract []*v1.CraftingSchema_Material, logger *zerolog.Logger)

Source from the content-addressed store, hash-verified

426
427// merge existing materials with group ones, taking the contract's one in case of conflict
428func getGroupMaterialsToAdd(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, fromContract []*v1.CraftingSchema_Material, logger *zerolog.Logger) ([]*v1.CraftingSchema_Material, error) {
429 toAdd := make([]*v1.CraftingSchema_Material, 0)
430 for _, groupMaterial := range group.GetSpec().GetPolicies().GetMaterials() {
431 // if material has no name, it's not enforced
432 if groupMaterial.GetName() == "" {
433 continue
434 }
435
436 // apply bindings if needed
437 csm, err := groupMaterialToCraftingSchemaMaterial(groupMaterial, group, pgAtt, logger)
438 if err != nil {
439 return nil, err
440 }
441 // skip if interpolated material name is still empty
442 if csm.GetName() == "" {
443 continue
444 }
445
446 // If the material is already declared in the contract, keep the contract
447 // definition and don't add a duplicate. Declaring the same material in
448 // both the contract and a policy group is a legitimate, expected case, so
449 // it's merged silently when the definitions are compatible; we only warn
450 // when the types genuinely conflict.
451 ignore := false
452 for _, mat := range fromContract {
453 if mat.GetName() != csm.GetName() {
454 continue
455 }
456 ignore = true
457 if mat.GetType() != csm.GetType() {
458 logger.Warn().Msgf("material '%s' is declared in both the contract (%s) and policy group '%s' (%s); using the contract definition",
459 mat.GetName(), mat.GetType(), group.GetMetadata().GetName(), csm.GetType())
460 }
461 break
462 }
463 if !ignore {
464 toAdd = append(toAdd, csm)
465 }
466 }
467
468 return toAdd, nil
469}
470
471// translates materials and interpolates material names
472func groupMaterialToCraftingSchemaMaterial(gm *v1.PolicyGroup_Material, group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, logger *zerolog.Logger) (*v1.CraftingSchema_Material, error) {

Callers 1

Calls 7

GetMaterialsMethod · 0.65
GetNameMethod · 0.65
GetMetadataMethod · 0.65
GetPoliciesMethod · 0.45
GetSpecMethod · 0.45
GetTypeMethod · 0.45

Tested by

no test coverage detected