-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
trigger.ts
313 lines (284 loc) · 11.3 KB
/
trigger.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { IAction } from './action';
import { CfnPipeline } from './codepipeline.generated';
/**
* Git push filter for trigger.
*/
export interface GitPushFilter {
/**
* The list of patterns of Git tags that, when pushed, are to be excluded from
* starting the pipeline.
*
* You can filter with glob patterns. The `tagsExcludes` takes priority
* over the `tagsIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no tags.
*/
readonly tagsExcludes?: string[];
/**
* The list of patterns of Git tags that, when pushed, are to be included as
* criteria that starts the pipeline.
*
* You can filter with glob patterns. The `tagsExcludes` takes priority
* over the `tagsIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no tags.
*/
readonly tagsIncludes?: string[];
}
/**
* Git pull request filter for trigger.
*/
export interface GitPullRequestFilter {
/**
* The list of patterns of Git branches that, when pull request events occurs, are
* to be excluded from starting the pipeline.
*
* You can filter with glob patterns. The `branchesExcludes` takes priority
* over the `branchesIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no branches.
*/
readonly branchesExcludes?: string[];
/**
* The list of patterns of Git branches that, when pull request events occurs, are
* to be included as criteria that starts the pipeline.
*
* You can filter with glob patterns. The `branchesExcludes` takes priority
* over the `branchesIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no branches.
*/
readonly branchesIncludes?: string[];
/**
* The list of patterns of Git repository file paths that, when pull request events occurs,
* are to be excluded from starting the pipeline.
*
* You can filter with glob patterns. The `filePathsExcludes` takes priority
* over the `filePathsIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no filePaths.
*/
readonly filePathsExcludes?: string[];
/**
* The list of patterns of Git repository file paths that, when pull request events occurs,
* are to be included as criteria that starts the pipeline.
*
* You can filter with glob patterns. The `filePathsExcludes` takes priority
* over the `filePathsIncludes`.
*
* Maximum length of this array is 8.
*
* @default - no filePaths.
*/
readonly filePathsIncludes?: string[];
/**
* The field that specifies which pull request events to filter on (opened, updated, closed)
* for the trigger configuration.
*
* @default - all events.
*/
readonly events?: GitPullRequestEvent[];
}
/**
* Event for trigger with pull request filter.
*/
export enum GitPullRequestEvent {
/**
* OPEN
*/
OPEN = 'OPEN',
/**
* UPDATED
*/
UPDATED = 'UPDATED',
/**
* CLOSED
*/
CLOSED = 'CLOSED',
}
/**
* Git configuration for trigger.
*/
export interface GitConfiguration {
/**
* The pipeline source action where the trigger configuration, such as Git tags.
*
* The trigger configuration will start the pipeline upon the specified change only.
* You can only specify one trigger configuration per source action.
*
* Since the provider for `sourceAction` must be `CodeStarSourceConnection`, you can use
* `CodeStarConnectionsSourceAction` construct in `aws-codepipeline-actions` module.
*/
readonly sourceAction: IAction;
/**
* The field where the repository event that will start the pipeline,
* such as pushing Git tags, is specified with details.
*
* Git tags is the only supported event type.
*
* The length must be less than or equal to 3.
*
* @default - no filter.
*/
readonly pushFilter?: GitPushFilter[];
/**
* The field where the repository event that will start the pipeline
* is specified as pull requests.
*
* The length must be less than or equal to 3.
*
* @default - no filter.
*/
readonly pullRequestFilter?: GitPullRequestFilter[];
}
/**
* Provider type for trigger.
*/
export enum ProviderType {
/**
* CodeStarSourceConnection
*/
CODE_STAR_SOURCE_CONNECTION = 'CodeStarSourceConnection',
}
/**
* Properties of trigger.
*/
export interface TriggerProps {
/**
* The source provider for the event, such as connections configured
* for a repository with Git tags, for the specified trigger configuration.
*/
readonly providerType: ProviderType;
/**
* Provides the filter criteria and the source stage for the repository
* event that starts the pipeline, such as Git tags.
*
* @default - no configuration.
*/
readonly gitConfiguration?: GitConfiguration;
}
/**
* Trigger.
*/
export class Trigger {
/**
* The pipeline source action where the trigger configuration.
*/
public readonly sourceAction: IAction | undefined;
constructor(private readonly props: TriggerProps) {
this.sourceAction = props.gitConfiguration?.sourceAction;
this.validate();
}
private validate() {
if (this.props.gitConfiguration) {
const { sourceAction, pushFilter, pullRequestFilter } = this.props.gitConfiguration;
if (sourceAction.actionProperties.provider !== 'CodeStarSourceConnection') {
throw new Error(`provider for actionProperties in sourceAction with name '${sourceAction.actionProperties.actionName}' must be 'CodeStarSourceConnection', got '${sourceAction.actionProperties.provider}'`);
}
if (pushFilter?.length && pullRequestFilter?.length) {
throw new Error(`cannot specify both pushFilter and pullRequestFilter for the trigger with sourceAction with name '${sourceAction.actionProperties.actionName}'`);
}
if (!pushFilter?.length && !pullRequestFilter?.length) {
throw new Error(`must specify either pushFilter or pullRequestFilter for the trigger with sourceAction with name '${sourceAction.actionProperties.actionName}'`);
}
if (pushFilter !== undefined && pushFilter.length > 3) {
throw new Error(`length of pushFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' must be less than or equal to 3, got ${pushFilter.length}`);
}
pushFilter?.forEach(filter => {
if (filter.tagsExcludes && filter.tagsExcludes.length > 8) {
throw new Error(`maximum length of tagsExcludes in pushFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.tagsExcludes.length}`);
}
if (filter.tagsIncludes && filter.tagsIncludes.length > 8) {
throw new Error(`maximum length of tagsIncludes in pushFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.tagsIncludes.length}`);
}
});
if (pullRequestFilter !== undefined && pullRequestFilter.length > 3) {
throw new Error(`length of pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' must be less than or equal to 3, got ${pullRequestFilter.length}`);
}
pullRequestFilter?.forEach(filter => {
if (!filter.branchesExcludes && !filter.branchesIncludes) {
throw new Error(`must specify branches in pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}'`);
}
if (filter.branchesExcludes && filter.branchesExcludes.length > 8) {
throw new Error(`maximum length of branchesExcludes in pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.branchesExcludes.length}`);
}
if (filter.branchesIncludes && filter.branchesIncludes.length > 8) {
throw new Error(`maximum length of branchesIncludes in pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.branchesIncludes.length}`);
}
if (filter.filePathsExcludes && filter.filePathsExcludes.length > 8) {
throw new Error(`maximum length of filePathsExcludes in pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.filePathsExcludes.length}`);
}
if (filter.filePathsIncludes && filter.filePathsIncludes.length > 8) {
throw new Error(`maximum length of filePathsIncludes in pullRequestFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' is 8, got ${filter.filePathsIncludes.length}`);
}
});
}
}
/**
* Render to CloudFormation property.
*
* @internal
*/
public _render(): CfnPipeline.PipelineTriggerDeclarationProperty {
let gitConfiguration: CfnPipeline.GitConfigurationProperty | undefined;
if (this.props.gitConfiguration) {
const { sourceAction, pushFilter, pullRequestFilter } = this.props.gitConfiguration;
const push = this.renderPushFilter(pushFilter);
const pullRequest = this.renderPullRequestFilter(pullRequestFilter);
gitConfiguration = {
// set to undefined if empty array because CloudFormation does not accept empty array
push: push?.length ? push : undefined,
// set to undefined if empty array because CloudFormation does not accept empty array
pullRequest: pullRequest?.length ? pullRequest : undefined,
sourceActionName: sourceAction.actionProperties.actionName,
};
}
return {
gitConfiguration,
providerType: this.props.providerType,
};
}
private renderPushFilter(pushFilter?: GitPushFilter[]): CfnPipeline.GitPushFilterProperty[] | undefined {
return pushFilter?.map(filter => {
const tags: CfnPipeline.GitTagFilterCriteriaProperty | undefined = {
// set to undefined if empty array because CloudFormation does not accept empty array
excludes: filter.tagsExcludes?.length ? filter.tagsExcludes : undefined,
includes: filter.tagsIncludes?.length ? filter.tagsIncludes : undefined,
};
return { tags };
});
}
private renderPullRequestFilter(pullRequest?: GitPullRequestFilter[]): CfnPipeline.GitPullRequestFilterProperty[] | undefined {
return pullRequest?.map(filter => {
const branches: CfnPipeline.GitBranchFilterCriteriaProperty | undefined =
filter.branchesExcludes?.length || filter.branchesIncludes?.length ? {
// set to undefined if empty array because CloudFormation does not accept empty array
excludes: filter.branchesExcludes?.length ? filter.branchesExcludes : undefined,
includes: filter.branchesIncludes?.length ? filter.branchesIncludes : undefined,
} : undefined;
const filePaths: CfnPipeline.GitFilePathFilterCriteriaProperty | undefined =
filter.filePathsExcludes?.length || filter.filePathsIncludes?.length ? {
// set to undefined if empty array because CloudFormation does not accept empty array
excludes: filter.filePathsExcludes?.length ? filter.filePathsExcludes : undefined,
includes: filter.filePathsIncludes?.length ? filter.filePathsIncludes : undefined,
} : undefined;
// set to all events if empty array or undefined because CloudFormation does not accept empty array and undefined
const events: string[] = filter.events?.length ? Array.from(new Set(filter.events)) : [
GitPullRequestEvent.OPEN,
GitPullRequestEvent.UPDATED,
GitPullRequestEvent.CLOSED,
];
return { branches, filePaths, events };
});
}
}