create git repos

This commit is contained in:
orosmatthew 2023-05-01 14:16:31 -04:00
parent 8a886d00b8
commit d37b94c465
4 changed files with 65 additions and 5 deletions

View File

@ -4,6 +4,8 @@ import { join } from 'path';
let gitRunning = false; let gitRunning = false;
export let repos: Git;
export function startGitServer() { export function startGitServer() {
if (!gitRunning) { if (!gitRunning) {
const port = const port =
@ -16,7 +18,7 @@ export function startGitServer() {
} }
const repoDir = process.env.GIT_REPO_DIR; const repoDir = process.env.GIT_REPO_DIR;
const repos = new Git(join(repoDir), { repos = new Git(join(repoDir), {
autoCreate: false, autoCreate: false,
authenticate: ({ type, user, repo }, next) => { authenticate: ({ type, user, repo }, next) => {
if (type == 'push') { if (type == 'push') {

View File

@ -1,4 +1,4 @@
import { error, redirect } from '@sveltejs/kit'; import { error, redirect, type Actions } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { db } from '$lib/server/prisma'; import { db } from '$lib/server/prisma';
@ -24,3 +24,17 @@ export const load = (async ({ params }) => {
}) })
}; };
}) satisfies PageServerLoad; }) satisfies PageServerLoad;
export const actions = {
delete: async ({ params }) => {
if (!params.contestId) {
return { success: false };
}
try {
await db.contest.delete({ where: { id: parseInt(params.contestId) } });
} catch {
return { success: false };
}
throw redirect(302, '/admin/contests');
}
} satisfies Actions;

View File

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { enhance } from '$app/forms';
import type { PageData } from './$types'; import type { PageData } from './$types';
export let data: PageData; export let data: PageData;
@ -14,6 +15,24 @@
<div class="col-6"> <div class="col-6">
<a href="/admin/contests" class="btn btn-outline-primary">All Contests</a> <a href="/admin/contests" class="btn btn-outline-primary">All Contests</a>
</div> </div>
<div class="col-6">
<div class="text-end">
<form
method="POST"
action="?/delete"
use:enhance={({ cancel }) => {
if (!confirm('Are you sure?')) {
cancel();
}
return async ({ update }) => {
update();
};
}}
>
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
</div> </div>
<div class="mt-3 row"> <div class="mt-3 row">

View File

@ -1,5 +1,9 @@
import { db } from '$lib/server/prisma'; import { db } from '$lib/server/prisma';
import { join } from 'path';
import type { Actions, PageServerLoad } from './$types'; import type { Actions, PageServerLoad } from './$types';
import fs from 'fs';
import { error } from 'console';
import { repos } from '$lib/server/gitserver';
export const load = (async () => { export const load = (async () => {
const teams = await db.team.findMany(); const teams = await db.team.findMany();
@ -15,7 +19,7 @@ export const load = (async () => {
}) satisfies PageServerLoad; }) satisfies PageServerLoad;
export const actions = { export const actions = {
create: async ({ request }) => { create: async ({ request, params }) => {
const data = await request.formData(); const data = await request.formData();
const name = data.get('name'); const name = data.get('name');
const problems = (await db.problem.findMany()).filter((problem) => { const problems = (await db.problem.findMany()).filter((problem) => {
@ -27,7 +31,7 @@ export const actions = {
if (!name) { if (!name) {
return { success: false }; return { success: false };
} }
await db.contest.create({ const createdContest = await db.contest.create({
data: { data: {
name: name.toString(), name: name.toString(),
teams: { teams: {
@ -40,8 +44,29 @@ export const actions = {
return { id: problem.id }; return { id: problem.id };
}) })
} }
},
include: { teams: true }
});
// Create repos
const repoDir = process.env.GIT_REPO_DIR;
if (!repoDir) {
throw error(500, 'No repo directory specified in env');
}
if (fs.existsSync(join(repoDir, createdContest.id.toString()))) {
fs.rmdirSync(join(repoDir, createdContest.id.toString()), { recursive: true });
}
createdContest.teams.forEach((team) => {
repos.create(join(createdContest.id.toString(), team.id.toString()), (e) => {
if (e) {
throw error(500, `Unable to create repo for team: ${team.name}: ${e.message}`);
} }
}); });
});
return { success: true }; return { success: true };
} }
} satisfies Actions; } satisfies Actions;