Skip to content

Commit

Permalink
Add a new ProtoMessageConverter provider interface to convert entitie…
Browse files Browse the repository at this point in the history
…s to typed protobuf messages (#4469)

This will help us get rid of database and/or property calls in the
webhook handler.

Related: #4327
  • Loading branch information
jhrozek authored Sep 12, 2024
1 parent afb7fbc commit ac41850
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 7 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 @@ -378,7 +378,7 @@ func entityWithPropertiesToEntityInfoWrapper(ewp *entModels.EntityWithProperties
case minderv1.Entity_ENTITY_ARTIFACTS:
ent, err = properties.ArtifactV1FromProperties(ewp.Properties)
case minderv1.Entity_ENTITY_PULL_REQUESTS:
ent = properties.PullRequestV1FromProperties(ewp.Properties)
ent, err = properties.PullRequestV1FromProperties(ewp.Properties)
}
if err != nil {
return nil, fmt.Errorf("error converting properties to entity: %w", err)
Expand Down
34 changes: 30 additions & 4 deletions internal/controlplane/handlers_githubwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,16 @@ func (s *Server) processPackageEvent(
return nil, fmt.Errorf("error extracting artifact from payload: %w", err)
}

pbArtifact, err := ghprop.ArtifactV1FromProperties(refreshedPkgProperties)
ewp := models.NewEntityWithProperties(*ei, refreshedPkgProperties)
pbMsg, err := s.props.EntityWithPropertiesAsProto(ctx, ewp, s.providerManager)
if err != nil {
return nil, fmt.Errorf("error converting artifact to protobuf: %w", err)
}

pbArtifact, ok := pbMsg.(*pb.Artifact)
if !ok {
return nil, errors.New("error converting proto message to protobuf")
}
pbArtifact.Versions = []*pb.ArtifactVersion{version}

eiw := entities.NewEntityInfoWrapper().
Expand Down Expand Up @@ -891,11 +897,16 @@ func (s *Server) processRelevantRepositoryEvent(

// For all other actions, we trigger an evaluation.
// protobufs are our API, so we always execute on these instead of the DB directly.
pbRepo, err := ghprop.RepoV1FromProperties(repoEntity.Properties)
pbMsg, err := s.props.EntityWithPropertiesAsProto(ctx, repoEntity, s.providerManager)
if err != nil {
return nil, fmt.Errorf("error converting repository to protobuf: %w", err)
}

pbRepo, ok := pbMsg.(*pb.Repository)
if !ok {
return nil, errors.New("error converting proto message to protobuf")
}

eiw := entities.NewEntityInfoWrapper().
WithActionEvent(event.GetAction()).
WithProjectID(repoEntity.Entity.ProjectID).
Expand Down Expand Up @@ -941,11 +952,16 @@ func (s *Server) processRepositoryEvent(
}

// protobufs are our API, so we always execute on these instead of the DB directly.
pbRepo, err := ghprop.RepoV1FromProperties(repoEnt.Properties)
pbMsg, err := s.props.EntityWithPropertiesAsProto(ctx, repoEnt, s.providerManager)
if err != nil {
return nil, fmt.Errorf("error converting repository to protobuf: %w", err)
}

pbRepo, ok := pbMsg.(*pb.Repository)
if !ok {
return nil, errors.New("error converting proto message to protobuf")
}

eiw := entities.NewEntityInfoWrapper().
WithActionEvent(event.GetAction()).
WithProjectID(repoEnt.Entity.ProjectID).
Expand All @@ -956,6 +972,7 @@ func (s *Server) processRepositoryEvent(
return &processingResult{topic: events.TopicQueueEntityEvaluate, wrapper: eiw}, nil
}

// nolint:gocyclo // This function will be re-simplified real soon
func (s *Server) processPullRequestEvent(
ctx context.Context,
payload []byte,
Expand Down Expand Up @@ -1024,7 +1041,16 @@ func (s *Server) processPullRequestEvent(

l.Info().Msgf("evaluating PR %s\n", event.GetPullRequest().GetURL())

pbPullRequest := ghprop.PullRequestV1FromProperties(prEntWithProps.Properties)
// protobufs are our API, so we always execute on these instead of the DB directly.
pbMsg, err := s.props.EntityWithPropertiesAsProto(ctx, prEntWithProps, s.providerManager)
if err != nil {
return nil, fmt.Errorf("error converting repository to protobuf: %w", err)
}

pbPullRequest, ok := pbMsg.(*pb.PullRequest)
if !ok {
return nil, errors.New("error converting proto message to protobuf")
}

eiw := entities.NewEntityInfoWrapper().
WithActionEvent(event.GetAction()).
Expand Down
Loading

0 comments on commit ac41850

Please sign in to comment.