# AWS CodeStarNotifications Construct Library

This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.

## NotificationRule

The [`NotificationRule`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRule.html) construct defines an AWS CodeStarNotifications rule.
The rule specifies the events you want notifications about and the targets
(such as Amazon SNS topics or AWS Chatbot clients configured for Slack)
where you want to receive them.
Notification targets are objects that implement the [`INotificationRuleTarget`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleTarget.html)
interface and notification source is object that implement the [`INotificationRuleSource`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleSource.html) interface.

## Notification Targets

This module includes classes that implement the [`INotificationRuleTarget`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleTarget.html) interface for SNS and slack in AWS Chatbot.

The following targets are supported:

* `SNS`: specify event and notify to SNS topic.
* `AWS Chatbot`: specify event and notify to slack channel and only support `SlackChannelConfiguration`.

## Examples

TypeScript:

```ts
import * as notifications from 'aws-cdk-lib/aws-codestarnotifications';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as chatbot from 'aws-cdk-lib/aws-chatbot';

const project = new codebuild.PipelineProject(this, 'MyProject');

const topic = new sns.Topic(this, 'MyTopic1');

const slack = new chatbot.SlackChannelConfiguration(this, 'MySlackChannel', {
  slackChannelConfigurationName: 'YOUR_CHANNEL_NAME',
  slackWorkspaceId: 'YOUR_SLACK_WORKSPACE_ID',
  slackChannelId: 'YOUR_SLACK_CHANNEL_ID',
});

const rule = new notifications.NotificationRule(this, 'NotificationRule', {
  source: project,
  events: [
    'codebuild-project-build-state-succeeded',
    'codebuild-project-build-state-failed',
  ],
  targets: [topic],
  notificationRuleName: 'MyNotificationRuleName',
  enabled: true, // The default is true
  detailType: notifications.DetailType.FULL, // The default is FULL
  createdBy: 'Jone Doe',
});
rule.addTarget(slack);
```

Python:

```python
import aws_cdk.aws_codestarnotifications as notifications
import aws_cdk.aws_codebuild as codebuild
import aws_cdk.aws_sns as sns
import aws_cdk.aws_chatbot as chatbot

project = codebuild.PipelineProject(self, "MyProject")

topic = sns.Topic(self, "MyTopic1")

slack = chatbot.SlackChannelConfiguration(self, "MySlackChannel",
    slack_channel_configuration_name="YOUR_CHANNEL_NAME",
    slack_workspace_id="YOUR_SLACK_WORKSPACE_ID",
    slack_channel_id="YOUR_SLACK_CHANNEL_ID"
)

rule = notifications.NotificationRule(self, "NotificationRule",
    source=project,
    events=["codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed"
    ],
    targets=[topic],
    notification_rule_name="MyNotificationRuleName",
    enabled=True,  # The default is true
    detail_type=notifications.DetailType.FULL,  # The default is FULL
    created_by="Jone Doe"
)
rule.add_target(slack)
```

Java:

```java
import software.amazon.awscdk.services.codestarnotifications.*;
import software.amazon.awscdk.services.codebuild.*;
import software.amazon.awscdk.services.sns.*;
import software.amazon.awscdk.services.chatbot.*;

PipelineProject project = new PipelineProject(this, "MyProject");

Topic topic = new Topic(this, "MyTopic1");

SlackChannelConfiguration slack = SlackChannelConfiguration.Builder.create(this, "MySlackChannel")
        .slackChannelConfigurationName("YOUR_CHANNEL_NAME")
        .slackWorkspaceId("YOUR_SLACK_WORKSPACE_ID")
        .slackChannelId("YOUR_SLACK_CHANNEL_ID")
        .build();

NotificationRule rule = NotificationRule.Builder.create(this, "NotificationRule")
        .source(project)
        .events(List.of("codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed"))
        .targets(List.of(topic))
        .notificationRuleName("MyNotificationRuleName")
        .enabled(true) // The default is true
        .detailType(DetailType.FULL) // The default is FULL
        .createdBy("Jone Doe")
        .build();
rule.addTarget(slack);
```

C#:

