diff options
| author | eric sciple <ericsciple@users.noreply.github.com> | 2019-12-10 02:52:19 -0500 |
|---|---|---|
| committer | eric sciple <ericsciple@users.noreply.github.com> | 2019-12-10 02:52:19 -0500 |
| commit | 4a3fee4df21eec578110d41563552655eff7f0db (patch) | |
| tree | 5f191024439d5194cb18282e7b7e2cab5fbc9c59 /src/github-api-helper.ts | |
| parent | 677b5f0e9b1033f0c1689030f47a1c2222fb74dc (diff) | |
.
Diffstat (limited to 'src/github-api-helper.ts')
| -rw-r--r-- | src/github-api-helper.ts | 202 |
1 files changed, 126 insertions, 76 deletions
diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 2b13bcb..0be91f2 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -28,38 +28,52 @@ export async function downloadRepository( const runnerTemp = process.env['RUNNER_TEMP'] as string assert.ok(runnerTemp, 'RUNNER_TEMP not defined') const archivePath = path.join(runnerTemp, 'checkout.tar.gz') - // await fs.promises.writeFile(archivePath, raw) - // Get the archive URL using the REST API - await retryHelper.execute(async () => { - // Prepare the archive stream - core.debug(`Preparing the archive stream: ${archivePath}`) - await io.rmRF(archivePath) - const fileStream = fs.createWriteStream(archivePath) - const fileStreamClosed = getFileClosedPromise(fileStream) - - try { - // Get the archive URL - core.info('Getting archive URL') - const archiveUrl = await getArchiveUrl( - accessToken, - owner, - repo, - ref, - commit - ) - - // Download the archive - core.info('Downloading the archive') // Do not print the archive URL because it has an embedded token - await downloadFile(archiveUrl, fileStream) - } finally { - fileStream.end() - await fileStreamClosed - } - - // return Buffer.from(response.data) // response.data is ArrayBuffer + // Ensure file does not exist + core.debug(`Ensuring archive file does not exist: ${archivePath}`) + await io.rmRF(archivePath) + + // Download the archive + let archiveData = await retryHelper.execute(async () => { + core.info('Downloading the archive using the REST API') + await await downloadArchive(accessToken, owner, repo, ref, commit) }) + // Write archive to disk + core.info('Writing archive to disk') + await fs.promises.writeFile(archivePath, archiveData) + archiveData = undefined + + // // Get the archive URL using the REST API + // await retryHelper.execute(async () => { + // // Prepare the archive stream + // core.debug(`Preparing the archive stream: ${archivePath}`) + // await io.rmRF(archivePath) + // const fileStream = fs.createWriteStream(archivePath) + // const fileStreamClosed = getFileClosedPromise(fileStream) + + // try { + // // Get the archive URL + // core.info('Getting archive URL') + // const archiveUrl = await getArchiveUrl( + // accessToken, + // owner, + // repo, + // ref, + // commit + // ) + + // // Download the archive + // core.info('Downloading the archive') // Do not print the archive URL because it has an embedded token + // await downloadFile(archiveUrl, fileStream) + // } finally { + // fileStream.end() + // await fileStreamClosed + // } + // }) + + // return Buffer.from(response.data) // response.data is ArrayBuffer + // // Download the archive // core.info('Downloading the archive') // Do not print the URL since it contains a token to download the archive // await downloadFile(archiveUrl, archivePath) @@ -120,16 +134,15 @@ export async function downloadRepository( } as ExecOptions) } -async function getArchiveUrl( +async function downloadArchive( accessToken: string, owner: string, repo: string, ref: string, commit: string -): Promise<string> { +): Promise<Buffer> { const octokit = new github.GitHub(accessToken) - const params: RequestOptions & ReposGetArchiveLinkParams = { - method: 'HEAD', + const params: ReposGetArchiveLinkParams = { owner: owner, repo: repo, archive_format: IS_WINDOWS ? 'zipball' : 'tarball', @@ -139,54 +152,91 @@ async function getArchiveUrl( console.log('GOT THE RESPONSE') console.log(`status=${response.status}`) console.log(`headers=${JSON.stringify(response.headers)}`) - console.log(`headers=${JSON.stringify(response.data)}`) + console.log(`data=${JSON.stringify(response.data)}`) if (response.status != 200) { throw new Error( `Unexpected response from GitHub API. Status: '${response.status}'` ) } - console.log('GETTING THE LOCATION') - const archiveUrl = response.headers['Location'] // Do not print the archive URL because it has an embedded token - assert.ok( - archiveUrl, - `Expected GitHub API response to contain 'Location' header` - ) - return archiveUrl -} -function downloadFile(url: string, fileStream: WriteStream): Promise<void> { - return new Promise((resolve, reject) => { - try { - https.get(url, (response: IncomingMessage) => { - if (response.statusCode != 200) { - reject(`Request failed with status '${response.statusCode}'`) - response.resume() // Consume response data to free up memory - return - } - - response.on('data', chunk => { - fileStream.write(chunk) - }) - response.on('end', () => { - resolve() - }) - response.on('error', err => { - reject(err) - }) - }) - } catch (err) { - reject(err) - } - }) -} + return Buffer.from(response.data) // response.data is ArrayBuffer -function getFileClosedPromise(stream: WriteStream): Promise<void> { - return new Promise((resolve, reject) => { - stream.on('error', err => { - reject(err) - }) - stream.on('finish', () => { - resolve() - }) - }) + // console.log('GETTING THE LOCATION') + // const archiveUrl = response.headers['Location'] // Do not print the archive URL because it has an embedded token + // assert.ok( + // archiveUrl, + // `Expected GitHub API response to contain 'Location' header` + // ) + // return archiveUrl } + +// async function getArchiveUrl( +// accessToken: string, +// owner: string, +// repo: string, +// ref: string, +// commit: string +// ): Promise<string> { +// const octokit = new github.GitHub(accessToken) +// const params: RequestOptions & ReposGetArchiveLinkParams = { +// method: 'HEAD', +// owner: owner, +// repo: repo, +// archive_format: IS_WINDOWS ? 'zipball' : 'tarball', +// ref: refHelper.getDownloadRef(ref, commit) +// } +// const response = await octokit.repos.getArchiveLink(params) +// console.log('GOT THE RESPONSE') +// console.log(`status=${response.status}`) +// console.log(`headers=${JSON.stringify(response.headers)}`) +// console.log(`data=${JSON.stringify(response.data)}`) +// if (response.status != 200) { +// throw new Error( +// `Unexpected response from GitHub API. Status: '${response.status}'` +// ) +// } +// console.log('GETTING THE LOCATION') +// const archiveUrl = response.headers['Location'] // Do not print the archive URL because it has an embedded token +// assert.ok( +// archiveUrl, +// `Expected GitHub API response to contain 'Location' header` +// ) +// return archiveUrl +// } + +// function downloadFile(url: string, fileStream: WriteStream): Promise<void> { +// return new Promise((resolve, reject) => { +// try { +// https.get(url, (response: IncomingMessage) => { +// if (response.statusCode != 200) { +// reject(`Request failed with status '${response.statusCode}'`) +// response.resume() // Consume response data to free up memory +// return +// } + +// response.on('data', chunk => { +// fileStream.write(chunk) +// }) +// response.on('end', () => { +// resolve() +// }) +// response.on('error', err => { +// reject(err) +// }) +// }) +// } catch (err) { +// reject(err) +// } +// }) +// } + +// function getFileClosedPromise(stream: WriteStream): Promise<void> { +// return new Promise((resolve, reject) => { +// stream.on('error', err => { +// reject(err) +// }) +// stream.on('finish', () => { +// resolve() +// }) +// }) +// } |
