Skip to content

Commit 6d30bc3

Browse files
committed
Update
1 parent 3c86048 commit 6d30bc3

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

Lambda/CodeCommit-Change-Notification/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ Note the `cloneUrlHttp` and `Arn` values in the response.
1515

1616
## Create and Subscribe to the SNS Topic
1717

18+
```sh
19+
aws sns create-topic --name CodeCommitChangeNotification
20+
21+
aws sns subscribe \
22+
--topic-arn arn:aws:sns:us-east-1:123456789012:CodeCommitChangeNotification \
23+
--protocol email \
24+
--notification-endpoint my-email@example.com
25+
```
26+
1827
## Create IAM Lambda Execution Role
1928

2029
1. Add `AWSLambdaBasicExecutionRole`
@@ -38,6 +47,12 @@ Note the `cloneUrlHttp` and `Arn` values in the response.
3847

3948
Name: **CodeCommitChangeNotification**
4049

50+
Set the following enviornment variables:
51+
52+
`REPOSITORY_NAME` = `ChangeNotification`
53+
54+
`SNS_TOPIC_ARN` = `arn:aws:sns:us-east-1:123456789012:CodeCommitChangeNotification`
55+
4156
## Create the CloudWatch Event Rule
4257

4358
This rule will detect branch or repository changes:

Lambda/CodeCommit-Change-Notification/lambda_function.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import datetime
2-
import json
32
import os
43

54
import boto3
@@ -29,23 +28,7 @@ def get_diff(repository_name, last_commit_id, previous_commit_id):
2928
afterCommitSpecifier=last_commit_id
3029
)
3130

32-
differences = []
33-
34-
if response is None:
35-
return differences
36-
37-
while "nextToken" in response:
38-
response = codecommit.get_differences(
39-
repositoryName=repository_name,
40-
beforeCommitSpecifier=previous_commit_id,
41-
afterCommitSpecifier=last_commit_id,
42-
nextToken=response["nextToken"]
43-
)
44-
differences += response.get("differences", [])
45-
else:
46-
differences += response["differences"]
47-
48-
return differences
31+
return response["differences"]
4932

5033

5134
def get_diff_change_message_type(change_type):
@@ -74,7 +57,6 @@ def get_last_commit_log(repository, commit_id):
7457

7558

7659
def get_message_text(differences, last_commit):
77-
print(last_commit)
7860
text = ''
7961
commit_id = last_commit['commitId']
8062
text += f'commit ID: {commit_id}\n'
@@ -98,7 +80,8 @@ def get_message_text(differences, last_commit):
9880
get_diff_change_message_type(diff['changeType']),
9981
diff['beforeBlob']['blobId'])
10082

101-
text += (f'Commit: https://{AWS_DEFAULT_REGION}.console.aws.amazon.com/codesuite/'
83+
text += (f'Commit: '
84+
f'https://{AWS_DEFAULT_REGION}.console.aws.amazon.com/codesuite/'
10285
f'codecommit/repositories/{REPOSITORY_NAME}/commit/{commit_id}?'
10386
f'region={AWS_DEFAULT_REGION}')
10487

@@ -114,13 +97,10 @@ def publish(repository, message):
11497

11598

11699
def lambda_handler(event, context):
117-
print(json.dumps(event))
118-
119-
repository = REPOSITORY_NAME
120100

121101
try:
122-
last_commit_id = get_last_commit_id(repository, MAIN_BRANCH_NAME)
123-
last_commit = get_last_commit_log(repository, last_commit_id)
102+
last_commit_id = get_last_commit_id(REPOSITORY_NAME, MAIN_BRANCH_NAME)
103+
last_commit = get_last_commit_log(REPOSITORY_NAME, last_commit_id)
124104

125105
previous_commit_id = None
126106
if len(last_commit['parents']) > 0:
@@ -129,15 +109,18 @@ def lambda_handler(event, context):
129109
print(f'Last commit ID: {last_commit_id}')
130110
print(f'Previous commit ID: {previous_commit_id}')
131111

132-
differences = get_diff(repository, last_commit_id, previous_commit_id)
112+
differences = get_diff(
113+
REPOSITORY_NAME, last_commit_id, previous_commit_id)
133114
message_text = get_message_text(differences, last_commit)
134115

135-
return publish(repository, message_text)
116+
print(message_text)
117+
118+
return publish(REPOSITORY_NAME, message_text)
136119

137120
except Exception as e:
138121
import traceback
139122
print(e)
140123
traceback.print_exc()
141-
print(f'Error getting repository {repository}. Ensure it exists in the '
142-
'same region as this function.')
124+
print(f'Error getting repository {REPOSITORY_NAME}. Ensure it exists '
125+
'in the same region as this function.')
143126
raise e

0 commit comments

Comments
 (0)