Skip to content

Commit

Permalink
Merge pull request #6 from soramitsu/fix_with-sign_commit
Browse files Browse the repository at this point in the history
add delays and check responses
  • Loading branch information
adaagava authored Jul 9, 2024
2 parents 42cef4c + 3247607 commit 1c34209
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
16 changes: 14 additions & 2 deletions src/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async function getTestCaseData(
): Promise<MetaMap> {
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);

Expand Down Expand Up @@ -120,15 +121,26 @@ async function getTestCaseData(
function customFieldsToMap(
input: ApiTestCaseCustomFieldData[],
): Map<string, CustomFieldData> {
if (!input) {
logger().warning({
msg: `Missing test!`
})
return new Map(null);
} else {
const entries = input
.map((x) => ({
id: x.customField.id,
name: x.customField.name,
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(
Expand Down Expand Up @@ -204,4 +216,4 @@ function pickResults<T extends ApiTestCaseResult>(
map.has(item.testCaseId) ? map : map.set(item.testCaseId, item),
new Map<number, T>(),
);
}
}
38 changes: 25 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand All @@ -83,20 +81,34 @@ export default class Api {
});
}

public getTestCaseCustomFields(
public async getTestCaseCustomFields(
id: number,
): Promise<ApiTestCaseCustomFieldData[]> {
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;
});
});
}

Expand All @@ -121,4 +133,4 @@ async function cacheData<T>(file: string, fn: () => Promise<T>): Promise<T> {
logger().debug({ msg: "Written cache", file });
return data;
}
}
}

0 comments on commit 1c34209

Please sign in to comment.