Skip to content

Commit 34c66c2

Browse files
committed
fix(org): address Evan's review comments
Signed-off-by: jaydeep869 <jaydeeppokhariya2106@gmail.com>
1 parent bda676f commit 34c66c2

8 files changed

Lines changed: 39 additions & 37 deletions

File tree

internal/controlplane/handlers_oauth.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/mindersec/minder/internal/logger"
3434
"github.com/mindersec/minder/internal/providers"
3535
"github.com/mindersec/minder/internal/providers/credentials"
36-
"github.com/mindersec/minder/internal/providers/github"
3736
"github.com/mindersec/minder/internal/providers/github/service"
3837
"github.com/mindersec/minder/internal/providers/manager"
3938
"github.com/mindersec/minder/internal/util"
@@ -443,8 +442,7 @@ func (s *Server) processAppCallback(ctx context.Context, w http.ResponseWriter,
443442
}
444443

445444
if dbProv != nil {
446-
login := github.GetGithubAppOwner(dbProv.Name)
447-
s.publishOrganizationEntityEvent(ctx, dbProv.ID, dbProv.ProjectID, login)
445+
s.publishOrganizationEntityEvent(ctx, dbProv.Provider.ID, dbProv.Provider.ProjectID, dbProv.InstallationOwner)
448446
}
449447

450448
if stateData.RedirectUrl.Valid || stateData.EncryptedRedirect.Valid {
@@ -543,10 +541,9 @@ func (s *Server) handleAppInstallWithoutInvite(ctx context.Context, token *oauth
543541
return nil, err
544542
}
545543
if dbProv != nil && proj != nil {
546-
login := github.GetGithubAppOwner(dbProv.Name)
547544
// It is generally safe to publish an event from within a transaction, as long
548545
// as the event handler evaluates the state matching later.
549-
s.publishOrganizationEntityEvent(ctx, dbProv.ID, proj.ID, login)
546+
s.publishOrganizationEntityEvent(ctx, dbProv.Provider.ID, proj.ID, dbProv.InstallationOwner)
550547
}
551548
return proj, nil
552549
})

