Split commands and update printing of comments

This commit is contained in:
Matej Focko 2019-11-22 21:13:07 +01:00
parent 1b527a7a6c
commit 6c53c3cdea
9 changed files with 201 additions and 140 deletions

View file

@ -1,114 +0,0 @@
#!/usr/bin/env python3
import re
from constants import HOMEWORK, SUFFIX
from git import add_files, commit, push
from gitlab import post_mr, get_mrs_for_branch, set_assignees, merge_mr, get_comments
from utils import run_cmd, get_branch
class MergeRequests:
@staticmethod
def get_files(submission: str) -> None:
relative_path = f"/home/kontr/kontr/_tmp_/ib111/{HOMEWORK}/{submission}"
files = f"{{master-naostro/LoadTest/{HOMEWORK}.py,teacher_email}}"
if run_cmd("rsync", "-avzP", f"aisa:{relative_path}/{files}", "./")[0] != 0:
exit(1)
@staticmethod
def call_flake() -> None:
process = run_cmd("flake8", "--exit-zero", f"{HOMEWORK}.py")[1]
with open("flake.log", "w") as f:
print(process.stdout, file=f)
@staticmethod
def get_mail() -> None:
with open("teacher_email") as file:
contents = file.read()
match = re.search(r"<pre>((.*\s+)+)<\/pre>", contents)
return match.group(1) if match else contents
def __call__(self, login: str, submission: str) -> None:
branch = get_branch(login)
self.get_files(submission)
self.call_flake()
add_files(f"{HOMEWORK}.py", "flake.log")
commit(f'"Add sources and flake log ({HOMEWORK}{SUFFIX} {login})"')
push("origin", branch)
post_mr(
source_branch=branch,
target_branch="master",
title=f"[{HOMEWORK}{SUFFIX}] {login}",
description=f"```\n{self.get_mail()}\n```",
labels=HOMEWORK,
remove_source_branch="true",
assignee_ids=["1772"],
)
class UpdateAssignees:
def __call__(self, login: str, submission: str) -> None:
branch = get_branch(login)
iid = get_mrs_for_branch(branch)[0]["iid"]
print(f"{login} @ {branch} : {iid}")
set_assignees(iid, ["39", "1772"])
class Merge:
def __call__(self, login: str, submission: str):
branch = get_branch(login)
iid = get_mrs_for_branch(branch)[0]["iid"]
merge_mr(iid)
class Test:
def __call__(self, login: str, submission: str):
print(f"{login} - {submission}")
class Comments:
@staticmethod
def sort_comments(comments):
result = {}
for comment in comments:
print(comment)
author = comment["author"]["username"], comment["author"]["name"]
if author not in result:
result[author] = list()
result[author].append(comment)
return result
@staticmethod
def print_comments(comments):
comments = Comments.sort_comments(comments)
for author in comments:
login, name = author
print(f"***** {name} ({login}) *****")
for comment in comments[author]:
if comment["type"] == "DiffNote":
print(f"""L{comment["position"]["new_line"]}: {comment["body"]}""")
else:
print(comment["body"])
def __call__(self, login: str, submission: str):
branch = get_branch(login)
iid = get_mrs_for_branch(branch)[0]["iid"]
comments = get_comments(iid)
self.print_comments(comments)

17
commands/__init__.py Normal file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env python3
from commands.comments import Comments
from commands.merge import Merge
from commands.merge_requests import MergeRequests
from commands.test import Test
from commands.update_assignees import UpdateAssignees
__all__ = [
Comments.__name__,
Merge.__name__,
MergeRequests.__name__,
Test.__name__,
UpdateAssignees.__name__,
]

38
commands/base.py Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python3
import os
from constants import HOMEWORK
from git import checkout_branch
from utils import make_pair, get_branch, mkcd
class BaseCommand:
def __init__(self, submissions):
self.submissions = map(make_pair, submissions)
self.branch = None
def __call__(self):
for login, submission in self.submissions:
self.branch = get_branch(login)
self.exec(login, submission)
def exec(self, login: str, submission: str):
raise NotImplementedError()
class BaseGitCommand(BaseCommand):
def __call__(self):
for login, submission in self.submissions:
self.branch = get_branch(login)
checkout_branch(self.branch)
mkcd(f"{HOMEWORK}/{login}")
self.exec(login, submission)
os.chdir("../..")
checkout_branch("master")

