Skip to content

Commit 2eb8cb5

Browse files
Fix update credentials
We forgot to account for gitea credentials when updating an entity. This change makes sure we look for credentials in the appropriate table. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 6b8c395 commit 2eb8cb5

File tree

12 files changed

+258
-94
lines changed

12 files changed

+258
-94
lines changed

database/sql/enterprise.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
174174
}
175175
}()
176176
var enterprise Enterprise
177-
var creds GithubCredentials
178177
err = s.conn.Transaction(func(tx *gorm.DB) error {
179178
var err error
180-
enterprise, err = s.getEnterpriseByID(ctx, tx, enterpriseID)
179+
enterprise, err = s.getEnterpriseByID(ctx, tx, enterpriseID, "Endpoint")
181180
if err != nil {
182181
return fmt.Errorf("error fetching enterprise: %w", err)
183182
}
@@ -186,19 +185,8 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
186185
return fmt.Errorf("error enterprise has no endpoint: %w", runnerErrors.ErrUnprocessable)
187186
}
188187

189-
if param.CredentialsName != "" {
190-
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
191-
if err != nil {
192-
return fmt.Errorf("error fetching credentials: %w", err)
193-
}
194-
if creds.EndpointName == nil {
195-
return fmt.Errorf("error credentials have no endpoint: %w", runnerErrors.ErrUnprocessable)
196-
}
197-
198-
if *creds.EndpointName != *enterprise.EndpointName {
199-
return fmt.Errorf("error endpoint mismatch: %w", runnerErrors.ErrBadRequest)
200-
}
201-
enterprise.CredentialsID = &creds.ID
188+
if err := s.updateEntityCredentials(ctx, tx, &enterprise, param.CredentialsName); err != nil {
189+
return err
202190
}
203191
if param.WebhookSecret != "" {
204192
secret, err := util.Seal([]byte(param.WebhookSecret), []byte(s.cfg.Passphrase))
@@ -216,7 +204,7 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
216204
enterprise.AgentMode = *param.AgentMode
217205
}
218206

219-
q := tx.Save(&enterprise)
207+
q := tx.Model(&enterprise).Omit("Endpoint").Save(&enterprise)
220208
if q.Error != nil {
221209
return fmt.Errorf("error saving enterprise: %w", q.Error)
222210
}

database/sql/enterprise_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,22 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBEncryptErr() {
419419
WithArgs(s.Fixtures.Enterprises[0].ID, 1).
420420
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
421421
AddRow(s.Fixtures.Enterprises[0].ID, s.Fixtures.Enterprises[0].Endpoint.Name))
422+
// Expect Endpoint preload for enterprise
423+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
424+
WithArgs(s.Fixtures.Enterprises[0].Endpoint.Name).
425+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
426+
AddRow(s.Fixtures.Enterprises[0].Endpoint.Name, s.Fixtures.Enterprises[0].Endpoint.EndpointType))
427+
// Expect credentials fetch
422428
s.Fixtures.SQLMock.
423429
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
424430
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
425431
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
426432
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
433+
// Expect Endpoint preload for credentials
427434
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
428-
WithArgs(s.testCreds.Endpoint.Name).
429-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
430-
AddRow(s.secondaryTestCreds.Endpoint.Name))
435+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
436+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
437+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
431438
s.Fixtures.SQLMock.ExpectRollback()
432439

