celery_tasks.py
Python script, ASCII text executable
1import common 2import time 3import os 4import config 5import email_send 6import shutil 7from celery import shared_task 8from app import db, _ 9from smtplib import SMTP 10from celery.utils.log import get_task_logger 11from sqlalchemy.orm import make_transient 12from datetime import datetime 13 14 15@shared_task(ignore_result=False) 16def send_notification(user_notification_id): 17from models import UserNotification 18user_notification = db.session.get(UserNotification, user_notification_id) 19user = user_notification.user 20notification = user_notification.notification 21 22if user.email: 23with (SMTP(config.MAIL_SERVER) as mail): 24if notification.data.get("type") == "welcome": 25message = email_send.make_multipart_message( 26"Welcome, {username}".format(username=user.username), 27config.NOTIFICATION_EMAIL, 28user.email, 29"welcome", 30username=user.username) 31 32mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message) 33 34return 0 # notification sent successfully 35 36 37@shared_task(ignore_result=False) 38def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True): 39from models import Repo, Commit 40server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 41if not os.path.isdir(server_repo_location): 42raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 43 44if base_route == head_route: 45common.git_command(server_repo_location, b"", "checkout", f"{base_branch}") 46if simulate: 47out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 48return_err=True, return_exit=True) 49 50# Undo the merge. 51common.git_command(server_repo_location, b"", "merge", "--abort") 52else: 53out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 54return_err=True, return_exit=True) 55 56return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 57 58# Otherwise, we need to fetch the head repo. 59remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 60 61out, err = b"", b"" 62part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 63out += part_out 64err += part_err 65part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True) 66out += part_out 67err += part_err 68part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 69out += part_out 70err += part_err 71new_commits = common.git_command(server_repo_location, b"", "log", "--oneline", f"{base_branch}..{head_branch}") 72 73if simulate: 74part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 75"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 76else: 77part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 78f"NEW/{head_branch}", return_err=True, return_exit=True) 79 80diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 81 82out += part_out 83err += part_err 84part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 85out += part_out 86err += part_err 87if simulate: 88# Undo the merge. 89common.git_command(server_repo_location, b"", "merge", "--abort") 90else: 91# Copy the commits rows from the head repo to the base repo 92for commit in new_commits: 93commit_data = Commit.query.filter_by(repo_name=head_route, sha=commit.split()) 94 95db.session.expunge(commit_data) 96make_transient(commit_data) 97 98commit_data.repo_name = base_route 99commit_data.identifier = f"{base_route}/{commit_data.sha}" 100commit_data.receive_date = datetime.now() 101db.session.add(commit_data) 102 103return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 104 105 106@shared_task(ignore_result=False) 107def copy_site(route): 108from models import Repo 109repo = db.session.get(Repo, route) 110server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/")) 111subdomain = repo.owner.username 112subpath = repo.name if repo.has_site != 2 else "" 113site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 114# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch. 115branch = repo.site_branch or repo.default_branch 116# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed. 117if os.path.isdir(site_location): 118# Delete the old site. 119shutil.rmtree(site_location) 120 121common.git_command(config.SITE_PATH, b"", "clone", "--depth=1", f"--branch={branch}", os.path.join(os.getcwd(), server_repo_location), os.path.join(subdomain, subpath)) 122 123 124@shared_task(ignore_result=False) 125def delete_site(route): 126from models import Repo 127repo = db.session.get(Repo, route) 128subdomain = repo.owner.username 129subpath = repo.name if repo.has_site != 2 else "." 130site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 131if os.path.isdir(site_location): 132shutil.rmtree(site_location) 133 134# Redo the primary site. 135primary_site = Repo.query.filter_by(owner=repo.owner, has_site=2).first() 136if primary_site: 137copy_site(primary_site.route) 138 139 140@shared_task(ignore_result=False) 141def request_email_change(username, email): 142from models import User, EmailChangeRequest 143user = db.session.get(User, username) 144 145request = EmailChangeRequest(user, email) 146 147db.session.add(request) 148db.session.commit() 149 150message = email_send.make_multipart_message( 151"Email change request for {username}".format(username=username), 152config.NOTIFICATION_EMAIL, 153email, 154"email-change", 155username=username, 156code=request.code, 157new_email=email, 158url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/settings/confirm-email/" + request.code 159) 160