TODO: The functions in this file are not truly fault tolerant. They should be refactored to run as idempotent, retryable background tasks. CreateProject creates a new project and provisions and reconciles a prod deployment for it.
(ctx context.Context, org *database.Organization, opts *database.InsertProjectOptions, deploy bool)
| 22 | |
| 23 | // CreateProject creates a new project and provisions and reconciles a prod deployment for it. |
| 24 | func (s *Service) CreateProject(ctx context.Context, org *database.Organization, opts *database.InsertProjectOptions, deploy bool) (*database.Project, error) { |
| 25 | // Get roles for initial setup |
| 26 | adminRole, err := s.DB.FindProjectRole(ctx, database.ProjectRoleNameAdmin) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | // Get the autogroup:members group |
| 32 | allMembers, err := s.DB.FindUsergroupByName(ctx, org.Name, database.UsergroupNameAutogroupMembers) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | // Create the project and add initial members using a transaction. |
| 38 | // The transaction is not used for provisioning and deployments, since they involve external services. |
| 39 | txCtx, tx, err := s.DB.NewTx(ctx, false) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | defer func() { _ = tx.Rollback() }() |
| 44 | |
| 45 | proj, err := s.DB.InsertProject(txCtx, opts) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | // The creating user becomes project admin |
| 51 | if opts.CreatedByUserID != nil { |
| 52 | err = s.InsertProjectMemberUser(txCtx, org.ID, proj.ID, *opts.CreatedByUserID, adminRole.ID, nil, false, nil) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Add the system-managed autogroup:members group to the project with the org.DefaultProjectRoleID role (if configured) |
| 59 | if org.DefaultProjectRoleID != nil { |
| 60 | err = s.DB.InsertProjectMemberUsergroup(txCtx, allMembers.ID, proj.ID, *org.DefaultProjectRoleID, false, nil) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | err = tx.Commit() |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | var createdByID, createdByEmail string |
| 72 | if opts.CreatedByUserID != nil { |
| 73 | user, err := s.DB.FindUser(ctx, *proj.CreatedByUserID) |
| 74 | if err == nil { |
| 75 | createdByID = user.ID |
| 76 | createdByEmail = user.Email |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | s.Logger.Info("created project", zap.String("id", proj.ID), zap.String("name", proj.Name), zap.String("org", org.Name), zap.String("user_id", createdByID), zap.String("user_email", createdByEmail)) |
| 81 |
nothing calls this directly
no test coverage detected