Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

origination: First get properties from provider before trying to persist #4660

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion internal/entities/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ type (
providerMockBuilder = func(controller *gomock.Controller) providerMock
)

func getPullRequestProperties() *properties.Properties {
props, err := properties.NewProperties(pullRequestPropMap)
if err != nil {
panic(err)
}

return props
}

func newProviderMock(opts ...func(providerMock)) providerMockBuilder {
return func(ctrl *gomock.Controller) providerMock {
mock := mockgithub.NewMockGitHub(ctrl)
Expand All @@ -116,6 +125,14 @@ func withSuccessfulGetEntityName(name string) func(providerMock) {
}
}

func withSuccessfulFetchAllProperties(props *properties.Properties) func(mock providerMock) {
return func(mock providerMock) {
mock.EXPECT().
FetchAllProperties(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(props, nil)
}
}

func buildEwp(t *testing.T, ewp models.EntityWithProperties, propMap map[string]any) *models.EntityWithProperties {
t.Helper()

Expand Down Expand Up @@ -415,7 +432,10 @@ func TestRefreshEntityAndDoHandler_HandleRefreshEntityAndEval(t *testing.T) {
EntityType: db.EntitiesRepository,
}),
),
providerSetup: newProviderMock(withSuccessfulGetEntityName(pullName)),
providerSetup: newProviderMock(
withSuccessfulGetEntityName(pullName),
withSuccessfulFetchAllProperties(getPullRequestProperties()),
),
providerManagerSetup: func(prov provifv1.Provider) provManFixtures.ProviderManagerMockBuilder {
return provManFixtures.NewProviderManagerMock(
provManFixtures.WithSuccessfulInstantiateFromID(prov),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,22 @@ func (a *addOriginatingEntityStrategy) GetEntity(
return nil, fmt.Errorf("error getting parent entity: %w", err)
}

legacyId, err := a.upsertLegacyEntity(ctx, entMsg.Entity.Type, parentEwp, childProps, t)
prov, err := a.provMgr.InstantiateFromID(ctx, parentEwp.Entity.ProviderID)
if err != nil {
return nil, fmt.Errorf("error upserting legacy entity: %w", err)
return nil, fmt.Errorf("error getting provider: %w", err)
}

prov, err := a.provMgr.InstantiateFromID(ctx, parentEwp.Entity.ProviderID)
upstreamProps, err := prov.FetchAllProperties(ctx, childProps, entMsg.Entity.Type, nil)
if err != nil {
return nil, fmt.Errorf("error getting provider: %w", err)
return nil, fmt.Errorf("error retrieving properties: %w", err)
}

childEntName, err := prov.GetEntityName(entMsg.Entity.Type, childProps)
legacyId, err := a.upsertLegacyEntity(ctx, entMsg.Entity.Type, parentEwp, upstreamProps, t)
if err != nil {
return nil, fmt.Errorf("error upserting legacy entity: %w", err)
}

childEntName, err := prov.GetEntityName(entMsg.Entity.Type, upstreamProps)
if err != nil {
return nil, fmt.Errorf("error getting child entity name: %w", err)
}
Expand All @@ -102,13 +107,14 @@ func (a *addOriginatingEntityStrategy) GetEntity(
return nil, err
}

upstreamProps, err := a.propSvc.RetrieveAllProperties(ctx, prov,
// Persist the properties
upstreamProps, err = a.propSvc.RetrieveAllProperties(ctx, prov,
parentEwp.Entity.ProjectID, parentEwp.Entity.ProviderID,
childProps, entMsg.Entity.Type,
propertyService.ReadBuilder().WithStoreOrTransaction(t),
)
if err != nil {
return nil, fmt.Errorf("error retrieving properties: %w", err)
return nil, fmt.Errorf("error persisting properties: %w", err)
}

return models.NewEntityWithProperties(childEnt, upstreamProps), nil
Expand Down
5 changes: 5 additions & 0 deletions internal/providers/github/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (c *GitHub) FetchAllProperties(
}

func filterOperational(cachedProperties *properties.Properties, fetcher properties2.GhPropertyFetcher) *properties.Properties {
if cachedProperties == nil {
// Nothing to filter
return nil
}

operational := fetcher.OperationalProperties()
if len(operational) == 0 {
return cachedProperties
Expand Down