(ctx context.Context, param params.CreateGithubCredentialsParams)
| 231 | } |
| 232 | |
| 233 | func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.CreateGithubCredentialsParams) (ghCreds params.ForgeCredentials, err error) { |
| 234 | userID, err := getUIDFromContext(ctx) |
| 235 | if err != nil { |
| 236 | return params.ForgeCredentials{}, fmt.Errorf("error creating github credentials: %w", err) |
| 237 | } |
| 238 | if param.Endpoint == "" { |
| 239 | return params.ForgeCredentials{}, fmt.Errorf("endpoint name is required: %w", runnerErrors.ErrBadRequest) |
| 240 | } |
| 241 | |
| 242 | defer func() { |
| 243 | if err == nil { |
| 244 | s.sendNotify(common.GithubCredentialsEntityType, common.CreateOperation, ghCreds) |
| 245 | } |
| 246 | }() |
| 247 | var creds GithubCredentials |
| 248 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 249 | var endpoint GithubEndpoint |
| 250 | if err := tx.Where("name = ? and endpoint_type = ?", param.Endpoint, params.GithubEndpointType).First(&endpoint).Error; err != nil { |
| 251 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 252 | return fmt.Errorf("github endpoint not found: %w", runnerErrors.ErrNotFound) |
| 253 | } |
| 254 | return fmt.Errorf("error fetching github endpoint: %w", err) |
| 255 | } |
| 256 | |
| 257 | if err := tx.Where("name = ? and user_id = ?", param.Name, userID).First(&creds).Error; err == nil { |
| 258 | return fmt.Errorf("github credentials already exists: %w", runnerErrors.ErrDuplicateEntity) |
| 259 | } |
| 260 | |
| 261 | var data []byte |
| 262 | var err error |
| 263 | switch param.AuthType { |
| 264 | case params.ForgeAuthTypePAT: |
| 265 | data, err = s.marshalAndSeal(param.PAT) |
| 266 | case params.ForgeAuthTypeApp: |
| 267 | data, err = s.marshalAndSeal(param.App) |
| 268 | default: |
| 269 | return fmt.Errorf("invalid auth type: %w", runnerErrors.ErrBadRequest) |
| 270 | } |
| 271 | if err != nil { |
| 272 | return fmt.Errorf("error marshaling and sealing credentials: %w", err) |
| 273 | } |
| 274 | |
| 275 | creds = GithubCredentials{ |
| 276 | Name: param.Name, |
| 277 | Description: param.Description, |
| 278 | EndpointName: &endpoint.Name, |
| 279 | AuthType: param.AuthType, |
| 280 | Payload: data, |
| 281 | UserID: &userID, |
| 282 | } |
| 283 | |
| 284 | if err := tx.Create(&creds).Error; err != nil { |
| 285 | return fmt.Errorf("error creating github credentials: %w", err) |
| 286 | } |
| 287 | // Skip making an extra query. |
| 288 | creds.Endpoint = endpoint |
| 289 | |
| 290 | return nil |
no test coverage detected