diff --git a/README-generator/README.md b/README-generator/README.md new file mode 100644 index 0000000..5da33ff --- /dev/null +++ b/README-generator/README.md @@ -0,0 +1,38 @@ +# Auto-README generation + +### Initial install + +``` +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +``` + +### Use on a single app + +``` +source venv/bin/activate +./make_readme.py /path/to/app +``` + +Then the README.md in the app folder will be updated + +### Launch webhook service for auto update + +Configure the webhook on github + +Also need to allow the bot to push on all repos + +Configure nginx to reverse proxy on port 80123 (or whichever port you set in the systemd config) + +```bash +echo "github_webhook_secret" > github_webhook_secret +echo "the_bot_login" > login +echo "the_bot_token" > token +``` + +Add the webhook.service to systemd config, then start it: + +```bash +systemctl start the_webhook_service +``` diff --git a/README-generator/__init__.py b/README-generator/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/README-generator/nginx.conf b/README-generator/nginx.conf new file mode 100644 index 0000000..4c1374a --- /dev/null +++ b/README-generator/nginx.conf @@ -0,0 +1,17 @@ +location / { + + # Force usage of https + if ($scheme = http) { + rewrite ^ https://$server_name$request_uri? permanent; + } + + client_max_body_size 100M; + + proxy_pass http://127.0.0.1:80123; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + + # preserve client IP + proxy_set_header X-Forwarded-For $remote_addr; +} diff --git a/README-generator/requirements.txt b/README-generator/requirements.txt index 908d867..8f402cb 100644 --- a/README-generator/requirements.txt +++ b/README-generator/requirements.txt @@ -1,2 +1,3 @@ argparse jinja2 +github-webhook==1.0.4 diff --git a/README-generator/webhook.py b/README-generator/webhook.py new file mode 100755 index 0000000..bfdcbe0 --- /dev/null +++ b/README-generator/webhook.py @@ -0,0 +1,53 @@ +import subprocess +import os + +from github_webhook import Webhook +from flask import Flask +from make_readme import generate_READMEs + +app = Flask(__name__) # Standard Flask app +webhook = Webhook(app) # Defines '/postreceive' endpoint + +webhook.secret = open("github_webhook_secret", "r").read().strip() + +my_env = os.environ.copy() +my_env["GIT_TERMINAL_PROMPT"] = "0" + +login = open("login").read().strip() +token = open("token").read().strip() + +def git(cmd, in_folder=None): + + if not isinstance(cmd, list): + cmd = cmd.split() + if in_folder: + cmd = ["-C", in_folder] + cmd + cmd = ["git"] + cmd + return subprocess.check_output(cmd, env=my_env).strip().decode("utf-8") + + +@app.route("/") +def main_route(): + return "You aren't supposed to go on this page using a browser, it's for webhooks push instead." + + +@webhook.hook() +def on_push(data): + + repository = data["repository"]["full_name"] + branch = data["ref"].split("/", 2)[2] + + folder = subprocess.check_output(["mktemp", "-d"]) + git(f"clone https://{login}:{token}@github.com/{repository} --single-branch --branch {branch} {folder}") + generate_READMEs(folder) + + diff_not_empty = bool(subprocess.check_output(f"cd {folder} && git diff HEAD --compact-summary", shell=True).strip().decode("utf-8")) + if not diff_not_empty: + return + + git(["commit", "-a", "-m", "Auto-update README", "--author='Yunohost-Bot <>'"], in_folder=folder) + git(f"push fork origin {branch} --quiet", in_folder=folder) + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=80123) diff --git a/README-generator/webhook.service b/README-generator/webhook.service new file mode 100644 index 0000000..08296b8 --- /dev/null +++ b/README-generator/webhook.service @@ -0,0 +1,16 @@ +[Unit] +Description=Auto-README webhook gunicorn daemon +After=network.target + +[Service] +PIDFile=/run/gunicorn/autoreadme_webhook-pid +User=autoreadme_webhook +Group=autoreadme_webhook +WorkingDirectory=__PATH_TO_README_GENERATOR__ +ExecStart=__PATH_TO_README_GENERATOR__/venv/bin/gunicorn -w 4 -b 127.0.0.1:80123 webhook:app +ExecReload=/bin/kill -s HUP $MAINPID +ExecStop=/bin/kill -s TERM $MAINPID +PrivateTmp=true + +[Install] +WantedBy=multi-user.target