Skip to content

Commit

Permalink
Configurable api url to add support for github enterprise server (#464)
Browse files Browse the repository at this point in the history
* Add github-api-url to workflow input

* Update readme
  • Loading branch information
robinraju authored Jun 20, 2022
1 parent 68e246f commit 0ea5409
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ A Github Action to download assets from github release. It can download specifie
# https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
# eg: token: ${{ secrets.MY_TOKEN }}
token: ""

# The URL of the Github API, only use this input if you are using Github Enterprise
# Default: "https://api.github.com"
# Use http(s)://[hostname]/api/v3 to access the API for GitHub Enterprise Server
github-api-url: ""
```
## Scenarios
Expand Down
29 changes: 28 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const outputFilePath = "./test-output"

beforeEach(() => {
const githubtoken = process.env.REPO_TOKEN || ""
const githubApiUrl = "https://api.github.com"

const credentialHandler = new handlers.BearerCredentialHandler(
githubtoken,
false
)
httpClent = new thc.HttpClient("gh-api-client", [credentialHandler])
downloader = new ReleaseDownloader(httpClent)
downloader = new ReleaseDownloader(httpClent, githubApiUrl)

nock("https://api.github.com")
.get("/repos/robinraju/probable-potato/releases/latest")
Expand Down Expand Up @@ -61,6 +62,16 @@ beforeEach(() => {
})
.get("/repos/robinraju/probable-potato/releases/assets/66946551")
.replyWithFile(200, __dirname + "/resource/assets/file_example.csv")

nock("https://my-gh-host.com/api/v3")
.get("/repos/my-enterprise/test-repo/releases/latest")
.reply(200, readFromFile("2-gh-enterprise.json"))

nock("https://my-gh-host.com/api/v3", {
reqheaders: {accept: "application/octet-stream"}
})
.get("/repos/my-enterprise/test-repo/releases/assets/66946546")
.replyWithFile(200, __dirname + "/resource/assets/test-1.txt")
})

afterEach(async () => {
Expand Down Expand Up @@ -156,3 +167,19 @@ test("Download a csv file with wildcard filename", async () => {
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)

test("Download file from Github Enterprise server", async () => {
downloader = new ReleaseDownloader(httpClent, "https://my-gh-host.com/api/v3")

const downloadSettings: IReleaseDownloadSettings = {
sourceRepoPath: "my-enterprise/test-repo",
isLatest: true,
tag: "",
fileName: "test-1.txt",
tarBall: false,
zipBall: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)
36 changes: 36 additions & 0 deletions __tests__/resource/2-gh-enterprise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/releases/68092191",
"assets_url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/releases/68092191/assets",
"upload_url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/releases/68092191/assets{?name,label}",
"html_url": "https://my-gh-host.com/repos/my-enterprise/test-repo/releases/tag/1.0.1",
"id": 68092191,
"author": {},
"node_id": "RE_kwDOHahpL84EDwEf",
"tag_name": "1.0.1",
"target_commitish": "main",
"name": "v1.0.1",
"draft": false,
"prerelease": false,
"created_at": "2022-05-29T12:03:47Z",
"published_at": "2022-05-29T12:04:42Z",
"assets": [
{
"url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/releases/assets/66946546",
"id": 66946546,
"node_id": "RA_kwDOHahpL84D_YXy",
"name": "test-1.txt",
"label": null,
"uploader": {},
"content_type": "text/plain",
"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://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/releases/download/1.0.1/test-1.txt"
}
],
"tarball_url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/tarball/1.0.1",
"zipball_url": "https://my-gh-host.com/api/v3/repos/my-enterprise/test-repo/zipball/1.0.1",
"body": ""
}
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ inputs:
description: "Github token to access private repos"
default: ${{ github.token }}
required: false
github-api-url:
description: "The URL of the Github API, only use this input if you are using Github Enterprise"
default: "https://api.github.com"
required: false
runs:
using: "node16"
main: "dist/index.js"
Expand Down
11 changes: 6 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function run(): Promise<void> {
try {
const downloadSettings = inputHelper.getInputs()
const authToken = core.getInput("token")
const githubApiUrl = core.getInput("github-api-url")

const credentialHandler = new handlers.BearerCredentialHandler(
authToken,
Expand All @@ -18,7 +19,7 @@ async function run(): Promise<void> {
credentialHandler
])

const downloader = new ReleaseDownloader(httpClient)
const downloader = new ReleaseDownloader(httpClient, githubApiUrl)

const res: string[] = await downloader.download(downloadSettings)
core.info(`Done: ${res}`)
Expand Down
9 changes: 5 additions & 4 deletions src/release-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {IReleaseDownloadSettings} from "./download-settings"
export class ReleaseDownloader {
private httpClient: thc.HttpClient

private _apiRoot = "https://api.github.com/repos"
private apiRoot: string

constructor(httpClient: thc.HttpClient) {
constructor(httpClient: thc.HttpClient, githubApiUrl: string) {
this.httpClient = httpClient
this.apiRoot = githubApiUrl
}

async download(
Expand Down Expand Up @@ -53,7 +54,7 @@ export class ReleaseDownloader {
const headers: IHeaders = {Accept: "application/vnd.github.v3+json"}

const response = await this.httpClient.get(
`${this._apiRoot}/${repoPath}/releases/latest`,
`${this.apiRoot}/repos/${repoPath}/releases/latest`,
headers
)

Expand Down Expand Up @@ -89,7 +90,7 @@ export class ReleaseDownloader {
const headers: IHeaders = {Accept: "application/vnd.github.v3+json"}

const response = await this.httpClient.get(
`${this._apiRoot}/${repoPath}/releases/tags/${tag}`,
`${this.apiRoot}/repos/${repoPath}/releases/tags/${tag}`,
headers
)

Expand Down

0 comments on commit 0ea5409

Please sign in to comment.