Implement MR comments

Closes #5
This commit is contained in:
Matej Focko 2019-11-22 20:12:37 +01:00
parent ae7a4402e6
commit 55807f2fdd
3 changed files with 52 additions and 2 deletions

View file

@ -7,7 +7,7 @@ import re
from constants import HOMEWORK, SUFFIX from constants import HOMEWORK, SUFFIX
from git import add_files, commit, push 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 from utils import run_cmd, get_branch
@ -75,3 +75,40 @@ class Merge:
class Test: class Test:
def __call__(self, login: str, submission: str): def __call__(self, login: str, submission: str):
print(f"{login} - {submission}") 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)

View file

@ -67,3 +67,15 @@ def set_assignees(iid, assignee_ids):
headers=headers, headers=headers,
) as req: ) as req:
print(req.status_code) 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()

View file

@ -7,7 +7,7 @@ from subprocess import run
import sys import sys
from commands import MergeRequests, UpdateAssignees, Merge, Test from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
from constants import SUBMISSIONS, HOMEWORK from constants import SUBMISSIONS, HOMEWORK
from git import checkout_branch from git import checkout_branch
from utils import get_branch, mkcd, make_pair from utils import get_branch, mkcd, make_pair
@ -29,6 +29,7 @@ def iterate_logins(func):
COMMANDS = { COMMANDS = {
"mrs": MergeRequests(), "mrs": MergeRequests(),
"update-assignees": UpdateAssignees(), "update-assignees": UpdateAssignees(),
"comments": Comments(),
"merge": Merge(), "merge": Merge(),
"test": Test(), "test": Test(),
} }