-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
43 lines (35 loc) · 1.15 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import uuid
import boto3
print("Loading function")
def lambda_handler(event, context):
# Extract the incoming message
msg = json.loads(event["Records"][0]["Sns"]["Message"])
# Extract the relevant CodeGuru call parameters
repo = msg["detail"]["repositoryName"]
branch = msg["detail"]["referenceName"]
assn = None
guru = boto3.client("codeguru-reviewer")
for x in guru.list_repository_associations()[
"RepositoryAssociationSummaries"
]:
if x["Name"] == repo:
assn = x["AssociationArn"]
if assn == None:
m = f"No CodeGuru association found for repo {repo}"
print(m)
return json.dumps({"status": m})
# Call CodeGuru
result = guru.create_code_review(
Name=f"{msg['detail']['repositoryName']}-{uuid.uuid4().hex}",
RepositoryAssociationArn=assn,
Type={
"RepositoryAnalysis": {"RepositoryHead": {"BranchName": branch}},
"AnalysisTypes": [
"Security",
"CodeQuality",
],
},
)
# Return the result of the Boto call
return json.dumps(result, default=str)