Skip to content

Commit 6d8cf8e

Browse files
committed
fixed nil pointer exeception and now all groups are found successfully
1 parent 8ff03ec commit 6d8cf8e

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

internal/core/integrations/gitlabint/gitlab_integration.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,36 @@ func (g *GitlabIntegration) ListGroups(ctx core.Context, userID string, provider
336336
}
337337
// get the groups for this user
338338
groups, _, err := gitlabClient.ListGroups(ctx.Request().Context(), &gitlab.ListGroupsOptions{
339-
ListOptions: gitlab.ListOptions{PerPage: 100}, // only list groups where the user has at least owner permissions
339+
ListOptions: gitlab.ListOptions{PerPage: 100},
340+
//MinAccessLevel: utils.Ptr(gitlab.ReporterPermissions),
341+
// only list groups where the user has at least reporter permissions
340342
})
341343

342344
if err != nil {
343345
slog.Error("failed to list groups", "err", err)
344346
return nil, err
345347
}
346348

347-
return utils.Map(groups, func(el *gitlab.Group) models.Project {
349+
cleanedGroups := make([]*gitlab.Group, 0, len(groups))
350+
351+
for _, group := range groups {
352+
//only check if we are member of the given group
353+
member, _, err := gitlabClient.GetMemberInGroup(ctx.Request().Context(), token.GitLabUserID, (*group).ID)
354+
if err != nil {
355+
if strings.Contains(err.Error(), "403 Forbidden") || strings.Contains(err.Error(), "404 Not Found") {
356+
//swallow the error
357+
continue
358+
} else {
359+
return nil, err
360+
}
361+
}
362+
if member.AccessLevel >= gitlab.ReporterPermissions {
363+
cleanedGroups = append(cleanedGroups, group)
364+
}
365+
366+
}
367+
368+
return utils.Map(cleanedGroups, func(el *gitlab.Group) models.Project {
348369
return groupToProject(el, providerID)
349370
}), nil
350371
}
@@ -451,7 +472,7 @@ func (g *GitlabIntegration) GetRoleInGroup(ctx context.Context, userID string, p
451472
member, _, err := gitlabClient.GetMemberInGroup(ctx, token.GitLabUserID, groupIDInt)
452473
if err != nil {
453474
slog.Error("failed to get member in group", "err", err)
454-
if strings.Contains(err.Error(), "404 Not Found") || strings.Contains(err.Error(), "403 Forbidden") {
475+
if strings.Contains(err.Error(), "404 Not Found") {
455476
// user is not a member of the group
456477
return "", nil
457478
}

0 commit comments

Comments
 (0)