(ctx context.Context, opts *biz.WorkflowCreateOpts)
| 55 | } |
| 56 | |
| 57 | func (r *WorkflowRepo) Create(ctx context.Context, opts *biz.WorkflowCreateOpts) (wf *biz.Workflow, err error) { |
| 58 | ctx, span := otelx.Start(ctx, workflowRepoTracer, "WorkflowRepo.Create") |
| 59 | defer span.End() |
| 60 | |
| 61 | orgUUID, err := uuid.Parse(opts.OrgID) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | // You can only provide one of the two |
| 67 | if opts.ContractName != "" && opts.ContractID != "" { |
| 68 | return nil, fmt.Errorf("contract name and ID cannot be provided at the same time") |
| 69 | } |
| 70 | |
| 71 | // Load an existing contract reference if provided |
| 72 | var contractUUID uuid.UUID |
| 73 | if opts.ContractID != "" { |
| 74 | contractUUID, err = uuid.Parse(opts.ContractID) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | } |
| 79 | // If we are expecting a specific contract we check it |
| 80 | if opts.ContractName != "" { |
| 81 | existingContract, err := contractInOrg(ctx, r.data.DB, orgUUID, nil, &opts.ContractName) |
| 82 | if err != nil { |
| 83 | if ent.IsNotFound(err) { |
| 84 | return nil, biz.NewErrNotFound(fmt.Sprintf("contract %q", opts.ContractName)) |
| 85 | } |
| 86 | |
| 87 | return nil, err |
| 88 | } |
| 89 | |
| 90 | contractUUID = existingContract.ID |
| 91 | } |
| 92 | |
| 93 | var entwf *ent.Workflow |
| 94 | // Create project and workflow in a transaction |
| 95 | if err = WithTx(ctx, r.data.DB, func(tx *ent.Tx) error { |
| 96 | // Find or create project. |
| 97 | projectID, err := tx.Project.Create().SetName(opts.Project).SetOrganizationID(orgUUID). |
| 98 | OnConflict( |
| 99 | sql.ConflictColumns(project.FieldName, project.FieldOrganizationID), |
| 100 | // Since we are using a partial index, we need to explicitly craft the upsert query |
| 101 | sql.ConflictWhere(sql.IsNull(project.FieldDeletedAt)), |
| 102 | ).Ignore().ID(ctx) |
| 103 | if err != nil { |
| 104 | return fmt.Errorf("creating project: %w", err) |
| 105 | } |
| 106 | |
| 107 | // Find or create the default project version |
| 108 | if _, err := findProjectVersionWithClient(ctx, tx.Client(), projectID, biz.DefaultVersionName); err != nil { |
| 109 | if !ent.IsNotFound(err) { |
| 110 | return fmt.Errorf("finding project version: %w", err) |
| 111 | } |
| 112 | |
| 113 | if _, err := createProjectVersionWithTx(ctx, tx, projectID, biz.DefaultVersionName, true, nil); err != nil { |
| 114 | return fmt.Errorf("creating project version: %w", err) |
nothing calls this directly
no test coverage detected