Skip to content

Commit 2298763

Browse files
refactor: use the new processing function, adapt code
1 parent 513ea2d commit 2298763

File tree

3 files changed

+59
-91
lines changed

3 files changed

+59
-91
lines changed

src/lib/server/github-cache.ts

Lines changed: 53 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -647,33 +647,23 @@ export class GitHubCache {
647647
* @param owner the GitHub organization name
648648
* @returns a list of members, or `undefined` if not existing
649649
*/
650-
async getOrganizationMembers(owner: string): Promise<Member[] | undefined> {
651-
const cacheKey = this.#getOwnerKey(owner, "members");
652-
653-
const cachedMembers = await this.#redis.json.get<Member[]>(cacheKey);
654-
if (cachedMembers) {
655-
console.log(`Cache hit for members of ${owner}`);
656-
return cachedMembers.length ? cachedMembers : undefined; // technically we can have a real empty list, but we ignore this case here
657-
}
658-
659-
console.log(`Cache miss for members of ${owner}, fetching from GitHub API`);
660-
661-
try {
662-
const { data: members } = await this.#octokit.rest.orgs.listPublicMembers({
663-
org: owner,
664-
per_page
665-
});
666-
667-
await this.#redis.json.set(cacheKey, "$", members);
668-
await this.#redis.expire(cacheKey, MEMBERS_TTL);
669-
670-
return members;
671-
} catch {
672-
await this.#redis.json.set(cacheKey, "$", []);
673-
await this.#redis.expire(cacheKey, MEMBERS_TTL);
674-
675-
return undefined;
676-
}
650+
async getOrganizationMembers(owner: string) {
651+
return await this.#processCached<Member[]>()(
652+
this.#getOwnerKey(owner, "members"),
653+
async () => {
654+
try {
655+
const { data: members } = await this.#octokit.rest.orgs.listPublicMembers({
656+
org: owner,
657+
per_page
658+
});
659+
return members;
660+
} catch {
661+
return [] as Member[];
662+
}
663+
},
664+
members => members,
665+
MEMBERS_TTL
666+
);
677667
}
678668

679669
/**
@@ -683,34 +673,24 @@ export class GitHubCache {
683673
* @param repo the GitHub repository name
684674
* @returns a list of issues, or `undefined` if not existing
685675
*/
686-
async getAllIssues(owner: string, repo: string): Promise<Issue[] | undefined> {
687-
const cacheKey = this.#getRepoKey(owner, repo, "issues");
688-
689-
const cachedIssues = await this.#redis.json.get<Issue[]>(cacheKey);
690-
if (cachedIssues) {
691-
console.log(`Cache hit for issues for ${owner}`);
692-
return cachedIssues.length ? cachedIssues : undefined;
693-
}
694-
695-
console.log(`Cache miss for issues for ${owner}`);
696-
697-
try {
698-
const { data: issues } = await this.#octokit.rest.issues.listForRepo({
699-
owner,
700-
repo,
701-
per_page
702-
});
703-
704-
await this.#redis.json.set(cacheKey, "$", issues);
705-
await this.#redis.expire(cacheKey, FULL_DETAILS_TTL);
706-
707-
return issues;
708-
} catch {
709-
await this.#redis.json.set(cacheKey, "$", []);
710-
await this.#redis.expire(cacheKey, FULL_DETAILS_TTL);
711-
712-
return undefined;
713-
}
676+
async getAllIssues(owner: string, repo: string) {
677+
return await this.#processCached<Issue[]>()(
678+
this.#getRepoKey(owner, repo, "issues"),
679+
async () => {
680+
try {
681+
const { data: issues } = await this.#octokit.rest.issues.listForRepo({
682+
owner,
683+
repo,
684+
per_page
685+
});
686+
return issues;
687+
} catch {
688+
return [] as Issue[];
689+
}
690+
},
691+
issues => issues,
692+
FULL_DETAILS_TTL
693+
);
714694
}
715695

