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 • 7.11 kiB
Python script, ASCII text executable
        
            
1
import common
2
import time
3
import os
4
import config
5
import email_send
6
import shutil
7
from celery import shared_task
8
from app import db, _
9
from smtplib import SMTP
10
from celery.utils.log import get_task_logger
11
from sqlalchemy.orm import make_transient
12
from datetime import datetime
13
14
15
@shared_task(ignore_result=False)
16
def send_notification(user_notification_id):
17
from models import UserNotification
18
user_notification = db.session.get(UserNotification, user_notification_id)
19
user = user_notification.user
20
notification = user_notification.notification
21
22
if user.email:
23
with (SMTP(config.MAIL_SERVER) as mail):
24
if notification.data.get("type") == "welcome":
25
message = email_send.make_multipart_message(
26
"Welcome, {username}".format(username=user.username),
27
config.NOTIFICATION_EMAIL,
28
user.email,
29
"welcome",
30
username=user.username)
31
32
mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message)
33
34
return 0 # notification sent successfully
35
36
37
@shared_task(ignore_result=False)
38
def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True):
39
from models import Repo, Commit
40
server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/"))
41
if not os.path.isdir(server_repo_location):
42
raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.")
43
44
if base_route == head_route:
45
common.git_command(server_repo_location, b"", "checkout", f"{base_branch}")
46
if simulate:
47
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
48
return_err=True, return_exit=True)
49
50
# Undo the merge.
51
common.git_command(server_repo_location, b"", "merge", "--abort")
52
else:
53
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
54
return_err=True, return_exit=True)
55
56
new_commits = common.git_command(server_repo_location, b"", "log", "--oneline", f"heads/{base_branch}..heads/{head_branch}")
57
58
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits
59
60
# Otherwise, we need to fetch the head repo.
61
remote_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", base_route.lstrip("/"))
62
63
out, err = b"", b""
64
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
65
out += part_out
66
err += part_err
67
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True)
68
out += part_out
69
err += part_err
70
part_out, part_err = common.git_command(server_repo_location, b"", "fetch", "NEW", f"{head_branch}", return_err=True)
71
out += part_out
72
err += part_err
73
part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
74
out += part_out
75
err += part_err
76
new_commits, part_err = common.git_command(server_repo_location, b"", "log", "--oneline", f"heads/{base_branch}..NEW/{head_branch}", "--", return_err=True)
77
print(new_commits)
78
err += part_err
79
80
if simulate:
81
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
82
"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True)
83
else:
84
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
85
f"NEW/{head_branch}", return_err=True, return_exit=True)
86
87
diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True)
88
89
out += part_out
90
err += part_err
91
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
92
out += part_out
93
err += part_err
94
if simulate:
95
# Undo the merge.
96
common.git_command(server_repo_location, b"", "merge", "--abort")
97
else:
98
# Copy the commits rows from the head repo to the base repo
99
for commit in new_commits:
100
commit_data = Commit.query.filter_by(repo_name=head_route, sha=commit.split())
101
102
db.session.expunge(commit_data)
103
make_transient(commit_data)
104
105
commit_data.repo_name = base_route
106
commit_data.identifier = f"{base_route}/{commit_data.sha}"
107
commit_data.receive_date = datetime.now()
108
db.session.add(commit_data)
109
110
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits
111
112
113
@shared_task(ignore_result=False)
114
def copy_site(route):
115
from models import Repo
116
repo = db.session.get(Repo, route)
117
server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/"))
118
subdomain = repo.owner.username
119
subpath = repo.name if repo.has_site != 2 else ""
120
site_location = os.path.join(config.SITE_PATH, subdomain, subpath)
121
# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch.
122
branch = repo.site_branch or repo.default_branch
123
# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed.
124
if os.path.isdir(site_location):
125
# Delete the old site.
126
shutil.rmtree(site_location)
127
128
common.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))
129
130
131
@shared_task(ignore_result=False)
132
def delete_site(route):
133
from models import Repo
134
repo = db.session.get(Repo, route)
135
subdomain = repo.owner.username
136
subpath = repo.name if repo.has_site != 2 else "."
137
site_location = os.path.join(config.SITE_PATH, subdomain, subpath)
138
if os.path.isdir(site_location):
139
shutil.rmtree(site_location)
140
141
# Redo the primary site.
142
primary_site = Repo.query.filter_by(owner=repo.owner, has_site=2).first()
143
if primary_site:
144
copy_site(primary_site.route)
145
146
147
@shared_task(ignore_result=False)
148
def request_email_change(username, email):
149
from models import User, EmailChangeRequest
150
user = db.session.get(User, username)
151
152
request = EmailChangeRequest(user, email)
153
154
db.session.add(request)
155
db.session.commit()
156
157
message = email_send.make_multipart_message(
158
"Email change request for {username}".format(username=username),
159
config.NOTIFICATION_EMAIL,
160
email,
161
"email-change",
162
username=username,
163
code=request.code,
164
new_email=email,
165
url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/settings/confirm-email/" + request.code
166
)
167