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

View file

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

View file

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

View file

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

View file

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

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

View file

@ -1,10 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json
import os
from pathlib import Path
import sys 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
@ -18,7 +22,7 @@ COMMANDS = {
def print_usage(): def print_usage():
print(f"{sys.argv[0]} <path_to_mbox> <homework> <correction> <deadline> <command>") print(f"{sys.argv[0]} <command>")
print() print()
print("Commands:") print("Commands:")
print("\tmrs\t\t\tFetch files and create merge requests for them") 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("\tcomments\t\tFetch all comments on MRs")
print("\tmerge\t\t\tMerge all MRs") print("\tmerge\t\t\tMerge all MRs")
print("\ttest\t\t\tDebugging function") 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(): 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() print_usage()
exit(2) exit(2)
_, path, hw, correction, deadline, command = sys.argv submissions = Parser(
correction = correction == "y" config["mbox_path"], config["deadline"], config["correction"]
).parse(config["homework"])
submissions = Parser(path, deadline, correction).parse(hw) COMMANDS[sys.argv[1]](submissions, Gitlab(config["project"], config["token"]))()
COMMANDS[command](submissions)()
if __name__ == "__main__": if __name__ == "__main__":