2019-11-22 21:13:07 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
2019-11-23 13:46:45 +01:00
|
|
|
import json
|
2020-05-25 23:49:52 +02:00
|
|
|
import math
|
2019-11-23 13:46:45 +01:00
|
|
|
|
|
|
|
|
2019-11-22 21:13:07 +01:00
|
|
|
from commands.base import BaseCommand
|
2019-11-30 17:47:52 +01:00
|
|
|
from submission import Submission
|
2019-11-22 21:13:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Comments(BaseCommand):
|
2019-11-23 13:46:45 +01:00
|
|
|
width = 5
|
|
|
|
|
2019-11-22 21:13:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def sort_comments(comments):
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
for comment in comments:
|
|
|
|
author = comment["author"]["username"], comment["author"]["name"]
|
|
|
|
if author not in result:
|
2020-05-26 14:17:11 +02:00
|
|
|
result[author] = dict()
|
2019-11-22 21:13:07 +01:00
|
|
|
|
2020-06-08 17:39:36 +02:00
|
|
|
file_path = (
|
|
|
|
comment["position"]["new_path"] if "position" in comment else None
|
|
|
|
)
|
2020-05-26 14:17:11 +02:00
|
|
|
if file_path not in result[author]:
|
|
|
|
result[author][file_path] = list()
|
|
|
|
|
|
|
|
result[author][file_path].append(comment)
|
2019-11-22 21:13:07 +01:00
|
|
|
|
2020-05-25 23:49:52 +02:00
|
|
|
# sort by lines
|
|
|
|
for author in result:
|
2020-05-26 14:17:11 +02:00
|
|
|
for file_path in result[author]:
|
2020-06-08 17:39:36 +02:00
|
|
|
result[author][file_path].sort(
|
|
|
|
key=lambda comment: comment["position"]["new_line"]
|
|
|
|
if "position" in comment
|
|
|
|
else math.inf
|
|
|
|
)
|
2020-05-25 23:49:52 +02:00
|
|
|
|
2019-11-22 21:13:07 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_comments(comments):
|
|
|
|
comments = Comments.sort_comments(comments)
|
|
|
|
|
|
|
|
for author in comments:
|
|
|
|
login, name = author
|
2019-11-30 18:08:26 +01:00
|
|
|
header = f"***** {name} ({login}) *****".center(40, "*").center(80)
|
|
|
|
print(header)
|
2019-11-22 21:13:07 +01:00
|
|
|
|
2020-05-26 14:17:11 +02:00
|
|
|
for file_path in comments[author]:
|
|
|
|
if file_path is not None:
|
2020-06-08 18:05:05 +02:00
|
|
|
filename = file_path.split("/")[-1]
|
2020-06-08 17:58:18 +02:00
|
|
|
print(f"\n`{filename}`\n{'-'*(len(filename) + 2)}")
|
2020-05-26 14:17:11 +02:00
|
|
|
for comment in comments[author][file_path]:
|
|
|
|
if comment["system"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if comment["type"] == "DiffNote":
|
|
|
|
body = comment["body"].replace(
|
2020-06-08 18:05:05 +02:00
|
|
|
"\n",
|
|
|
|
"\n"
|
|
|
|
+ " " * (len(str(comment["position"]["new_line"])) + 5),
|
2020-05-26 14:17:11 +02:00
|
|
|
)
|
2020-06-08 17:58:18 +02:00
|
|
|
|
2020-06-08 18:05:05 +02:00
|
|
|
print(f"""- L{comment["position"]["new_line"]}: {body}""")
|
2020-05-26 14:17:11 +02:00
|
|
|
else:
|
|
|
|
print(f"""[{comment["created_at"]}]\n{comment["body"]}""")
|
2019-11-30 18:08:26 +01:00
|
|
|
print(header)
|
2019-11-22 21:13:07 +01:00
|
|
|
|
2019-11-30 17:47:52 +01:00
|
|
|
def exec(self, submission: Submission) -> None:
|
2019-12-12 12:15:52 +01:00
|
|
|
mrs = self.gitlab.get_mrs_for_branch(submission.branch)
|
2019-11-22 21:13:07 +01:00
|
|
|
if not mrs:
|
|
|
|
return
|
|
|
|
|
|
|
|
iid = mrs[0]["iid"]
|
2019-12-12 12:15:52 +01:00
|
|
|
comments = self.gitlab.get_comments(iid)
|
2019-11-22 21:13:07 +01:00
|
|
|
|
2019-11-30 18:08:26 +01:00
|
|
|
branch = f"### {submission.branch.center(20)} ###".upper().center(80, "#")
|
|
|
|
|
|
|
|
print(branch)
|
2019-11-22 21:13:07 +01:00
|
|
|
self.print_comments(comments)
|
2019-11-30 18:08:26 +01:00
|
|
|
print(branch)
|