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