roundabout,
created on Friday, 26 April 2024, 12:49:49 (1714135789),
received on Wednesday, 31 July 2024, 06:54:46 (1722408886)
Author identity: vlad <vlad.muntoiu@gmail.com>
2050f295d49cf8abcfa7bafa72e366ff79b2f08c
app.py
@@ -25,6 +25,7 @@ from flask_migrate import Migrate
from PIL import Image
from flask_httpauth import HTTPBasicAuth
import config
from common import git_command
from flask_babel import Babel, gettext, ngettext, force_locale
_ = gettext
@@ -1418,7 +1419,9 @@ def repository_prs_merge_stage_two(username, repository, id):
@app.route("/task/<task_id>")
def task_monitor(task_id):
task_result = worker.AsyncResult(task_id)
print(task_result.status)
if task_result.status == "FAILURE":
app.logger.error(f"Task {task_id} failed")
return flask.render_template("task-monitor.html", result=task_result), 500
return flask.render_template("task-monitor.html", result=task_result)
celery_tasks.py
@@ -1,17 +1,17 @@
import common
import time
import os
import config
import email_send
from celery import shared_task
from app import db
from misc_utils import *
from models import *
from smtplib import SMTP
from celery.utils.log import get_task_logger
@shared_task(ignore_result=False)
def send_notification(user_notification_id):
from models import UserNotification
user_notification = db.session.get(UserNotification, user_notification_id)
user = user_notification.user
notification = user_notification.notification
@@ -33,52 +33,53 @@ def send_notification(user_notification_id):
@shared_task(ignore_result=False)
def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True):
from models import Repo
server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/"))
if not os.path.isdir(server_repo_location):
raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.")
if base_route == head_route:
git_command(server_repo_location, b"", "checkout", f"{base_branch}")
common.git_command(server_repo_location, b"", "checkout", f"{base_branch}")
if simulate:
out, err, merge_exit = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
return_err=True, return_exit=True)
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
return_err=True, return_exit=True)
# Undo the merge.
git_command(server_repo_location, b"", "merge", "--abort")
common.git_command(server_repo_location, b"", "merge", "--abort")
else:
out, err, merge_exit = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
return_err=True, return_exit=True)
out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
return_err=True, return_exit=True)
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/"))
out, err = b"", b""
part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
out += part_out
err += part_err
part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True)
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True)
out += part_out
err += part_err
part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
out += part_out
err += part_err
if simulate:
part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True)
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True)
else:
part_out, part_err, merge_exit = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
f"NEW/{head_branch}", return_err=True, return_exit=True)
part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
f"NEW/{head_branch}", return_err=True, return_exit=True)
diff, diff_exit = git_command(server_repo_location, b"", "diff", "--check", return_exit=True)
diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True)
out += part_out
err += part_err
part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
out += part_out
err += part_err
if simulate:
# Undo the merge.
git_command(server_repo_location, b"", "merge", "--abort")
common.git_command(server_repo_location, b"", "merge", "--abort")
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit
common.py
@@ -0,0 +1,29 @@
import os
import subprocess
def git_command(repo, data, *args, return_err=False, return_exit=False):
if not os.path.isdir(repo):
raise FileNotFoundError(f"Repo {repo} not found")
env = os.environ.copy()
command = ["git", *args]
proc = subprocess.Popen(" ".join(command), cwd=repo, env=env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
if data:
proc.stdin.write(data)
out, err = proc.communicate()
exit_code = proc.returncode
result = (out,)
if return_err:
result = result + (err,)
if return_exit:
result = result + (exit_code,)
return result[0] if len(result) == 1 else result
git_http.py
@@ -3,6 +3,7 @@ import uuid
from models import *
from app import app, db, bcrypt
from misc_utils import *
from common import git_command
import os
import shutil
import config
misc_utils.py
@@ -1,35 +1,14 @@
from common import *
__all__ = ["git_command", "only_chars", "get_permission_level", "get_visibility", "get_favourite", "human_size",
"guess_mime", "convert_to_html", "js_to_bool",]
import subprocess
import os
import magic
from models import *
def git_command(repo, data, *args, return_err=False, return_exit=False):
if not os.path.isdir(repo):
raise FileNotFoundError(f"Repo {repo} not found")
env = os.environ.copy()
command = ["git", *args]
proc = subprocess.Popen(" ".join(command), cwd=repo, env=env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
if data:
proc.stdin.write(data)
out, err = proc.communicate()
exit_code = proc.returncode
result = (out,)
if return_err:
result = result + (err,)
if return_exit:
result = result + (exit_code,)
return result[0] if len(result) == 1 else result
def only_chars(string, chars):
chars = set(chars)
all_chars = set(string)
models.py
@@ -1,17 +1,3 @@
import subprocess
from app import app, db, bcrypt
import git
from datetime import datetime
from enum import Enum
from PIL import Image
from cairosvg import svg2png
import os
import config
import cairosvg
import random
import celery_tasks
__all__ = [
"RepoAccess",
"RepoFavourite",
@@ -26,6 +12,19 @@ __all__ = [
"PullRequest",
]
import subprocess
from app import app, db, bcrypt
import git
from datetime import datetime
from enum import Enum
from PIL import Image
from cairosvg import svg2png
import os
import config
import cairosvg
import random
import celery_tasks
with (app.app_context()):
class RepoAccess(db.Model):
id = db.Column(db.Integer, primary_key=True)