-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapi.v1.projects.$projectRef.branches.archive.ts
More file actions
99 lines (83 loc) · 2.75 KB
/
api.v1.projects.$projectRef.branches.archive.ts
File metadata and controls
99 lines (83 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
import { tryCatch } from "@trigger.dev/core";
import { z } from "zod";
import { prisma } from "~/db.server";
import { authenticateRequest } from "~/services/apiAuth.server";
import { ArchiveBranchService } from "~/services/archiveBranch.server";
import { logger } from "~/services/logger.server";
const ParamsSchema = z.object({
projectRef: z.string(),
});
const BodySchema = z.object({
branch: z.string(),
});
export async function action({ request, params }: ActionFunctionArgs) {
if (request.method !== "POST") {
return json({ error: "Method not allowed" }, { status: 405 });
}
logger.info("Archive branch", { url: request.url, params });
const authenticationResult = await authenticateRequest(request, {
personalAccessToken: true,
organizationAccessToken: true,
apiKey: false,
});
if (!authenticationResult) {
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
}
const parsedParams = ParamsSchema.safeParse(params);
if (!parsedParams.success) {
return json({ error: "Invalid Params" }, { status: 400 });
}
const { projectRef } = parsedParams.data;
const [error, body] = await tryCatch(request.json());
if (error) {
return json({ error: error.message }, { status: 400 });
}
const parsed = BodySchema.safeParse(body);
if (!parsed.success) {
return json({ error: parsed.error.message }, { status: 400 });
}
const environments = await prisma.runtimeEnvironment.findMany({
select: {
id: true,
archivedAt: true,
},
where: {
organization:
authenticationResult.type === "organizationAccessToken"
? { id: authenticationResult.result.organizationId }
: {
members: {
some: {
userId: authenticationResult.result.userId,
},
},
},
project: {
externalRef: projectRef,
},
branchName: parsed.data.branch,
},
});
if (environments.length === 0) {
return json({ error: "Branch not found" }, { status: 404 });
}
const environment = environments.find((env) => env.archivedAt === null);
if (!environment) {
return json({ error: "Branch already archived" }, { status: 400 });
}
const service = new ArchiveBranchService();
const result = await service.call(
authenticationResult.type === "organizationAccessToken"
? { type: "orgId", organizationId: authenticationResult.result.organizationId }
: { type: "userMembership", userId: authenticationResult.result.userId },
{
environmentId: environment.id,
}
);
if (result.success) {
return json(result);
} else {
return json(result, { status: 400 });
}
}