|
| 1 | +import logger from "../../logger.js"; |
| 2 | +import { TestStatus, FailedTestInfo, TestRun, TestDetails } from "./types.js"; |
| 3 | + |
| 4 | +export async function getTestIds( |
| 5 | + buildId: string, |
| 6 | + authString: string, |
| 7 | + status?: TestStatus, |
| 8 | +): Promise<FailedTestInfo[]> { |
| 9 | + const baseUrl = `https://api-automation.browserstack.com/ext/v1/builds/${buildId}/testRuns`; |
| 10 | + let url = status ? `${baseUrl}?test_statuses=${status}` : baseUrl; |
| 11 | + let allFailedTests: FailedTestInfo[] = []; |
| 12 | + let requestNumber = 0; |
| 13 | + |
| 14 | + // Construct Basic auth header |
| 15 | + const encodedCredentials = Buffer.from(authString).toString("base64"); |
| 16 | + const authHeader = `Basic ${encodedCredentials}`; |
| 17 | + |
| 18 | + try { |
| 19 | + while (true) { |
| 20 | + requestNumber++; |
| 21 | + |
| 22 | + const response = await fetch(url, { |
| 23 | + headers: { |
| 24 | + Authorization: authHeader, |
| 25 | + "Content-Type": "application/json", |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + if (!response.ok) { |
| 30 | + throw new Error( |
| 31 | + `Failed to fetch test runs: ${response.status} ${response.statusText}`, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + const data = (await response.json()) as TestRun; |
| 36 | + |
| 37 | + // Extract failed IDs from current page |
| 38 | + if (data.hierarchy && data.hierarchy.length > 0) { |
| 39 | + const currentFailedTests = extractFailedTestIds(data.hierarchy); |
| 40 | + allFailedTests = allFailedTests.concat(currentFailedTests); |
| 41 | + } |
| 42 | + |
| 43 | + // Check for pagination termination conditions |
| 44 | + if (!data.pagination?.has_next || !data.pagination.next_page) { |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + // Safety limit to prevent runaway requests |
| 49 | + if (requestNumber >= 5) { |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + // Prepare next request |
| 54 | + url = `${baseUrl}?next_page=${encodeURIComponent(data.pagination.next_page)}`; |
| 55 | + } |
| 56 | + |
| 57 | + // Return unique failed test IDs |
| 58 | + return allFailedTests; |
| 59 | + } catch (error) { |
| 60 | + logger.error("Error fetching failed tests:", error); |
| 61 | + throw error; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Recursive function to extract failed test IDs from hierarchy |
| 66 | +function extractFailedTestIds(hierarchy: TestDetails[]): FailedTestInfo[] { |
| 67 | + let failedTests: FailedTestInfo[] = []; |
| 68 | + |
| 69 | + for (const node of hierarchy) { |
| 70 | + if (node.details?.status === "failed" && node.details?.run_count) { |
| 71 | + if (node.details?.observability_url) { |
| 72 | + const idMatch = node.details.observability_url.match(/details=(\d+)/); |
| 73 | + if (idMatch) { |
| 74 | + failedTests.push({ |
| 75 | + test_id: idMatch[1], |
| 76 | + test_name: node.display_name || `Test ${idMatch[1]}`, |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (node.children && node.children.length > 0) { |
| 83 | + failedTests = failedTests.concat(extractFailedTestIds(node.children)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return failedTests; |
| 88 | +} |
0 commit comments