Skip to content

Commit cd976a8

Browse files
committed
feat: auto-create workflow during attestation
Signed-off-by: Miguel Martinez <[email protected]>
1 parent ecaf8c2 commit cd976a8

13 files changed

+1025
-499
lines changed

app/cli/cmd/attestation_init.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import (
2525

2626
func newAttestationInitCmd() *cobra.Command {
2727
var (
28-
force bool
29-
contractRevision int
30-
attestationDryRun bool
31-
workflowName string
32-
projectName string
28+
force bool
29+
contractRevision int
30+
attestationDryRun bool
31+
workflowName string
32+
projectName string
33+
newWorkflowcontractName string
3334
)
3435

3536
cmd := &cobra.Command{
@@ -59,7 +60,7 @@ func newAttestationInitCmd() *cobra.Command {
5960
}
6061

6162
// Initialize it
62-
attestationID, err := a.Run(cmd.Context(), contractRevision, projectName, workflowName)
63+
attestationID, err := a.Run(cmd.Context(), contractRevision, projectName, workflowName, newWorkflowcontractName)
6364
if err != nil {
6465
if errors.Is(err, action.ErrAttestationAlreadyExist) {
6566
return err
@@ -110,6 +111,7 @@ func newAttestationInitCmd() *cobra.Command {
110111

111112
cmd.Flags().StringVar(&projectName, "project", "", "name of the project of this workflow")
112113
cobra.CheckErr(cmd.MarkFlagRequired("project"))
114+
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)")
113115

114116
return cmd
115117
}

app/cli/internal/action/attestation_init.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewAttestationInit(cfg *AttestationInitOpts) (*AttestationInit, error) {
7070
}
7171

7272
// returns the attestation ID
73-
func (action *AttestationInit) Run(ctx context.Context, contractRevision int, projectName, workflowName string) (string, error) {
73+
func (action *AttestationInit) Run(ctx context.Context, contractRevision int, projectName, workflowName, newWorkflowContractName string) (string, error) {
7474
if action.dryRun && action.useRemoteState {
7575
return "", errors.New("remote state is not compatible with dry-run mode")
7676
}
@@ -85,7 +85,18 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
8585

8686
action.Logger.Debug().Msg("Retrieving attestation definition")
8787
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
88-
// get information of the workflow
88+
// 1 - Find or create the workflow
89+
workflowsResp, err := client.FindOrCreateWorkflow(ctx, &pb.FindOrCreateWorkflowRequest{
90+
ProjectName: projectName,
91+
WorkflowName: workflowName,
92+
ContractName: newWorkflowContractName,
93+
})
94+
if err != nil {
95+
return "", err
96+
}
97+
workflow := workflowsResp.GetResult()
98+
99+
// 2 - Get contract
89100
contractResp, err := client.GetContract(ctx, &pb.AttestationServiceGetContractRequest{
90101
ContractRevision: int32(contractRevision),
91102
WorkflowName: workflowName,
@@ -95,10 +106,7 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
95106
return "", err
96107
}
97108

98-
workflow := contractResp.GetResult().GetWorkflow()
99109
contractVersion := contractResp.Result.GetContract()
100-
contract := contractResp.GetResult().GetContract().GetV1()
101-
102110
workflowMeta := &clientAPI.WorkflowMetadata{
103111
WorkflowId: workflow.GetId(),
104112
Name: workflow.GetName(),
@@ -110,7 +118,7 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
110118
action.Logger.Debug().Msg("workflow contract and metadata retrieved from the control plane")
111119

112120
// Auto discover the runner context and enforce against the one in the contract if needed
113-
discoveredRunner, err := crafter.DiscoverAndEnforceRunner(contract.GetRunner().GetType(), action.dryRun, action.Logger)
121+
discoveredRunner, err := crafter.DiscoverAndEnforceRunner(contractVersion.GetV1().GetRunner().GetType(), action.dryRun, action.Logger)
114122
if err != nil {
115123
return "", ErrRunnerContextNotFound{err.Error()}
116124
}

0 commit comments

Comments
 (0)