aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/installation/fetch_latest_artifact_release_info.ts
blob: 29ee029a708752875024e43ed96611f839356294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import fetch from "node-fetch";
import { GithubRepo, ArtifactReleaseInfo } from "./interfaces";

const GITHUB_API_ENDPOINT_URL = "https://api.github.com";

/**
 * Fetches the latest release from GitHub `repo` and returns metadata about
 * `artifactFileName` shipped with this release or `null` if no such artifact was published.
 */
export async function fetchLatestArtifactReleaseInfo(
    repo: GithubRepo, artifactFileName: string
): Promise<null | ArtifactReleaseInfo> {

    const repoOwner = encodeURIComponent(repo.owner);
    const repoName  = encodeURIComponent(repo.name);

    const apiEndpointPath = `/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);

    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;
        }>;
    }
}