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.77 kiB
Python script, ASCII text executable
        
            
1
import common
2
import time
3
import os
4
import config
5
import email_send
6
from celery import shared_task
7
from app import db
8
from smtplib import SMTP
9
from celery.utils.log import get_task_logger
10
11
12
@shared_task(ignore_result=False)
13
def send_notification(user_notification_id):
14
from models import UserNotification
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
from models import Repo
37
server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/"))
38
if not os.path.isdir(server_repo_location):
39
raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.")
40
41
if base_route == head_route:
42
common.git_command(server_repo_location, b"", "checkout", f"{base_branch}")
43
if simulate:
44
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
45
return_err=True, return_exit=True)
46
47
# Undo the merge.
48
common.git_command(server_repo_location, b"", "merge", "--abort")
49
else:
50
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
51
return_err=True, return_exit=True)
52
53
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
54
55
remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/"))
56
57
out, err = b"", b""
58
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
59
out += part_out
60
err += part_err
61
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True)
62
out += part_out
63
err += part_err
64
part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
65
out += part_out
66
err += part_err
67
if simulate:
68
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
69
"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True)
70
else:
71
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
72
f"NEW/{head_branch}", return_err=True, return_exit=True)
73
74
diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True)
75
76
out += part_out
77
err += part_err
78
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
79
out += part_out
80
err += part_err
81
if simulate:
82
# Undo the merge.
83
common.git_command(server_repo_location, b"", "merge", "--abort")
84
85
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
86