Skip to content

Commit ecaf8c2

Browse files
authored
feat: remove workflow-name org scoping (#1414)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 98099fc commit ecaf8c2

38 files changed

+330
-385
lines changed

app/cli/cmd/attestation_init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func newAttestationInitCmd() *cobra.Command {
107107
cobra.CheckErr(cmd.Flags().MarkDeprecated("name", "please use --workflow instead"))
108108
cmd.Flags().StringVar(&workflowName, "workflow-name", "", "name of the workflow to run the attestation")
109109
cobra.CheckErr(cmd.Flags().MarkDeprecated("workflow-name", "please use --workflow instead"))
110+
110111
cmd.Flags().StringVar(&projectName, "project", "", "name of the project of this workflow")
112+
cobra.CheckErr(cmd.MarkFlagRequired("project"))
113+
111114
return cmd
112115
}

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

Lines changed: 243 additions & 243 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_run.proto

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ message AttestationServiceGetPolicyGroupResponse {
8787
message AttestationServiceGetContractRequest {
8888
int32 contract_revision = 1;
8989
string workflow_name = 2 [(buf.validate.field).string.min_len = 1];
90-
// optional for now for compatibility with the CLI
91-
string project_name = 3;
90+
string project_name = 3 [(buf.validate.field).string.min_len = 1];
9291
}
9392

9493
message AttestationServiceGetContractResponse {
@@ -104,9 +103,8 @@ message AttestationServiceInitRequest {
104103
int32 contract_revision = 1;
105104
string job_url = 2;
106105
workflowcontract.v1.CraftingSchema.Runner.RunnerType runner = 3;
107-
string workflow_name = 4;
108-
// optional for now for compatibility with the CLI
109-
string project_name = 5;
106+
string workflow_name = 4 [(buf.validate.field).string.min_len = 1];
107+
string project_name = 5 [(buf.validate.field).string.min_len = 1];
110108
}
111109

112110
message AttestationServiceInitResponse {

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

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

app/controlplane/api/gen/jsonschema/controlplane.v1.AttestationServiceGetContractRequest.jsonschema.json

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

app/controlplane/api/gen/jsonschema/controlplane.v1.AttestationServiceGetContractRequest.schema.json

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

app/controlplane/api/gen/jsonschema/controlplane.v1.AttestationServiceInitRequest.jsonschema.json

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

app/controlplane/api/gen/jsonschema/controlplane.v1.AttestationServiceInitRequest.schema.json

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

app/controlplane/internal/dispatcher/dispatcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ func (s *dispatcherTestSuite) SetupTest() {
221221
assert.NoError(s.T(), err)
222222

223223
// Workflow
224-
s.workflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID})
224+
s.workflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID, Project: "test-project"})
225225
assert.NoError(s.T(), err)
226226

227227
// Workflow
228-
s.emptyWorkflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "empty-workflow", OrgID: s.org.ID})
228+
s.emptyWorkflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "empty-workflow", OrgID: s.org.ID, Project: "test-project"})
229229
assert.NoError(s.T(), err)
230230

231231
customImplementation := mockedSDK.NewFanOutPlugin(s.T())

app/controlplane/pkg/biz/casmapping_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ func (s *casMappingIntegrationSuite) SetupTest() {
353353

354354
// Create workflowRun in the database
355355
// Workflow
356-
workflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org1.ID})
356+
workflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org1.ID, Project: "test-project"})
357357
assert.NoError(err)
358358

359-
publicWorkflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow-public", OrgID: s.org1.ID, Public: true})
359+
publicWorkflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow-public", OrgID: s.org1.ID, Public: true, Project: "test-project"})
360360
assert.NoError(err)
361361

362362
// Find contract revision

app/controlplane/pkg/biz/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (s *testSuite) SetupTest() {
278278
assert.NoError(err)
279279

280280
// Workflow
281-
s.workflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID})
281+
s.workflow, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID, Project: "test-project"})
282282
assert.NoError(err)
283283

284284
// Integration configuration

app/controlplane/pkg/biz/organization_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (s *OrgIntegrationTestSuite) SetupTest() {
214214
assert.NoError(err)
215215

216216
// Workflow + contract
217-
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID})
217+
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID, Project: "test-project"})
218218
assert.NoError(err)
219219

220220
// check integration, OCI repository and workflow and contracts are present in the db

app/controlplane/pkg/biz/referrer_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,9 @@ func (s *referrerIntegrationTestSuite) SetupTest() {
402402
s.org2UUID, err = uuid.Parse(s.org2.ID)
403403
require.NoError(s.T(), err)
404404

405-
s.workflow1, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "wf", Team: "team", OrgID: s.org1.ID})
405+
s.workflow1, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "wf", Team: "team", OrgID: s.org1.ID, Project: "test"})
406406
require.NoError(s.T(), err)
407-
s.workflow2, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "wf-from-org-2", Team: "team", OrgID: s.org2.ID})
407+
s.workflow2, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "wf-from-org-2", Team: "team", OrgID: s.org2.ID, Project: "test"})
408408
require.NoError(s.T(), err)
409409

