|
| 1 | +import * as core from '@actions/core' |
| 2 | +import {Octokit} from '@octokit/rest' |
| 3 | +import {getArtifactMetadata} from './git' |
| 4 | +import {spawn} from 'child_process' |
| 5 | +import * as fs from 'fs' |
| 6 | + |
| 7 | +async function sleep(milliseconds: number): Promise<void> { |
| 8 | + return new Promise<void>((resolve, _reject) => { |
| 9 | + setTimeout(resolve, milliseconds) |
| 10 | + }) |
| 11 | +} |
| 12 | + |
| 13 | +export async function getViaCIArtifacts( |
| 14 | + architecture: string, |
| 15 | + githubToken?: string |
| 16 | +): Promise<{ |
| 17 | + artifactName: string |
| 18 | + id: string |
| 19 | + download: ( |
| 20 | + outputDirectory: string, |
| 21 | + verbose?: number | boolean |
| 22 | + ) => Promise<void> |
| 23 | +}> { |
| 24 | + const owner = 'git-for-windows' |
| 25 | + |
| 26 | + const {repo, artifactName} = getArtifactMetadata('minimal', architecture) |
| 27 | + |
| 28 | + const octokit = githubToken ? new Octokit({auth: githubToken}) : new Octokit() |
| 29 | + |
| 30 | + const { |
| 31 | + name, |
| 32 | + updated_at: updatedAt, |
| 33 | + browser_download_url: url |
| 34 | + } = await (async () => { |
| 35 | + let error: Error | undefined |
| 36 | + for (const seconds of [0, 5, 10, 15, 20, 40]) { |
| 37 | + if (seconds) await sleep(seconds) |
| 38 | + |
| 39 | + const ciArtifactsResponse = await octokit.repos.getReleaseByTag({ |
| 40 | + owner, |
| 41 | + repo, |
| 42 | + tag: 'ci-artifacts' |
| 43 | + }) |
| 44 | + |
| 45 | + if (ciArtifactsResponse.status !== 200) { |
| 46 | + error = new Error( |
| 47 | + `Failed to get ci-artifacts release from the ${owner}/${repo} repo: ${ciArtifactsResponse.status}` |
| 48 | + ) |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + core.info( |
| 53 | + `Found ci-artifacts release: ${ciArtifactsResponse.data.html_url}` |
| 54 | + ) |
| 55 | + const tarGzArtifact = ciArtifactsResponse.data.assets.find(asset => |
| 56 | + asset.name.endsWith('.tar.gz') |
| 57 | + ) |
| 58 | + |
| 59 | + if (!tarGzArtifact) { |
| 60 | + error = new Error( |
| 61 | + `Failed to find a .tar.gz artifact in the ci-artifacts release of the ${owner}/${repo} repo` |
| 62 | + ) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + return tarGzArtifact |
| 67 | + } |
| 68 | + throw error |
| 69 | + })() |
| 70 | + core.info(`Found ${name} at ${url}`) |
| 71 | + |
| 72 | + return { |
| 73 | + artifactName, |
| 74 | + id: `ci-artifacts-${updatedAt}`, |
| 75 | + download: async ( |
| 76 | + outputDirectory: string, |
| 77 | + verbose: number | boolean = false |
| 78 | + ): Promise<void> => { |
| 79 | + return new Promise<void>((resolve, reject) => { |
| 80 | + const curl = spawn( |
| 81 | + `${process.env.SYSTEMROOT}/system32/curl.exe`, |
| 82 | + [ |
| 83 | + ...(githubToken |
| 84 | + ? ['-H', `Authorization: Bearer ${githubToken}`] |
| 85 | + : []), |
| 86 | + '-H', |
| 87 | + 'Accept: application/octet-stream', |
| 88 | + `-${verbose === true ? '' : 's'}fL`, |
| 89 | + url |
| 90 | + ], |
| 91 | + { |
| 92 | + stdio: ['ignore', 'pipe', process.stderr] |
| 93 | + } |
| 94 | + ) |
| 95 | + curl.on('error', error => reject(error)) |
| 96 | + |
| 97 | + fs.mkdirSync(outputDirectory, {recursive: true}) |
| 98 | + |
| 99 | + const tar = spawn( |
| 100 | + `${process.env.SYSTEMROOT}/system32/tar.exe`, |
| 101 | + ['-C', outputDirectory, `-x${verbose === true ? 'v' : ''}f`, '-'], |
| 102 | + {stdio: ['pipe', process.stdout, process.stderr]} |
| 103 | + ) |
| 104 | + tar.on('error', error => reject(error)) |
| 105 | + tar.on('close', code => { |
| 106 | + if (code === 0) resolve() |
| 107 | + else reject(new Error(`tar exited with code ${code}`)) |
| 108 | + }) |
| 109 | + |
| 110 | + curl.stdout.pipe(tar.stdin) |
| 111 | + }) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments