-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_gitlab_api_operations.py
More file actions
338 lines (291 loc) · 9.92 KB
/
basic_gitlab_api_operations.py
File metadata and controls
338 lines (291 loc) · 9.92 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
Basic git lab GET operations using gitlab APIs and python
Supports :
1. user id based on user name upon object creation
2. user projects
3. project id based on project name
4. branches based on project name
5. project files based on project id
6. file content based on project id and file path
"""
import functools
import sys
import requests
if sys.version_info.major == 3:
import urllib.parse as parser
else:
import urllib as parser
def handle_exception(function):
"""
:param function:
:return:
"""
@functools.wraps(function)
def func(*args, **kwargs):
try:
return function(*args, **kwargs)
except Exception as exe:
return {"function": function.__name__, "message": exe}
return func
class ErrorHandler(type):
"""
"""
def __new__(mcs, name, bases, dct):
"""
:param name:
:param bases:
:param dct:
:return:
"""
for m in dct:
if hasattr(dct[m], '__call__'):
dct[m] = handle_exception(dct[m])
return type.__new__(mcs, name, bases, dct)
class GitLab:
"""
GitLab GET APIs operations
"""
__metaclass__ = ErrorHandler
def __init__(self, user_name, access_token):
self._user_name = user_name
self._access_token = access_token
self._user_id = None
self._api_url = "https://gitlab.com/api/v4/"
@property
def user_name(self):
return self._user_name
@user_name.setter
def user_name(self, user_name):
self._user_name = user_name
@user_name.deleter
def user_name(self):
del self._user_name
@property
def access_token(self):
return self._access_token
@access_token.setter
def access_token(self, access_token):
self._access_token = access_token
@access_token.deleter
def access_token(self):
del self._access_token
@property
def api_url(self):
return self._api_url
@property
def user_id(self):
return self._user_id
def __repr__(self):
return {"user_name": self._user_name, "access_token": self._access_token}
def __str__(self):
return "Auth(user_name= {0}, access_token= {1})".format(self.user_name, self.access_token)
def get_user_id(self):
"""
Gets user id based on the given user_name upon object creation
:return: user id of type int
"""
api = self.api_url + "user"
url = api
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.json()
self._user_id = data["id"]
else:
self._user_id = data.json()
return self._user_id
def get_user_projects(self):
"""
Gets projects related to user based on user id
:return: array of dictionaries containing project details
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
projects = []
page = 1
api = self.api_url + "users/{0}/projects?page_size=100&page={1}"
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
while True:
url = api.format(self.user_id, page)
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.json()
else:
break
if data:
projects += data
else:
break
page += 1
return projects
def get_project_id(self, project_name):
"""
Gets project id based on project name
:param project_name: string containing project name
:return: project id of type integer
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
project_id = None
page = 1
api = self.api_url + "users/{0}/projects?search={1}&page_size=100&page={2}"
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
while not project_id:
url = api.format(self.user_id, project_name, page)
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.json()
else:
break
if not data:
break
data_len = len(data)
if data_len == 1:
if data[0]["name"] == project_name:
project_id = data[0]["id"]
break
else:
for i in range(-(data_len // 2)):
if data[i]["name"] == project_name:
project_id = data[i]["id"]
break
if data[data_len - i]["name"] == project_name:
project_id = data[data_len - i]["id"]
break
page += 1
return project_id
def get_branches(self, project_id):
"""
Gets branches based on the project id
:param project_id: integer containing project id
:return: array of dictionaries containing branch details
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
branches = []
page = 1
api = self.api_url + "projects/{0}/repository/branches?page_size=100&page={1}"
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
while True:
url = api.format(project_id, page)
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.json()
else:
break
if data:
branches += data
else:
break
page += 1
return branches
def get_all_project_files(self, project_id, branch="master"):
"""
Gets files related to the project id
:param project_id: integer containing project id
:param branch: string containing the branch name from which the files are to be fetched
:return: array of dictionaries containing details regarding project files
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
project_files = []
page = 1
api = self.api_url + "projects/{0}/repository/tree?ref={1}&recursive=1&per_page=100&page={2}"
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
while True:
url = api.format(project_id, branch, page)
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.json()
else:
break
if data:
project_files += data
else:
break
page += 1
return project_files
def get_project_files(self, project_id, path, branch="master"):
"""
Gets files related to the project id and path
:param path: string containing path from which files are to be retrieved
:param project_id: integer containing project id
:param branch: string containing the branch name from which the files are to be fetched
:return: array of dictionaries containing details regarding project files
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
project_files = self.get_all_project_files(project_id=project_id, branch=branch)
files = []
path = path.strip("/")
path_len = len(path)
for file in project_files:
if file["type"] == "blob" and file["path"][:path_len] == path:
files.append(file)
return files
def get_file_raw(self, project_id, path, branch="master"):
"""
Gets raw content from the given file path
:param project_id: integer containing project id
:param path: string containing the path to the file for which the raw content is to be fetched
:param branch: string containing the branch name from which the files are to be fetched
:return: string containing raw file data
"""
if not self.user_id:
self.get_user_id()
if type(self.user_id).__name__ == "dict":
return self.user_id
project_file_raw = None
api = self.api_url + "projects/{0}/repository/files/{1}/raw?ref={2}"
headers = {
'Private-Token': self.access_token,
"Content-Type": "application/json"
}
path = parser.quote(path, safe="")
url = api.format(project_id, path, branch)
data = requests.get(url=url, headers=headers)
if data.ok:
data = data.content
if data:
project_file_raw = data
return project_file_raw
def main():
"""
main
:return: None
"""
gl = GitLab(user_name="vishnu2981997", access_token="access_token")
user_id = gl.get_user_id()
projects = gl.get_user_projects()
project_id = gl.get_project_id(project_name="wasup_bro")
project_branches = gl.get_branches(project_id=project_id)
project_files = gl.get_all_project_files(project_id=project_id, branch="master")
file_content = gl.get_file_raw(project_id=project_id, path="booo/booo_file_1.py", branch="master")
files = gl.get_project_files(project_id=project_id, path="/booo/", branch="master")
if __name__ == "__main__":
main()