-
Notifications
You must be signed in to change notification settings - Fork 1
/
stargazer.py
39 lines (32 loc) · 1.05 KB
/
stargazer.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
import json
import os
import requests
WEBHOOK_URL = os.environ['WEBHOOK_URL']
def stargazer(event, context):
"""Stargazer event handler, process WatchEvent from Github and post to Slack webhook"""
message_body = json.loads(event['body'])
repo = message_body['repository']['name']
stars = message_body['repository']['stargazers_count']
username = message_body['sender']['login']
url = message_body['sender']['html_url']
slack_post_data = { 'text': f"""
New Github star for _{repo}_ repo!.,
The *{repo}* repo now has *{stars}* stars! :tada:.,
Your new fan is <{url}|{username}>"""
}
try:
r = requests.post(
WEBHOOK_URL,
json=slack_post_data,
headers={'Content-Type': 'application/json'}
)
return {
"statusCode": 200,
"body": r.text
}
except requests.exceptions.RequestException as err:
err_message = f'RequestException: {err}'
return {
"statusCode": 500,
'error': err_message
}