diff --git a/commands.py b/commands.py deleted file mode 100644 index 14b7069..0000000 --- a/commands.py +++ /dev/null @@ -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"
((.*\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)
diff --git a/commands/__init__.py b/commands/__init__.py
new file mode 100644
index 0000000..35e2930
--- /dev/null
+++ b/commands/__init__.py
@@ -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__,
+]
diff --git a/commands/base.py b/commands/base.py
new file mode 100644
index 0000000..2894972
--- /dev/null
+++ b/commands/base.py
@@ -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")
\ No newline at end of file
diff --git a/commands/comments.py b/commands/comments.py
new file mode 100644
index 0000000..2f6bb68
--- /dev/null
+++ b/commands/comments.py
@@ -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}")
diff --git a/commands/merge.py b/commands/merge.py
new file mode 100644
index 0000000..bedd294
--- /dev/null
+++ b/commands/merge.py
@@ -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)
\ No newline at end of file
diff --git a/commands/merge_requests.py b/commands/merge_requests.py
new file mode 100644
index 0000000..dd63747
--- /dev/null
+++ b/commands/merge_requests.py
@@ -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"
((.*\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"],
+        )
diff --git a/commands/test.py b/commands/test.py
new file mode 100644
index 0000000..d661291
--- /dev/null
+++ b/commands/test.py
@@ -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}")
\ No newline at end of file
diff --git a/commands/update_assignees.py b/commands/update_assignees.py
new file mode 100644
index 0000000..5cba0da
--- /dev/null
+++ b/commands/update_assignees.py
@@ -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"])
\ No newline at end of file
diff --git a/reviews.py b/reviews.py
index 8cf414c..a489376 100644
--- a/reviews.py
+++ b/reviews.py
@@ -1,37 +1,19 @@
 #!/usr/bin/env python3
 
-import os
-import re
-import requests
-from subprocess import run
+
 import sys
 
 
 from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
-from constants import SUBMISSIONS, HOMEWORK
-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")
+from constants import SUBMISSIONS
 
 
 COMMANDS = {
-    "mrs": MergeRequests(),
-    "update-assignees": UpdateAssignees(),
-    "comments": Comments(),
-    "merge": Merge(),
-    "test": Test(),
+    "mrs": MergeRequests,
+    "update-assignees": UpdateAssignees,
+    "comments": Comments,
+    "merge": Merge,
+    "test": Test,
 }
 
 
@@ -40,7 +22,7 @@ def main():
         print("Invalid command")
         exit(2)
 
-    iterate_logins(COMMANDS[sys.argv[1]])
+    COMMANDS[sys.argv[1]](SUBMISSIONS)()
 
 
 if __name__ == "__main__":