( identifier: string, headers: Record<string, string>, context: ExecutionContext, )
| 368 | } |
| 369 | |
| 370 | async function resolveLogin( |
| 371 | identifier: string, |
| 372 | headers: Record<string, string>, |
| 373 | context: ExecutionContext, |
| 374 | ): Promise<string> { |
| 375 | const trimmed = identifier.trim(); |
| 376 | if (trimmed.includes('@')) { |
| 377 | context.emitProgress('Resolving GitHub username from email...'); |
| 378 | const query = encodeURIComponent(`${trimmed} in:email`); |
| 379 | const searchResponse = await context.http.fetch( |
| 380 | `https://api.github.com/search/users?q=${query}&per_page=1`, |
| 381 | { |
| 382 | headers, |
| 383 | }, |
| 384 | ); |
| 385 | |
| 386 | if (!searchResponse.ok) { |
| 387 | const body = await safeReadText(searchResponse); |
| 388 | throw fromHttpResponse( |
| 389 | searchResponse, |
| 390 | `Failed to resolve GitHub username for ${trimmed}: ${body}`, |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | const payload = (await searchResponse.json()) as { |
| 395 | total_count: number; |
| 396 | items: { login: string }[]; |
| 397 | }; |
| 398 | |
| 399 | if (!payload.total_count || payload.items.length === 0) { |
| 400 | throw new NotFoundError( |
| 401 | `No public GitHub user found for email ${trimmed}. Provide a username instead.`, |
| 402 | { |
| 403 | resourceType: 'user', |
| 404 | resourceId: trimmed, |
| 405 | }, |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | const { login } = payload.items[0]; |
| 410 | context.logger.info(`[GitHub] Resolved email ${trimmed} to username ${login}.`); |
| 411 | return login; |
| 412 | } |
| 413 | |
| 414 | context.logger.info(`[GitHub] Using provided username ${trimmed}.`); |
| 415 | return trimmed; |
| 416 | } |
| 417 | |
| 418 | async function safeReadText(response: Response): Promise<string> { |
| 419 | try { |
no test coverage detected