Skip to content

Commit

Permalink
✨ Add a button to drop all repos in local
Browse files Browse the repository at this point in the history
  • Loading branch information
Sstark97 committed Nov 6, 2024
1 parent 9c24309 commit 5a28a46
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
31 changes: 23 additions & 8 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Textarea } from "@/components/ui/textarea"
import { Button } from "@/components/ui/button"
import { Card, CardContent } from "@/components/ui/card"
import { Label } from "@/components/ui/label"
import { Code2, Shuffle } from "lucide-react"
import {OpenAnonymousRandomRepositoryServerAction} from "@/src/backend/server";
import {Textarea} from "@/components/ui/textarea"
import {Button} from "@/components/ui/button"
import {Card, CardContent} from "@/components/ui/card"
import {Label} from "@/components/ui/label"
import {Code2, Shuffle} from "lucide-react"
import {deleteAllRepositoriesServerAction, openAnonymousRandomRepositoryServerAction} from "@/src/backend/server";

export default function CodeReviewQuiz() {
const isLocal = process.env.NODE_ENV === 'development'

return (
<main className="flex-1 flex items-center justify-center p-4">
<Card
Expand All @@ -21,7 +23,7 @@ export default function CodeReviewQuiz() {
</h2>
<p className="text-gray-300">¡Deja que el azar elija tu próxima revisión de código!</p>
</div>
<form action={OpenAnonymousRandomRepositoryServerAction} className="space-y-4">
<form action={openAnonymousRandomRepositoryServerAction} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="urls" className="text-lg font-semibold text-gray-300">
URLs de Repositorios
Expand All @@ -33,13 +35,26 @@ export default function CodeReviewQuiz() {
className="h-32 bg-gray-800 border-gray-700 text-white placeholder-gray-500 focus:border-purple-500 focus:ring-purple-500"
/>
</div>

<Button
type="submit"
className="w-full bg-gradient-to-r from-gray-700 to-[#39b3c2] hover:from-gray-600 hover:to-[#2a8a96] text-white font-bold py-3 rounded-full transition-all duration-300 transform hover:scale-105"
className="w-full bg-gradient-to-r from-gray-700 to-[#39b3c2] hover:from-gray-600 hover:to-[#2a8a96] text-white font-bold py-3 rounded-full transition-all duration-300 transform hover:scale-105 mr-2"
>
<Shuffle className="mr-2 h-5 w-5"/> ¡Girar la Ruleta!
</Button>
</form>
{isLocal ? (
<form action={deleteAllRepositoriesServerAction} className="space-y-4">
<Button
type="submit"
className="w-full bg-gradient-to-r from-gray-700 to-[#39b3c2] hover:from-gray-600 hover:to-[#2a8a96] text-white font-bold py-3 rounded-full transition-all duration-300 transform hover:scale-105"
>
Limpiar Repositorios
</Button>
</form>
)
: <></>
}
</CardContent>
</Card>
</main>
Expand Down
32 changes: 32 additions & 0 deletions src/backend/gitRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,36 @@ export class GitRepositoryManager implements RepositoryManager {
console.error('Error al subir el código al nuevo repositorio:', error);
}
}

async deleteAllReposInOrg(org: string) {
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
try {
// Obtener todos los repositorios de la organización
const repos = await octokit.paginate(octokit.rest.repos.listForOrg, {
org,
per_page: 100,
});

console.log(`Repositorios encontrados en la organización "${org}": ${repos.length}`);

// Eliminar cada repositorio
for (const repo of repos) {
try {
await octokit.rest.repos.delete({
owner: org,
repo: repo.name,
});
console.log(`Repositorio eliminado: ${repo.name}`);
} catch (error) {
console.error(`Error al eliminar el repositorio ${repo.name}:`, error);
}
}

console.log('Todos los repositorios han sido eliminados.');
} catch (error) {
console.error('Error al obtener repositorios:', error);
}
}
}
6 changes: 5 additions & 1 deletion src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import {v4 as uuidv4} from 'uuid';
import {RepositoryManager} from "@/src/backend/repositoryManager";
import {randomInt} from "node:crypto";

export async function OpenAnonymousRandomRepositoryServerAction(formData: FormData) {
export async function openAnonymousRandomRepositoryServerAction(formData: FormData) {
const urls = formData.get("urls") as string
const urlList = urls.split("\n").filter(url => url.trim() !== "").map(url => url.replace("\r", ""))
console.log(urlList)
await execute(urlList, new GitRepositoryManager());
}

export async function deleteAllRepositoriesServerAction() {
await new GitRepositoryManager().deleteAllReposInOrg("anonymous-code-review");
}

export async function execute(repos: string[], repositoryManager: RepositoryManager) {
const tmpDir = os.tmpdir();
const repoUrl = getRandomFrom(repos);
Expand Down

0 comments on commit 5a28a46

Please sign in to comment.