|
| 1 | +const core = require('@actions/core'); |
| 2 | +const github = require('@actions/github'); |
| 3 | + |
| 4 | +const perPage = 100; // Maximum allowed by GitHub API |
| 5 | + |
| 6 | +async function fetchArtifacts(octokit, repository, name) { |
| 7 | + const result = []; |
| 8 | + let page = 1; |
| 9 | + |
| 10 | + while (true) { |
| 11 | + const response = await octokit.rest.actions.listArtifactsForRepo({ |
| 12 | + owner: repository.split('/')[0], |
| 13 | + repo: repository.split('/')[1], |
| 14 | + name, |
| 15 | + per_page: perPage, |
| 16 | + page, |
| 17 | + }); |
| 18 | + |
| 19 | + const artifacts = response.data.artifacts; |
| 20 | + result.push(...artifacts); |
| 21 | + |
| 22 | + if (artifacts.length < perPage) { |
| 23 | + break; |
| 24 | + } |
| 25 | + |
| 26 | + page++; |
| 27 | + } |
| 28 | + |
| 29 | + result.sort((a, b) => new Date(b.expires_at) - new Date(a.expires_at)); |
| 30 | + return result; |
| 31 | +} |
| 32 | + |
| 33 | +function getPrNumber() { |
| 34 | + if (github.context.eventName === 'pull_request') { |
| 35 | + return github.context.payload.pull_request.number; |
| 36 | + } |
| 37 | + return undefined; |
| 38 | +} |
| 39 | + |
| 40 | +async function run() { |
| 41 | + try { |
| 42 | + const token = core.getInput('github-token'); |
| 43 | + const repository = core.getInput('repository'); |
| 44 | + const name = core.getInput('name'); |
| 45 | + const reSign = core.getInput('re-sign'); |
| 46 | + const prNumber = getPrNumber(); |
| 47 | + |
| 48 | + const octokit = github.getOctokit(token); |
| 49 | + const artifactsByName = await fetchArtifacts(octokit, repository, name); |
| 50 | + const artifactsByPrNumber = |
| 51 | + prNumber && reSign |
| 52 | + ? await fetchArtifacts(octokit, repository, `${name}-${prNumber}`) |
| 53 | + : []; |
| 54 | + const artifacts = [...artifactsByPrNumber, ...artifactsByName]; |
| 55 | + |
| 56 | + if (artifacts.length === 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + console.log(`Found ${artifacts.length} related artifacts:`); |
| 61 | + for (const artifact of artifacts) { |
| 62 | + console.log( |
| 63 | + `- ID: ${artifact.id}, Name: ${artifact.name}, Size: ${formatSize( |
| 64 | + artifact.size_in_bytes, |
| 65 | + )}, Expires at: ${artifact.expires_at}`, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + const firstArtifact = artifacts.find(artifact => !artifact.expired); |
| 70 | + console.log(`First artifact: ${JSON.stringify(firstArtifact, null, 2)}`); |
| 71 | + |
| 72 | + const url = formatDownloadUrl( |
| 73 | + repository, |
| 74 | + firstArtifact.workflow_run.id, |
| 75 | + firstArtifact.id, |
| 76 | + ); |
| 77 | + console.log('Stable download URL:', url); |
| 78 | + |
| 79 | + let artifactName = name; |
| 80 | + // There are artifacts from PR but the base artifact is gone, recreate with the original name |
| 81 | + if (artifactsByName.length === 0) { |
| 82 | + artifactName = name; |
| 83 | + // First time an artifact is re-signed, it's not yet in artifact storage, setting the name explicitly. |
| 84 | + } else if (prNumber && reSign) { |
| 85 | + artifactName = `${name}-${prNumber}`; |
| 86 | + } |
| 87 | + core.setOutput('artifact-name', artifactName); |
| 88 | + core.setOutput('artifact-id', firstArtifact.id); |
| 89 | + core.setOutput('artifact-url', url); |
| 90 | + core.setOutput( |
| 91 | + 'artifact-ids', |
| 92 | + artifactsByPrNumber.map(artifact => artifact.id).join(' '), |
| 93 | + ); |
| 94 | + } catch (error) { |
| 95 | + core.setFailed(`Action failed with error: ${error.message}`); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// The artifact URL returned by the GitHub API expires in 1 minute, we need to generate a permanent one. |
| 100 | +function formatDownloadUrl(repository, workflowRunId, artifactId) { |
| 101 | + return `https://github.com/${repository}/actions/runs/${workflowRunId}/artifacts/${artifactId}`; |
| 102 | +} |
| 103 | + |
| 104 | +function formatSize(size) { |
| 105 | + if (size > 0.75 * 1024 * 1024) { |
| 106 | + return `${(size / 1024 / 1024).toFixed(2)} MB`; |
| 107 | + } |
| 108 | + |
| 109 | + return `${(size / 1024).toFixed(2)} KB`; |
| 110 | +} |
| 111 | + |
| 112 | +run(); |
0 commit comments