| 30 | ) |
| 31 | |
| 32 | func (s *sqlDatabase) CreateRepository(ctx context.Context, owner, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType, agentMode bool) (param params.Repository, err error) { |
| 33 | defer func() { |
| 34 | if err == nil { |
| 35 | s.sendNotify(common.RepositoryEntityType, common.CreateOperation, param) |
| 36 | } |
| 37 | }() |
| 38 | |
| 39 | if webhookSecret == "" { |
| 40 | return params.Repository{}, errors.New("creating repo: missing secret") |
| 41 | } |
| 42 | secret, err := util.Seal([]byte(webhookSecret), []byte(s.cfg.Passphrase)) |
| 43 | if err != nil { |
| 44 | return params.Repository{}, fmt.Errorf("failed to encrypt string") |
| 45 | } |
| 46 | |
| 47 | newRepo := Repository{ |
| 48 | Name: name, |
| 49 | Owner: owner, |
| 50 | WebhookSecret: secret, |
| 51 | PoolBalancerType: poolBalancerType, |
| 52 | AgentMode: agentMode, |
| 53 | } |
| 54 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 55 | switch credentials.ForgeType { |
| 56 | case params.GithubEndpointType: |
| 57 | newRepo.CredentialsID = &credentials.ID |
| 58 | case params.GiteaEndpointType: |
| 59 | newRepo.GiteaCredentialsID = &credentials.ID |
| 60 | default: |
| 61 | return runnerErrors.NewBadRequestError("unsupported credentials type") |
| 62 | } |
| 63 | |
| 64 | newRepo.EndpointName = &credentials.Endpoint.Name |
| 65 | q := tx.Create(&newRepo) |
| 66 | if q.Error != nil { |
| 67 | return fmt.Errorf("error creating repository: %w", q.Error) |
| 68 | } |
| 69 | return nil |
| 70 | }) |
| 71 | if err != nil { |
| 72 | return params.Repository{}, fmt.Errorf("error creating repository: %w", err) |
| 73 | } |
| 74 | |
| 75 | ret, err := s.GetRepositoryByID(ctx, newRepo.ID.String()) |
| 76 | if err != nil { |
| 77 | return params.Repository{}, fmt.Errorf("error creating repository: %w", err) |
| 78 | } |
| 79 | |
| 80 | return ret, nil |
| 81 | } |
| 82 | |
| 83 | func (s *sqlDatabase) GetRepository(ctx context.Context, owner, name, endpointName string) (params.Repository, error) { |
| 84 | repo, err := s.getRepo(ctx, owner, name, endpointName) |