-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
129 lines (108 loc) · 4.42 KB
/
models.py
File metadata and controls
129 lines (108 loc) · 4.42 KB
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
import sys
import requests
import json
from datetime import datetime
from argv_parse import get_params
from config import TOKEN, USERNAME
from man import info
def get_token():
return TOKEN
class User():
def __init__(self):
self.token()
self.headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {self.token}'
}
def token(self):
self.token = get_token()
@property
def authorized(self):
auth_res = requests.get('https://api.github.com/user', headers=self.headers)
if auth_res.ok:
print('--Authorized--')
return True
else:
print(info)
print('**Not authorized**')
return False
class Params(User):
def __init__(self, argv=get_params(), *args, **kwargs):
super(Params, self).__init__(*args, **kwargs)
self.url = argv.get('url')
self.start_date = argv.get('start_date')
self.end_date = datetime.fromisoformat(argv.get('end_date').isoformat(timespec='seconds'))
self.branch = argv.get('branch')
self.payload = {'until' : f"{self.end_date.isoformat(timespec='seconds')}Z"}
def validation(self):
self.check_url()
self.check_branch()
self.check_start_date()
def check_url(self):
if not self.url.endswith('/'):
try:
url, owner, repo = self.url.split('/')[2:]
if url != 'github.com':
print(f'\nInvalid url: {url}\n\nExample: https://github.com/<username>/<repository>\n')
exit()
else:
res = requests.get(f'https://api.github.com/users/{owner}', headers={
'Authorization': f'token {self.token}'
}
)
if res.status_code != 200:
if res.status_code == 404:
print(f'\nUser <{owner}> not found\n\n')
exit()
else:
res = requests.get(f'https://api.github.com/repos/{owner}/{repo}', headers={
'Authorization': f'token {self.token}'
}
)
if res.status_code != 200:
if res.status_code == 404:
print(f'\nRepository <{repo}> not found\n\n')
exit()
else:
self.url = f'https://api.github.com/repos/{owner}/{repo}'
return self.url
except ValueError:
print(f'\nInvalid url: {self.url}\n\nExample: https://github.com/username/repository\n')
exit()
else:
print(f'\nInvalid url: {self.url}\n\nExample: https://github.com/username/repository\n')
exit()
def check_branch(self):
res = requests.get(f'{self.url}/branches/{self.branch}', headers={
'Authorization': f'token {self.token}'
}
)
if res.status_code != 200:
if res.status_code == 404:
print(f'\nBranch <{self.branch}> not found\n')
exit()
else:
self.payload['branch'] = self.branch
self.payload['sha'] = self.branch
return self.branch
def check_start_date(self):
if self.start_date:
self.start_date = datetime.fromisoformat(self.start_date.isoformat(timespec='seconds'))
self.payload['since'] = f"{self.start_date.isoformat(timespec='seconds')}Z"
else:
res = requests.get(self.url, headers={
'Authorization': f'token {self.token}'
}
)
self.start_date = datetime.fromisoformat(res.json()['created_at'][:-1:])
self.payload['since'] = res.json()['created_at']
class Query(User):
def __init__(self, params, *args, **kwargs):
super(Query, self).__init__(*args, **kwargs)
self.params = params
self.url_repo = params.url
self.url_repo_branch = self.url_repo + f'/branches/{self.params.branch}'
self.url_repo_commits = self.url_repo + '/commits'
self.url_repo_pulls = self.url_repo + '/pulls'
self.url_repo_issues = self.url_repo + '/issues'
self.payload = self.params.payload