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