Skip to content

Commit

Permalink
Add support to extract release archives - fixes #612 (#613)
Browse files Browse the repository at this point in the history
* support automatic extraction of archives

* bump actions/io to 1.1.2 & update dev deps

* add extractAssets to interface & fix input parsing

* Update download-settings: fix doc comment
  • Loading branch information
hegerdes authored Apr 26, 2023
1 parent 36198f7 commit 623a3e0
Show file tree
Hide file tree
Showing 13 changed files with 34,296 additions and 11,649 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ A Github Action to download assets from github release. It can download specifie
# It will create the target directory automatically if not present
# eg: out-file-path: "my-downloads" => It will create directory $GITHUB_WORKSPACE/my-downloads
out-file-path: ""


# A flag to set if the downloaded assets are archives and should be extracted
# Checks all downloaded files if they end with zip, tar or tar.gz and extracts them, if true.
# Prints a warning if enabled but file is not an archive - but does not fail.
extract: false

# Github access token to download files from private repositories
# https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
# eg: token: ${{ secrets.MY_TOKEN }}
Expand Down Expand Up @@ -138,3 +143,13 @@ A Github Action to download assets from github release. It can download specifie
releaseId: "123123"
fileName: "foo.zip"
```

### Download and extracts archives

```yaml
- uses: robinraju/release-downloader@v1.7
with:
fileName: "foo.zip"
latest: true
extract: true
```
45 changes: 44 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as core from "@actions/core"
import * as fs from "fs"
import * as path from "path"
import * as handlers from "typed-rest-client/Handlers"
import * as io from "@actions/io"
import * as thc from "typed-rest-client/HttpClient"

import {IReleaseDownloadSettings} from "../src/download-settings"
import {ReleaseDownloader} from "../src/release-downloader"
import nock from "nock"
import {extract} from "../src/unarchive"

let downloader: ReleaseDownloader
let httpClent: thc.HttpClient
Expand Down Expand Up @@ -61,6 +63,12 @@ beforeEach(() => {
.get("/repos/robinraju/probable-potato/releases/assets/66946550")
.replyWithFile(200, __dirname + "/resource/assets/lorem-ipsum.pdf")

nock("https://api.github.com", {
reqheaders: {accept: "application/octet-stream"}
})
.get("/repos/robinraju/probable-potato/releases/assets/66946552")
.replyWithFile(200, __dirname + "/resource/assets/archive-example.zip")

nock("https://api.github.com", {
reqheaders: {accept: "application/octet-stream"}
})
Expand Down Expand Up @@ -97,10 +105,11 @@ test("Download all files from public repo", async () => {
fileName: "*",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(6)
expect(result.length).toBe(7)
}, 10000)

test("Download single file from public repo", async () => {
Expand All @@ -112,6 +121,7 @@ test("Download single file from public repo", async () => {
fileName: "test-1.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -127,6 +137,7 @@ test("Fail loudly if given filename is not found in a release", async () => {
fileName: "missing-file.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = downloader.download(downloadSettings)
Expand All @@ -144,6 +155,7 @@ test("Fail loudly if release is not identified", async () => {
fileName: "missing-file.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = downloader.download(downloadSettings)
Expand All @@ -161,6 +173,7 @@ test("Download files with wildcard from public repo", async () => {
fileName: "test-*.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -176,6 +189,7 @@ test("Download single file with wildcard from public repo", async () => {
fileName: "3-*.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -191,6 +205,7 @@ test("Download multiple pdf files with wildcard filename", async () => {
fileName: "*.pdf",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -206,6 +221,7 @@ test("Download a csv file with wildcard filename", async () => {
fileName: "*.csv",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -223,6 +239,7 @@ test("Download file from Github Enterprise server", async () => {
fileName: "test-1.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
Expand All @@ -238,8 +255,34 @@ test("Download file from release identified by ID", async () => {
fileName: "test-2.txt",
tarBall: false,
zipBall: false,
extractAssets: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)

test("Download all archive files from public repo", async () => {
const downloadSettings: IReleaseDownloadSettings = {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "*.zip",
tarBall: false,
zipBall: false,
extractAssets: true,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
if (downloadSettings.extractAssets) {
for (const asset of result) {
await extract(asset, downloadSettings.outFilePath)
}
}

expect(result.length).toBe(1)
expect(
fs.existsSync(path.join(downloadSettings.outFilePath, "test-3.txt"))
).toBe(true)
}, 10000)
15 changes: 15 additions & 0 deletions __tests__/resource/1-release-latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@
"created_at": "2022-05-29T12:08:23Z",
"updated_at": "2022-05-29T12:08:23Z",
"browser_download_url": "https://github.com/robinraju/probable-potato/releases/download/1.0.1/file_example.csv"
},
{
"url": "https://api.github.com/repos/robinraju/probable-potato/releases/assets/66946552",
"id": 66946552,
"node_id": "RA_kwDOHahpL84D_YXa",
"name": "archive-example.zip",
"label": null,
"uploader": {},
"content_type": "application/zip",
"state": "uploaded",
"size": 7253,
"download_count": 56,
"created_at": "2022-05-29T12:08:23Z",
"updated_at": "2022-05-29T12:08:23Z",
"browser_download_url": "https://github.com/robinraju/probable-potato/releases/download/1.0.1/archive-example.zip"
}
],
"tarball_url": "https://api.github.com/repos/robinraju/probable-potato/tarball/1.0.1",
Expand Down
Binary file added __tests__/resource/assets/archive-example.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
description: "Relative path under $GITHUB_WORKSPACE to place the downloaded files"
default: "."
required: true
extract:
description: "If the downladed assets should be extracted to `out-file-path`. Supports tar, tar.gz and zip"
default: "false"
required: false
token:
description: "Github token to access private repos"
default: ${{ github.token }}
Expand Down
Loading

0 comments on commit 623a3e0

Please # to comment.