-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworkflow.ts
More file actions
528 lines (467 loc) · 10.3 KB
/
workflow.ts
File metadata and controls
528 lines (467 loc) · 10.3 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import { Construct, Node } from 'constructs';
import { Job } from './job';
import { StringMap, DefaultsProps } from './types';
import { renameKeys, camelToSnake } from './utils';
/**
* Configuration for the CheckRun event.
*/
export interface CheckRunTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'rerequested' | 'completed' | 'requested_action'>;
}
/**
* Configuration for the CheckSuite event.
*/
export interface CheckSuiteTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'rerequested' | 'requested'>;
}
/**
* Configuration for the IssueComment event.
*/
export interface IssueCommentTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'edited' | 'deleted'>;
}
/**
* Configuration for the Issues event.
*/
export interface IssuesTypes {
/**
* Supported types.
*/
readonly types: Array<
| 'opened'
| 'edited'
| 'deleted'
| 'transferred'
| 'pinned'
| 'unpinned'
| 'closed'
| 'reopened'
| 'assigned'
| 'unassigned'
| 'labeled'
| 'unlabeled'
| 'locked'
| 'unlocked'
| 'milestoned'
| 'demilestoned'
>;
}
/**
* Configuration for the Label event.
*/
export interface LabelTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'edited' | 'deleted'>;
}
/**
* Configuration for the Milestone event.
*/
export interface MilestoneTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'closed' | 'opened' | 'edited' | 'deleted'>;
}
/**
* Configuration for the ProjectTypes event.
*/
export interface ProjectTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'updated' | 'closed' | 'reopened' | 'edited' | 'deleted'>;
}
/**
* Configuration for the ProjectCard event.
*/
export interface ProjectCardTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'moved' | 'converted' | 'edited' | 'deleted'>;
}
/**
* Configuration for the ProjectColumn event.
*/
export interface ProjectColumnTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'updated' | 'moved' | 'deleted'>;
}
/**
* Configuration for the PullRequest event.
*/
export interface PullRequestTypes extends PushTypes {
/**
* Supported types.
*/
readonly types?: Array<
| 'assigned'
| 'unassigned'
| 'labeled'
| 'unlabeled'
| 'opened'
| 'edited'
| 'closed'
| 'reopened'
| 'synchronize'
| 'ready_for_review'
| 'locked'
| 'unlocked'
| 'review_requested'
| 'review_request_removed'
>;
}
/**
* Configuration for the PullRequestReview event.
*/
export interface PullRequestReviewTypes {
/**
* Supported types.
*/
readonly types: Array<'submitted' | 'edited' | 'dismissed'>;
}
/**
* Configuration for the PullRequestReviewComment event.
*/
export interface PullRequestReviewCommentTypes {
/**
* Supported types.
*/
readonly types: Array<'created' | 'edited' | 'deleted'>;
}
/**
* Configuration for the PullRequestTarget event.
*/
export interface PullRequestTargetTypes {
/**
* Supported types.
*/
readonly types: Array<
| 'assigned'
| 'unassigned'
| 'labeled'
| 'unlabeled'
| 'opened'
| 'edited'
| 'closed'
| 'reopened'
| 'synchronize'
| 'ready_for_review'
| 'locked'
| 'unlocked'
| 'review_requested'
| 'review_request_removed'
>;
}
/**
* Configuration for the RegistryPackage event.
*/
export interface RegistryPackageTypes {
/**
* Supported types.
*/
readonly types: Array<'published' | 'updated'>;
}
/**
* Configuration for the Release event.
*/
export interface ReleaseTypes {
/**
* Supported types.
*/
readonly types: Array<'published' | 'unpublished' | 'created' | 'edited' | 'deleted' | 'prereleased' | 'released'>;
}
/**
* Configuration for the Watch event.
*/
export interface WatchTypes {
/**
* Supported types.
*/
readonly types: Array<'started'>;
}
/**
* Configuration for the Push event.
*/
export interface PushTypes {
/**
* Branches to trigger the workflow on.
*/
readonly branches?: string[];
/**
* Branches to ignore when triggering the workflow.
*/
readonly branchesIgnore?: string[];
/**
* Tags to trigger the workflow on.
*/
readonly tags?: string[];
/**
* Tags to ignore when triggering the workflow.
*/
readonly tagsIgnore?: string[];
/**
* Paths to trigger the workflow on.
*/
readonly paths?: string[];
/**
* Paths to ignore when triggering the workflow.
*/
readonly pathsIgnore?: string[];
}
/**
* Configuration for the Schedule event.
*/
export interface ScheduleEvent {
/**
* A cron schedule to run the workflow on.
*/
readonly schedule: [{ cron: string }];
}
/**
* Configuration for the WorkflowRun event.
*/
export interface WorkflowRunEvent {
/**
* A list of workflows to trigger from.
*/
readonly workflows: string[];
/**
* Branches to trigger this workflow on.
*/
readonly branches?: string[];
/**
* Branches to ignore when triggering this workflow.
*/
readonly branchesIgnore?: string[];
/**
* Supported types.
*/
readonly types?: Array<'completed' | 'requested'>;
}
/**
* Workflow dispatch input types.
*/
export enum WorkflowDispatchInputType {
CHOICE = 'choice',
BOOLEAN = 'boolean',
ENVIRONMENT = 'environment',
STRING = 'string',
}
/**
* Configuration for a manually dispatched workflow input
*/
export interface WorkflowDispatchEventInputProps {
/**
* The default value for the input if not supplied.
*/
readonly default?: string | boolean;
/**
* A description for this particular input.
*/
readonly description?: string;
/**
* Whether the input is required.
*/
readonly required: boolean;
/**
* The type that identifies this input.
*/
readonly type: WorkflowDispatchInputType;
}
/**
* Configuration for the WorkflowDispatch event properties.
*/
export interface WorkflowDispatchEventProps {
readonly inputs?: Record<string, WorkflowDispatchEventInputProps>;
}
/**
* Configuration for the WorkflowDispatch event.
*/
export interface WorkflowDispatchEvent {
readonly workflowDispatch: WorkflowDispatchEventProps;
}
/**
* Events without additional subtypes.
*/
export type EventStrings =
| 'repositoryDispatch'
| 'create'
| 'delete'
| 'deployment'
| 'deploymentStatus'
| 'fork'
| 'gollum'
| 'pageBuild'
| 'public'
| 'status';
/**
* Events with additional subtypes.
*/
export interface EventMap {
/**
* The checkRun event.
*/
readonly checkRun?: CheckRunTypes;
/**
* The checkSuite event.
*/
readonly checkSuite?: CheckSuiteTypes;
/**
* The issueComment event.
*/
readonly issueComment?: IssueCommentTypes;
/**
* The issues event.
*/
readonly issues?: IssuesTypes;
/**
* The label event.
*/
readonly label?: LabelTypes;
/**
* The milestone event.
*/
readonly milestone?: MilestoneTypes;
/**
* The project event.
*/
readonly project?: ProjectTypes;
/**
* The projectCard event.
*/
readonly projectCard?: ProjectCardTypes;
/**
* The projectColumn event.
*/
readonly projectColumn?: ProjectColumnTypes;
/**
* The pullRequest event.
*/
readonly pullRequest?: PullRequestTypes;
/**
* The pullRequestReview event.
*/
readonly pullRequestReview?: PullRequestReviewTypes;
/**
* The pullRequestReviewComment event.
*/
readonly pullRequestReviewComment?: PullRequestReviewCommentTypes;
/**
* The pullRequestTarget event.
*/
readonly pullRequestTarget?: PullRequestTargetTypes;
/**
* The push event.
*/
readonly push?: PushTypes;
/**
* The registryPackage event.
*/
readonly registryPackage?: RegistryPackageTypes;
/**
* The release event.
*/
readonly release?: ReleaseTypes;
/**
* The watch event.
*/
readonly watch?: WatchTypes;
}
/**
* All the events that can trigger a workflow.
*/
export type Events = keyof EventMap | EventStrings;
/**
* Configuration for a single GitHub Action workflow.
*/
export interface WorkflowProps {
/**
* Name of the workflow.
*/
readonly name: string;
/**
* When to run this workflow.
*/
readonly on: Events | Array<Events> | EventMap | ScheduleEvent | WorkflowRunEvent | WorkflowDispatchEvent;
/**
* A map of environment variables to provide to the job.
*/
readonly env?: StringMap;
/**
* A map of default settings to apply to all steps in this job.
*/
readonly defaults?: DefaultsProps;
}
/**
* Represents a GH Action workflow.
*/
export class Workflow extends Construct {
/**
* File to save synthesized workflow manifest in.
*/
public readonly outputFile: string;
/**
* An internal representation of a GH Action workflow.
*/
private readonly action: WorkflowProps;
/**
* Represents a GitHub Actions workflow.
* @param scope An ActionsStack instance.
* @param id The name of this workflow.
* @param action The configuration for this workflow.
*/
public constructor(scope: Construct, id: string, config: WorkflowProps) {
super(scope, id);
this.action = config;
this.outputFile = `cdkactions_${id}.yaml`;
}
/**
* Converts the workflow's configuration into a format that is GitHub Actions compatible.
*/
public toGHAction(): any {
const workflow = renameKeys(this.action, {
branchesIgnore: 'branches-ignore',
tagsIgnore: 'tags-ignore',
pathsIgnore: 'paths-ignore',
checkRun: 'check_run',
checkSuite: 'check_suite',
issueComment: 'issue_comment',
projectCard: 'project_card',
projectColumn: 'project_column',
pullRequest: 'pull_request',
pullRequestReview: 'pull_request_review',
pullRequestReviewComment: 'pull_request_review_comment',
pullRequestTarget: 'pull_request_target',
registryPackage: 'registry_package',
workingDirectory: 'working-directory',
});
if (typeof workflow.on == 'string') {
workflow.on = camelToSnake(workflow.on);
}
if (Array.isArray(workflow.on)) {
workflow.on = workflow.on.map(camelToSnake);
}
const ghaction: any = { ...workflow, jobs: {} };
for (const child of Node.of(this).children) {
if (child instanceof Job) {
// AWS constructs ensure children of constructs must have unique ids.
ghaction.jobs[child.id] = child.toGHAction();
}
}
return ghaction;
}
}