-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathupsertBranch.server.ts
More file actions
187 lines (170 loc) · 5.71 KB
/
upsertBranch.server.ts
File metadata and controls
187 lines (170 loc) · 5.71 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { type PrismaClient, type PrismaClientOrTransaction } from "@trigger.dev/database";
import slug from "slug";
import { prisma } from "~/db.server";
import { createApiKeyForEnv, createPkApiKeyForEnv } from "~/models/api-key.server";
import { type CreateBranchOptions } from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches/route";
import { isValidGitBranchName, sanitizeBranchName } from "~/v3/gitBranch";
import { logger } from "./logger.server";
import { getLimit } from "./platform.v3.server";
export class UpsertBranchService {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call(
// The orgFilter approach is not ideal but we need to keep it this way for now because of how the service is used in routes and api endpoints.
// Currently authorization checks are spread across the controller/route layer and the service layer. Often we check in multiple places for org/project membership.
// Ideally we would take care of both the authentication and authorization checks in the controllers and routes.
// That would unify how we handle authorization and org/project membership checks. Also it would make the service layer queries simpler.
orgFilter:
| { type: "userMembership"; userId: string }
| { type: "orgId"; organizationId: string },
{ parentEnvironmentId, branchName, git }: CreateBranchOptions
) {
const sanitizedBranchName = sanitizeBranchName(branchName);
if (!sanitizedBranchName) {
return {
success: false as const,
error: "Branch name has an invalid format",
};
}
if (!isValidGitBranchName(sanitizedBranchName)) {
return {
success: false as const,
error: "Invalid branch name, contains disallowed character sequences",
};
}
try {
const parentEnvironment = await this.#prismaClient.runtimeEnvironment.findFirst({
where: {
id: parentEnvironmentId,
organization:
orgFilter.type === "userMembership"
? {
members: {
some: {
userId: orgFilter.userId,
},
},
}
: { id: orgFilter.organizationId },
},
include: {
organization: {
select: {
id: true,
slug: true,
maximumConcurrencyLimit: true,
},
},
project: {
select: {
id: true,
slug: true,
},
},
},
});
if (!parentEnvironment) {
return {
success: false as const,
error: "You don't have preview branches setup. Go to the dashboard to enable them.",
};
}
if (!parentEnvironment.isBranchableEnvironment) {
return {
success: false as const,
error: "Your preview environment is not branchable",
};
}
const limits = await checkBranchLimit(
this.#prismaClient,
parentEnvironment.organization.id,
parentEnvironment.project.id,
sanitizedBranchName
);
if (limits.isAtLimit) {
return {
success: false as const,
error: `You've used all ${limits.used} of ${limits.limit} branches for your plan. Upgrade to get more branches or archive some.`,
};
}
const branchSlug = `${slug(`${parentEnvironment.slug}-${sanitizedBranchName}`)}`;
const apiKey = createApiKeyForEnv(parentEnvironment.type);
const pkApiKey = createPkApiKeyForEnv(parentEnvironment.type);
const shortcode = branchSlug;
const now = new Date();
const branch = await this.#prismaClient.runtimeEnvironment.upsert({
where: {
projectId_shortcode: {
projectId: parentEnvironment.project.id,
shortcode: shortcode,
},
},
create: {
slug: branchSlug,
apiKey,
pkApiKey,
shortcode,
maximumConcurrencyLimit: parentEnvironment.maximumConcurrencyLimit,
organization: {
connect: {
id: parentEnvironment.organization.id,
},
},
project: {
connect: { id: parentEnvironment.project.id },
},
branchName: sanitizedBranchName,
type: parentEnvironment.type,
parentEnvironment: {
connect: { id: parentEnvironment.id },
},
git: git ?? undefined,
},
update: {
git: git ?? undefined,
},
});
const alreadyExisted = branch.createdAt < now;
return {
success: true as const,
alreadyExisted: alreadyExisted,
branch,
organization: parentEnvironment.organization,
project: parentEnvironment.project,
};
} catch (e) {
logger.error("CreateBranchService error", { error: e });
return {
success: false as const,
error: e instanceof Error ? e.message : "Failed to create branch",
};
}
}
}
export async function checkBranchLimit(
prisma: PrismaClientOrTransaction,
organizationId: string,
projectId: string,
newBranchName?: string
) {
const usedEnvs = await prisma.runtimeEnvironment.findMany({
where: {
projectId,
branchName: {
not: null,
},
archivedAt: null,
},
});
const count = newBranchName
? usedEnvs.filter((env) => env.branchName !== newBranchName).length
: usedEnvs.length;
const limit = await getLimit(organizationId, "branches", 100_000_000);
return {
used: count,
limit,
isAtLimit: count >= limit,
};
}