This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda_function.py
executable file
·116 lines (97 loc) · 2.8 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
Search GitHub to see if a given commit is in a release
"""
from typing import Dict
import sys
import json
from argparse import ArgumentParser
import requests
def lambda_handler(event, context):
"""
AWS Lambda handler
"""
print(event)
status = check_for_commit(
account=event["queryStringParameters"]["account"],
repository=event["queryStringParameters"]["repository"],
commitish=event["queryStringParameters"]["commit"],
)
response_code = 200
body = json.dumps({'status': status})
response = {'statusCode': response_code, 'body': body}
return response
def main():
"""
Retrieve the arguments and check for the provided commit in the latest release
"""
config = get_args_config()
check_for_commit(
account=config["account"],
repository=config["repository"],
commitish=config["commit"],
)
def check_for_commit(*, account: str, repository: str, commitish: str) -> bool:
"""
Check a provided account/repo for a commitish
"""
url = (
"https://api.github.com/repos/"
+ account
+ "/"
+ repository
+ "/releases/latest"
)
headers = {"Accept": "application/vnd.github.v3+json"}
response = requests.get(url, headers=headers)
latest_release_json = response.json()
try:
latest_release_sha = latest_release_json["target_commitish"]
except KeyError:
print(
"Unable to identify the latest release commitish, are you being rate limited?"
)
sys.exit(1)
url = (
"https://api.github.com/repos/"
+ account
+ "/"
+ repository
+ "/compare/"
+ commitish
+ "..."
+ latest_release_sha
)
response = requests.get(url, headers=headers)
github_status_json = response.json()
try:
github_status = github_status_json["status"]
except KeyError:
print("Unable to identify the comparison status, are you being rate limited?")
sys.exit(1)
if github_status in ("ahead", "identical"):
print("YES! Go update")
return True
print("not yet ;(")
return False
def get_args_config() -> Dict:
"""
Get the configs passed as arguments
"""
parser = create_arg_parser()
return vars(parser.parse_args())
def create_arg_parser() -> ArgumentParser:
"""Parse the arguments"""
parser = ArgumentParser()
parser.add_argument(
"--account", type=str, required=True, help="github account",
)
parser.add_argument(
"--repository", type=str, required=True, help="github repository",
)
parser.add_argument(
"--commit", type=str, required=True, help="commitish to monitor for",
)
return parser
if __name__ == "__main__":
main()