410410
// user 1 has access to org 1 and 2

app/controlplane/pkg/biz/robotaccount_integration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ func (s *robotAccountTestSuite) SetupTest() {
7474
s.NoError(err)
7575

7676
wf, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{
77-
Name: "myworkflow",
78-
OrgID: s.org.ID,
77+
Name: "myworkflow",
78+
Project: "myproject",
79+
OrgID: s.org.ID,
7980
})
8081
s.NoError(err)
8182
s.ra, err = s.RobotAccount.Create(ctx, "myRobotAccount", s.org.ID, wf.ID.String())

app/controlplane/pkg/biz/workflow.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func NewWorkflowUsecase(wfr WorkflowRepo, schemaUC *WorkflowContractUseCase, log
8686
func (uc *WorkflowUseCase) Create(ctx context.Context, opts *WorkflowCreateOpts) (*Workflow, error) {
8787
if opts.Name == "" {
8888
return nil, errors.New("workflow name is required")
89+
} else if opts.Project == "" {
90+
return nil, errors.New("project name is required")
8991
}
9092

9193
// validate format of the name and the project
@@ -118,8 +120,7 @@ func (uc *WorkflowUseCase) Create(ctx context.Context, opts *WorkflowCreateOpts)
118120
wf, err := uc.wfRepo.Create(ctx, opts)
119121
if err != nil {
120122
if IsErrAlreadyExists(err) {
121-
// mask the error but return a custom error
122-
return nil, NewErrAlreadyExistsStr("name already taken")
123+
return nil, NewErrAlreadyExistsStr(fmt.Sprintf("workflow %q in project %q already exists", opts.Name, opts.Project))
123124
}
124125

125126
return nil, fmt.Errorf("failed to create workflow: %w", err)
@@ -238,9 +239,10 @@ func (uc *WorkflowUseCase) FindByNameInOrg(ctx context.Context, orgID, projectNa
238239

239240
if workflowName == "" {
240241
return nil, NewErrValidationStr("empty workflow name")
242+
} else if projectName == "" {
243+
return nil, NewErrValidationStr("empty project name")
241244
}
242245

243-
// TODO: enforce projectName
244246
wf, err := uc.wfRepo.GetOrgScopedByProjectAndName(ctx, orgUUID, projectName, workflowName)
245247
if err != nil {
246248
return nil, fmt.Errorf("failed to get workflow: %w", err)

app/controlplane/pkg/biz/workflow_integration_test.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,59 @@ func (s *workflowIntegrationTestSuite) TestCreateDuplicatedName() {
8989
ctx := context.Background()
9090

9191
const workflowName = "name"
92-
existingWorkflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName})
92+
existingWorkflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName, Project: "project"})
9393
require.NoError(s.T(), err)
9494

95-
s.Run("can't create a workflow with the same name", func() {
96-
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName})
97-
s.ErrorContains(err, "name already taken")
95+
s.Run("can't create a workflow with the same name in the same project", func() {
96+
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName, Project: "project"})
97+
s.ErrorContains(err, "already exists")
98+
})
99+
100+
s.Run("but can do it in another project", func() {
101+
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName, Project: "another-project"})
102+
s.NoError(err)
98103
})
99104

100105
s.Run("but if we delete it we can", func() {
101106
err = s.Workflow.Delete(ctx, s.org.ID, existingWorkflow.ID.String())
102107
require.NoError(s.T(), err)
103108

104-
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName})
109+
_, err = s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: workflowName, Project: "project"})
105110
require.NoError(s.T(), err)
106111
})
107112
}
108113

109114
func (s *workflowIntegrationTestSuite) TestCreate() {
110115
ctx := context.Background()
116+
const project = "project"
111117
testCases := []struct {
112118
name string
113119
opts *biz.WorkflowCreateOpts
114120
wantErrMsg string
115121
}{
116122
{
117123
name: "org missing",
118-
opts: &biz.WorkflowCreateOpts{Name: "name"},
124+
opts: &biz.WorkflowCreateOpts{Name: "name", Project: project},
119125
wantErrMsg: "required",
120126
},
121127
{
122128
name: "name missing",
123-
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID},
129+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Project: project},
130+
wantErrMsg: "required",
131+
},
132+
{
133+
name: "project missing",
134+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "name"},
124135
wantErrMsg: "required",
125136
},
126137
{
127138
name: "invalid name",
128-
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "this/not/valid"},
139+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "this/not/valid", Project: project},
129140
wantErrMsg: "RFC 1123",
130141
},
131142
{
132143
name: "another invalid name",
133-
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "this-not Valid"},
144+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "this-not Valid", Project: project},
134145
wantErrMsg: "RFC 1123",
135146
},
136147
{
@@ -140,11 +151,11 @@ func (s *workflowIntegrationTestSuite) TestCreate() {
140151
},
141152
{
142153
name: "non-existing contract will create it",
143-
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "name", ContractName: uuid.Generate().String()},
154+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "name", ContractName: uuid.Generate().String(), Project: project},
144155
},
145156
{
146-
name: "can create it with just the name and the org",
147-
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "name2"},
157+
name: "can create it with just the name, the project and the org",
158+
opts: &biz.WorkflowCreateOpts{OrgID: s.org.ID, Name: "name2", Project: project},
148159
},
149160
{
150161
name: "with all items",
@@ -184,7 +195,7 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
184195

185196
org2, err := s.Organization.CreateWithRandomName(context.Background())
186197
require.NoError(s.T(), err)
187-
workflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: name, OrgID: s.org.ID})
198+
workflow, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: name, OrgID: s.org.ID, Project: project})
188199
require.NoError(s.T(), err)
189200

