Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/_internalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ async function fetchOk(input: RequestInfo | URL, init?: RequestInit): Promise<Re
return response;
}

export async function getJSON<T>(input: RequestInfo | URL, init?: RequestInit, backoff: number[] = HTTP_BACKOFF): Promise<T> {
export async function getJSON<T>(input: RequestInfo | URL, init?: RequestInit, backoff: number[] = HTTP_BACKOFF): Promise<T | undefined> {
return await retryWithBackoff(async () => {
const response = await fetchOk(input, init);
return await response.json() as T;
const text = await response.text();
return text.trim() ? JSON.parse(text) as T : undefined;
Comment thread
aslushnikov marked this conversation as resolved.
Resolved
}, backoff);
}

Expand Down
10 changes: 8 additions & 2 deletions src/fetchTestDurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,14 @@ class TestDurationsFetcher {
// would reject an empty/garbage bearer token as UNAUTHORIZED.
if (token)
headers['Authorization'] = `Bearer ${token}`;
return await getJSON<OUTPUT>(url, {
const result = await getJSON<OUTPUT>(url, {
method: 'POST',
headers,
body: body ? JSON.stringify(body) : undefined,
});
if (result === undefined)
throw new Error(`Empty JSON response from ${url}`);
return result;
}

async fetch(): Promise<FlakinessReport.Report> {
Expand Down Expand Up @@ -174,7 +177,10 @@ class TestDurationsFetcher {
createResponse.testDurationsToken,
);

return await getJSON<FlakinessReport.Report>(submitResponse.downloadUrl, undefined, DOWNLOAD_BACKOFF);
const result = await getJSON<FlakinessReport.Report>(submitResponse.downloadUrl, undefined, DOWNLOAD_BACKOFF);
if (result === undefined)
throw new Error(`Empty JSON response from ${submitResponse.downloadUrl}`);
return result;
}

private async _uploadReport(data: string, uploadUrl: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/githubOIDC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class GithubOIDC {
const url = new URL(this._requestUrl);
url.searchParams.set('audience', flakinessProject);

let json: { value?: string };
let json: { value?: string } | undefined;
try {
json = await getJSON<{ value?: string }>(url, {
headers: {
Expand All @@ -70,7 +70,7 @@ export class GithubOIDC {
throw new Error(`Failed to request GitHub OIDC token: ${error.message || String(error)}`);
}

if (!json.value)
if (!json?.value)
throw new Error('GitHub OIDC token response did not contain a token value.');

return json.value;
Expand Down