Gitlab as class
This commit is contained in:
parent
5d87c66da7
commit
3b2e4d7de9
7 changed files with 79 additions and 82 deletions
|
@ -4,15 +4,16 @@
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
from git import checkout_branch
|
from git import checkout_branch
|
||||||
|
from gitlab import Gitlab
|
||||||
from utils import mkcd
|
from utils import mkcd
|
||||||
from submission import Submission
|
from submission import Submission
|
||||||
|
|
||||||
|
|
||||||
class BaseCommand:
|
class BaseCommand:
|
||||||
def __init__(self, submissions: List[Submission]) -> None:
|
def __init__(self, submissions: List[Submission], gitlab: Gitlab) -> None:
|
||||||
self.submissions = submissions
|
self.submissions = submissions
|
||||||
|
self.gitlab = gitlab
|
||||||
|
|
||||||
def __call__(self) -> None:
|
def __call__(self) -> None:
|
||||||
for submission in self.submissions:
|
for submission in self.submissions:
|
||||||
|
|
|
@ -5,7 +5,6 @@ import json
|
||||||
|
|
||||||
|
|
||||||
from commands.base import BaseCommand
|
from commands.base import BaseCommand
|
||||||
from gitlab import get_mrs_for_branch, get_comments
|
|
||||||
from submission import Submission
|
from submission import Submission
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,12 +49,12 @@ class Comments(BaseCommand):
|
||||||
print(header)
|
print(header)
|
||||||
|
|
||||||
def exec(self, submission: Submission) -> None:
|
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:
|
if not mrs:
|
||||||
return
|
return
|
||||||
|
|
||||||
iid = mrs[0]["iid"]
|
iid = mrs[0]["iid"]
|
||||||
comments = get_comments(iid)
|
comments = self.gitlab.get_comments(iid)
|
||||||
|
|
||||||
branch = f"### {submission.branch.center(20)} ###".upper().center(80, "#")
|
branch = f"### {submission.branch.center(20)} ###".upper().center(80, "#")
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
|
|
||||||
from commands.base import BaseCommand
|
from commands.base import BaseCommand
|
||||||
from gitlab import get_mrs_for_branch, merge_mr
|
|
||||||
from submission import Submission
|
from submission import Submission
|
||||||
|
|
||||||
|
|
||||||
class Merge(BaseCommand):
|
class Merge(BaseCommand):
|
||||||
def exec(self, submission: Submission) -> None:
|
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)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import re
|
||||||
|
|
||||||
from commands.base import BaseGitCommand
|
from commands.base import BaseGitCommand
|
||||||
from git import add_files, commit, push
|
from git import add_files, commit, push
|
||||||
from gitlab import post_mr
|
|
||||||
from submission import Submission
|
from submission import Submission
|
||||||
from utils import run_cmd
|
from utils import run_cmd
|
||||||
|
|
||||||
|
@ -33,7 +32,7 @@ class MergeRequests(BaseGitCommand):
|
||||||
commit(f'"Add sources and flake log ({submission.branch} {submission.login})"')
|
commit(f'"Add sources and flake log ({submission.branch} {submission.login})"')
|
||||||
push("origin", submission.branch)
|
push("origin", submission.branch)
|
||||||
|
|
||||||
post_mr(
|
self.gitlab.post_mr(
|
||||||
source_branch=submission.branch,
|
source_branch=submission.branch,
|
||||||
target_branch="master",
|
target_branch="master",
|
||||||
title=f"[{submission.homework}{'-opravne' if submission.correction else ''}] {submission.login}",
|
title=f"[{submission.homework}{'-opravne' if submission.correction else ''}] {submission.login}",
|
||||||
|
|
|
@ -2,13 +2,12 @@
|
||||||
|
|
||||||
|
|
||||||
from commands.base import BaseCommand
|
from commands.base import BaseCommand
|
||||||
from gitlab import get_mrs_for_branch, set_assignees
|
|
||||||
from submission import Submission
|
from submission import Submission
|
||||||
|
|
||||||
|
|
||||||
class UpdateAssignees(BaseCommand):
|
class UpdateAssignees(BaseCommand):
|
||||||
def exec(self, submission: Submission) -> None:
|
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}")
|
print(f"{submission.login} @ {submission.branch} : {iid}")
|
||||||
set_assignees(iid, [39, 1772])
|
self.gitlab.set_assignees(iid, [39, 1772])
|
||||||
|
|
131
gitlab.py
131
gitlab.py
|
@ -4,83 +4,80 @@
|
||||||
import os
|
import os
|
||||||
import requests
|
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(
|
self.headers = {"Private-Token": token}
|
||||||
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}
|
|
||||||
|
|
||||||
with requests.post(
|
def post_mr(
|
||||||
f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests",
|
self,
|
||||||
params=params,
|
source_branch,
|
||||||
headers=headers,
|
target_branch,
|
||||||
) as req:
|
title,
|
||||||
print(req.status_code)
|
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):
|
def get_mrs_for_branch(self, branch):
|
||||||
params = {"source_branch": branch}
|
params = {"source_branch": branch}
|
||||||
headers = {"Private-Token": TOKEN}
|
|
||||||
|
|
||||||
with requests.get(
|
with requests.get(
|
||||||
f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests",
|
f"{self.base_url}/{self.project}/merge_requests",
|
||||||
params=params,
|
params=params,
|
||||||
headers=headers,
|
headers=self.headers,
|
||||||
) as req:
|
) as req:
|
||||||
return req.json()
|
return req.json()
|
||||||
|
|
||||||
|
def merge_mr(self, iid):
|
||||||
|
|
||||||
def merge_mr(iid):
|
with requests.put(
|
||||||
headers = {"Private-Token": TOKEN}
|
f"{self.base_url}/{self.project}/merge_requests/{iid}/merge",
|
||||||
|
headers=self.headers,
|
||||||
|
) as req:
|
||||||
|
print(req.status_code)
|
||||||
|
|
||||||
with requests.put(
|
def set_assignees(self, iid, assignee_ids):
|
||||||
f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests/{iid}/merge",
|
params = {"assignee_ids[]": assignee_ids}
|
||||||
headers=headers,
|
|
||||||
) as req:
|
|
||||||
print(req.status_code)
|
|
||||||
|
|
||||||
|
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):
|
def get_comments(self, iid, page=1):
|
||||||
params = {"assignee_ids[]": assignee_ids}
|
params = {"sort": "asc", "page": page}
|
||||||
headers = {"Private-Token": TOKEN}
|
|
||||||
|
|
||||||
with requests.put(
|
with requests.get(
|
||||||
f"https://gitlab.fi.muni.cz/api/v4/projects/{PROJECT}/merge_requests/{iid}",
|
f"{self.base_url}/{self.project}/merge_requests/{iid}/notes",
|
||||||
params=params,
|
params=params,
|
||||||
headers=headers,
|
headers=self.headers,
|
||||||
) as req:
|
) as req:
|
||||||
print(req.status_code)
|
comments = req.json()
|
||||||
|
if 'rel="next"' in req.headers["Link"]:
|
||||||
|
comments.extend(self.get_comments(iid, page + 1))
|
||||||
def get_comments(iid, page=1):
|
return comments
|
||||||
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
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import sys
|
||||||
|
|
||||||
|
|
||||||
from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
|
from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
|
||||||
|
from gitlab import Gitlab
|
||||||
from kontr_emails import Parser
|
from kontr_emails import Parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,7 +40,9 @@ def load_config():
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
with open(config_file_path) as config_file:
|
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():
|
def main():
|
||||||
|
@ -52,7 +55,7 @@ def main():
|
||||||
submissions = Parser(
|
submissions = Parser(
|
||||||
config["mbox_path"], config["deadline"], config["correction"]
|
config["mbox_path"], config["deadline"], config["correction"]
|
||||||
).parse(config["homework"])
|
).parse(config["homework"])
|
||||||
COMMANDS[sys.argv[1]](submissions)()
|
COMMANDS[sys.argv[1]](submissions, Gitlab(config["project"], config["token"]))()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Reference in a new issue