/
code-build-projects.ts
67 lines (60 loc) · 2.52 KB
/
code-build-projects.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
import * as AWS from 'aws-sdk';
import { ISDK } from '../aws-auth';
import { ChangeHotswapImpact, ChangeHotswapResult, establishResourcePhysicalName, HotswapOperation, HotswappableChangeCandidate, lowerCaseFirstCharacter, transformObjectKeys } from './common';
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template';
export async function isHotswappableCodeBuildProjectChange(
logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate,
): Promise<ChangeHotswapResult> {
if (change.newValue.Type !== 'AWS::CodeBuild::Project') {
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
}
const updateProjectInput: AWS.CodeBuild.UpdateProjectInput = {
name: '',
};
for (const updatedPropName in change.propertyUpdates) {
const updatedProp = change.propertyUpdates[updatedPropName];
switch (updatedPropName) {
case 'Source':
updateProjectInput.source = transformObjectKeys(
await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue),
convertSourceCloudformationKeyToSdkKey,
);
break;
case 'Environment':
updateProjectInput.environment = await transformObjectKeys(
await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue),
lowerCaseFirstCharacter,
);
break;
case 'SourceVersion':
updateProjectInput.sourceVersion = await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue);
break;
default:
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
}
}
const projectName = await establishResourcePhysicalName(logicalId, change.newValue.Properties?.Name, evaluateCfnTemplate);
if (!projectName) {
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
}
updateProjectInput.name = projectName;
return new ProjectHotswapOperation(updateProjectInput);
}
class ProjectHotswapOperation implements HotswapOperation {
public readonly service = 'codebuild'
public readonly resourceNames: string[];
constructor(
private readonly updateProjectInput: AWS.CodeBuild.UpdateProjectInput,
) {
this.resourceNames = [`CodeBuild project '${updateProjectInput.name}'`];
}
public async apply(sdk: ISDK): Promise<any> {
return sdk.codeBuild().updateProject(this.updateProjectInput).promise();
}
}
function convertSourceCloudformationKeyToSdkKey(key: string): string {
if (key.toLowerCase() === 'buildspec') {
return key.toLowerCase();
}
return lowerCaseFirstCharacter(key);
}