-
Notifications
You must be signed in to change notification settings - Fork 9
/
lambda_function.py
38 lines (30 loc) · 1.13 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
import base64
import json
import os
import requests
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
def lambda_main(event, context):
path = event['rawPath']
if 'body' in event:
if event['isBase64Encoded']:
body = base64.b64decode(event['body'])
else:
body = event['body']
else:
body = None
signature = event['headers'].get("x-hub-signature", "")
if signature.startswith("sha1="):
signature = signature[5:]
if path == "/example-path":
# https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#star
payload = json.loads(body)
repository = payload['repository']
repository_url = repository['url']
stars = payload['stargazers_count']
new_name = f"This-Repo-Has-{stars}-Star{'s' if stars != 1 else ''}"
headers = {'Authorization': f"Bearer {GITHUB_TOKEN}"}
data = {'name': new_name}
response = requests.patch(repository_url, headers=headers, json=data)
return {'statusCode': response.status_code, 'body': "OK"}
else:
return {'statusCode': 404, 'body': ""}