Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 20x 20x 20x 20x 20x 20x 19x 20x 32x 20x 27x 27x 2x 25x 14x 14x 11x 11x 20x 20x 20x 20x 72x 72x 207x 72x 71x 1x 70x 70x 1x 69x 20x 33x 33x 20x 13x 13x 13x 13x 13x 13x 13x 54x 2x 2x 52x 4117x 5x 2x 3x 50x 4115x 10x 50x 11x 11x 11x 18x 20x 13x 13x 13x 13x 1x 1x 9x 9x 3x 3x 13x 13x 18x 16x 16x 16x 16x 13x 13x 11x 4x 2x 2x 3x 3x 3x | import * as z from 'zod';
import { enhancedFetch } from './fetch';
import { transliterate } from 'transliteration';
import { instanceAtLeast } from '../entities/instance-version';
import { GITLAB_DEFAULT_PER_PAGE, GITLAB_MAX_PER_PAGE } from '../entities/utils';
/**
* User query type detected by pattern analysis
*/
export type QueryType = 'email' | 'username' | 'name';
/**
* Pattern detection result
*/
export interface QueryPattern {
type: QueryType;
hasTransliteration: boolean;
originalQuery: string;
transliteratedQuery?: string;
}
/**
* Parameters for GitLab Users API
*/
export interface UserSearchParams {
username?: string;
public_email?: string;
search?: string;
active?: boolean;
humans?: boolean;
without_project_bots?: boolean;
[key: string]: unknown;
}
/**
* Search result with metadata
*/
export interface SmartSearchResult {
users: unknown[];
searchMetadata: {
query: string;
pattern: QueryPattern;
searchPhases: Array<{
phase: string;
params: UserSearchParams;
resultCount: number;
}>;
totalApiCalls: number;
/** Set when the returned users may not fully match the requested filters. */
warning?: string;
};
}
/**
* Transliterate non-Latin text to Latin characters
*/
export function transliterateText(text: string): string {
return transliterate(text);
}
/**
* Detect if text contains non-Latin characters that would benefit from transliteration
*/
export function hasNonLatin(text: string): boolean {
// Check for any non-Latin characters (excluding common punctuation and numbers)
// eslint-disable-next-line no-control-regex
return /[^\u0000-\u007F\u0080-\u00FF]/.test(text);
}
/**
* Analyze query pattern to determine optimal search strategy
*/
export function analyzeQuery(query: string): QueryPattern {
const trimmedQuery = query.trim();
// Email pattern: basic validation for @domain format
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmedQuery)) {
return {
type: 'email',
hasTransliteration: false,
originalQuery: trimmedQuery,
};
}
// Username pattern: 3-30 chars, basic chars with ._- (no spaces)
// Allow international characters but detect them for transliteration
if (trimmedQuery.length >= 3 && trimmedQuery.length <= 30 && !/\s/.test(trimmedQuery)) {
const hasTransliterationNeeded = hasNonLatin(trimmedQuery);
return {
type: 'username',
hasTransliteration: hasTransliterationNeeded,
originalQuery: trimmedQuery,
transliteratedQuery: hasTransliterationNeeded ? transliterateText(trimmedQuery) : undefined,
};
}
// Name pattern: everything else (contains spaces, long text, or anything not matching username)
const hasTransliterationNeeded = hasNonLatin(trimmedQuery);
return {
type: 'name',
hasTransliteration: hasTransliterationNeeded,
originalQuery: trimmedQuery,
transliteratedQuery: hasTransliterationNeeded ? transliterateText(trimmedQuery) : undefined,
};
}
const ListedUsersSchema = z.array(
z.looseObject({
state: z.string().optional(),
/** Exposed only in the full user entity (administrators); absent otherwise. */
bot: z.boolean().optional(),
}),
);
/** Users from GET /users, and why they may not fully match the requested filters. */
export interface FetchedUsers {
users: unknown[];
warning?: string;
}
const PARTIAL_HUMANS_WARNING =
'Filtered to humans without the bot flag (GitLab before 17.3 shows it only to administrators): bot accounts other than project bots may be included';
/** Pages of /users one call may scan while emulating filters, bounding its requests. */
const MAX_EMULATED_PAGES = 20;
const TRUNCATED_WARNING = `Filtered client-side (GitLab before 17.3) and only the first ${MAX_EMULATED_PAGES * GITLAB_MAX_PER_PAGE} users were scanned: later matches may be missing; narrow the search to reach them`;
/** One validated GET /users page. */
async function fetchUsersPage(query: Record<string, unknown>) {
const queryParams = new URLSearchParams();
Object.entries(query).forEach(([key, value]) => {
if (value !== undefined) queryParams.set(key, String(value));
});
const response = await enhancedFetch(`${process.env.GITLAB_API_URL}/api/v4/users?${queryParams}`);
if (!response.ok) {
throw new Error(`GitLab API error: ${response.status} ${response.statusText}`);
}
const parsed = ListedUsersSchema.safeParse(await response.json());
if (!parsed.success) {
throw new Error(
`GitLab API error: unexpected users response (${parsed.error.issues[0]?.message ?? 'invalid'})`,
);
}
return parsed.data;
}
/**
* GET /users with the user-type filters GitLab added in 17.3 (humans,
* exclude_humans, exclude_active). Older instances ignore them, so there they
* are emulated before pagination: GitLab's pages are walked until the requested
* filtered page is complete. exclude_active checks each user's state; humans
* excludes project bots server-side and any user flagged as a bot, but the bot
* flag reaches administrators only, so without it the result carries a warning.
* exclude_humans cannot work without the flag and is refused. The walk stops
* after MAX_EMULATED_PAGES, with a warning that later matches may be missing.
*/
export async function fetchUsers(params: Record<string, unknown>): Promise<FetchedUsers> {
const { humans, exclude_humans, exclude_active, page, per_page, ...filters } = params;
if (instanceAtLeast('17.3') || !(humans || exclude_humans || exclude_active)) {
return { users: await fetchUsersPage(params) };
}
const perPage = typeof per_page === 'number' ? per_page : GITLAB_DEFAULT_PER_PAGE;
const wanted = (typeof page === 'number' ? page : 1) * perPage;
const serverQuery = { ...filters, ...(humans ? { without_project_bots: true } : {}) };
const matches: unknown[] = [];
let botFlagMissing = false;
let truncated = false;
for (let serverPage = 1; matches.length < wanted; serverPage++) {
if (serverPage > MAX_EMULATED_PAGES) {
truncated = true;
break;
}
const batch = await fetchUsersPage({
...serverQuery,
per_page: GITLAB_MAX_PER_PAGE,
page: serverPage,
});
if (batch.some((user) => user.bot === undefined)) {
if (exclude_humans) {
throw new Error(
'Filtering to bot users needs GitLab 17.3+, or an administrator token on older instances',
);
}
botFlagMissing = true;
}
for (const user of batch) {
if (
!(humans && user.bot === true) &&
!(exclude_humans && user.bot === false) &&
!(exclude_active && user.state === 'active')
) {
matches.push(user);
}
}
if (batch.length < GITLAB_MAX_PER_PAGE) break;
}
const users = matches.slice(wanted - perPage, wanted);
const warnings = [
...(humans && botFlagMissing ? [PARTIAL_HUMANS_WARNING] : []),
...(truncated ? [TRUNCATED_WARNING] : []),
];
return warnings.length > 0 ? { users, warning: warnings.join('; ') } : { users };
}
/**
* Make GitLab Users API call with given parameters
*/
async function callUsersAPI(params: UserSearchParams): Promise<FetchedUsers> {
// Default to active humans, unless the caller excludes exactly those: the
// default and the exclusion together can only return nothing.
return fetchUsers({
...(params.exclude_active ? {} : { active: true }),
...(params.exclude_humans ? {} : { humans: true }),
...params,
});
}
/**
* Smart user search with pattern detection and fallback strategies
*/
export async function smartUserSearch(
query: string,
additionalParams: Partial<UserSearchParams> = {},
): Promise<SmartSearchResult> {
const pattern = analyzeQuery(query);
const searchPhases: Array<{ phase: string; params: UserSearchParams; resultCount: number }> = [];
let totalApiCalls = 0;
// Phase 1: Targeted search based on detected pattern
let targetParams: UserSearchParams;
switch (pattern.type) {
case 'email':
targetParams = { public_email: pattern.originalQuery, ...additionalParams };
break;
case 'username':
targetParams = { username: pattern.originalQuery, ...additionalParams };
break;
case 'name':
targetParams = { search: pattern.originalQuery, ...additionalParams };
break;
}
// A failed call propagates: an empty result would claim nobody matched.
// Warnings of every phase run are kept: an earlier phase that scanned only
// part of the instance still qualifies an empty final answer.
const warnings = new Set<string>();
const runPhase = async (phase: string, params: UserSearchParams): Promise<FetchedUsers> => {
const fetched = await callUsersAPI(params);
totalApiCalls++;
searchPhases.push({ phase, params, resultCount: fetched.users.length });
if (fetched.warning) warnings.add(fetched.warning);
return fetched;
};
const finish = ({ users }: FetchedUsers): SmartSearchResult => ({
users,
searchMetadata: {
query,
pattern,
searchPhases,
totalApiCalls,
...(warnings.size > 0 ? { warning: [...warnings].join('; ') } : {}),
},
});
let fetched = await runPhase(`targeted-${pattern.type}`, targetParams);
if (fetched.users.length > 0) return finish(fetched);
// Phase 2: Broad search fallback if targeted search returned empty
if (pattern.type !== 'name') {
fetched = await runPhase('broad-search', {
search: pattern.originalQuery,
...additionalParams,
});
if (fetched.users.length > 0) return finish(fetched);
}
// Phase 3: Transliteration search if query has Cyrillic and no results yet
Eif (pattern.hasTransliteration && pattern.transliteratedQuery) {
fetched = await runPhase('transliteration', {
search: pattern.transliteratedQuery,
...additionalParams,
});
}
return finish(fetched);
}
|