Skip to content

Commit

Permalink
Merge pull request #28 from gammarers/feature/add-pipeline-execution-…
Browse files Browse the repository at this point in the history
…change-state-filter

feat: add pipeline execution change state filter
  • Loading branch information
yicr authored Nov 15, 2024
2 parents 581c557 + 5a34060 commit 9f5c0e2
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 1 deletion.
69 changes: 69 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion src/codepipeline-execution-state-change-detection-event-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ import { Construct } from 'constructs';
* export interface CodePipelineStateChangeDetectionEventRuleProps extends Omit<events.RuleProps, 'eventPattern'> {}
*/

export interface CodePipelineExecutionStateChangeDetectionEventRuleProps extends events.RuleProps {}
export enum CodePipelineExecutionState {
CANCELED = 'CANCELED',
FAILED = 'FAILED',
RESUMED = 'RESUMED',
STARTED = 'STARTED',
STOPPED = 'STOPPED',
STOPPING = 'STOPPING',
SUCCEEDED = 'SUCCEEDED',
SUPERSEDED = 'SUPERSEDED',
}

export interface CodePipelineExecutionStateChangeDetectionEventRuleProps extends events.RuleProps {
readonly targetStates?: CodePipelineExecutionState[];
}

export class CodePipelineExecutionStateChangeDetectionEventRule extends events.Rule {
constructor(scope: Construct, id: string, props: CodePipelineExecutionStateChangeDetectionEventRuleProps) {
Expand All @@ -22,6 +35,17 @@ export class CodePipelineExecutionStateChangeDetectionEventRule extends events.R
return {
source: ['aws.codepipeline'],
detailType: ['CodePipeline Pipeline Execution State Change'],
// detail: {
// state: props.targetStates,
// },
detail: (() => {
if (props.targetStates) {
return {
state: props.targetStates,
};
}
return undefined;
})(),
};
})(),
});
Expand Down
63 changes: 63 additions & 0 deletions test/__snapshots__/rule.options.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions test/rule.options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { App, Stack } from 'aws-cdk-lib';
import { Template, Match } from 'aws-cdk-lib/assertions';
import * as events from 'aws-cdk-lib/aws-events';
import { CodePipelineExecutionState, CodePipelineExecutionStateChangeDetectionEventRule } from '../src';

describe('Default Rule Check', () => {

const app = new App();
const stack = new Stack(app, 'TestingStack', {
env: {
account: '123456789012',
region: 'us-east-1',
},
});

const rule = new CodePipelineExecutionStateChangeDetectionEventRule(stack, 'CodePipelineExecutionStateChangeDetectionEventRule', {
ruleName: 'codepipeline-state-change-detection-event-rule',
targetStates: [
CodePipelineExecutionState.FAILED,
CodePipelineExecutionState.CANCELED,
],
});

it('Is Rule', async () => {
expect(rule).toBeInstanceOf(events.Rule);
});

const template = Template.fromStack(stack);

it('Should match event rule.', async () => {
template.hasResourceProperties('AWS::Events::Rule', Match.objectEquals({
Name: 'codepipeline-state-change-detection-event-rule',
State: 'ENABLED',
EventPattern: Match.objectEquals({
'source': [
'aws.codepipeline',
],
'detail-type': [
'CodePipeline Pipeline Execution State Change',
],
'detail': {
state: Match.arrayEquals(['FAILED', 'CANCELED']),
},
}),
}));
});

it('Should match snapshot.', async () => {
expect(template.toJSON()).toMatchSnapshot();
});

});

0 comments on commit 9f5c0e2

Please sign in to comment.