-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
integ.pipeline-code-commit-custom-event.ts
118 lines (109 loc) · 4.14 KB
/
integ.pipeline-code-commit-custom-event.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
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as cdk from 'aws-cdk-lib';
import * as cpactions from 'aws-cdk-lib/aws-codepipeline-actions';
import { CODECOMMIT_SOURCE_ACTION_DEFAULT_BRANCH_NAME, CODEPIPELINE_DEFAULT_PIPELINE_TYPE_TO_V2 } from 'aws-cdk-lib/cx-api';
import { ExpectedResult, IntegTest } from '@aws-cdk/integ-tests-alpha';
import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets';
import { Code, Runtime, Function } from 'aws-cdk-lib/aws-lambda';
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import * as path from 'path';
const defaultFeatureFlag = {
[CODECOMMIT_SOURCE_ACTION_DEFAULT_BRANCH_NAME]: true,
[CODEPIPELINE_DEFAULT_PIPELINE_TYPE_TO_V2]: false,
};
const app = new cdk.App({ postCliContext: defaultFeatureFlag });
const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-codecommit-custom-event');
const branch = 'test-branch';
const repo = new codecommit.Repository(stack, 'MyRepo', {
repositoryName: 'my-repo',
});
const eventPattern
= {
'detail-type': ['CodeCommit Repository State Change'],
'resources': [repo.repositoryArn],
'source': ['aws.codecommit'],
'detail': {
referenceType: ['branch'],
event: ['referenceCreated', 'referenceUpdated'],
referenceName: [branch],
},
};
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', {
pipelineName: 'IntegCustomEventPipeline',
crossAccountKeys: true,
stages: [
{
stageName: 'source',
actions: [
new cpactions.CodeCommitSourceAction({
actionName: 'source',
repository: repo,
output: new codepipeline.Artifact('SourceArtifact'),
customEventRule: {
eventPattern,
description: 'Custom event rule for CodeCommit',
ruleName: 'CustomEventRule',
target: new LambdaFunction(
new Function(stack, 'MyFunc', {
runtime: Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: Code.fromAsset(path.join(__dirname, 'custom-event-lambda')),
initialPolicy: [
new PolicyStatement({
actions: ['codepipeline:StartPipelineExecution'],
resources: [`arn:aws:codepipeline:${stack.region}:${stack.account}:*`],
}),
],
}),
),
},
}),
],
},
{
stageName: 'build',
actions: [
new cpactions.ManualApprovalAction({ actionName: 'manual' }),
],
},
],
});
const integ = new IntegTest(app, 'IntegTest', {
testCases: [stack],
});
// 1. create a commit on the default branch because you need a commit to create a new branch
const commit = integ.assertions.awsApiCall('CodeCommit', 'CreateCommit', {
branchName: 'main',
repositoryName: repo.repositoryName,
commitMessage: 'create default branch',
email: 'test@foo.bar',
authorName: 'test',
putFiles: [{
filePath: 'test.txt',
fileContent: Buffer.from('hello world').toString('base64'),
}],
});
// 2. Create a new branch because we test the custom event on a custom branch
integ.assertions.awsApiCall('CodeCommit', 'CreateBranch', {
branchName: branch,
commitId: commit.getAttString('commitId'),
repositoryName: repo.repositoryName,
});
// 3. Create a commit on the new branch
integ.assertions.awsApiCall('CodeCommit', 'CreateCommit', {
branchName: branch,
parentCommitId: commit.getAttString('commitId'),
repositoryName: repo.repositoryName,
commitMessage: 'Test Custom Event',
email: 'test@foo.bar',
authorName: 'test',
putFiles: [{
filePath: 'integ.txt',
fileContent: Buffer.from('This tests the custom events').toString('base64'),
}],
}).waitForAssertions().assertAtPath('commitId', ExpectedResult.stringLikeRegexp('^[0-9a-f]{40}$'));
// Finally, assert that the pipeline is in progress
integ.assertions.awsApiCall('CodePipeline', 'ListPipelineExecutions', {
pipelineName: pipeline.pipelineName,
}).waitForAssertions().assertAtPath('pipelineExecutionSummaries.0.status', ExpectedResult.stringLikeRegexp('InProgress'));