PIT: add backend logic for deep PIT and rollback

This commit is contained in:
carlosmonastyrski
2025-05-14 10:26:41 -03:00
parent 9e1d38a27b
commit 8bfd3913da
14 changed files with 1818 additions and 462 deletions
+96 -13
View File
@@ -3,9 +3,52 @@ import { z } from "zod";
import { readLimit } from "@app/server/config/rateLimiter";
export const registerPITRouter = async (server: FastifyZodProvider) => {
// Get all commits for a folder
server.route({
method: "GET",
url: "/diff",
url: "/commits/:folderId",
config: {
rateLimit: readLimit
},
schema: {
params: z.object({
folderId: z.string().trim()
}),
response: {
200: z.any()
}
},
handler: async (req) => {
const commits = await server.services.folderCommit.getCommitsByFolderId(req.params.folderId);
return commits;
}
});
// Get commit changes for a specific commit
server.route({
method: "GET",
url: "/commits/:commitId/changes",
config: {
rateLimit: readLimit
},
schema: {
params: z.object({
commitId: z.string().trim()
}),
response: {
200: z.any()
}
},
handler: async (req) => {
const changes = await server.services.folderCommit.getCommitChanges(req.params.commitId);
return changes;
}
});
// Compare folder states between commits
server.route({
method: "GET",
url: "/compare",
config: {
rateLimit: readLimit
},
@@ -19,12 +62,16 @@ export const registerPITRouter = async (server: FastifyZodProvider) => {
}
},
handler: async (req) => {
const backup = await server.services.folderCommit.compareFolderStates(req.query.fromCommit, req.query.toCommit);
const diff = await server.services.folderCommit.compareFolderStates({
currentCommitId: req.query.fromCommit,
targetCommitId: req.query.toCommit
});
return backup;
return diff;
}
});
// Rollback to a previous commit
server.route({
method: "POST",
url: "/rollback",
@@ -36,6 +83,46 @@ export const registerPITRouter = async (server: FastifyZodProvider) => {
fromCommit: z.string().trim(),
toCommit: z.string().trim(),
folderId: z.string().trim(),
projectId: z.string().trim(),
reconstructNewFolders: z.boolean().default(false)
}),
response: {
200: z.any()
}
},
handler: async (req) => {
const diff = await server.services.folderCommit.compareFolderStates({
currentCommitId: req.body.fromCommit,
targetCommitId: req.body.toCommit
});
const response = await server.services.folderCommit.applyFolderStateDifferences({
differences: diff,
actorInfo: {
actorType: req.permission?.type || "PLATFORM",
actorId: req.permission?.id,
message: "Rollback to previous commit"
},
folderId: req.body.folderId,
projectId: req.body.projectId,
reconstructNewFolders: req.body.reconstructNewFolders
});
return response;
}
});
// Deep rollback to a specific commit
server.route({
method: "POST",
url: "/deep-rollback",
config: {
rateLimit: readLimit
},
schema: {
body: z.object({
commitId: z.string().trim(),
envId: z.string().trim(),
projectId: z.string().trim()
}),
response: {
@@ -43,19 +130,15 @@ export const registerPITRouter = async (server: FastifyZodProvider) => {
}
},
handler: async (req) => {
const diff = await server.services.folderCommit.compareFolderStates(req.body.fromCommit, req.body.toCommit);
const response = await server.services.folderCommit.applyFolderStateDifferences(
diff,
{
actorType: req.permission?.type || "PLATFORM",
actorId: req.permission?.id,
message: "Rollback to previous commit"
},
req.body.folderId,
await server.services.folderCommit.deepRollbackFolder(
req.body.commitId,
req.body.envId,
req.permission?.id || "PLATFORM",
req.permission?.type || "PLATFORM",
req.body.projectId
);
return response;
return { success: true };
}
});
};