import { BaseResource } from '@gitbeaker/requester-utils'; import { RequestHelper, endpoint } from '../infrastructure'; import type { AllOrNone, AsAdmin, GitlabAPIResponse, PaginationRequestOptions, PaginationTypes, ShowExpanded, Sudo, } from '../infrastructure'; import type { ProjectSchema, ProjectStatisticsSchema, SimpleProjectSchema } from './Projects'; import type { AllEventOptions, EventSchema } from './Events'; import type { PersonalAccessTokenSchema } from './PersonalAccessTokens'; import type { CustomAttributeSchema } from '../templates/ResourceCustomAttributes'; import { AccessLevel } from '../constants'; export interface SimpleUserSchema extends Record { id: number; name: string; username: string; state: string; avatar_url: string; web_url: string; created_at: string; } export interface UserSchema extends SimpleUserSchema { locked: boolean | null; bio: string | null; bot: boolean | null; location: string | null; public_email: string | null; skype: string | null; linkedin: string | null; twitter: string | null; discord: string | null; website_url: string | null; pronouns: string | null; organization: string | null; job_title: string | null; work_information: string | null; followers: number | null; following: number | null; local_time: string | null; is_followed: boolean | null; } export interface ExpandedUserSchema extends UserSchema { is_admin: boolean | null; bot: boolean; last_sign_in_at: string; confirmed_at: string; last_activity_on: string; email: string; theme_id: number; color_scheme_id: number; projects_limit: number; current_sign_in_at: string | null; note: string | null; identities: { provider: string; extern_uid: string; saml_provider_id: number }[] | null; can_create_group: boolean; can_create_project: boolean; two_factor_enabled: boolean; external: boolean; private_profile: string | null; namespace_id: number | null; created_by: string | null; } export interface AdminUserSchema extends ExpandedUserSchema { current_sign_in_ip: string; last_sign_in_ip: string; using_license_seat: boolean | null; email_reset_offered_at: string | null; shared_runners_minutes_limit?: number | null; extra_shared_runners_minutes_limit?: number | null; is_auditor?: boolean | null; provisioned_by_group_id?: number | null; plan?: string; trial?: boolean; } export interface UserActivitySchema extends Record { username: string; last_activity_on: string; last_activity_at: string; } export interface UserStatusSchema extends Record { emoji: string; availability: string; message: string; message_html: string; clear_status_at: string; } export interface UserPreferenceSchema extends Record { id: number; user_id: number; view_diffs_file_by_file: boolean; show_whitespace_in_diffs: boolean; } export interface UserCountSchema extends Record { merge_requests: number; assigned_issues: number; assigned_merge_requests: number; review_requested_merge_requests: number; todos: number; } export interface UserAssociationCountSchema extends Record { groups_count: number; projects_count: number; issues_count: number; merge_requests_count: number; } export interface UserMembershipSchema extends Record { source_id: number; source_name: string; source_type: 'Project' | 'Namespace'; access_level: Exclude; } export interface UserRunnerSchema extends Record { id: number; token: string; token_expires_at: string | null; } export type AllUsersOptions = { orderBy?: 'name' | 'username' | 'created_at' | 'updated_at'; createdBy?: string; sort?: 'asc' | 'desc'; twoFactor?: string; withoutProjects?: boolean; admins?: boolean; samlProviderId?: number; skipLdap?: boolean; search?: string; username?: string; active?: boolean; blocked?: boolean; external?: boolean; excludeInternal?: boolean; excludeExternal?: boolean; withoutProjectBots?: boolean; createdBefore?: string; createdAfter?: string; withCustomAttributes?: boolean; customAttributes?: Record; } & AllOrNone<{ provider: string; externUid: string }>; export type CreateUserOptions = { admin?: boolean; auditor?: boolean; avatar?: { content: Blob; filename?: string }; bio?: string; canCreateGroup?: boolean; colorSchemeId?: number; commitEmail?: string; email?: string; externUid?: string; external?: boolean; extraSharedRunnersMinutesLimit?: number; forceRandomPassword?: boolean; groupIdForSaml?: number; linkedin?: string; location?: string; name?: string; note?: string; organization?: string; password?: string; privateProfile?: string; projectsLimit?: number; pronouns?: string; provider?: string; publicEmail?: string; resetPassword?: boolean; sharedRunnersMinutesLimit?: number; skipConfirmation?: boolean; skype?: string; themeId?: number; twitter?: string; discord?: string; username?: string; viewDiffsFileByFile?: boolean; websiteUrl?: string; }; export type EditUserOptions = CreateUserOptions; export type CreateUserCIRunnerOptions = { groupId?: number; projectId?: number; description?: string; paused?: boolean; locked?: boolean; runUntagged?: boolean; tagList?: string[]; accessLevel?: 'not_protected' | 'ref_protected'; maximumTimeout?: number; maintenanceNote?: string; }; export type AllUserProjectsOptions = { archived?: boolean; idAfter?: number; idBefore?: number; membership?: boolean; minAccessLevel?: Exclude; orderBy?: 'id' | 'name' | 'path' | 'created_at' | 'updated_at' | 'last_activity_at'; owned?: boolean; search?: string; simple?: boolean; sort?: 'asc' | 'desc'; starred?: boolean; statistics?: boolean; visibility?: 'public' | 'internal' | 'private'; withCustomAttributes?: boolean; withIssuesEnabled?: boolean; withMergeRequestsEnabled?: boolean; withProgrammingLanguage?: string; updatedBefore?: string; updatedAfter?: string; }; export class Users extends BaseResource { activate( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/activate`, options); } all( options?: { withCustomAttributes: true } & AsAdmin & AllUsersOptions & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise< GitlabAPIResponse< ((A extends false ? SimpleUserSchema : AdminUserSchema) & { custom_attributes: CustomAttributeSchema[]; })[], C, E, P > >; all( options?: AllUsersOptions & AsAdmin & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise>; all( options?: AllUsersOptions & AsAdmin & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get<(A extends false ? SimpleUserSchema : AdminUserSchema)[]>()( this, 'users', options, ); } allActivities( options?: { from?: string } & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()(this, 'user/activities', options); } allEvents( userId: number, options?: AllEventOptions & PaginationRequestOptions

& Sudo & ShowExpanded, ) { return RequestHelper.get()(this, endpoint`users/${userId}/events`, options); } allFollowers( userId: number, options?: PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}/followers`, options, ); } allFollowing( userId: number, options?: PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}/following`, options, ); } allMemberships( userId: number, options?: { type?: 'Project' | 'Namespace' } & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}/memberships`, options, ); } allProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { simple: true }, ): Promise>; allProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { statistics: true }, ): Promise< GitlabAPIResponse<(ProjectSchema & { statistics: ProjectStatisticsSchema })[], C, E, P> >; allProjects( userId: string | number, options?: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded, ): Promise>; allProjects( userId: string | number, options?: AllUserProjectsOptions & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()(this, endpoint`users/${userId}/projects`, options); } allContributedProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { simple: true }, ): Promise>; allContributedProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { statistics: true }, ): Promise< GitlabAPIResponse<(ProjectSchema & { statistics: ProjectStatisticsSchema })[], C, E, P> >; allContributedProjects( userId: string | number, options?: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded, ): Promise>; allContributedProjects( userId: string | number, options?: AllUserProjectsOptions & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}/contributed_projects`, options, ); } // Convenience method - Functionality already present in the all method in the Projects wrapper allStarredProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { simple: true }, ): Promise>; allStarredProjects( userId: string | number, options: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded & { statistics: true }, ): Promise< GitlabAPIResponse<(ProjectSchema & { statistics: ProjectStatisticsSchema })[], C, E, P> >; allStarredProjects( userId: string | number, options?: PaginationRequestOptions

& AllUserProjectsOptions & Sudo & ShowExpanded, ): Promise>; allStarredProjects( userId: string | number, options?: AllUserProjectsOptions & PaginationRequestOptions

& Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}/starred_projects`, options, ); } approve( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post<{ message: string }>()( this, endpoint`users/${userId}/approve`, options, ); } ban( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/ban`, options); } block( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/block`, options); } create( options?: CreateUserOptions & Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, 'users', options); } createPersonalAccessToken( userId: number, name: string, scopes: string[], options?: { expiresAt?: string } & Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()( this, endpoint`users/${userId}/personal_access_tokens`, { name, scopes, ...options, }, ); } createCIRunner( runnerType: 'instance_type' | 'group_type' | 'project_type', options?: CreateUserCIRunnerOptions & Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, 'user/runners', { ...options, runnerType, }); } deactivate( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/deactivate`, options); } disableTwoFactor( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.patch()(this, endpoint`users/${userId}/disable_two_factor`, options); } edit( userId: number, { avatar, ...options }: EditUserOptions & Sudo & ShowExpanded = {}, ) { const opts: Record = { ...options, isForm: true, }; if (avatar) opts.avatar = [avatar.content, avatar.filename]; return RequestHelper.put()(this, endpoint`users/${userId}`, opts); } editStatus( options?: { emoji?: string; message?: string; clearStatusAfter?: | '30_minutes' | '3_hours' | '8_hours' | '1_day' | '3_days' | '7_days' | '30_days'; } & Sudo & ShowExpanded, ): Promise> { return RequestHelper.put()(this, 'user/status', options); } editCurrentUserPreferences( viewDiffsFileByFile: boolean, showWhitespaceInDiffs: boolean, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.put()(this, 'user/preferences', { viewDiffsFileByFile, showWhitespaceInDiffs, ...options, }); } follow( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/follow`, options); } reject( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post<{ message: string }>()( this, endpoint`users/${userId}/reject`, options, ); } show( userId: number, options?: AsAdmin & Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`users/${userId}`, options, ); } showCount( options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()(this, 'user_counts', options); } showAssociationsCount( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, `users/${userId}/associations_count`, options, ); } showCurrentUser( options?: AsAdmin & Sudo & ShowExpanded, ): Promise< GitlabAPIResponse > { return RequestHelper.get()( this, 'user', options, ); } showCurrentUserPreferences( options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()(this, 'user/preferences', options); } showStatus({ iDOrUsername, ...options }: { iDOrUsername?: string | number } & Sudo & ShowExpanded = {}): Promise< GitlabAPIResponse > { let url: string; if (iDOrUsername) url = `users/${iDOrUsername}/status`; else url = 'user/status'; return RequestHelper.get()(this, url, options); } remove( userId: number, options?: { hardDelete?: boolean } & Sudo & ShowExpanded, ): Promise> { return RequestHelper.del()(this, endpoint`users/${userId}`, options); } removeAuthenticationIdentity( userId: number, provider: string, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.del()(this, endpoint`users/${userId}/identities/${provider}`, options); } unban( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/unban`, options); } unblock( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/unblock`, options); } unfollow( userId: number, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.post()(this, endpoint`users/${userId}/unfollow`, options); } }