From 9eb3faad4cc40aaa09d8da47718b1f7d03eb48ad Mon Sep 17 00:00:00 2001 From: mAmineChniti Date: Mon, 18 Mar 2024 18:31:27 +0100 Subject: [PATCH 1/2] getting repo size directly from github rest api, added a new format function (github repo size comes in KB) --- page/tsconfig.json | 2 +- public/manifest.json | 38 +++++------------------- src/scripts/internal/api.ts | 37 +++++++++++++++++++++++ src/scripts/internal/dom-manipulation.ts | 16 +++++----- src/scripts/internal/format.ts | 29 ++++++++++++++++++ tsconfig.json | 6 ++-- 6 files changed, 85 insertions(+), 43 deletions(-) diff --git a/page/tsconfig.json b/page/tsconfig.json index ff647ec..174cade 100644 --- a/page/tsconfig.json +++ b/page/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "astro/tsconfigs/strict", - "include": ["../src/scripts/**/*.ts"] + "include": ["../src/scripts/**/*.ts"], } diff --git a/public/manifest.json b/public/manifest.json index ae01dc8..bb039f8 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -6,51 +6,29 @@ "version": "0.2.7", "web_accessible_resources": [ { - "resources": [ - "script.js" - ], - "matches": [ - "https://github.com/*" - ] + "resources": ["script.js"], + "matches": ["https://github.com/*"] } ], "content_scripts": [ { - "matches": [ - "https://github.com/*" - ], - "js": [ - "content.js" - ], + "matches": ["https://github.com/*"], + "js": ["content.js"], "run_at": "document_end", - "css": [ - "content.css" - ] + "css": ["content.css"] } ], "action": { "default_popup": "index.html" }, - "permissions": [ - "storage", - "tabs", - "webNavigation" - ], + "permissions": ["storage", "tabs", "webNavigation"], "background": { - "scripts": [ - "background.js" - ] + "service_worker": "background.js" }, "icons": { "16": "images/icon16.png", "32": "images/icon32.png", "48": "images/icon48.png", "128": "images/icon128.png" - }, - "browser_specific_settings": { - "gecko": { - "id": "github-repo-size@gmail.com", - "strict_min_version": "42.0" - } } -} \ No newline at end of file +} diff --git a/src/scripts/internal/api.ts b/src/scripts/internal/api.ts index 2254eac..1f24767 100644 --- a/src/scripts/internal/api.ts +++ b/src/scripts/internal/api.ts @@ -152,3 +152,40 @@ export async function getRepoInfo( }); return response; } + +/** + * Get the size of the repository in the specified branch. + * @param repo - The repository name in the format 'owner/repo' + * @param branch - The branch name + * @returns The size of the repository in KB + */ +export async function getRepoSize( + username: string | undefined, + reponame: string | undefined +): Promise { + const token = await getToken(); + const headers = new Headers({ + 'User-Agent': 'Your-User-Agent', // Replace with your user agent + Accept: 'application/vnd.github.v3+json', + ...(token && { Authorization: `Bearer ${token}` }), + }); + + try { + const response = await fetch( + `https://api.github.com/repos/${username}/${reponame}`, + { headers } + ); + if (!response.ok) { + throw new Error( + `Failed to fetch repo details: ${response.status} ${response.statusText}` + ); + } + + const data = await response.json(); + const size = data.size; + return size; + } catch (error) { + console.error('Error fetching repository size for branch:', error); + return -1; + } +} diff --git a/src/scripts/internal/dom-manipulation.ts b/src/scripts/internal/dom-manipulation.ts index 92f5170..f730424 100644 --- a/src/scripts/internal/dom-manipulation.ts +++ b/src/scripts/internal/dom-manipulation.ts @@ -3,7 +3,6 @@ import { createSizeSpan, createEmptySizeSpan, createTotalSizeElement, - formatBytes, getFileAnchors, getFirstTd, getNavButtons, @@ -11,12 +10,14 @@ import { getRepoInfo, getSize, getSizeLabel, + getRepoSize, getTable, getThead, getTotalSizeButton, getTotalSizeSpan, + formatKilobytes, } from '.'; -import type { GRSUpdate, GitHubTree } from './types'; +import type { GRSUpdate, PathObject } from './types'; /** * Insert the size label element into the table head. @@ -100,7 +101,7 @@ function insertToFileExplorer( * * @param repoInfo - The repo info */ -function setTotalSize(repoInfo: GitHubTree) { +async function setTotalSize(pathObject: PathObject) { const navButtons = getNavButtons(); if (!navButtons) { return; @@ -128,12 +129,9 @@ function setTotalSize(repoInfo: GitHubTree) { return; } - let totalSize = 0; - repoInfo.tree.forEach((item) => { - totalSize += item.size ?? 0; - }); + const totalSize = await getRepoSize(pathObject.owner, pathObject.repo); - span.innerText = formatBytes(totalSize); + span.innerText = formatKilobytes(totalSize); navButtons.appendChild(totalSizeButton); } @@ -213,7 +211,7 @@ export async function updateDOM() { insertSizeColumn(); - setTotalSize(repoInfo); + setTotalSize(pathObject); updates.forEach(({ anchor, span, index }) => { // for some reason the rows have two td's with name of each file diff --git a/src/scripts/internal/format.ts b/src/scripts/internal/format.ts index 9683e26..771bbcc 100644 --- a/src/scripts/internal/format.ts +++ b/src/scripts/internal/format.ts @@ -23,3 +23,32 @@ export function formatBytes(bytes: number, decimals: number = 2): string { const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } + +/** + * Formats a number in kilobytes to a string with a given amount of decimals. + * + * @param kilobytes - The number in kilobytes to format + * @param decimals - The number of decimals + * @returns The formatted string + * @example + * ```ts + * formatKilobytes(1024); + * // '1 KB' + * ``` + * @example + * ```ts + * formatKilobytes(1024, 2); + * // '1.00 KB' + * ``` + */ +export function formatKilobytes( + kilobytes: number, + decimals: number = 2 +): string { + if (kilobytes === 0) return '0 KB'; + const k = 1000; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const i = Math.floor(Math.log(kilobytes) / Math.log(k)); + return parseFloat((kilobytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} diff --git a/tsconfig.json b/tsconfig.json index 36d909a..d2afef5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "types": [ "bun-types", // add Bun type definitions "./types.d.ts", // add your own type definitions - "@types/chrome" + "@types/chrome", ], // enable latest features @@ -27,6 +27,6 @@ // best practices "strict": true, "forceConsistentCasingInFileNames": true, - "skipLibCheck": true - } + "skipLibCheck": true, + }, } From 4154abac658b696f68d5f77dd83de8485bf99e7e Mon Sep 17 00:00:00 2001 From: mAmineChniti Date: Mon, 18 Mar 2024 22:55:45 +0100 Subject: [PATCH 2/2] removed page/.vscode, and ran format script --- page/.vscode/extensions.json | 4 ---- page/.vscode/launch.json | 11 ----------- 2 files changed, 15 deletions(-) delete mode 100644 page/.vscode/extensions.json delete mode 100644 page/.vscode/launch.json diff --git a/page/.vscode/extensions.json b/page/.vscode/extensions.json deleted file mode 100644 index 22a1505..0000000 --- a/page/.vscode/extensions.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "recommendations": ["astro-build.astro-vscode"], - "unwantedRecommendations": [] -} diff --git a/page/.vscode/launch.json b/page/.vscode/launch.json deleted file mode 100644 index d642209..0000000 --- a/page/.vscode/launch.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "command": "./node_modules/.bin/astro dev", - "name": "Development server", - "request": "launch", - "type": "node-terminal" - } - ] -}