Skip to content

Commit 57666bb

Browse files
committed
Add retry for finding result-index artifact
It seems like the result-index artifact may not be available immediately after the workflow run has finished. This adds a retry mechanism to wait for the result-index to be available. It will retry at most 10 times with a wait of 1 second between each retry.
1 parent ac74b96 commit 57666bb

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

extensions/ql-vscode/src/remote-queries/gh-actions-api-client.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export async function getRemoteQueryIndex(
4343
const workflowUri = `https://github.com/${owner}/${repoName}/actions/runs/${workflowRunId}`;
4444
const artifactsUrlPath = `/repos/${owner}/${repoName}/actions/artifacts`;
4545

46-
const artifactList = await listWorkflowRunArtifacts(credentials, owner, repoName, workflowRunId);
47-
const resultIndexArtifactId = tryGetArtifactIDfromName('result-index', artifactList);
46+
const [artifactList, resultIndexArtifactId] = await waitForArtifact(credentials, owner, repoName, workflowRunId, 'result-indx');
4847
if (!resultIndexArtifactId) {
4948
return undefined;
5049
}
@@ -255,6 +254,44 @@ function tryGetArtifactIDfromName(
255254
return artifact?.id;
256255
}
257256

257+
/**
258+
* Wait for an artifact to be available in a workflow run.
259+
* @param credentials Credentials for authenticating to the GitHub API.
260+
* @param owner
261+
* @param repo
262+
* @param workflowRunId The ID of the workflow run to get the artifact for.
263+
* @param artifactName The artifact name, as a string.
264+
* @param maxAttempts The maximum number of attempts to download the artifact.
265+
* @returns An array containing the full list of artifacts and the ID of the artifact with the given name.
266+
*/
267+
async function waitForArtifact(
268+
credentials: Credentials,
269+
owner: string,
270+
repo: string,
271+
workflowRunId: number,
272+
artifactName: string,
273+
maxAttempts = 10,
274+
intervalBetweenAttempts = 1000
275+
): Promise<[Awaited<ReturnType<typeof listWorkflowRunArtifacts>>, number | undefined]> {
276+
let attemptCount = 0;
277+
let artifactList: Awaited<ReturnType<typeof listWorkflowRunArtifacts>> = [];
278+
279+
while (attemptCount < maxAttempts) {
280+
artifactList = await listWorkflowRunArtifacts(credentials, owner, repo, workflowRunId);
281+
282+
const resultIndexArtifactId = tryGetArtifactIDfromName(artifactName, artifactList);
283+
if (resultIndexArtifactId) {
284+
return [artifactList, resultIndexArtifactId];
285+
}
286+
287+
await new Promise(resolve => setTimeout(resolve, intervalBetweenAttempts));
288+
289+
attemptCount++;
290+
}
291+
292+
return [artifactList, undefined];
293+
}
294+
258295
/**
259296
* Downloads an artifact from a workflow run.
260297
* @param credentials Credentials for authenticating to the GitHub API.

0 commit comments

Comments
 (0)