MCPcopy Create free account
hub / github.com/cloudbase/garm / UpdateGithubCredentials

Method UpdateGithubCredentials

database/sql/github.go:401–485  ·  view source on GitHub ↗
(ctx context.Context, id uint, param params.UpdateGithubCredentialsParams)

Source from the content-addressed store, hash-verified

399}
400
401func (s *sqlDatabase) UpdateGithubCredentials(ctx context.Context, id uint, param params.UpdateGithubCredentialsParams) (ghCreds params.ForgeCredentials, err error) {
402 var rowsAffected int64
403 defer func() {
404 if err == nil && rowsAffected > 0 {
405 s.sendNotify(common.GithubCredentialsEntityType, common.UpdateOperation, ghCreds)
406 }
407 }()
408 var creds GithubCredentials
409 err = s.conn.Transaction(func(tx *gorm.DB) error {
410 q := tx.Preload("Endpoint")
411 if !auth.IsAdmin(ctx) {
412 userID, err := getUIDFromContext(ctx)
413 if err != nil {
414 return fmt.Errorf("error updating github credentials: %w", err)
415 }
416 q = q.Where("user_id = ?", userID)
417 }
418
419 if err := q.Where("id = ?", id).First(&creds).Error; err != nil {
420 if errors.Is(err, gorm.ErrRecordNotFound) {
421 return fmt.Errorf("github credentials not found: %w", runnerErrors.ErrNotFound)
422 }
423 return fmt.Errorf("error fetching github credentials: %w", err)
424 }
425
426 updates := make(map[string]interface{})
427
428 if param.Name != nil && *param.Name != creds.Name {
429 updates["name"] = *param.Name
430 }
431 if param.Description != nil && *param.Description != creds.Description {
432 updates["description"] = *param.Description
433 }
434
435 var data []byte
436 var err error
437 switch creds.AuthType {
438 case params.ForgeAuthTypePAT:
439 if param.PAT != nil {
440 data, err = s.marshalAndSeal(param.PAT)
441 }
442
443 if param.App != nil {
444 return fmt.Errorf("cannot update app credentials for PAT: %w", runnerErrors.ErrBadRequest)
445 }
446 case params.ForgeAuthTypeApp:
447 if param.App != nil {
448 data, err = s.marshalAndSeal(param.App)
449 }
450
451 if param.PAT != nil {
452 return fmt.Errorf("cannot update PAT credentials for app: %w", runnerErrors.ErrBadRequest)
453 }
454 default:
455 // This should never happen, unless there was a bug in the DB migration code,
456 // or the DB was manually modified.
457 return fmt.Errorf("invalid auth type: %w", runnerErrors.ErrBadRequest)
458 }

Callers

nothing calls this directly

Calls 5

sendNotifyMethod · 0.95
marshalAndSealMethod · 0.95
IsAdminFunction · 0.92
getUIDFromContextFunction · 0.85

Tested by

no test coverage detected