Skip to content
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

feat(frontend,backend): insights #1289

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/auth-service/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NODE_ENV=

PORT=

CLERK_SECRET_KEY=
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine3.16 as base
FROM node:20-alpine3.16 AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
Expand Down
6 changes: 3 additions & 3 deletions apps/auth-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codemod-com/auth-service",
"version": "0.0.15",
"version": "0.0.18",
"scripts": {
"build": "tsc && node esbuild.config.js",
"start": "node build/index.js"
Expand All @@ -21,11 +21,11 @@
"tsx": "^4.7.1"
},
"dependencies": {
"@clerk/backend": "catalog:",
"@clerk/fastify": "catalog:",
"@codemod-com/auth": "workspace:*",
"@codemod-com/database": "workspace:*",
"@codemod-com/utilities": "workspace:*",
"@clerk/backend": "catalog:",
"@clerk/fastify": "catalog:",
"@fastify/busboy": "catalog:",
"@fastify/cors": "catalog:",
"@fastify/rate-limit": "catalog:",
Expand Down
11 changes: 7 additions & 4 deletions apps/backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
NODE_ENV=

PORT=

ENCRYPTION_KEY=

AWS_SECRET_ACCESS_KEY=
AWS_ACCESS_KEY_ID=
AWS_PUBLIC_BUCKET_NAME=
AWS_PRIVATE_BUCKET_NAME=

FRONTEND_URL=
AUTH_SERVICE_URL=
DATABASE_URI=

REDIS_HOST=
REDIS_PORT=
TASK_MANAGER_QUEUE_NAME=

POSTHOG_API_KEY=
POSTHOG_PROJECT_ID=

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine3.16 as base
FROM node:20-alpine3.16 AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
Expand Down
9 changes: 6 additions & 3 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@codemod-com/backend",
"version": "0.0.158",
"version": "0.0.161",
"imports": {
"#*": "./src/*"
},
"scripts": {
"build": "tsc && node esbuild.config.js",
"start": "node build/index.js",
Expand Down Expand Up @@ -31,12 +34,12 @@
"vitest": "1.1.0"
},
"dependencies": {
"@aws-sdk/client-s3": "catalog:",
"@aws-sdk/s3-request-presigner": "catalog:",
"@codemod-com/auth": "workspace:*",
"@codemod-com/database": "workspace:*",
"@codemod-com/runner": "workspace:*",
"@codemod-com/utilities": "workspace:*",
"@aws-sdk/client-s3": "catalog:",
"@aws-sdk/s3-request-presigner": "catalog:",
"@fastify/busboy": "catalog:",
"@fastify/cors": "catalog:",
"@fastify/multipart": "catalog:",
Expand Down
34 changes: 34 additions & 0 deletions apps/backend/src/handlers/codemod-runs/codemod-runs.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { ApiResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import { prisma } from "@codemod-com/database";
import type { CodemodRun } from "@codemod-com/database";
import type { RouteHandler } from "fastify";
import { parsePaginatedGetQuery } from "#schemata/schema.js";

export type GetCodemodRunsResponse = ApiResponse<{
total: number;
data: CodemodRun[];
}>;

export const getCodemodRunsHandler: RouteHandler<{
Reply: GetCodemodRunsResponse;
}> = async (request: UserDataPopulatedRequest) => {
const query = parsePaginatedGetQuery(request.query);

const userId = request.user?.id;
const [total, codemodRuns] = await Promise.all([
prisma.codemodRun.count({
where: { ownerId: userId },
}),
prisma.codemodRun.findMany({
where: { ownerId: userId },
take: query.size,
skip: query.size && query.page ? query.size * (query.page - 1) : 0,
}),
]);

return {
total,
data: codemodRuns,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { RouteHandler } from "fastify";
import {
parseClientIdentifierSchema,
parseListCodemodsQuery,
} from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";
import { telemetryService } from "../services/TelemetryService.js";
} from "#schemata/schema.js";
import { codemodService } from "#services/CodemodService.js";
import { telemetryService } from "#services/TelemetryService.js";

export const getCodemodsListHandler: RouteHandler<{
Reply: CodemodListResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ApiResponse, GetCodemodResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import type { RouteHandler } from "fastify";
import { parseGetCodemodBySlugParams } from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";
import { processHandlerError } from "../types/errors.js";
import { parseGetCodemodBySlugParams } from "#schemata/schema.js";
import { codemodService } from "#services/CodemodService.js";
import { processHandlerError } from "#types/errors.js";

export const getCodemodHandler: RouteHandler<{
Reply: ApiResponse<GetCodemodResponse>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { ApiResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import type { Codemod } from "@codemod-com/database";
import type { RouteHandler } from "fastify";
import { parseGetCodemodsQuery } from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";
import { parseGetCodemodsQuery } from "#schemata/schema.js";
import { codemodService } from "#services/CodemodService.js";

export type GetCodemodsResponse = ApiResponse<{
total: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type {
} from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import type { FastifyReply, RouteHandler } from "fastify";
import { parseGetCodemodLatestVersionQuery } from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";
import { processHandlerError } from "../types/errors.js";
import { environment } from "../util.js";
import { parseGetCodemodLatestVersionQuery } from "#schemata/schema.js";
import { codemodService } from "#services/CodemodService.js";
import { processHandlerError } from "#types/errors.js";
import { environment } from "#util.js";

export type GetCodemodDownloadLinkResponse =
ApiResponse<CodemodDownloadLinkResponse>;
Expand Down
35 changes: 35 additions & 0 deletions apps/backend/src/handlers/insights/insight.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { ApiResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import { prisma } from "@codemod-com/database";
import type { Insight, Widget } from "@codemod-com/database";
import type { FastifyReply, RouteHandler } from "fastify";
import * as v from "valibot";

export type GetInsightResponse = ApiResponse<Insight & { widgets: Widget[] }>;

export const getInsightHandler: RouteHandler<{
Reply: GetInsightResponse;
}> = async (request: UserDataPopulatedRequest, reply: FastifyReply) => {
const { id } = v.parse(
v.object({ id: v.pipe(v.string(), v.transform(Number), v.number()) }),
request.params,
);

const insight = await prisma.insight.findFirst({
where: { id },
include: {
widgets: true,
codemodRuns: { select: { data: true, repoUrl: true, branch: true } },
},
});

if (!insight) {
return reply.status(400).send({
// @TODO
error: "INSIGHT_NOT_FOUND",
errorText: "Insight not found",
});
}

return insight;
};
127 changes: 127 additions & 0 deletions apps/backend/src/handlers/insights/insight.put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import type { ApiResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import { prisma } from "@codemod-com/database";
import type { Insight } from "@codemod-com/database";
import {
type CodemodRunResponse,
parsePutInsightBody,
} from "@codemod-com/utilities";
import axios from "axios";
import type { FastifyReply, RouteHandler } from "fastify";
import { environment } from "#util.js";

const DEFAULT_CODEMODS = [
{
engine: "workflow",
name: "drift_analyzer",
},
{
engine: "workflow",
name: "drift_analyzer_pkg",
},
];

export type PutInsightResponse = ApiResponse<Insight>;

export const putInsightHandler: RouteHandler<{
Reply: PutInsightResponse;
}> = async (request: UserDataPopulatedRequest, reply: FastifyReply) => {
// try
const body = parsePutInsightBody(request.body);
// return reply.status(400).send({
// // @TODO
// error: "INVALID_INSIGHT",
// errorText: "Invalid insight",
// });

try {
if ("id" in body) {
const insight = await prisma.insight
.update({ where: { id: body.id }, data: body })
.catch(() => null);

if (body.repoUrls.length > 0) {
await Promise.all(
body.repoUrls.map(async (repoUrl) => {
const { data: response } = await axios.post<CodemodRunResponse>(
`${environment.RUN_SERVICE_URL}/codemodRun`,
{
repoUrl,
codemods: DEFAULT_CODEMODS,
},
{
headers: {
Authorization: request.headers?.authorization,
},
},
);

const { data } = response;

for (const codemodRunResult of data) {
await prisma.codemodRun.update({
where: { jobId: codemodRunResult.jobId },
data: {
insight: { connect: { id: body.id } },
},
});
}
}),
);
}

if (insight === null) {
return reply.status(400).send({
// @TODO
error: "INSIGHT_NOT_FOUND",
errorText: "Insight not found",
});
}

return insight;
}

const insight = await prisma.insight.create({
data: {
ownerId: request.user!.id,
repoUrls: body.repoUrls,
},
});

await Promise.all(
body.repoUrls.map(async (repoUrl) => {
const { data: response } = await axios.post<CodemodRunResponse>(
`${environment.RUN_SERVICE_URL}/codemodRun`,
{
repoUrl,
codemods: DEFAULT_CODEMODS,
},
{
headers: {
Authorization: request.headers?.authorization,
},
},
);

const { data } = response;

for (const codemodRunResult of data) {
await prisma.codemodRun.update({
where: { jobId: codemodRunResult.jobId },
data: {
insight: { connect: { id: insight.id } },
},
});
}
}),
);

return insight;
} catch (err) {
return reply.status(400).send({
// @TODO
error: "INSIGHT_NOT_FOUND",
errorText: "Insight not found",
});
}
};
50 changes: 50 additions & 0 deletions apps/backend/src/handlers/insights/insights-new.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { ApiResponse } from "@codemod-com/api-types";
import type { UserDataPopulatedRequest } from "@codemod-com/auth";
import { type Insight, prisma } from "@codemod-com/database";
import {
type CodemodRunResponse,
parsePostInsightBody,
} from "@codemod-com/utilities";
import axios from "axios";
import type { FastifyReply, RouteHandler } from "fastify";
import { environment } from "#util.js";

const DEFAULT_CODEMODS = [
{ engine: "workflow", name: "drift_analyzer" },
{ engine: "workflow", name: "drift_analyzer_pkg" },
];

export type PostNewInsightResponse = ApiResponse<Insight>;

export const postNewInsightHandler: RouteHandler<{
Reply: PostNewInsightResponse;
}> = async (request: UserDataPopulatedRequest, reply: FastifyReply) => {
const { repoUrls } = parsePostInsightBody(request.body);

const insight = await prisma.insight.create({
data: { ownerId: request.user!.id, repoUrls },
});

await Promise.all(
repoUrls.map(async (repoUrl) => {
const { data: response } = await axios.post<CodemodRunResponse>(
`${environment.RUN_SERVICE_URL}/codemodRun`,
{ repoUrl, codemods: DEFAULT_CODEMODS },
{ headers: { Authorization: request.headers?.authorization } },
);

console.dir({ response }, { depth: 8 });

const { data } = response;

for (const codemodRunResult of data) {
await prisma.codemodRun.update({
where: { jobId: codemodRunResult.jobId },
data: { insight: { connect: { id: insight.id } } },
});
}
}),
);

return insight;
};
Loading
Loading