From 55807f2fdd827cc5450cd4848c65cab42c99852f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 22 Nov 2019 20:12:37 +0100 Subject: [PATCH] Implement MR comments Closes #5 --- commands.py | 39 ++++++++++++++++++++++++++++++++++++++- gitlab.py | 12 ++++++++++++ reviews.py | 3 ++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/commands.py b/commands.py index c129004..14b7069 100644 --- a/commands.py +++ b/commands.py @@ -7,7 +7,7 @@ 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 +from gitlab import post_mr, get_mrs_for_branch, set_assignees, merge_mr, get_comments from utils import run_cmd, get_branch @@ -75,3 +75,40 @@ class Merge: 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/gitlab.py b/gitlab.py index db5aa2d..215ebdd 100644 --- a/gitlab.py +++ b/gitlab.py @@ -67,3 +67,15 @@ def set_assignees(iid, assignee_ids): headers=headers, ) as req: print(req.status_code) + + +def get_comments(iid): + params = {"sort": "asc"} + headers = {"Private-Token": TOKEN} + + with requests.get( + f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests/{iid}/notes", + params=params, + headers=headers, + ) as req: + return req.json() diff --git a/reviews.py b/reviews.py index 59e5e19..b899de2 100644 --- a/reviews.py +++ b/reviews.py @@ -7,7 +7,7 @@ from subprocess import run import sys -from commands import MergeRequests, UpdateAssignees, Merge, Test +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 @@ -29,6 +29,7 @@ def iterate_logins(func): COMMANDS = { "mrs": MergeRequests(), "update-assignees": UpdateAssignees(), + "comments": Comments(), "merge": Merge(), "test": Test(), }