From 0f7abeb03599964e58d979820134c9e7a61a690e Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sun, 16 Feb 2020 03:12:25 +0200 Subject: vscode: add release tag option to fetchArtifactReleaseInfo() --- .../installation/fetch_artifact_release_info.ts | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 editors/code/src/installation/fetch_artifact_release_info.ts (limited to 'editors/code/src/installation/fetch_artifact_release_info.ts') diff --git a/editors/code/src/installation/fetch_artifact_release_info.ts b/editors/code/src/installation/fetch_artifact_release_info.ts new file mode 100644 index 000000000..7d497057a --- /dev/null +++ b/editors/code/src/installation/fetch_artifact_release_info.ts @@ -0,0 +1,52 @@ +import fetch from "node-fetch"; +import { GithubRepo, ArtifactReleaseInfo } from "./interfaces"; + +const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; + + +/** + * Fetches the release with `releaseTag` (or just latest release when not specified) + * from GitHub `repo` and returns metadata about `artifactFileName` shipped with + * this release or `null` if no such artifact was published. + */ +export async function fetchArtifactReleaseInfo( + repo: GithubRepo, artifactFileName: string, releaseTag?: string +): Promise { + + const repoOwner = encodeURIComponent(repo.owner); + const repoName = encodeURIComponent(repo.name); + + const apiEndpointPath = releaseTag + ? `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}` + : `/repos/${repoOwner}/${repoName}/releases/latest`; + + const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath; + + // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) + + console.log("Issuing request for released artifacts metadata to", requestUrl); + + // FIXME: handle non-ok response + const response: GithubRelease = await fetch(requestUrl, { + headers: { Accept: "application/vnd.github.v3+json" } + }) + .then(res => res.json()); + + const artifact = response.assets.find(artifact => artifact.name === artifactFileName); + + if (!artifact) return null; + + return { + releaseName: response.name, + downloadUrl: artifact.browser_download_url + }; + + // We omit declaration of tremendous amount of fields that we are not using here + interface GithubRelease { + name: string; + assets: Array<{ + name: string; + browser_download_url: string; + }>; + } +} -- cgit v1.2.3