433440
_, err := s.StoreSQLMocked.UpdateEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID, s.Fixtures.UpdateRepoParams)
@@ -444,15 +451,22 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBSaveErr() {
444451
WithArgs(s.Fixtures.Enterprises[0].ID, 1).
445452
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
446453
AddRow(s.Fixtures.Enterprises[0].ID, s.Fixtures.Enterprises[0].Endpoint.Name))
454+
// Expect Endpoint preload for enterprise
455+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
456+
WithArgs(s.Fixtures.Enterprises[0].Endpoint.Name).
457+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
458+
AddRow(s.Fixtures.Enterprises[0].Endpoint.Name, s.Fixtures.Enterprises[0].Endpoint.EndpointType))
459+
// Expect credentials fetch
447460
s.Fixtures.SQLMock.
448461
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
449462
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
450463
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
451464
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
465+
// Expect Endpoint preload for credentials
452466
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
453-
WithArgs(s.testCreds.Endpoint.Name).
454-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
455-
AddRow(s.secondaryTestCreds.Endpoint.Name))
467+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
468+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
469+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
456470
s.Fixtures.SQLMock.
457471
ExpectExec(("UPDATE `enterprises` SET")).
458472
WillReturnError(fmt.Errorf("saving enterprise mock error"))
@@ -475,15 +489,22 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBDecryptingErr() {
475489
WithArgs(s.Fixtures.Enterprises[0].ID, 1).
476490
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
477491
AddRow(s.Fixtures.Enterprises[0].ID, s.Fixtures.Enterprises[0].Endpoint.Name))
492+
// Expect Endpoint preload for enterprise
493+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
494+
WithArgs(s.Fixtures.Enterprises[0].Endpoint.Name).
495+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
496+
AddRow(s.Fixtures.Enterprises[0].Endpoint.Name, s.Fixtures.Enterprises[0].Endpoint.EndpointType))
497+
// Expect credentials fetch
478498
s.Fixtures.SQLMock.
479499
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
480500
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
481501
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
482502
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
503+
// Expect Endpoint preload for credentials
483504
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
484-
WithArgs(s.testCreds.Endpoint.Name).
485-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
486-
AddRow(s.secondaryTestCreds.Endpoint.Name))
505+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
506+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
507+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
487508
s.Fixtures.SQLMock.ExpectRollback()
488509

489510
_, err := s.StoreSQLMocked.UpdateEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID, s.Fixtures.UpdateRepoParams)

database/sql/models.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"gorm.io/datatypes"
2323
"gorm.io/gorm"
2424

25+
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
2526
commonParams "github.com/cloudbase/garm-provider-common/params"
2627
"github.com/cloudbase/garm/params"
2728
)
@@ -483,6 +484,14 @@ type GithubCredentials struct {
483484
Enterprises []Enterprise `gorm:"foreignKey:CredentialsID"`
484485
}
485486

