Skip to content

Commit b972089

Browse files
authored
feat: add contract selection on workflow update (#646)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 222fdf5 commit b972089

File tree

10 files changed

+93
-14
lines changed

10 files changed

+93
-14
lines changed

app/cli/cmd/workflow_update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func newWorkflowUpdateCmd() *cobra.Command {
26-
var workflowID, name, description, project, team string
26+
var workflowID, name, description, project, team, contractID string
2727
var public bool
2828

2929
cmd := &cobra.Command{
@@ -48,6 +48,10 @@ func newWorkflowUpdateCmd() *cobra.Command {
4848
opts.Description = &description
4949
}
5050

51+
if cmd.Flags().Changed("contract") {
52+
opts.ContractID = &contractID
53+
}
54+
5155
res, err := action.NewWorkflowUpdate(actionOpts).Run(context.Background(), workflowID, opts)
5256
if err != nil {
5357
return err
@@ -67,6 +71,7 @@ func newWorkflowUpdateCmd() *cobra.Command {
6771
cmd.Flags().StringVar(&team, "team", "", "team name")
6872
cmd.Flags().StringVar(&project, "project", "", "project name")
6973
cmd.Flags().BoolVar(&public, "public", false, "is the workflow public")
74+
cmd.Flags().StringVar(&contractID, "contract", "", "the ID of an existing contract")
7075

7176
return cmd
7277
}

app/cli/internal/action/workflow_update.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func NewWorkflowUpdate(cfg *ActionsOpts) *WorkflowUpdate {
3030
}
3131

3232
type NewWorkflowUpdateOpts struct {
33-
Name, Description, Project, Team *string
34-
Public *bool
33+
Name, Description, Project, Team, ContractID *string
34+
Public *bool
3535
}
3636

3737
func (action *WorkflowUpdate) Run(ctx context.Context, id string, opts *NewWorkflowUpdateOpts) (*WorkflowItem, error) {
@@ -40,7 +40,8 @@ func (action *WorkflowUpdate) Run(ctx context.Context, id string, opts *NewWorkf
4040
Id: id,
4141
Name: opts.Name, Description: opts.Description,
4242
Project: opts.Project, Team: opts.Team,
43-
Public: opts.Public,
43+
Public: opts.Public,
44+
SchemaId: opts.ContractID,
4445
})
4546

4647
if err != nil {

app/controlplane/api/controlplane/v1/workflow.pb.go

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

app/controlplane/api/controlplane/v1/workflow.pb.validate.go

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

app/controlplane/api/controlplane/v1/workflow.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ message WorkflowServiceUpdateRequest {
4949
optional string team = 4;
5050
optional bool public = 5;
5151
optional string description = 6;
52+
optional string schema_id = 7;
5253
}
5354

5455
message WorkflowServiceUpdateResponse {

app/controlplane/api/gen/frontend/controlplane/v1/workflow.ts

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

app/controlplane/internal/biz/workflow.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ type WorkflowCreateOpts struct {
5858
}
5959

6060
type WorkflowUpdateOpts struct {
61-
Name, Project, Team, Description *string
62-
Public *bool
61+
Name, Project, Team, Description, ContractID *string
62+
Public *bool
6363
}
6464

6565
type WorkflowUseCase struct {
@@ -144,6 +144,15 @@ func (uc *WorkflowUseCase) Update(ctx context.Context, orgID, workflowID string,
144144
return nil, NewErrNotFound("workflow in organization")
145145
}
146146

147+
// Double check that the contract exists
148+
if opts.ContractID != nil {
149+
if c, err := uc.contractUC.FindByIDInOrg(ctx, orgID, *opts.ContractID); err != nil {
150+
return nil, err
151+
} else if c == nil {
152+
return nil, NewErrNotFound("contract")
153+
}
154+
}
155+
147156
wf, err := uc.wfRepo.Update(ctx, workflowUUID, opts)
148157
if err != nil {
149158
if errors.Is(err, ErrAlreadyExists) {

app/controlplane/internal/biz/workflow_integration_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
157157
workflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: name, OrgID: s.org.ID})
158158
require.NoError(s.T(), err)
159159

160+
// Create two contracts in two different orgs
161+
contract1, err := s.WorkflowContract.Create(ctx, &biz.WorkflowContractCreateOpts{Name: "contract-1", OrgID: s.org.ID})
162+
require.NoError(s.T(), err)
163+
contract2, err := s.WorkflowContract.Create(ctx, &biz.WorkflowContractCreateOpts{Name: "contract-2", OrgID: org2.ID})
164+
require.NoError(s.T(), err)
165+
160166
s.Run("by default the workflow is private", func() {
161167
s.False(workflow.Public)
162168
})
@@ -238,6 +244,16 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
238244
updates: &biz.WorkflowUpdateOpts{Name: toPtrS("")},
239245
wantErr: true,
240246
},
247+
{
248+
name: "can update contract",
249+
updates: &biz.WorkflowUpdateOpts{ContractID: toPtrS(contract1.ID.String())},
250+
want: &biz.Workflow{Description: description, Team: team, Project: project, ContractID: contract2.ID},
251+
},
252+
{
253+
name: "can not update contract in another org",
254+
updates: &biz.WorkflowUpdateOpts{ContractID: toPtrS(contract2.ID.String())},
255+
wantErr: true,
256+
},
241257
{
242258
name: "but other opts can",
243259
updates: &biz.WorkflowUpdateOpts{Team: toPtrS(""), Project: toPtrS(""), Description: toPtrS("")},

app/controlplane/internal/data/workflow.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ func (r *WorkflowRepo) Update(ctx context.Context, id uuid.UUID, opts *biz.Workf
9191
req = req.SetName(*opts.Name)
9292
}
9393

94+
// Update the contract if provided
95+
if opts.ContractID != nil {
96+
contractUUID, err := uuid.Parse(*opts.ContractID)
97+
if err != nil {
98+
return nil, err
99+
}
100+
req = req.SetContractID(contractUUID)
101+
}
102+
94103
wf, err := req.Save(ctx)
95104

96105
if err != nil {

app/controlplane/internal/service/workflow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2024 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -79,6 +79,7 @@ func (s *WorkflowService) Update(ctx context.Context, req *pb.WorkflowServiceUpd
7979
Team: req.Team,
8080
Public: req.Public,
8181
Description: req.Description,
82+
ContractID: req.SchemaId,
8283
}
8384

8485
p, err := s.useCase.Update(ctx, currentOrg.ID, req.Id, updateOpts)

0 commit comments

Comments
 (0)