diff --git a/src/lib/GitHubActionsRunner.ts b/src/lib/GitHubActionsRunner.ts index 693aae2..66f47a9 100644 --- a/src/lib/GitHubActionsRunner.ts +++ b/src/lib/GitHubActionsRunner.ts @@ -1,5 +1,5 @@ import { GithubApiClient } from './github/GithubApiClient'; -import { GithubApiManager } from './github/GithubApiManager'; +import { GithubApiManager, WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS } from './github/GithubApiManager'; import { logger, setLoggerLevel } from './utils/logger'; /** @@ -183,7 +183,12 @@ export class GitHubActionsRunner { const logs = await this.githubApiManager.fetchWorkflowRunLogs(workflowRun.id); logger.info(logs); + if (workflowRun.conclusion !== WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS) { + throw new Error(`Workflow run failed with conclusion "${workflowRun.conclusion}".`); + } + if (artifactsPath) { + // if no artifacts are found, the method will throw an error await this.githubApiManager.downloadArtifacts(workflowRun, artifactsPath); } } diff --git a/src/lib/github/GithubApiManager.ts b/src/lib/github/GithubApiManager.ts index db6f7a9..b732d45 100644 --- a/src/lib/github/GithubApiManager.ts +++ b/src/lib/github/GithubApiManager.ts @@ -20,6 +20,8 @@ type WorkflowRun = WorkflowRuns[number]; type Artifacts = RestEndpointMethodTypes['actions']['listWorkflowRunArtifacts']['response']['data']['artifacts']; type Artifact = Artifacts[number]; +export const WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS = 'success'; + /** * Statuses for a workflow run, indicating state of the workflow in the progress. */ @@ -439,11 +441,21 @@ export class GithubApiManager { * @param workflowRun The workflow run to download artifacts from. * @param artifactsPath The path to save the downloaded artifacts. * @returns A promise that resolves when all artifacts are downloaded. - * @throws An error if the download fails. + * @throws An error if the download fails or no artifacts are found. */ async downloadArtifacts(workflowRun: WorkflowRun, artifactsPath: string): Promise { logger.info('Downloading artifacts...'); + const artifactsList = await this.listWorkflowArtifacts(workflowRun.id); + + /** + * This method is called only when an artifacts path is provided, indicating that artifacts are expected. + * Consequently, if no artifacts are found, an error should be thrown. + */ + if (artifactsList.length === 0) { + throw new Error('No artifacts found'); + } + logger.info(`Artifacts found: ${artifactsList.map((artifact) => artifact.name).join(', ')}`); await Promise.all(artifactsList.map((artifact) => {