Skip to content

Commit 15955f7

Browse files
authored
Add "options" to properties service calls (#4459)
Instead of passing the db store and other optional values as explicit paramters, this introduces options to the properties service. This allows us to start building queries in a more dynamic way. The querier is passed always now as an option. Disregarding stale data was introduced as an option, and it was also used in the places it's needed, that is, in GET calls. Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent 5d2e740 commit 15955f7

File tree

11 files changed

+234
-87
lines changed

11 files changed

+234
-87
lines changed

internal/controlplane/handlers_evalstatus.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ func (s *Server) buildRuleEvaluationStatusFromDBEvaluation(
597597
Msg("error converting severity will use defaults")
598598
}
599599

600-
err = s.props.RetrieveAllPropertiesForEntity(ctx, efp, s.providerManager, s.store)
600+
err = s.props.RetrieveAllPropertiesForEntity(ctx, efp, s.providerManager,
601+
propSvc.ReadBuilder().WithStoreOrTransaction(s.store).TolerateStaleData())
601602
if err != nil {
602603
return nil, fmt.Errorf("error fetching properties for entity: %s: %w", efp.Entity.ID.String(), err)
603604
}

internal/controlplane/handlers_githubwebhooks.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/stacklok/minder/internal/engine/entities"
4343
"github.com/stacklok/minder/internal/entities/models"
4444
"github.com/stacklok/minder/internal/entities/properties"
45+
"github.com/stacklok/minder/internal/entities/properties/service"
4546
"github.com/stacklok/minder/internal/events"
4647
"github.com/stacklok/minder/internal/projects/features"
4748
"github.com/stacklok/minder/internal/providers/github/clients"
@@ -730,7 +731,9 @@ func (s *Server) processPackageEvent(
730731
refreshedPkgProperties, err = s.props.RetrieveAllProperties(
731732
ctx, provider,
732733
repoEnt.Entity.ProjectID, repoEnt.Entity.ProviderID,
733-
pkgLookupProps, pb.Entity_ENTITY_ARTIFACTS, tx)
734+
pkgLookupProps, pb.Entity_ENTITY_ARTIFACTS,
735+
service.ReadBuilder().WithStoreOrTransaction(tx))
736+
734737
if err != nil {
735738
return nil, fmt.Errorf("error retrieving properties: %w", err)
736739
}
@@ -776,7 +779,8 @@ func (s *Server) processPackageEvent(
776779
refreshedPkgProperties, err = s.props.RetrieveAllProperties(
777780
ctx, provider,
778781
ent.ProjectID, ent.ProviderID,
779-
refreshedPkgProperties, pb.Entity_ENTITY_ARTIFACTS, tx)
782+
refreshedPkgProperties, pb.Entity_ENTITY_ARTIFACTS,
783+
service.ReadBuilder().WithStoreOrTransaction(tx))
780784
if err != nil {
781785
return nil, fmt.Errorf("error retrieving properties: %w", err)
782786
}
@@ -1613,7 +1617,9 @@ func (s *Server) updatePullRequestInfoFromProvider(
16131617

16141618
prProps, err := s.props.RetrieveAllProperties(ctx, provider,
16151619
repoEnt.Entity.ProjectID, repoEnt.Entity.ProviderID,
1616-
lookupProperties, pb.Entity_ENTITY_PULL_REQUESTS, qtx)
1620+
lookupProperties, pb.Entity_ENTITY_PULL_REQUESTS,
1621+
service.ReadBuilder().WithStoreOrTransaction(qtx))
1622+
16171623
if err != nil {
16181624
return nil, fmt.Errorf("error retrieving properties: %w", err)
16191625
}

internal/controlplane/handlers_profile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ func (s *Server) getRuleEvalStatus(
461461
return nil, fmt.Errorf("error fetching entity for properties: %w", err)
462462
}
463463

464-
err = s.props.RetrieveAllPropertiesForEntity(ctx, efp, s.providerManager, s.store)
464+
err = s.props.RetrieveAllPropertiesForEntity(ctx, efp, s.providerManager,
465+
propSvc.ReadBuilder().WithStoreOrTransaction(s.store).TolerateStaleData())
465466
if err != nil {
466467
return nil, fmt.Errorf("error fetching properties for entity: %w", err)
467468
}

internal/engine/executor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ func (e *executor) profileEvalStatus(
235235
}
236236

237237
// get the entity with properties by the entity UUID
238-
ewp, err := e.propService.EntityWithProperties(ctx, entityID, e.querier)
238+
ewp, err := e.propService.EntityWithProperties(ctx, entityID,
239+
service.CallBuilder().WithStoreOrTransaction(e.querier))
239240
if err != nil {
240241
return fmt.Errorf("error getting entity with properties: %w", err)
241242
}

internal/entities/properties/service/helpers.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import (
3636
func (ps *propertiesService) retrieveAllPropertiesForEntity(
3737
ctx context.Context, provider provifv1.Provider, entID uuid.UUID,
3838
lookupProperties *properties.Properties, entType minderv1.Entity,
39-
qtx db.ExtendQuerier, l zerolog.Logger,
39+
opts *ReadOptions, l zerolog.Logger,
4040
) (*properties.Properties, error) {
41+
qtx := ps.getStoreOrTransaction(opts)
4142

4243
var dbProps []db.Property
4344
if entID != uuid.Nil {
@@ -61,7 +62,7 @@ func (ps *propertiesService) retrieveAllPropertiesForEntity(
6162
if err != nil {
6263
return nil, fmt.Errorf("failed to convert properties: %w", err)
6364
}
64-
if ps.areDatabasePropertiesValid(dbProps) {
65+
if ps.areDatabasePropertiesValid(dbProps, opts) {
6566
l.Info().Msg("properties are valid, skipping provider fetch")
6667
return modelProps, nil
6768
}
@@ -84,7 +85,7 @@ func (ps *propertiesService) retrieveAllPropertiesForEntity(
8485
}
8586

8687
// save updated properties to db, thus making sure that the updatedAt are bumped
87-
err = ps.ReplaceAllProperties(ctx, entID, refreshedProps, qtx)
88+
err = ps.ReplaceAllProperties(ctx, entID, refreshedProps, opts.getPropertiesServiceCallOptions())
8889
if err != nil {
8990
return nil, fmt.Errorf("failed to update properties: %w", err)
9091
}
@@ -93,15 +94,15 @@ func (ps *propertiesService) retrieveAllPropertiesForEntity(
9394
return refreshedProps, nil
9495
}
9596

96-
func (ps *propertiesService) getEntityIdByProperties(
97+
func getEntityIdByProperties(
9798
ctx context.Context, projectId uuid.UUID,
9899
providerID uuid.UUID,
99100
props *properties.Properties, entType minderv1.Entity,
100101
qtx db.ExtendQuerier,
101102
) (uuid.UUID, error) {
102103
upstreamID := props.GetProperty(properties.PropertyUpstreamID)
103104
if upstreamID != nil {
104-
ent, getErr := ps.getEntityIdByUpstreamID(ctx, projectId, providerID, upstreamID.GetString(), entType, qtx)
105+
ent, getErr := getEntityIdByUpstreamID(ctx, projectId, providerID, upstreamID.GetString(), entType, qtx)
105106
if getErr == nil {
106107
return ent, nil
107108
} else if !errors.Is(getErr, ErrEntityNotFound) {
@@ -114,14 +115,14 @@ func (ps *propertiesService) getEntityIdByProperties(
114115
// Fall back to name if no upstream ID is provided
115116
name := props.GetProperty(properties.PropertyName)
116117
if name != nil {
117-
return ps.getEntityIdByName(ctx, projectId, providerID, name.GetString(), entType, qtx)
118+
return getEntityIdByName(ctx, projectId, providerID, name.GetString(), entType, qtx)
118119
}
119120

120121
// returning nil ID and nil error would make us just go to the provider. Slow, but we'd continue.
121122
return uuid.Nil, nil
122123
}
123124

124-
func (_ *propertiesService) getEntityIdByName(
125+
func getEntityIdByName(
125126
ctx context.Context, projectId uuid.UUID,
126127
providerID uuid.UUID,
127128
name string, entType minderv1.Entity,
@@ -142,7 +143,7 @@ func (_ *propertiesService) getEntityIdByName(
142143
return ent.ID, nil
143144
}
144145

145-
func (_ *propertiesService) getEntityIdByUpstreamID(
146+
func getEntityIdByUpstreamID(
146147
ctx context.Context, projectId uuid.UUID,
147148
providerID uuid.UUID,
148149
upstreamID string, entType minderv1.Entity,
@@ -173,20 +174,21 @@ func (_ *propertiesService) getEntityIdByUpstreamID(
173174
return uuid.Nil, ErrEntityNotFound
174175
}
175176

176-
func (ps *propertiesService) areDatabasePropertiesValid(dbProps []db.Property) bool {
177+
func (ps *propertiesService) areDatabasePropertiesValid(
178+
dbProps []db.Property, opts *ReadOptions) bool {
177179
// if the all the properties are to be valid, neither must be older than
178180
// the cache timeout
179181
for _, prop := range dbProps {
180-
if !ps.isDatabasePropertyValid(prop) {
182+
if !ps.isDatabasePropertyValid(prop, opts) {
181183
return false
182184
}
183185
}
184186
return true
185187
}
186188

187-
func (ps *propertiesService) isDatabasePropertyValid(dbProp db.Property) bool {
188-
if ps.entityTimeout == bypassCacheTimeout {
189-
// this is mostly for testing
189+
func (ps *propertiesService) isDatabasePropertyValid(
190+
dbProp db.Property, opts *ReadOptions) bool {
191+
if ps.entityTimeout == bypassCacheTimeout || opts.canTolerateStaleData() {
190192
return false
191193
}
192194
return time.Since(dbProp.UpdatedAt) < ps.entityTimeout

internal/entities/properties/service/mock/service.go

+29-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)