forked from atlassian-api/atlassian-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
3652 lines (3356 loc) · 134 KB
/
__init__.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
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
import logging
from enum import Enum
from deprecated import deprecated
from requests import HTTPError
from .base import BitbucketBase
from atlassian.bitbucket.cloud import Cloud
log = logging.getLogger(__name__)
class MergeStrategy(Enum):
"""
Merge strategies used by the merge_pull_request method.
"""
MERGE_COMMIT = "merge_commit"
SQUASH = "squash"
FAST_FORWARD = "fast_forward"
class Bitbucket(BitbucketBase):
def __init__(self, url, *args, **kwargs):
if "cloud" not in kwargs and ("bitbucket.org" in url):
kwargs["cloud"] = True
if "api_version" not in kwargs:
kwargs["api_version"] = "2.0" if "cloud" in kwargs and kwargs["cloud"] else "1.0"
if "cloud" in kwargs:
kwargs["api_root"] = "" if "api.bitbucket.org" in url else "rest/api"
super(Bitbucket, self).__init__(url, *args, **kwargs)
def markup_preview(self, data):
"""
Preview generated HTML for the given markdown content.
Only authenticated users may call this resource.
:param data:
:return:
"""
url = self.resource_url("markup/preview")
return self.post(url, data=data)
################################################################################################
# Administrative functions
################################################################################################
def _url_admin(self, api_version=None):
return self.resource_url("admin", api_version=api_version)
def get_groups(self, group_filter=None, limit=25, start=0):
"""
Get list of bitbucket groups.
Use 'group_filter' for get specific group or get all group if necessary.
:param group_filter: str - groupname
:param limit: int - paginated limit to retrieve
:param start: int - paginated point to start retrieving
:return: The collection as JSON with all relevant information about the group
"""
url = self.resource_url("groups", api_version="1.0")
params = {}
if group_filter:
params["filter"] = group_filter
if limit:
params["limit"] = limit
if start:
params["start"] = start
return self._get_paged(url, params=params)
def group_members(self, group, start=0, limit=None):
"""
Get group of members
:param group: The group name to query
:param start:
:param limit:
:return: A list of group members
"""
url = "{}/groups/more-members".format(self._url_admin())
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if group:
params["context"] = group
return self._get_paged(url, params=params)
def all_project_administrators(self):
"""
Get the list of project administrators
:return: A generator object containing a map with the project_key, project_name and project_administrators
"""
for project in self.project_list():
log.info("Processing project: %s - %s", project.get("key"), project.get("name"))
yield {
"project_key": project.get("key"),
"project_name": project.get("name"),
"project_administrators": [
{"email": x["emailAddress"], "name": x["displayName"]}
for x in self.project_users_with_administrator_permissions(project["key"])
],
}
def reindex(self):
"""
Rebuild the bundled Elasticsearch indexes for Bitbucket Server
:return:
"""
url = self.resource_url("sync", api_root="rest/indexing", api_version="latest")
return self.post(url)
def check_reindexing_status(self):
"""
Check reindexing status
:return:
"""
url = self.resource_url("status", api_root="rest/indexing", api_version="latest")
return self.get(url)
def get_users(self, user_filter=None, limit=25, start=0):
"""
Get list of bitbucket users.
Use 'user_filter' for get specific users or get all users if necessary.
:param user_filter: str - username, displayname or email
:param limit: int - paginated limit to retrieve
:param start: int - paginated point to start retreiving
:return: The collection as JSON with all relevant information about the licensed user
"""
url = self.resource_url("users", api_version="1.0")
params = {}
if user_filter:
params["filter"] = user_filter
if limit:
params["limit"] = limit
if start:
params["start"] = start
return self.get(url, params=params)
def get_users_info(self, user_filter=None, start=0, limit=25):
"""
The authenticated user must have the LICENSED_USER permission to call this resource.
:param user_filter: if specified only users with usernames, display name or email addresses
containing the supplied string will be returned
:param limit:
:param start:
:return:
"""
url = "{}/users".format(self._url_admin(api_version="1.0"))
params = {}
if limit:
params["limit"] = limit
if start:
params["start"] = start
if user_filter:
params["filter"] = user_filter
return self._get_paged(url, params=params)
def get_current_license(self):
"""
Retrieves details about the current license, as well as the current status of the system with
regard to the installed license. The status includes the current number of users applied
toward the license limit, as well as any status messages about the license (warnings about expiry
or user counts exceeding license limits).
The authenticated user must have ADMIN permission. Unauthenticated users, and non-administrators,
are not permitted to access license details.
:return:
"""
url = "{}/license".format(self._url_admin())
return self.get(url)
def _url_mail_server(self):
return "{}/mail-server".format(self._url_admin())
def get_mail_configuration(self):
"""
Retrieves the current mail configuration.
The authenticated user must have the SYS_ADMIN permission to call this resource.
:return:
"""
url = self._url_mail_server()
return self.get(url)
def _url_mail_server_sender_address(self):
return "{}/sender-address".format(self._url_mail_server())
def get_mail_sender_address(self):
"""
Retrieves the server email address
:return:
"""
url = self._url_mail_server_sender_address()
return self.get(url)
def remove_mail_sender_address(self):
"""
Clears the server email address.
The authenticated user must have the ADMIN permission to call this resource.
:return:
"""
url = self._url_mail_server_sender_address()
return self.delete(url)
def get_ssh_settings(self):
"""
Retrieve ssh settings for user
:return:
"""
url = self.resource_url("settings", api_root="rest/ssh")
return self.get(url)
def health_check(self):
"""
Get health status
https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html
:return:
"""
# check as Troubleshooting & Support Tools Plugin
response = self.get("rest/troubleshooting/1.0/check/")
if not response:
# check as support tools
response = self.get("rest/supportHealthCheck/1.0/check/")
return response
def get_associated_build_statuses(self, commit):
"""
To get the build statuses associated with a commit.
:commit: str- commit id
:return:
"""
url = self.resource_url(
"commits/{commitId}".format(commitId=commit),
api_root="rest/build-status",
)
return self.get(url)
def _url_announcement_banner(self):
return "{}/banner".format(self._url_admin())
def get_announcement_banner(self):
"""
Gets the announcement banner, if one exists and is available to the user
:return:
"""
url = self._url_announcement_banner()
return self.get(url)
def set_announcement_banner(self, body):
"""
Sets the announcement banner with the provided JSON.
Only users authenticated as Admins may call this resource
:param body
{
"id": "https://docs.atlassian.com/jira/REST/schema/rest-announcement-banner#",
"title": "Rest Announcement Banner",
"type": "object"
}
:return:
"""
url = self._url_announcement_banner()
return self.put(url, data=body)
def delete_announcement_banner(self):
"""
Gets the announcement banner, if one exists and is available to the user
:return:
"""
url = self._url_announcement_banner()
return self.delete(url)
def upload_plugin(self, plugin_path):
"""
Provide plugin path for upload into BitBucket e.g. useful for auto deploy
:param plugin_path:
:return:
"""
upm_token = self.request(
method="GET",
path="rest/plugins/1.0/",
headers=self.no_check_headers,
trailing=True,
).headers["upm-token"]
url = "rest/plugins/1.0/?token={}".format(upm_token)
files = {"plugin": open(plugin_path, "rb")}
return self.post(url, files=files, headers=self.no_check_headers)
def get_categories(self, project_key, repository_slug=None):
"""
Get a list of categories assigned to a project or repository.
:param project_key: The project key as shown in URL.
:param repository_slug: The repository as shown in URL (optional).
:return: If 'repository_slug', returns the list with categories of the repository,
otherwise, returns the list with the categories of the project 'project_key'
"""
url = "project/{}".format(project_key)
if repository_slug:
url = "{}/repository/{}".format(url, repository_slug)
url = self.resource_url(url, api_root="rest/categories", api_version="latest")
data = self.get(url)
return data.get("result").get("categories")
################################################################################################
# Functions related to projects
################################################################################################
def _url_projects(self, api_root=None, api_version=None):
return self.resource_url("projects", api_root, api_version)
def project_list(self, start=0, limit=None):
"""
Provide the project list
:return: A list of projects
"""
url = self._url_projects()
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def create_project(self, key, name, description=""):
"""
Create project
:param key: The project key
:param name: The project name
:param description: The project description
:return: The value of the post request.
"""
url = self._url_projects()
data = {"key": key, "name": name, "description": description}
return self.post(url, data=data)
################################################################################################
# Functions related to a specific project
################################################################################################
def _url_project(self, project_key, api_root=None, api_version=None):
return "{}/{}".format(self._url_projects(api_root, api_version), project_key)
def project(self, key):
"""
Provide project info
:param key: The project key
:return:
"""
url = self._url_project(key)
return self.get(url) or {}
def project_exists(self, project_key):
"""
Check if project with the provided project key exists and available.
:param project_key: Key of the project where to check for repository.
:return: False is requested repository doesn't exist in the project or not accessible to the requestor
"""
exists = False
try:
self.project(project_key)
exists = True
except HTTPError as e:
if e.response.status_code in (401, 404):
pass
return exists
def update_project(self, key, **params):
"""
Update project
:param key: The project key
:return: The value of the put request.
"""
url = self._url_project(key)
return self.put(url, data=params)
def _url_project_avatar(self, project_key):
return "{}/avatar.png".format(self._url_project(project_key))
def project_summary(self, key):
"""
Get a project summary
:param key: The project key
:return: Map with the project information
"""
return {
"key": key,
"data": self.project(key),
"users": self.project_users(key),
"groups": self.project_groups(key),
"avatar": self.project_avatar(key),
}
def project_avatar(self, key, content_type="image/png"):
"""
Get project avatar
:param key: The project key
:param content_type: The content type to get
:return: Value of get request
"""
url = self._url_project_avatar(key)
headers = dict(self.default_headers)
headers["Accept"] = content_type
headers["X-Atlassian-Token"] = "no-check"
return self.get(url, not_json_response=True, headers=headers)
def set_project_avatar(self, key, icon, content_type="image/png"):
"""
Set project avatar
:param key: The Project key
:param icon: The icon file
:param content_type: The content type of icon
:return: Value of post request
"""
url = self._url_project_avatar(key)
headers = {"X-Atlassian-Token": "no-check"}
files = {"avatar": ("avatar.png", icon, content_type)}
return self.post(url, files=files, headers=headers)
def project_keys(self, key, start=0, limit=None, filter_str=None):
"""
Get SSH access keys added to the project
:param start:
:param limit:
:param key: The project key
:param filter_str: OPTIONAL: users filter string
:return: The list of SSH access keys
"""
url = "{}/ssh".format(self._url_project(key, api_root="rest/keys"))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def _url_project_users(self, project_key):
return "{}/permissions/users".format(self._url_project(project_key))
def project_users(self, key, start=0, limit=None, filter_str=None):
"""
Get users with permission in project
:param key: The project key
:param filter_str: OPTIONAL: users filter string
:param start:
:param limit:
:return: The list of project users
"""
url = self._url_project_users(key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def project_users_with_administrator_permissions(self, key):
"""
Get project administrators for project
:param key: The project key
:return: List of project administrators
"""
project_administrators = [
user["user"] for user in self.project_users(key) if user["permission"] == "PROJECT_ADMIN"
]
for group in self.project_groups_with_administrator_permissions(key):
for user in self.group_members(group):
project_administrators.append(user)
return project_administrators
def project_grant_user_permissions(self, project_key, username, permission):
"""
Grant the specified project permission to a specific user
:param project_key: The project key
:param username: username to be granted
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = self._url_project_users(project_key)
params = {"permission": permission, "name": username}
return self.put(url, params=params)
def project_remove_user_permissions(self, project_key, username):
"""
Revoke all permissions for the specified project for a user.
The authenticated user must have PROJECT_ADMIN permission for
the specified project or a higher global permission to call this resource.
In addition, a user may not revoke their own project permissions if they do not have a higher global permission.
:param project_key: The project key
:param username: username to be granted
:return:
"""
url = self._url_project_users(project_key)
params = {"name": username}
return self.delete(url, params=params)
def _url_project_groups(self, project_key):
return "{}/permissions/groups".format(self._url_project(project_key))
def project_groups(self, key, start=0, limit=None, filter_str=None):
"""
Get Project Groups
:param limit:
:param limit:
:param start:
:param key: The project key
:param filter_str: OPTIONAL: group filter string
:return:
"""
url = self._url_project_groups(key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def project_grant_group_permissions(self, project_key, group_name, permission):
"""
Grant the specified project permission to a specific group
:param project_key: The project key
:param group_name: group to be granted
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = self._url_project_groups(project_key)
params = {"permission": permission, "name": group_name}
return self.put(url, params=params)
def project_remove_group_permissions(self, project_key, groupname):
"""
Revoke all permissions for the specified project for a group.
The authenticated user must have PROJECT_ADMIN permission for the specified project
or a higher global permission to call this resource.
In addition, a user may not revoke a group's permissions
if it will reduce their own permission level.
:param project_key: The project key
:param groupname: group to be granted
:return:
"""
url = self._url_project_groups(project_key)
params = {"name": groupname}
return self.delete(url, params=params)
def project_default_permissions(self, project_key, permission):
"""
Check if the specified permission is the default permission for a given project
:param project_key: The project key
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
return self.get(url)
def project_grant_default_permissions(self, project_key, permission):
"""
Grant the specified project permission to all users for a given project
:param project_key: The project key
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
return self.post(url, params={"allow": True})
def project_remove_default_permissions(self, project_key, permission):
"""
Revoke the specified project permission for all users for a given project
:param project_key: The project key
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
return self.post(url, params={"allow": False})
def _url_project_repo_hook_settings(self, project_key):
return "{}/settings/hooks".format(self._url_project(project_key))
def all_project_repo_hook_settings(self, project_key, start=0, limit=None, filter_type=None):
"""
Get all repository hooks for a given project
:param project_key: The project key
:param start:
:param limit: OPTIONAL: The limit of the number of changes to return, this may be restricted by
fixed system limits. Default by built-in method: None
:param filter_type: OPTIONAL: PRE_RECEIVE|POST_RECEIVE if present,
controls how repository hooks should be filtered.
:return:
"""
url = self._url_project_repo_hook_settings(project_key)
params = {}
if filter_type:
params["type"] = filter_type
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params)
def get_project_repo_hook_settings(self, project_key, hook_key):
"""
Get a repository hook from a given project
:param project_key: The project key
:param hook_key: The repository hook key
:return:
"""
url = "{}/{}".format(self._url_project_repo_hook_settings(project_key), hook_key)
return self.get(url)
def enable_project_repo_hook_settings(self, project_key, hook_key):
"""
Enable a repository hook for a given project
:param project_key: The project key
:param hook_key: The repository hook key
:return:
"""
url = "{}/{}/enabled".format(self._url_project_repo_hook_settings(project_key), hook_key)
return self.put(url)
def disable_project_repo_hook_settings(self, project_key, hook_key):
"""
Disable a repository hook for a given project
:param project_key: The project key
:param hook_key: The repository hook key
:return:
"""
url = "{}/{}/enabled".format(self._url_project_repo_hook_settings(project_key), hook_key)
return self.delete(url)
def _url_project_conditions(self, project_key):
return "{}/conditions".format(
self._url_project(
project_key,
api_root="rest/default-reviewers",
api_version="1.0",
)
)
def get_project_conditions(self, project_key):
"""
Request type: GET
Return a page of defaults conditions with reviewers list that have been configured for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264904368
:projectKey: str
:return:
"""
url = self._url_project_conditions(project_key)
return self.get(url) or {}
def _url_project_condition(self, project_key, id_condition=None):
url = "{}/condition".format(
self._url_project(
project_key,
api_root="rest/default-reviewers",
api_version="1.0",
)
)
if id_condition is not None:
url += "/{}".format(id_condition)
return url
def get_project_condition(self, project_key, id_condition):
"""
Request type: GET
Return a specific condition with reviewers list that has been configured for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264901504
:projectKey: str - project key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.get(url) or {}
def create_project_condition(self, project_key, condition):
"""
Request type: POST
Create a new condition for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264893584
:projectKey: str- project key involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_project_condition(project_key)
return self.post(url, data=condition) or {}
def update_project_condition(self, project_key, condition, id_condition):
"""
Request type: PUT
Update a new condition for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
:projectKey: str- project key involved
:idCondition: int - condition id involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.put(url, data=condition) or {}
def delete_project_condition(self, project_key, id_condition):
"""
Delete a specific condition for this repository slug inside project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264896304
Request type: DELETE
:projectKey: str- project key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.delete(url) or {}
def _url_project_audit_log(self, project_key):
if self.cloud:
raise Exception("Not supported in Bitbucket Cloud")
return "{}/events".format(self._url_project(project_key, api_root="rest/audit"))
def get_project_audit_log(self, project_key, start=0, limit=None):
"""
Get the audit log of the project
:param start:
:param limit:
:param project_key: The project key
:return: List of events of the audit log
"""
url = self._url_project_audit_log(project_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def _url_repos(self, project_key, api_root=None, api_version=None):
return "{}/repos".format(self._url_project(project_key, api_root, api_version))
def repo_list(self, project_key, start=0, limit=25):
"""
Get repositories list from project
:param project_key: The project key
:param start:
:param limit:
:return:
"""
url = self._url_repos(project_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def repo_all_list(self, project_key):
"""
Get all repositories list from project
:param project_key:
:return:
"""
return self.repo_list(project_key, limit=None)
def create_repo(self, project_key, repository_slug, forkable=False, is_private=True):
"""Create a new repository.
Requires an existing project in which this repository will be created. The only parameters which will be used
are name and scmId.
The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.
:param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
:type project_key: str
:param repository_slug: Name of repository to create (i.e. "My repo").
:param forkable: Set the repository to be forkable or not.
:type forkable: bool
:param is_private: Set the repository to be private or not.
:type is_private: bool
:return:
201 - application/json (repository)
400 - application/json (errors)
401 - application/json (errors)
409 - application/json (errors)
:rtype: requests.Response
"""
url = self._url_repos(project_key)
data = {
"name": repository_slug,
"scmId": "git",
"forkable": forkable,
"is_private": is_private,
}
return self.post(url, data=data)
################################################################################################
# Functions related to a specific repository
################################################################################################
def _url_repo(self, project_key, repo, api_root=None, api_version=None):
return "{}/{}".format(self._url_repos(project_key, api_root, api_version), repo)
def reindex_repo(self, project_key, repository_slug):
"""
Reindex repo
:param project_key:
:param repository_slug:
:return:
"""
url = "{urlRepo}/sync".format(
urlRepo=self._url_repo(
project_key,
repository_slug,
api_root="rest/indexing",
api_version="1.0",
)
)
return self.post(url)
def reindex_repo_dev_panel(self, project_key, repository_slug):
"""
Reindex all the Jira issues related to this repository_slug, including branches and pull requests.
This automatically happens as part of an upgrade, and calling this manually should only be required
if something unforeseen happens and the index becomes out of sync.
The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
:param project_key:
:param repository_slug:
:return:
"""
url = "{}/reindex".format(self._url_repo(project_key, repository_slug, api_root="rest/jira-dev"))
return self.post(url)
def get_repo(self, project_key, repository_slug):
"""
Get a specific repository from a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: Dictionary of request response
"""
url = self._url_repo(project_key, repository_slug)
return self.get(url)
def repo_exists(self, project_key, repository_slug):
"""
Check if given combination of project and repository exists and available.
:param project_key: Key of the project where to check for repository.
:param repository_slug: url-compatible repository identifier to look for.
:return: False is requested repository doesn't exist in the project or not accessible to the requestor
"""
exists = False
try:
self.get_repo(project_key, repository_slug)
exists = True
except HTTPError as e:
if e.response.status_code in (401, 404):
pass
return exists
def update_repo(self, project_key, repository_slug, **params):
"""
Update a repository in a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: The value of the put request.
"""
url = self._url_repo(project_key, repository_slug)
return self.put(url, data=params)
def delete_repo(self, project_key, repository_slug):
"""
Delete a specific repository from a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: Dictionary of request response
"""
url = self._url_repo(project_key, repository_slug)
return self.delete(url)
def fork_repository(self, project_key, repository_slug, new_repository_slug):
"""
Forks a repository within the same project.
:param project_key:
:param repository_slug:
:param new_repository_slug:
:return:
"""
url = self._url_repo(project_key, repository_slug)
body = {}
if new_repository_slug is not None:
body["name"] = new_repository_slug
body["project"] = {"key": project_key}
return self.post(url, data=body)
def fork_repository_new_project(
self,
project_key,
repository_slug,
new_project_key,
new_repository_slug,
):
"""
Forks a repository to a separate project.
:param project_key: Origin Project Key
:param repository_slug: Origin repository slug
:param new_project_key: Project Key of target project
:param new_repository_slug: Target Repository slug
:return:
"""
url = self._url_repo(project_key, repository_slug)
body = {}
if new_repository_slug is not None and new_project_key is not None:
body["name"] = new_repository_slug
body["project"] = {"key": new_project_key}
return self.post(url, data=body)
def repo_keys(self, project_key, repo_key, start=0, limit=None, filter_str=None):
"""
Get SSH access keys added to the repository
:param start:
:param limit:
:param project_key: The project key
:param repo_key: The repository key
:param filter_str: OPTIONAL: users filter string
:return:
"""
url = "{}/ssh".format(self._url_repo(project_key, repo_key, api_root="rest/keys"))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def _url_repo_users(self, project_key, repo):
return "{}/permissions/users".format(self._url_repo(project_key, repo))
def repo_users(self, project_key, repo_key, start=0, limit=None, filter_str=None):
"""
Get users with permission in repository
:param start:
:param limit:
:param project_key: The project key
:param repo_key: The repository key
:param filter_str: OPTIONAL: Users filter string
:return:
"""
url = self._url_repo_users(project_key, repo_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def repo_grant_user_permissions(self, project_key, repo_key, username, permission):
"""
Grant the specified repository permission to a specific user
:param project_key: The project key
:param repo_key: The repository key (slug)
:param username: username to be granted
:param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
:return:
"""
url = self._url_repo_users(project_key, repo_key)
params = {"permission": permission, "name": username}
return self.put(url, params=params)
def repo_remove_user_permissions(self, project_key, repo_key, username):