This repository was archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy path__init__.py
More file actions
2215 lines (1803 loc) · 75.4 KB
/
__init__.py
File metadata and controls
2215 lines (1803 loc) · 75.4 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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
pyapi-gitlab, a gitlab python wrapper for the gitlab API
by Itxaka Serrano Garcia <itxakaserrano@gmail.com>
Check the license on the LICENSE file
"""
import requests
from . import exceptions
from .session import Session
from .users import Users
from .keys import Keys
from .helper import deprecated, format_string
class Gitlab(Session, Users, Keys):
"""
Gitlab class
On init we setup the token used for all the api calls and all the urls
:param host: host of gitlab
:param token: token
:param verify_ssl: Weather or not to verify the SSL cert
:param auth: Authentication
:param timeout: Timeout
:param suppress_http_error: Use :obj:`False` to unsuppress
:class:`requests.exceptions.HTTPError` exceptions on failure
:return: None
"""
def setsudo(self, user=None):
"""
Set the subsequent API calls to the user provided
:param user: User id or username to change to, None to return to the logged user
:return: Nothing
"""
if user is None:
try:
self.headers.pop('SUDO')
except KeyError:
pass
else:
self.headers['SUDO'] = user
def getprojectsowned(self, page=1, per_page=20):
"""
Returns a dictionary of all the projects for the current user
:return: list with the repo name, description, last activity, web url, ssh url, owner and if its public
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/owned'.format(self.projects_url), params=data, headers=self.headers,
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def get_project(self, project):
"""
Get info for a project identified by id or namespace/project_name
:param project: The ID or URL-encoded path of the project
:return: Dictionary containing the Project
:raise: HttpError: If invalid response returned
"""
project = format_string(project)
return self.get(
'/projects/{project}'.format(project=project))
def getproject(self, project_id):
"""Get info for a project identified by id or namespace/project_name
:param project_id: id or namespace/project_name of the project
:return: False if not found, a dictionary if found
"""
return self.get_project(project_id)
def getprojectevents(self, project_id, page=1, per_page=20):
"""
Get the project identified by id, events(commits)
:param project_id: id of the project
:return: False if no project with that id, a dictionary with the events if found
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/{1}/events'.format(self.projects_url, project_id), params=data,
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def createproject(self, name, **kwargs):
"""
Creates a new project owned by the authenticated user.
:param name: new project name
:param path: custom repository name for new project. By default generated based on name
:param namespace_id: namespace for the new project (defaults to user)
:param description: short project description
:param issues_enabled:
:param merge_requests_enabled:
:param wiki_enabled:
:param snippets_enabled:
:param public: if true same as setting visibility_level = 20
:param visibility_level:
:param sudo:
:param import_url:
:return:
"""
data = {'name': name}
if kwargs:
data.update(kwargs)
request = requests.post(
self.projects_url, headers=self.headers, data=data,
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
elif request.status_code == 403:
if 'Your own projects limit is 0' in request.text:
print(request.text)
return False
else:
return False
def editproject(self, project_id, **kwargs):
"""
Edit an existing project.
:param name: new project name
:param path: custom repository name for new project. By default generated based on name
:param default_branch: they default branch
:param description: short project description
:param issues_enabled:
:param merge_requests_enabled:
:param wiki_enabled:
:param snippets_enabled:
:param public: if true same as setting visibility_level = 20
:param visibility_level:
:return:
"""
data = {"id": project_id}
if kwargs:
data.update(kwargs)
request = requests.put(
'{0}/{1}'.format(self.projects_url, project_id), headers=self.headers,
data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
elif request.status_code == 400:
if "Your param's are invalid" in request.text:
print(request.text)
return False
else:
return False
def shareproject(self, project_id, group_id, group_access):
"""
Allow to share project with group.
:param project_id: The ID of a project
:param group_id: The ID of a group
:param group_access: Level of permissions for sharing
:return: True is success
"""
data = {'id': project_id, 'group_id': group_id, 'group_access': group_access}
request = requests.post(
'{0}/{1}/share'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl)
return request.status_code == 201
def delete_project(self, id):
"""
Delete a project from the Gitlab server
Gitlab currently returns a Boolean True if the deleted and as such we return an
empty Dictionary
:param id: The ID of the project or NAMESPACE/PROJECT_NAME
:return: Dictionary
:raise: HttpError: If invalid response returned
"""
url = '/projects/{id}'.format(id=id)
response = self.delete(url)
if response is True:
return {}
else:
return response
@deprecated
def deleteproject(self, project_id):
"""
Delete a project
.. warning:: Warning this is being deprecated please use :func:`gitlab.Gitlab.delete_project`
:param project_id: project id
:type project_id: int
:return: always true
"""
self.delete_project(project_id)
return True
def createprojectuser(self, user_id, name, **kwargs):
"""
Creates a new project owned by the specified user. Available only for admins.
:param user_id: user_id of owner
:param name: new project name
:param description: short project description
:param default_branch: 'master' by default
:param issues_enabled:
:param merge_requests_enabled:
:param wiki_enabled:
:param snippets_enabled:
:param public: if true same as setting visibility_level = 20
:param visibility_level:
:param import_url:
:param sudo:
:return:
"""
data = {'name': name}
if kwargs:
data.update(kwargs)
request = requests.post(
'{0}/user/{1}'.format(self.projects_url, user_id), headers=self.headers,
data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return True
else:
return False
def getprojectmembers(self, project_id, query=None, page=1, per_page=20):
"""
Lists the members of a given project id
:param project_id: the project id
:param query: Optional search query
:param page: Which page to return (default is 1)
:param per_page: Number of items to return per page (default is 20)
:return: the projects members, false if there is an error
"""
data = {'page': page, 'per_page': per_page}
if query:
data['query'] = query
request = requests.get(
'{0}/{1}/members'.format(self.projects_url, project_id),
params=data, headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def addprojectmember(self, project_id, user_id, access_level):
"""Adds a project member to a project
:param project_id: project id
:param user_id: user id
:param access_level: access level, see gitlab help to know more
:return: True if success
"""
# if isinstance(access_level, basestring):
if access_level.lower() == 'master':
access_level = 40
elif access_level.lower() == 'developer':
access_level = 30
elif access_level.lower() == 'reporter':
access_level = 20
else:
access_level = 10
data = {'id': project_id, 'user_id': user_id, 'access_level': access_level}
request = requests.post(
'{0}/{1}/members'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return True
else:
return False
def editprojectmember(self, project_id, user_id, access_level):
"""Edit a project member
:param project_id: project id
:param user_id: user id
:param access_level: access level
:return: True if success
"""
if access_level.lower() == 'master':
access_level = 40
elif access_level.lower() == 'developer':
access_level = 30
elif access_level.lower() == 'reporter':
access_level = 20
else:
access_level = 10
data = {'id': project_id, 'user_id': user_id, 'access_level': access_level}
request = requests.put(
'{0}/{1}/members/{2}'.format(self.projects_url, project_id, user_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def deleteprojectmember(self, project_id, user_id):
"""Delete a project member
:param project_id: project id
:param user_id: user id
:return: always true
"""
request = requests.delete(
'{0}/{1}/members/{2}'.format(self.projects_url, project_id, user_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True # It always returns true
def getprojecthooks(self, project_id, page=1, per_page=20):
"""Get all the hooks from a project
:param project_id: project id
:param page: Page number
:param per_page: Records per page
:return: the hooks
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/{1}/hooks'.format(self.projects_url, project_id), params=data,
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getprojecthook(self, project_id, hook_id):
"""Get a particular hook from a project
:param project_id: project id
:param hook_id: hook id
:return: the hook
"""
request = requests.get(
'{0}/{1}/hooks/{2}'.format(self.projects_url, project_id, hook_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def addprojecthook(self, project_id, url, push=False, issues=False, merge_requests=False, tag_push=False):
"""
add a hook to a project
:param project_id: project id
:param url: url of the hook
:return: True if success
"""
data = {
'id': project_id,
'url': url,
'push_events': int(bool(push)),
'issues_events': int(bool(issues)),
'merge_requests_events': int(bool(merge_requests)),
'tag_push_events': int(bool(tag_push)),
}
request = requests.post(
'{0}/{1}/hooks'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
else:
return False
def editprojecthook(self, project_id, hook_id, url, push=False, issues=False, merge_requests=False, tag_push=False):
"""
edit an existing hook from a project
:param id_: project id
:param hook_id: hook id
:param url: the new url
:return: True if success
"""
data = {
"id": project_id,
"hook_id": hook_id,
"url": url,
'push_events': int(bool(push)),
'issues_events': int(bool(issues)),
'merge_requests_events': int(bool(merge_requests)),
'tag_push_events': int(bool(tag_push)),
}
request = requests.put(
'{0}/{1}/hooks/{2}'.format(self.projects_url, project_id, hook_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def deleteprojecthook(self, project_id, hook_id):
"""
Delete a project hook
:param project_id: project id
:param hook_id: hook id
:return: True if success
"""
request = requests.delete(
'{0}/{1}/hooks/{2}'.format(self.projects_url, project_id, hook_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def getsystemhooks(self, page=1, per_page=20):
"""
Get all system hooks
:param page: Page number
:param per_page: Records per page
:return: list of hooks
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
self.hook_url, params=data, headers=self.headers,
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def addsystemhook(self, url):
"""
Add a system hook
:param url: url of the hook
:return: True if success
"""
data = {"url": url}
request = requests.post(
self.hook_url, headers=self.headers, data=data,
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return True
else:
return False
def testsystemhook(self, hook_id):
"""
Test a system hook
:param hook_id: hook id
:return: list of hooks
"""
data = {"id": hook_id}
request = requests.get(
self.hook_url, data=data, headers=self.headers,
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def deletesystemhook(self, hook_id):
"""
Delete a project hook
:param hook_id: hook id
:return: True if success
"""
data = {"id": hook_id}
request = requests.delete(
'{0}/{1}'.format(self.hook_url, hook_id), data=data,
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def getbranches(self, project_id):
"""
List all the branches from a project
:param project_id: project id
:return: the branches
"""
request = requests.get(
'{0}/{1}/repository/branches'.format(self.projects_url, project_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getbranch(self, project_id, branch):
"""
List one branch from a project
:param project_id: project id
:param branch: branch id
:return: the branch
"""
request = requests.get(
'{0}/{1}/repository/branches/{2}'.format(self.projects_url, project_id, branch),
headers=self.headers, verify=self.verify_ssl,
auth=self.auth, timeout=self.timeout
)
if request.status_code == 200:
return request.json()
else:
return False
def createbranch(self, project_id, branch, ref):
"""
Create branch from commit SHA or existing branch
:param project_id: The ID of a project
:param branch: The name of the branch
:param ref: Create branch from commit SHA or existing branch
:return: True if success, False if not
"""
data = {"id": project_id, "branch_name": branch, "ref": ref}
request = requests.post(
'{0}/{1}/repository/branches'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
else:
return False
def deletebranch(self, project_id, branch):
"""
Delete branch by name
:param project_id: The ID of a project
:param branch: The name of the branch
:return: True if success, False if not
"""
request = requests.delete(
'{0}/{1}/repository/branches/{2}'.format(self.projects_url, project_id, branch),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def protectbranch(self, project_id, branch):
"""
Protect a branch from changes
:param project_id: project id
:param branch: branch id
:return: True if success
"""
request = requests.put(
'{0}/{1}/repository/branches/{2}/protect'.format(self.projects_url, project_id, branch),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def unprotectbranch(self, project_id, branch):
"""
Stop protecting a branch
:param project_id: project id
:param branch: branch id
:return: true if success
"""
request = requests.put(
'{0}/{1}/repository/branches/{2}/unprotect'.format(self.projects_url, project_id, branch),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def createforkrelation(self, project_id, from_project_id):
"""
Create a fork relation.
This DO NOT create a fork but only adds a link as fork the relation between 2 repositories
:param project_id: project id
:param from_project_id: from id
:return: true if success
"""
data = {'id': project_id, 'forked_from_id': from_project_id}
request = requests.post(
'{0}/{1}/fork/{2}'.format(self.projects_url, project_id, from_project_id),
headers=self.headers, data=data, verify=self.verify_ssl,
auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return True
else:
return False
def removeforkrelation(self, project_id):
"""
Remove an existing fork relation. this DO NOT remove the fork,only the relation between them
:param project_id: project id
:return: true if success
"""
request = requests.delete(
'{0}/{1}/fork'.format(self.projects_url, project_id),
headers=self.headers, verify=self.verify_ssl,
auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def createfork(self, project_id):
"""
Forks a project into the user namespace of the authenticated user.
:param project_id: Project ID to fork
:return: True if succeed
"""
request = requests.post(
'{0}/fork/{1}'.format(self.projects_url, project_id),
timeout=self.timeout, verify=self.verify_ssl)
if request.status_code == 200:
return True
else:
return False
def getissues(self, page=1, per_page=20):
"""
Return a global list of issues for your user.
:param page: Page number
:param per_page: Records per page
:return: list of issues
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/api/v3/issues'.format(self.host),
params=data, headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getprojectissues(self, project_id, page=1, per_page=20, **kwargs):
"""
Return a list of issues for project id.
:param: project_id: The id for the project.
:param page: Page number
:param per_page: Records per page
:param kwargs: Extra data to send
:return: list of issues
"""
kwargs['page'] = page
kwargs['per_page'] = per_page
data = kwargs
request = requests.get(
'{0}/{1}/issues'.format(self.projects_url, project_id),
params=data, headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getprojectissue(self, project_id, issue_id):
"""
Get an specific issue id from a project
:param project_id: project id
:param issue_id: issue id
:return: the issue
"""
request = requests.get(
'{0}/{1}/issues/{2}'.format(self.projects_url, project_id, issue_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def createissue(self, project_id, title, **kwargs):
"""
Create a new issue
:param project_id: project id
:param title: title of the issue
:return: dict with the issue created
"""
data = {'id': id, 'title': title}
if kwargs:
data.update(kwargs)
request = requests.post(
'{0}/{1}/issues'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
else:
return False
def editissue(self, project_id, issue_id, **kwargs):
"""
Edit an existing issue data
:param project_id: project id
:param issue_id: issue id
:return: true if success
"""
data = {'id': project_id, 'issue_id': issue_id}
if kwargs:
data.update(kwargs)
request = requests.put(
'{0}/{1}/issues/{2}'.format(self.projects_url, project_id, issue_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getmilestones(self, project_id, page=1, per_page=20):
"""
Get the milestones for a project
:param project_id: project id
:param page: Page number
:param per_page: Records per page
:return: the milestones
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/{1}/milestones'.format(self.projects_url, project_id), params=data,
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getmilestone(self, project_id, milestone_id):
"""
Get an specific milestone
:param project_id: project id
:param milestone_id: milestone id
:return: dict with the new milestone
"""
request = requests.get(
'{0}/{1}/milestones/{2}'.format(self.projects_url, project_id, milestone_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def createmilestone(self, project_id, title, **kwargs):
"""
Create a new milestone
:param project_id: project id
:param title: title
:param description: description
:param due_date: due date
:param sudo: do the request as another user
:return: dict of the new issue
"""
data = {'id': project_id, 'title': title}
if kwargs:
data.update(kwargs)
request = requests.post(
'{0}/{1}/milestones'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
else:
return False
def editmilestone(self, project_id, milestone_id, **kwargs):
"""Edit an existing milestone
:param project_id: project id
:param milestone_id: milestone id
:param title: title
:param description: description
:param due_date: due date
:param state_event: state
:param sudo: do the request as another user
:return: dict with the modified milestone
"""
data = {'id': project_id, 'milestone_id': milestone_id}
if kwargs:
data.update(kwargs)
request = requests.put(
'{0}/{1}/milestones/{2}'.format(self.projects_url, project_id, milestone_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def getmilestoneissues(self, project_id, milestone_id, page=1, per_page=20):
"""
Get the issues associated with a milestone
:param project_id: project id
:param milestone_id: milestone id
:return: list of issues
"""
data = {'page': page, 'per_page': per_page}
request = requests.get(
'{0}/{1}/milestones/{2}/issues'.format(self.projects_url, project_id, milestone_id),
params=data, headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def get_all_deploy_keys(self):
"""
Get a list of all deploy keys across all projects of the GitLab instance.
This endpoint requires admin access.
>>> gitlab = Gitlab(host='http://localhost:10080', verify_ssl=False)
>>> gitlab.login(user='root', password='5iveL!fe')
>>> gitlab.get_all_deploy_keys()
:return: List of Dictionaries containing all deploy keys
:raise: HttpError: If invalid response returned
"""
return self.get('/deploy_keys', default_response=[])
def enable_deploy_key(self, project, key_id):
"""
Enables a deploy key for a project.
>>> gitlab = Gitlab(host='http://localhost:10080', verify_ssl=False)
>>> gitlab.login(user='root', password='5iveL!fe')
>>> gitlab.enable_deploy_key(15, 5)
:param project: The ID or URL-encoded path of the project owned by the authenticated user
:param key_id: The ID of the deploy key
:return: A dictionary containing deploy key details
:raise: HttpError: If invalid response returned
"""
url = '/projects/{project}/deploy_keys/{key_id}/enable'.format(
project=project, key_id=key_id)
return self.post(url, default_response={})
def getdeploykeys(self, project_id):
"""
Get a list of a project's deploy keys.
:param project_id: project id
:return: the keys in a dictionary if success, false if not
"""
request = requests.get(
'{0}/{1}/keys'.format(self.projects_url, project_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout
)
if request.status_code == 200:
return request.json()
else:
return False
def getdeploykey(self, project_id, key_id):
"""
Get a single key.
:param project_id: project id
:param key_id: key id
:return: the key in a dict if success, false if not
"""
request = requests.get(
'{0}/{1}/keys/{2}'.format(self.projects_url, project_id, key_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return request.json()
else:
return False
def adddeploykey(self, project_id, title, key):
"""
Creates a new deploy key for a project.
:param project_id: project id
:param title: title of the key
:param key: the key itself
:return: true if success, false if not
"""
data = {'id': project_id, 'title': title, 'key': key}
request = requests.post(
'{0}/{1}/keys'.format(self.projects_url, project_id),
headers=self.headers, data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 201:
return request.json()
else:
return False
def deletedeploykey(self, project_id, key_id):
"""
Delete a deploy key from a project
:param project_id: project id
:param key_id: key id to delete
:return: true if success, false if not
"""
request = requests.delete(
'{0}/{1}/keys/{2}'.format(self.projects_url, project_id, key_id),
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
if request.status_code == 200:
return True
else:
return False
def creategroup(self, name, path, **kwargs):
"""
Creates a new group