51
commands/comments.py Normal file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python3
from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, get_comments
class Comments(BaseCommand):
@staticmethod
def sort_comments(comments):
result = {}
for comment in comments:
author = comment["author"]["username"], comment["author"]["name"]
if author not in result:
result[author] = list()
result[author].append(comment)
return result
@staticmethod
def print_comments(comments):
comments = Comments.sort_comments(comments)
for author in comments:
login, name = author
print(f"***** {name} ({login}) *****")
for comment in comments[author]:
if comment["system"]:
continue
if comment["type"] == "DiffNote":
print(f"""L{comment["position"]["new_line"]}: {comment["body"]}""")
else:
print(f"""[{comment["created_at"]}]\n{comment["body"]}""")
print()
print()
def exec(self, login: str, submission: str):
mrs = get_mrs_for_branch(self.branch)
if not mrs:
return
iid = mrs[0]["iid"]
comments = get_comments(iid)
print(f"# {self.branch}")
self.print_comments(comments)
print(f"# {self.branch}")

12
commands/merge.py Normal file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, merge_mr
class Merge(BaseCommand):
def exec(self, login: str, submission: str):
iid = get_mrs_for_branch(self.branch)[0]["iid"]
merge_mr(iid)

View file

@ -0,0 +1,53 @@
#!/usr/bin/env python3
import re
from constants import HOMEWORK, SUFFIX
from commands.base import BaseGitCommand
from git import add_files, commit, push
from gitlab import post_mr
from utils import run_cmd
class MergeRequests(BaseGitCommand):
@staticmethod
def get_files(submission: str) -> None:
relative_path = f"/home/kontr/kontr/_tmp_/ib111/{HOMEWORK}/{submission}"
files = f"{{master-naostro/LoadTest/{HOMEWORK}.py,teacher_email}}"
if run_cmd("rsync", "-avzP", f"aisa:{relative_path}/{files}", "./")[0] != 0:
exit(1)
@staticmethod
def call_flake() -> None:
process = run_cmd("flake8", "--exit-zero", f"{HOMEWORK}.py")[1]
with open("flake.log", "w") as f:
print(process.stdout, file=f)
@staticmethod
def get_mail() -> None:
with open("teacher_email") as file:
contents = file.read()
match = re.search(r"<pre>((.*\s+)+)<\/pre>", contents)
return match.group(1) if match else contents
def exec(self, login: str, submission: str) -> None:
self.get_files(submission)
self.call_flake()
add_files(f"{HOMEWORK}.py", "flake.log")
commit(f'"Add sources and flake log ({HOMEWORK}{SUFFIX} {login})"')
push("origin", self.branch)
post_mr(
source_branch=self.branch,
target_branch="master",
title=f"[{HOMEWORK}{SUFFIX}] {login}",
description=f"```\n{self.get_mail()}\n```",
labels=HOMEWORK,
remove_source_branch="true",
assignee_ids=["1772"],
)

9
commands/test.py Normal file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env python3
from commands.base import BaseCommand
class Test(BaseCommand):
def exec(self, login: str, submission: str):
print(f"{login} - {submission}")

View file

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, set_assignees
class UpdateAssignees(BaseCommand):
def exec(self, login: str, submission: str) -> None:
iid = get_mrs_for_branch(self.branch)[0]["iid"]
print(f"{login} @ {self.branch} : {iid}")
set_assignees(iid, ["39", "1772"])

View file

@ -1,37 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import re
import requests
from subprocess import run
import sys import sys
from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
from constants import SUBMISSIONS, HOMEWORK from constants import SUBMISSIONS
from git import checkout_branch
from utils import get_branch, mkcd, make_pair
def iterate_logins(func):
for login, submission in map(make_pair, SUBMISSIONS):
branch = get_branch(login)
checkout_branch(branch)
mkcd(f"{HOMEWORK}/{login}")
func(login, submission)
os.chdir("../..")
checkout_branch("master")
COMMANDS = { COMMANDS = {
"mrs": MergeRequests(), "mrs": MergeRequests,
"update-assignees": UpdateAssignees(), "update-assignees": UpdateAssignees,
"comments": Comments(), "comments": Comments,
"merge": Merge(), "merge": Merge,
"test": Test(), "test": Test,
} }
@ -40,7 +22,7 @@ def main():
print("Invalid command") print("Invalid command")
exit(2) exit(2)
iterate_logins(COMMANDS[sys.argv[1]]) COMMANDS[sys.argv[1]](SUBMISSIONS)()
if __name__ == "__main__": if __name__ == "__main__":