|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import { createWriteStream, mkdirSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { pipeline } from "node:stream/promises"; |
| 6 | + |
| 7 | +import { RequestError } from "@octokit/request-error"; |
| 8 | +import { Octokit } from "@octokit/rest"; |
| 9 | + |
| 10 | +import { error, info } from "../logging.ts"; |
| 11 | +import type { CodeSampleWithSDK } from "../settings.ts"; |
| 12 | +import { InternalError } from "../util/internalError.ts"; |
| 13 | + |
| 14 | +type GithubSdkConfig = { |
| 15 | + owner: string; |
| 16 | + repo: string; |
| 17 | + version: string; |
| 18 | +} & CodeSampleWithSDK; |
| 19 | + |
| 20 | +export async function withGithubSdk( |
| 21 | + sdk: GithubSdkConfig, |
| 22 | + token?: string |
| 23 | +): Promise<CodeSampleWithSDK> { |
| 24 | + const githubToken = token ?? process.env.GITHUB_TOKEN; |
| 25 | + if (githubToken === undefined) { |
| 26 | + error( |
| 27 | + "GitHub Personal Access Token is required. Please pass to withGithubSdks or set GITHUB_TOKEN environment variable" |
| 28 | + ); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + const octokit = new Octokit({ |
| 33 | + auth: githubToken, |
| 34 | + }); |
| 35 | + |
| 36 | + const { owner, repo } = sdk; |
| 37 | + |
| 38 | + info( |
| 39 | + `Fetching ${sdk.language} SDK from ${owner}/${repo}@${sdk.version ? sdk.version : "latest"}` |
| 40 | + ); |
| 41 | + |
| 42 | + const tarballUrl = await getTarballUrl(octokit, owner, repo, sdk.version); |
| 43 | + |
| 44 | + const tempDir = join(tmpdir(), `speakeasy-github-${randomUUID()}`); |
| 45 | + mkdirSync(tempDir, { recursive: true }); |
| 46 | + |
| 47 | + const sdkTarballPath = join(tempDir, `${repo}-${sdk.version}.tar.gz`); |
| 48 | + |
| 49 | + const response = await fetch(tarballUrl, { |
| 50 | + headers: { |
| 51 | + Authorization: `token ${token}`, |
| 52 | + Accept: "application/vnd.github.v3+json", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + if (!response.ok || !response.body) { |
| 57 | + error( |
| 58 | + `Failed to download archive for repo ${repo}}: ${response.status} ${response.statusText}` |
| 59 | + ); |
| 60 | + process.exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + const fileStream = createWriteStream(sdkTarballPath); |
| 64 | + await pipeline(response.body as unknown as NodeJS.ReadableStream, fileStream); |
| 65 | + |
| 66 | + return { ...sdk, sdkTarballPath }; |
| 67 | +} |
| 68 | + |
| 69 | +async function getTarballUrl( |
| 70 | + octokit: Octokit, |
| 71 | + owner: string, |
| 72 | + repo: string, |
| 73 | + version?: string |
| 74 | +): Promise<string> { |
| 75 | + try { |
| 76 | + const { data } = |
| 77 | + version && version !== "latest" |
| 78 | + ? await octokit.repos.getReleaseByTag({ |
| 79 | + owner, |
| 80 | + repo, |
| 81 | + tag: version, |
| 82 | + }) |
| 83 | + : await octokit.repos.getLatestRelease({ |
| 84 | + owner, |
| 85 | + repo, |
| 86 | + }); |
| 87 | + |
| 88 | + if (!data.tarball_url) { |
| 89 | + error(`No tarball URL found for release ${version} in ${owner}/${repo}`); |
| 90 | + process.exit(1); |
| 91 | + } |
| 92 | + return data.tarball_url; |
| 93 | + } catch (e) { |
| 94 | + if (e instanceof RequestError) { |
| 95 | + if (e.status === 404) { |
| 96 | + error( |
| 97 | + `Release ${version} not found in ${owner}/${repo}. Make sure the tag exists and the token has access to the repository.` |
| 98 | + ); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | + } |
| 102 | + throw new InternalError( |
| 103 | + `Failed to fetch artifact from Github: ${(e as Error).message}` |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments