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.18 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", head_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", "--pretty=format:\"%H\"", f"heads/{base_branch}..NEW/{head_branch}", "--", return_err=True)
77
new_commits = new_commits.decode().splitlines()
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).first()
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
db.session.commit()
111
112
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit, new_commits
113
114
115
@shared_task(ignore_result=False)
116
def copy_site(route):
117
from models import Repo
118
repo = db.session.get(Repo, route)
119
server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/"))
120
subdomain = repo.owner.username
121
subpath = repo.name if repo.has_site != 2 else ""
122
site_location = os.path.join(config.SITE_PATH, subdomain, subpath)
123
# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch.
124
branch = repo.site_branch or repo.default_branch
125
# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed.
126
if os.path.isdir(site_location):
127
# Delete the old site.
128
shutil.rmtree(site_location)
129
130
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))
131
132
133
@shared_task(ignore_result=False)
134
def delete_site(route):
135
from models import Repo
136
repo = db.session.get(Repo, route)
137
subdomain = repo.owner.username
138
subpath = repo.name if repo.has_site != 2 else "."
139
site_location = os.path.join(config.SITE_PATH, subdomain, subpath)
140
if os.path.isdir(site_location):
141
shutil.rmtree(site_location)
142
143
# Redo the primary site.
144
primary_site = Repo.query.filter_by(owner=repo.owner, has_site=2).first()
145
if primary_site:
146
copy_site(primary_site.route)
147
148
149
@shared_task(ignore_result=False)
150
def request_email_change(username, email):
151
from models import User, EmailChangeRequest
152
user = db.session.get(User, username)
153
154
request = EmailChangeRequest(user, email)
155
156
db.session.add(request)
157
db.session.commit()
158
159
message = email_send.make_multipart_message(
160
"Email change request for {username}".format(username=username),
161
config.NOTIFICATION_EMAIL,
162
email,
163
"email-change",
164
username=username,
165
code=request.code,
166
new_email=email,
167
url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/settings/confirm-email/" + request.code
168
)
169