-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeployment-create-from-merge.service.ts
More file actions
87 lines (75 loc) · 2.53 KB
/
deployment-create-from-merge.service.ts
File metadata and controls
87 lines (75 loc) · 2.53 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
import { findLatestDeployment, upsertDeployment } from "./deployment.service";
import { logger } from "../../../lib/logger";
import { DeploymentChangeType } from "@prisma/client";
import { hasChangedFilesInSubdirectory } from "./deployment-monorepo.service";
import { DataIntegrityException } from "../../errors/exceptions/data-integrity.exception";
import {
linkPullRequestsToDeployment,
updatePullRequestDeploymentTracking,
} from "./deployment-pr-linking.service";
import { CreateDeploymentFromPullRequestMergeArgs } from "./deployment-create-from-merge.types";
import { BusinessRuleException } from "../../errors/exceptions/business-rule.exception";
export const createDeploymentFromPullRequestMerge = async ({
application,
environment,
pullRequest,
workspaceId,
}: CreateDeploymentFromPullRequestMergeArgs) => {
if (!pullRequest.mergedAt || !pullRequest.mergeCommitSha) {
throw new BusinessRuleException(
"Deployment cannot be created from a non-merged pull request",
{
extra: { pullRequest: pullRequest },
}
);
}
const deploymentSettings = application.deploymentSettings as {
subdirectory?: string;
};
const subdirectory = deploymentSettings?.subdirectory;
const files = pullRequest.files as { path: string }[];
if (!files?.length) {
throw new DataIntegrityException("Pull Request has no files", {
extra: { pullRequest: pullRequest },
});
}
const shouldSkip = subdirectory
? !hasChangedFilesInSubdirectory({
files,
subdirectory,
})
: false;
if (shouldSkip) return;
const latestDeployment = await findLatestDeployment({
workspaceId,
applicationId: application.id,
environmentId: environment.id,
});
const deployment = await upsertDeployment({
workspaceId,
environmentId: environment.id,
applicationId: application.id,
authorId: pullRequest.authorId,
deployedAt: pullRequest.mergedAt,
commitHash: pullRequest.mergeCommitSha,
version: pullRequest.mergeCommitSha,
changeType: latestDeployment
? DeploymentChangeType.FORWARD
: DeploymentChangeType.BASELINE,
description: `Auto-deployed PR #${pullRequest.number}: ${pullRequest.title}`,
});
await linkPullRequestsToDeployment({
workspaceId,
deploymentId: deployment.id,
pullRequestIds: [pullRequest.id],
});
await updatePullRequestDeploymentTracking({
pullRequest,
deployment,
workspaceId,
});
logger.info("deploymentCreateFromMergeWorker: Deployment created", {
deployment,
});
return deployment;
};