Skip to content

feat(cli,backend): adds support for multiple diffs and iterative codemod learn (CDMD-3480, CDMD-3524, CDMD-3508) #895

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

Draft
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:
run: pnpm build
env:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: 'pk_test_c3VtbWFyeS13YWxydXMtMjUuY2xlcmsuYWNjb3VudHMuZGV2JA'
NEXT_PUBLIC_API_URL: 'http://localhost:8081'
NEXT_PUBLIC_AI_API_URL: 'http://localhost:8081'
NEXT_PUBLIC_AUTH_API_URL: 'http://localhost:8080'
NEXT_PUBLIC_WS_URL: 'ws://localhost:8000'
NEXT_PUBLIC_API_URL: 'http://localhost:8081'
NEXT_PUBLIC_MODGPT_API_URL: 'http://localhost:8082'
NEXT_PUBLIC_CODEMODAI_API_URL: 'http://localhost:8091'
HUBSPOT_CONTACT_FORM_ID: ${{ secrets.HUBSPOT_CONTACT_FORM_ID }}
HUBSPOT_JOB_FORM_ID: ${{ secrets.HUBSPOT_JOB_FORM_ID }}
HUBSPOT_NEWSLETTER_FORM_ID: ${{ secrets.HUBSPOT_NEWSLETTER_FORM_ID }}
Expand Down
7 changes: 3 additions & 4 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codemod-com/backend",
"version": "0.0.157",
"version": "0.0.159",
"scripts": {
"build": "tsc && node esbuild.config.js",
"start": "node build/index.js",
Expand All @@ -13,11 +13,10 @@
"@codemod-com/telemetry": "workspace:*",
"@codemod-com/tsconfig": "workspace:*",
"@faker-js/faker": "catalog:",
"@total-typescript/ts-reset": "catalog:",
"@types/node": "20.10.5",
"@types/parse-github-url": "catalog:",
"@types/pg": "catalog:",
"@types/semver": "7.5.8",
"@types/semver": "catalog:",
"@types/supertest": "catalog:",
"@types/ws": "^8.5.11",
"dotenv-cli": "catalog:",
Expand Down Expand Up @@ -59,7 +58,7 @@
"parse-github-url": "catalog:",
"pg": "catalog:",
"replicate": "catalog:",
"semver": "7.6.0",
"semver": "catalog:",
"tar": "^6.2.0",
"valibot": "catalog:",
"ws": "^8.18.0",
Expand Down
14 changes: 12 additions & 2 deletions apps/backend/src/schemata/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ export const ivObjectSchema = object({
export const parseIv = (input: unknown) => parse(ivObjectSchema, input);

export const diffCreationBodySchema = object({
before: string(),
after: string(),
diffs: array(object({ before: string(), after: string() })),
source: union([literal("cli"), literal("studio")]),
name: optional(string(), "untitled"),
});
Expand All @@ -154,6 +153,17 @@ export const getCodeDiffSchema = object({
id: string(),
});

export const beforeAfterDiffSchema = object({
before: string(),
after: string(),
});

export const parseBeforeAfterDiff = (input: unknown) =>
parse(beforeAfterDiffSchema, input);

export const parseBeforeAfterDiffArray = (input: unknown) =>
parse(array(beforeAfterDiffSchema), input);

export const parseGetCodeDiffParams = (input: unknown) =>
parse(getCodeDiffSchema, input);

Expand Down
95 changes: 49 additions & 46 deletions apps/backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { randomBytes } from "node:crypto";
import type { CodemodListResponse } from "@codemod-com/api-types";
import {
type ApiResponse,
BAD_REQUEST,
type CodemodListResponse,
INTERNAL_SERVER_ERROR,
} from "@codemod-com/api-types";
import { getAuthPlugin } from "@codemod-com/auth";
import { prisma } from "@codemod-com/database";
import { decryptWithIv, encryptWithIv } from "@codemod-com/utilities";
Expand All @@ -10,6 +15,7 @@ import Fastify, {
type FastifyPluginCallback,
type FastifyRequest,
} from "fastify";
import type { InferOutput } from "valibot";
import {
type GetCodemodDownloadLinkResponse,
getCodemodDownloadLink,
Expand All @@ -25,6 +31,8 @@ import {
publishHandler,
} from "./publishHandler.js";
import {
type beforeAfterDiffSchema,
parseBeforeAfterDiffArray,
parseCreateIssueBody,
parseCreateIssueParams,
parseDiffCreationBody,
Expand Down Expand Up @@ -189,48 +197,46 @@ const routes: FastifyPluginCallback = (instance, _opts, done) => {
getCodemodsListHandler,
);

instance.get<{ Reply: { before: string; after: string } }>(
"/diffs/:id",
async (request, reply) => {
const { id } = parseGetCodeDiffParams(request.params);
const { iv: ivStr } = parseIv(request.query);
instance.get<{
Reply: ApiResponse<{ diffs: InferOutput<typeof beforeAfterDiffSchema>[] }>;
}>("/diffs/:id", async (request, reply) => {
const { id } = parseGetCodeDiffParams(request.params);
const { iv: ivStr } = parseIv(request.query);

const key = Buffer.from(environment.ENCRYPTION_KEY, "base64url");
const iv = Buffer.from(ivStr, "base64url");
const key = Buffer.from(environment.ENCRYPTION_KEY, "base64url");
const iv = Buffer.from(ivStr, "base64url");

const codeDiff = await prisma.codeDiff.findUnique({
where: { id },
});
const codeDiff = await prisma.codeDiff.findUnique({
where: { id },
});

if (!codeDiff) {
reply.code(400).send();
return;
}
if (!codeDiff) {
return reply
.code(400)
.send({ errorText: "Code diff not found", error: BAD_REQUEST });
}

let before: string;
let after: string;
try {
before = decryptWithIv(
"aes-256-cbc",
{ key, iv },
Buffer.from(codeDiff.before, "base64url"),
).toString();
after = decryptWithIv(
"aes-256-cbc",
{ key, iv },
Buffer.from(codeDiff.after, "base64url"),
).toString();
} catch (err) {
reply.code(400).send();
return;
}
try {
const diffs = parseBeforeAfterDiffArray(
JSON.parse(
decryptWithIv(
"aes-256-cbc",
{ key, iv },
Buffer.from(codeDiff.diffs, "base64url"),
).toString(),
),
);

reply.type("application/json").code(200);
return { before, after };
},
);
reply.type("application/json").code(200).send({ diffs });
} catch (err) {
return reply.code(400).send({
errorText: `Failed to decrypt the diffs array: ${(err as Error).message}`,
error: INTERNAL_SERVER_ERROR,
});
}
});

instance.post<{ Reply: { id: string; iv: string } }>(
instance.post<{ Reply: ApiResponse<{ id: string; iv: string }> }>(
"/diffs",
async (request, reply) => {
const body = parseDiffCreationBody(request.body);
Expand All @@ -242,21 +248,18 @@ const routes: FastifyPluginCallback = (instance, _opts, done) => {
data: {
name: body.name,
source: body.source,
before: encryptWithIv(
diffs: encryptWithIv(
"aes-256-cbc",
{ key, iv },
Buffer.from(body.before),
).toString("base64url"),
after: encryptWithIv(
"aes-256-cbc",
{ key, iv },
Buffer.from(body.after),
Buffer.from(JSON.stringify(body.diffs)),
).toString("base64url"),
},
});

reply.type("application/json").code(200);
return { id: codeDiff.id, iv: iv.toString("base64url") };
return reply
.type("application/json")
.code(200)
.send({ id: codeDiff.id, iv: iv.toString("base64url") });
},
);

Expand Down
23 changes: 18 additions & 5 deletions apps/cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ import { hideBin } from "yargs/helpers";
// Build envs for local production build, can be used by running `pnpm build --prod`
const localProdBuildEnvs = {
"process.env.NODE_ENV": '"production"',
"process.env.IGNORE_TELEMETRY": "true",

"process.env.BACKEND_URL": '"https://backend.codemod.com"',
"process.env.AUTH_BACKEND_URL": '"https://backend.codemod.com/auth"',
"process.env.RUNNER_URL": '"http://backend.codemod.com/run"',
"process.env.MODGPT_URL": '"http://backend.codemod.com/modgpt"',
"process.env.CODEMODAI_URL": '"http://backend.codemod.com/ai"',

"process.env.CODEMOD_HOME_PAGE_URL": '"https://codemod.com"',
"process.env.CODEMOD_STUDIO_URL": '"https://codemod.com/studio"',
"process.env.IGNORE_TELEMETRY": "true",
"process.env.RUNNER_URL": '"https://backend.codemod.com/run"',
};

// Build envs for staging, it is the default when running `pnpm build`
const stagingBuildEnvs = {
"process.env.NODE_ENV": '"staging"',
"process.env.IGNORE_TELEMETRY": "true",

"process.env.BACKEND_URL": '"https://staging-backend.codemod.com"',
"process.env.AUTH_BACKEND_URL": '"https://staging-backend.codemod.com/auth"',
"process.env.RUNNER_URL": '"http://staging-backend.codemod.com/run"',
"process.env.MODGPT_URL": '"http://staging-backend.codemod.com/modgpt"',
"process.env.CODEMODAI_URL": '"http://staging-backend.codemod.com/ai"',

"process.env.CODEMOD_HOME_PAGE_URL": '"https://staging.codemod.com"',
"process.env.CODEMOD_STUDIO_URL": '"https://staging.codemod.com/studio"',
"process.env.IGNORE_TELEMETRY": "true",
};

// Build envs for publishing to npm, it would usually happen during prepublishOnly script
Expand All @@ -32,12 +41,16 @@ const publishEnvs = {
// Can be used by running `pnpm build --local`
const localEnvs = {
"process.env.NODE_ENV": '"development"',
"process.env.IGNORE_TELEMETRY": "true",

"process.env.BACKEND_URL": '"http://localhost:8081"',
"process.env.AUTH_BACKEND_URL": '"http://localhost:8080"',
"process.env.RUNNER_URL": '"http://localhost:8083"',
"process.env.MODGPT_URL": '"http://localhost:8084"',
"process.env.CODEMODAI_URL": '"http://localhost:8091"',

"process.env.CODEMOD_HOME_PAGE_URL": '"http://localhost:3000"',
"process.env.CODEMOD_STUDIO_URL": '"http://localhost:3000/studio"',
"process.env.IGNORE_TELEMETRY": "true",
"process.env.RUNNER_URL": '"http://localhost:8083"',
};

const argv = hideBin(process.argv);
Expand Down
7 changes: 6 additions & 1 deletion apps/cli/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ declare global {
namespace NodeJS {
interface ProcessEnv {
NODE_ENV: "development" | "production";
IGNORE_TELEMETRY: boolean;

BACKEND_URL: string;
AUTH_BACKEND_URL: string;
RUNNER_URL: string;
MODGPT_URL: string;
CODEMODAI_URL: string

CODEMOD_HOME_PAGE_URL: string;
CODEMOD_STUDIO_URL: string;
IGNORE_TELEMETRY: boolean;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"imports": {
"#*": "./src/*"
},
"version": "0.13.7",
"version": "0.14.0",
"description": "A codemod engine for Node.js libraries (jscodeshift, ts-morph, etc.)",
"type": "module",
"exports": null,
Expand Down Expand Up @@ -47,7 +47,7 @@
"@types/inquirer": "catalog:",
"@types/node": "18.11.9",
"@types/prettyjson": "catalog:",
"@types/semver": "^7.5.8",
"@types/semver": "catalog:",
"@types/yargs": "catalog:",
"@vitest/coverage-v8": "catalog:",
"axios": "catalog:",
Expand All @@ -61,7 +61,7 @@
"open": "catalog:",
"prettier": "^3.2.5",
"prettyjson": "catalog:",
"semver": "^7.6.2",
"semver": "catalog:",
"terminal-link": "catalog:",
"ts-morph": "18.0.0",
"valibot": "catalog:",
Expand Down
Loading
Loading