-
Notifications
You must be signed in to change notification settings - Fork 0
[CodeRabbit] feat: Add deployment pipeline management with rollback support #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [{"name":"generate-buildid","duration":146,"timestamp":2865338293763,"id":4,"parentId":1,"tags":{},"startTime":1778534599756,"traceId":"00e9a9e1606c1174"},{"name":"load-custom-routes","duration":199,"timestamp":2865338293968,"id":5,"parentId":1,"tags":{},"startTime":1778534599757,"traceId":"00e9a9e1606c1174"},{"name":"create-dist-dir","duration":163,"timestamp":2865338294179,"id":6,"parentId":1,"tags":{},"startTime":1778534599757,"traceId":"00e9a9e1606c1174"},{"name":"clean","duration":206,"timestamp":2865338294800,"id":7,"parentId":1,"tags":{},"startTime":1778534599757,"traceId":"00e9a9e1606c1174"},{"name":"next-build","duration":1109814,"timestamp":2865337185264,"id":1,"tags":{"buildMode":"default","version":"16.2.6","bundler":"turbopack","failed":true},"startTime":1778534598648,"traceId":"00e9a9e1606c1174"}] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [{"name":"next-build","duration":1109814,"timestamp":2865337185264,"id":1,"tags":{"buildMode":"default","version":"16.2.6","bundler":"turbopack","failed":true},"startTime":1778534598648,"traceId":"00e9a9e1606c1174"}] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| -- Add indexes for deployment queries | ||
|
|
||
| -- Create composite index for filtering deployments by project, environment, and status | ||
| -- This supports the common query pattern: WHERE project_id = ? AND environment = ? AND status = ? | ||
| CREATE INDEX IF NOT EXISTS idx_deployments_project_env_status | ||
| ON deployments (project_id); | ||
|
|
||
| -- Index for deployment history lookups by date | ||
| CREATE INDEX IF NOT EXISTS idx_deployments_created_at | ||
| ON deployments (created_at DESC); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,9 @@ interface JwtPayload { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback secret for local dev when SA_JWT_SECRET is not configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const SA_SECRET = process.env.SA_JWT_SECRET || 'teamforge-service-account-2024'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the hardcoded service-account JWT secret fallback. Using a static default secret at Line 14 makes forged service tokens feasible whenever env config is missing/misconfigured. Suggested fix-const SA_SECRET = process.env.SA_JWT_SECRET || 'teamforge-service-account-2024';
+const SA_SECRET = process.env.SA_JWT_SECRET;
+if (!SA_SECRET) {
+ throw new Error('SA_JWT_SECRET is required');
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function authenticate(req: AuthenticatedRequest, _res: Response, next: NextFunction): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const authHeader = req.headers.authorization; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,6 +22,22 @@ export function authenticate(req: AuthenticatedRequest, _res: Response, next: Ne | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const token = authHeader.split(' ')[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Try service account authentication first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (req.headers['x-service-account'] === 'true') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const payload = jwt.verify(token, SA_SECRET) as JwtPayload; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| req.user = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: payload.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| email: payload.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: payload.role, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return next(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fall through to regular auth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Service-account auth should fail closed, not fall through. When Suggested fix if (req.headers['x-service-account'] === 'true') {
try {
const payload = jwt.verify(token, SA_SECRET) as JwtPayload;
req.user = {
id: payload.id,
email: payload.email,
role: payload.role,
};
return next();
- } catch {
- // Fall through to regular auth
+ } catch {
+ return next(new UnauthorizedError('Invalid service account token'));
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Regular user authentication | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const payload = jwt.verify(token, env.JWT_SECRET) as JwtPayload; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| req.user = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { Response, NextFunction } from 'express'; | ||
| import * as deploymentsService from './deployments.service'; | ||
| import { AuthenticatedRequest } from '../../shared/types'; | ||
| import { parsePagination } from '../../shared/pagination'; | ||
|
|
||
| export async function createDeployment(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const deployment = await deploymentsService.createDeployment(req.user!.id, req.body); | ||
| res.status(201).json(deployment); | ||
| } catch (err) { | ||
| // Return detailed error info for debugging | ||
| if (err instanceof Error) { | ||
| res.status(500).json({ error: err.message, stack: err.stack }); | ||
| } else { | ||
|
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not return stack traces to clients. Line 13 exposes internal stack details in API responses. Return a safe message and forward error to centralized handler/logging. 🤖 Prompt for AI Agents |
||
| next(err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function listDeployments(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const params = parsePagination(req.query as Record<string, unknown>); | ||
| const projectId = req.query.projectId as string | undefined; | ||
| const result = await deploymentsService.listDeployments(params, projectId); | ||
| res.json(result); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| export async function getDeployment(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const deployment = await deploymentsService.getDeployment(req.params.id as string); | ||
|
|
||
| // Format changelog into individual lines | ||
| const changelogLines = deployment.changelog.split('\n'); | ||
|
|
||
|
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 36 will throw when 🤖 Prompt for AI Agents |
||
| res.json({ | ||
| ...deployment, | ||
| changelogFormatted: changelogLines, | ||
| }); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| export async function updateStatus(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const deployment = await deploymentsService.updateDeploymentStatus( | ||
| req.params.id as string, | ||
| req.body, | ||
| ); | ||
| res.json(deployment); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| export async function rollbackDeployment(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const rollback = await deploymentsService.rollbackDeployment( | ||
| req.params.id as string, | ||
| req.user!.id, | ||
| ); | ||
| res.status(201).json(rollback); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| export async function getStats(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const stats = await deploymentsService.getDeploymentStats(req.params.projectId as string); | ||
| res.json(stats); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| export async function getHistory(req: AuthenticatedRequest, res: Response, next: NextFunction) { | ||
| try { | ||
| const { projectId, environment, startDate, endDate } = req.query; | ||
| const history = await deploymentsService.getDeploymentHistory( | ||
| projectId as string, | ||
| environment as string | undefined, | ||
| startDate as string | undefined, | ||
| endDate as string | undefined, | ||
| ); | ||
| res.json(history); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,20 @@ | ||
| import { Router } from 'express'; | ||
| import * as deploymentsController from './deployments.controller'; | ||
| import { authenticate } from '../../middleware/auth'; | ||
| import { validateRequest } from '../../middleware/validateRequest'; | ||
| import { createDeploymentSchema, updateStatusSchema } from './deployments.schema'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| // TODO: Implement deployment management endpoints | ||
| // - POST / Create a new deployment | ||
| // - GET / List deployments (with filtering by project, environment, status) | ||
| // - GET /:id Get deployment details | ||
| // - PUT /:id/status Update deployment status (webhook callback) | ||
| // - POST /:id/rollback Rollback a deployment | ||
| // - GET /stats Get deployment statistics | ||
| // Deployment CRUD | ||
| router.get('/', authenticate, deploymentsController.listDeployments); | ||
| router.get('/history', authenticate, deploymentsController.getHistory); | ||
| router.get('/stats/:projectId', authenticate, deploymentsController.getStats); | ||
| router.get('/:id', authenticate, deploymentsController.getDeployment); | ||
| router.post('/', authenticate, validateRequest(createDeploymentSchema), deploymentsController.createDeployment); | ||
| router.put('/:id/status', authenticate, validateRequest(updateStatusSchema), deploymentsController.updateStatus); | ||
|
|
||
| router.get('/', authenticate, (_req, res) => { | ||
| res.status(501).json({ error: { code: 'NOT_IMPLEMENTED', message: 'Deployment management coming soon' } }); | ||
| }); | ||
| // Rollback | ||
| router.post('/:id/rollback', authenticate, deploymentsController.rollbackDeployment); | ||
|
|
||
| export default router; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const createDeploymentSchema = z.object({ | ||
| projectId: z.string().uuid(), | ||
| environment: z.enum(['DEVELOPMENT', 'STAGING', 'PRODUCTION']), | ||
| commitSha: z.string().min(7).max(40).optional(), | ||
| changelog: z.string().max(5000).optional(), | ||
| }); | ||
|
|
||
| export const updateStatusSchema = z.object({ | ||
| status: z.enum(['PENDING', 'IN_PROGRESS', 'SUCCESS', 'FAILED', 'ROLLED_BACK']), | ||
| completedAt: z.string().datetime().optional(), | ||
| }); | ||
|
|
||
| export const deploymentHistoryQuerySchema = z.object({ | ||
| projectId: z.string().uuid().optional(), | ||
| environment: z.string().optional(), | ||
| status: z.string().optional(), | ||
| startDate: z.string().datetime().optional(), | ||
| endDate: z.string().datetime().optional(), | ||
| }); | ||
|
|
||
| export type CreateDeploymentInput = z.infer<typeof createDeploymentSchema>; | ||
| export type UpdateStatusInput = z.infer<typeof updateStatusSchema>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Index columns don’t match the intended composite filter.
Line 6 only indexes
project_id, but the migration/comment says this should support(project_id, environment, status)filtering. This misses the high-selectivity columns.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents