/
github_api.py
122 lines (93 loc) · 3.82 KB
/
github_api.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import toolz
from github import Github
from collections import defaultdict
import urllib
import logging
def get_user_login_name(access_token):
github = Github(login_or_token=access_token)
return github.get_user().login
def get_github_user(access_token):
github = Github(login_or_token=access_token)
return github.get_user()
def get_project_list(user_id):
pass
def get_a_repo(github_user, repo_name):
f_list = filter(lambda r:r.name == repo_name, github_user.get_repos(type='all'))
return f_list[0] if f_list else None
def get_repo_list(user):
""" Get all github repositories of the user."""
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
return sorted([('{0}*{1}'.format(repo.id, repo.name), repo.name)
for repo in git_user.get_repos(type='all')], key=lambda x: x[1])
def get_users_list(user, repo_name_list):
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
user_list = set()
for repo_name in repo_name_list:
repo = get_a_repo(git_user, repo_name)
for g_user in repo.get_collaborators():
user_list.add(g_user)
return sorted([user for user in toolz.unique(user_list, key=lambda x: x.login)], key=lambda x: x.login)
def get_label_list(user, repo_name_list):
label_list = set()
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
for repo_name in repo_name_list:
repo = get_a_repo(git_user, repo_name)
if repo is None:
continue
for lbl in repo.get_labels():
label_list.add(lbl.name)
return sorted(label_list)
def get_issue_dict(user, repo_list, lbl_list):
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
label_names = [l.name for l in lbl_list]
issues_dict = defaultdict(list)
for repo_name in repo_list:
repo = get_a_repo(git_user, repo_name)
if repo is None:
continue
issue_list = repo.get_issues()
for issue in issue_list:
for lbl in issue.labels:
if lbl.name in label_names:
issues_dict[lbl.name].append(issue)
return issues_dict
def change_issue_label(user, lbl_from, lbl_to, repo_name, issue_number):
# retrieve Github objects
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
repo = get_a_repo(git_user, repo_name)
# retrieve the issue
issue = repo.get_issue(int(issue_number))
# check if the destination label is DONE
if lbl_to == "DONE":
# change the status of the issue to close
issue.edit(state='closed')
else:
# remove the from label
issue.remove_from_labels(urllib.quote(lbl_from))
# check if new label exists in the repository
# get all labels from this repository
labels = [label for label in repo.get_labels()]
if any(filter(lambda l:l.name == lbl_to, labels)):
# get the label object
new_label = repo.get_label(lbl_to)
else:
# create the new label in repository
new_label = repo.create_label(lbl_to, "00ff00")
issue.add_to_labels(new_label)
return new_label
def create_new_issue(user, repo_name, title, body, label_name):
github = Github(login_or_token=user.github_access_token)
git_user = github.get_user()
repo = get_a_repo(git_user, repo_name)
label_objects = [lbl for lbl in repo.get_labels() if lbl.name == label_name]
# check if this label exists in this repo, if not create one
if len(label_objects) == 0:
new_label = repo.create_label(label_name, "00ff00")
label_objects = [new_label]
issue = repo.create_issue(title=title, body=body, labels=label_objects)
return issue