Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 2315efa

Browse files
committed
Initial commit
0 parents  commit 2315efa

File tree

6 files changed

+295
-0
lines changed

6 files changed

+295
-0
lines changed

.gitignore

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# lambda
2+
package
3+
function.zip
4+
5+
6+
7+
8+
9+
# Created by https://www.toptal.com/developers/gitignore/api/vim,python
10+
# Edit at https://www.toptal.com/developers/gitignore?templates=vim,python
11+
12+
### Python ###
13+
# Byte-compiled / optimized / DLL files
14+
__pycache__/
15+
*.py[cod]
16+
*$py.class
17+
18+
# C extensions
19+
*.so
20+
21+
# Distribution / packaging
22+
.Python
23+
build/
24+
develop-eggs/
25+
dist/
26+
downloads/
27+
eggs/
28+
.eggs/
29+
lib/
30+
lib64/
31+
parts/
32+
sdist/
33+
var/
34+
wheels/
35+
pip-wheel-metadata/
36+
share/python-wheels/
37+
*.egg-info/
38+
.installed.cfg
39+
*.egg
40+
MANIFEST
41+
42+
# PyInstaller
43+
# Usually these files are written by a python script from a template
44+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
45+
*.manifest
46+
*.spec
47+
48+
# Installer logs
49+
pip-log.txt
50+
pip-delete-this-directory.txt
51+
52+
# Unit test / coverage reports
53+
htmlcov/
54+
.tox/
55+
.nox/
56+
.coverage
57+
.coverage.*
58+
.cache
59+
nosetests.xml
60+
coverage.xml
61+
*.cover
62+
*.py,cover
63+
.hypothesis/
64+
.pytest_cache/
65+
pytestdebug.log
66+
67+
# Translations
68+
*.mo
69+
*.pot
70+
71+
# Django stuff:
72+
*.log
73+
local_settings.py
74+
db.sqlite3
75+
db.sqlite3-journal
76+
77+
# Flask stuff:
78+
instance/
79+
.webassets-cache
80+
81+
# Scrapy stuff:
82+
.scrapy
83+
84+
# Sphinx documentation
85+
docs/_build/
86+
doc/_build/
87+
88+
# PyBuilder
89+
target/
90+
91+
# Jupyter Notebook
92+
.ipynb_checkpoints
93+
94+
# IPython
95+
profile_default/
96+
ipython_config.py
97+
98+
# pyenv
99+
.python-version
100+
101+
# pipenv
102+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
103+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
104+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
105+
# install all needed dependencies.
106+
#Pipfile.lock
107+
108+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
109+
__pypackages__/
110+
111+
# Celery stuff
112+
celerybeat-schedule
113+
celerybeat.pid
114+
115+
# SageMath parsed files
116+
*.sage.py
117+
118+
# Environments
119+
.env
120+
.venv
121+
env/
122+
venv/
123+
ENV/
124+
env.bak/
125+
venv.bak/
126+
127+
# Spyder project settings
128+
.spyderproject
129+
.spyproject
130+
131+
# Rope project settings
132+
.ropeproject
133+
134+
# mkdocs documentation
135+
/site
136+
137+
# mypy
138+
.mypy_cache/
139+
.dmypy.json
140+
dmypy.json
141+
142+
# Pyre type checker
143+
.pyre/
144+
145+
# pytype static type analyzer
146+
.pytype/
147+
148+
### Vim ###
149+
# Swap
150+
[._]*.s[a-v][a-z]
151+
!*.svg # comment out if you don't need vector files
152+
[._]*.sw[a-p]
153+
[._]s[a-rt-v][a-z]
154+
[._]ss[a-gi-z]
155+
[._]sw[a-p]
156+
157+
# Session
158+
Session.vim
159+
Sessionx.vim
160+
161+
# Temporary
162+
.netrwhist
163+
*~
164+
# Auto-generated tag files
165+
tags
166+
# Persistent undo
167+
[._]*.un~
168+
169+
# End of https://www.toptal.com/developers/gitignore/api/vim,python

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.PHONY: all
2+
all: lambda
3+
4+
.PHONY: requirements
5+
requirements: requirements-to-freeze.txt
6+
@python3 -c 'print("Updating requirements.txt...")'
7+
@docker run --rm -v $$(pwd):/usr/src/app/ python:3.8 /bin/bash -c "python -m pip install --upgrade pip && pip install -r /usr/src/app/requirements-to-freeze.txt && pip freeze > /usr/src/app/requirements.txt"
8+
9+
.PHONY: clean
10+
clean: clean-python
11+
12+
.PHONY: clean-python
13+
clean-python:
14+
@# Prepending each recipe with - to continue regardless of errors per
15+
@# https://www.gnu.org/software/make/manual/html_node/Errors.html
16+
@-find . -type d -name '__pycache__' -exec rm -rf {} +
17+
@-find . -type d -name '.mypy_cache' -exec rm -rf {} +
18+
@-find . -type d -name '.pytest_cache' -exec rm -rf {} +
19+
@-find . -type f -name '*.pyc' -delete
20+
21+
.PHONY: lambda
22+
lambda: clean
23+
@docker run --rm -v $$(pwd):/usr/src/app/ python:3.8 /bin/bash -c "cd /usr/src/app/ && apt-get update && apt-get -y --no-install-recommends install zip && python -m pip install --upgrade pip && zip function.zip lambda_function.py && pip install --target ./package -r requirements.txt && cd package && zip -r9 ../function.zip ."
24+
@echo "docker run --rm -it --env-file <(env | grep AWS_) -v $$(pwd):/usr/src/app/ -v ${HOME}/.aws:/root/.aws easy_infra aws lambda update-function-code --function-name release_monitor --zip-file fileb:///usr/src/app/function.zip"

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Release Monitor
2+
3+
This project uses the unauthenticated GitHub APIs to monitor a provided account/repo for a release containing a provided commit. It is meant to be run as an AWS lambda on a cron, and send an email notification when a release with the provided commit is detected.
4+
5+
## Quickstart
6+
1. Create a lambda in your AWS account.
7+
1. Auth to AWS via environment variables.
8+
1. Run the following:
9+
```bash
10+
make
11+
docker run --rm -it --env-file <(env | grep AWS_) -v $(pwd):/usr/src/app/ -v ${HOME}/.aws:/root/.aws seiso/easy_infra aws lambda update-function-code --function-name release_monitor --zip-file fileb:///usr/src/app/function.zip
12+
```
13+

