Merge branch 'json' into 'master'

Json

See merge request xfocko/ib111-pushee!3
This commit is contained in:
Matej Focko 2019-12-12 17:06:03 +01:00
commit 18fa316bac
7 changed files with 99 additions and 88 deletions

View file

@ -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:

View file

@ -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, "#")

View file

@ -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)

View file

@ -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}",

View file

@ -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])

131
gitlab.py
View file

@ -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

View file

@ -1,10 +1,14 @@
#!/usr/bin/env python3
import json
import os
from pathlib import Path
import sys
from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test
from gitlab import Gitlab
from kontr_emails import Parser
@ -18,7 +22,7 @@ COMMANDS = {
def print_usage():
print(f"{sys.argv[0]} <path_to_mbox> <homework> <correction> <deadline> <command>")
print(f"{sys.argv[0]} <command>")
print()
print("Commands:")
print("\tmrs\t\t\tFetch files and create merge requests for them")
@ -26,19 +30,32 @@ def print_usage():
print("\tcomments\t\tFetch all comments on MRs")
print("\tmerge\t\t\tMerge all MRs")
print("\ttest\t\t\tDebugging function")
print("Format of date: %Y_%m%d_%H%M%S")
# print("Format of date: %Y_%m%d_%H%M%S")
def load_config():
config_file_path = Path("~/.pushee.json").expanduser()
if not os.path.exists(config_file_path):
print("Couldn't find config file", file=sys.stderr)
exit(1)
with open(config_file_path) as config_file:
config = json.load(config_file)
config["token"] = os.getenv("GITLAB_FI_TOKEN")
return config
def main():
if len(sys.argv) != 6 or sys.argv[5] not in COMMANDS:
config = load_config()
if len(sys.argv) != 2 or sys.argv[1] not in COMMANDS:
print_usage()
exit(2)
_, path, hw, correction, deadline, command = sys.argv
correction = correction == "y"
submissions = Parser(path, deadline, correction).parse(hw)
COMMANDS[command](submissions)()
submissions = Parser(
config["mbox_path"], config["deadline"], config["correction"]
).parse(config["homework"])
COMMANDS[sys.argv[1]](submissions, Gitlab(config["project"], config["token"]))()
if __name__ == "__main__":