diff --git a/commands/base.py b/commands/base.py index 6470e2c..4dfde68 100644 --- a/commands/base.py +++ b/commands/base.py @@ -4,15 +4,16 @@ import os from typing import List - from git import checkout_branch +from gitlab import Gitlab from utils import mkcd from submission import Submission class BaseCommand: - def __init__(self, submissions: List[Submission]) -> None: + def __init__(self, submissions: List[Submission], gitlab: Gitlab) -> None: self.submissions = submissions + self.gitlab = gitlab def __call__(self) -> None: for submission in self.submissions: diff --git a/commands/comments.py b/commands/comments.py index adb695b..3993fbd 100644 --- a/commands/comments.py +++ b/commands/comments.py @@ -5,7 +5,6 @@ import json from commands.base import BaseCommand -from gitlab import get_mrs_for_branch, get_comments from submission import Submission @@ -50,12 +49,12 @@ class Comments(BaseCommand): print(header) def exec(self, submission: Submission) -> None: - mrs = get_mrs_for_branch(submission.branch) + mrs = self.gitlab.get_mrs_for_branch(submission.branch) if not mrs: return iid = mrs[0]["iid"] - comments = get_comments(iid) + comments = self.gitlab.get_comments(iid) branch = f"### {submission.branch.center(20)} ###".upper().center(80, "#") diff --git a/commands/merge.py b/commands/merge.py index d222574..061be24 100644 --- a/commands/merge.py +++ b/commands/merge.py @@ -2,12 +2,11 @@ from commands.base import BaseCommand -from gitlab import get_mrs_for_branch, merge_mr from submission import Submission class Merge(BaseCommand): def exec(self, submission: Submission) -> None: - iid = get_mrs_for_branch(submission.branch)[0]["iid"] + iid = self.gitlab.get_mrs_for_branch(submission.branch)[0]["iid"] - merge_mr(iid) + self.gitlab.merge_mr(iid) diff --git a/commands/merge_requests.py b/commands/merge_requests.py index 54611c2..beede7e 100644 --- a/commands/merge_requests.py +++ b/commands/merge_requests.py @@ -6,7 +6,6 @@ import re from commands.base import BaseGitCommand from git import add_files, commit, push -from gitlab import post_mr from submission import Submission from utils import run_cmd @@ -33,7 +32,7 @@ class MergeRequests(BaseGitCommand): commit(f'"Add sources and flake log ({submission.branch} {submission.login})"') push("origin", submission.branch) - post_mr( + self.gitlab.post_mr( source_branch=submission.branch, target_branch="master", title=f"[{submission.homework}{'-opravne' if submission.correction else ''}] {submission.login}", diff --git a/commands/update_assignees.py b/commands/update_assignees.py index 05a320f..df97310 100644 --- a/commands/update_assignees.py +++ b/commands/update_assignees.py @@ -2,13 +2,12 @@ from commands.base import BaseCommand -from gitlab import get_mrs_for_branch, set_assignees from submission import Submission class UpdateAssignees(BaseCommand): def exec(self, submission: Submission) -> None: - iid = get_mrs_for_branch(submission.branch)[0]["iid"] + iid = self.gitlab.get_mrs_for_branch(submission.branch)[0]["iid"] print(f"{submission.login} @ {submission.branch} : {iid}") - set_assignees(iid, [39, 1772]) + self.gitlab.set_assignees(iid, [39, 1772]) diff --git a/gitlab.py b/gitlab.py index 236f2e0..2a7b123 100644 --- a/gitlab.py +++ b/gitlab.py @@ -4,83 +4,80 @@ import os import requests -PROJECT = os.getenv("PUSHEE_PROJECT") -TOKEN = os.getenv("GITLAB_FI_TOKEN") +class Gitlab: + def __init__(self, project, token): + self.base_url = "https://gitlab.fi.muni.cz/api/v4/projects" + self.project = project + self.token = token -def post_mr( - source_branch, - target_branch, - title, - description, - labels, - remove_source_branch, - assignee_ids, -): - params = { - "source_branch": source_branch, - "target_branch": target_branch, - "title": title, - "description": description, - "labels": labels, - "remove_source_branch": remove_source_branch, - "assignee_ids": assignee_ids, - } - headers = {"Private-Token": TOKEN} + self.headers = {"Private-Token": token} - with requests.post( - f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests", - params=params, - headers=headers, - ) as req: - print(req.status_code) + def post_mr( + self, + source_branch, + target_branch, + title, + description, + labels, + remove_source_branch, + assignee_ids, + ): + params = { + "source_branch": source_branch, + "target_branch": target_branch, + "title": title, + "description": description, + "labels": labels, + "remove_source_branch": remove_source_branch, + "assignee_ids": assignee_ids, + } + with requests.post( + f"{self.base_url}/{self.project}/merge_requests", + params=params, + headers=self.headers, + ) as req: + print(req.status_code) -def get_mrs_for_branch(branch): - params = {"source_branch": branch} - headers = {"Private-Token": TOKEN} + def get_mrs_for_branch(self, branch): + params = {"source_branch": branch} - with requests.get( - f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests", - params=params, - headers=headers, - ) as req: - return req.json() + with requests.get( + f"{self.base_url}/{self.project}/merge_requests", + params=params, + headers=self.headers, + ) as req: + return req.json() + def merge_mr(self, iid): -def merge_mr(iid): - headers = {"Private-Token": TOKEN} + with requests.put( + f"{self.base_url}/{self.project}/merge_requests/{iid}/merge", + headers=self.headers, + ) as req: + print(req.status_code) - with requests.put( - f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests/{iid}/merge", - headers=headers, - ) as req: - print(req.status_code) + def set_assignees(self, iid, assignee_ids): + params = {"assignee_ids[]": assignee_ids} + with requests.put( + f"{self.base_url}/{self.project}/merge_requests/{iid}", + params=params, + headers=self.headers, + ) as req: + print(req.status_code) -def set_assignees(iid, assignee_ids): - params = {"assignee_ids[]": assignee_ids} - headers = {"Private-Token": TOKEN} + def get_comments(self, iid, page=1): + params = {"sort": "asc", "page": page} - with requests.put( - f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests/{iid}", - params=params, - headers=headers, - ) as req: - print(req.status_code) - - -def get_comments(iid, page=1): - params = {"sort": "asc", "page": page} - 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: - comments = req.json() - if 'rel="next"' in req.headers["Link"]: - comments.extend(get_comments(iid, page + 1)) - return comments + with requests.get( + f"{self.base_url}/{self.project}/merge_requests/{iid}/notes", + params=params, + headers=self.headers, + ) as req: + comments = req.json() + if 'rel="next"' in req.headers["Link"]: + comments.extend(self.get_comments(iid, page + 1)) + return comments diff --git a/pushee.py b/pushee.py index 6bfc062..8dcd2ae 100644 --- a/pushee.py +++ b/pushee.py @@ -8,6 +8,7 @@ import sys from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test +from gitlab import Gitlab from kontr_emails import Parser @@ -39,7 +40,9 @@ def load_config(): exit(1) with open(config_file_path) as config_file: - return json.load(config_file) + config = json.load(config_file) + config["token"] = os.getenv("GITLAB_FI_TOKEN") + return config def main(): @@ -52,7 +55,7 @@ def main(): submissions = Parser( config["mbox_path"], config["deadline"], config["correction"] ).parse(config["homework"]) - COMMANDS[sys.argv[1]](submissions)() + COMMANDS[sys.argv[1]](submissions, Gitlab(config["project"], config["token"]))() if __name__ == "__main__":