Gitlab Org Provider changes

Signed-off-by: Dominik Pfaffenbauer <dominik@pfaffenbauer.at>
This commit is contained in:
Dominik Pfaffenbauer
2023-01-25 14:58:49 +01:00
parent 3283ff18ec
commit 4930413406
4 changed files with 53 additions and 14 deletions
@@ -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<T> = {
items: T[];
nextPage?: number;
@@ -51,7 +59,9 @@ export class GitLabClient {
return this.config.host !== 'gitlab.com';
}
async listProjects(options?: ListOptions): Promise<PagedResponse<any>> {
async listProjects(
options?: ListProjectOptions,
): Promise<PagedResponse<any>> {
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<PagedResponse<GitLabUser>> {
return this.pagedRequest(`/users`, options);
async listUsers(
options?: UserListOptions,
): Promise<PagedResponse<GitLabUser>> {
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<PagedResponse<GitLabGroup>> {
async listGroups(
options?: CommonListOptions,
): Promise<PagedResponse<GitLabGroup>> {
return this.pagedRequest(`/groups`, options);
}
@@ -150,7 +173,7 @@ export class GitLabClient {
*/
async pagedRequest<T = any>(
endpoint: string,
options?: ListOptions,
options?: CommonListOptions,
): Promise<PagedResponse<T>> {
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<T = any>(
request: (options: ListOptions) => Promise<PagedResponse<T>>,
options: ListOptions,
request: (options: CommonListOptions) => Promise<PagedResponse<T>>,
options: CommonListOptions,
) {
let res;
do {
@@ -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[];
@@ -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) {