wip: Ingest sub group users from gitlab

Signed-off-by: Jack Palmer <jackpalmer@spotify.com>
This commit is contained in:
Jack Palmer
2025-05-29 15:56:04 +01:00
parent 14b2521751
commit 70649aeb93
@@ -359,7 +359,7 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
}
let groups;
let users;
const allUsers = [];
// Self-hosted: Fetch the users either from the defined group (restrictUsersToGroup) or fetch all users from the GitLab instance
// SaaS: Fetch the users from the defined group (restrictUsersToGroup) or fetch all users from the root group.
@@ -367,13 +367,15 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
groups = (await this.gitLabClient.listDescendantGroups(this.config.group))
.items;
groups.push(await this.gitLabClient.getGroupByPath(this.config.group)); // adds the parent group for #26554
users = paginated<GitLabUser>(
options =>
this.gitLabClient.listGroupMembers(this.config.group, options), // calls /groups/<groupId>/members
{
page: 1,
per_page: 100,
},
allUsers.push(
paginated<GitLabUser>(
options =>
this.gitLabClient.listGroupMembers(this.config.group, options), // calls /groups/<groupId>/members
{
page: 1,
per_page: 100,
},
),
);
} else if (
this.gitLabClient.isSelfManaged() &&
@@ -387,35 +389,46 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
all_available: true,
},
);
users = paginated<GitLabUser>(
options => this.gitLabClient.listUsers(options), // calls /users?
{ page: 1, per_page: 100, active: true },
allUsers.push(
paginated<GitLabUser>(
options => this.gitLabClient.listUsers(options), // calls /users?
{ page: 1, per_page: 100, active: true },
),
);
}
// SaaS: Fetch the users from the defined group (restrictUsersToGroup) or fetch all users from the root group.
else {
groups = (await this.gitLabClient.listDescendantGroups(this.config.group))
.items;
const descendantGroups = (
await this.gitLabClient.listDescendantGroups(this.config.group)
).items;
groups = descendantGroups;
groups.push(await this.gitLabClient.getGroupByPath(this.config.group)); // adds the parent group for #26554
const rootGroupSplit = this.config.group.split('/');
const groupPath = this.config.restrictUsersToGroup
? this.config.group
: rootGroupSplit[0];
users = paginated<GitLabUser>(
options =>
this.gitLabClient.listSaaSUsers(
groupPath,
options,
this.config.includeUsersWithoutSeat,
const groupPaths = this.config.restrictUsersToGroup
? [this.config.group]
: [rootGroupSplit[0], ...descendantGroups.map(g => g.id)];
// Fetch users group and descendant groups
for (const group of groupPaths) {
logger.debug(`Fetching users for group: ${group}`);
allUsers.push(
paginated<GitLabUser>(
options =>
this.gitLabClient.listSaaSUsers(
String(group),
options,
this.config.includeUsersWithoutSeat,
),
{
page: 1,
per_page: 100,
},
),
{
page: 1,
per_page: 100,
},
);
);
}
}
const idMappedUser: { [userId: number]: GitLabUser } = {};
@@ -430,16 +443,23 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
matches: [],
};
for await (const user of users) {
userRes.scanned++;
for (const users of allUsers) {
// Iterate through paginated users
for await (const user of users) {
// Skip if user already processed and do not count it as scanned
if (user.id in idMappedUser) {
continue; // Skip if user already processed
}
userRes.scanned++;
if (!this.shouldProcessUser(user)) {
logger.debug(`Skipped user: ${user.username}`);
continue;
if (!this.shouldProcessUser(user)) {
logger.debug(`Skipped user: ${user.username}`);
continue;
}
idMappedUser[user.id] = user;
userRes.matches.push(user);
}
idMappedUser[user.id] = user;
userRes.matches.push(user);
}
for await (const group of groups) {