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, Commit, Post, PullRequest 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): 24match notification.data.get("type"): 25case "welcome": 26message = email_send.make_multipart_message( 27f"[system] Welcome, {user.username}", 28config.NOTIFICATION_EMAIL, 29user.email, 30"welcome", 31username=user.username 32) 33case "commit": 34commit = db.session.get(Commit, notification.data.get("commit")) 35line_separator = "\n\n" # hack so it works in older Pythons 36newline = "\n" 37message = email_send.make_multipart_message( 38f"[commit in {notification.data.get('repo')}] {commit.message.partition(line_separator)[0].replace(newline, ' ')}", 39config.NOTIFICATION_EMAIL, 40user.email, 41"commit", 42username=user.username, 43commit=commit, 44url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/repo/" + notification.data.get("repo") + "/commit/" + notification.data.get("commit") 45) 46case "post": 47post = db.session.get(Post, notification.data.get("post")) 48message = email_send.make_multipart_message( 49f"[post in {notification.data.get('repo')}] {post.subject}", 50config.NOTIFICATION_EMAIL, 51user.email, 52"forum", 53username=user.username, 54post=post, 55url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/repo/" + notification.data.get("repo") + "/post/" + notification.data.get("post") 56) 57case "pr": 58pr = db.session.get(PullRequest, notification.data.get("pr")) 59message = email_send.make_multipart_message( 60f"[PR in {notification.data.get('repo')}] {pr.head_route}:{pr.head_branch} -> {pr.base_route}:{pr.base_branch}", 61config.NOTIFICATION_EMAIL, 62user.email, 63"pr", 64username=user.username, 65pr=pr, 66url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + notification.data.get("base") + "/prs/" 67) 68 69mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message) 70 71return 0 # notification sent successfully 72 73 74@shared_task(ignore_result=False) 75def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True): 76from models import Repo, Commit 77server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 78if not os.path.isdir(server_repo_location): 79raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 80 81if base_route == head_route: 82common.git_command(server_repo_location, b"", "checkout", f"{base_branch}") 83if simulate: 84out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 85return_err=True, return_exit=True) 86 87# Undo the merge. 88common.git_command(server_repo_location, b"", "merge", "--abort") 89else: 90out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 91return_err=True, return_exit=True) 92 93new_commits = common.git_command(server_repo_location, b"", "log", "--oneline", f"heads/{base_branch}..heads/{head_branch}") 94 95return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits 96 97# Otherwise, we need to fetch the head repo. 98remote_url = "https://" if config.suggest_https else "http://" + os.path.join(config.BASE_DOMAIN + f":{config.port}" if config.port not in {80, 443} else "", "git", head_route.lstrip("/")) 99 100out, err = b"", b"" 101part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 102out += part_out 103err += part_err 104part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True) 105out += part_out 106err += part_err 107part_out, part_err = common.git_command(server_repo_location, b"", "fetch", "NEW", f"{head_branch}", return_err=True) 108out += part_out 109err += part_err 110part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 111out += part_out 112err += part_err 113new_commits, part_err = common.git_command(server_repo_location, b"", "log", "--pretty=format:\"%H\"", f"heads/{base_branch}..NEW/{head_branch}", "--", return_err=True) 114new_commits = new_commits.decode().splitlines() 115err += part_err 116 117if simulate: 118part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 119"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 120else: 121part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 122f"NEW/{head_branch}", return_err=True, return_exit=True) 123 124diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 125 126out += part_out 127err += part_err 128part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 129out += part_out 130err += part_err 131if simulate: 132# Undo the merge. 133common.git_command(server_repo_location, b"", "merge", "--abort") 134else: 135# Copy the commits rows from the head repo to the base repo 136for commit in new_commits: 137commit_data = Commit.query.filter_by(repo_name=head_route, sha=commit).first() 138 139db.session.expunge(commit_data) 140make_transient(commit_data) 141 142commit_data.repo_name = base_route 143commit_data.identifier = f"{base_route}/{commit_data.sha}" 144commit_data.receive_date = datetime.now() 145db.session.add(commit_data) 146 147db.session.commit() 148 149return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits 150 151 152@shared_task(ignore_result=False) 153def copy_site(route): 154from models import Repo 155repo = db.session.get(Repo, route) 156server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/")) 157subdomain = repo.owner.username 158subpath = repo.name if repo.has_site != 2 else "" 159site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 160# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch. 161branch = repo.site_branch or repo.default_branch 162# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed. 163if os.path.isdir(site_location): 164# Delete the old site. 165shutil.rmtree(site_location) 166 167common.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)) 168 169 170@shared_task(ignore_result=False) 171def delete_site(route): 172from models import Repo 173repo = db.session.get(Repo, route) 174subdomain = repo.owner.username 175subpath = repo.name if repo.has_site != 2 else "." 176site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 177if os.path.isdir(site_location): 178shutil.rmtree(site_location) 179 180# Redo the primary site. 181primary_site = Repo.query.filter_by(owner=repo.owner, has_site=2).first() 182if primary_site: 183copy_site(primary_site.route) 184 185 186@shared_task(ignore_result=False) 187def request_email_change(username, email): 188from models import User, EmailChangeRequest 189user = db.session.get(User, username) 190 191request = EmailChangeRequest(user, email) 192 193db.session.add(request) 194db.session.commit() 195 196message = email_send.make_multipart_message( 197"Email change request for {username}".format(username=username), 198config.NOTIFICATION_EMAIL, 199email, 200"email-change", 201username=username, 202code=request.code, 203new_email=email, 204url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/settings/confirm-email/" + request.code 205) 206 207with (SMTP(config.MAIL_SERVER) as mail): 208mail.sendmail(config.NOTIFICATION_EMAIL, email, message) 209