import AppQueries from "../domain/AppQueries"; import { UpdateDefinition } from "../domain/projections/ApplicationUpdates"; import Caprover, { Application } from "../services/Caprover"; import DockerHub from "../services/DockerHub"; import Layout, { html } from "../ui/Layout"; const UpdateForm = (application: Application, latestVersions: string[]) => { // sort by number of "dots" in the version to have the most specific version first latestVersions.sort((a, b) => { return b.split(".").length - a.split(".").length; }); return html`
`; }; const PendingUpdates = (pendingUpdates: UpdateDefinition[]) => { if (pendingUpdates.length === 0) { return ""; } return html`

Pending updates

`; }; const Page = ( application: Application, latestVersions: string[], pendingUpdates: UpdateDefinition[] ) => { return html`

Updating ${application.name}

${PendingUpdates(pendingUpdates)}
Last deployment
${application.lastDeployedAt.toLocaleString("fr-FR")}
Current version
${application.dockerImage ? `Docker hub ${application.dockerImage.name}:${application.dockerImage.tag} ` : `${application.imageName}… check yourself!`}
Latest versions
${latestVersions.length === 0 ? "No version found" : UpdateForm(application, latestVersions)}
`; }; export default async ( req: Request, caprover: Caprover, dockerHub: DockerHub, queries: AppQueries ): Promise => { if (req.method === "POST") { const body = await req.formData(); await caprover.updateApplication( body.get("appName") as string, body.get("version") as string ); } const applications = await caprover.getApps(); const nameFilter = new URL(req.url).searchParams.get("name"); const appToUpdate = applications .filter((app) => { // can we help to update this app? return app.dockerImage; }) .find((app) => { return nameFilter ? app.name === nameFilter : app.isOlderThan(30); }); if (!appToUpdate) { return new Response(Layout(html`

No application to update 🎉

`), { headers: { "Content-Type": "text/html" }, }); } const latestVersions = await dockerHub.getLatestVersions( appToUpdate.dockerImage!.name ); const pendingUpdates = queries.pendingApplicationUpdates(appToUpdate.name); return new Response( Layout(Page(appToUpdate, latestVersions, pendingUpdates)), { headers: { "Content-Type": "text/html" }, } ); };