From ee4c6d407665feab7b32c83266e1b20c3b0a5be4 Mon Sep 17 00:00:00 2001 From: "Ankit Kr. Chowdhury" Date: Sun, 18 May 2025 23:47:56 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Remove=20deprecated=20rough=20API?= =?UTF-8?q?=20and=20related=20functions=20for=20organization=20and=20repos?= =?UTF-8?q?itory=20deletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/rough.ts | 160 ----------------------------------------- src/pages/api/rough.ts | 75 ------------------- 2 files changed, 235 deletions(-) delete mode 100644 src/lib/rough.ts delete mode 100644 src/pages/api/rough.ts diff --git a/src/lib/rough.ts b/src/lib/rough.ts deleted file mode 100644 index dd1d835..0000000 --- a/src/lib/rough.ts +++ /dev/null @@ -1,160 +0,0 @@ -// this is a temporary file for testing purposes -import type { Config } from "@/types/config"; - -export async function deleteAllReposInOrg({ - config, - org, -}: { - config: Partial; - org: string; -}) { - if (!config.giteaConfig?.url || !config.giteaConfig?.token) { - throw new Error("Gitea config is required."); - } - - // Step 1: Get all repositories in the organization - const repoRes = await fetch( - `${config.giteaConfig.url}/api/v1/orgs/${org}/repos`, - { - headers: { - Authorization: `token ${config.giteaConfig.token}`, - }, - } - ); - - if (!repoRes.ok) { - console.error( - `Failed to fetch repos for org ${org}: ${await repoRes.text()}` - ); - return; - } - - const repos = await repoRes.json(); - - // Step 2: Delete each repository - for (const repo of repos) { - const deleteRes = await fetch( - `${config.giteaConfig.url}/api/v1/repos/${org}/${repo.name}`, - { - method: "DELETE", - headers: { - Authorization: `token ${config.giteaConfig.token}`, - }, - } - ); - - if (!deleteRes.ok) { - console.error( - `Failed to delete repo ${repo.name}: ${await deleteRes.text()}` - ); - } else { - console.log(`Successfully deleted repo ${repo.name}`); - } - } -} - -export async function deleteOrg({ - config, - org, -}: { - config: Partial; - org: string; -}) { - if (!config.giteaConfig?.url || !config.giteaConfig?.token) { - throw new Error("Gitea config is required."); - } - - const deleteOrgRes = await fetch( - `${config.giteaConfig.url}/api/v1/orgs/${org}`, - { - method: "DELETE", - headers: { - Authorization: `token ${config.giteaConfig.token}`, - }, - } - ); - - if (!deleteOrgRes.ok) { - console.error(`Failed to delete org ${org}: ${await deleteOrgRes.text()}`); - } else { - console.log(`Successfully deleted org ${org}`); - } -} - -export async function deleteAllOrgs({ - config, - orgs, -}: { - config: Partial; - orgs: string[]; -}) { - for (const org of orgs) { - console.log(`Starting deletion for org: ${org}`); - - // First, delete all repositories in the organization - await deleteAllReposInOrg({ config, org }); - - // Then, delete the organization itself - await deleteOrg({ config, org }); - - console.log(`Finished deletion for org: ${org}`); - } -} - -export async function deleteAllReposInGitea({ - config, -}: { - config: Partial; -}) { - if (!config.giteaConfig?.url || !config.giteaConfig?.token) { - throw new Error("Gitea config is required."); - } - - console.log("Fetching all repositories..."); - - // Step 1: Get all repositories (user + org repos) - const repoRes = await fetch(`${config.giteaConfig.url}/api/v1/user/repos`, { - headers: { - Authorization: `token ${config.giteaConfig.token}`, - }, - }); - - if (!repoRes.ok) { - console.error(`Failed to fetch repositories: ${await repoRes.text()}`); - return; - } - - const repos = await repoRes.json(); - - if (repos.length === 0) { - console.log("No repositories found to delete."); - return; - } - - console.log(`Found ${repos.length} repositories. Starting deletion...`); - - // Step 2: Delete all repositories in parallel - await Promise.allSettled( - repos.map((repo: any) => - fetch( - `${config.giteaConfig?.url}/api/v1/repos/${repo.owner.username}/${repo.name}`, - { - method: "DELETE", - headers: { - Authorization: `token ${config.giteaConfig?.token}`, - }, - } - ).then(async (res) => { - if (!res.ok) { - console.error( - `Failed to delete repo ${repo.full_name}: ${await res.text()}` - ); - } else { - console.log(`Successfully deleted repo ${repo.full_name}`); - } - }) - ) - ); - - console.log("Finished deleting all repositories."); -} diff --git a/src/pages/api/rough.ts b/src/pages/api/rough.ts deleted file mode 100644 index 818f7ca..0000000 --- a/src/pages/api/rough.ts +++ /dev/null @@ -1,75 +0,0 @@ -//this is a rough api route that will delete all the orgs and repos in gitea. can be used for some other testing purposes as well - -import { configs, db } from "@/lib/db"; -import { deleteAllOrgs, deleteAllReposInGitea } from "@/lib/rough"; -import type { APIRoute } from "astro"; -import { eq } from "drizzle-orm"; - -export const GET: APIRoute = async ({ request }) => { - try { - const url = new URL(request.url); - const userId = url.searchParams.get("userId"); - - if (!userId) { - return new Response(JSON.stringify({ error: "Missing userId" }), { - status: 400, - headers: { "Content-Type": "application/json" }, - }); - } - - // Fetch user configuration - const userConfig = await db - .select() - .from(configs) - .where(eq(configs.userId, userId)) - .limit(1); - - if (userConfig.length === 0) { - return new Response( - JSON.stringify({ error: "No configuration found for this user" }), - { - status: 404, - headers: { "Content-Type": "application/json" }, - } - ); - } - - const config = userConfig[0]; - - if (!config.githubConfig || !config.githubConfig.token) { - return new Response( - JSON.stringify({ error: "GitHub token is missing in config" }), - { status: 400, headers: { "Content-Type": "application/json" } } - ); - } - - //adjust this based on the orgs you you want to delete - await deleteAllOrgs({ - config, - orgs: [ - "Neucruit", - "initify", - "BitBustersx719", - "uiastra", - "conductor-oss", - "HackForge-JUSL", - "vercel", - ], - }); - - await deleteAllReposInGitea({ - config, - }); - - return new Response(JSON.stringify({ message: "Process completed." }), { - status: 200, - headers: { "Content-Type": "application/json" }, - }); - } catch (error) { - console.error("Error in long-running process:", error); - return new Response(JSON.stringify({ error: "Internal server error" }), { - status: 500, - headers: { "Content-Type": "application/json" }, - }); - } -};