mirror of
https://github.com/MaxenceG2M/github-release-notifier.git
synced 2025-12-08 13:53:24 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a43d462bf1 | |||
| 2d48a37ccb | |||
| 68fd6e78d9 | |||
| e57d9cf656 | |||
| 8dba474e23 | |||
| 07dccee235 | |||
| fd7e6b73d1 | |||
| 0ee46a5d9e | |||
| 9709947c28 | |||
| 89e88c8a1b | |||
| 148c647ac7 | |||
| 02fad7767c | |||
| 74e6488044 | |||
| f3380139ed | |||
| 92953dbb5c | |||
| 11834803c8 | |||
| bd5bf11661 |
2
.git-blame-ignore-revs
Normal file
2
.git-blame-ignore-revs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Black format commit
|
||||||
|
89e88c8a1b26e2c6ca242d0bba6cdba8da35c3ae
|
||||||
28
.pre-commit-config.yaml
Normal file
28
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v4.5.0
|
||||||
|
hooks:
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: check-yaml
|
||||||
|
- id: check-toml
|
||||||
|
- id: check-added-large-files
|
||||||
|
- id: check-merge-conflict
|
||||||
|
- repo: https://github.com/psf/black
|
||||||
|
rev: 23.11.0
|
||||||
|
hooks:
|
||||||
|
- id: black
|
||||||
|
- repo: https://github.com/pycqa/isort
|
||||||
|
rev: 5.12.0
|
||||||
|
hooks:
|
||||||
|
- id: isort
|
||||||
|
name: isort
|
||||||
|
- repo: https://github.com/PyCQA/autoflake
|
||||||
|
rev: v2.1.1
|
||||||
|
hooks:
|
||||||
|
- id: autoflake
|
||||||
|
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
|
||||||
|
rev: v2.11.0
|
||||||
|
hooks:
|
||||||
|
- id: pretty-format-yaml
|
||||||
|
args: [--autofix, --indent, '2', --offset, '2']
|
||||||
10
Dockerfile
Normal file
10
Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
FROM python:3.10-alpine3.18
|
||||||
|
|
||||||
|
RUN pip install requests
|
||||||
|
WORKDIR /app
|
||||||
|
COPY notifier.py template.html /app/
|
||||||
|
|
||||||
|
# TODO Dev purporse
|
||||||
|
COPY conf.ini /app/conf.ini
|
||||||
|
|
||||||
|
ENTRYPOINT ["python3", "/app/notifier.py"]
|
||||||
67
Justfile
Normal file
67
Justfile
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# https://github.com/casey/just
|
||||||
|
|
||||||
|
venv := "./venv"
|
||||||
|
pip := venv / "bin/pip"
|
||||||
|
python := venv / "bin/python"
|
||||||
|
|
||||||
|
last_commit_sha1 := `git rev-parse --short HEAD`
|
||||||
|
remote_image_name := "gitea.gdemontauzan.fr/maxenceg2m/github-release-notifier"
|
||||||
|
remote_build_image := remote_image_name + ":" + last_commit_sha1
|
||||||
|
|
||||||
|
# Run the script
|
||||||
|
run: _ensure_venv_is_ok
|
||||||
|
{{ python }} notifier.py
|
||||||
|
|
||||||
|
# Init python virtual env
|
||||||
|
init:
|
||||||
|
python3 -m venv venv
|
||||||
|
{{ pip }} install --requirement requirements.txt
|
||||||
|
sha256sum requirements.txt > {{ venv }}/requirements.sha
|
||||||
|
|
||||||
|
# Inspiration: https://github.com/behave/behave/blob/afb6b6716cd0f3e028829416475312db804a6aa9/justfile
|
||||||
|
_ensure_venv_is_ok:
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
from subprocess import run
|
||||||
|
from os import path
|
||||||
|
if run("sha256sum -c {{ venv }}/requirements.sha", shell=True).returncode != 0:
|
||||||
|
run("just init", shell=True)
|
||||||
|
|
||||||
|
# Clean workspace - remove venv - and init
|
||||||
|
reinit: hclean init
|
||||||
|
|
||||||
|
# Remove virtual env (venv)
|
||||||
|
hclean:
|
||||||
|
rm -fr venv
|
||||||
|
|
||||||
|
# Run docker compose then show logs
|
||||||
|
dup: dbuild
|
||||||
|
docker compose up -d
|
||||||
|
docker compose logs
|
||||||
|
|
||||||
|
# Build with docker compose
|
||||||
|
dbuild:
|
||||||
|
docker compose build
|
||||||
|
|
||||||
|
# Down docker compose then build
|
||||||
|
drebuild: ddown dbuild
|
||||||
|
|
||||||
|
# Down docker compose
|
||||||
|
ddown:
|
||||||
|
docker compose down
|
||||||
|
|
||||||
|
# Docker build without cache
|
||||||
|
dforce-build:
|
||||||
|
docker compose build --no-cache
|
||||||
|
|
||||||
|
# Push a working images on registry, tagged with commit-sha1
|
||||||
|
dpush: dbuild
|
||||||
|
docker tag github-release-notifier {{ remote_build_image }}
|
||||||
|
docker push {{ remote_build_image }}
|
||||||
|
echo "To push a tagged version, do 'just release <version>'"
|
||||||
|
|
||||||
|
# Use just a number! Without 'v'! Release a version - create a tagged images, push it and create git tag.
|
||||||
|
release version: dbuild
|
||||||
|
docker tag github-release-notifier {{ remote_image_name }}:{{ version }}
|
||||||
|
docker push {{ remote_image_name }}:{{ version }}
|
||||||
|
git tag -a v{{ version }} -m ""
|
||||||
|
git push --tags
|
||||||
@@ -37,6 +37,9 @@ I wrote this script really quickly, certainly faster than this README. So I alre
|
|||||||
* The script sends mail even if no new projets or release has been detected
|
* The script sends mail even if no new projets or release has been detected
|
||||||
* The biggest, you have to edit script to specify absolute path...
|
* The biggest, you have to edit script to specify absolute path...
|
||||||
* A lot of other little problems, like the code that's disgusting and so on.
|
* A lot of other little problems, like the code that's disgusting and so on.
|
||||||
|
|
||||||
|
For who's asking: yes, it's normal that I have put all code in one main function [like a blind gunner.](https://media.giphy.com/media/1yMexL5rkwYhuiVEmZ/giphy.gif). Really quickly I said!
|
||||||
|
|
||||||
But overall, the script works and sends mail!
|
But overall, the script works and sends mail!
|
||||||
|
|
||||||
Hey boy, what is the `pit.db` file?
|
Hey boy, what is the `pit.db` file?
|
||||||
@@ -44,5 +47,3 @@ Hey boy, what is the `pit.db` file?
|
|||||||
Oh, just for fun, and because I love this project, I use [pit by michaeldv](https://github.com/michaeldv/pit) to follow my task etc.
|
Oh, just for fun, and because I love this project, I use [pit by michaeldv](https://github.com/michaeldv/pit) to follow my task etc.
|
||||||
|
|
||||||
It makes me think I should push my python version of this project on occasion when I will take the time to do...
|
It makes me think I should push my python version of this project on occasion when I will take the time to do...
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
conf.ini
4
conf.ini
@@ -1,7 +1,7 @@
|
|||||||
[config]
|
[config]
|
||||||
github_api_url = https://api.github.com/repos/
|
github_api_url = https://api.github.com/repos/
|
||||||
smtp_port = 25
|
smtp_port = 1025
|
||||||
smtp_server = localhost
|
smtp_server = mailpit
|
||||||
sender_email = sender@host.eu
|
sender_email = sender@host.eu
|
||||||
receiver_email = receiver@anotherhost.eu
|
receiver_email = receiver@anotherhost.eu
|
||||||
|
|
||||||
|
|||||||
24
docker-compose.yml
Normal file
24
docker-compose.yml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
notifier:
|
||||||
|
build: .
|
||||||
|
image: github-release-notifier
|
||||||
|
container_name: github-release-notifier
|
||||||
|
volumes:
|
||||||
|
- ./conf.ini:/app/conf.ini
|
||||||
|
|
||||||
|
mailpit:
|
||||||
|
image: axllent/mailpit
|
||||||
|
ports:
|
||||||
|
- "8025:8025"
|
||||||
|
- "1025:1025"
|
||||||
|
|
||||||
|
ofelia:
|
||||||
|
image: mcuadros/ofelia:latest
|
||||||
|
depends_on:
|
||||||
|
- notifier
|
||||||
|
command: daemon --config=/opt/config.ini
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
- ./ofelia.ini:/opt/config.ini
|
||||||
167
notifier.py
167
notifier.py
@@ -1,103 +1,138 @@
|
|||||||
from configparser import ConfigParser
|
import configparser
|
||||||
import json
|
|
||||||
import smtplib
|
|
||||||
from email.mime.text import MIMEText
|
|
||||||
from email.mime.multipart import MIMEMultipart
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import smtplib
|
||||||
|
import sys
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
SMTP_PORT = 0
|
|
||||||
SMTP_SERVER = 'null'
|
class EnvInjection(configparser.Interpolation):
|
||||||
SENDER_EMAIL = 'a@b.c'
|
"""
|
||||||
RECEIVER_EMAIL = 'd@e.f'
|
Derived interpolation to take env variable before file variable.
|
||||||
|
Permit to keep the ini file for local / traditionnal use
|
||||||
|
And use env variable to overload configuration in a Docker usage.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def before_get(self, parser, section, option, value, defaults):
|
||||||
|
file_value = super().before_get(parser, section, option, value, defaults)
|
||||||
|
if section != parser.default_section:
|
||||||
|
return file_value
|
||||||
|
|
||||||
|
env_value = os.getenv(option.upper())
|
||||||
|
return env_value if env_value else file_value
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global SMTP_PORT, SMTP_SERVER, SENDER_EMAIL, RECEIVER_EMAIL
|
script_dir = os.path.dirname(__file__)
|
||||||
parser = ConfigParser()
|
conf_file = os.path.join(script_dir, "conf.ini")
|
||||||
parser.read('conf.ini')
|
template_file = os.path.join(script_dir, "template.html")
|
||||||
|
|
||||||
SMTP_PORT = parser.get('config', 'smtp_port')
|
parser = configparser.ConfigParser(
|
||||||
SMTP_SERVER = parser.get('config', 'smtp_server')
|
default_section="config", interpolation=EnvInjection()
|
||||||
SENDER_EMAIL = parser.get('config', 'sender_email')
|
)
|
||||||
RECEIVER_EMAIL = parser.get('config', 'receiver_email')
|
parser.read(conf_file)
|
||||||
|
default_config = parser["config"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
projects = json.loads(parser.get("projects", "projects"))
|
||||||
|
except json.decoder.JSONDecodeError as jse:
|
||||||
|
print("ERROR: config file is not correctly JSON formatted!", end="\n\t")
|
||||||
|
print(jse)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
projects = json.loads(parser.get('projects', 'projects'))
|
|
||||||
new_releases = []
|
new_releases = []
|
||||||
new_projects = []
|
new_projects = []
|
||||||
|
|
||||||
if not parser.has_section('release'):
|
if not parser.has_section("release"):
|
||||||
parser.add_section('release')
|
parser.add_section("release")
|
||||||
|
|
||||||
for project in projects:
|
for project in projects:
|
||||||
last_release = get_last_release(project)
|
last_release = get_last_release(project)
|
||||||
|
|
||||||
if not parser.has_option('release', project):
|
if not parser.has_option("release", project):
|
||||||
new_projects.append(last_release)
|
new_projects.append(last_release)
|
||||||
else:
|
else:
|
||||||
last_config_tag = parser.get('release', project)
|
last_config_tag = parser.get("release", project)
|
||||||
if last_config_tag != last_release['release_tag']:
|
if last_config_tag != last_release["release_tag"]:
|
||||||
last_release['preview_tag'] = last_config_tag
|
last_release["previous_tag"] = last_config_tag
|
||||||
new_releases.append(last_release)
|
new_releases.append(last_release)
|
||||||
parser.set('release', project, last_release['release_tag'])
|
parser.set("release", project, last_release["release_tag"])
|
||||||
|
|
||||||
|
if not new_releases and not new_projects:
|
||||||
|
print("No new projets or new release detected. Bye!")
|
||||||
|
return
|
||||||
|
|
||||||
content = ""
|
content = ""
|
||||||
|
|
||||||
for new_r in new_releases:
|
for new_r in new_releases:
|
||||||
content += """
|
content += f"""
|
||||||
<li><a href="{}" target="_blank">{}</a>: new release <a href="{}" target="_blank">{}</a> available (old: {}).
|
<li>{get_html_project_link(new_r)}
|
||||||
(published {})</li>
|
: new release
|
||||||
""".format(
|
{get_html_release_link(new_r)}
|
||||||
new_r['release_url'],
|
available (old: {new_r["previous_tag"]}).
|
||||||
new_r['project_name'],
|
(published {convert_date(new_r["published_date"])})</li>"""
|
||||||
new_r['release_url'],
|
|
||||||
new_r['release_tag'],
|
|
||||||
new_r['preview_tag'],
|
|
||||||
convert_date(new_r['published_date']))
|
|
||||||
for new_p in new_projects:
|
for new_p in new_projects:
|
||||||
content += """
|
content += f"""
|
||||||
<li><a href="{}" target="_blank">{}</a> was added to your configuration. Last release: <a href="{}" target="_blank">{}</a>
|
<li>{get_html_project_link(new_p)}
|
||||||
(published {})</li>""".format(
|
was added to your configuration.
|
||||||
new_p['release_url'],
|
Last release:
|
||||||
new_p['project_name'],
|
{get_html_release_link(new_p)}
|
||||||
new_p['release_url'],
|
(published {convert_date(new_p["published_date"])})</li>"""
|
||||||
new_p['release_tag'],
|
|
||||||
convert_date(new_p['published_date']))
|
|
||||||
|
|
||||||
# print(content)
|
with open(template_file, "r", encoding="utf-8") as f_template:
|
||||||
|
|
||||||
with open('template.html', 'r') as f_template:
|
|
||||||
template = f_template.read()
|
template = f_template.read()
|
||||||
|
|
||||||
send_mail(template.replace('{{content}}', content))
|
send_mail(template.replace("{{content}}", content), default_config)
|
||||||
|
|
||||||
with open('conf.ini', 'w') as configfile:
|
with open("conf.ini", "w", encoding="utf-8") as configfile:
|
||||||
parser.write(configfile)
|
parser.write(configfile)
|
||||||
|
|
||||||
|
|
||||||
def get_last_release(project):
|
def get_last_release(project):
|
||||||
url = 'https://api.github.com/repos/{}/releases/latest'.format(project)
|
url = f"https://api.github.com/repos/{project}/releases/latest"
|
||||||
result = requests.get(url)
|
result = requests.get(url, timeout=10)
|
||||||
|
|
||||||
print(project)
|
print(project)
|
||||||
print(url)
|
print(url)
|
||||||
release = result.json()
|
release = result.json()
|
||||||
release_tag = release['tag_name']
|
release_tag = release["tag_name"]
|
||||||
published_date = release['published_at']
|
published_date = release["published_at"]
|
||||||
# body = release['body']
|
# body = release['body']
|
||||||
release_url = release['html_url']
|
release_url = release["html_url"]
|
||||||
|
|
||||||
return {'release_tag': release_tag,
|
return {
|
||||||
'published_date': published_date,
|
"release_tag": release_tag,
|
||||||
# 'body': body,
|
"published_date": published_date,
|
||||||
'project_name': project,
|
# 'body': body,
|
||||||
'release_url': release_url}
|
"project_name": project,
|
||||||
|
"release_url": release_url,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_html_project_link(el):
|
||||||
|
project_url = f'https://github.com/{el["project_name"]}'
|
||||||
|
return f'<a href="{project_url}" target="_blank">{el["project_name"]}</a>'
|
||||||
|
|
||||||
|
|
||||||
|
def get_html_release_link(el):
|
||||||
|
return f'<a href="{el["release_url"]}" target="_blank">{el["release_tag"]}</a>'
|
||||||
|
|
||||||
|
|
||||||
|
def send_mail(content, config):
|
||||||
|
smtp_port = config.get("smtp_port")
|
||||||
|
smtp_server = config.get("smtp_server")
|
||||||
|
sender_email = config.get("sender_email")
|
||||||
|
receiver_email = config.get("receiver_email")
|
||||||
|
|
||||||
def send_mail(content):
|
|
||||||
message = MIMEMultipart("alternative")
|
message = MIMEMultipart("alternative")
|
||||||
message["Subject"] = "New Github releases"
|
message["Subject"] = "New Github releases"
|
||||||
message["From"] = SENDER_EMAIL
|
message["From"] = sender_email
|
||||||
message["To"] = RECEIVER_EMAIL
|
message["To"] = receiver_email
|
||||||
|
|
||||||
# part1 = MIMEText(text, "plain")
|
# part1 = MIMEText(text, "plain")
|
||||||
part2 = MIMEText(content, "html")
|
part2 = MIMEText(content, "html")
|
||||||
@@ -105,11 +140,13 @@ def send_mail(content):
|
|||||||
# message.attach(part1)
|
# message.attach(part1)
|
||||||
message.attach(part2)
|
message.attach(part2)
|
||||||
|
|
||||||
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
||||||
server.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, message.as_string())
|
server.sendmail(sender_email, receiver_email, message.as_string())
|
||||||
|
|
||||||
|
|
||||||
|
def convert_date(date: str, dest_format="%d %b %Y at %H:%M") -> str:
|
||||||
|
return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ").strftime(dest_format)
|
||||||
|
|
||||||
def convert_date(date: str, format='%d %b %Y at %H:%M') -> str:
|
|
||||||
return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ").strftime(format)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
9
ofelia.ini
Normal file
9
ofelia.ini
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[global]
|
||||||
|
smtp-host = mailpit
|
||||||
|
smtp-port = 1025
|
||||||
|
email-to = max@ence.fr
|
||||||
|
email-from = ofelia@container.sh
|
||||||
|
|
||||||
|
[job-run "notifier"]
|
||||||
|
schedule = @every 30s
|
||||||
|
container = github-release-notifier
|
||||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
requests
|
||||||
|
pre-commit
|
||||||
|
black
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user