190201
// Create two contracts in two different orgs

app/controlplane/pkg/biz/workflowrun_integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,12 @@ func setupWorkflowRunTestData(t *testing.T, suite *testhelpers.TestingUseCases,
337337
assert.NoError(err)
338338

339339
// Workflow
340-
s.workflowOrg1, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID})
340+
s.workflowOrg1, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org.ID, Project: "test-project"})
341341
assert.NoError(err)
342-
s.workflowOrg2, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org2.ID})
342+
s.workflowOrg2, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-workflow", OrgID: s.org2.ID, Project: "test-project"})
343343
assert.NoError(err)
344344
// Public workflow
345-
s.workflowPublicOrg2, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-public-workflow", OrgID: s.org2.ID, Public: true})
345+
s.workflowPublicOrg2, err = suite.Workflow.Create(ctx, &biz.WorkflowCreateOpts{Name: "test-public-workflow", OrgID: s.org2.ID, Public: true, Project: "test-project"})
346346
assert.NoError(err)
347347

348348
// Find contract revision
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Drop index "workflow_name_organization_id" from table: "workflows"
2+
DROP INDEX "workflow_name_organization_id";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "workflows" table
2+
ALTER TABLE "workflows" ALTER COLUMN "project" SET NOT NULL;

app/controlplane/pkg/data/ent/migrate/migrations/atlas.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:tuCJBEVVWbRmd0tPzFCI0VSQ2lKNfjfSCqtsW5SijOA=
1+
h1:39lFYMXkGW9lu7cOjzwNO9fmePg5Y6aTG1el6AD9Mfs=
22
20230706165452_init-schema.sql h1:VvqbNFEQnCvUVyj2iDYVQQxDM0+sSXqocpt/5H64k8M=
33
20230710111950-cas-backend.sql h1:A8iBuSzZIEbdsv9ipBtscZQuaBp3V5/VMw7eZH6GX+g=
44
20230712094107-cas-backends-workflow-runs.sql h1:a5rzxpVGyd56nLRSsKrmCFc9sebg65RWzLghKHh5xvI=
@@ -45,3 +45,5 @@ h1:tuCJBEVVWbRmd0tPzFCI0VSQ2lKNfjfSCqtsW5SijOA=
4545
20240920193509.sql h1:FREEMo5oD//cf+6iq72YgbVJgXN/fuy7DB/jvx6NgKM=
4646
20241011075107.sql h1:8UrMNKA0QAKkhfdetnNCZfI7DBOTKLLxsqqH+c8VhdU=
4747
20241016094434.sql h1:mSu1nkNggqnH5wZzjYhY4wo2m8NA+mEL5QOtPXc/Lqg=
48+
20241019142805.sql h1:j4rd1ktedNnB5WDjL7lWmBhpFsNhT90/G4hzxkvu2D4=
49+
20241019145455.sql h1:8Moysfwh/gJ/4Me5ujndurNFIbqFtKbNBTSAZq+S9EM=

app/controlplane/pkg/data/ent/migrate/schema.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ var (
332332
WorkflowsColumns = []*schema.Column{
333333
{Name: "id", Type: field.TypeUUID, Unique: true},
334334
{Name: "name", Type: field.TypeString},
335-
{Name: "project", Type: field.TypeString, Nullable: true},
335+
{Name: "project", Type: field.TypeString},
336336
{Name: "team", Type: field.TypeString, Nullable: true},
337337
{Name: "runs_count", Type: field.TypeInt, Default: 0},
338338
{Name: "created_at", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP"},
@@ -370,14 +370,6 @@ var (
370370
Where: "deleted_at IS NULL",
371371
},
372372
},
373-
{
374-
Name: "workflow_name_organization_id",
375-
Unique: true,
376-
Columns: []*schema.Column{WorkflowsColumns[1], WorkflowsColumns[9]},
377-
Annotation: &entsql.IndexAnnotation{
378-
Where: "deleted_at IS NULL",
379-
},
380-
},
381373
{
382374
Name: "workflow_organization_id_id",
383375
Unique: true,

0 commit comments

Comments
 (0)