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;
export let repos: Git;
export function startGitServer() {
if (!gitRunning) {
const port =
@ -16,7 +18,7 @@ export function startGitServer() {
}
const repoDir = process.env.GIT_REPO_DIR;
const repos = new Git(join(repoDir), {
repos = new Git(join(repoDir), {
autoCreate: false,
authenticate: ({ type, user, repo }, next) => {
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 { db } from '$lib/server/prisma';
@ -24,3 +24,17 @@ export const load = (async ({ params }) => {
})
};
}) 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">
import { enhance } from '$app/forms';
import type { PageData } from './$types';
export let data: PageData;
@ -14,6 +15,24 @@
<div class="col-6">
<a href="/admin/contests" class="btn btn-outline-primary">All Contests</a>
</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 class="mt-3 row">

View File

@ -1,5 +1,9 @@
import { db } from '$lib/server/prisma';
import { join } from 'path';
import type { Actions, PageServerLoad } from './$types';
import fs from 'fs';
import { error } from 'console';
import { repos } from '$lib/server/gitserver';
export const load = (async () => {
const teams = await db.team.findMany();
@ -15,7 +19,7 @@ export const load = (async () => {
}) satisfies PageServerLoad;
export const actions = {
create: async ({ request }) => {
create: async ({ request, params }) => {
const data = await request.formData();
const name = data.get('name');
const problems = (await db.problem.findMany()).filter((problem) => {
@ -27,7 +31,7 @@ export const actions = {
if (!name) {
return { success: false };
}
await db.contest.create({
const createdContest = await db.contest.create({
data: {
name: name.toString(),
teams: {
@ -40,8 +44,29 @@ export const actions = {
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 };
}
} satisfies Actions;