-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeployment-triggered-by-pull-request-merge.worker.ts
More file actions
120 lines (104 loc) · 3.47 KB
/
deployment-triggered-by-pull-request-merge.worker.ts
File metadata and controls
120 lines (104 loc) · 3.47 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
import { Job } from "bullmq";
import { SweetQueue } from "../../../bull-mq/queues";
import { createWorker } from "../../../bull-mq/workers";
import { logger } from "../../../lib/logger";
import { DeploymentSettingsTrigger } from "../../applications/services/application.types";
import {
DEFAULT_PRODUCTION_ENVIRONMENT_NAME,
findOrCreateEnvironment,
} from "../../environments/services/environment.service";
import { ResourceNotFoundException } from "../../errors/exceptions/resource-not-found.exception";
import { findRepositoryById } from "../../repositories/services/repository.service";
import { createDeploymentFromPullRequestMerge } from "../services/deployment-create-from-merge.service";
import { findPullRequestById } from "../../pull-requests/services/pull-request.service";
import { PullRequest, PullRequestTracking } from "@prisma/client";
interface DeploymentTriggeredByPullRequestMergeJobData {
workspaceId: number;
pullRequestId: number;
installationId: string;
}
export const deploymentTriggeredByPullRequestMergeWorker = createWorker(
SweetQueue.DEPLOYMENT_TRIGGERED_BY_PULL_REQUEST_MERGE,
async (job: Job<DeploymentTriggeredByPullRequestMergeJobData>) => {
logger.info("deploymentTriggeredByPullRequestMergeWorker", job.data);
const { workspaceId, pullRequestId } = job.data;
if (!workspaceId) {
throw new ResourceNotFoundException("Workspace ID not found", {
extra: { data: job.data },
});
}
if (!pullRequestId) {
throw new ResourceNotFoundException("Pull Request ID not found", {
extra: { data: job.data },
});
}
const pullRequest = await findPullRequestById({
workspaceId,
pullRequestId,
include: {
tracking: true,
},
});
if (!pullRequest?.tracking) {
throw new ResourceNotFoundException("Pull request not found", {
extra: { data: job.data, pullRequest },
});
}
const repository = await findRepositoryById({
workspaceId,
repositoryId: pullRequest.repositoryId,
include: {
applications: {
where: {
AND: [
{
deploymentSettings: {
path: ["trigger"],
equals: DeploymentSettingsTrigger.MERGE,
},
},
{
deploymentSettings: {
path: ["targetBranch"],
equals: pullRequest.targetBranch,
},
},
],
},
},
},
});
if (!repository) {
throw new ResourceNotFoundException("Repository not found");
}
if (!pullRequest.mergedAt || !pullRequest.mergeCommitSha) {
logger.info(
"deploymentTriggeredByPullRequestMergeWorker: Pull request not merged",
job.data
);
return;
}
if (!repository.applications?.length) {
logger.info(
"deploymentTriggeredByPullRequestMergeWorker: No applications found",
job.data
);
return;
}
const environment = await findOrCreateEnvironment({
workspaceId: workspaceId,
name: DEFAULT_PRODUCTION_ENVIRONMENT_NAME,
});
const promises = repository.applications.map(async (application) =>
createDeploymentFromPullRequestMerge({
application,
environment,
pullRequest: pullRequest as PullRequest & {
tracking: PullRequestTracking;
},
workspaceId: workspaceId,
})
);
await Promise.all(promises);
}
);