487+
func (g GithubCredentials) GetEndpointName() *string {
488+
return g.EndpointName
489+
}
490+
491+
func (g GithubCredentials) GetID() uint {
492+
return g.ID
493+
}
494+
486495
type GiteaCredentials struct {
487496
gorm.Model
488497

@@ -501,6 +510,80 @@ type GiteaCredentials struct {
501510
Organizations []Organization `gorm:"foreignKey:GiteaCredentialsID"`
502511
}
503512

513+
func (g GiteaCredentials) GetEndpointName() *string {
514+
return g.EndpointName
515+
}
516+
517+
func (g GiteaCredentials) GetID() uint {
518+
return g.ID
519+
}
520+
521+
// ForgeEntity represents an entity (Repository, Organization, Enterprise) that can have forge credentials
522+
type ForgeEntity interface {
523+
GetEndpoint() GithubEndpoint
524+
GetEndpointName() *string
525+
SetCredentials(id uint) error
526+
}
527+
528+
// Repository implements ForgeEntity
529+
func (r *Repository) GetEndpoint() GithubEndpoint {
530+
return r.Endpoint
531+
}
532+
533+
func (r *Repository) GetEndpointName() *string {
534+
return r.EndpointName
535+
}
536+
537+
func (r *Repository) SetCredentials(id uint) error {
538+
switch r.Endpoint.EndpointType {
539+
case params.GithubEndpointType:
540+
r.CredentialsID = &id
541+
case params.GiteaEndpointType:
542+
r.GiteaCredentialsID = &id
543+
default:
544+
return runnerErrors.NewBadRequestError("unsupported endpoint type: %s", r.Endpoint.EndpointType)
545+
}
546+
return nil
547+
}
548+
549+
// Organization implements ForgeEntity
550+
func (o *Organization) GetEndpoint() GithubEndpoint {
551+
return o.Endpoint
552+
}
553+
554+
func (o *Organization) GetEndpointName() *string {
555+
return o.EndpointName
556+
}
557+
558+
func (o *Organization) SetCredentials(id uint) error {
559+
switch o.Endpoint.EndpointType {
560+
case params.GithubEndpointType:
561+
o.CredentialsID = &id
562+
case params.GiteaEndpointType:
563+
o.GiteaCredentialsID = &id
564+
default:
565+
return runnerErrors.NewBadRequestError("unsupported endpoint type: %s", o.Endpoint.EndpointType)
566+
}
567+
return nil
568+
}
569+
570+
// Enterprise implements ForgeEntity
571+
func (e *Enterprise) GetEndpoint() GithubEndpoint {
572+
return e.Endpoint
573+
}
574+
575+
func (e *Enterprise) GetEndpointName() *string {
576+
return e.EndpointName
577+
}
578+
579+
func (e *Enterprise) SetCredentials(id uint) error {
580+
if e.Endpoint.EndpointType != params.GithubEndpointType {
581+
return runnerErrors.NewBadRequestError("enterprise only supports GitHub credentials")
582+
}
583+
e.CredentialsID = &id
584+
return nil
585+
}
586+
504587
// FileObject represents the table that holds files. This can be used to store
505588
// GARM agent binaries, runner binary downloads that may be cached, etc.
506589
type FileObject struct {

database/sql/organizations.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,30 +158,18 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
158158
}
159159
}()
160160
var org Organization
161-
var creds GithubCredentials
162161
err = s.conn.Transaction(func(tx *gorm.DB) error {
163162
var err error
164-
org, err = s.getOrgByID(ctx, tx, orgID)
163+
org, err = s.getOrgByID(ctx, tx, orgID, "Endpoint")
165164
if err != nil {
166165
return fmt.Errorf("error fetching org: %w", err)
167166
}
168167
if org.EndpointName == nil {
169168
return fmt.Errorf("error org has no endpoint: %w", runnerErrors.ErrUnprocessable)
170169
}
171170

172-
if param.CredentialsName != "" {
173-
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
174-
if err != nil {
175-
return fmt.Errorf("error fetching credentials: %w", err)
176-
}
177-
if creds.EndpointName == nil {
178-
return fmt.Errorf("error credentials have no endpoint: %w", runnerErrors.ErrUnprocessable)
179-
}
180-
181-
if *creds.EndpointName != *org.EndpointName {
182-
return fmt.Errorf("error endpoint mismatch: %w", runnerErrors.ErrBadRequest)
183-
}
184-
org.CredentialsID = &creds.ID
171+
if err := s.updateEntityCredentials(ctx, tx, &org, param.CredentialsName); err != nil {
172+
return err
185173
}
186174

187175
if param.WebhookSecret != "" {
@@ -200,7 +188,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
200188
org.AgentMode = *param.AgentMode
201189
}
202190

203-
q := tx.Save(&org)
191+
q := tx.Model(&org).Omit("Endpoint").Save(&org)
204192
if q.Error != nil {
205193
return fmt.Errorf("error saving org: %w", q.Error)
206194
}

database/sql/organizations_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,22 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBEncryptErr() {
485485
WithArgs(s.Fixtures.Orgs[0].ID, 1).
486486
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
487487
AddRow(s.Fixtures.Orgs[0].ID, s.Fixtures.Orgs[0].Endpoint.Name))
488+
// Expect Endpoint preload for organization
489+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
490+
WithArgs(s.Fixtures.Orgs[0].Endpoint.Name).
491+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
492+
AddRow(s.Fixtures.Orgs[0].Endpoint.Name, s.Fixtures.Orgs[0].Endpoint.EndpointType))
493+
// Expect credentials fetch
488494
s.Fixtures.SQLMock.
489495
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
490496
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
491497
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
492498
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
499+
// Expect Endpoint preload for credentials
493500
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
494-
WithArgs(s.testCreds.Endpoint.Name).
495-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
496-
AddRow(s.secondaryTestCreds.Endpoint.Name))
501+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
502+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
503+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
497504
s.Fixtures.SQLMock.ExpectRollback()
498505

499506
_, err := s.StoreSQLMocked.UpdateOrganization(s.adminCtx, s.Fixtures.Orgs[0].ID, s.Fixtures.UpdateRepoParams)
@@ -510,15 +517,22 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBSaveErr() {
510517
WithArgs(s.Fixtures.Orgs[0].ID, 1).
511518
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
512519
AddRow(s.Fixtures.Orgs[0].ID, s.Fixtures.Orgs[0].Endpoint.Name))
520+
// Expect Endpoint preload for organization
521+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
522+
WithArgs(s.Fixtures.Orgs[0].Endpoint.Name).
523+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
524+
AddRow(s.Fixtures.Orgs[0].Endpoint.Name, s.Fixtures.Orgs[0].Endpoint.EndpointType))
525+
// Expect credentials fetch
513526
s.Fixtures.SQLMock.
514527
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
515528
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
516529
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
517530
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
531+
// Expect Endpoint preload for credentials
518532
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
519-
WithArgs(s.testCreds.Endpoint.Name).
520-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
521-
AddRow(s.secondaryTestCreds.Endpoint.Name))
533+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
534+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
535+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
522536
s.Fixtures.SQLMock.
523537
ExpectExec(("UPDATE `organizations` SET")).
524538
WillReturnError(fmt.Errorf("saving org mock error"))
@@ -539,17 +553,24 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBDecryptingErr() {
539553
s.Fixtures.SQLMock.
540554
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `organizations` WHERE id = ? AND `organizations`.`deleted_at` IS NULL ORDER BY `organizations`.`id` LIMIT ?")).
541555
WithArgs(s.Fixtures.Orgs[0].ID, 1).
542-
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
543-
AddRow(s.Fixtures.Orgs[0].ID, s.Fixtures.Orgs[0].Endpoint.Name))
556+
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name", "webhook_secret"}).
557+
AddRow(s.Fixtures.Orgs[0].ID, s.Fixtures.Orgs[0].Endpoint.Name, s.Fixtures.Orgs[0].WebhookSecret))
558+
// Expect Endpoint preload for organization
559+
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
560+
WithArgs(s.Fixtures.Orgs[0].Endpoint.Name).
561+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
562+
AddRow(s.Fixtures.Orgs[0].Endpoint.Name, s.Fixtures.Orgs[0].Endpoint.EndpointType))
563+
// Expect credentials fetch
544564
s.Fixtures.SQLMock.
545565
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
546566
WithArgs(s.adminUserID, s.secondaryTestCreds.Name, 1).
547567
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
548568
AddRow(s.secondaryTestCreds.ID, s.secondaryTestCreds.Endpoint.Name))
569+
// Expect Endpoint preload for credentials
549570
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
550-
WithArgs(s.testCreds.Endpoint.Name).
551-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
552-
AddRow(s.secondaryTestCreds.Endpoint.Name))
571+
WithArgs(s.secondaryTestCreds.Endpoint.Name).
572+
WillReturnRows(sqlmock.NewRows([]string{"name", "endpoint_type"}).
573+
AddRow(s.secondaryTestCreds.Endpoint.Name, s.secondaryTestCreds.Endpoint.EndpointType))
553574
s.Fixtures.SQLMock.ExpectRollback()
554575

