Skip to content

Commit 46f1bbf

Browse files
authored
fix(workflow): respect contract name from schema v2 (#2740)
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
1 parent 7afe3d7 commit 46f1bbf

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

app/controlplane/pkg/data/workflow.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,64 @@ func (r *WorkflowRepo) Create(ctx context.Context, opts *biz.WorkflowCreateOpts)
110110
}
111111

112112
// We do not have an explicit contract
113-
// 1 - try to find it with the default name
114-
// 2 - if not found, create it with the default name
115113
if contractUUID == uuid.Nil {
116-
defaultContractName := fmt.Sprintf("%s-%s", opts.Project, opts.Name)
117-
// Try to find the one with the default name or create it
118-
contract, err := contractInOrg(ctx, r.data.DB, orgUUID, nil, &defaultContractName)
119-
if err != nil {
120-
if ent.IsNotFound(err) {
121-
// Check if contract creation is restricted to org admins
122-
if opts.OrgRestrictContractCreationToAdmins && !opts.UserIsOrgAdmin {
123-
return biz.NewErrUnauthorizedStr("contract creation is restricted to organization administrators. Please use existing contracts or contact your administrator")
124-
}
114+
var contract *ent.WorkflowContract
115+
116+
// Try contract schema v2
117+
var metadataContractName string
118+
if opts.ContractBytes != nil && opts.DetectedContract != nil && opts.DetectedContract.Schemav2 != nil {
119+
metadataContractName = opts.DetectedContract.Schemav2.GetMetadata().GetName()
120+
}
121+
122+
if metadataContractName != "" {
123+
// Contract must not exist with the same name in the org, otherwise we might be linking to an existing contract that doesn't match the detected one
124+
_, err := contractInOrg(ctx, r.data.DB, orgUUID, nil, &metadataContractName)
125+
if err == nil {
126+
return biz.NewErrAlreadyExistsStr(fmt.Sprintf("contract %q already exists", metadataContractName))
127+
}
128+
if !ent.IsNotFound(err) {
129+
return fmt.Errorf("failed to check contract: %w", err)
130+
}
125131

126-
// Create a new contract associated with the workflow
127-
contract, _, err = r.contractRepo.addCreateToTx(ctx, tx, &biz.ContractCreateOpts{
128-
OrgID: orgUUID,
129-
Name: defaultContractName,
130-
Contract: opts.DetectedContract,
131-
ProjectID: &projectID,
132-
})
133-
if err != nil {
134-
return fmt.Errorf("creating contract: %w", err)
132+
// Check if contract creation is restricted to org admins
133+
if opts.OrgRestrictContractCreationToAdmins && !opts.UserIsOrgAdmin {
134+
return biz.NewErrUnauthorizedStr("contract creation is restricted to organization administrators. Please use existing contracts or contact your administrator")
135+
}
136+
137+
// Create the contract
138+
contract, _, err = r.contractRepo.addCreateToTx(ctx, tx, &biz.ContractCreateOpts{
139+
OrgID: orgUUID,
140+
Name: metadataContractName,
141+
Contract: opts.DetectedContract,
142+
ProjectID: &projectID,
143+
})
144+
if err != nil {
145+
return fmt.Errorf("creating contract: %w", err)
146+
}
147+
} else {
148+
// Try to find the one with the default name or create it
149+
defaultContractName := fmt.Sprintf("%s-%s", opts.Project, opts.Name)
150+
contract, err = contractInOrg(ctx, r.data.DB, orgUUID, nil, &defaultContractName)
151+
if err != nil {
152+
if ent.IsNotFound(err) {
153+
// Check if contract creation is restricted to org admins
154+
if opts.OrgRestrictContractCreationToAdmins && !opts.UserIsOrgAdmin {
155+
return biz.NewErrUnauthorizedStr("contract creation is restricted to organization administrators. Please use existing contracts or contact your administrator")
156+
}
157+
158+
// Create a new contract associated with the workflow
159+
contract, _, err = r.contractRepo.addCreateToTx(ctx, tx, &biz.ContractCreateOpts{
160+
OrgID: orgUUID,
161+
Name: defaultContractName,
162+
Contract: opts.DetectedContract,
163+
ProjectID: &projectID,
164+
})
165+
if err != nil {
166+
return fmt.Errorf("creating contract: %w", err)
167+
}
168+
} else {
169+
return fmt.Errorf("failed to find contract: %w", err)
135170
}
136-
} else {
137-
return fmt.Errorf("failed to find contract: %w", err)
138171
}
139172
}
140173

0 commit comments

Comments
 (0)