Skip to content

Commit

Permalink
add cache with 60s duration
Browse files Browse the repository at this point in the history
  • Loading branch information
jobenjada committed Sep 28, 2024
1 parent c7513d9 commit b5167fb
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/github/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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<TPullRequest[]> => {
export const getPullRequestsByGithubLogin = unstable_cache(
fetchPullRequestsByGithubLogin,
["fetchPullRequestsByGithubLogin"],
{ revalidate: 60 }
);

const fetchAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise<TPullRequest[]> => {
const githubHeaders = {
Authorization: `Bearer ${GITHUB_APP_ACCESS_TOKEN}`,
Accept: "application/vnd.github.v3+json",
Expand All @@ -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,
Expand All @@ -121,3 +123,9 @@ export const getAllOssGgIssuesOfRepos = async (repoGithubIds: number[]): Promise

return allIssues.flat();
};

export const getAllOssGgIssuesOfRepos = unstable_cache(
fetchAllOssGgIssuesOfRepos,
["fetchAllOssGgIssuesOfRepos"],
{ revalidate: 60 }
);

0 comments on commit b5167fb

Please sign in to comment.