555576
_, err := s.StoreSQLMocked.UpdateOrganization(s.adminCtx, s.Fixtures.Orgs[0].ID, s.Fixtures.UpdateRepoParams)

database/sql/repositories.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,30 +160,18 @@ func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param
160160
}
161161
}()
162162
var repo Repository
163-
var creds GithubCredentials
164163
err = s.conn.Transaction(func(tx *gorm.DB) error {
165164
var err error
166-
repo, err = s.getRepoByID(ctx, tx, repoID)
165+
repo, err = s.getRepoByID(ctx, tx, repoID, "Endpoint")
167166
if err != nil {
168167
return fmt.Errorf("error fetching repo: %w", err)
169168
}
170169
if repo.EndpointName == nil {
171170
return runnerErrors.NewUnprocessableError("repository has no endpoint")
172171
}
173172

174-
if param.CredentialsName != "" {
175-
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
176-
if err != nil {
177-
return fmt.Errorf("error fetching credentials: %w", err)
178-
}
179-
if creds.EndpointName == nil {
180-
return runnerErrors.NewUnprocessableError("credentials have no endpoint")
181-
}
182-
183-
if *creds.EndpointName != *repo.EndpointName {
184-
return runnerErrors.NewBadRequestError("endpoint mismatch")
185-
}
186-
repo.CredentialsID = &creds.ID
173+
if err := s.updateEntityCredentials(ctx, tx, &repo, param.CredentialsName); err != nil {
174+
return err
187175
}
188176

189177
if param.WebhookSecret != "" {
@@ -201,7 +189,7 @@ func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param
201189
repo.AgentMode = *param.AgentMode
202190
}
203191

204-
q := tx.Save(&repo)
192+
q := tx.Model(&repo).Omit("Endpoint").Save(&repo)
205193
if q.Error != nil {
206194
return fmt.Errorf("error saving repo: %w", q.Error)
207195
}

0 commit comments

Comments
 (0)