Skip to content

Commit

Permalink
feat: add support for project versioning in CLI (chainloop-dev#1450)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Martinez <[email protected]>
  • Loading branch information
migmartri authored Oct 29, 2024
1 parent 0d02d23 commit 1c88afb
Show file tree
Hide file tree
Showing 24 changed files with 548 additions and 268 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ linters:
# - unparam

linters-settings:
gosec:
excludes:
# https://github.com/moby/moby/issues/48358
- G115
gofmt:
simplify: true
dupl:
Expand Down
15 changes: 12 additions & 3 deletions app/cli/cmd/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newAttestationInitCmd() *cobra.Command {
attestationDryRun bool
workflowName string
projectName string
projectVersion string
newWorkflowcontractName string
)

Expand Down Expand Up @@ -60,7 +61,14 @@ func newAttestationInitCmd() *cobra.Command {
}

// Initialize it
attestationID, err := a.Run(cmd.Context(), contractRevision, projectName, workflowName, newWorkflowcontractName)
attestationID, err := a.Run(cmd.Context(), &action.AttestationInitRunOpts{
ContractRevision: contractRevision,
ProjectName: projectName,
ProjectVersion: projectVersion,
WorkflowName: workflowName,
NewWorkflowContractName: newWorkflowcontractName,
})

if err != nil {
if errors.Is(err, action.ErrAttestationAlreadyExist) {
return err
Expand Down Expand Up @@ -93,8 +101,7 @@ func newAttestationInitCmd() *cobra.Command {
}

return encodeOutput(res, fullStatusTable)
},
}
}}

// This option is only useful for local-based attestation states
cmd.Flags().BoolVarP(&force, "replace", "f", false, "replace any existing in-progress attestation")
Expand All @@ -113,5 +120,7 @@ func newAttestationInitCmd() *cobra.Command {
cobra.CheckErr(cmd.MarkFlagRequired("project"))
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)")

cmd.Flags().StringVar(&projectVersion, "version", "", "project version, i.e 0.1.0")

return cmd
}
6 changes: 6 additions & 0 deletions app/cli/cmd/attestation_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func attestationStatusTableOutput(status *action.AttestationStatusResult, full b
gt.AppendRow(table.Row{"Organization", meta.Organization})
gt.AppendRow(table.Row{"Name", meta.Name})
gt.AppendRow(table.Row{"Project", meta.Project})
projectVersion := "none"
if meta.ProjectVersion != "" {
projectVersion = meta.ProjectVersion
}

gt.AppendRow(table.Row{"Version", projectVersion})
gt.AppendRow(table.Row{"Contract Revision", meta.ContractRevision})
if status.RunnerContext.JobURL != "" {
gt.AppendRow(table.Row{"Runner Type", status.RunnerContext.RunnerType})
Expand Down
6 changes: 3 additions & 3 deletions app/cli/cmd/workflow_workflow_run_list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2023 The Chainloop Authors.
// Copyright 2024 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,7 +95,7 @@ func workflowRunListTableOutput(runs []*action.WorkflowRunItem) error {
return nil
}

header := table.Row{"ID", "Workflow", "State", "Created At", "Runner"}
header := table.Row{"ID", "Workflow", "Version", "State", "Created At", "Runner"}
if full {
header = append(header, "Finished At", "Failure reason")
}
Expand All @@ -105,7 +105,7 @@ func workflowRunListTableOutput(runs []*action.WorkflowRunItem) error {

for _, p := range runs {
wf := p.Workflow
r := table.Row{p.ID, wf.NamespacedName(), p.State, p.CreatedAt.Format(time.RFC822), p.RunnerType}
r := table.Row{p.ID, wf.NamespacedName(), p.ProjectVersion.Version, p.State, p.CreatedAt.Format(time.RFC822), p.RunnerType}

if full {
var finishedAt string
Expand Down
30 changes: 20 additions & 10 deletions app/cli/internal/action/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ func NewAttestationInit(cfg *AttestationInitOpts) (*AttestationInit, error) {
}

// returns the attestation ID
func (action *AttestationInit) Run(ctx context.Context, contractRevision int, projectName, workflowName, newWorkflowContractName string) (string, error) {
type AttestationInitRunOpts struct {
ContractRevision int
ProjectName string
ProjectVersion string
WorkflowName string
NewWorkflowContractName string
}

func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRunOpts) (string, error) {
if action.dryRun && action.useRemoteState {
return "", errors.New("remote state is not compatible with dry-run mode")
}
Expand All @@ -87,9 +95,9 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
// 1 - Find or create the workflow
workflowsResp, err := client.FindOrCreateWorkflow(ctx, &pb.FindOrCreateWorkflowRequest{
ProjectName: projectName,
WorkflowName: workflowName,
ContractName: newWorkflowContractName,
ProjectName: opts.ProjectName,
WorkflowName: opts.WorkflowName,
ContractName: opts.NewWorkflowContractName,
})
if err != nil {
return "", err
Expand All @@ -98,9 +106,9 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr

// 2 - Get contract
contractResp, err := client.GetContract(ctx, &pb.AttestationServiceGetContractRequest{
ContractRevision: int32(contractRevision),
WorkflowName: workflowName,
ProjectName: projectName,
ContractRevision: int32(opts.ContractRevision),
WorkflowName: opts.WorkflowName,
ProjectName: opts.ProjectName,
})
if err != nil {
return "", err
Expand All @@ -113,6 +121,7 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
Project: workflow.GetProject(),
Team: workflow.GetTeam(),
SchemaRevision: strconv.Itoa(int(contractVersion.GetRevision())),
ProjectVersion: opts.ProjectVersion,
}

action.Logger.Debug().Msg("workflow contract and metadata retrieved from the control plane")
Expand All @@ -133,10 +142,11 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
&pb.AttestationServiceInitRequest{
Runner: discoveredRunner.ID(),
JobUrl: discoveredRunner.RunURI(),
ContractRevision: int32(contractRevision),
ContractRevision: int32(opts.ContractRevision),
// send the workflow name explicitly provided by the user to detect that functional case
WorkflowName: workflowName,
ProjectName: projectName,
WorkflowName: opts.WorkflowName,
ProjectName: opts.ProjectName,
ProjectVersion: opts.ProjectVersion,
},
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion app/cli/internal/action/attestation_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type AttestationResultRunnerContext struct {
}

type AttestationStatusWorkflowMeta struct {
WorkflowID, Name, Team, Project, ContractRevision, Organization string
WorkflowID, Name, Team, Project, ContractRevision, Organization, ProjectVersion string
}

type AttestationStatusResultMaterial struct {
Expand Down Expand Up @@ -98,6 +98,7 @@ func (action *AttestationStatus) Run(ctx context.Context, attestationID string)
Project: workflowMeta.GetProject(),
Team: workflowMeta.GetTeam(),
ContractRevision: workflowMeta.GetSchemaRevision(),
ProjectVersion: workflowMeta.GetProjectVersion(),
},
InitializedAt: toTimePtr(att.InitializedAt.AsTime()),
DryRun: c.CraftingState.DryRun,
Expand Down
Loading

0 comments on commit 1c88afb

Please sign in to comment.