diff --git a/src/dependency-graph.ts b/src/dependency-graph.ts index d6978201..34383ce8 100644 --- a/src/dependency-graph.ts +++ b/src/dependency-graph.ts @@ -1,7 +1,6 @@ import * as core from '@actions/core' import * as github from '@actions/github' import * as glob from '@actions/glob' -import * as toolCache from '@actions/tool-cache' import {DefaultArtifactClient} from '@actions/artifact' import {GitHub} from '@actions/github/lib/utils' import {RequestError} from '@octokit/request-error' @@ -13,7 +12,7 @@ import fs from 'fs' import * as layout from './repository-layout' import {DependencyGraphOption, getJobMatrix, getArtifactRetentionDays} from './input-params' -const DEPENDENCY_GRAPH_ARTIFACT = 'dependency-graph' +const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_' export async function setup(option: DependencyGraphOption): Promise { if (option === DependencyGraphOption.Disabled) { @@ -68,7 +67,7 @@ async function uploadDependencyGraphs(dependencyGraphFiles: string[]): Promise { - const workspaceDirectory = layout.workspaceDirectory() try { - await submitDependencyGraphs(await retrieveDependencyGraphs(workspaceDirectory)) + await submitDependencyGraphs(await downloadDependencyGraphs()) } catch (e) { core.warning(`Download and submit dependency graph failed. Will continue. ${String(e)}`) } @@ -123,66 +121,32 @@ async function submitDependencyGraphFile(jsonFile: string): Promise { core.notice(`Submitted ${relativeJsonFile}: ${response.data.message}`) } -async function retrieveDependencyGraphs(workspaceDirectory: string): Promise { - if (github.context.payload.workflow_run) { - return await retrieveDependencyGraphsForWorkflowRun(github.context.payload.workflow_run.id, workspaceDirectory) - } - return retrieveDependencyGraphsForCurrentWorkflow(workspaceDirectory) -} - -async function retrieveDependencyGraphsForWorkflowRun(runId: number, workspaceDirectory: string): Promise { - const octokit = getOctokit() - - // Find the workflow run artifacts named "dependency-graph" - const artifacts = await octokit.rest.actions.listWorkflowRunArtifacts({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - run_id: runId - }) - - const matchArtifact = artifacts.data.artifacts.find(candidate => { - return candidate.name === DEPENDENCY_GRAPH_ARTIFACT - }) - - if (matchArtifact === undefined) { - throw new Error(`Dependency graph artifact not found. Has it been generated by workflow run '${runId}'?`) - } - - // Download the dependency-graph artifact - const download = await octokit.rest.actions.downloadArtifact({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - artifact_id: matchArtifact.id, - archive_format: 'zip' - }) - - const downloadBuffer = download.data as ArrayBuffer - const downloadZip = path.resolve(workspaceDirectory, 'dependency-graph.zip') - fs.writeFileSync(downloadZip, Buffer.from(downloadBuffer)) - - // Expance the dependency-graph zip and locate each dependency-graph JSON file - const extractDir = path.resolve(workspaceDirectory, 'dependency-graph') - const extracted = await toolCache.extractZip(downloadZip, extractDir) - core.info(`Extracted dependency graph artifacts to ${extracted}: ${fs.readdirSync(extracted)}`) +async function downloadDependencyGraphs(): Promise { + const workspaceDirectory = layout.workspaceDirectory() - return findDependencyGraphFiles(extracted) -} + const findBy = github.context.payload.workflow_run + ? { + token: getGithubToken(), + workflowRunId: github.context.payload.workflow_run.id, + repositoryName: github.context.repo.repo, + repositoryOwner: github.context.repo.owner + } + : undefined -async function retrieveDependencyGraphsForCurrentWorkflow(workspaceDirectory: string): Promise { const artifactClient = new DefaultArtifactClient() const downloadPath = path.resolve(workspaceDirectory, 'dependency-graph') const dependencyGraphArtifacts = ( await artifactClient.listArtifacts({ - latest: true + latest: true, + findBy }) - ).artifacts.filter(candidate => { - return candidate.name.startsWith('dependency-graph_') - }) + ).artifacts.filter(candidate => candidate.name.startsWith(DEPENDENCY_GRAPH_PREFIX)) for (const artifact of dependencyGraphArtifacts) { const downloadedArtifact = await artifactClient.downloadArtifact(artifact.id, { - path: downloadPath + path: downloadPath, + findBy }) core.info(`Downloading dependency-graph artifact ${artifact.name} to ${downloadedArtifact.downloadPath}`) }