generated from szikszail/npm-package
/
codebuild.ts
218 lines (199 loc) · 6.97 KB
/
codebuild.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Match } from "aws-cdk-lib/assertions";
import { BuildEnvironmentVariableType, CfnProject, CfnSourceCredential } from "aws-cdk-lib/aws-codebuild";
import { AdvancedTemplate } from "./advanced-template";
import { Resource, RemovableResource } from "./resource";
export interface CodeBuildProjectTriggerEvent {
type: string;
pattern?: string;
}
export enum CredentialAuthType {
OAUTH = 'OAUTH',
BASIC_AUTH = 'BASIC_AUTH',
PERSONAL_ACCESS_TOKEN = 'PERSONAL_ACCESS_TOKEN'
}
export enum CrednetialServerType {
GITHUB = 'GITHUB',
GITHUB_ENTERPRISE = 'GITHUB_ENTERPRISE',
BITBUCKET = 'BITBUCKET',
}
/**
* A test construct for CloudBuild Source Credentials resource
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codebuild.CfnSourceCredential.html}
*/
export class CodeBuildSourceCredentials extends RemovableResource {
constructor(template: AdvancedTemplate, props?: any) {
super(CfnSourceCredential.CFN_RESOURCE_TYPE_NAME, template, props);
}
/**
* Sets matching credential properties for the source credentials
* @param authType The authentication type (exact match)
* @param serverType The server type (exact match)
* @param token The token (partial match)
* @param username The username of the token (partial match)
* @returns
*/
public withCredentials(authType: CredentialAuthType, serverType: CrednetialServerType, token: string, username?: string) {
this.withProperty('AuthType', authType);
this.withProperty('ServerType', serverType);
this.withProperty('Token', Match.stringLikeRegexp(token));
if (username) {
this.withProperty('Username', Match.stringLikeRegexp(username));
}
return this;
}
/**
* Sets matching credentials for GitHub
* @param token The GitHub access token
* @returns
*/
public withGitHubPersonalAccessToken(token: string) {
return this.withCredentials(CredentialAuthType.PERSONAL_ACCESS_TOKEN, CrednetialServerType.GITHUB, token);
}
/**
* Sets matching credentials for GitHub Enterprise
* @param token The GitHub access token
* @returns
*/
public withGitHubEnterprisePersonalAccessToken(token: string) {
return this.withCredentials(CredentialAuthType.PERSONAL_ACCESS_TOKEN, CrednetialServerType.GITHUB_ENTERPRISE, token);
}
/**
* Sets matching credentials for BitBucket
* @param username The username
* @param password The password
* @returns
*/
public withBitBucketUser(username: string, password: string) {
return this.withCredentials(CredentialAuthType.BASIC_AUTH, CrednetialServerType.BITBUCKET, password, username);
}
}
/**
* A test construct for a CodeBuild Project resource
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codebuild.CfnProject.html}
*/
export class CodeBuildProject extends RemovableResource {
private environmentVariables: any[];
private source: any;
constructor(template: AdvancedTemplate, props?: any) {
super(CfnProject.CFN_RESOURCE_TYPE_NAME, template, props);
this.environmentVariables = [];
this.source = {};
}
/**
* Sets a matching Service Role to another test construct resource
* @param resource The test construct resource to set
* @returns
*/
public withServiceRole(resource: Resource) {
this.withProperty('ServiceRole', resource.arn);
return this;
}
/**
* Sets a matching source for the project
* @param type The source type to match (exact match)
* @param location The whole or a partial location
* @returns
*/
public withSource(type: string, location: string) {
this.source = this.source || {};
this.source.Location = Match.stringLikeRegexp(location);
this.source.Type = type;
this.withProperty('Source', Match.objectLike(this.source));
return this;
}
/**
* Sets a matching environment variable for the project
* @param name The whole of a partial name of the environment variable
* @param value The value of the environment variable (resource, string, or a matcher)
* @param type The type of the environment variable
* @returns
*/
public withEnvironmentVariable(name: string, value?: any, type?: BuildEnvironmentVariableType) {
const environmentVariable: any = {
Name: Match.stringLikeRegexp(name),
Type: type || BuildEnvironmentVariableType.PLAINTEXT,
};
if (value) {
if (value instanceof Resource) {
environmentVariable.Value = value.arn;
} else if (typeof value === "string") {
environmentVariable.Value = Match.stringLikeRegexp(value);
} else {
environmentVariable.Value = value;
}
}
this.environmentVariables.push(Match.objectLike(environmentVariable));
this.withProperty('Environment', Match.objectLike({
EnvironmentVariables: Match.arrayWith(this.environmentVariables),
}));
return this;
}
/**
* Sets a matching concurrent build limit
* @param limit The exact limit to match
* @returns
*/
public withConcurrentBuildLimit(limit: number) {
this.withProperty('ConcurrentBuildLimit', limit);
return this;
}
/**
* Sets a matching trigger for the project
* @param events The trigger events to match with
* @param webhook Whether a webhook is used for the trigger
* @returns
*/
public withTriggers(events: CodeBuildProjectTriggerEvent[], webhook = true) {
const eventMatchers = events.map(e => {
const filter: any = { Type: e.type };
if (e.pattern) {
filter.Pattern = Match.stringLikeRegexp(e.pattern);
}
return Match.objectLike(filter);
});
this.withProperty('Triggers', Match.objectLike({
Webhook: !!webhook,
FilterGroups: Match.arrayWith([
Match.arrayWith(eventMatchers)
])
}));
return this;
}
/**
* Sets a matching artifact for the project
* @param type The type of the artifact (e.g., S3)
* @param location The location of the artifact (resource, string, or a matcher)
* @param encryption Whether encryption is enabled for the artifact
* @param packaging Whether packaging is enabled for the artifact
* @returns
*/
public withArtifact(type: string, location?: any, encryption?: boolean, packaging?: boolean) {
const artifact: any = {
EncryptionDisabled: !encryption,
Type: type || "S3",
Packaging: packaging || "NONE",
};
if (location) {
if (location instanceof Resource) {
artifact.Location = location.ref;
} else if (typeof location === "string") {
artifact.Location = Match.stringLikeRegexp(location);
} else {
artifact.Location = location;
}
}
this.withProperty('Artifacts', Match.objectLike(artifact));
return this;
}
/**
* Sets a matching build spec for the project
* @param command A partial build spec command
* @returns
*/
public withBuildSpec(command: string) {
this.source = this.source || {};
this.source.BuildSpec = Match.stringLikeRegexp(command);
this.withProperty('Source', Match.objectLike(this.source));
return this;
}
}