From 4930413406fa05d07f9ea923c8a95b14580ec3be Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Wed, 25 Jan 2023 14:58:49 +0100 Subject: [PATCH] Gitlab Org Provider changes Signed-off-by: Dominik Pfaffenbauer --- docs/integrations/gitlab/org.md | 5 +++ .../src/lib/client.ts | 41 +++++++++++++++---- .../src/lib/types.ts | 4 +- .../GitlabOrgDiscoveryEntityProvider.ts | 17 ++++++-- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/docs/integrations/gitlab/org.md b/docs/integrations/gitlab/org.md index caf0851477..fc87f968d1 100644 --- a/docs/integrations/gitlab/org.md +++ b/docs/integrations/gitlab/org.md @@ -19,6 +19,11 @@ integrations: token: ${GITLAB_TOKEN} ``` +This will query all users and groups from your gitlab installation. Depending on the size +of the Gitlab Instance, this can take some time our resources. + +The token that is used for the Organization Integration, has to be an Admin PAT. + ```yaml catalog: providers: diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.ts index b86de7bf1d..bad0eb2a86 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.ts @@ -22,14 +22,22 @@ import { import { Logger } from 'winston'; import { GitLabGroup, GitLabMembership, GitLabUser } from './types'; -export type ListOptions = { +export type CommonListOptions = { [key: string]: string | number | boolean | undefined; - group?: string; per_page?: number | undefined; page?: number | undefined; active?: boolean; }; +interface ListProjectOptions extends CommonListOptions { + group?: string; +} + +interface UserListOptions extends CommonListOptions { + without_project_bots?: boolean | undefined; + exclude_internal?: boolean | undefined; +} + export type PagedResponse = { items: T[]; nextPage?: number; @@ -51,7 +59,9 @@ export class GitLabClient { return this.config.host !== 'gitlab.com'; } - async listProjects(options?: ListOptions): Promise> { + async listProjects( + options?: ListProjectOptions, + ): Promise> { if (options?.group) { return this.pagedRequest( `/groups/${encodeURIComponent(options?.group)}/projects`, @@ -65,11 +75,24 @@ export class GitLabClient { return this.pagedRequest(`/projects`, options); } - async listUsers(options?: ListOptions): Promise> { - return this.pagedRequest(`/users`, options); + async listUsers( + options?: UserListOptions, + ): Promise> { + let requestOptions = options; + + if (!requestOptions) { + requestOptions = {}; + } + + requestOptions.without_project_bots = true; + requestOptions.exclude_internal = true; + + return this.pagedRequest(`/users?`, requestOptions); } - async listGroups(options?: ListOptions): Promise> { + async listGroups( + options?: CommonListOptions, + ): Promise> { return this.pagedRequest(`/groups`, options); } @@ -150,7 +173,7 @@ export class GitLabClient { */ async pagedRequest( endpoint: string, - options?: ListOptions, + options?: CommonListOptions, ): Promise> { const request = new URL(`${this.config.apiBaseUrl}${endpoint}`); for (const key in options) { @@ -195,8 +218,8 @@ export class GitLabClient { * @param options - Initial ListOptions for the request function. */ export async function* paginated( - request: (options: ListOptions) => Promise>, - options: ListOptions, + request: (options: CommonListOptions) => Promise>, + options: CommonListOptions, ) { let res; do { diff --git a/plugins/catalog-backend-module-gitlab/src/lib/types.ts b/plugins/catalog-backend-module-gitlab/src/lib/types.ts index 97ea87ea98..2b2b888851 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/types.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/types.ts @@ -34,9 +34,9 @@ export type GitLabProject = { export type GitLabUser = { id: number; username: string; - name: string; email: string; - active: boolean; + name: string; + state: string; web_url: string; avatar_url: string; groups?: GitLabGroup[]; diff --git a/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts b/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts index df480de3cb..a66efc1369 100644 --- a/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts +++ b/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts @@ -206,13 +206,13 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { } for await (const user of users) { - if (!this.config.userPattern.test(user.email ?? '')) { + if (!this.config.userPattern.test(user.email ?? user.username ?? '')) { continue; } res.scanned++; - if (user.active) { + if (user.state !== 'active') { continue; } @@ -314,7 +314,6 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { }, spec: { profile: { - email: user.email, displayName: user.name, picture: user.avatar_url, }, @@ -322,6 +321,18 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { }, }; + if (user.email) { + if (!entity.spec) { + entity.spec = {}; + } + + if (!entity.spec.profile) { + entity.spec.profile = {}; + } + + entity.spec.profile.email = user.email; + } + if (user.groups) { for (const group of user.groups) { if (!entity.spec.memberOf) {