aws-lambda Added CodeBuild CloudWatch state event (#45490)

* added type and handler for CodeBuild Cloudwatch State event

* added type and handler for CodeBuild Cloudwatch event

* added test for codebuild cloudwatch event

* extended definitions block

* added SECRETS_MANAGER as environment variable type

* removed handlers

* removed unnecessary general codebuild types

* CodeBuildCloudWatchStateEvent extends EventBridgeEvent and added CodeBuildCloudWatchStateHandler

* added general type strArrayOrUndefined for tests

* added tests for CodeBuildCloudWatchStateHandler
This commit is contained in:
Ivan Martos 2020-06-24 13:31:44 +02:00 committed by GitHub
parent c98e50d9ce
commit 413fe846ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 332 additions and 0 deletions

View File

@ -16,6 +16,7 @@ declare let bool: boolean;
declare let boolOrUndefined: boolean | undefined;
declare let boolOrNumOrStr: boolean | number | string;
declare let numOrUndefined: number | undefined;
declare let strArrayOrUndefined: string[] | undefined;
// handler.d.ts types
declare let context: AWSLambda.Context;

View File

@ -35,6 +35,7 @@
// Alexander Pepper <https://github.com/apepper>
// Alessandro Palumbo <https://github.com/apalumbo>
// Sachin Shekhar <https://github.com/SachinShekhar>
// Ivan Martos <https://github.com/ivanmartos>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@ -52,6 +53,7 @@ export * from "./trigger/cloudfront-response";
export * from "./trigger/cloudwatch-events";
export * from "./trigger/cloudwatch-logs";
// TODO: export * from "./trigger/codecommit";
export * from "./trigger/codebuild-cloudwatch-state";
export * from "./trigger/codepipeline";
export * from "./trigger/codepipeline-cloudwatch";
export * from "./trigger/codepipeline-cloudwatch-action";

View File

@ -0,0 +1,224 @@
import {
CodeBuildCloudWatchStateEvent,
CodeBuildCloudWatchStateHandler, CodeBuildStateEventDetail,
SNSEvent,
} from 'aws-lambda';
declare let codeBuildStateEventDetail: CodeBuildStateEventDetail;
/* CodeBuild CloudWatch Events
* see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
const CodeBuildCloudWatchStateEvent: CodeBuildCloudWatchStateEvent = {
version: '0',
id: 'abcdefgrt-2384-7605-5520-1530a88cf416',
'detail-type': 'CodeBuild Build State Change',
source: 'aws.codebuild',
account: '123456789123',
time: '2020-06-15T10:41:43Z',
region: 'eu-west-1',
resources: [
'arn:aws:codebuild:eu-west-1:123456789123:build/codebuild-project-name:12345678-1234-5678-aaaa-bbbbbbbbbbbb',
],
detail: {
'build-status': 'FAILED',
'project-name': 'codebuild-project-name',
'build-id':
'arn:aws:codebuild:eu-west-1:123456789123:build/codebuild-project-name:12345678-1234-5678-aaaa-bbbbbbbbbbbb',
'additional-information': {
cache: {
type: 'NO_CACHE',
},
'build-number': 1027,
'timeout-in-minutes': 60,
'build-complete': true,
initiator: 'GitHub-Hookshot/123456',
'build-start-time': 'Jun 15, 2020 10:30:41 AM',
source: {
buildspec: 'folder/buildspec.yml',
location: 'https://github.com/org/project-name',
type: 'GITHUB',
},
'source-version': '123456e065cfbfd44b4a21871c83fdyagdansindr',
artifact: {
location: '',
},
environment: {
image: 'aws/codebuild/standard:3.0',
'privileged-mode': true,
'image-pull-credentials-type': 'CODEBUILD',
'compute-type': 'BUILD_GENERAL1_MEDIUM',
type: 'LINUX_CONTAINER',
'environment-variables': [
{
name: 'AWS_ACCOUNT_ID',
type: 'PLAINTEXT',
value: '123456789123',
},
{
name: 'STAGE',
type: 'PLAINTEXT',
value: 'stageName',
},
],
},
'project-file-system-locations': [],
logs: {
'group-name': '/aws/codebuild/codebuild-project-name',
'stream-name': '12345678-1234-5678-aaaa-bbbbbbbbbbbb',
'deep-link':
'https://console.aws.amazon.com/cloudwatch/home?region=eu-west-1#logEvent:group=/aws/codebuild/codebuild-project-name;stream=12345678-1234-5678-aaaa-bbbbbbbbbbbb',
},
phases: [
{
'phase-context': [],
'start-time': 'Jun 15, 2020 10:30:41 AM',
'end-time': 'Jun 15, 2020 10:30:42 AM',
'duration-in-seconds': 0,
'phase-type': 'SUBMITTED',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [],
'start-time': 'Jun 15, 2020 10:30:42 AM',
'end-time': 'Jun 15, 2020 10:30:43 AM',
'duration-in-seconds': 1,
'phase-type': 'QUEUED',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:30:43 AM',
'end-time': 'Jun 15, 2020 10:31:07 AM',
'duration-in-seconds': 23,
'phase-type': 'PROVISIONING',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:31:07 AM',
'end-time': 'Jun 15, 2020 10:31:33 AM',
'duration-in-seconds': 26,
'phase-type': 'DOWNLOAD_SOURCE',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:31:33 AM',
'end-time': 'Jun 15, 2020 10:33:12 AM',
'duration-in-seconds': 99,
'phase-type': 'INSTALL',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:33:12 AM',
'end-time': 'Jun 15, 2020 10:33:12 AM',
'duration-in-seconds': 0,
'phase-type': 'PRE_BUILD',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [
'COMMAND_EXECUTION_ERROR: Error while executing command: $(npm bin)/cypress run --headless --browser chrome. Reason: exit status 10',
],
'start-time': 'Jun 15, 2020 10:33:12 AM',
'end-time': 'Jun 15, 2020 10:41:39 AM',
'duration-in-seconds': 506,
'phase-type': 'BUILD',
'phase-status': 'FAILED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:41:39 AM',
'end-time': 'Jun 15, 2020 10:41:39 AM',
'duration-in-seconds': 0,
'phase-type': 'POST_BUILD',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:41:39 AM',
'end-time': 'Jun 15, 2020 10:41:39 AM',
'duration-in-seconds': 0,
'phase-type': 'UPLOAD_ARTIFACTS',
'phase-status': 'SUCCEEDED',
},
{
'phase-context': [': '],
'start-time': 'Jun 15, 2020 10:41:39 AM',
'end-time': 'Jun 15, 2020 10:41:41 AM',
'duration-in-seconds': 2,
'phase-type': 'FINALIZING',
'phase-status': 'SUCCEEDED',
},
{
'start-time': 'Jun 15, 2020 10:41:41 AM',
'phase-type': 'COMPLETED',
},
],
'queued-timeout-in-minutes': 480,
},
'current-phase': 'COMPLETED',
'current-phase-context': '[: ]',
version: '1',
},
};
const eventBridgeHandler: CodeBuildCloudWatchStateHandler = async (event, context, callback) => {
str = event.account;
codeBuildStateEventDetail = event.detail;
str = event.id;
str = event.region;
str = event.resources[0];
str = event.source;
str = event.time;
str = event['detail-type'];
const detail: CodeBuildStateEventDetail = event.detail;
str = detail['build-status'];
str = detail['project-name'];
str = detail['build-id'];
str = detail['current-phase'];
str = detail['current-phase-context'];
str = detail.version;
str = detail['additional-information'].cache.type;
num = detail['additional-information']['build-number'];
num = detail['additional-information']['timeout-in-minutes'];
bool = detail['additional-information']['build-complete'];
str = detail['additional-information']['initiator'];
str = detail['additional-information']['build-start-time'];
str = detail['additional-information'].source.buildspec;
str = detail['additional-information'].source.location;
str = detail['additional-information'].source.type;
str = detail['additional-information']['source-version'];
str = detail['additional-information'].artifact.location;
str = detail['additional-information'].environment.image;
bool = detail['additional-information'].environment['privileged-mode'];
strOrUndefined = detail['additional-information'].environment['image-pull-credentials-type'];
str = detail['additional-information'].environment['compute-type'];
str = detail['additional-information'].environment.type;
str = detail['additional-information'].environment['environment-variables'][0].name;
strOrUndefined = detail['additional-information'].environment['environment-variables'][0].type;
str = detail['additional-information'].environment['environment-variables'][0].value;
str = detail['additional-information'].logs['group-name'];
str = detail['additional-information'].logs['stream-name'];
str = detail['additional-information'].logs['deep-link'];
strArrayOrUndefined = detail['additional-information'].phases[0]['phase-context'];
str = detail['additional-information'].phases[0]['start-time'];
strOrUndefined = detail['additional-information'].phases[0]['end-time'];
numOrUndefined = detail['additional-information'].phases[0]['duration-in-seconds'];
str = detail['additional-information'].phases[0]['phase-type'];
strOrUndefined = detail['additional-information'].phases[0]['phase-status'];
num = detail['additional-information']['queued-timeout-in-minutes'];
callback();
callback(new Error());
};
export const snsEventCodeBuildStateHandler = async (snsEvent: SNSEvent) => {
const event: CodeBuildCloudWatchStateEvent = JSON.parse(snsEvent.Records[0].Sns.Message);
codeBuildStateEventDetail = event.detail;
};

View File

@ -0,0 +1,104 @@
import { EventBridgeEvent, EventBridgeHandler } from './eventbridge';
export type CodeBuildCloudWatchStateHandler = EventBridgeHandler<'CodeBuild Build State Change', CodeBuildStateEventDetail, void>;
export type CodeBuildStateType = 'IN_PROGRESS' | 'SUCCEEDED' | 'FAILED' | 'STOPPED';
export type CodeBuildPhaseType =
| 'COMPLETED'
| 'FINALIZING'
| 'UPLOAD_ARTIFACTS'
| 'POST_BUILD'
| 'BUILD'
| 'PRE_BUILD'
| 'INSTALL'
| 'QUEUED'
| 'DOWNLOAD_SOURCE'
| 'PROVISIONING'
| 'SUBMITTED';
export type CodeBuildPhaseStatusType =
| 'TIMED_OUT'
| 'STOPPED'
| 'FAILED'
| 'SUCCEEDED'
| 'FAULT'
| 'CLIENT_ERROR';
export type CodeBuildCacheType = 'NO_CACHE' | 'LOCAL' | 'S3';
export type CodeBuildSourceLocationType =
| 'CODECOMMIT'
| 'CODEPIPELINE'
| 'GITHUB'
| 'GITHUB_ENTERPRISE'
| 'BITBUCKET'
| 'S3'
| 'NO_SOURCE';
export type CodeBuildEnvironmentType =
| 'LINUX_CONTAINER'
| 'LINUX_GPU_CONTAINER'
| 'WINDOWS_CONTAINER'
| 'ARM_CONTAINER';
export type CodeBuildEnvironmentPullCredentialsType = 'CODEBUILD' | 'SERVICE_ROLE';
export type CodeBuildEnvironmentComputeType =
| 'BUILD_GENERAL1_SMALL'
| 'BUILD_GENERAL1_MEDIUM'
| 'BUILD_GENERAL1_LARGE'
| 'BUILD_GENERAL1_2XLARGE';
export type CodeBuildEnvironmentVariableType = 'PARAMETER_STORE' | 'PLAINTEXT' | 'SECRETS_MANAGER';
export interface CodeBuildStateEventDetail {
'build-status': CodeBuildStateType;
'project-name': string;
'build-id': string;
'current-phase': CodeBuildPhaseType;
'current-phase-context': string;
version: string;
'additional-information': {
cache: {
type: CodeBuildCacheType;
};
'build-number': number;
'timeout-in-minutes': number;
'build-complete': boolean;
initiator: string;
'build-start-time': string;
source: {
buildspec: string;
location: string;
type: CodeBuildSourceLocationType;
};
'source-version': string;
artifact: {
location: string;
};
environment: {
image: string;
'privileged-mode': boolean;
'image-pull-credentials-type'?: CodeBuildEnvironmentPullCredentialsType;
'compute-type': CodeBuildEnvironmentComputeType;
type: CodeBuildEnvironmentType;
'environment-variables': Array<{
name: string;
type?: CodeBuildEnvironmentVariableType;
value: string;
}>;
};
'project-file-system-locations': [];
logs: {
'group-name': string;
'stream-name': string;
'deep-link': string;
};
phases: Array<{
'phase-context'?: string[]; // Not available for COMPLETED phase-type
'start-time': string;
'end-time'?: string; // Not available for COMPLETED phase-type
'duration-in-seconds'?: number; // Not available for COMPLETED phase-type
'phase-type': CodeBuildPhaseType;
'phase-status'?: CodeBuildPhaseStatusType; // Not available for COMPLETED phase-type
}>;
'queued-timeout-in-minutes': number;
};
}
export interface CodeBuildCloudWatchStateEvent extends EventBridgeEvent<'CodeBuild Build State Change', CodeBuildStateEventDetail> {
source: 'aws.codebuild';
}

View File

@ -25,6 +25,7 @@
"test/cloudfront-tests.ts",
"test/cloudwatch-tests.ts",
"test/codepipeline-tests.ts",
"test/codebuild-tests.ts",
"test/cognito-tests.ts",
"test/dynamodb-tests.ts",
"test/eventbridge-tests.ts",