This repository has been archived on 2023-07-17. You can view files and clone it, but cannot push or open issues or pull requests.
pushee/commands/comments.py
2020-05-26 14:17:11 +02:00

78 lines
2.4 KiB
Python

#!/usr/bin/env python3
import json
import math
from commands.base import BaseCommand
from submission import Submission
class Comments(BaseCommand):
width = 5
@staticmethod
def sort_comments(comments):
result = {}
for comment in comments:
author = comment["author"]["username"], comment["author"]["name"]
if author not in result:
result[author] = dict()
file_path = comment["position"]["new_path"] if "position" in comment else None
if file_path not in result[author]:
result[author][file_path] = list()
result[author][file_path].append(comment)
# sort by lines
for author in result:
for file_path in result[author]:
result[author][file_path].sort(key=lambda comment:
comment["position"]["new_line"] if "position" in comment
else math.inf)
return result
@staticmethod
def print_comments(comments):
comments = Comments.sort_comments(comments)
for author in comments:
login, name = author
header = f"***** {name} ({login}) *****".center(40, "*").center(80)
print(header)
for file_path in comments[author]:
if file_path is not None:
print(f"# `{file_path.split('/')[-1]}`")
for comment in comments[author][file_path]:
if comment["system"]:
continue
if comment["type"] == "DiffNote":
body = comment["body"].replace(
"\n", "\n" + " " * (Comments.width + 2)
)
print(
f"""{f'L{comment["position"]["new_line"]}':>{Comments.width}}: {body}"""
)
else:
print(f"""[{comment["created_at"]}]\n{comment["body"]}""")
print(header)
def exec(self, submission: Submission) -> None:
mrs = self.gitlab.get_mrs_for_branch(submission.branch)
if not mrs:
return
iid = mrs[0]["iid"]
comments = self.gitlab.get_comments(iid)
branch = f"### {submission.branch.center(20)} ###".upper().center(80, "#")
print(branch)
self.print_comments(comments)
print(branch)