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, pr_id, simulate=True): 76from models import Repo, Commit, PullRequest 77server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 78pull_request = db.session.get(PullRequest, pr_id) 79if not os.path.isdir(server_repo_location): 80raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 81 82if base_route == head_route: 83common.git_command(server_repo_location, b"", "checkout", f"{base_branch}") 84if simulate: 85out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 86return_err=True, return_exit=True) 87 88# Undo the merge. 89common.git_command(server_repo_location, b"", "merge", "--abort") 90else: 91out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 92return_err=True, return_exit=True) 93 94pull_request.state = 1 95 96for resolves in pull_request.resolves: 97resolves.post.state = 0 98 99db.session.commit() 100 101new_commits = common.git_command(server_repo_location, b"", "log", "--oneline", f"heads/{base_branch}..heads/{head_branch}") 102 103return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits 104 105# Otherwise, we need to fetch the head repo. 106remote_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("/")) 107 108out, err = b"", b"" 109part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 110out += part_out 111err += part_err 112part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True) 113out += part_out 114err += part_err 115part_out, part_err = common.git_command(server_repo_location, b"", "fetch", "NEW", f"{head_branch}", return_err=True) 116out += part_out 117err += part_err 118part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 119out += part_out 120err += part_err 121new_commits, part_err = common.git_command(server_repo_location, b"", "log", "--pretty=format:\"%H\"", f"heads/{base_branch}..NEW/{head_branch}", "--", return_err=True) 122new_commits = new_commits.decode().splitlines() 123err += part_err 124 125if simulate: 126part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 127"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 128else: 129part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 130f"NEW/{head_branch}", return_err=True, return_exit=True) 131 132diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 133 134out += part_out 135err += part_err 136part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 137out += part_out 138err += part_err 139if simulate: 140# Undo the merge. 141common.git_command(server_repo_location, b"", "merge", "--abort") 142else: 143# Copy the commits rows from the head repo to the base repo 144for commit in new_commits: 145commit_data = Commit.query.filter_by(repo_name=head_route, sha=commit).first() 146 147db.session.expunge(commit_data) 148make_transient(commit_data) 149 150commit_data.repo_name = base_route 151commit_data.identifier = f"{base_route}/{commit_data.sha}" 152commit_data.receive_date = datetime.now() 153db.session.add(commit_data) 154 155# Consider the PR merged. 156pull_request.state = 1 157 158for resolves in pull_request.resolves: 159resolves.post.state = 0 160 161db.session.commit() 162 163return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits 164 165 166@shared_task(ignore_result=False) 167def copy_site(route): 168from models import Repo 169repo = db.session.get(Repo, route) 170server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/")) 171subdomain = repo.owner.username 172subpath = repo.name if repo.has_site != 2 else "" 173site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 174# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch. 175branch = repo.site_branch or repo.default_branch 176# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed. 177if os.path.isdir(site_location): 178# Delete the old site. 179shutil.rmtree(site_location) 180 181common.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)) 182 183 184@shared_task(ignore_result=False) 185def delete_site(route): 186from models import Repo 187repo = db.session.get(Repo, route) 188subdomain = repo.owner.username 189subpath = repo.name if repo.has_site != 2 else "." 190site_location = os.path.join(config.SITE_PATH, subdomain, subpath) 191if os.path.isdir(site_location): 192shutil.rmtree(site_location) 193 194# Redo the primary site. 195primary_site = Repo.query.filter_by(owner=repo.owner, has_site=2).first() 196if primary_site: 197copy_site(primary_site.route) 198 199 200@shared_task(ignore_result=False) 201def request_email_change(username, email): 202from models import User, EmailChangeRequest 203user = db.session.get(User, username) 204 205request = EmailChangeRequest(user, email) 206 207db.session.add(request) 208db.session.commit() 209 210message = email_send.make_multipart_message( 211"Email change request for {username}".format(username=username), 212config.NOTIFICATION_EMAIL, 213email, 214"email-change", 215username=username, 216code=request.code, 217new_email=email, 218url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/settings/confirm-email/" + request.code 219) 220 221with (SMTP(config.MAIL_SERVER) as mail): 222mail.sendmail(config.NOTIFICATION_EMAIL, email, message) 223