716696
/**
@@ -720,34 +700,24 @@ export class GitHubCache {
720700
* @param repo the GitHub repository name
721701
* @returns a list of pull requests, or `undefined` if not existing
722702
*/
723-
async getAllPRs(owner: string, repo: string): Promise<ListedPullRequest[] | undefined> {
724-
const cacheKey = this.#getRepoKey(owner, repo, "prs");
725-
726-
const cachedPrs = await this.#redis.json.get<ListedPullRequest[]>(cacheKey);
727-
if (cachedPrs) {
728-
console.log(`Cache hit for PRs for ${owner}`);
729-
return cachedPrs.length ? cachedPrs : undefined;
730-
}
731-
732-
console.log(`Cache miss for PRs for PRs for ${owner}`);
733-
734-
try {
735-
const { data: prs } = await this.#octokit.rest.pulls.list({
736-
owner,
737-
repo,
738-
per_page
739-
});
740-
741-
await this.#redis.json.set(cacheKey, "$", prs);
742-
await this.#redis.expire(cacheKey, FULL_DETAILS_TTL);
743-
744-
return prs;
745-
} catch {
746-
await this.#redis.json.set(cacheKey, "$", []);
747-
await this.#redis.expire(cacheKey, FULL_DETAILS_TTL);
748-
749-
return undefined;
750-
}
703+
async getAllPRs(owner: string, repo: string) {
704+
return await this.#processCached<ListedPullRequest[]>()(
705+
this.#getRepoKey(owner, repo, "prs"),
706+
async () => {
707+
try {
708+
const { data: prs } = await this.#octokit.rest.pulls.list({
709+
owner,
710+
repo,
711+
per_page
712+
});
713+
return prs;
714+
} catch {
715+
return [] as ListedPullRequest[];
716+
}
717+
},
718+
issues => issues,
719+
FULL_DETAILS_TTL
720+
);
751721
}
752722

753723
/**

src/routes/tracker/[org]/[repo]/+page.server.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const closingKeywords = [
1616

1717
export async function load({ params }) {
1818
const members = await gitHubCache.getOrganizationMembers(params.org);
19-
if (!members) error(404, `Organization ${params.org} not found or empty`);
19+
if (!members.length) error(404, `Organization ${params.org} not found or empty`);
2020

2121
const membersNames = members.map(({ login }) => login);
2222

@@ -26,7 +26,7 @@ export async function load({ params }) {
2626
]);
2727
return {
2828
prs: unfilteredPRs
29-
?.filter(({ user, body }) => {
29+
.filter(({ user, body }) => {
3030
if (!membersNames.includes(user?.login ?? "")) return false;
3131
if (!body) return true;
3232
const lowerBody = body.toLowerCase();
@@ -44,9 +44,7 @@ export async function load({ params }) {
4444
.toSorted((a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime())
4545
.slice(0, 10),
4646
issues: unfilteredIssues
47-
?.filter(
48-
({ user, pull_request }) => !pull_request && membersNames.includes(user?.login ?? "")
49-
)
47+
.filter(({ user, pull_request }) => !pull_request && membersNames.includes(user?.login ?? ""))
5048
.toSorted((a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime())
5149
.slice(0, 10)
5250
};

src/routes/tracker/[org]/[repo]/+page.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts">
22
import { Transparent } from "svelte-exmarkdown";
33
import { Separator } from "$lib/components/ui/separator";
4-
import MarkdownRenderer from "$lib/components/MarkdownRenderer.svelte";
54
import GHBadge from "$lib/components/GHBadge.svelte";
5+
import MarkdownRenderer from "$lib/components/MarkdownRenderer.svelte";
66
77
let { data } = $props();
88
@@ -92,15 +92,15 @@
9292
</a>
9393
{/snippet}
9494

95-
{#if data.prs}
95+
{#if data.prs.length}
9696
{@render list(
9797
"Pull requests",
9898
data.prs,
9999
pr => `/pull/${pr.base.repo.owner.login}/${pr.base.repo.name}/${pr.number}`
100100
)}
101101
{/if}
102102

103-
{#if data.issues}
103+
{#if data.issues.length}
104104
{@render list("Issues", data.issues, issue => {
105105
const ownerSlashRepo = issue.html_url
106106
.replace("https://github.com/", "")

0 commit comments

Comments
 (0)