Skip to content

Commit

Permalink
[CLI, Backend] feat: codemod version, error consts, bump version on p…
Browse files Browse the repository at this point in the history
…ublish (CDMD-3573, CDMD-3556, CDMD-2665) (#1055)
  • Loading branch information
r4zendev authored Jul 11, 2024
1 parent fe631d3 commit b7729c7
Show file tree
Hide file tree
Showing 33 changed files with 603 additions and 1,151 deletions.
2 changes: 1 addition & 1 deletion 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.8",
"version": "0.0.9",
"scripts": {
"build": "node esbuild.config.js",
"start": "node build/index.js"
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const routes: FastifyPluginCallback = (instance, _opts, done) => {
].filter(isNeitherNullNorUndefined);

if (user.username) {
allowedNamespaces.push(user.username);
allowedNamespaces.unshift(user.username);

if (environment.VERIFIED_PUBLISHERS.includes(user.username)) {
allowedNamespaces.push("codemod-com", "codemod.com");
Expand Down
2 changes: 1 addition & 1 deletion 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.130",
"version": "0.0.131",
"scripts": {
"build": "node esbuild.config.js",
"start": "node build/index.js",
Expand Down
12 changes: 0 additions & 12 deletions apps/backend/src/handlers/getCodemodBySlugHandler.ts

This file was deleted.

42 changes: 33 additions & 9 deletions apps/backend/src/handlers/getCodemodDownloadLink.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import type { RouteHandler } from "fastify";
import {
type ApiResponse,
CODEMOD_NOT_FOUND,
type CodemodDownloadLinkResponse,
INTERNAL_SERVER_ERROR,
} from "@codemod-com/utilities";
import type { FastifyReply, RouteHandler } from "fastify";
import { CodemodNotFoundError, processHandlerError } from "~/types/errors.js";
import type { UserDataPopulatedRequest } from "../plugins/authPlugin.js";
import { parseGetCodemodLatestVersionQuery } from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";
import { environment } from "../util.js";

export type GetCodemodDownloadLinkResponse = { link: string };
export type GetCodemodDownloadLinkResponse =
ApiResponse<CodemodDownloadLinkResponse>;

export const getCodemodDownloadLink: RouteHandler<{
Reply: GetCodemodDownloadLinkResponse;
}> = async (request: UserDataPopulatedRequest) => {
}> = async (request: UserDataPopulatedRequest, reply: FastifyReply) => {
const { name } = parseGetCodemodLatestVersionQuery(request.query);

if (!request?.user?.id) {
return codemodService.getCodemodDownloadLink(name, null, []);
try {
return await codemodService.getCodemodDownloadLink(name, null, []);
} catch (err) {
return processHandlerError(
err,
reply,
"Failed to retrieve codemod download link",
);
}
}

const allowedNamespaces = request?.allowedNamespaces;
Expand All @@ -39,9 +55,17 @@ export const getCodemodDownloadLink: RouteHandler<{
);
};

return codemodService.getCodemodDownloadLink(
name,
generateSignedUrl,
allowedNamespaces,
);
try {
return await codemodService.getCodemodDownloadLink(
name,
generateSignedUrl,
allowedNamespaces,
);
} catch (err) {
return processHandlerError(
err,
reply,
"Failed to retrieve codemod download link",
);
}
};
22 changes: 22 additions & 0 deletions apps/backend/src/handlers/getCodemodHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
type ApiResponse,
CODEMOD_NOT_FOUND,
type GetCodemodResponse,
INTERNAL_SERVER_ERROR,
} from "@codemod-com/utilities";
import type { RouteHandler } from "fastify";
import { CodemodNotFoundError, processHandlerError } from "~/types/errors.js";
import { parseGetCodemodBySlugParams } from "../schemata/schema.js";
import { codemodService } from "../services/CodemodService.js";

export const getCodemodHandler: RouteHandler<{
Reply: ApiResponse<GetCodemodResponse>;
}> = async (request, reply) => {
const { criteria } = parseGetCodemodBySlugParams(request.params);

try {
return await codemodService.getCodemod(criteria);
} catch (err) {
processHandlerError(err, reply, "Failed to retrieve codemod");
}
};
65 changes: 25 additions & 40 deletions apps/backend/src/publishHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createHash } from "node:crypto";
import type { CodemodConfigInput } from "@codemod-com/utilities";
import {
CODEMOD_NAME_TAKEN,
CODEMOD_VERSION_EXISTS,
type CodemodConfigInput,
INTERNAL_SERVER_ERROR,
NO_MAIN_FILE_FOUND,
UNAUTHORIZED,
} from "@codemod-com/utilities";
import supertest from "supertest";
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
import { runServer } from "./server.js";
Expand Down Expand Up @@ -76,26 +83,6 @@ vi.mock("@aws-sdk/client-s3", async () => {
};
});

vi.mock("./schemata/env.js", async () => {
const actual = await vi.importActual("./schemata/env.js");

return {
...actual,
parseEnvironment: vi.fn().mockImplementation(() => {
return {
PORT: "8081",
DATABASE_URI: "sqlite://:memory:",
VERIFIED_PUBLISHERS: [],
CLERK_PUBLISH_KEY: "CLERK_PUBLISH_KEY",
CLERK_SECRET_KEY: "CLERK_SECRET_KEY",
CLERK_JWT_KEY: "CLERK_JWT_KEY",
TASK_MANAGER_QUEUE_NAME: "TASK_MANAGER_QUEUE_NAME",
CODEMOD_COM_API_URL: "https://codemod.com/api",
};
}),
};
});

vi.mock("./services/tokenService.js", async () => {
const actual = await vi.importActual("./services/tokenService.js");

Expand Down Expand Up @@ -241,7 +228,7 @@ describe("/publish route", async () => {
},
);

expect(response.body).toEqual({ success: true });
expect(response.body).toEqual({});
});

it("should publish piranha codemods", async () => {
Expand Down Expand Up @@ -309,7 +296,7 @@ describe("/publish route", async () => {
requestTimeout: 5000,
});

expect(response.body).toEqual({ success: true });
expect(response.body).toEqual({});
});

it("should not allow further execution if required files were not provided", async () => {
Expand Down Expand Up @@ -337,8 +324,8 @@ describe("/publish route", async () => {
.expect(expectedCode);

expect(response.body).toEqual({
error: "No main file was provided",
success: false,
error: NO_MAIN_FILE_FOUND,
errorText: "No main file was provided",
});
});

Expand Down Expand Up @@ -382,8 +369,8 @@ describe("/publish route", async () => {
expect(mocks.PutObjectCommand.mock.instances.length).toEqual(0);

expect(response.body).toEqual({
error: `Failed writing codemod to the database: ${errorMsg}`,
success: false,
error: INTERNAL_SERVER_ERROR,
errorText: `Failed writing codemod to the database: ${errorMsg}`,
});
});

Expand Down Expand Up @@ -420,8 +407,8 @@ describe("/publish route", async () => {
expect(mocks.prisma.codemod.upsert).toHaveBeenCalledTimes(0);

expect(response.body).toEqual({
error: `Codemod ${codemodRcContents.name} version ${codemodRcContents.version} is lower than the latest published or the same as the latest published version: 1.0.0`,
success: false,
error: CODEMOD_VERSION_EXISTS,
errorText: `Codemod ${codemodRcContents.name} version ${codemodRcContents.version} is lower than the latest published or the same as the latest published version: 1.0.0`,
});
});

Expand Down Expand Up @@ -459,8 +446,8 @@ describe("/publish route", async () => {
expect(mocks.prisma.codemod.upsert).toHaveBeenCalledTimes(0);

expect(response.body).toEqual({
error: `Codemod name \`${codemodRcContents.name}\` is already taken.`,
success: false,
error: CODEMOD_NAME_TAKEN,
errorText: `Codemod name \`${codemodRcContents.name}\` is already taken.`,
});
});

Expand Down Expand Up @@ -529,8 +516,8 @@ describe("/publish route", async () => {
});

expect(response.body).toEqual({
error: `Failed publishing to S3: ${errorMsg}`,
success: false,
error: INTERNAL_SERVER_ERROR,
errorText: `Failed publishing to S3: ${errorMsg}`,
});
});

Expand Down Expand Up @@ -596,8 +583,8 @@ describe("/publish route", async () => {
});

expect(response.body).toEqual({
error: `Failed publishing to S3: ${errorMsg}`,
success: false,
error: INTERNAL_SERVER_ERROR,
errorText: `Failed publishing to S3: ${errorMsg}`,
});
});
});
Expand Down Expand Up @@ -662,9 +649,7 @@ describe("/publish route", async () => {
const clientInstance = mocks.S3Client.mock.instances[0];
expect(clientInstance.send).toHaveBeenCalledOnce();

expect(response.body).toEqual({
success: true,
});
expect(response.body).toEqual({});
});

it("should fail if user has no access to the org", async () => {
Expand Down Expand Up @@ -718,8 +703,8 @@ describe("/publish route", async () => {
expect(mocks.prisma.codemod.upsert).toHaveBeenCalledTimes(0);

expect(response.body).toEqual({
error: `You are not allowed to publish under namespace "org"`,
success: false,
error: UNAUTHORIZED,
errorText: `You are not allowed to publish under namespace "org"`,
});
});
});
Expand Down
Loading

0 comments on commit b7729c7

Please sign in to comment.