Skip to content

Commit

Permalink
fix: api key error on cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
tri2820 committed Dec 5, 2024
1 parent 0ce61c6 commit a678fe4
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions web/src/server.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use server";

import { getRequestEvent } from "solid-js/web";

const cacheStore: {
[pr_number: number]: {
result: any;
Expand All @@ -8,9 +10,14 @@ const cacheStore: {
} = {};

export async function fetchRawFile(url: string) {
const event = getRequestEvent();
const api_key =
event?.nativeEvent.context.cloudflare?.env.GITHUB_ACCESS_TOKEN ??
process.env.GITHUB_ACCESS_TOKEN;

const response = await fetch(url, {
headers: {
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
Authorization: `Bearer ${api_key}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});
Expand All @@ -25,41 +32,53 @@ export async function getPRFiles(
files: GitFile[];
cacheStatus: string;
}> {
const cache = cacheStore[number];
if (cache) {
const invalidated = new Date(cache.date_iso) < new Date(last_updated_at);
if (invalidated) {
console.log("cache invalidated!", number);
delete cacheStore[number];
try {
const cache = cacheStore[number];
if (cache) {
const invalidated = new Date(cache.date_iso) < new Date(last_updated_at);
if (invalidated) {
console.log("cache invalidated!", number);
delete cacheStore[number];
} else {
console.log("cache hit!", number);
return {
files: cache.result,
cacheStatus: "hit",
};
}
} else {
console.log("cache hit!", number);
return {
files: cache.result,
cacheStatus: "hit",
};
console.log("cache miss!", number);
}
} else {
console.log("cache miss!", number);
}

const response = await fetch(
`https://api.github.com/repos/unibas-tilics/tilics/pulls/${number}/files`,
{
headers: {
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
"X-GitHub-Api-Version": "2022-11-28",
},
}
);
const json = await response.json();
const event = getRequestEvent();
const api_key =
event?.nativeEvent.context.cloudflare?.env.GITHUB_ACCESS_TOKEN ??
process.env.GITHUB_ACCESS_TOKEN;
const response = await fetch(
`https://api.github.com/repos/unibas-tilics/tilics/pulls/${number}/files`,
{
headers: {
Authorization: `Bearer ${api_key}`,
"X-GitHub-Api-Version": "2022-11-28",
},
}
);
const json = await response.json();

cacheStore[number] = {
result: json,
date_iso: new Date().toISOString(),
};
cacheStore[number] = {
result: json,
date_iso: new Date().toISOString(),
};

return {
files: json,
cacheStatus: "miss",
};
return {
files: json,
cacheStatus: "miss",
};
} catch (e) {
console.error(e);
return {
files: [],
cacheStatus: "error",
};
}
}

0 comments on commit a678fe4

Please sign in to comment.