Skip to content

Instantly share code, notes, and snippets.

@saggie
Created May 17, 2020 04:53
Show Gist options
  • Save saggie/c3987d1118d13cdd7c7326b0efe930c3 to your computer and use it in GitHub Desktop.
Save saggie/c3987d1118d13cdd7c7326b0efe930c3 to your computer and use it in GitHub Desktop.
Notify a comment on AWS CodeCommit to Teams Webhook
import json
import urllib.request
teams_endpoint = "https://outlook.office.com/webhook/.../IncomingWebhook/..."
def build_text(detail, attr):
def get_username_from_arn(arn):
return arn.split(":")[-1]
def get_last_comment(comments):
return comments[-1]["commentText"]
def get_pullrequest_url(notificationBody):
return notificationBody[notificationBody.find("https://") :]
username = get_username_from_arn(detail["callerUserArn"])
repository_name = detail["repositoryName"]
file_path = attr["filePath"]
last_comment = get_last_comment(attr["comments"])
url = get_pullrequest_url(detail["notificationBody"])
text = []
text.append(
"### {} commented on {}/{}:".format(username, repository_name, file_path)
)
text.append(last_comment)
text.append("[View it on CodeCommit](" + url + ")")
return text
def lambda_handler(event, context):
message = json.loads(event["Records"][0]["Sns"]["Message"])
print("message: {}".format(message))
detail = message["detail"]
attr = message["additionalAttributes"]
text = build_text(detail, attr)
body = {"text": "\n\n".join(text)}
req = urllib.request.Request(
teams_endpoint,
data=json.dumps(body).encode("utf8"),
headers={"content-type": "application/json"},
)
urllib.request.urlopen(req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment