Skip to content

Commit

Permalink
Use the new handlers when evaluating repo webhooks
Browse files Browse the repository at this point in the history
We added new watermill handlers that allow to refresh and evaluate a
generic entity with properties. This is the first patch in a series that
takes these handlers into account with the eventual goal of removing all
the bespoke code from the GitHub webhook handler and eventually remove
direct database access from the webhook handler.

I'll expand the same method for the other webhook events we issue, but
let's do small PRs so we can have some test runs in between them and
revert in case of issues.

Related: mindersec#4327
  • Loading branch information
jhrozek committed Sep 22, 2024
1 parent a32f58d commit 52e6c77
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 953 deletions.
47 changes: 24 additions & 23 deletions internal/controlplane/handlers_githubwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/stacklok/minder/internal/controlplane/metrics"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine/entities"
entityMessage "github.com/stacklok/minder/internal/entities/handlers/message"
"github.com/stacklok/minder/internal/entities/models"
"github.com/stacklok/minder/internal/entities/properties"
"github.com/stacklok/minder/internal/entities/properties/service"
Expand Down Expand Up @@ -944,29 +945,7 @@ func (s *Server) processRepositoryEvent(

l.Info().Msg("handling event for repository")

repoEnt, err := s.fetchRepo(ctx, event.GetRepo())
if err != nil {
return nil, err
}

// protobufs are our API, so we always execute on these instead of the DB directly.
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().
WithProjectID(repoEnt.Entity.ProjectID).
WithProviderID(repoEnt.Entity.ProviderID).
WithRepository(pbRepo).
WithRepositoryID(repoEnt.Entity.ID)

return &processingResult{topic: events.TopicQueueEntityEvaluate, wrapper: eiw}, nil
return s.sendEvaluateRepoMessage(event.GetRepo(), events.TopicQueueRefreshEntityAndEvaluate)
}

// nolint:gocyclo // This function will be re-simplified real soon
Expand Down Expand Up @@ -1252,6 +1231,28 @@ func (_ *Server) repositoryAdded(
}, nil
}

func (_ *Server) sendEvaluateRepoMessage(
repo *repo,
handler string,
) (*processingResult, error) {
lookByProps, err := properties.NewProperties(map[string]any{
// the PropertyUpstreamID is always a string
properties.PropertyUpstreamID: strconv.FormatInt(repo.GetID(), 10),
})
if err != nil {
return nil, fmt.Errorf("error creating repository properties: %w", err)
}

entRefresh := entityMessage.NewEntityRefreshAndDoMessage().
WithEntity(pb.Entity_ENTITY_REPOSITORIES, lookByProps).
WithProviderImplementsHint(string(db.ProviderTypeGithub))

return &processingResult{
topic: handler,
wrapper: entRefresh},
nil
}

func (s *Server) fetchRepo(
ctx context.Context,
repo *repo,
Expand Down
Loading

0 comments on commit 52e6c77

Please sign in to comment.