-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub_utils.py
64 lines (57 loc) · 2.4 KB
/
github_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import os
from json import JSONDecodeError
from github import Github
from github import UnknownObjectException
class GithubUtil:
def __init__(self, access_token):
self._access_token = access_token
self._repo = None
def set_github_repo(self, repository_name):
"""
github repo object를 얻는 함수
:param repository_name: 해당 repo의 이름
:return: repo object
"""
g = Github(self._access_token)
self._repo = g.get_user().get_repo(repository_name)
def upload_github_issue(self, title, body, labels=None):
"""
해당 repo의 issue에 새롭게 작성된 post의 내용을 등록하는 함수
:param title: 이슈 제목
:param body: 이슈 내용
:param labels: 이슈의 labels
:return: None
"""
self._repo.create_issue(title=title, body=body, labels=labels)
def upload_github_push(self, message, content, path, branch):
"""
해당 repo에 변경된 json file을 push 해주는 함수
:param message: push message
:param content: push content
:param path: push 할 대상의 file 위치
:param branch: push 할 branch
:return: None
"""
try:
contents = self._repo.get_contents(path, branch)
data = json.loads(contents.decoded_content.decode('utf-8'))
data = self._check_json_username(data=data, content=content)
self._repo.update_file(contents.path, message, data, contents.sha, branch=branch)
except JSONDecodeError:
data = self._check_json_username(content=content)
self._repo.update_file(contents.path, message, data, contents.sha, branch=branch)
except UnknownObjectException:
# if old file is not exists make new file
data = self._check_json_username(content=content)
self._repo.create_file(path, message, data, branch=branch)
def _check_json_username(self, content, data=None):
data = data if data else dict()
if data.get('username') != os.environ.get('USERNAME'):
data['username'] = os.environ.get('USERNAME')
data['posts'] = content
else:
data['posts'].update(content)
data = dict(sorted(data.items(), reverse=True))
data = json.dumps(data, ensure_ascii=False, indent='\t')
return data