```csharp
using Amazon.CDK.AWS.CodeStarNotifications;
using Amazon.CDK.AWS.CodeBuild;
using Amazon.CDK.AWS.SNS;
using Amazon.CDK.AWS.Chatbot;

var project = new PipelineProject(this, "MyProject");

var topic = new Topic(this, "MyTopic1");

var slack = new SlackChannelConfiguration(this, "MySlackChannel", new SlackChannelConfigurationProps {
    SlackChannelConfigurationName = "YOUR_CHANNEL_NAME",
    SlackWorkspaceId = "YOUR_SLACK_WORKSPACE_ID",
    SlackChannelId = "YOUR_SLACK_CHANNEL_ID"
});

var rule = new NotificationRule(this, "NotificationRule", new NotificationRuleProps {
    Source = project,
    Events = new [] { "codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed" },
    Targets = new [] { topic },
    NotificationRuleName = "MyNotificationRuleName",
    Enabled = true,  // The default is true
    DetailType = DetailType.FULL,  // The default is FULL
    CreatedBy = "Jone Doe"
});
rule.AddTarget(slack);
```

Go:

```go
import "github.com/aws/aws-cdk-go/awscdk"
import codebuild "github.com/aws/aws-cdk-go/awscdk"
import sns "github.com/aws/aws-cdk-go/awscdk"
import chatbot "github.com/aws/aws-cdk-go/awscdk"

project := codebuild.NewPipelineProject(this, jsii.String("MyProject"))

topic := sns.NewTopic(this, jsii.String("MyTopic1"))

slack := chatbot.NewSlackChannelConfiguration(this, jsii.String("MySlackChannel"), &SlackChannelConfigurationProps{
	SlackChannelConfigurationName: jsii.String("YOUR_CHANNEL_NAME"),
	SlackWorkspaceId: jsii.String("YOUR_SLACK_WORKSPACE_ID"),
	SlackChannelId: jsii.String("YOUR_SLACK_CHANNEL_ID"),
})

rule := notifications.NewNotificationRule(this, jsii.String("NotificationRule"), &NotificationRuleProps{
	Source: project,
	Events: []*string{
		jsii.String("codebuild-project-build-state-succeeded"),
		jsii.String("codebuild-project-build-state-failed"),
	},
	Targets: []INotificationRuleTarget{
		topic,
	},
	NotificationRuleName: jsii.String("MyNotificationRuleName"),
	Enabled: jsii.Boolean(true),
	 // The default is true
	DetailType: notifications.DetailType_FULL,
	 // The default is FULL
	CreatedBy: jsii.String("Jone Doe"),
})
rule.AddTarget(slack)
```

## Notification Source

This module includes classes that implement the [`INotificationRuleSource`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleSource.html) interface for AWS CodeBuild,
AWS CodePipeline and will support AWS CodeCommit, AWS CodeDeploy in future.

The following sources are supported:

* `AWS CodeBuild`: support codebuild project to trigger notification when event specified.
* `AWS CodePipeline`: support codepipeline to trigger notification when event specified.

## Events

For the complete list of supported event types for CodeBuild and CodePipeline, see:

* [Events for notification rules on build projects](https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-buildproject).
* [Events for notification rules on pipelines](https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline).

## Types

### Constructs (L2)

- [`NotificationRule`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRule.html) Construct (L2) A new notification rule.

### Constructs (L1)

- [`CfnNotificationRule`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.CfnNotificationRule.html) Construct (L1) Creates a notification rule for a resource.

### Structs

- [`CfnNotificationRule.TargetProperty`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.CfnNotificationRule.TargetProperty.html) Struct Information about the topics or clients associated with a notification rule.
- [`CfnNotificationRuleProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.CfnNotificationRuleProps.html) Struct Properties for defining a `CfnNotificationRule`.
- [`NotificationRuleOptions`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRuleOptions.html) Struct Standard set of options for `notifyOnXxx` codestar notification handler on construct.
- [`NotificationRuleProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRuleProps.html) Struct Properties for a new notification rule.
- [`NotificationRuleSourceConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRuleSourceConfig.html) Struct Information about the Codebuild or CodePipeline associated with a notification source.
- [`NotificationRuleTargetConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.NotificationRuleTargetConfig.html) Struct Information about the SNS topic or AWS Chatbot client associated with a notification target.

### Enums

- [`DetailType`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.DetailType.html) Enum The level of detail to include in the notifications for this resource.

### Interfaces

- [`INotificationRule`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRule.html) Interface Represents a notification rule.
- [`INotificationRuleSource`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleSource.html) Interface Represents a notification source The source that allows CodeBuild and CodePipeline to associate with this rule.
- [`INotificationRuleTarget`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codestarnotifications.INotificationRuleTarget.html) Interface Represents a notification target That allows AWS Chatbot and SNS topic to associate with this rule target.
