From ec53dfca8a924837fae6baeba7c646e9c400714b Mon Sep 17 00:00:00 2001 From: Naheel Muhammed Date: Mon, 9 Feb 2026 14:41:52 +0530 Subject: [PATCH 1/2] create and add octokit for github data Co-Authored-By: Adithyan <100783336+adithyanmkd@users.noreply.github.com> --- src/lib/octokit.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/lib/octokit.ts diff --git a/src/lib/octokit.ts b/src/lib/octokit.ts new file mode 100644 index 0000000..520d637 --- /dev/null +++ b/src/lib/octokit.ts @@ -0,0 +1,28 @@ +import { Octokit } from "octokit"; + +/** + * Note: + * We use an environment variable for the token to keep it out of the source code. + * Even if the token is missing, we initialize the client (it will just be rate-limited). + */ +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; + +export const octokit = new Octokit({ + auth: GITHUB_TOKEN || undefined, +}); + +/** + * Helper to fetch repo metadata safely + */ +export async function getRepoData(owner: string, repo: string) { + try { + const { data } = await octokit.rest.repos.get({ + owner, + repo, + }); + return data; + } catch (error) { + console.error("Error fetching GitHub repo:", error); + return null; + } +} \ No newline at end of file From 8f3cb1838cb7f82ac5752103b788a333dcebbc3f Mon Sep 17 00:00:00 2001 From: Naheel Muhammed Date: Mon, 9 Feb 2026 14:56:26 +0530 Subject: [PATCH 2/2] Update octokit.ts --- src/lib/octokit.ts | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/lib/octokit.ts b/src/lib/octokit.ts index 520d637..ccf478b 100644 --- a/src/lib/octokit.ts +++ b/src/lib/octokit.ts @@ -1,28 +1,39 @@ import { Octokit } from "octokit"; -/** - * Note: - * We use an environment variable for the token to keep it out of the source code. - * Even if the token is missing, we initialize the client (it will just be rate-limited). - */ -const GITHUB_TOKEN = process.env.GITHUB_TOKEN; +// Private variable for Singleton pattern +let _octokit: Octokit | null = null; + +export function getOctokit(): Octokit { + if (_octokit) return _octokit; + + const auth = process.env.GITHUB_TOKEN; + + // We don't throw an error here because Octokit can work without a token + // (unauthenticated), though it will be heavily rate-limited. + _octokit = new Octokit({ + auth: auth || undefined, + }); -export const octokit = new Octokit({ - auth: GITHUB_TOKEN || undefined, -}); + return _octokit; +} /** - * Helper to fetch repo metadata safely + * Fetches repository metadata safely. + * Notice we only log the error message, NOT the full error object. */ export async function getRepoData(owner: string, repo: string) { + const client = getOctokit(); + try { - const { data } = await octokit.rest.repos.get({ + const { data } = await client.rest.repos.get({ owner, repo, }); return data; - } catch (error) { - console.error("Error fetching GitHub repo:", error); + } catch (error: unknown) { + const message = error instanceof Error ? error.message : "An unknown error occurred"; + console.error("Error fetching GitHub repo:", message); + return null; } -} \ No newline at end of file +}