-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
274 lines (196 loc) · 7.34 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import requests
import json
import os
from github import Github
from github_token import GITHUB_TOKEN, user
# 1. GitHub username
username = user
print(username)
'''
# url to request
url = f"https://api.github.com/users/{username}"
# make the request and return the json
user_data = requests.get(url).json()
# pretty print JSON data
# pprint(user_data)
# Storing the data in a json file
user_data_json = user_data
with open('github_data.json', 'w') as f:
json.dump(user_data_json, f)
'''
def get_user_basic(login_name:str):
"""Function which prints all the user's basic information including the list of the repositories
login_name:
string of the GitHub login name
:return:
a json contents that is printed to the 'github_data.json'
"""
# url to request
url = f"https://api.github.com/users/{username}"
# make the request and return the json
user_data = requests.get(url).json()
# pretty print JSON data
# pprint(user_data)
# Storing the data in a json file
user_data_json = user_data
with open('github_data.json', 'w') as f:
json.dump(user_data_json, f)
# 2. Getting Public Repositories of a User
def get_repositories(name: str):
"""
Parameters
----------
name: string
"""
# Code adapted from https://www.thepythoncode.com/article/using-github-api-in-python
# Github username
# pygithub object
g = Github()
# get that user by username
user_object = g.get_user(name)
# Load the JSON File to then append the information
data_file = open("github_data.json", "r")
data = json.load(data_file)
data["repo_names"] = []
for repo in user_object.get_repos():
print(repo)
data["repo_names"].append(str(repo))
# Writes the whole object with the appended information to the JSON File
print(data)
with open("github_data.json", "w") as data_file:
data_file.write(json.dumps(data))
def get_org_repos_issues(org_name: str, separator: str, token: str) -> str:
"""Create a txt file with all the issues from an organization.
Parameters
----------
org_name : string
in small letters of the repository name
separator : string
symbol for separating the columns
token: string
with the GitHub token
Returns
------
gen_list_issues.txt : txt
file with lines of the organization's issues' information
"""
g = Github(token)
print(token)
# Get repositories for an organization
org = g.get_organization(org_name)
repos = org.get_repos()
repos_id_list = []
# Repo ID list printing
print(repos_id_list)
for repo in repos:
print(repo)
repos_id_list.append(repo.id)
# Get issues for a repo
issues = repo.get_issues()
lines = []
for issue in issues:
print('----------------------------------')
print(issue)
issue_string = repo.name + separator + str(
issue.state) + separator + str(issue.number) + separator + str(issue.title) + separator + str(
issue.created_at) + separator + str(issue.user.login) + separator + str(issue.comments.real) + separator
# print(issue_string)
# issues_number_list.append(issue.number)
# Getting the last comment if there are any comments
if issue.comments.real > 0:
comments = issue.get_comments()
last_comment = comments[issue.comments.real - 1]
# print(last_comment.updated_at)
issue_string = issue_string + str(last_comment.updated_at) + separator
# print(issue_string)
else:
issue_string = issue_string + "NA" + separator
print(issue_string)
# print(dir(issue))
# Get labels on a particular issue
labels = issue.get_labels()
labels_number_list = []
label_string = ""
for label in labels:
labels_number_list.append(label)
# print(label)
# Adding labels to the string
label_string = label_string + "/" + str(label.name)
# print(label_string)
print(labels_number_list)
issue_string = issue_string + label_string
print(issue_string)
lines.append(issue_string)
with open('gen_list_issues.txt', 'a') as f:
for line in lines:
f.write(line)
f.write('\n')
def get_repo_issues(repo_name: str):
"""Create a text file with
:param
repo_name: string
:return:
string lines of code dumped into the gen_list_issues.txt file
"""
# Variables
g = Github(username, GITHUB_TOKEN)
separator = ";"
user_object = g.get_user()
#repo_name = "github_api"
lines = []
print(GITHUB_TOKEN)
# Get repositories for an organization
repo = user_object.get_repo(repo_name)
# Get issues for a repo
issues = repo.get_issues()
for issue in issues:
print('----------------------------------')
print(issue)
issue_string = repo_name + separator + str(issue.number) + separator + str(issue.title) + separator + str(issue.created_at) + separator + str(issue.user.name) + separator + str(issue.comments.real) + separator
# print(issue_string)
# issues_number_list.append(issue.number)
# Getting the last comment if there are any comments
if issue.comments.real > 0:
comments = issue.get_comments()
last_comment = comments[issue.comments.real - 1]
# print(last_comment.updated_at)
issue_string = issue_string + str(last_comment.updated_at) + separator
# print(issue_string)
else:
issue_string = issue_string + "NA" + separator
print(issue_string)
# print(dir(issue))
# Get labels on a particular issue
labels = issue.get_labels()
labels_number_list = []
label_string = ""
for label in labels:
labels_number_list.append(label)
#print(label)
# Adding labels to the string
label_string = label_string + "/" + str(label.name)
# print(label_string)
print(labels_number_list)
issue_string = issue_string + label_string
print(issue_string)
lines.append(issue_string)
with open('gen_list_issues.txt', 'a') as f:
for line in lines:
f.write(line)
f.write('\n')
'''
Execution of code
----------------------------
Examples of function uses:
- Getting all the user information of basic interest:
get_user_basic(username)
get_user_basic('cgbosse')
- Getting organization information:
get_org_repos_issues("torrust", ";", GITHUB_TOKEN)
get_org_repos_issues("nautilus-cyberneering", ";", GITHUB_TOKEN)
- Getting individual user's repositories:
get_repositories(user)
- Getting individual user's repositories's issues:
get_repo_issues()
get_repo_issues("github_api")
'''