-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdefaultReleaseManager.ts
103 lines (87 loc) · 3.07 KB
/
defaultReleaseManager.ts
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
import type { DataRelease } from '@/core/typings/Data';
import type { GitlabDeploymentHook } from '@/core/typings/GitlabDeploymentHook';
import type {
ReleaseManager,
ReleaseOptions,
} from '../../src/release/typings/ReleaseManager';
import type { ReleaseStateUpdate } from '../../src/release/typings/ReleaseStateUpdate';
const dockerBuildJobNames = [
'Build Image',
'Build with KO',
'build-application',
'build_ecs_image',
'build_image',
];
async function getReleaseStateUpdate(
{ failedDeployments, state, successfulDeployments }: DataRelease,
deploymentHook?: GitlabDeploymentHook
): Promise<ReleaseStateUpdate[]> {
if (deploymentHook === undefined) {
const isProductionEnvironment = successfulDeployments.some((env) =>
env.startsWith('production')
);
return isProductionEnvironment && state === 'monitoring'
? [{ deploymentState: 'completed', environment: 'production' }]
: [];
}
const { environment, status } = deploymentHook;
if (environment.startsWith('staging')) {
switch (status) {
case 'failed':
return [{ deploymentState: 'failed', environment: 'staging' }];
case 'running':
return [{ deploymentState: 'deploying', environment: 'staging' }];
case 'success':
return [{ deploymentState: 'monitoring', environment: 'staging' }];
default:
throw new Error(`Unhandled staging deployment status: ${status}`);
}
} else if (environment.startsWith('production')) {
switch (status) {
case 'failed':
return [{ deploymentState: 'failed', environment: 'production' }];
case 'running':
return failedDeployments.length === 0
? [
{ deploymentState: 'completed', environment: 'staging' },
{ deploymentState: 'deploying', environment: 'production' },
]
: [{ deploymentState: 'deploying', environment: 'production' }];
case 'success':
return [{ deploymentState: 'monitoring', environment: 'production' }];
default:
throw new Error(`Unhandled production deployment status: ${status}`);
}
} else if (environment.startsWith('support')) {
switch (status) {
case 'failed':
return [{ deploymentState: 'failed', environment: 'support' }];
case 'running':
return [{ deploymentState: 'deploying', environment: 'support' }];
case 'success':
return [{ deploymentState: 'completed', environment: 'support' }];
default:
throw new Error(`Unhandled support deployment status: ${status}`);
}
}
return [];
}
async function isReadyToRelease(
{ projectId }: DataRelease,
mainBranchPipelineId: number,
{ gitlab: { fetchPipelineJobs } }: ReleaseOptions
) {
const pipelinesJobs = await fetchPipelineJobs(
projectId,
mainBranchPipelineId
);
const dockerBuildJob = pipelinesJobs.find((job) =>
dockerBuildJobNames.includes(job.name)
);
return dockerBuildJob?.status === 'success';
}
const defaultReleaseManager: ReleaseManager = {
getReleaseStateUpdate,
isReadyToRelease,
};
export default defaultReleaseManager;