forked from unfoldingWord-dev/python-gitea-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities.py
More file actions
529 lines (424 loc) · 12.9 KB
/
Copy pathentities.py
File metadata and controls
529 lines (424 loc) · 12.9 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
"""
Various immutable classes that represent Gogs entities.
"""
def json_get(parsed_json, key):
"""
Retrieves the key from a parsed_json dictionary, or raises an exception if the
key is not present
"""
if key not in parsed_json:
raise ValueError("JSON does not contain a {} field".format(key))
return parsed_json[key]
class GogsUser(object):
"""
An immutable representation of a Gogs user
"""
def __init__(self, user_id, username, full_name, email, avatar_url):
self._id = user_id
self._username = username
self._full_name = full_name
self._email = email
self._avatar_url = avatar_url
@staticmethod
def from_json(parsed_json):
user_id = json_get(parsed_json, "id")
username = json_get(parsed_json, "username")
full_name = json_get(parsed_json, "full_name")
email = parsed_json.get("email", None)
avatar_url = parsed_json.get("avatar_url", None)
return GogsUser(user_id=user_id, username=username, full_name=full_name,
email=email, avatar_url=avatar_url)
@property # named user_id to avoid conflict with built-in id
def user_id(self):
"""
The user's id
:rtype: int
"""
return self._id
@property
def username(self):
"""
The user's username
:rtype: str
"""
return self._username
@property
def full_name(self):
"""
The user's full name
:rtype: str
"""
return self._full_name
@property
def email(self):
"""
The user's email address. Can be empty as a result of invalid authentication
:rtype: str
"""
return self._email
@property
def avatar_url(self):
"""
The user's avatar URL
:rtype: str
"""
return self._avatar_url
class GogsRepo(object):
"""
An immutable representation of a Gogs repository
"""
def __init__(self, repo_id, owner, full_name, private, fork, urls, permissions):
self._repo_id = repo_id
self._owner = owner
self._full_name = full_name
self._private = private
self._fork = fork
self._urls = urls
self._permissions = permissions
@staticmethod
def from_json(parsed_json):
repo_id = json_get(parsed_json, "id")
owner = GogsUser.from_json(json_get(parsed_json, "owner"))
full_name = json_get(parsed_json, "full_name")
private = json_get(parsed_json, "private")
fork = json_get(parsed_json, "fork")
urls = GogsRepo.Urls(json_get(parsed_json, "html_url"), json_get(parsed_json, "clone_url"),
json_get(parsed_json, "ssh_url"))
permissions = GogsRepo.Permissions.from_json(json_get(parsed_json, "permissions"))
return GogsRepo(repo_id=repo_id, owner=owner, full_name=full_name, private=private, fork=fork,
urls=urls, permissions=permissions)
@property # named repo_id to avoid conflict with built-in id
def repo_id(self):
"""
The repository's id
:rtype: int
"""
return self._repo_id
@property
def owner(self):
"""
The owner of the repository
:rtype: entities.GogsUser
"""
return self._owner
@property
def full_name(self):
"""
The full name of the repository
:rtype: str
"""
return self._full_name
@property
def private(self):
"""
Whether the repository is private
:rtype: bool
"""
return self._private
@property
def fork(self):
"""
Whether the repository is a fork
:rtype: bool
"""
return self._fork
@property
def urls(self):
"""
URLs of the repository
:rtype: GogsRepo.Urls
"""
return self._urls
@property
def permissions(self):
"""
Permissions for the repository
:rtype: GogsRepo.Permissions
"""
return self._permissions
class Urls(object):
def __init__(self, html_url, clone_url, ssh_url):
self._html_url = html_url
self._clone_url = clone_url
self._ssh_url = ssh_url
@property
def html_url(self):
"""
URL for the repository's webpage
:rtype: str
"""
return self._html_url
@property
def clone_url(self):
"""
URL for cloning the repository (via HTTP)
:rtype: str
"""
return self._clone_url
@property
def ssh_url(self):
"""
URL for cloning the repository via SSH
:rtype: str
"""
return self._ssh_url
class Permissions(object):
def __init__(self, admin, push, pull):
self._admin = admin
self._push = push
self._pull = pull
@staticmethod
def from_json(parsed_json):
admin = parsed_json.get("admin", False)
push = parsed_json.get("push", False)
pull = parsed_json.get("pull", False)
return GogsRepo.Permissions(admin, push, pull)
@property
def admin(self):
"""
Whether the user that requested this repository has admin permissions
:rtype: bool
"""
return self._admin
@property
def push(self):
"""
Whether the user that requested this repository has push permissions
:rtype: bool
"""
return self._push
@property
def pull(self):
"""
Whether the user that requested this repository has pull permissions
:rtype: bool
"""
return self._pull
class Hook(object):
def __init__(self, hook_id, hook_type, events, active, config):
self._id = hook_id
self._type = hook_type
self._events = events
self._active = active
self._config = config
@staticmethod
def from_json(parsed_json):
hook_id = json_get(parsed_json, "id")
hook_type = json_get(parsed_json, "type")
events = json_get(parsed_json, "events")
active = json_get(parsed_json, "active")
config = json_get(parsed_json, "config")
return GogsRepo.Hook(hook_id=hook_id, hook_type=hook_type, events=events, active=active,
config=config)
@property # named hook_id to avoid conflict with built-in id
def hook_id(self):
"""
The hook's id number
:rtype: int
"""
return self._id
@property # named hook_type to avoid conflict with built-in type
def hook_type(self):
"""
The hook's type (gogs, slack, etc.)
:rtype: str
"""
return self._type
@property
def events(self):
"""
The events that fire the hook
:rtype: List[str]
"""
return self._events
@property
def active(self):
"""
Whether the hook is active
:rtype: bool
"""
return self._active
@property
def config(self):
"""
Config of the hook. Possible keys include ``"content_type"``, ``"url"``, ``"secret"``
:rtype: dict
"""
return self._config
class DeployKey(object):
def __init__(self, key_id, key, url, title, created_at, read_only):
self._id = key_id
self._key = key
self._url = url
self._title = title
self._created_at = created_at
self._read_only = read_only
@staticmethod
def from_json(parsed_json):
key_id = json_get(parsed_json, "id")
key = json_get(parsed_json, "key")
url = json_get(parsed_json, "url")
title = json_get(parsed_json, "title")
created_at = json_get(parsed_json, "created_at")
read_only = json_get(parsed_json, "read_only")
return GogsRepo.DeployKey(key_id=key_id, key=key, url=url,
title=title, created_at=created_at, read_only=read_only)
@property # named key_id to avoid conflict with built-in id
def key_id(self):
"""
The key's id number
:rtype: int
"""
return self._id
@property
def key(self):
"""
The content of the key
:rtype: str
"""
return self._key
@property
def url(self):
"""
URL where the key can be found
:rtype: str
"""
return self._url
@property
def title(self):
"""
The name of the key
:rtype: str
"""
return self._title
@property
def created_at(self):
"""
Creation date of the key
:rtype: str
"""
return self._created_at
@property
def read_only(self):
"""
Whether key is read-only
:rtype: bool
"""
return self._read_only
class GogsOrg(object):
"""
An immutable representation of a Gogs Organization
"""
def __init__(self, org_id, username, full_name, avatar_url, description, website, location):
self._id = org_id
self._username = username
self._full_name = full_name
self._avatar_url = avatar_url
self._description = description
self._website = website
self._location = location
@staticmethod
def from_json(parsed_json):
org_id = json_get(parsed_json, "id")
username = json_get(parsed_json, "username")
full_name = json_get(parsed_json, "full_name")
avatar_url = json_get(parsed_json, "avatar_url")
description = json_get(parsed_json, "description")
website = json_get(parsed_json, "website")
location = json_get(parsed_json, "location")
return GogsOrg(org_id=org_id, username=username, full_name=full_name,
avatar_url=avatar_url, description=description,
website=website, location=location)
@property # named org_id to avoid conflict with built-in id
def org_id(self):
"""
The organization's id
:rtype: int
"""
return self._id
@property
def username(self):
"""
Organization's username
:rtype: str
"""
return self._username
@property
def full_name(self):
"""
Organization's full name
:rtype: str
"""
return self._full_name
@property
def avatar_url(self):
"""
Organization's avatar url
:rtype: str
"""
return self._avatar_url
@property
def description(self):
"""
Organization's description
:rtype: str
"""
return self._description
@property
def website(self):
"""
Organization's website address
:rtype: str
"""
return self._website
@property
def location(self):
"""
Organization's location
:rtype: str
"""
return self._location
class GogsTeam(object):
"""
An immutable representation of a Gogs organization team
"""
def __init__(self, team_id, name, description, permission):
self._id = team_id
self._name = name
self._description = description
self._permission = permission
@staticmethod
def from_json(parsed_json):
team_id = json_get(parsed_json, "id")
name = json_get(parsed_json, "name")
description = json_get(parsed_json, "description")
permission = json_get(parsed_json, "permission")
return GogsTeam(team_id=team_id, name=name, description=description, permission=permission)
@property # named team_id to avoid conflict with built-in id
def team_id(self):
"""
Team's id
:rtype: int
"""
return self._id
@property
def name(self):
"""
Team name
:rtype: str
"""
return self._name
@property
def description(self):
"""
Description of the team
:rtype: str
"""
return self._description
@property
def permission(self):
"""
Team permission, can be read, write or admin, default is read
:rtype: int
"""
return self._permission