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(user_notification_id): 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): 36server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 37if not os.path.isdir(server_repo_location): 38raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 39 40if base_route == head_route: 41git_command(server_repo_location, b"", "checkout", f"{base_branch}") 42if simulate: 43out, err, merge_exit = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 44return_err=True, return_exit=True) 45 46# Undo the merge. 47git_command(server_repo_location, b"", "merge", "--abort") 48else: 49out, err, merge_exit = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 50return_err=True, return_exit=True) 51 52return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 53 54remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 55 56out, err = b"", b"" 57part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 58out += part_out 59err += part_err 60part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True) 61out += part_out 62err += part_err 63part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 64out += part_out 65err += part_err 66if simulate: 67part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 68"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 69else: 70part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 71f"NEW/{head_branch}", return_err=True, return_exit=True) 72 73diff, diff_exit = git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 74 75out += part_out 76err += part_err 77part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 78out += part_out 79err += part_err 80if simulate: 81# Undo the merge. 82git_command(server_repo_location, b"", "merge", "--abort") 83 84return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 85