Skip to content

Commit

Permalink
Create repositories on entities table as well
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Aug 13, 2024
1 parent abca3bd commit 13ac34e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 39 deletions.
97 changes: 58 additions & 39 deletions internal/repositories/github/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions internal/repositories/github/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 13ac34e

Please sign in to comment.