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.76 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(notification_id, users, level):
15
notification = db.session.get(Notification, notification_id)
16
17
for user in users:
18
db.session.add(UserNotification(db.session.get(User, user), notification, level))
19
20
if db.session.get(User, user).email:
21
with (SMTP(config.MAIL_SERVER) as mail):
22
if notification.data.get("type") == "welcome":
23
message = email_send.make_multipart_message(
24
f"Welcome, {user}",
25
config.NOTIFICATION_EMAIL,
26
db.session.get(User, user).email,
27
"welcome",
28
username=user)
29
30
mail.sendmail(config.NOTIFICATION_EMAIL, db.session.get(User, user).email, message)
31
db.session.commit()
32
33
return 0 # notification sent successfully
34
35
36
@shared_task(ignore_result=False)
37
def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True):
38
server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/"))
39
if not os.path.isdir(server_repo_location):
40
raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.")
41
42
if base_route == head_route:
43
git_command(server_repo_location, b"", "checkout", f"{base_branch}")
44
if simulate:
45
out, err, merge_exit = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
46
return_err=True, return_exit=True)
47
48
# Undo the merge.
49
git_command(server_repo_location, b"", "merge", "--abort")
50
else:
51
out, err, merge_exit = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
52
return_err=True, return_exit=True)
53
54
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
55
56
remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/"))
57
58
out, err = b"", b""
59
part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
60
out += part_out
61
err += part_err
62
part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True)
63
out += part_out
64
err += part_err
65
part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
66
out += part_out
67
err += part_err
68
if simulate:
69
part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
70
"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True)
71
else:
72
part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
73
f"NEW/{head_branch}", return_err=True, return_exit=True)
74
75
diff, diff_exit = git_command(server_repo_location, b"", "diff", "--check", return_exit=True)
76
77
out += part_out
78
err += part_err
79
part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
80
out += part_out
81
err += part_err
82
if simulate:
83
# Undo the merge.
84
git_command(server_repo_location, b"", "merge", "--abort")
85
86
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
87