Skip to content
Merged
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
63 changes: 33 additions & 30 deletions apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ export const POST = request()
),
)
.handle<{ user: User; can: PermissionChecker; body: z.infer<typeof body> }>(
({ db, body }) =>
db.transaction(async (tx) => {
try {
async ({ db, body }) => {
try {
const existingEnv = await db
.select()
.from(schema.environment)
.where(eq(schema.environment.name, body.name))
.then(takeFirstOrNull);

const environment = await db.transaction(async (tx) => {
const releaseChannels = body.releaseChannels?.length ?? 0;
const deploymentVersionChannels =
body.deploymentVersionChannels?.length ?? 0;
Expand All @@ -56,36 +62,33 @@ export const POST = request()
)
: [];

const existingEnv = await db
.select()
.from(schema.environment)
.where(eq(schema.environment.name, body.name))
.then(takeFirstOrNull);

const environment = await upsertEnv(tx, { ...body, versionChannels });

if (existingEnv != null)
await getQueue(Channel.UpdateEnvironment).add(environment.id, {
...environment,
oldSelector: existingEnv.resourceSelector,
});

if (existingEnv == null)
await getQueue(Channel.NewEnvironment).add(
environment.id,
environment,
);

await createJobsForNewEnvironment(tx, environment);
const { metadata } = body;
return NextResponse.json({ ...environment, metadata });
} catch (e) {
const error = e instanceof Error ? e.message : e;
logger.error("Failed to create environment", { error });
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
return { ...environment, metadata };
});

if (existingEnv != null)
await getQueue(Channel.UpdateEnvironment).add(environment.id, {
...environment,
oldSelector: existingEnv.resourceSelector,
});

if (existingEnv == null)
await getQueue(Channel.NewEnvironment).add(
environment.id,
environment,
);
}
}),

return NextResponse.json(environment);
} catch (e) {
const error = e instanceof Error ? e.message : e;
logger.error("Failed to create environment", { error });
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
);
}
},
);
23 changes: 18 additions & 5 deletions e2e/tests/api/environments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,27 @@ test.describe("Environments API", () => {
});

test("should match resources to new environment", async ({ api, page }) => {
const systemPrefix = importedEntities.system.slug.split("-")[0]!;
const environmentResponse = await api.POST("/v1/environments", {
body: {
name: faker.string.alphanumeric(10),
systemId: importedEntities.system.id,
resourceSelector: {
type: "metadata",
operator: "equals",
key: "env",
value: "qa",
type: "comparison",
operator: "and",
conditions: [
{
type: "metadata",
operator: "equals",
key: "env",
value: "qa",
},
{
type: "identifier",
operator: "starts-with",
value: systemPrefix,
},
],
},
},
});
Expand All @@ -71,7 +83,8 @@ test.describe("Environments API", () => {
expect(resourcesResponse.response.status).toBe(200);
expect(resourcesResponse.data?.resources?.length).toBe(1);
expect(resourcesResponse.data?.resources?.[0]?.identifier).toBe(
importedEntities.resources[0].identifier,
importedEntities.resources.find((r) => r.metadata?.env === "qa")
?.identifier,
);
});
});
1 change: 0 additions & 1 deletion e2e/tests/api/environments.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ resources:
enabled: true
metadata:
env: prod