diff --git a/src/action.ts b/src/action.ts index 0d1d3bb..23c3031 100644 --- a/src/action.ts +++ b/src/action.ts @@ -17,7 +17,7 @@ import { Registry } from "./registry"; import { writeBunfig } from "./bunfig"; import { saveState } from "@actions/core"; import { addExtension } from "./utils"; -import { DownloadMeta, getDownloadMeta } from "./download-url"; +import { getDownloadUrl } from "./download-url"; import { cwd } from "node:process"; export type Input = { @@ -29,7 +29,7 @@ export type Input = { profile?: boolean; registries?: Registry[]; noCache?: boolean; - token: string; + token?: string; }; export type Output = { @@ -51,8 +51,7 @@ export default async (options: Input): Promise => { const bunfigPath = join(cwd(), "bunfig.toml"); writeBunfig(bunfigPath, options.registries); - const downloadMeta = await getDownloadMeta(options); - const url = downloadMeta.url; + const url = await getDownloadUrl(options); const cacheEnabled = isCacheEnabled(options); @@ -110,7 +109,7 @@ export default async (options: Input): Promise => { if (!cacheHit) { info(`Downloading a new version of Bun: ${url}`); - revision = await downloadBun(downloadMeta, bunPath); + revision = await downloadBun(url, bunPath); } } @@ -164,11 +163,11 @@ function isVersionMatch( } async function downloadBun( - downloadMeta: DownloadMeta, + url: string, bunPath: string, ): Promise { // Workaround for https://github.com/oven-sh/setup-bun/issues/79 and https://github.com/actions/toolkit/issues/1179 - const zipPath = addExtension(await downloadTool(downloadMeta.url), ".zip"); + const zipPath = addExtension(await downloadTool(url), ".zip"); const extractedZipPath = await extractZip(zipPath); const extractedBunPath = await extractBun(extractedZipPath); try { diff --git a/src/download-url.ts b/src/download-url.ts index a6cd39b..2800d45 100644 --- a/src/download-url.ts +++ b/src/download-url.ts @@ -2,28 +2,16 @@ import { compareVersions, satisfies, validate } from "compare-versions"; import { Input } from "./action"; import { getArchitecture, getPlatform, request } from "./utils"; -export interface DownloadMeta { - url: string; - auth?: string; -} - -export async function getDownloadMeta(options: Input): Promise { +export async function getDownloadUrl(options: Input): Promise { const { customUrl } = options; if (customUrl) { - return { - url: customUrl, - }; + return customUrl; } - return await getSemverDownloadMeta(options); + return await getSemverDownloadUrl(options); } -interface Run { - id: string; - head_sha: string; -} - -async function getSemverDownloadMeta(options: Input): Promise { +async function getSemverDownloadUrl(options: Input): Promise { const res = (await ( await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags", { headers: options.token @@ -71,7 +59,5 @@ async function getSemverDownloadMeta(options: Input): Promise { "https://github.com/oven-sh/bun/releases/download/", ); - return { - url: href, - }; + return href; } diff --git a/src/utils.ts b/src/utils.ts index ab11486..3f21f37 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,7 +7,15 @@ export async function request( url: string, init?: RequestInit, ): Promise { - const res = await fetch(url, init); + const headers = new Headers(init?.headers); + if (!headers.has("User-Agent")) { + headers.set("User-Agent", "@oven-sh/setup-bun"); + } + + const res = await fetch(url, { + ...init, + headers, + }); if (!res.ok) { const body = await res.text().catch(() => ""); throw new Error(