Significantly Reduce Github API Requests (#895)

[SKIP]
This commit is contained in:
Integer Limit 2024-08-12 20:47:10 +10:00 committed by GitHub
parent 787fc12089
commit ac60dfe582
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 26 deletions

View File

@ -6,9 +6,10 @@ import { Octokit } from "@octokit/rest";
import { import {
formatAuthor, formatAuthor,
getIssueURL, getIssueURL,
getNewestCommitAuthors, getCommitAuthors,
getNewestIssueURLs, getIssueURLs,
} from "#utils/util.ts"; } from "#utils/util.ts";
import logInfo from "#utils/log.ts";
let data: ChangelogData; let data: ChangelogData;
let octokit: Octokit; let octokit: Octokit;
@ -32,8 +33,10 @@ export async function pushSetup(): Promise<void> {
auth: process.env.GITHUB_TOKEN, auth: process.env.GITHUB_TOKEN,
}); });
// Save Issue/PR Info to Cache // Fill Caches
await getNewestIssueURLs(octokit); logInfo("Filling Caches...");
await getIssueURLs(octokit);
await getCommitAuthors(octokit);
} }
export function pushTitle(inputData: ChangelogData): void { export function pushTitle(inputData: ChangelogData): void {
@ -129,7 +132,6 @@ async function pushCategory(category: Category) {
} }
// Format Main Messages (Async so Author Fetch is Fast) // Format Main Messages (Async so Author Fetch is Fast)
await getNewestCommitAuthors(octokit);
const formatted: { message: ChangelogMessage; formatted: string }[] = const formatted: { message: ChangelogMessage; formatted: string }[] =
await Promise.all( await Promise.all(
list.map((message) => list.map((message) =>

View File

@ -608,25 +608,19 @@ export function cleanupVersion(version?: string): string {
const issueURLCache: Map<number, string> = new Map<number, string>(); const issueURLCache: Map<number, string> = new Map<number, string>();
/** /**
* Gets newest updated 100 closed issue/PR URLs of the repo and saves it to the cache. * Gets all closed issue/PR URLs of the repo, sorted by updated, and saves it to the cache.
*/ */
export async function getNewestIssueURLs(octokit: Octokit): Promise<void> { export async function getIssueURLs(octokit: Octokit): Promise<void> {
if (issueURLCache.size > 0) return; if (issueURLCache.size > 0) return;
try { try {
const issues = await octokit.issues.listForRepo({ const issues = await octokit.paginate(octokit.issues.listForRepo, {
owner: repoOwner, owner: repoOwner,
repo: repoName, repo: repoName,
per_page: 100, per_page: 100,
state: "closed", state: "closed",
sort: "updated", sort: "updated",
}); });
if (issues.status !== 200) { issues.forEach((issue) => {
logError(
`Failed to get all Issue URLs of Repo. Returned Status Code ${issues.status}, expected Status 200.`,
);
return;
}
issues.data.forEach((issue) => {
if (!issueURLCache.has(issue.number)) if (!issueURLCache.has(issue.number))
issueURLCache.set(issue.number, issue.html_url); issueURLCache.set(issue.number, issue.html_url);
}); });
@ -647,6 +641,7 @@ export async function getIssueURL(
if (issueURLCache.has(issueNumber)) if (issueURLCache.has(issueNumber))
return issueURLCache.get(issueNumber) ?? ""; return issueURLCache.get(issueNumber) ?? "";
try { try {
// Try to retrieve, might be open
const issueInfo = await octokit.issues.get({ const issueInfo = await octokit.issues.get({
owner: repoOwner, owner: repoOwner,
repo: repoName, repo: repoName,
@ -666,7 +661,7 @@ export async function getIssueURL(
return issueInfo.data.html_url; return issueInfo.data.html_url;
} catch (e) { } catch (e) {
logError( logError(
`Failed to get the Issue/PR Info for Issue/PR #${issueNumber}. This may be because this is not a PR or Issue, or could be because of rate limits.`, `Failed to get the Issue/PR Info for Issue/PR #${issueNumber}. This may be because this is not a PR or Issue, it was deleted, or because of rate limits.`,
); );
issueURLCache.set(issueNumber, ""); issueURLCache.set(issueNumber, "");
return ""; return "";
@ -677,23 +672,17 @@ export async function getIssueURL(
const commitAuthorCache: Map<string, string> = new Map<string, string>(); const commitAuthorCache: Map<string, string> = new Map<string, string>();
/** /**
* Fills the Commit Author Cache with the newest 100 commits from the repo. * Fills the Commit Author Cache.
*/ */
export async function getNewestCommitAuthors(octokit: Octokit): Promise<void> { export async function getCommitAuthors(octokit: Octokit): Promise<void> {
if (commitAuthorCache.size > 0) return; if (commitAuthorCache.size > 0) return;
try { try {
const commits = await octokit.repos.listCommits({ const commits = await octokit.paginate(octokit.repos.listCommits, {
owner: repoOwner, owner: repoOwner,
repo: repoName, repo: repoName,
per_page: 100, per_page: 100,
}); });
if (commits.status !== 200) { commits.forEach((commit) => {
logError(
`Failed to get all Commit Authors. Returned Status Code ${commits.status}, expected Status 200.`,
);
return;
}
commits.data.forEach((commit) => {
if (!commitAuthorCache.has(commit.sha)) if (!commitAuthorCache.has(commit.sha))
commitAuthorCache.set(commit.sha, commit.author?.login ?? ""); commitAuthorCache.set(commit.sha, commit.author?.login ?? "");
}); });
@ -717,6 +706,7 @@ export async function formatAuthor(commit: Commit, octokit: Octokit) {
} }
try { try {
// Try to retrieve, just in case
const commitInfo = await octokit.repos.getCommit({ const commitInfo = await octokit.repos.getCommit({
owner: repoOwner, owner: repoOwner,
repo: repoName, repo: repoName,