@@ -52,28 +52,40 @@ func (s externalEntityProviderService) TriggerSync(c echo.Context) error {
5252 return echo .NewHTTPError (400 , "organization is not an external entity provider" )
5353}
5454
55- func (s externalEntityProviderService ) syncOrgs (c echo.Context ) error {
55+ func (s externalEntityProviderService ) SyncOrgs (c echo.Context ) error {
5656 // return the enabled git providers as well
5757 thirdPartyIntegration := core .GetThirdPartyIntegration (c )
58- orgs , err := thirdPartyIntegration .ListOrgs (c )
59- if err != nil {
60- return fmt .Errorf ("could not list organizations: %w" , err )
61- }
62- // make sure, that the third party organizations exists inside the database
63- if err := s .organizationRepository .Upsert (utils .Ptr (utils .Map (orgs , utils .Ptr )), []clause.Column {
64- {Name : "external_entity_provider_id" },
65- }, nil ); err != nil {
66- return fmt .Errorf ("could not upsert organizations: %w" , err )
67- }
68- return nil
58+ userID := core .GetSession (c ).GetUserID ()
59+ _ , err , _ := s .singleFlightGroup .Do ("syncOrgs/" + userID , func () (any , error ) {
60+ orgs , err := thirdPartyIntegration .ListOrgs (c )
61+ if err != nil {
62+ return nil , fmt .Errorf ("could not list organizations: %w" , err )
63+ }
64+
65+ orgsPtr := utils .Map (orgs , utils .Ptr )
66+
67+ // make sure, that the third party organizations exists inside the database
68+ if err := s .organizationRepository .Upsert (& orgsPtr , []clause.Column {
69+ {Name : "external_entity_provider_id" },
70+ }, nil ); err != nil {
71+ return nil , fmt .Errorf ("could not upsert organizations: %w" , err )
72+ }
73+
74+ // make sure the user is a member of the organizations
75+ for _ , org := range orgsPtr {
76+ if err := s .rbacProvider .GetDomainRBAC (org .GetID ().String ()).GrantRole (userID , core .RoleMember ); err != nil {
77+ slog .Warn ("could not grant role for user in organization" , "user" , userID , "orgID" , org .GetID (), "err" , err )
78+ }
79+ }
80+
81+ return nil , nil
82+ })
83+
84+ return err
6985}
7086
7187func (s externalEntityProviderService ) RefreshExternalEntityProviderProjects (ctx core.Context , org models.Org , user string ) error {
72- err := s .syncOrgs (ctx )
73- if err != nil {
74- slog .Warn ("could not sync organizations" , "orgID" , org .GetID (), "user" , user , "err" , err )
75- return err
76- }
88+
7789 _ , err , shared := s .singleFlightGroup .Do (org .ID .String ()+ "/" + user , func () (any , error ) {
7890 if org .ExternalEntityProviderID == nil {
7991 return nil , fmt .Errorf ("organization %s does not have an external entity provider configured" , org .GetID ())
0 commit comments