diff --git a/internal/repositories/github/service.go b/internal/repositories/github/service.go index 4cc92525e3..e56d68a223 100644 --- a/internal/repositories/github/service.go +++ b/internal/repositories/github/service.go @@ -360,49 +360,68 @@ func (r *repositoryService) persistRepository( projectID uuid.UUID, provider *db.Provider, ) (uuid.UUID, *pb.Repository, error) { - // instantiate the response object - pbRepo := &pb.Repository{ - Name: githubRepo.GetName(), - Owner: githubRepo.GetOwner().GetLogin(), - RepoId: githubRepo.GetID(), - HookId: githubHook.GetID(), - HookUrl: githubHook.GetURL(), - DeployUrl: githubRepo.GetDeploymentsURL(), - CloneUrl: githubRepo.GetCloneURL(), - HookType: githubHook.GetType(), - HookName: githubHook.GetName(), - HookUuid: hookUUID, - IsPrivate: githubRepo.GetPrivate(), - IsFork: githubRepo.GetFork(), - DefaultBranch: githubRepo.GetDefaultBranch(), - } + var outid uuid.UUID + pbr, err := db.WithTransaction(r.store, func(t db.ExtendQuerier) (*pb.Repository, error) { + // instantiate the response object + pbRepo := &pb.Repository{ + Name: githubRepo.GetName(), + Owner: githubRepo.GetOwner().GetLogin(), + RepoId: githubRepo.GetID(), + HookId: githubHook.GetID(), + HookUrl: githubHook.GetURL(), + DeployUrl: githubRepo.GetDeploymentsURL(), + CloneUrl: githubRepo.GetCloneURL(), + HookType: githubHook.GetType(), + HookName: githubHook.GetName(), + HookUuid: hookUUID, + IsPrivate: githubRepo.GetPrivate(), + IsFork: githubRepo.GetFork(), + DefaultBranch: githubRepo.GetDefaultBranch(), + } - // update the database - dbRepo, err := r.store.CreateRepository(ctx, db.CreateRepositoryParams{ - Provider: provider.Name, - ProviderID: provider.ID, - ProjectID: projectID, - RepoOwner: pbRepo.Owner, - RepoName: pbRepo.Name, - RepoID: pbRepo.RepoId, - IsPrivate: pbRepo.IsPrivate, - IsFork: pbRepo.IsFork, - WebhookID: sql.NullInt64{ - Int64: pbRepo.HookId, - Valid: true, - }, - CloneUrl: pbRepo.CloneUrl, - WebhookUrl: pbRepo.HookUrl, - DeployUrl: pbRepo.DeployUrl, - DefaultBranch: sql.NullString{ - String: pbRepo.DefaultBranch, - Valid: true, - }, + // update the database + dbRepo, err := t.CreateRepository(ctx, db.CreateRepositoryParams{ + Provider: provider.Name, + ProviderID: provider.ID, + ProjectID: projectID, + RepoOwner: pbRepo.Owner, + RepoName: pbRepo.Name, + RepoID: pbRepo.RepoId, + IsPrivate: pbRepo.IsPrivate, + IsFork: pbRepo.IsFork, + WebhookID: sql.NullInt64{ + Int64: pbRepo.HookId, + Valid: true, + }, + CloneUrl: pbRepo.CloneUrl, + WebhookUrl: pbRepo.HookUrl, + DeployUrl: pbRepo.DeployUrl, + DefaultBranch: sql.NullString{ + String: pbRepo.DefaultBranch, + Valid: true, + }, + }) + if err != nil { + return pbRepo, err + } + + outid = dbRepo.ID + pbRepo.Id = ptr.Ptr(dbRepo.ID.String()) + + // TODO: Replace with CreateEntity call + _, err = t.CreateEntityWithID(ctx, db.CreateEntityWithIDParams{ + ID: dbRepo.ID, + EntityType: db.EntitiesRepository, + Name: fmt.Sprintf("%s/%s", pbRepo.Owner, pbRepo.Name), + ProjectID: projectID, + ProviderID: provider.ID, + }) + + return pbRepo, err }) if err != nil { return uuid.Nil, nil, err } - pbRepo.Id = ptr.Ptr(dbRepo.ID.String()) - return dbRepo.ID, pbRepo, nil + return outid, pbr, nil } diff --git a/internal/repositories/github/service_test.go b/internal/repositories/github/service_test.go index 99e6ec4bd2..1d37a64c2a 100644 --- a/internal/repositories/github/service_test.go +++ b/internal/repositories/github/service_test.go @@ -501,15 +501,25 @@ func withSuccessfulGetByName(mock dbMock) { } func withFailedCreate(mock dbMock) { + mock.EXPECT().GetQuerierWithTransaction(gomock.Any()).Return(mock) + mock.EXPECT().BeginTransaction().Return(nil, nil) mock.EXPECT(). CreateRepository(gomock.Any(), gomock.Any()). Return(db.Repository{}, errDefault) + mock.EXPECT().Rollback(gomock.Any()).Return(nil) } func withSuccessfulCreate(mock dbMock) { + mock.EXPECT().GetQuerierWithTransaction(gomock.Any()).Return(mock) + mock.EXPECT().BeginTransaction().Return(nil, nil) mock.EXPECT(). CreateRepository(gomock.Any(), gomock.Any()). Return(dbRepo, nil) + mock.EXPECT(). + CreateEntityWithID(gomock.Any(), gomock.Any()). + Return(db.EntityInstance{}, nil) + mock.EXPECT().Commit(gomock.Any()).Return(nil) + mock.EXPECT().Rollback(gomock.Any()).Return(nil) } func withPrivateReposEnabled(mock dbMock) {