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

View File

@ -608,25 +608,19 @@ export function cleanupVersion(version?: string): 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;
try {
const issues = await octokit.issues.listForRepo({
const issues = await octokit.paginate(octokit.issues.listForRepo, {
owner: repoOwner,
repo: repoName,
per_page: 100,
state: "closed",
sort: "updated",
});
if (issues.status !== 200) {
logError(
`Failed to get all Issue URLs of Repo. Returned Status Code ${issues.status}, expected Status 200.`,
);
return;
}
issues.data.forEach((issue) => {
issues.forEach((issue) => {
if (!issueURLCache.has(issue.number))
issueURLCache.set(issue.number, issue.html_url);
});
@ -647,6 +641,7 @@ export async function getIssueURL(
if (issueURLCache.has(issueNumber))
return issueURLCache.get(issueNumber) ?? "";
try {
// Try to retrieve, might be open
const issueInfo = await octokit.issues.get({
owner: repoOwner,
repo: repoName,
@ -666,7 +661,7 @@ export async function getIssueURL(
return issueInfo.data.html_url;
} catch (e) {
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, "");
return "";
@ -677,23 +672,17 @@ export async function getIssueURL(
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;
try {
const commits = await octokit.repos.listCommits({
const commits = await octokit.paginate(octokit.repos.listCommits, {
owner: repoOwner,
repo: repoName,
per_page: 100,
});
if (commits.status !== 200) {
logError(
`Failed to get all Commit Authors. Returned Status Code ${commits.status}, expected Status 200.`,
);
return;
}
commits.data.forEach((commit) => {
commits.forEach((commit) => {
if (!commitAuthorCache.has(commit.sha))
commitAuthorCache.set(commit.sha, commit.author?.login ?? "");
});
@ -717,6 +706,7 @@ export async function formatAuthor(commit: Commit, octokit: Octokit) {
}
try {
// Try to retrieve, just in case
const commitInfo = await octokit.repos.getCommit({
owner: repoOwner,
repo: repoName,