Skip to content

Commit 13025c4

Browse files
authored
refactor(codepipeline): API cleanup. (#2982)
BREAKING CHANGE: rename CodeCommitAction.pollForSourceChanges to trigger, and make it an enum. * Rename S3SourceAction.pollForSourceChanges to trigger, and make it an enum. * Rename StageAddToPipelineProps interface to StageOptions. * Remove the classes CloudFormationAction and CloudFormationDeployAction.
1 parent f73ee98 commit 13025c4

14 files changed

+385
-81
lines changed

packages/@aws-cdk/aws-codepipeline-actions/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const sourceAction = new codepipeline_actions.GitHubSourceAction({
6565
oauthToken: token.value,
6666
output: sourceOutput,
6767
branch: 'develop', // default: 'master'
68-
trigger: codepipeline_actions.GitHubTrigger.Poll // default: 'WebHook', 'None' is also possible for no Source trigger
68+
trigger: codepipeline_actions.GitHubTrigger.POLL // default: 'WEBHOOK', 'NONE' is also possible for no Source trigger
6969
});
7070
pipeline.addStage({
7171
stageName: 'Source',
@@ -99,8 +99,8 @@ pipeline.addStage({
9999
```
100100

101101
By default, the Pipeline will poll the Bucket to detect changes.
102-
You can change that behavior to use CloudWatch Events by setting the `pollForSourceChanges`
103-
property to `false` (it's `true` by default).
102+
You can change that behavior to use CloudWatch Events by setting the `trigger`
103+
property to `S3Trigger.EVENTS` (it's `S3Trigger.POLL` by default).
104104
If you do that, make sure the source Bucket is part of an AWS CloudTrail Trail -
105105
otherwise, the CloudWatch Events will not be emitted,
106106
and your Pipeline will not react to changes in the Bucket.
@@ -119,7 +119,7 @@ const sourceAction = new codepipeline_actions.S3SourceAction({
119119
bucketKey: key,
120120
bucket: sourceBucket,
121121
output: sourceOutput,
122-
pollForSourceChanges: false, // default: true
122+
trigger: codepipeline_actions.S3Trigger.EVENTS, // default: S3Trigger.POLL
123123
});
124124
```
125125

packages/@aws-cdk/aws-codepipeline-actions/lib/cloudformation/pipeline-actions.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Stack } from '@aws-cdk/cdk';
88
/**
99
* Properties common to all CloudFormation actions
1010
*/
11-
export interface CloudFormationActionProps extends codepipeline.CommonActionProps {
11+
interface CloudFormationActionProps extends codepipeline.CommonActionProps {
1212
/**
1313
* The name of the stack to apply this action to
1414
*/
@@ -60,7 +60,7 @@ export interface CloudFormationActionProps extends codepipeline.CommonActionProp
6060
/**
6161
* Base class for Actions that execute CloudFormation
6262
*/
63-
export abstract class CloudFormationAction extends codepipeline.Action {
63+
abstract class CloudFormationAction extends codepipeline.Action {
6464
constructor(props: CloudFormationActionProps, configuration?: any) {
6565
super({
6666
...props,
@@ -119,7 +119,7 @@ export class CloudFormationExecuteChangeSetAction extends CloudFormationAction {
119119
/**
120120
* Properties common to CloudFormation actions that stage deployments
121121
*/
122-
export interface CloudFormationDeployActionProps extends CloudFormationActionProps {
122+
interface CloudFormationDeployActionProps extends CloudFormationActionProps {
123123
/**
124124
* IAM role to assume when deploying changes.
125125
*
@@ -216,7 +216,7 @@ export interface CloudFormationDeployActionProps extends CloudFormationActionPro
216216
/**
217217
* Base class for all CloudFormation actions that execute or stage deployments.
218218
*/
219-
export abstract class CloudFormationDeployAction extends CloudFormationAction {
219+
abstract class CloudFormationDeployAction extends CloudFormationAction {
220220
private _deploymentRole?: iam.IRole;
221221
private readonly props: CloudFormationDeployActionProps;
222222

packages/@aws-cdk/aws-codepipeline-actions/lib/codecommit/source-action.ts

+48-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import targets = require('@aws-cdk/aws-events-targets');
44
import iam = require('@aws-cdk/aws-iam');
55
import { sourceArtifactBounds } from '../common';
66

7+
/**
8+
* How should the CodeCommit Action detect changes.
9+
* This is the type of the {@link CodeCommitSourceAction.trigger} property.
10+
*/
11+
export enum CodeCommitTrigger {
12+
/**
13+
* The Action will never detect changes -
14+
* the Pipeline it's part of will only begin a run when explicitly started.
15+
*/
16+
NONE = 'None',
17+
18+
/**
19+
* CodePipeline will poll the repository to detect changes.
20+
*/
21+
POLL = 'Poll',
22+
23+
/**
24+
* CodePipeline will use CloudWatch Events to be notified of changes.
25+
* This is the default method of detecting changes.
26+
*/
27+
EVENTS = 'Events',
28+
}
29+
730
/**
831
* Construction properties of the {@link CodeCommitSourceAction CodeCommit source CodePipeline Action}.
932
*/
@@ -19,12 +42,11 @@ export interface CodeCommitSourceActionProps extends codepipeline.CommonActionPr
1942
readonly branch?: string;
2043

2144
/**
22-
* Whether AWS CodePipeline should poll for source changes.
23-
* If this is `false`, the Pipeline will use CloudWatch Events to detect source changes instead.
45+
* How should CodePipeline detect source changes for this Action.
2446
*
25-
* @default false
47+
* @default CodeCommitTrigger.EVENTS
2648
*/
27-
readonly pollForSourceChanges?: boolean;
49+
readonly trigger?: CodeCommitTrigger;
2850

2951
/**
3052
* The CodeCommit repository.
@@ -36,9 +58,13 @@ export interface CodeCommitSourceActionProps extends codepipeline.CommonActionPr
3658
* CodePipeline Source that is provided by an AWS CodeCommit repository.
3759
*/
3860
export class CodeCommitSourceAction extends codepipeline.Action {
39-
private readonly props: CodeCommitSourceActionProps;
61+
private readonly repository: codecommit.IRepository;
62+
private readonly branch: string;
63+
private readonly createEvent: boolean;
4064

4165
constructor(props: CodeCommitSourceActionProps) {
66+
const branch = props.branch || 'master';
67+
4268
super({
4369
...props,
4470
category: codepipeline.ActionCategory.SOURCE,
@@ -47,34 +73,35 @@ export class CodeCommitSourceAction extends codepipeline.Action {
4773
outputs: [props.output],
4874
configuration: {
4975
RepositoryName: props.repository.repositoryName,
50-
BranchName: props.branch || 'master',
51-
PollForSourceChanges: props.pollForSourceChanges || false,
76+
BranchName: branch,
77+
PollForSourceChanges: props.trigger === CodeCommitTrigger.POLL,
5278
},
5379
});
5480

55-
this.props = props;
81+
this.repository = props.repository;
82+
this.branch = branch;
83+
this.createEvent = props.trigger === undefined ||
84+
props.trigger === CodeCommitTrigger.EVENTS;
5685
}
5786

5887
protected bind(info: codepipeline.ActionBind): void {
59-
if (!this.props.pollForSourceChanges) {
60-
this.props.repository.onCommit(info.pipeline.node.uniqueId + 'EventRule', {
88+
if (this.createEvent) {
89+
this.repository.onCommit(info.pipeline.node.uniqueId + 'EventRule', {
6190
target: new targets.CodePipeline(info.pipeline),
62-
branches: [this.props.branch || 'master']
91+
branches: [this.branch],
6392
});
6493
}
6594

6695
// https://docs.aws.amazon.com/codecommit/latest/userguide/auth-and-access-control-permissions-reference.html#aa-acp
67-
const actions = [
68-
'codecommit:GetBranch',
69-
'codecommit:GetCommit',
70-
'codecommit:UploadArchive',
71-
'codecommit:GetUploadArchiveStatus',
72-
'codecommit:CancelUploadArchive',
73-
];
74-
7596
info.role.addToPolicy(new iam.PolicyStatement({
76-
resources: [this.props.repository.repositoryArn],
77-
actions
97+
resources: [this.repository.repositoryArn],
98+
actions: [
99+
'codecommit:GetBranch',
100+
'codecommit:GetCommit',
101+
'codecommit:UploadArchive',
102+
'codecommit:GetUploadArchiveStatus',
103+
'codecommit:CancelUploadArchive',
104+
],
78105
}));
79106
}
80107
}

packages/@aws-cdk/aws-codepipeline-actions/lib/github/source-action.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export interface GitHubSourceActionProps extends codepipeline.CommonActionProps
5050
/**
5151
* How AWS CodePipeline should be triggered
5252
*
53-
* With the default value "WebHook", a webhook is created in GitHub that triggers the action
54-
* With "Poll", CodePipeline periodically checks the source for changes
53+
* With the default value "WEBHOOK", a webhook is created in GitHub that triggers the action
54+
* With "POLL", CodePipeline periodically checks the source for changes
5555
* With "None", the action is not triggered through changes in the source
5656
*
57-
* @default GitHubTrigger.WebHook
57+
* @default GitHubTrigger.WEBHOOK
5858
*/
5959
readonly trigger?: GitHubTrigger;
6060
}

packages/@aws-cdk/aws-codepipeline-actions/lib/s3/source-action.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ import targets = require('@aws-cdk/aws-events-targets');
33
import s3 = require('@aws-cdk/aws-s3');
44
import { sourceArtifactBounds } from '../common';
55

6+
/**
7+
* How should the S3 Action detect changes.
8+
* This is the type of the {@link S3SourceAction.trigger} property.
9+
*/
10+
export enum S3Trigger {
11+
/**
12+
* The Action will never detect changes -
13+
* the Pipeline it's part of will only begin a run when explicitly started.
14+
*/
15+
NONE = 'None',
16+
17+
/**
18+
* CodePipeline will poll S3 to detect changes.
19+
* This is the default method of detecting changes.
20+
*/
21+
POLL = 'Poll',
22+
23+
/**
24+
* CodePipeline will use CloudWatch Events to be notified of changes.
25+
* Note that the Bucket that the Action uses needs to be part of a CloudTrail Trail
26+
* for the events to be delivered.
27+
*/
28+
EVENTS = 'Events',
29+
}
30+
631
/**
732
* Construction properties of the {@link S3SourceAction S3 source Action}.
833
*/
@@ -20,15 +45,14 @@ export interface S3SourceActionProps extends codepipeline.CommonActionProps {
2045
readonly bucketKey: string;
2146

2247
/**
23-
* Whether AWS CodePipeline should poll for source changes.
24-
* If this is `false`, the Pipeline will use CloudWatch Events to detect source changes instead.
25-
* Note that if this is `false`, you need to make sure to include the source Bucket in a CloudTrail Trail,
48+
* How should CodePipeline detect source changes for this Action.
49+
* Note that if this is S3Trigger.EVENTS, you need to make sure to include the source Bucket in a CloudTrail Trail,
2650
* as otherwise the CloudWatch Events will not be emitted.
2751
*
28-
* @default true
52+
* @default S3Trigger.POLL
2953
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/log-s3-data-events.html
3054
*/
31-
readonly pollForSourceChanges?: boolean;
55+
readonly trigger?: S3Trigger;
3256

3357
/**
3458
* The Amazon S3 bucket that stores the source code
@@ -46,6 +70,8 @@ export class S3SourceAction extends codepipeline.Action {
4670
private readonly props: S3SourceActionProps;
4771

4872
constructor(props: S3SourceActionProps) {
73+
const pollForSourceChanges = props.trigger && props.trigger === S3Trigger.POLL;
74+
4975
super({
5076
...props,
5177
category: codepipeline.ActionCategory.SOURCE,
@@ -55,15 +81,15 @@ export class S3SourceAction extends codepipeline.Action {
5581
configuration: {
5682
S3Bucket: props.bucket.bucketName,
5783
S3ObjectKey: props.bucketKey,
58-
PollForSourceChanges: props.pollForSourceChanges,
84+
PollForSourceChanges: pollForSourceChanges,
5985
},
6086
});
6187

6288
this.props = props;
6389
}
6490

6591
protected bind(info: codepipeline.ActionBind): void {
66-
if (this.props.pollForSourceChanges === false) {
92+
if (this.props.trigger === S3Trigger.EVENTS) {
6793
this.props.bucket.onCloudTrailPutObject(info.pipeline.node.uniqueId + 'SourceEventRule', {
6894
target: new targets.CodePipeline(info.pipeline),
6995
paths: [this.props.bucketKey]

packages/@aws-cdk/aws-codepipeline-actions/test/cloudformation/test.cloudformation-pipeline-actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export = {
3131
actionName: 'source',
3232
output: sourceOutput,
3333
repository: repo,
34-
pollForSourceChanges: true,
34+
trigger: cpactions.CodeCommitTrigger.POLL,
3535
});
3636
pipeline.addStage({
3737
stageName: 'source',

0 commit comments

Comments
 (0)