Always fall back to anonymous download (#304)

Closes: #268
Closes: #305
This commit is contained in:
Kevin Stillhammer
2025-02-28 17:25:17 +01:00
committed by GitHub
parent 754a7d4c2d
commit 0313224678
8 changed files with 97239 additions and 42569 deletions

View File

@ -5,11 +5,7 @@ import { promises as fs } from "node:fs";
import { OWNER, REPO, TOOL_CACHE_NAME } from "../utils/constants";
import type { Architecture, Platform } from "../utils/platforms";
import { validateChecksum } from "./checksum/checksum";
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
const PaginatingOctokit = Octokit.plugin(paginateRest, restEndpointMethods);
import { Octokit } from "../utils/octokit";
export function tryGetFromToolCache(
arch: Architecture,
@ -98,7 +94,7 @@ export async function resolveVersion(
async function getAvailableVersions(githubToken: string): Promise<string[]> {
try {
const octokit = new PaginatingOctokit({
const octokit = new Octokit({
auth: githubToken,
});
return await getReleaseTagNames(octokit);
@ -107,7 +103,7 @@ async function getAvailableVersions(githubToken: string): Promise<string[]> {
core.info(
"No (valid) GitHub token provided. Falling back to anonymous. Requests might be rate limited.",
);
const octokit = new PaginatingOctokit();
const octokit = new Octokit();
return await getReleaseTagNames(octokit);
}
throw err;
@ -115,7 +111,7 @@ async function getAvailableVersions(githubToken: string): Promise<string[]> {
}
async function getReleaseTagNames(
octokit: InstanceType<typeof PaginatingOctokit>,
octokit: InstanceType<typeof Octokit>,
): Promise<string[]> {
const response = await octokit.paginate(octokit.rest.repos.listReleases, {
owner: OWNER,
@ -126,7 +122,7 @@ async function getReleaseTagNames(
async function getLatestVersion(githubToken: string) {
core.debug("Getting latest version...");
const octokit = new PaginatingOctokit({
const octokit = new Octokit({
auth: githubToken,
});
@ -134,15 +130,14 @@ async function getLatestVersion(githubToken: string) {
try {
latestRelease = await getLatestRelease(octokit);
} catch (err) {
if ((err as Error).message.includes("Bad credentials")) {
core.info(
"No (valid) GitHub token provided. Falling back to anonymous. Requests might be rate limited.",
);
const octokit = new PaginatingOctokit();
latestRelease = await getLatestRelease(octokit);
} else {
throw err;
core.info(
"No (valid) GitHub token provided. Falling back to anonymous. Requests might be rate limited.",
);
if (err instanceof Error) {
core.debug(err.message);
}
const octokit = new Octokit();
latestRelease = await getLatestRelease(octokit);
}
if (!latestRelease) {
@ -152,9 +147,7 @@ async function getLatestVersion(githubToken: string) {
return latestRelease.tag_name;
}
async function getLatestRelease(
octokit: InstanceType<typeof PaginatingOctokit>,
) {
async function getLatestRelease(octokit: InstanceType<typeof Octokit>) {
const { data: latestRelease } = await octokit.rest.repos.getLatestRelease({
owner: OWNER,
repo: REPO,

View File

@ -1,20 +1,16 @@
import * as semver from "semver";
import * as core from "@actions/core";
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { Octokit } from "./utils/octokit";
import { OWNER, REPO } from "./utils/constants";
import { updateChecksums } from "./download/checksum/update-known-checksums";
const PaginatingOctokit = Octokit.plugin(paginateRest, restEndpointMethods);
async function run(): Promise<void> {
const checksumFilePath = process.argv.slice(2)[0];
const github_token = process.argv.slice(2)[1];
const octokit = new PaginatingOctokit({
const octokit = new Octokit({
auth: github_token,
});

58
src/utils/octokit.ts Normal file
View File

@ -0,0 +1,58 @@
import { Octokit as Core } from "@octokit/core";
import type {
Constructor,
OctokitOptions,
} from "@octokit/core/dist-types/types";
import {
paginateRest,
type PaginateInterface,
} from "@octokit/plugin-paginate-rest";
import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { fetch as undiciFetch, ProxyAgent, type RequestInit } from "undici";
export type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
const DEFAULTS = {
baseUrl: "https://api.github.com",
userAgent: "setup-uv",
};
export function getProxyAgent() {
const httpProxy = process.env.HTTP_PROXY || process.env.http_prox;
if (httpProxy) {
return new ProxyAgent(httpProxy);
}
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
if (httpsProxy) {
return new ProxyAgent(httpsProxy);
}
return undefined;
}
export const customFetch = async (url: string, opts: RequestInit) =>
await undiciFetch(url, {
dispatcher: getProxyAgent(),
...opts,
});
export const Octokit: typeof Core &
Constructor<
{
paginate: PaginateInterface;
} & ReturnType<typeof legacyRestEndpointMethods>
> = Core.plugin(paginateRest, legacyRestEndpointMethods).defaults(
function buildDefaults(options: OctokitOptions): OctokitOptions {
return {
...DEFAULTS,
...options,
request: {
fetch: customFetch,
...options.request,
},
};
},
);
export type Octokit = InstanceType<typeof Octokit>;