celery_tasks.py
Python script, ASCII text executable
1import time 2import os 3import config 4import email_send 5from celery import shared_task 6from app import db 7from misc_utils import * 8from models import * 9from smtplib import SMTP 10from celery.utils.log import get_task_logger 11 12 13@shared_task(ignore_result=False) 14def send_notification(notification_id, users, level): 15notification = db.session.get(Notification, notification_id) 16 17for user in users: 18db.session.add(UserNotification(db.session.get(User, user), notification, level)) 19 20if db.session.get(User, user).email: 21with (SMTP(config.MAIL_SERVER) as mail): 22if notification.data.get("type") == "welcome": 23message = email_send.make_multipart_message( 24f"Welcome, {user}", 25config.NOTIFICATION_EMAIL, 26db.session.get(User, user).email, 27"welcome", 28username=user) 29 30mail.sendmail(config.NOTIFICATION_EMAIL, db.session.get(User, user).email, message) 31db.session.commit() 32 33return 0 # notification sent successfully 34 35 36@shared_task(ignore_result=False) 37def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True): 38server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 39if not os.path.isdir(server_repo_location): 40raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 41 42if base_route == head_route: 43git_command(server_repo_location, b"", "checkout", f"{base_branch}") 44if simulate: 45out, err, merge_exit = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 46return_err=True, return_exit=True) 47 48# Undo the merge. 49git_command(server_repo_location, b"", "merge", "--abort") 50else: 51out, err, merge_exit = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 52return_err=True, return_exit=True) 53 54return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 55 56remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 57 58out, err = b"", b"" 59part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 60out += part_out 61err += part_err 62part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True) 63out += part_out 64err += part_err 65part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 66out += part_out 67err += part_err 68if simulate: 69part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 70"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 71else: 72part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 73f"NEW/{head_branch}", return_err=True, return_exit=True) 74 75diff, diff_exit = git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 76 77out += part_out 78err += part_err 79part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 80out += part_out 81err += part_err 82if simulate: 83# Undo the merge. 84git_command(server_repo_location, b"", "merge", "--abort") 85 86return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 87