Skip to content

Commit 273874e

Browse files
committed
Enable preview deployments only if the preview environemtn is enabled
1 parent 6d611ee commit 273874e

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

apps/webapp/app/services/projectSettings.server.ts

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DeleteProjectService } from "~/services/deleteProject.server";
44
import { BranchTrackingConfigSchema, type BranchTrackingConfig } from "~/v3/github";
55
import { checkGitHubBranchExists } from "~/services/gitHub.server";
66
import { errAsync, fromPromise, okAsync, ResultAsync } from "neverthrow";
7-
import { BuildSettings } from "~/v3/buildSettings";
7+
import { type BuildSettings } from "~/v3/buildSettings";
88

99
export class ProjectSettingsService {
1010
#prismaClient: PrismaClient;
@@ -82,7 +82,7 @@ export class ProjectSettingsService {
8282
(error) => ({ type: "other" as const, cause: error })
8383
);
8484

85-
const createConnectedRepo = (defaultBranch: string) =>
85+
const createConnectedRepo = (defaultBranch: string, previewDeploymentsEnabled: boolean) =>
8686
fromPromise(
8787
this.#prismaClient.connectedGithubRepository.create({
8888
data: {
@@ -92,21 +92,23 @@ export class ProjectSettingsService {
9292
prod: { branch: defaultBranch },
9393
staging: {},
9494
} satisfies BranchTrackingConfig,
95-
previewDeploymentsEnabled: true,
95+
previewDeploymentsEnabled,
9696
},
9797
}),
9898
(error) => ({ type: "other" as const, cause: error })
9999
);
100100

101-
return ResultAsync.combine([getRepository(), findExistingConnection()]).andThen(
102-
([repository, existingConnection]) => {
103-
if (existingConnection) {
104-
return errAsync({ type: "project_already_has_connected_repository" as const });
105-
}
106-
107-
return createConnectedRepo(repository.defaultBranch);
101+
return ResultAsync.combine([
102+
getRepository(),
103+
findExistingConnection(),
104+
this.isPreviewEnvironmentEnabled(projectId),
105+
]).andThen(([repository, existingConnection, previewEnvironmentEnabled]) => {
106+
if (existingConnection) {
107+
return errAsync({ type: "project_already_has_connected_repository" as const });
108108
}
109-
);
109+
110+
return createConnectedRepo(repository.defaultBranch, previewEnvironmentEnabled);
111+
});
110112
}
111113

112114
disconnectGitHubRepo(projectId: string) {
@@ -208,18 +210,22 @@ export class ProjectSettingsService {
208210
return okAsync(stagingBranch);
209211
};
210212

211-
const updateConnectedRepo = () =>
213+
const updateConnectedRepo = (data: {
214+
productionBranch: string | undefined;
215+
stagingBranch: string | undefined;
216+
previewDeploymentsEnabled: boolean | undefined;
217+
}) =>
212218
fromPromise(
213219
this.#prismaClient.connectedGithubRepository.update({
214220
where: {
215221
projectId: projectId,
216222
},
217223
data: {
218224
branchTracking: {
219-
prod: productionBranch ? { branch: productionBranch } : {},
220-
staging: stagingBranch ? { branch: stagingBranch } : {},
225+
prod: data.productionBranch ? { branch: data.productionBranch } : {},
226+
staging: data.stagingBranch ? { branch: data.stagingBranch } : {},
221227
} satisfies BranchTrackingConfig,
222-
previewDeploymentsEnabled: previewDeploymentsEnabled,
228+
previewDeploymentsEnabled: data.previewDeploymentsEnabled,
223229
},
224230
}),
225231
(error) => ({ type: "other" as const, cause: error })
@@ -240,8 +246,14 @@ export class ProjectSettingsService {
240246
fullRepoName: connectedRepo.repository.fullName,
241247
oldStagingBranch: connectedRepo.branchTracking?.staging?.branch,
242248
}),
249+
this.isPreviewEnvironmentEnabled(projectId),
243250
]);
244251
})
252+
.map(([productionBranch, stagingBranch, previewEnvironmentEnabled]) => ({
253+
productionBranch,
254+
stagingBranch,
255+
previewDeploymentsEnabled: previewDeploymentsEnabled && previewEnvironmentEnabled,
256+
}))
245257
.andThen(updateConnectedRepo);
246258
}
247259

@@ -296,4 +308,22 @@ export class ProjectSettingsService {
296308
});
297309
});
298310
}
311+
312+
private isPreviewEnvironmentEnabled(projectId: string) {
313+
return fromPromise(
314+
this.#prismaClient.runtimeEnvironment.findFirst({
315+
select: {
316+
id: true,
317+
},
318+
where: {
319+
projectId: projectId,
320+
slug: "preview",
321+
},
322+
}),
323+
(error) => ({
324+
type: "other" as const,
325+
cause: error,
326+
})
327+
).map((previewEnvironment) => previewEnvironment !== null);
328+
}
299329
}

0 commit comments

Comments
 (0)