Skip to content

Commit 7539543

Browse files
committed
Use email on user trait instead of parsing resource ID as email.
1 parent f9518e9 commit 7539543

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

pkg/connector/groups.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,27 @@ func (r *groupBuilder) Grant(
219219
accessLevelValue := AccessLevel(parts[2])
220220
userId, err := strconv.Atoi(principal.Id.Resource)
221221
if err != nil {
222-
l.Warn("baton-gitlab grant: unable to parse user ID", zap.Error(err))
223-
userEmail := principal.Id.Resource
222+
l.Warn("baton-gitlab grant: unable to parse user ID. falling back to email invite", zap.Error(err))
223+
ut, err := resourceSdk.GetUserTrait(principal)
224+
if err != nil {
225+
return nil, fmt.Errorf("baton-gitlab: error getting user trait: %w", err)
226+
}
227+
if len(ut.Emails) == 0 {
228+
return nil, fmt.Errorf("baton-gitlab: user has no email. cannot invite user to group")
229+
}
230+
userEmail := ut.Emails[0].Address
231+
for _, email := range ut.Emails {
232+
if email.IsPrimary {
233+
userEmail = email.Address
234+
break
235+
}
236+
}
237+
l.Info("baton-gitlab grant: inviting user to group", zap.String("email", userEmail))
224238
err = r.InviteGroupMember(ctx, groupId, userEmail, accessLevelValue)
225239
if err != nil {
226240
return nil, fmt.Errorf("error inviting user to group: %w", err)
227241
}
242+
return nil, nil
228243
}
229244

230245
err = r.AddGroupMember(ctx, groupId, userId, accessLevelValue)

0 commit comments

Comments
 (0)