Skip to content

Commit

Permalink
support automatic extraction of archives
Browse files Browse the repository at this point in the history
  • Loading branch information
hegerdes committed Mar 15, 2023
1 parent 36198f7 commit c28b8bb
Show file tree
Hide file tree
Showing 11 changed files with 32,136 additions and 2,267 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 flagt to set if the downloaded assats 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/[email protected]
with:
fileName: "foo.zip"
latest: true
extract: true
```
35 changes: 34 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 @@ -100,7 +108,7 @@ test("Download all files from public repo", async () => {
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 Down Expand Up @@ -243,3 +251,28 @@ test("Download file from release identified by ID", async () => {
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)

test("Download all archive files from public repo", async () => {
const extract_assats = true
const downloadSettings: IReleaseDownloadSettings = {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "*.zip",
tarBall: false,
zipBall: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
if (extract_assats) {
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 c28b8bb

Please sign in to comment.