internal/controlplane/handlers_oauth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ func TestHandleGitHubAppCallback(t *testing.T) {
829829
}, nil)
830830
service.EXPECT().
831831
CreateGitHubAppProvider(gomock.Any(), gomock.Any(), gomock.Any(), installationID, gomock.Any()).
832-
Return(&db.Provider{}, nil)
832+
Return(&ghService.GitHubProviderFacet{Provider: &db.Provider{}}, nil)
833833
},
834834
checkResponse: func(t *testing.T, resp httptest.ResponseRecorder) {
835835
t.Helper()

internal/controlplane/handlers_user.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/mindersec/minder/internal/db"
2727
"github.com/mindersec/minder/internal/logger"
2828
"github.com/mindersec/minder/internal/projects"
29-
"github.com/mindersec/minder/internal/providers/github"
3029
"github.com/mindersec/minder/internal/util"
3130
pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
3231
)
@@ -141,8 +140,7 @@ func (s *Server) claimGitHubInstalls(ctx context.Context, qtx db.ExtendQuerier)
141140
continue
142141
}
143142
if dbProv != nil {
144-
login := github.GetGithubAppOwner(dbProv.Name)
145-
s.publishOrganizationEntityEvent(ctx, dbProv.ID, proj.ID, login)
143+
s.publishOrganizationEntityEvent(ctx, dbProv.Provider.ID, proj.ID, dbProv.InstallationOwner)
146144
}
147145
if proj != nil {
148146
userProjects = append(userProjects, proj)

internal/providers/github/properties/organization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func fetchOrganizationProperties(
9393
}
9494
}
9595
if user.CreatedAt != nil {
96-
result[properties.OrgPropertyCreatedAt] = user.GetCreatedAt().Time.Format(time.RFC3339)
96+
result[properties.OrgPropertyCreatedAt] = user.GetCreatedAt().Format(time.RFC3339)
9797
}
9898
if user.Plan != nil {
9999
result[properties.OrgPropertyPlanName] = user.GetPlan().GetName()

internal/providers/github/service/backfill.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package service
66
import (
77
"context"
88
"database/sql"
9-
"encoding/json"
109
"errors"
1110
"fmt"
1211

@@ -59,16 +58,10 @@ func BackfillOrganizations(ctx context.Context, store db.Store) error {
5958
}
6059

6160
// Set the default property (login name)
62-
propVal := map[string]any{
63-
"minder.internal.type": "string",
64-
"minder.internal.value": login,
65-
}
66-
propBytes, _ := json.Marshal(propVal)
67-
68-
_, err = qtx.UpsertProperty(ctx, db.UpsertPropertyParams{
61+
_, err = qtx.UpsertPropertyValueV1(ctx, db.UpsertPropertyValueV1Params{
6962
EntityID: ent.ID,
7063
Key: properties.PropertyName,
71-
Value: propBytes,
64+
Value: login,
7265
})
7366

7467
if err == nil {

internal/providers/github/service/mock/service.go

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/providers/github/service/service.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,23 @@ import (
3232

3333
//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE
3434

35+
// GitHubProviderFacet encapsulates the created db.Provider and additional GitHub specific properties
36+
type GitHubProviderFacet struct {
37+
Provider *db.Provider
38+
InstallationOwner string
39+
}
40+
3541
// GitHubProviderService encapsulates methods for creating and updating providers
3642
type GitHubProviderService interface {
3743
// CreateGitHubAppProvider creates a GitHub App provider with an installation ID in a known project
3844
CreateGitHubAppProvider(ctx context.Context, token oauth2.Token, stateData db.GetProjectIDBySessionStateRow,
39-
installationID int64, state string) (*db.Provider, error)
45+
installationID int64, state string) (*GitHubProviderFacet, error)
4046
// CreateGitHubAppWithoutInvitation either creates a new project for the selected app, or stores
4147
// the installation in preparation for creating a new project when the authorizing user logs in.
4248
//
4349
// Note that this function may return nil, nil if the installation user is not known to Minder.
4450
CreateGitHubAppWithoutInvitation(ctx context.Context, qtx db.ExtendQuerier, userID int64,
45-
installationID int64) (*db.Project, *db.Provider, error)
51+
installationID int64) (*db.Project, *GitHubProviderFacet, error)
4652
// ValidateGitHubInstallationId checks if the supplied GitHub token has access to the installation ID
4753
ValidateGitHubInstallationId(ctx context.Context, token *oauth2.Token, installationID int64) error
4854
// DeleteGitHubAppInstallation deletes the GitHub App installation and provider from the database.
@@ -106,13 +112,13 @@ func (p *ghProviderService) CreateGitHubAppProvider(
106112
stateData db.GetProjectIDBySessionStateRow,
107113
installationID int64,
108114
state string,
109-
) (*db.Provider, error) {
115+
) (*GitHubProviderFacet, error) {
110116
installationOwner, err := p.getInstallationOwner(ctx, installationID)
111117
if err != nil {
112118
return nil, err
113119
}
114120

115-
return db.WithTransaction(p.store, func(qtx db.ExtendQuerier) (*db.Provider, error) {
121+
return db.WithTransaction(p.store, func(qtx db.ExtendQuerier) (*GitHubProviderFacet, error) {
116122
validateOwnership := func(ctx context.Context) error {
117123
// Older enrollments may not have a RemoteUser stored; these should age out fairly quickly.
118124
p.mt.AddTokenOpCount(ctx, "check", stateData.RemoteUser.Valid)
@@ -157,7 +163,10 @@ func (p *ghProviderService) CreateGitHubAppProvider(
157163
},
158164
)
159165

160-
return &provider, err
166+
return &GitHubProviderFacet{
167+
Provider: &provider,
168+
InstallationOwner: installationOwner.GetLogin(),
169+
}, err
161170
})
162171
}
163172

@@ -170,7 +179,7 @@ func (p *ghProviderService) CreateGitHubAppWithoutInvitation(
170179
qtx db.ExtendQuerier,
171180
userID int64,
172181
installationID int64,
173-
) (*db.Project, *db.Provider, error) {
182+
) (*db.Project, *GitHubProviderFacet, error) {
174183
installationOwner, err := p.getInstallationOwner(ctx, installationID)
175184
if err != nil {
176185
return nil, nil, err
@@ -213,7 +222,10 @@ func (p *ghProviderService) CreateGitHubAppWithoutInvitation(
213222

214223
}
215224

216-
return project, &provider, err
225+
return project, &GitHubProviderFacet{
226+
Provider: &provider,
227+
InstallationOwner: installationOwner.GetLogin(),
228+
}, err
217229
}
218230

219231
// Internal shared implementation between CreateGitHubAppProvider and CreateGitHubAppWithoutInvitation.

internal/providers/github/service/service_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,16 @@ func TestProviderService_CreateGitHubAppProvider(t *testing.T) {
271271
require.NoError(t, err)
272272
require.NotNil(t, dbProv)
273273

274-
require.Equal(t, dbProv.ProjectID, dbproj.ID)
275-
require.Equal(t, dbProv.AuthFlows, clients.AppAuthorizationFlows)
276-
require.Equal(t, dbProv.Implements, clients.AppImplements)
277-
require.Equal(t, dbProv.Class, db.ProviderClassGithubApp)
278-
require.Contains(t, dbProv.Name, db.ProviderClassGithubApp)
279-
require.Contains(t, dbProv.Name, accountLogin)
274+
require.Equal(t, dbProv.Provider.ProjectID, dbproj.ID)
275+
require.Equal(t, dbProv.Provider.AuthFlows, clients.AppAuthorizationFlows)
276+
require.Equal(t, dbProv.Provider.Implements, clients.AppImplements)
277+
require.Equal(t, dbProv.Provider.Class, db.ProviderClassGithubApp)
278+
require.Contains(t, dbProv.Provider.Name, db.ProviderClassGithubApp)
279+
require.Contains(t, dbProv.Provider.Name, accountLogin)
280+
require.Equal(t, accountLogin, dbProv.InstallationOwner)
280281

281282
dbInstall, err := mocks.fakeStore.GetInstallationIDByProviderID(context.Background(),
282-
uuid.NullUUID{UUID: dbProv.ID, Valid: true},
283+
uuid.NullUUID{UUID: dbProv.Provider.ID, Valid: true},
283284
)
284285
require.NoError(t, err)
285286
require.Equal(t, dbInstall.AppInstallationID, int64(installationID))

0 commit comments

Comments
 (0)