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

Simplify the attributes sent to the entity_add handler #4517

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 3 additions & 6 deletions internal/controlplane/handlers_githubwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,7 @@ func (s *Server) processRelevantRepositoryEvent(
WithProjectID(repoEntity.Entity.ProjectID).
WithProviderID(repoEntity.Entity.ProviderID).
WithEntityType("repository").
WithEntityID(repoEntity.Entity.ID).
WithAttribute("repoID", repoEntity.Entity.ID.String())
WithEntityID(repoEntity.Entity.ID)

return &processingResult{
topic: events.TopicQueueReconcileEntityDelete,
Expand Down Expand Up @@ -1221,8 +1220,7 @@ func (s *Server) repositoryRemoved(
WithProjectID(repoEnt.Entity.ProjectID).
WithProviderID(repoEnt.Entity.ProviderID).
WithEntityType("repository").
WithEntityID(repoEnt.Entity.ID).
WithAttribute("repoID", repoEnt.Entity.ID.String())
WithEntityID(repoEnt.Entity.ID)

return &processingResult{
topic: events.TopicQueueReconcileEntityDelete,
Expand All @@ -1243,8 +1241,7 @@ func (_ *Server) repositoryAdded(
WithProjectID(installation.ProjectID.UUID).
WithProviderID(installation.ProviderID.UUID).
WithEntityType("repository").
WithAttribute("repoName", repo.GetName()).
WithAttribute("repoOwner", repo.GetOwner())
WithAttribute(properties.PropertyName, repo.GetFullName())

return &processingResult{
topic: events.TopicQueueReconcileEntityAdd,
Expand Down
10 changes: 2 additions & 8 deletions internal/controlplane/handlers_githubwebhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ func (s *UnitTestSuite) TestHandleWebHookRepository() {
require.NoError(t, validator.New().Struct(&inner))
require.Equal(t, providerID, inner.ProviderID)
require.Equal(t, projectID, inner.ProjectID)
require.Equal(t, repositoryID.String(), inner.Entity["repoID"])
require.Equal(t, nil, inner.Entity["repoName"]) // optional
require.Equal(t, nil, inner.Entity["repoOwner"]) // optional

Expand Down Expand Up @@ -1236,7 +1235,6 @@ func (s *UnitTestSuite) TestHandleGitHubWebHook() {
require.NoError(t, validator.New().Struct(&evt))
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
require.Equal(t, nil, evt.Entity["repoName"]) // optional
require.Equal(t, nil, evt.Entity["repoOwner"]) // optional
},
Expand Down Expand Up @@ -1288,7 +1286,6 @@ func (s *UnitTestSuite) TestHandleGitHubWebHook() {
require.NoError(t, validator.New().Struct(&evt))
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
require.Equal(t, nil, evt.Entity["repoName"]) // optional
require.Equal(t, nil, evt.Entity["repoOwner"]) // optional
},
Expand Down Expand Up @@ -1837,7 +1834,6 @@ func (s *UnitTestSuite) TestHandleGitHubWebHook() {
require.NoError(t, validator.New().Struct(&evt))
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
require.Equal(t, nil, evt.Entity["repoName"]) // optional
require.Equal(t, nil, evt.Entity["repoOwner"]) // optional
},
Expand Down Expand Up @@ -1888,7 +1884,6 @@ func (s *UnitTestSuite) TestHandleGitHubWebHook() {
require.NoError(t, validator.New().Struct(&evt))
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
require.Equal(t, nil, evt.Entity["repoName"]) // optional
require.Equal(t, nil, evt.Entity["repoOwner"]) // optional
},
Expand Down Expand Up @@ -2143,7 +2138,6 @@ func (s *UnitTestSuite) TestHandleGitHubWebHook() {
require.NoError(t, validator.New().Struct(&evt))
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
require.Equal(t, nil, evt.Entity["repoName"]) // optional
require.Equal(t, nil, evt.Entity["repoOwner"]) // optional
},
Expand Down Expand Up @@ -4523,6 +4517,7 @@ func (s *UnitTestSuite) TestHandleGitHubAppWebHook() {
require.NoError(t, err)
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, "stacklok/minder", evt.Entity[properties.PropertyName])

received = withTimeout(ch, timeout)
require.NotNilf(t, received, "no event received after waiting %s", timeout)
Expand All @@ -4534,6 +4529,7 @@ func (s *UnitTestSuite) TestHandleGitHubAppWebHook() {
require.NoError(t, err)
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, "stacklok/trusty", evt.Entity[properties.PropertyName])
},
},
{
Expand Down Expand Up @@ -4675,7 +4671,6 @@ func (s *UnitTestSuite) TestHandleGitHubAppWebHook() {
require.NoError(t, err)
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])

received = withTimeout(ch, timeout)
require.NotNilf(t, received, "no event received after waiting %s", timeout)
Expand All @@ -4687,7 +4682,6 @@ func (s *UnitTestSuite) TestHandleGitHubAppWebHook() {
require.NoError(t, err)
require.Equal(t, providerID, evt.ProviderID)
require.Equal(t, projectID, evt.ProjectID)
require.Equal(t, repositoryID.String(), evt.Entity["repoID"])
},
},

Expand Down
16 changes: 5 additions & 11 deletions internal/reconcilers/entity_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package reconcilers

import (
"encoding/json"
"errors"
"fmt"

"github.com/ThreeDotsLabs/watermill/message"
Expand Down Expand Up @@ -52,21 +51,17 @@ func (r *Reconciler) handleEntityAddEvent(msg *message.Message) error {
return nil
}

var repoOwner string
var repoName string
var entName string
var ok bool
if repoOwner, ok = event.Entity["repoOwner"].(string); !ok {
return errors.New("invalid repo owner")
}
if repoName, ok = event.Entity["repoName"].(string); !ok {
return errors.New("invalid repo name")
if entName, ok = event.Entity[properties.PropertyName].(string); !ok {
return fmt.Errorf("invalid or unset entity name: %s", entName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this continue using the name? Or will this switch to using the upstream ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting idea, this code goes to repositories.CreateRepository which uses the name as a property for RetrieveAllProperties. I'm not sure if that and especially the underlying code supports retrieving a repo by name. I need to check that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal what property is used, as long as we use properties and not tie something to names.

}

// TODO: This should be using the properties map coming from the event
// as-is, but for now we are using the repoOwner and repoName to create
// the properties map.
fetchByProps, err := properties.NewProperties(map[string]interface{}{
properties.PropertyName: fmt.Sprintf("%s/%s", repoOwner, repoName),
properties.PropertyName: entName,
})
if err != nil {
return fmt.Errorf("error creating properties: %w", err)
Expand All @@ -75,8 +70,7 @@ func (r *Reconciler) handleEntityAddEvent(msg *message.Message) error {
l = zerolog.Ctx(ctx).With().
Str("provider_id", event.ProviderID.String()).
Str("project_id", event.ProjectID.String()).
Str("repo_name", repoName).
Str("repo_owner", repoOwner).
Str("ent_name", entName).
Logger()

// Telemetry logging
Expand Down
13 changes: 5 additions & 8 deletions internal/reconcilers/entity_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (

df "github.com/stacklok/minder/database/mock/fixtures"
db "github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/entities/properties"
"github.com/stacklok/minder/internal/reconcilers/messages"
rf "github.com/stacklok/minder/internal/repositories/mock/fixtures"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

var (
repoOwner = "stacklok"
repoName = "minder"
repoFullName = "stacklok/minder"
)

func TestHandleEntityAdd(t *testing.T) {
Expand Down Expand Up @@ -64,8 +64,7 @@ func TestHandleEntityAdd(t *testing.T) {
WithProviderID(providerID).
WithProjectID(projectID).
WithEntityType("repository").
WithAttribute("repoName", repoName).
WithAttribute("repoOwner", repoOwner).
WithAttribute(properties.PropertyName, repoFullName).
ToMessage(m)
require.NoError(t, err, "invalid message")
return m
Expand All @@ -86,8 +85,7 @@ func TestHandleEntityAdd(t *testing.T) {
WithProviderID(providerID).
WithProjectID(projectID).
WithEntityType("repository").
WithAttribute("repoName", repoName).
WithAttribute("repoOwner", repoOwner).
WithAttribute(properties.PropertyName, repoFullName).
ToMessage(m)
require.NoError(t, err, "invalid message")
return m
Expand Down Expand Up @@ -119,8 +117,7 @@ func TestHandleEntityAdd(t *testing.T) {
WithProviderID(providerID).
WithProjectID(projectID).
WithEntityType("repository").
WithAttribute("repoName", repoName).
WithAttribute("repoOwner", repoOwner).
WithAttribute(properties.PropertyName, repoFullName).
ToMessage(m)
require.NoError(t, err, "invalid message")
return m
Expand Down
Loading