Skip to content
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

feat: get release by api id #563

Merged
merged 5 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ beforeEach(() => {
.get("/repos/robinraju/probable-potato/releases/latest")
.reply(200, readFromFile("1-release-latest.json"))

nock("https://api.github.com")
.get("/repos/robinraju/probable-potato/releases/68092191")
.reply(200, readFromFile("1-release-latest.json"))

nock("https://api.github.com", {
reqheaders: {accept: "application/octet-stream"}
})
Expand Down Expand Up @@ -89,6 +93,7 @@ test("Download all files from public repo", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "*",
tarBall: false,
zipBall: false,
Expand All @@ -103,6 +108,7 @@ test("Download single file from public repo", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "test-1.txt",
tarBall: false,
zipBall: false,
Expand All @@ -117,6 +123,7 @@ test("Fail loudly if given filename is not found in a release", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "missing-file.txt",
tarBall: false,
zipBall: false,
Expand All @@ -133,6 +140,7 @@ test("Download files with wildcard from public repo", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "test-*.txt",
tarBall: false,
zipBall: false,
Expand All @@ -147,6 +155,7 @@ test("Download single file with wildcard from public repo", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "3-*.txt",
tarBall: false,
zipBall: false,
Expand All @@ -161,6 +170,7 @@ test("Download multiple pdf files with wildcard filename", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "*.pdf",
tarBall: false,
zipBall: false,
Expand All @@ -175,6 +185,7 @@ test("Download a csv file with wildcard filename", async () => {
sourceRepoPath: "robinraju/probable-potato",
isLatest: true,
tag: "",
id: "",
fileName: "*.csv",
tarBall: false,
zipBall: false,
Expand All @@ -191,6 +202,7 @@ test("Download file from Github Enterprise server", async () => {
sourceRepoPath: "my-enterprise/test-repo",
isLatest: true,
tag: "",
id: "",
fileName: "test-1.txt",
tarBall: false,
zipBall: false,
Expand All @@ -199,3 +211,18 @@ test("Download file from Github Enterprise server", async () => {
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)

test("Download file from release identified by ID", async () => {
const downloadSettings: IReleaseDownloadSettings = {
sourceRepoPath: "robinraju/probable-potato",
isLatest: false,
tag: "",
id: "68092191",
fileName: "test-2.txt",
tarBall: false,
zipBall: false,
outFilePath: outputFilePath
}
const result = await downloader.download(downloadSettings)
expect(result.length).toBe(1)
}, 10000)
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ inputs:
default: "false"
required: false
tag:
description: "The github tag to download the release from"
description: "The github tag to download the release from"
default: ""
required: false
fileName:
Expand All @@ -25,7 +25,7 @@ inputs:
zipBall:
description: "Download zipball from assets"
default: "false"
required: false
required: false
out-file-path:
description: "Relative path under $GITHUB_WORKSPACE to place the downloaded files"
default: "."
Expand All @@ -38,6 +38,10 @@ inputs:
description: "The URL of the Github API, only use this input if you are using Github Enterprise"
default: "https://api.github.com"
required: false
releaseId:
description: "The release id to download the file from"
default: ""
required: false
outputs:
tag_name:
description: "The github tag used to download the release"
Expand Down
5 changes: 5 additions & 0 deletions src/download-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface IReleaseDownloadSettings {
*/
tag: string

/**
* The release id
*/
id: string

/**
* Name of the file to download
*/
Expand Down
1 change: 1 addition & 0 deletions src/gh-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface GhAsset {

export interface GithubRelease {
name: string
id: number
tag_name: String
assets: GhAsset[]
tarball_url: string
Expand Down
47 changes: 46 additions & 1 deletion src/release-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ export class ReleaseDownloader {

if (downloadSettings.isLatest) {
ghRelease = await this.getlatestRelease(downloadSettings.sourceRepoPath)
} else {
} else if (downloadSettings.tag !== "") {
ghRelease = await this.getReleaseByTag(
downloadSettings.sourceRepoPath,
downloadSettings.tag
)
} else if (downloadSettings.id !== "") {
ghRelease = await this.getReleaseById(
downloadSettings.sourceRepoPath,
downloadSettings.id
)
} else {
throw new Error(
"Config error: Please input a valid tag or release ID, or specify `latest`"
robinraju marked this conversation as resolved.
Show resolved Hide resolved
)
}

// Set the output variables for use by other actions
Expand Down Expand Up @@ -111,6 +120,42 @@ export class ReleaseDownloader {
return release
}

/**
* Gets release data of the specified release ID
* @param repoPath The source repository
* @param id The github release ID to fetch.
*/
private async getReleaseById(
repoPath: string,
id: string
): Promise<GithubRelease> {
core.info(`Fetching release id:${id} from repo ${repoPath}`)

if (id === "") {
throw new Error("Config error: Please input a valid release ID")
}

const headers: IHeaders = {Accept: "application/vnd.github.v3+json"}

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

if (response.message.statusCode !== 200) {
const err: Error = new Error(
`[getReleaseById] Unexpected response: ${response.message.statusCode}`
)
throw err
}

const responseBody = await response.readBody()
const release: GithubRelease = JSON.parse(responseBody.toString())
core.info(`Found release tag: ${release.tag_name}`)

return release
}

private resolveAssets(
ghRelease: GithubRelease,
downloadSettings: IReleaseDownloadSettings
Expand Down