Skip to content

Commit 782f50e

Browse files
authored
att init: create contract on workflow creation (#1902)
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
1 parent d55d686 commit 782f50e

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

app/cli/cmd/attestation_init.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func newAttestationInitCmd() *cobra.Command {
3131
workflowName string
3232
projectName string
3333
projectVersion string
34-
projectVersionRelease bool
35-
newWorkflowcontractName string
34+
projectVersionRelease bool
35+
newWorkflowcontract string
3636
)
3737

3838
cmd := &cobra.Command{
@@ -90,7 +90,7 @@ func newAttestationInitCmd() *cobra.Command {
9090
ProjectName: projectName,
9191
ProjectVersion: projectVersion,
9292
WorkflowName: workflowName,
93-
NewWorkflowContractName: newWorkflowcontractName,
93+
NewWorkflowContractRef: newWorkflowcontract,
9494
ProjectVersionMarkAsReleased: projectVersionRelease,
9595
})
9696

@@ -146,7 +146,7 @@ func newAttestationInitCmd() *cobra.Command {
146146

147147
cmd.Flags().StringVar(&projectName, "project", "", "name of the project of this workflow")
148148
cobra.CheckErr(cmd.MarkFlagRequired("project"))
149-
cmd.Flags().StringVar(&newWorkflowcontractName, "contract", "", "name of an existing contract to attach it to the auto-created workflow (it doesn't update an existing one)")
149+
cmd.Flags().StringVar(&newWorkflowcontract, "contract", "", "name of an existing contract or the path/URL to a contract file, to attach it to the auto-created workflow (it doesn't update an existing one)")
150150

151151
cmd.Flags().StringVar(&projectVersion, "version", "", "project version, i.e 0.1.0")
152152
cmd.Flags().BoolVar(&projectVersionRelease, "release", false, "promote the provided version as a release")

app/cli/internal/action/attestation_init.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
clientAPI "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
2828
"github.com/chainloop-dev/chainloop/pkg/policies"
2929
"github.com/rs/zerolog"
30+
"google.golang.org/grpc/codes"
31+
"google.golang.org/grpc/status"
3032
)
3133

3234
type AttestationInitOpts struct {
@@ -80,7 +82,7 @@ type AttestationInitRunOpts struct {
8082
ProjectVersion string
8183
ProjectVersionMarkAsReleased bool
8284
WorkflowName string
83-
NewWorkflowContractName string
85+
NewWorkflowContractRef string
8486
}
8587

8688
func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRunOpts) (string, error) {
@@ -98,11 +100,32 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
98100

99101
action.Logger.Debug().Msg("Retrieving attestation definition")
100102
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
103+
104+
// 0 - find or create the contract if we are creating the workflow (if any)
105+
contractRef := opts.NewWorkflowContractRef
106+
_, err := NewWorkflowDescribe(action.ActionsOpts).Run(ctx, opts.WorkflowName, opts.ProjectName)
107+
if err != nil && status.Code(err) == codes.NotFound {
108+
// Not found, let's see if we need to create the contract
109+
if contractRef != "" {
110+
// Try to find it by name
111+
_, err := NewWorkflowContractDescribe(action.ActionsOpts).Run(contractRef, 0)
112+
// An invalid argument might be raised if we use a file or URL in the "name" field, which must be DNS-1123
113+
// TODO: validate locally before doing the query
114+
if err != nil && (status.Code(err) == codes.NotFound || status.Code(err) == codes.InvalidArgument) {
115+
createResp, err := NewWorkflowContractCreate(action.ActionsOpts).Run(fmt.Sprintf("%s-%s", opts.ProjectName, opts.WorkflowName), nil, contractRef)
116+
if err != nil {
117+
return "", err
118+
}
119+
contractRef = createResp.Name
120+
}
121+
}
122+
}
123+
101124
// 1 - Find or create the workflow
102125
workflowsResp, err := client.FindOrCreateWorkflow(ctx, &pb.FindOrCreateWorkflowRequest{
103126
ProjectName: opts.ProjectName,
104127
WorkflowName: opts.WorkflowName,
105-
ContractName: opts.NewWorkflowContractName,
128+
ContractName: contractRef,
106129
})
107130
if err != nil {
108131
return "", err

0 commit comments

Comments
 (0)