|
| 1 | +import { ScheduleService } from "../services/schedule.service"; |
| 2 | +import { execFile } from "child_process"; |
| 3 | +import Logger from "../lib/logger"; |
| 4 | +import { |
| 5 | + createBackup, |
| 6 | + resumeCronJob, |
| 7 | + scheduleCronJobs, |
| 8 | + stopCronJob, |
| 9 | +} from "../server"; |
| 10 | +import { Request, Response } from "express"; |
| 11 | +import { AuthenticatedRequest } from "../types/AuthenticatedRequest"; |
| 12 | + |
| 13 | +const logger = new Logger("schedule.controller"); |
| 14 | + |
| 15 | +export async function getSchedules(req: Request, res: Response) { |
| 16 | + const username = (req as AuthenticatedRequest).user.username; |
| 17 | + const schedules = await ScheduleService.getSchedulesByUser(username); |
| 18 | + return res.json(schedules); |
| 19 | +} |
| 20 | + |
| 21 | +export async function createSchedule(req: Request, res: Response) { |
| 22 | + const schedule = req.body; |
| 23 | + const username = (req as AuthenticatedRequest).user.username; |
| 24 | + |
| 25 | + let repoUrl: URL; |
| 26 | + try { |
| 27 | + repoUrl = new URL(schedule.repository); |
| 28 | + } catch (error) { |
| 29 | + return res.status(400).send("Invalid repository URL"); |
| 30 | + } |
| 31 | + |
| 32 | + const initialUrl = repoUrl.href; |
| 33 | + if (schedule.private) { |
| 34 | + const accessToken = await ScheduleService.getAccessToken( |
| 35 | + schedule.accessTokenId, |
| 36 | + ); |
| 37 | + if (!accessToken) { |
| 38 | + return res.status(400).send("Access token not found"); |
| 39 | + } |
| 40 | + |
| 41 | + repoUrl.href = repoUrl.href.replace( |
| 42 | + "https://", |
| 43 | + `https://${accessToken.token}@`, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + const options = { |
| 48 | + env: { |
| 49 | + ...process.env, |
| 50 | + GIT_ASKPASS: "/bin/false", |
| 51 | + }, |
| 52 | + }; |
| 53 | + const child = execFile( |
| 54 | + "git", |
| 55 | + ["ls-remote", repoUrl.href], |
| 56 | + options, |
| 57 | + async (error, stdout, stderr) => { |
| 58 | + if (error) { |
| 59 | + logger.error(error.toString()); |
| 60 | + return res |
| 61 | + .status(400) |
| 62 | + .send( |
| 63 | + "Invalid repository. Either it does not exist or you forgot to select an access token.", |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const newSchedule = await ScheduleService.addSchedule( |
| 68 | + username, |
| 69 | + schedule, |
| 70 | + initialUrl, |
| 71 | + ); |
| 72 | + scheduleCronJobs(); |
| 73 | + return res.json(newSchedule); |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + const timeout = setTimeout(() => { |
| 78 | + child.kill(); |
| 79 | + }, 5000); |
| 80 | + |
| 81 | + child.on("exit", (code) => { |
| 82 | + clearTimeout(timeout); |
| 83 | + if (code === null) { |
| 84 | + return res.status(400).send("The process took too long and was aborted."); |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +export async function updateSchedule(req: Request, res: Response) { |
| 90 | + const { id } = req.params; |
| 91 | + const schedule = req.body; |
| 92 | + |
| 93 | + let url: URL; |
| 94 | + |
| 95 | + try { |
| 96 | + url = new URL(schedule.repository); |
| 97 | + } catch (error) { |
| 98 | + return res.status(400).send("Invalid repository URL"); |
| 99 | + } |
| 100 | + |
| 101 | + const options = { |
| 102 | + env: { |
| 103 | + ...process.env, |
| 104 | + GIT_ASKPASS: "/bin/false", |
| 105 | + }, |
| 106 | + }; |
| 107 | + const child = execFile( |
| 108 | + "git", |
| 109 | + ["ls-remote", url.href], |
| 110 | + options, |
| 111 | + async (error, stdout, stderr) => { |
| 112 | + if (error) { |
| 113 | + logger.error(error.toString()); |
| 114 | + return res |
| 115 | + .status(400) |
| 116 | + .send( |
| 117 | + "Invalid repository. Either it does not exist or you forgot to select an access token.", |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const updatedSchedule = await ScheduleService.editSchedule(schedule, id); |
| 122 | + scheduleCronJobs(); |
| 123 | + return res.json(updatedSchedule); |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
| 127 | + const timeout = setTimeout(() => { |
| 128 | + child.kill(); |
| 129 | + }, 5000); |
| 130 | + |
| 131 | + child.on("exit", (code) => { |
| 132 | + clearTimeout(timeout); |
| 133 | + if (code === null) { |
| 134 | + return res.status(400).send("The process took too long and was aborted."); |
| 135 | + } |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +export async function backupScheduleNow(req: Request, res: Response) { |
| 140 | + const { id } = req.params; |
| 141 | + |
| 142 | + const schedule = await ScheduleService.getScheduleById(id); |
| 143 | + |
| 144 | + if (!schedule) { |
| 145 | + return res.status(404).send("Schedule not found"); |
| 146 | + } |
| 147 | + |
| 148 | + createBackup(schedule.id, schedule.name, schedule.repository); |
| 149 | + return res.send( |
| 150 | + "Backup started. Depending on the size of the repository, this may take a while.", |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +export async function pauseSchedule(req: Request, res: Response) { |
| 155 | + const { id } = req.params; |
| 156 | + |
| 157 | + const pausedSchedule = await ScheduleService.pauseSchedule(id); |
| 158 | + stopCronJob(parseInt(id)); |
| 159 | + return res.json(pausedSchedule); |
| 160 | +} |
| 161 | + |
| 162 | +export async function resumeSchedule(req: Request, res: Response) { |
| 163 | + const { id } = req.params; |
| 164 | + |
| 165 | + const resumedSchedule = await ScheduleService.resumeSchedule(id); |
| 166 | + resumeCronJob(parseInt(id)); |
| 167 | + return res.json(resumedSchedule); |
| 168 | +} |
| 169 | + |
| 170 | +export async function deleteSchedule(req: Request, res: Response) { |
| 171 | + const { id } = req.params; |
| 172 | + |
| 173 | + await ScheduleService.deleteSchedule(id); |
| 174 | + stopCronJob(parseInt(id), true); |
| 175 | + return res.send("Schedule deleted"); |
| 176 | +} |
0 commit comments