@@ -206,7 +206,7 @@ type GroupSyncSettings struct {
206
206
// group IDs are used to account for group renames.
207
207
// For legacy configurations, this config option has to remain.
208
208
// Deprecated: Use GroupMapping instead.
209
- LegacyGroupNameMapping map [string ]string
209
+ LegacyGroupNameMapping map [string ]string `json:"legacy_group_name_mapping,omitempty"`
210
210
}
211
211
212
212
func (s * GroupSyncSettings ) Set (v string ) error {
@@ -251,6 +251,12 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
251
251
252
252
groups := make ([]ExpectedGroup , 0 )
253
253
for _ , group := range parsedGroups {
254
+ // Legacy group mappings happen before the regex filter.
255
+ mappedGroupName , ok := s .LegacyGroupNameMapping [group ]
256
+ if ok {
257
+ group = mappedGroupName
258
+ }
259
+
254
260
// Only allow through groups that pass the regex
255
261
if s .RegexFilter != nil {
256
262
if ! s .RegexFilter .MatchString (group ) {
@@ -267,11 +273,6 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
267
273
continue
268
274
}
269
275
270
- mappedGroupName , ok := s .LegacyGroupNameMapping [group ]
271
- if ok {
272
- groups = append (groups , ExpectedGroup {GroupName : & mappedGroupName })
273
- continue
274
- }
275
276
group := group
276
277
groups = append (groups , ExpectedGroup {GroupName : & group })
277
278
}
@@ -332,6 +333,23 @@ func (s GroupSyncSettings) HandleMissingGroups(ctx context.Context, tx database.
332
333
if err != nil {
333
334
return nil , xerrors .Errorf ("insert missing groups: %w" , err )
334
335
}
336
+
337
+ if len (missingGroups ) != len (createdMissingGroups ) {
338
+ // This is unfortunate, but if legacy params are used, then some existing groups
339
+ // can come as params. So we need to fetch them
340
+ allGroups , err := tx .GetGroups (ctx , database.GetGroupsParams {
341
+ OrganizationID : orgID ,
342
+ GroupNames : missingGroups ,
343
+ })
344
+ if err != nil {
345
+ return nil , xerrors .Errorf ("get groups by names: %w" , err )
346
+ }
347
+
348
+ createdMissingGroups = db2sdk .List (allGroups , func (g database.GetGroupsRow ) database.Group {
349
+ return g .Group
350
+ })
351
+ }
352
+
335
353
for _ , created := range createdMissingGroups {
336
354
addIDs = append (addIDs , created .ID )
337
355
}
0 commit comments