Skip to content

Commit

Permalink
Merge pull request #1492 from github/koesie10/retry-artifacts
Browse files Browse the repository at this point in the history
Add retry for finding result-index artifact
  • Loading branch information
koesie10 committed Sep 5, 2022
2 parents 5067fbc + 80867e6 commit da90651
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
25 changes: 24 additions & 1 deletion extensions/ql-vscode/src/remote-queries/gh-actions-api-client.ts
Expand Up @@ -10,6 +10,8 @@ import { RemoteQuery } from './remote-query';
import { RemoteQueryFailureIndexItem, RemoteQueryResultIndex, RemoteQuerySuccessIndexItem } from './remote-query-result-index';
import { getErrorMessage } from '../pure/helpers-pure';

export const RESULT_INDEX_ARTIFACT_NAME = 'result-index';

interface ApiSuccessIndexItem {
nwo: string;
id: string;
Expand Down Expand Up @@ -44,7 +46,7 @@ export async function getRemoteQueryIndex(
const artifactsUrlPath = `/repos/${owner}/${repoName}/actions/artifacts`;

const artifactList = await listWorkflowRunArtifacts(credentials, owner, repoName, workflowRunId);
const resultIndexArtifactId = tryGetArtifactIDfromName('result-index', artifactList);
const resultIndexArtifactId = tryGetArtifactIDfromName(RESULT_INDEX_ARTIFACT_NAME, artifactList);
if (!resultIndexArtifactId) {
return undefined;
}
Expand Down Expand Up @@ -116,6 +118,27 @@ export async function downloadArtifactFromLink(
return path.join(extractedPath, downloadLink.innerFilePath || '');
}

/**
* Checks whether a specific artifact is present in the list of artifacts of a workflow run.
* @param credentials Credentials for authenticating to the GitHub API.
* @param owner
* @param repo
* @param workflowRunId The ID of the workflow run to get the artifact for.
* @param artifactName The artifact name, as a string.
* @returns A boolean indicating if the artifact is available.
*/
export async function isArtifactAvailable(
credentials: Credentials,
owner: string,
repo: string,
workflowRunId: number,
artifactName: string,
): Promise<boolean> {
const artifactList = await listWorkflowRunArtifacts(credentials, owner, repo, workflowRunId);

return tryGetArtifactIDfromName(artifactName, artifactList) !== undefined;
}

/**
* Downloads the result index artifact and extracts the result index items.
* @param credentials Credentials for authenticating to the GitHub API.
Expand Down
22 changes: 20 additions & 2 deletions extensions/ql-vscode/src/remote-queries/remote-queries-monitor.ts
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { Credentials } from '../authentication';
import { Logger } from '../logging';
import { getWorkflowStatus } from './gh-actions-api-client';
import { getWorkflowStatus, isArtifactAvailable, RESULT_INDEX_ARTIFACT_NAME } from './gh-actions-api-client';
import { RemoteQuery } from './remote-query';
import { RemoteQueryWorkflowResult } from './remote-query-workflow-result';

Expand Down Expand Up @@ -42,7 +42,25 @@ export class RemoteQueriesMonitor {
remoteQuery.controllerRepository.name,
remoteQuery.actionsWorkflowRunId);

if (workflowStatus.status !== 'InProgress') {
// Even if the workflow indicates it has completed, artifacts
// might still take a while to become available. So we need to
// check for the artifact before we can declare the workflow
// as having completed.
if (workflowStatus.status === 'CompletedSuccessfully') {
const resultIndexAvailable = await isArtifactAvailable(
credentials,
remoteQuery.controllerRepository.owner,
remoteQuery.controllerRepository.name,
remoteQuery.actionsWorkflowRunId,
RESULT_INDEX_ARTIFACT_NAME
);

if (resultIndexAvailable) {
return workflowStatus;
}

// We don't have a result-index yet, so we'll keep monitoring.
} else if (workflowStatus.status !== 'InProgress') {
return workflowStatus;
}

Expand Down

0 comments on commit da90651

Please sign in to comment.