By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 celery_tasks.py

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