From 324760707f65a2f16ab1b10255e79ee27c85456b Mon Sep 17 00:00:00 2001 From: Ekaterina Semenova <109272254+adaagava@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:28:10 +0200 Subject: [PATCH] add delays and check responses --- src/aggregate.ts | 16 ++++++++++++++-- src/api.ts | 38 +++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/aggregate.ts b/src/aggregate.ts index 596b6ea..1a90461 100644 --- a/src/aggregate.ts +++ b/src/aggregate.ts @@ -83,6 +83,7 @@ async function getTestCaseData( ): Promise { const entries = await Promise.all( testCaseIdList.map(async (id): Promise<[number, TestCaseMeta][]> => { + await delay(50000); const custom_fields = await api.getTestCaseCustomFields(id); const map = customFieldsToMap(custom_fields); @@ -120,6 +121,12 @@ async function getTestCaseData( function customFieldsToMap( input: ApiTestCaseCustomFieldData[], ): Map { + if (!input) { + logger().warning({ + msg: `Missing test!` + }) + return new Map(null); + } else { const entries = input .map((x) => ({ id: x.customField.id, @@ -127,8 +134,13 @@ function customFieldsToMap( value: x.name, })) .map((x): [string, CustomFieldData] => [x.name, x]); + return new Map(entries); +} - return new Map(entries); +} + +function delay(ms: number) { + return new Promise( resolve => setTimeout(resolve, ms) ); } function aggregateStories( @@ -204,4 +216,4 @@ function pickResults( map.has(item.testCaseId) ? map : map.set(item.testCaseId, item), new Map(), ); -} +} \ No newline at end of file diff --git a/src/api.ts b/src/api.ts index 3a5b76b..ff17f0b 100644 --- a/src/api.ts +++ b/src/api.ts @@ -68,9 +68,7 @@ export default class Api { size: "999999", sort: "created_date,DESC", }); - - logger().debug({ msg: "Request", URL }); - + logger().debug({ msg: "Request", URL }); return fetch(URL, { headers: this.commonHeaders(), }) @@ -83,20 +81,34 @@ export default class Api { }); } - public getTestCaseCustomFields( + public async getTestCaseCustomFields( id: number, ): Promise { - return cacheData(`cache/test-case-${id}.json`, () => { - logger().debug({ msg: "Loading test case custom fields", id }); - + return cacheData(`cache/test-case-${id}.json`, () => { + logger().debug({ msg: "Loading test case custom fields", id }); return fetch(`${this.baseUrl}/api/rs/testcase/${id}/cfv`, { headers: this.commonHeaders(), }) - .then((x) => x.json()) - .then((x) => { - logger().debug({ msg: "Test case custom fields", id, data: x }); - return x; - }); + .then ((x) => { + if (x.status !== 200) { + logger().error({ msg: "Failed to load test case custom fields", id, status: x.status }); + } + return x.text(); + }) + .then((text) => { + try { + const json = JSON.parse(text); // Attempt to parse the text as JSON + logger().debug({ msg: "Test case custom fields", id, data: json }); + return json; + } catch (error) { + logger().error({ msg: "Invalid JSON response", id, error: error.message }); + return; + } + }) + .then((x) => { + logger().debug({ msg: "Test case custom fields", id, data: x }); + return x; + }); }); } @@ -121,4 +133,4 @@ async function cacheData(file: string, fn: () => Promise): Promise { logger().debug({ msg: "Written cache", file }); return data; } -} +} \ No newline at end of file