From eed20d30d59d982721aded3dbe61fde387a2f005 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 01:53:12 -0500 Subject: [PATCH 01/10] . --- dist/index.js | 1 + src/github-api-helper.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index bad1e73..0ded637 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8392,6 +8392,7 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat }; const response = yield octokit.repos.getArchiveLink(params); console.log('GOT THE RESPONSE'); + console.log(response.status); if (response.status != 302) { throw new Error(`Unexpected response from GitHub API. Status: '${response.status}'`); } diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index f4aa8a8..764156c 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -52,6 +52,7 @@ export async function downloadRepository( } const response = await octokit.repos.getArchiveLink(params) console.log('GOT THE RESPONSE') + console.log(response.status) if (response.status != 302) { throw new Error( `Unexpected response from GitHub API. Status: '${response.status}'` From 7a2b445a4b9c1818428cec57d8b07aab4fddef95 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 01:53:49 -0500 Subject: [PATCH 02/10] . --- dist/index.js | 1 + src/github-api-helper.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index 0ded637..25b27e7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8404,6 +8404,7 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat yield downloadFile(archiveUrl, fileStream); } finally { + fileStream.end(); yield fileStreamClosed; } // return Buffer.from(response.data) // response.data is ArrayBuffer diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 764156c..37a467d 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -69,6 +69,7 @@ export async function downloadRepository( 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 } From 154a05918b057eb6a3095b1643c5ed51f4f973b3 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:01:59 -0500 Subject: [PATCH 03/10] . --- dist/index.js | 1 - src/github-api-helper.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 25b27e7..42064d3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8477,7 +8477,6 @@ function downloadFile(url, fileStream) { response.on('error', err => { reject(err); }); - // response.pipe(fileStream) }); } catch (err) { diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 37a467d..e05e1e1 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -155,7 +155,6 @@ function downloadFile(url: string, fileStream: WriteStream): Promise { response.on('error', err => { reject(err) }) - // response.pipe(fileStream) }) } catch (err) { reject(err) From 31d9a4bd3798e6cde1d4c9a1f4e9194d8c3afe22 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:02:20 -0500 Subject: [PATCH 04/10] . --- dist/index.js | 2 +- src/github-api-helper.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 42064d3..28a56dc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8393,7 +8393,7 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat const response = yield octokit.repos.getArchiveLink(params); console.log('GOT THE RESPONSE'); console.log(response.status); - if (response.status != 302) { + if (response.status != 200) { throw new Error(`Unexpected response from GitHub API. Status: '${response.status}'`); } console.log('GETTING THE LOCATION'); diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index e05e1e1..8720656 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -53,7 +53,7 @@ export async function downloadRepository( const response = await octokit.repos.getArchiveLink(params) console.log('GOT THE RESPONSE') console.log(response.status) - if (response.status != 302) { + if (response.status != 200) { throw new Error( `Unexpected response from GitHub API. Status: '${response.status}'` ) From 0b63af4c8c1d1020492a785b6799a26c1b6af620 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:07:58 -0500 Subject: [PATCH 05/10] . --- dist/index.js | 3 ++- src/github-api-helper.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 28a56dc..f49291a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8392,7 +8392,8 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat }; const response = yield octokit.repos.getArchiveLink(params); console.log('GOT THE RESPONSE'); - console.log(response.status); + console.log(`status=${response.status}`); + console.log(`headers=${JSON.stringify(response.headers)}`); if (response.status != 200) { throw new Error(`Unexpected response from GitHub API. Status: '${response.status}'`); } diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 8720656..3407c25 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -52,7 +52,8 @@ export async function downloadRepository( } const response = await octokit.repos.getArchiveLink(params) console.log('GOT THE RESPONSE') - console.log(response.status) + console.log(`status=${response.status}`) + console.log(`headers=${JSON.stringify(response.headers)}`) if (response.status != 200) { throw new Error( `Unexpected response from GitHub API. Status: '${response.status}'` From 0fa906a06730db9521ca66592fc45691dee4274f Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:31:36 -0500 Subject: [PATCH 06/10] . --- dist/index.js | 47 ++++++++++++++++------------ src/github-api-helper.ts | 66 +++++++++++++++++++++++++--------------- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/dist/index.js b/dist/index.js index f49291a..2bb2354 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8380,26 +8380,9 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat const fileStream = fs.createWriteStream(archivePath); const fileStreamClosed = getFileClosedPromise(fileStream); try { - // Get the archive URL using the GitHub REST API - core.info('Getting archive URL from GitHub REST API'); - const octokit = new github.GitHub(accessToken); - const params = { - method: 'HEAD', - archive_format: IS_WINDOWS ? 'zipball' : 'tarball', - owner: owner, - repo: repo, - ref: refHelper.getDownloadRef(ref, commit) - }; - const response = yield octokit.repos.getArchiveLink(params); - console.log('GOT THE RESPONSE'); - console.log(`status=${response.status}`); - console.log(`headers=${JSON.stringify(response.headers)}`); - 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`); + // Get the archive URL + core.info('Getting archive URL'); + const archiveUrl = yield 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 yield downloadFile(archiveUrl, fileStream); @@ -8460,6 +8443,30 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat }); } exports.downloadRepository = downloadRepository; +function getArchiveUrl(accessToken, owner, repo, ref, commit) { + return __awaiter(this, void 0, void 0, function* () { + const octokit = new github.GitHub(accessToken); + const params = { + method: 'HEAD', + owner: owner, + repo: repo, + archive_format: IS_WINDOWS ? 'zipball' : 'tarball', + ref: refHelper.getDownloadRef(ref, commit) + }; + const response = yield octokit.repos.getArchiveLink(params); + console.log('GOT THE RESPONSE'); + console.log(`status=${response.status}`); + console.log(`headers=${JSON.stringify(response.headers)}`); + console.log(`headers=${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, fileStream) { return new Promise((resolve, reject) => { try { diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 3407c25..1b1cf65 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -40,30 +40,14 @@ export async function downloadRepository( const fileStreamClosed = getFileClosedPromise(fileStream) try { - // Get the archive URL using the GitHub REST API - core.info('Getting archive URL from GitHub REST API') - const octokit = new github.GitHub(accessToken) - const params: RequestOptions & ReposGetArchiveLinkParams = { - method: 'HEAD', - archive_format: IS_WINDOWS ? 'zipball' : 'tarball', - owner: owner, - repo: repo, - 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)}`) - 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` + // Get the archive URL + core.info('Getting archive URL') + const archiveUrl = await getArchiveUrl( + accessToken, + owner, + repo, + ref, + commit ) // Download the archive @@ -137,6 +121,40 @@ export async function downloadRepository( } as ExecOptions) } +async function getArchiveUrl( + accessToken: string, + owner: string, + repo: string, + ref: string, + commit: string +): Promise { + 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(`headers=${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 { return new Promise((resolve, reject) => { try { From d79ea533078392e4878f35e929e6eb0932b8fd64 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:39:09 -0500 Subject: [PATCH 07/10] . --- src/github-api-helper.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 1b1cf65..2b13bcb 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -11,8 +11,7 @@ import * as retryHelper from './retry-helper' import * as toolCache from '@actions/tool-cache' import {ExecOptions} from '@actions/exec/lib/interfaces' import {IncomingMessage} from 'http' -import {ReposGetArchiveLinkParams} from '@octokit/rest' -import {RequestOptions} from 'https' +import {RequestOptions, ReposGetArchiveLinkParams} from '@octokit/rest' import {WriteStream} from 'fs' const IS_WINDOWS = process.platform === 'win32' From 093dbebc2ee6383a91173339ce2bf0e588fb7ff9 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:52:19 -0500 Subject: [PATCH 08/10] . --- dist/index.js | 178 ++++++++++++++++++++++------------- src/github-api-helper.ts | 198 ++++++++++++++++++++++++--------------- 2 files changed, 238 insertions(+), 138 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2bb2354..43edeb8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8358,7 +8358,6 @@ const core = __importStar(__webpack_require__(470)); const exec = __importStar(__webpack_require__(986)); const fs = __importStar(__webpack_require__(747)); const github = __importStar(__webpack_require__(469)); -const https = __importStar(__webpack_require__(211)); const io = __importStar(__webpack_require__(1)); const path = __importStar(__webpack_require__(622)); const refHelper = __importStar(__webpack_require__(227)); @@ -8371,28 +8370,44 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat const runnerTemp = process.env['RUNNER_TEMP']; 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 - yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { - // Prepare the archive stream - core.debug(`Preparing the archive stream: ${archivePath}`); - yield io.rmRF(archivePath); - const fileStream = fs.createWriteStream(archivePath); - const fileStreamClosed = getFileClosedPromise(fileStream); - try { - // Get the archive URL - core.info('Getting archive URL'); - const archiveUrl = yield 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 - yield downloadFile(archiveUrl, fileStream); - } - finally { - fileStream.end(); - yield fileStreamClosed; - } - // return Buffer.from(response.data) // response.data is ArrayBuffer + // Ensure file does not exist + core.debug(`Ensuring archive file does not exist: ${archivePath}`); + yield io.rmRF(archivePath); + // Download the archive + let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { + core.info('Downloading the archive using the REST API'); + yield yield downloadArchive(accessToken, owner, repo, ref, commit); })); + // Write archive to disk + core.info('Writing archive to disk'); + yield 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) @@ -8443,11 +8458,10 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat }); } exports.downloadRepository = downloadRepository; -function getArchiveUrl(accessToken, owner, repo, ref, commit) { +function downloadArchive(accessToken, owner, repo, ref, commit) { return __awaiter(this, void 0, void 0, function* () { const octokit = new github.GitHub(accessToken); const params = { - method: 'HEAD', owner: owner, repo: repo, archive_format: IS_WINDOWS ? 'zipball' : 'tarball', @@ -8457,51 +8471,87 @@ function getArchiveUrl(accessToken, owner, repo, ref, commit) { 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, fileStream) { - return new Promise((resolve, reject) => { - try { - https.get(url, (response) => { - 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) { - return new Promise((resolve, reject) => { - stream.on('error', err => { - reject(err); - }); - stream.on('finish', () => { - resolve(); - }); + return Buffer.from(response.data); // response.data is ArrayBuffer + // 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 { +// 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 { +// 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 { +// return new Promise((resolve, reject) => { +// stream.on('error', err => { +// reject(err) +// }) +// stream.on('finish', () => { +// resolve() +// }) +// }) +// } /***/ }), 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) + // Ensure file does not exist + core.debug(`Ensuring archive file does not exist: ${archivePath}`) + await io.rmRF(archivePath) - 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 + 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 { +): Promise { 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 + + return Buffer.from(response.data) // response.data is ArrayBuffer + + // 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 { - 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 - } +// async function getArchiveUrl( +// accessToken: string, +// owner: string, +// repo: string, +// ref: string, +// commit: string +// ): Promise { +// 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 +// } - response.on('data', chunk => { - fileStream.write(chunk) - }) - response.on('end', () => { - resolve() - }) - response.on('error', err => { - reject(err) - }) - }) - } catch (err) { - reject(err) - } - }) -} +// function downloadFile(url: string, fileStream: WriteStream): Promise { +// 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 +// } -function getFileClosedPromise(stream: WriteStream): Promise { - return new Promise((resolve, reject) => { - stream.on('error', err => { - reject(err) - }) - stream.on('finish', () => { - resolve() - }) - }) -} +// 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 { +// return new Promise((resolve, reject) => { +// stream.on('error', err => { +// reject(err) +// }) +// stream.on('finish', () => { +// resolve() +// }) +// }) +// } From 59bb2dd7cdbba517519f03adbbfa26d683228209 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 02:58:11 -0500 Subject: [PATCH 09/10] . --- dist/index.js | 29 +++-------------------------- src/github-api-helper.ts | 36 +++--------------------------------- 2 files changed, 6 insertions(+), 59 deletions(-) diff --git a/dist/index.js b/dist/index.js index 43edeb8..5d50de4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8376,12 +8376,12 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat // Download the archive let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () { core.info('Downloading the archive using the REST API'); - yield yield downloadArchive(accessToken, owner, repo, ref, commit); + return yield downloadArchive(accessToken, owner, repo, ref, commit); })); // Write archive to disk core.info('Writing archive to disk'); yield fs.promises.writeFile(archivePath, archiveData); - archiveData = undefined; + archiveData = Buffer.from(''); // Free memory // // Get the archive URL using the REST API // await retryHelper.execute(async () => { // // Prepare the archive stream @@ -8407,28 +8407,12 @@ function downloadRepository(accessToken, owner, repo, ref, commit, repositoryPat // 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) - // // console.log(`status=${response.status}`) - // // console.log(`headers=${JSON.stringify(response.headers)}`) - // // console.log(`data=${response.data}`) - // // console.log(`data=${JSON.stringify(response.data)}`) - // // for (const key of Object.keys(response.data)) { - // // console.log(`data['${key}']=${response.data[key]}`) - // // } - // // Write archive to file - // const runnerTemp = process.env['RUNNER_TEMP'] as string - // assert.ok(runnerTemp, 'RUNNER_TEMP not defined') - // const archivePath = path.join(runnerTemp, 'checkout.tar.gz') - // await io.rmRF(archivePath) // await fs.promises.writeFile(archivePath, raw) // // await exec.exec(`ls -la "${archiveFile}"`, [], { // // cwd: repositoryPath // // } as ExecOptions) // Extract archive - const extractPath = path.join(runnerTemp, `checkout-archive${IS_WINDOWS ? '.zip' : '.tar.gz'}`); + const extractPath = path.join(runnerTemp, `checkout`); yield io.rmRF(extractPath); yield io.mkdirP(extractPath); if (IS_WINDOWS) { @@ -8476,13 +8460,6 @@ function downloadArchive(accessToken, owner, repo, ref, commit) { throw new Error(`Unexpected response from GitHub API. Status: '${response.status}'`); } return Buffer.from(response.data); // response.data is ArrayBuffer - // 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( diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index 0be91f2..b3385a5 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -36,13 +36,13 @@ export async function downloadRepository( // 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) + return 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 + archiveData = Buffer.from('') // Free memory // // Get the archive URL using the REST API // await retryHelper.execute(async () => { @@ -72,35 +72,13 @@ export async function downloadRepository( // } // }) - // 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) - - // // console.log(`status=${response.status}`) - // // console.log(`headers=${JSON.stringify(response.headers)}`) - // // console.log(`data=${response.data}`) - // // console.log(`data=${JSON.stringify(response.data)}`) - // // for (const key of Object.keys(response.data)) { - // // console.log(`data['${key}']=${response.data[key]}`) - // // } - - // // Write archive to file - // const runnerTemp = process.env['RUNNER_TEMP'] as string - // assert.ok(runnerTemp, 'RUNNER_TEMP not defined') - // const archivePath = path.join(runnerTemp, 'checkout.tar.gz') - // await io.rmRF(archivePath) // await fs.promises.writeFile(archivePath, raw) // // await exec.exec(`ls -la "${archiveFile}"`, [], { // // cwd: repositoryPath // // } as ExecOptions) // Extract archive - const extractPath = path.join( - runnerTemp, - `checkout-archive${IS_WINDOWS ? '.zip' : '.tar.gz'}` - ) + const extractPath = path.join(runnerTemp, `checkout`) await io.rmRF(extractPath) await io.mkdirP(extractPath) if (IS_WINDOWS) { @@ -160,14 +138,6 @@ async function downloadArchive( } return Buffer.from(response.data) // response.data is ArrayBuffer - - // 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( From 096b500552f8ef398fe070364e7232be81bacad5 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Tue, 10 Dec 2019 03:00:06 -0500 Subject: [PATCH 10/10] . --- src/github-api-helper.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index b3385a5..dc30870 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -72,11 +72,6 @@ export async function downloadRepository( // } // }) - // await fs.promises.writeFile(archivePath, raw) - // // await exec.exec(`ls -la "${archiveFile}"`, [], { - // // cwd: repositoryPath - // // } as ExecOptions) - // Extract archive const extractPath = path.join(runnerTemp, `checkout`) await io.rmRF(extractPath) @@ -86,9 +81,6 @@ export async function downloadRepository( } else { await toolCache.extractTar(archivePath, extractPath) } - // await exec.exec(`tar -xzf "${archiveFile}"`, [], { - // cwd: extractPath - // } as ExecOptions) // Determine the real directory to copy (ignore extra dir at root of the archive) const archiveFileNames = await fs.promises.readdir(extractPath)