Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codecommit): make Repository a source for CodeStar Notifications #15739

Merged
merged 18 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-codecommit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ const rule = repo.onCommentOnPullRequest('CommentOnPullRequest', {
target: new targets.SnsTopic(myTopic),
});
```

## CodeStar Notifications

To define CodeStar Notification rules for Repositories, use one of the `notifyOnXxx()` methods.
They are very similar to `onXxx()` methods for CloudWatch events:

```ts
const target = new chatbot.SlackChannelConfiguration(stack, 'MySlackChannel', {
slackChannelConfigurationName: 'YOUR_CHANNEL_NAME',
slackWorkspaceId: 'YOUR_SLACK_WORKSPACE_ID',
slackChannelId: 'YOUR_SLACK_CHANNEL_ID',
});
const rule = repository.notifyOnPullRequestCreated('NotifyOnPullRequestCreated', target);
266 changes: 256 additions & 10 deletions packages/@aws-cdk/aws-codecommit/lib/repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import * as notifications from '@aws-cdk/aws-codestarnotifications';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import { IResource, Lazy, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnRepository } from './codecommit.generated';

export interface IRepository extends IResource {
/**
* Additional options to pass to the notification rule.
*/
export interface RepositoryNotifyOnOptions extends notifications.NotificationRuleOptions {
/**
* A list of event types associated with this notification rule for CodeCommit repositories.
* For a complete list of event types and IDs, see Notification concepts in the Developer Tools Console User Guide.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#concepts-api
*/
readonly events: RepositoryNotificationEvents[];
}

export interface IRepository extends IResource, notifications.INotificationRuleSource {
/**
* The ARN of this Repository.
* @attribute
Expand All @@ -18,19 +31,19 @@ export interface IRepository extends IResource {
readonly repositoryName: string;

/**
* The HTTP clone URL
* The HTTP clone URL.
* @attribute
*/
readonly repositoryCloneUrlHttp: string;

/**
* The SSH clone URL
* The SSH clone URL.
* @attribute
*/
readonly repositoryCloneUrlSsh: string;

/**
* The HTTPS (GRC) clone URL
* The HTTPS (GRC) clone URL.
*
* HTTPS (GRC) is the protocol to use with git-remote-codecommit (GRC).
*
Expand Down Expand Up @@ -92,7 +105,7 @@ export interface IRepository extends IResource {
onCommit(id: string, options?: OnCommitOptions): events.Rule;

/**
* Grant the given principal identity permissions to perform the actions on this repository
* Grant the given principal identity permissions to perform the actions on this repository.
*/
grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant;

Expand All @@ -110,13 +123,90 @@ export interface IRepository extends IResource {
* Grant the given identity permissions to read this repository.
*/
grantRead(grantee: iam.IGrantable): iam.Grant;

/**
* Defines a CodeStar Notification rule triggered when the project
* events specified by you are emitted. Similar to `onEvent` API.
*
* You can also use the methods to define rules for the specific event emitted.
* eg: `notifyOnPullRequstCreated`.
*
* @returns CodeStar Notifications rule associated with this repository.
*/
notifyOn(
id: string,
target: notifications.INotificationRuleTarget,
options: RepositoryNotifyOnOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when a comment is made on a pull request.
*/
notifyOnPullRequestComment(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when an approval status is changed.
*/
notifyOnApprovalStatusChanged(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when an approval rule is overridden.
*/
notifyOnApprovalRuleOverridden(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when a pull request is created.
*/
notifyOnPullRequestCreated(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when a pull request is merged.
*/
notifiyOnPullRequestMerged(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when a new branch or tag is created.
*/
notifyOnBranchOrTagCreated(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;

/**
* Defines a CodeStar Notification rule which triggers when a branch or tag is deleted.
*/
notifyOnBranchOrTagDeleted(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;
}
skinny85 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Options for the onCommit() method
* Options for the onCommit() method.
*/
export interface OnCommitOptions extends events.OnEventOptions {

/**
* The branch to monitor.
*
Expand Down Expand Up @@ -268,6 +358,101 @@ abstract class RepositoryBase extends Resource implements IRepository {
'codecommit:Describe*',
);
}

public notifyOn(
id: string,
target: notifications.INotificationRuleTarget,
options: RepositoryNotifyOnOptions,
): notifications.INotificationRule {
return new notifications.NotificationRule(this, id, {
...options,
source: this,
targets: [target],
});
}

public notifyOnPullRequestComment(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.PULL_REQUEST_COMMENT],
});
}

public notifyOnApprovalStatusChanged(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.APPROVAL_STATUS_CHANGED],
});
}

public notifyOnApprovalRuleOverridden(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.APPROVAL_RULE_OVERRIDDEN],
});
}

public notifyOnPullRequestCreated(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.PULL_REQUEST_CREATED],
});
}

public notifiyOnPullRequestMerged(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.PULL_REQUEST_MERGED],
});
}

public notifyOnBranchOrTagCreated(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.BRANCH_OR_TAG_CREATED],
});
}

public notifyOnBranchOrTagDeleted(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule {
return this.notifyOn(id, target, {
...options,
events: [RepositoryNotificationEvents.BRANCH_OR_TAG_DELETED],
});
}

public bindAsNotificationRuleSource(_scope: Construct): notifications.NotificationRuleSourceConfig {
return {
sourceArn: this.repositoryArn,
};
}
}

export interface RepositoryProps {
Expand All @@ -288,7 +473,7 @@ export interface RepositoryProps {
}

/**
* Provides a CodeCommit Repository
* Provides a CodeCommit Repository.
*/
export class Repository extends RepositoryBase {

Expand Down Expand Up @@ -401,7 +586,7 @@ export class Repository extends RepositoryBase {
*/
export interface RepositoryTriggerOptions {
/**
* A name for the trigger.Triggers on a repository must have unique names
* A name for the trigger.Triggers on a repository must have unique names.
*/
readonly name?: string;

Expand Down Expand Up @@ -437,7 +622,7 @@ export enum RepositoryEventTrigger {
}

/**
* Returns the clone URL for a protocol
* Returns the clone URL for a protocol.
*/
function makeCloneUrl(stack: Stack, repositoryName: string, protocol: 'https' | 'ssh' | 'grc', region?: string) {
switch (protocol) {
Expand All @@ -448,3 +633,64 @@ function makeCloneUrl(stack: Stack, repositoryName: string, protocol: 'https' |
return `codecommit::${region ?? stack.region}://${repositoryName}`;
}
}

/**
* List of event types for AWS CodeCommit
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-repositories
*/
export enum RepositoryNotificationEvents {
/**
* Trigger notication when comment made on commit.
*/
COMMIT_COMMENT = 'codecommit-repository-comments-on-commits',

/**
* Trigger notification when comment made on pull request.
*/
PULL_REQUEST_COMMENT = 'codecommit-repository-comments-on-pull-requests',

/**
* Trigger notification when approval status changed.
*/
APPROVAL_STATUS_CHANGED = 'codecommit-repository-approvals-status-changed',

/**
* Trigger notifications when approval rule is overridden.
*/
APPROVAL_RULE_OVERRIDDEN = 'codecommit-repository-approvals-rule-override',

/**
* Trigger notification when pull request created.
*/
PULL_REQUEST_CREATED = 'codecommit-repository-pull-request-created',

/**
* Trigger notification when pull request source updated.
*/
PULL_REQUEST_SOURCE_UPDATED = 'codecommit-repository-pull-request-source-updated',

/**
* Trigger notification when pull request status is changed.
*/
PULL_REQUEST_STATUS_CHANGED = 'codecommit-repository-pull-request-status-changed',

/**
* Trigger notification when pull requset is merged.
*/
PULL_REQUEST_MERGED = 'codecommit-repository-pull-request-merged',

/**
* Trigger notification when a branch or tag is created.
*/
BRANCH_OR_TAG_CREATED = 'codecommit-repository-branches-and-tags-created',

/**
* Trigger notification when a branch or tag is deleted.
*/
BRANCH_OR_TAG_DELETED = 'codecommit-repository-branches-and-tags-deleted',

/**
* Trigger notification when a branch or tag is updated.
*/
BRANCH_OR_TAG_UPDATED = 'codecommit-repository-branches-and-tags-updated',
}
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-codecommit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@
"@aws-cdk/assert-internal": "0.0.0"
},
"dependencies": {
"@aws-cdk/aws-codestarnotifications": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.3.69"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-codestarnotifications": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/core": "0.0.0",
Expand Down
Loading