From c93cb3058a3fd0bf2b33962f08121a4836a5456f Mon Sep 17 00:00:00 2001 From: iTrooz_ Date: Sun, 19 Jun 2022 22:36:08 +0200 Subject: [PATCH] write zip file in path (#169) --- .github/workflows/download.yml | 8 ++++--- main.js | 44 ++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/.github/workflows/download.yml b/.github/workflows/download.yml index 8da78cc4..4a280823 100644 --- a/.github/workflows/download.yml +++ b/.github/workflows/download.yml @@ -103,9 +103,11 @@ jobs: skip_unpack: true - name: Test run: | - test -f artifact.zip - ! test -d artifact - unzip -l artifact.zip + test -d artifact + test -f artifact/artifact.zip + ! test -d artifact/artifact + ! test -f artifact.zip + unzip -l artifact/artifact.zip download-dry-run-exists: runs-on: ubuntu-latest needs: wait diff --git a/main.js b/main.js index af24923d..92c9473f 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,11 @@ const core = require('@actions/core') +const http = require('http'); const github = require('@actions/github') const AdmZip = require('adm-zip') const filesize = require('filesize') const pathname = require('path') -const fs = require('fs') +const fs = require('fs'); +const url = require('url'); function inform(key, val) { core.info(`==> ${key}: ${val}`) @@ -183,23 +185,50 @@ async function main() { core.info(`==> Downloading: ${artifact.name}.zip (${size})`) - const zip = await client.actions.downloadArtifact({ + let saveTo = `${pathname.join(path, artifact.name)}.zip` + fs.mkdirSync(path, { recursive: true }) + + let request = client.actions.downloadArtifact.endpoint({ owner: owner, repo: repo, artifact_id: artifact.id, archive_format: "zip", - }) + }); + + const options = { + hostname: url.parse(request.url).hostname, + path: url.parse(request.url).pathname, + port: 443, + headers: { + ...request.headers, + Authorization: `token ${token}`, + } + } + + http.get(options, (response) => { + response.data.pipe(fs.createWriteStream(saveTo)); + });; + + // await client.actions.downloadArtifact({ + // owner: owner, + // repo: repo, + // artifact_id: artifact.id, + // archive_format: "zip", + // }).then(function (response) { + // response.data.pipe(fs.createWriteStream(saveTo)); + // }); + + // fs.writeFileSync(saveTo, Buffer.from(zip.data), 'binary') if (skipUnpack) { - fs.writeFileSync(`${artifact.name}.zip`, Buffer.from(zip.data), 'binary') continue } const dir = name ? path : pathname.join(path, artifact.name) fs.mkdirSync(dir, { recursive: true }) - - const adm = new AdmZip(Buffer.from(zip.data)) + core.info(`==> Extracting: ${saveTo}`) + const adm = new AdmZip(saveTo) core.startGroup(`==> Extracting: ${artifact.name}.zip`) adm.getEntries().forEach((entry) => { @@ -207,9 +236,10 @@ async function main() { const filepath = pathname.join(dir, entry.entryName) core.info(` ${action}: ${filepath}`) + adm.extractEntryTo(entry.entryName, dir) }) - adm.extractAllTo(dir, true) + // adm.extractAllTo(dir, true) core.endGroup() } } catch (error) {