Skip to content

Commit

Permalink
Fill in provider information to SelectorEntity
Browse files Browse the repository at this point in the history
In order to be able to evaluate selectors including the provider, we
need to fill in the provider information.

Related: #4564
  • Loading branch information
jhrozek committed Sep 30, 2024
1 parent dfff481 commit 0ed942b
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cmd/dev/app/rule_type/rttst.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func selectAndEval(
evalStatus *engif.EvalStatusParams,
profileSelectors selectors.Selection,
) error {
selEnt := provsel.EntityToSelectorEntity(ctx, inf.Type, ewp)
selEnt := provsel.EntityToSelectorEntity(ctx, nil, inf.Type, ewp)
if selEnt == nil {
return fmt.Errorf("error converting entity to selector entity")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (e *executor) profileEvalStatus(
return fmt.Errorf("error getting entity with properties: %w", err)
}

selEnt := provsel.EntityToSelectorEntity(ctx, eiw.Type, ewp)
selEnt := provsel.EntityToSelectorEntity(ctx, e.querier, eiw.Type, ewp)
if selEnt == nil {
return fmt.Errorf("error converting entity to selector entity")
}
Expand Down
121 changes: 77 additions & 44 deletions internal/providers/selectors/selector_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,35 @@ package selectors

import (
"context"
"fmt"

"github.com/rs/zerolog"
"google.golang.org/protobuf/proto"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/entities/models"
"github.com/stacklok/minder/internal/entities/properties"
internalpb "github.com/stacklok/minder/internal/proto"
ghprop "github.com/stacklok/minder/internal/providers/github/properties"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

type toSelectorEntity func(ctx context.Context, entityWithProps *models.EntityWithProperties) *internalpb.SelectorEntity

func repoToSelectorEntity(
_ context.Context, entityWithProps *models.EntityWithProperties) *internalpb.SelectorEntity {
if entityWithProps.Entity.Type != minderv1.Entity_ENTITY_REPOSITORIES {
return nil
func buildBaseSelectorEntity(
entityWithProps *models.EntityWithProperties, selProv *internalpb.SelectorProvider) *internalpb.SelectorEntity {
return &internalpb.SelectorEntity{
EntityType: entityWithProps.Entity.Type,
Name: entityWithProps.Entity.Name,
Provider: selProv,
}
}

type toSelectorEntity func(
entityWithProps *models.EntityWithProperties, selProv *internalpb.SelectorProvider,
) *internalpb.SelectorEntity

func repoToSelectorEntity(
entityWithProps *models.EntityWithProperties, selProv *internalpb.SelectorProvider,
) *internalpb.SelectorEntity {
var isFork *bool
if propIsFork, err := entityWithProps.Properties.GetProperty(properties.RepoPropertyIsFork).AsBool(); err == nil {
isFork = proto.Bool(propIsFork)
Expand All @@ -47,62 +57,53 @@ func repoToSelectorEntity(
isPrivate = proto.Bool(propIsPrivate)
}

return &internalpb.SelectorEntity{
EntityType: minderv1.Entity_ENTITY_REPOSITORIES,
Name: entityWithProps.Entity.Name,
Entity: &internalpb.SelectorEntity_Repository{
Repository: &internalpb.SelectorRepository{
Name: entityWithProps.Entity.Name,
IsFork: isFork,
IsPrivate: isPrivate,
Properties: entityWithProps.Properties.ToProtoStruct(),
},
selEnt := buildBaseSelectorEntity(entityWithProps, selProv)
selEnt.Entity = &internalpb.SelectorEntity_Repository{
Repository: &internalpb.SelectorRepository{
Name: entityWithProps.Entity.Name,
IsFork: isFork,
IsPrivate: isPrivate,
Properties: entityWithProps.Properties.ToProtoStruct(),
Provider: selProv,
},
}
return selEnt
}

func artifactToSelectorEntity(
ctx context.Context, entityWithProps *models.EntityWithProperties) *internalpb.SelectorEntity {
if entityWithProps.Entity.Type != minderv1.Entity_ENTITY_ARTIFACTS {
return nil
}

entityWithProps *models.EntityWithProperties, selProv *internalpb.SelectorProvider,
) *internalpb.SelectorEntity {
var artifactType string
var err error
artifactType, err = entityWithProps.Properties.GetProperty(properties.ArtifactPropertyType).AsString()
if err != nil {
artifactType = entityWithProps.Properties.GetProperty(ghprop.ArtifactPropertyType).GetString()
}

return &internalpb.SelectorEntity{
EntityType: minderv1.Entity_ENTITY_ARTIFACTS,
Name: entityWithProps.Entity.Name,
Entity: &internalpb.SelectorEntity_Artifact{
Artifact: &internalpb.SelectorArtifact{
Name: entityWithProps.Entity.Name,
Type: artifactType,
Properties: entityWithProps.Properties.ToProtoStruct(),
},
selEnt := buildBaseSelectorEntity(entityWithProps, selProv)
selEnt.Entity = &internalpb.SelectorEntity_Artifact{
Artifact: &internalpb.SelectorArtifact{
Name: entityWithProps.Entity.Name,
Type: artifactType,
Properties: entityWithProps.Properties.ToProtoStruct(),
Provider: selProv,
},
}
return selEnt
}

func pullRequestToSelectorEntity(
_ context.Context, entityWithProps *models.EntityWithProperties) *internalpb.SelectorEntity {
if entityWithProps.Entity.Type != minderv1.Entity_ENTITY_PULL_REQUESTS {
return nil
}

return &internalpb.SelectorEntity{
EntityType: minderv1.Entity_ENTITY_PULL_REQUESTS,
Name: entityWithProps.Entity.Name,
Entity: &internalpb.SelectorEntity_PullRequest{
PullRequest: &internalpb.SelectorPullRequest{
Name: entityWithProps.Entity.Name,
Properties: entityWithProps.Properties.ToProtoStruct(),
},
entityWithProps *models.EntityWithProperties, selProv *internalpb.SelectorProvider,
) *internalpb.SelectorEntity {
selEnt := buildBaseSelectorEntity(entityWithProps, selProv)
selEnt.Entity = &internalpb.SelectorEntity_PullRequest{
PullRequest: &internalpb.SelectorPullRequest{
Name: entityWithProps.Entity.Name,
Properties: entityWithProps.Properties.ToProtoStruct(),
Provider: selProv,
},
}
return selEnt
}

// newConverterFactory creates a new converterFactory with the default converters for each entity type
Expand All @@ -118,9 +119,31 @@ func newConverter(entType minderv1.Entity) toSelectorEntity {
return nil
}

func fillProviderInfo(
ctx context.Context,
querier db.Store,
entityWithProps *models.EntityWithProperties,
) (*internalpb.SelectorProvider, error) {
if querier == nil {
zerolog.Ctx(ctx).Warn().Msg("No querier, will not fill provider information")
return nil, nil
}

dbProv, err := querier.GetProviderByID(ctx, entityWithProps.Entity.ProviderID)
if err != nil {
return nil, fmt.Errorf("failed to get provider %s by ID", entityWithProps.Entity.ProviderID.String())
}

return &internalpb.SelectorProvider{
Name: dbProv.Name,
Class: string(dbProv.Class),
}, nil
}

// EntityToSelectorEntity converts an entity to a SelectorEntity
func EntityToSelectorEntity(
ctx context.Context,
querier db.Store,
entType minderv1.Entity,
entityWithProps *models.EntityWithProperties,
) *internalpb.SelectorEntity {
Expand All @@ -129,5 +152,15 @@ func EntityToSelectorEntity(
zerolog.Ctx(ctx).Error().Str("entType", entType.ToString()).Msg("No converter available")
return nil
}
return converter(ctx, entityWithProps)

selProv, err := fillProviderInfo(ctx, querier, entityWithProps)
if err != nil {
zerolog.Ctx(ctx).Error().
Str("providerID", entityWithProps.Entity.ProviderID.String()).
Err(err).
Msg("Cannot fill provider information")
return nil
}
selEnt := converter(entityWithProps, selProv)
return selEnt
}
Loading

0 comments on commit 0ed942b

Please sign in to comment.