forked from dawidd6/action-download-artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Handle non-zipped artifacts from upload-artifact@v7 (archive: false) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
7
commits into
master
Choose a base branch
from
copilot/fix-artifact-download-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
812dd03
Initial plan
Copilot 12a2d6a
feat: support non-zipped artifacts from upload-artifact@v7
Copilot 1871e5c
test: add tests for archive:false non-ZIP artifact download
Copilot f45bde0
ci: add test workflow to run npm test on push and pull_request
Copilot 5136e6d
ci: add upload/download of archive:false (unarchived) artifacts to wo…
Copilot 3468611
ci: fix upload-unarchived artifact name - path is used as name when a…
Copilot 4e8c598
fix: use redirect:manual + Location header, URL ZIP fallback, Content…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: '20' | ||
| - name: Install dependencies | ||
| run: npm install | ||
| - name: Run tests | ||
| run: npm test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { test, describe, beforeEach, afterEach } from 'node:test' | ||
| import assert from 'node:assert/strict' | ||
| import fs from 'node:fs' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
| import AdmZip from 'adm-zip' | ||
|
|
||
| // main.js is guarded by import.meta.url so importing it does not call main(). | ||
| const { isZipContentType } = await import('./main.js') | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // isZipContentType – unit tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('isZipContentType', () => { | ||
| test('returns true for application/zip', () => { | ||
| assert.equal(isZipContentType('application/zip'), true) | ||
| }) | ||
|
|
||
| test('returns true for application/x-zip-compressed', () => { | ||
| assert.equal(isZipContentType('application/x-zip-compressed'), true) | ||
| }) | ||
|
|
||
| test('returns true for application/zip-compressed', () => { | ||
| assert.equal(isZipContentType('application/zip-compressed'), true) | ||
| }) | ||
|
|
||
| test('returns true for application/zip with extra parameters', () => { | ||
| assert.equal(isZipContentType('application/zip; charset=utf-8'), true) | ||
| }) | ||
|
|
||
| test('returns true regardless of case', () => { | ||
| assert.equal(isZipContentType('Application/Zip'), true) | ||
| assert.equal(isZipContentType('APPLICATION/ZIP'), true) | ||
| }) | ||
|
|
||
| // archive: false uploads use application/octet-stream or similar | ||
| test('returns false for application/octet-stream (archive: false direct upload)', () => { | ||
| assert.equal(isZipContentType('application/octet-stream'), false) | ||
| }) | ||
|
|
||
| test('returns false for text/plain', () => { | ||
| assert.equal(isZipContentType('text/plain'), false) | ||
| }) | ||
|
|
||
| test('returns false for image/png', () => { | ||
| assert.equal(isZipContentType('image/png'), false) | ||
| }) | ||
|
|
||
| test('returns false for empty string', () => { | ||
| assert.equal(isZipContentType(''), false) | ||
| }) | ||
|
|
||
| test('returns false for null', () => { | ||
| assert.equal(isZipContentType(null), false) | ||
| }) | ||
|
|
||
| test('returns false for undefined', () => { | ||
| assert.equal(isZipContentType(undefined), false) | ||
| }) | ||
| }) | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Download behaviour – integration tests using a real temp directory | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('artifact download behaviour', () => { | ||
| let tmpDir | ||
|
|
||
| beforeEach(() => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'artifact-test-')) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| test('non-ZIP artifact (archive: false) is written as a plain file', () => { | ||
| // Simulate what main.js does when isZipFile is false: | ||
| // fs.writeFileSync(pathname.join(dir, filename), buffer, 'binary') | ||
| const artifactName = 'my-binary' | ||
| const content = 'raw file content' | ||
| const buffer = Buffer.from(content) | ||
|
|
||
| // isZipContentType must return false for a direct-upload content type | ||
| assert.equal(isZipContentType('application/octet-stream'), false) | ||
|
|
||
| const outputPath = path.join(tmpDir, artifactName) | ||
| fs.writeFileSync(outputPath, buffer, 'binary') | ||
|
|
||
| assert.ok(fs.existsSync(outputPath), 'output file should exist') | ||
| assert.equal(fs.readFileSync(outputPath, 'utf8'), content) | ||
| }) | ||
|
|
||
| test('ZIP artifact is extracted to the target directory', () => { | ||
| // Build a minimal in-memory zip containing one entry | ||
| const adm = new AdmZip() | ||
| adm.addFile('hello.txt', Buffer.from('hello from zip')) | ||
| const zipBuffer = adm.toBuffer() | ||
|
|
||
| // isZipContentType must return true for application/zip | ||
| assert.equal(isZipContentType('application/zip'), true) | ||
|
|
||
| // Simulate main.js ZIP extraction path (adm-zip branch) | ||
| const adm2 = new AdmZip(zipBuffer) | ||
| adm2.extractAllTo(tmpDir, true) | ||
|
|
||
| const extracted = path.join(tmpDir, 'hello.txt') | ||
| assert.ok(fs.existsSync(extracted), 'extracted file should exist') | ||
| assert.equal(fs.readFileSync(extracted, 'utf8'), 'hello from zip') | ||
| }) | ||
|
|
||
| test('skip_unpack with non-ZIP writes file without .zip extension', () => { | ||
| const artifactName = 'my-report' | ||
| const buffer = Buffer.from('report data') | ||
| const isZipFile = isZipContentType('application/octet-stream') // false | ||
|
|
||
| const ext = isZipFile ? '.zip' : '' | ||
| const outputPath = path.join(tmpDir, `${artifactName}${ext}`) | ||
| fs.writeFileSync(outputPath, buffer, 'binary') | ||
|
|
||
| // Should not have .zip extension | ||
| assert.ok(fs.existsSync(path.join(tmpDir, 'my-report')), 'file without extension should exist') | ||
| assert.ok(!fs.existsSync(path.join(tmpDir, 'my-report.zip')), 'file with .zip extension should not exist') | ||
| }) | ||
|
|
||
| test('skip_unpack with ZIP writes file with .zip extension', () => { | ||
| const adm = new AdmZip() | ||
| adm.addFile('data.txt', Buffer.from('data')) | ||
| const artifactName = 'my-archive' | ||
| const buffer = adm.toBuffer() | ||
| const isZipFile = isZipContentType('application/zip') // true | ||
|
|
||
| const ext = isZipFile ? '.zip' : '' | ||
| const outputPath = path.join(tmpDir, `${artifactName}${ext}`) | ||
| fs.writeFileSync(outputPath, buffer, 'binary') | ||
|
|
||
| assert.ok(fs.existsSync(path.join(tmpDir, 'my-archive.zip')), 'file with .zip extension should exist') | ||
| assert.ok(!fs.existsSync(path.join(tmpDir, 'my-archive')), 'file without extension should not exist') | ||
| }) | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // URL-based ZIP detection | ||
| // ------------------------------------------------------------------------- | ||
| test('URL path ending with .zip is detected as ZIP', () => { | ||
| const blobUrl = 'https://storage.example.com/artifact-sha.zip?sig=abc' | ||
| const urlPath = new URL(blobUrl).pathname.toLowerCase() | ||
| assert.ok(urlPath.endsWith('.zip'), 'URL pathname should end with .zip') | ||
| }) | ||
|
|
||
| test('URL path not ending with .zip is not detected as ZIP via URL', () => { | ||
| const blobUrl = 'https://storage.example.com/artifact-sha?sig=abc' | ||
| const urlPath = new URL(blobUrl).pathname.toLowerCase() | ||
| assert.ok(!urlPath.endsWith('.zip'), 'URL pathname should not end with .zip') | ||
| }) | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Content-Disposition filename extraction | ||
| // ------------------------------------------------------------------------- | ||
| test('Content-Disposition filename is used when present', () => { | ||
| // Simulate the filename extraction logic from main.js | ||
| function extractFilename(contentDisposition, fallback) { | ||
| const cdMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/) | ||
| const rawFilename = cdMatch ? cdMatch[1].replace(/^['"]|['"]$/g, '').trim() : fallback | ||
| return path.basename(rawFilename) || fallback | ||
| } | ||
|
|
||
| assert.equal(extractFilename('attachment; filename="sha"', 'artifact-name'), 'sha') | ||
| assert.equal(extractFilename('attachment; filename=sha', 'artifact-name'), 'sha') | ||
| assert.equal(extractFilename('', 'artifact-name'), 'artifact-name') | ||
| }) | ||
|
|
||
| test('path.basename sanitizes path traversal in artifact name', () => { | ||
| const dangerousName = '../../../etc/passwd' | ||
| const safe = path.basename(dangerousName) | ||
| assert.equal(safe, 'passwd') | ||
| // Ensure writing to a tmpDir using the sanitized name stays within tmpDir | ||
| const outputPath = path.join(tmpDir, safe) | ||
| fs.writeFileSync(outputPath, 'data', 'utf8') | ||
| assert.ok(fs.existsSync(outputPath)) | ||
| }) | ||
| }) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.