lambda_function.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Search GitHub to see if a given commit is in a release
4+
"""
5+
6+
from typing import Dict
7+
import sys
8+
from argparse import ArgumentParser
9+
import requests
10+
11+
def lambda_handler(event, context):
12+
"""
13+
AWS Lambda handler
14+
"""
15+
check_for_commit(account=event["account"], repository=event["repository"], commitish=event["commit"])
16+
17+
18+
def main():
19+
"""
20+
Retrieve the arguments and check for the provided commit in the latest release
21+
"""
22+
config = get_args_config()
23+
24+
check_for_commit(account=config["account"], repository=config["repository"], commitish=config["commit"])
25+
26+
27+
def check_for_commit(*, account: str, repository: str, commitish: str) -> bool:
28+
"""
29+
Check a provided account/repo for a commitish
30+
"""
31+
url = "https://api.github.com/repos/" + account + "/" + repository + "/releases/latest"
32+
headers = {"Accept": "application/vnd.github.v3+json"}
33+
34+
response = requests.get(url, headers=headers)
35+
latest_release_json = response.json()
36+
try:
37+
latest_release_sha = latest_release_json["target_commitish"]
38+
except KeyError:
39+
print("Unable to identify the latest release commitish, are you being rate limited?")
40+
sys.exit(1)
41+
42+
url = "https://api.github.com/repos/" + account + "/" + repository + "/compare/" + commitish + "..." + latest_release_sha
43+
44+
response = requests.get(url, headers=headers)
45+
github_status_json = response.json()
46+
try:
47+
github_status = github_status_json["status"]
48+
except KeyError:
49+
print("Unable to identify the comparison status, are you being rate limited?")
50+
sys.exit(1)
51+
52+
if github_status in ("ahead", "identical"):
53+
print("YES! Go update")
54+
return True
55+
56+
print("not yet ;(")
57+
return False
58+
59+
60+
def get_args_config() -> Dict:
61+
"""
62+
Get the configs passed as arguments
63+
"""
64+
parser = create_arg_parser()
65+
return vars(parser.parse_args())
66+
67+
68+
def create_arg_parser() -> ArgumentParser:
69+
"""Parse the arguments"""
70+
parser = ArgumentParser()
71+
parser.add_argument(
72+
"--account", type=str, required=True, help="github account",
73+
)
74+
parser.add_argument(
75+
"--repository", type=str, required=True, help="github repository",
76+
)
77+
parser.add_argument(
78+
"--commit", type=str, required=True, help="commitish to monitor for",
79+
)
80+
return parser
81+
82+
if __name__ == "__main__":
83+
main()

requirements-to-freeze.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
certifi==2020.6.20
2+
chardet==3.0.4
3+
idna==2.10
4+
requests==2.24.0
5+
urllib3==1.25.10

0 commit comments

Comments
 (0)