From c7513d9a9ea3d093e155e43a307f23cf101a4735 Mon Sep 17 00:00:00 2001 From: Dhruwang Date: Fri, 27 Sep 2024 15:25:26 +0530 Subject: [PATCH 1/2] remove caching from getAllOssGgIssuesOfRepos --- lib/github/service.ts | 84 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/lib/github/service.ts b/lib/github/service.ts index 1b1f1fd..bdc4588 100644 --- a/lib/github/service.ts +++ b/lib/github/service.ts @@ -82,48 +82,42 @@ export const getPullRequestsByGithubLogin = async ( return pullRequests; }; -export const getAllOssGgIssuesOfRepos = unstable_cache( - async (repoGithubIds: number[]): Promise => { - const githubHeaders = { - Authorization: `Bearer ${GITHUB_APP_ACCESS_TOKEN}`, - Accept: "application/vnd.github.v3+json", - }; - - const allIssues = await Promise.all( - repoGithubIds.map(async (repoGithubId) => { - const repoResponse = await fetch(`https://api.github.com/repositories/${repoGithubId}`, { - headers: githubHeaders, - }); - const repoData = await repoResponse.json(); - - const issuesResponse = await fetch( - `https://api.github.com/search/issues?q=repo:${repoData.full_name}+is:issue+is:open+label:"${OSS_GG_LABEL}"&sort=created&order=desc`, - { headers: githubHeaders } - ); - const issuesData = await issuesResponse.json(); - const validatedData = ZGithubApiResponseSchema.parse(issuesData); - - // Map the GitHub API response to TPullRequest format - return validatedData.items.map((issue) => - ZPullRequest.parse({ - title: issue.title, - href: issue.html_url, - author: issue.user.login, - repositoryFullName: repoData.full_name, - dateOpened: issue.created_at, - dateMerged: null, - dateClosed: issue.closed_at, - status: "open", - points: extractPointsFromLabels(issue.labels), - }) - ); - }) - ); - - return allIssues.flat(); - }, - [`getOpenIssues`], - { - revalidate: GITHUB_CACHE_REVALIDATION_INTERVAL, - } -); +export const getAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise => { + const githubHeaders = { + Authorization: `Bearer ${GITHUB_APP_ACCESS_TOKEN}`, + Accept: "application/vnd.github.v3+json", + }; + + const allIssues = await Promise.all( + repoGithubIds.map(async (repoGithubId) => { + const repoResponse = await fetch(`https://api.github.com/repositories/${repoGithubId}`, { + headers: githubHeaders, + }); + const repoData = await repoResponse.json(); + + const issuesResponse = await fetch( + `https://api.github.com/search/issues?q=repo:${repoData.full_name}+is:issue+is:open+label:"${OSS_GG_LABEL}"&sort=created&order=desc`, + { headers: githubHeaders } + ); + const issuesData = await issuesResponse.json(); + const validatedData = ZGithubApiResponseSchema.parse(issuesData); + + // Map the GitHub API response to TPullRequest format + return validatedData.items.map((issue) => + ZPullRequest.parse({ + title: issue.title, + href: issue.html_url, + author: issue.user.login, + repositoryFullName: repoData.full_name, + dateOpened: issue.created_at, + dateMerged: null, + dateClosed: issue.closed_at, + status: "open", + points: extractPointsFromLabels(issue.labels), + }) + ); + }) + ); + + return allIssues.flat(); +}; From b5167fb52cb1abf4b85c008cded33b5f09386e2e Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 28 Sep 2024 09:41:34 -0700 Subject: [PATCH 2/2] add cache with 60s duration --- lib/github/service.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/github/service.ts b/lib/github/service.ts index bdc4588..35d0f6d 100644 --- a/lib/github/service.ts +++ b/lib/github/service.ts @@ -5,14 +5,14 @@ import { TPullRequest, ZPullRequest } from "@/types/pullRequest"; import { Octokit } from "@octokit/rest"; import { unstable_cache } from "next/cache"; -import { GITHUB_APP_ACCESS_TOKEN, GITHUB_CACHE_REVALIDATION_INTERVAL, OSS_GG_LABEL } from "../constants"; +import { GITHUB_APP_ACCESS_TOKEN, OSS_GG_LABEL } from "../constants"; import { extractPointsFromLabels } from "./utils"; type PullRequestStatus = "open" | "merged" | "closed" | undefined; const octokit = new Octokit({ auth: GITHUB_APP_ACCESS_TOKEN }); -export const getPullRequestsByGithubLogin = async ( +const fetchPullRequestsByGithubLogin = async ( playerRepositoryIds: string[], githubLogin: string, status?: PullRequestStatus @@ -41,8 +41,6 @@ export const getPullRequestsByGithubLogin = async ( }); for (const pr of data.items) { - // console.log(`Complete PR object: ${JSON.stringify(pr, null, 2)}`); - let prStatus: "open" | "merged" | "closed"; if (pr.state === "open") { prStatus = "open"; @@ -76,13 +74,18 @@ export const getPullRequestsByGithubLogin = async ( console.error(`Error fetching or processing pull requests:`, error); } - // Sort pullRequests by dateOpened in descending order pullRequests.sort((a, b) => new Date(b.dateOpened).getTime() - new Date(a.dateOpened).getTime()); return pullRequests; }; -export const getAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise => { +export const getPullRequestsByGithubLogin = unstable_cache( + fetchPullRequestsByGithubLogin, + ["fetchPullRequestsByGithubLogin"], + { revalidate: 60 } +); + +const fetchAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise => { const githubHeaders = { Authorization: `Bearer ${GITHUB_APP_ACCESS_TOKEN}`, Accept: "application/vnd.github.v3+json", @@ -102,7 +105,6 @@ export const getAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise const issuesData = await issuesResponse.json(); const validatedData = ZGithubApiResponseSchema.parse(issuesData); - // Map the GitHub API response to TPullRequest format return validatedData.items.map((issue) => ZPullRequest.parse({ title: issue.title, @@ -121,3 +123,9 @@ export const getAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise return allIssues.flat(); }; + +export const getAllOssGgIssuesOfRepos = unstable_cache( + fetchAllOssGgIssuesOfRepos, + ["fetchAllOssGgIssuesOfRepos"], + { revalidate: 60 } +);