atlassian-python-api 3.41.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. atlassian/VERSION +1 -0
  2. atlassian/__init__.py +32 -0
  3. atlassian/bamboo.py +1343 -0
  4. atlassian/bitbucket/__init__.py +3572 -0
  5. atlassian/bitbucket/base.py +198 -0
  6. atlassian/bitbucket/cloud/__init__.py +24 -0
  7. atlassian/bitbucket/cloud/base.py +123 -0
  8. atlassian/bitbucket/cloud/common/__init__.py +1 -0
  9. atlassian/bitbucket/cloud/common/builds.py +85 -0
  10. atlassian/bitbucket/cloud/common/comments.py +50 -0
  11. atlassian/bitbucket/cloud/common/users.py +79 -0
  12. atlassian/bitbucket/cloud/repositories/__init__.py +437 -0
  13. atlassian/bitbucket/cloud/repositories/branchRestrictions.py +175 -0
  14. atlassian/bitbucket/cloud/repositories/commits.py +188 -0
  15. atlassian/bitbucket/cloud/repositories/defaultReviewers.py +94 -0
  16. atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py +264 -0
  17. atlassian/bitbucket/cloud/repositories/diffstat.py +92 -0
  18. atlassian/bitbucket/cloud/repositories/groupPermissions.py +81 -0
  19. atlassian/bitbucket/cloud/repositories/hooks.py +117 -0
  20. atlassian/bitbucket/cloud/repositories/issues.py +164 -0
  21. atlassian/bitbucket/cloud/repositories/pipelines.py +274 -0
  22. atlassian/bitbucket/cloud/repositories/pullRequests.py +483 -0
  23. atlassian/bitbucket/cloud/repositories/refs.py +136 -0
  24. atlassian/bitbucket/cloud/repositories/repositoryVariables.py +146 -0
  25. atlassian/bitbucket/cloud/workspaces/__init__.py +146 -0
  26. atlassian/bitbucket/cloud/workspaces/members.py +62 -0
  27. atlassian/bitbucket/cloud/workspaces/permissions.py +125 -0
  28. atlassian/bitbucket/cloud/workspaces/projects.py +215 -0
  29. atlassian/bitbucket/server/__init__.py +46 -0
  30. atlassian/bitbucket/server/base.py +63 -0
  31. atlassian/bitbucket/server/common/__init__.py +1 -0
  32. atlassian/bitbucket/server/common/permissions.py +220 -0
  33. atlassian/bitbucket/server/globalPermissions.py +198 -0
  34. atlassian/bitbucket/server/projects/__init__.py +244 -0
  35. atlassian/bitbucket/server/projects/repos/__init__.py +327 -0
  36. atlassian/cloud_admin.py +32 -0
  37. atlassian/confluence.py +3336 -0
  38. atlassian/crowd.py +266 -0
  39. atlassian/errors.py +27 -0
  40. atlassian/insight.py +678 -0
  41. atlassian/jira.py +5324 -0
  42. atlassian/marketplace.py +84 -0
  43. atlassian/portfolio.py +108 -0
  44. atlassian/request_utils.py +39 -0
  45. atlassian/rest_client.py +558 -0
  46. atlassian/service_desk.py +911 -0
  47. atlassian/statuspage.py +3133 -0
  48. atlassian/utils.py +327 -0
  49. atlassian/xray.py +632 -0
  50. atlassian_python_api-3.41.13.dist-info/LICENSE +201 -0
  51. atlassian_python_api-3.41.13.dist-info/METADATA +261 -0
  52. atlassian_python_api-3.41.13.dist-info/RECORD +54 -0
  53. atlassian_python_api-3.41.13.dist-info/WHEEL +5 -0
  54. atlassian_python_api-3.41.13.dist-info/top_level.txt +1 -0
@@ -0,0 +1,3572 @@
1
+ # coding=utf-8
2
+ import logging
3
+
4
+ from deprecated import deprecated
5
+ from requests import HTTPError
6
+
7
+ from .base import BitbucketBase
8
+ from atlassian.bitbucket.cloud import Cloud
9
+
10
+ log = logging.getLogger(__name__)
11
+
12
+
13
+ class Bitbucket(BitbucketBase):
14
+ def __init__(self, url, *args, **kwargs):
15
+ if "cloud" not in kwargs and ("bitbucket.org" in url):
16
+ kwargs["cloud"] = True
17
+ if "api_version" not in kwargs:
18
+ kwargs["api_version"] = "2.0" if "cloud" in kwargs and kwargs["cloud"] else "1.0"
19
+ if "cloud" in kwargs:
20
+ kwargs["api_root"] = "" if "api.bitbucket.org" in url else "rest/api"
21
+
22
+ super(Bitbucket, self).__init__(url, *args, **kwargs)
23
+
24
+ def markup_preview(self, data):
25
+ """
26
+ Preview generated HTML for the given markdown content.
27
+ Only authenticated users may call this resource.
28
+ :param data:
29
+ :return:
30
+ """
31
+
32
+ url = self.resource_url("markup/preview")
33
+ return self.post(url, data=data)
34
+
35
+ ################################################################################################
36
+ # Administrative functions
37
+ ################################################################################################
38
+
39
+ def _url_admin(self, api_version=None):
40
+ return self.resource_url("admin", api_version=api_version)
41
+
42
+ def get_groups(self, group_filter=None, limit=25, start=0):
43
+ """
44
+ Get list of bitbucket groups.
45
+ Use 'group_filter' for get specific group or get all group if necessary.
46
+
47
+ :param group_filter: str - groupname
48
+ :param limit: int - paginated limit to retrieve
49
+ :param start: int - paginated point to start retrieving
50
+ :return: The collection as JSON with all relevant information about the group
51
+ """
52
+ url = self.resource_url("groups", api_version="1.0")
53
+ params = {}
54
+ if group_filter:
55
+ params["filter"] = group_filter
56
+ if limit:
57
+ params["limit"] = limit
58
+ if start:
59
+ params["start"] = start
60
+ return self._get_paged(url, params=params)
61
+
62
+ def group_members(self, group, start=0, limit=None):
63
+ """
64
+ Get group of members
65
+ :param group: The group name to query
66
+ :param start:
67
+ :param limit:
68
+ :return: A list of group members
69
+ """
70
+
71
+ url = "{}/groups/more-members".format(self._url_admin())
72
+ params = {}
73
+ if start:
74
+ params["start"] = start
75
+ if limit:
76
+ params["limit"] = limit
77
+ if group:
78
+ params["context"] = group
79
+ return self._get_paged(url, params=params)
80
+
81
+ def all_project_administrators(self):
82
+ """
83
+ Get the list of project administrators
84
+
85
+ :return: A generator object containing a map with the project_key, project_name and project_administrators
86
+ """
87
+ for project in self.project_list():
88
+ log.info("Processing project: %s - %s", project.get("key"), project.get("name"))
89
+ yield {
90
+ "project_key": project.get("key"),
91
+ "project_name": project.get("name"),
92
+ "project_administrators": [
93
+ {"email": x["emailAddress"], "name": x["displayName"]}
94
+ for x in self.project_users_with_administrator_permissions(project["key"])
95
+ ],
96
+ }
97
+
98
+ def reindex(self):
99
+ """
100
+ Rebuild the bundled Elasticsearch indexes for Bitbucket Server
101
+ :return:
102
+ """
103
+ url = self.resource_url("sync", api_root="rest/indexing", api_version="latest")
104
+ return self.post(url)
105
+
106
+ def check_reindexing_status(self):
107
+ """
108
+ Check reindexing status
109
+ :return:
110
+ """
111
+ url = self.resource_url("status", api_root="rest/indexing", api_version="latest")
112
+ return self.get(url)
113
+
114
+ def get_users(self, user_filter=None, limit=25, start=0):
115
+ """
116
+ Get list of bitbucket users.
117
+ Use 'user_filter' for get specific users or get all users if necessary.
118
+ :param user_filter: str - username, displayname or email
119
+ :param limit: int - paginated limit to retrieve
120
+ :param start: int - paginated point to start retreiving
121
+ :return: The collection as JSON with all relevant information about the licensed user
122
+ """
123
+ url = self.resource_url("users", api_version="1.0")
124
+ params = {}
125
+ if user_filter:
126
+ params["filter"] = user_filter
127
+ if limit:
128
+ params["limit"] = limit
129
+ if start:
130
+ params["start"] = start
131
+ return self.get(url, params=params)
132
+
133
+ def get_users_info(self, user_filter=None, start=0, limit=25):
134
+ """
135
+ The authenticated user must have the LICENSED_USER permission to call this resource.
136
+ :param user_filter: if specified only users with usernames, display name or email addresses
137
+ containing the supplied string will be returned
138
+ :param limit:
139
+ :param start:
140
+ :return:
141
+ """
142
+ url = "{}/users".format(self._url_admin(api_version="1.0"))
143
+ params = {}
144
+ if limit:
145
+ params["limit"] = limit
146
+ if start:
147
+ params["start"] = start
148
+ if user_filter:
149
+ params["filter"] = user_filter
150
+ return self._get_paged(url, params=params)
151
+
152
+ def get_current_license(self):
153
+ """
154
+ Retrieves details about the current license, as well as the current status of the system with
155
+ regard to the installed license. The status includes the current number of users applied
156
+ toward the license limit, as well as any status messages about the license (warnings about expiry
157
+ or user counts exceeding license limits).
158
+ The authenticated user must have ADMIN permission. Unauthenticated users, and non-administrators,
159
+ are not permitted to access license details.
160
+ :return:
161
+ """
162
+ url = "{}/license".format(self._url_admin())
163
+ return self.get(url)
164
+
165
+ def _url_mail_server(self):
166
+ return "{}/mail-server".format(self._url_admin())
167
+
168
+ def get_mail_configuration(self):
169
+ """
170
+ Retrieves the current mail configuration.
171
+ The authenticated user must have the SYS_ADMIN permission to call this resource.
172
+ :return:
173
+ """
174
+ url = self._url_mail_server()
175
+ return self.get(url)
176
+
177
+ def _url_mail_server_sender_address(self):
178
+ return "{}/sender-address".format(self._url_mail_server())
179
+
180
+ def get_mail_sender_address(self):
181
+ """
182
+ Retrieves the server email address
183
+ :return:
184
+ """
185
+ url = self._url_mail_server_sender_address()
186
+ return self.get(url)
187
+
188
+ def remove_mail_sender_address(self):
189
+ """
190
+ Clears the server email address.
191
+ The authenticated user must have the ADMIN permission to call this resource.
192
+ :return:
193
+ """
194
+ url = self._url_mail_server_sender_address()
195
+ return self.delete(url)
196
+
197
+ def get_ssh_settings(self):
198
+ """
199
+ Retrieve ssh settings for user
200
+ :return:
201
+ """
202
+ url = self.resource_url("settings", api_root="rest/ssh")
203
+ return self.get(url)
204
+
205
+ def health_check(self):
206
+ """
207
+ Get health status
208
+ https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html
209
+ :return:
210
+ """
211
+ # check as Troubleshooting & Support Tools Plugin
212
+ response = self.get("rest/troubleshooting/1.0/check/")
213
+ if not response:
214
+ # check as support tools
215
+ response = self.get("rest/supportHealthCheck/1.0/check/")
216
+ return response
217
+
218
+ def get_associated_build_statuses(self, commit):
219
+ """
220
+ To get the build statuses associated with a commit.
221
+ :commit: str- commit id
222
+ :return:
223
+ """
224
+ url = self.resource_url(
225
+ "commits/{commitId}".format(commitId=commit),
226
+ api_root="rest/build-status",
227
+ )
228
+ return self.get(url)
229
+
230
+ def _url_announcement_banner(self):
231
+ return "{}/banner".format(self._url_admin())
232
+
233
+ def get_announcement_banner(self):
234
+ """
235
+ Gets the announcement banner, if one exists and is available to the user
236
+ :return:
237
+ """
238
+ url = self._url_announcement_banner()
239
+ return self.get(url)
240
+
241
+ def set_announcement_banner(self, body):
242
+ """
243
+ Sets the announcement banner with the provided JSON.
244
+ Only users authenticated as Admins may call this resource
245
+ :param body
246
+ {
247
+ "id": "https://docs.atlassian.com/jira/REST/schema/rest-announcement-banner#",
248
+ "title": "Rest Announcement Banner",
249
+ "type": "object"
250
+ }
251
+ :return:
252
+ """
253
+ url = self._url_announcement_banner()
254
+ return self.put(url, data=body)
255
+
256
+ def delete_announcement_banner(self):
257
+ """
258
+ Gets the announcement banner, if one exists and is available to the user
259
+ :return:
260
+ """
261
+ url = self._url_announcement_banner()
262
+ return self.delete(url)
263
+
264
+ def upload_plugin(self, plugin_path):
265
+ """
266
+ Provide plugin path for upload into BitBucket e.g. useful for auto deploy
267
+ :param plugin_path:
268
+ :return:
269
+ """
270
+ upm_token = self.request(
271
+ method="GET",
272
+ path="rest/plugins/1.0/",
273
+ headers=self.no_check_headers,
274
+ trailing=True,
275
+ ).headers["upm-token"]
276
+ url = "rest/plugins/1.0/?token={}".format(upm_token)
277
+ files = {"plugin": open(plugin_path, "rb")}
278
+ return self.post(url, files=files, headers=self.no_check_headers)
279
+
280
+ def get_categories(self, project_key, repository_slug=None):
281
+ """
282
+ Get a list of categories assigned to a project or repository.
283
+ :param project_key: The project key as shown in URL.
284
+ :param repository_slug: The repository as shown in URL (optional).
285
+ :return: If 'repository_slug', returns the list with categories of the repository,
286
+ otherwise, returns the list with the categories of the project 'project_key'
287
+ """
288
+ url = "project/{}".format(project_key)
289
+ if repository_slug:
290
+ url = "{}/repository/{}".format(url, repository_slug)
291
+ url = self.resource_url(url, api_root="rest/categories", api_version="latest")
292
+ data = self.get(url)
293
+ return data.get("result").get("categories")
294
+
295
+ ################################################################################################
296
+ # Functions related to projects
297
+ ################################################################################################
298
+
299
+ def _url_projects(self, api_root=None, api_version=None):
300
+ return self.resource_url("projects", api_root, api_version)
301
+
302
+ def project_list(self, start=0, limit=None):
303
+ """
304
+ Provide the project list
305
+
306
+ :return: A list of projects
307
+ """
308
+ url = self._url_projects()
309
+ params = {}
310
+ if start:
311
+ params["start"] = start
312
+ if limit:
313
+ params["limit"] = limit
314
+ return self._get_paged(url, params=params)
315
+
316
+ def create_project(self, key, name, description=""):
317
+ """
318
+ Create project
319
+ :param key: The project key
320
+ :param name: The project name
321
+ :param description: The project description
322
+
323
+ :return: The value of the post request.
324
+ """
325
+ url = self._url_projects()
326
+ data = {"key": key, "name": name, "description": description}
327
+ return self.post(url, data=data)
328
+
329
+ ################################################################################################
330
+ # Functions related to a specific project
331
+ ################################################################################################
332
+
333
+ def _url_project(self, project_key, api_root=None, api_version=None):
334
+ return "{}/{}".format(self._url_projects(api_root, api_version), project_key)
335
+
336
+ def project(self, key):
337
+ """
338
+ Provide project info
339
+ :param key: The project key
340
+ :return:
341
+ """
342
+ url = self._url_project(key)
343
+ return self.get(url) or {}
344
+
345
+ def project_exists(self, project_key):
346
+ """
347
+ Check if project with the provided project key exists and available.
348
+ :param project_key: Key of the project where to check for repository.
349
+ :return: False is requested repository doesn't exist in the project or not accessible to the requestor
350
+ """
351
+ exists = False
352
+ try:
353
+ self.project(project_key)
354
+ exists = True
355
+ except HTTPError as e:
356
+ if e.response.status_code in (401, 404):
357
+ pass
358
+ return exists
359
+
360
+ def update_project(self, key, **params):
361
+ """
362
+ Update project
363
+ :param key: The project key
364
+ :return: The value of the put request.
365
+ """
366
+ url = self._url_project(key)
367
+ return self.put(url, data=params)
368
+
369
+ def _url_project_avatar(self, project_key):
370
+ return "{}/avatar.png".format(self._url_project(project_key))
371
+
372
+ def project_summary(self, key):
373
+ """
374
+ Get a project summary
375
+ :param key: The project key
376
+
377
+ :return: Map with the project information
378
+ """
379
+ return {
380
+ "key": key,
381
+ "data": self.project(key),
382
+ "users": self.project_users(key),
383
+ "groups": self.project_groups(key),
384
+ "avatar": self.project_avatar(key),
385
+ }
386
+
387
+ def project_avatar(self, key, content_type="image/png"):
388
+ """
389
+ Get project avatar
390
+ :param key: The project key
391
+ :param content_type: The content type to get
392
+
393
+ :return: Value of get request
394
+ """
395
+ url = self._url_project_avatar(key)
396
+ headers = dict(self.default_headers)
397
+ headers["Accept"] = content_type
398
+ headers["X-Atlassian-Token"] = "no-check"
399
+
400
+ return self.get(url, not_json_response=True, headers=headers)
401
+
402
+ def set_project_avatar(self, key, icon, content_type="image/png"):
403
+ """
404
+ Set project avatar
405
+ :param key: The Project key
406
+ :param icon: The icon file
407
+ :param content_type: The content type of icon
408
+
409
+ :return: Value of post request
410
+ """
411
+ url = self._url_project_avatar(key)
412
+ headers = {"X-Atlassian-Token": "no-check"}
413
+ files = {"avatar": ("avatar.png", icon, content_type)}
414
+ return self.post(url, files=files, headers=headers)
415
+
416
+ def project_keys(self, key, start=0, limit=None, filter_str=None):
417
+ """
418
+ Get SSH access keys added to the project
419
+ :param start:
420
+ :param limit:
421
+ :param key: The project key
422
+ :param filter_str: OPTIONAL: users filter string
423
+ :return: The list of SSH access keys
424
+ """
425
+ url = "{}/ssh".format(self._url_project(key, api_root="rest/keys"))
426
+ params = {}
427
+ if start:
428
+ params["start"] = start
429
+ if limit:
430
+ params["limit"] = limit
431
+ if filter_str:
432
+ params["filter"] = filter_str
433
+ return self._get_paged(url, params=params)
434
+
435
+ def _url_project_users(self, project_key):
436
+ return "{}/permissions/users".format(self._url_project(project_key))
437
+
438
+ def project_users(self, key, start=0, limit=None, filter_str=None):
439
+ """
440
+ Get users with permission in project
441
+ :param key: The project key
442
+ :param filter_str: OPTIONAL: users filter string
443
+ :param start:
444
+ :param limit:
445
+ :return: The list of project users
446
+ """
447
+ url = self._url_project_users(key)
448
+ params = {}
449
+ if start:
450
+ params["start"] = start
451
+ if limit:
452
+ params["limit"] = limit
453
+ if filter_str:
454
+ params["filter"] = filter_str
455
+ return self._get_paged(url, params=params)
456
+
457
+ def project_users_with_administrator_permissions(self, key):
458
+ """
459
+ Get project administrators for project
460
+ :param key: The project key
461
+ :return: List of project administrators
462
+ """
463
+ project_administrators = [
464
+ user["user"] for user in self.project_users(key) if user["permission"] == "PROJECT_ADMIN"
465
+ ]
466
+ for group in self.project_groups_with_administrator_permissions(key):
467
+ for user in self.group_members(group):
468
+ project_administrators.append(user)
469
+ return project_administrators
470
+
471
+ def project_grant_user_permissions(self, project_key, username, permission):
472
+ """
473
+ Grant the specified project permission to a specific user
474
+ :param project_key: The project key
475
+ :param username: username to be granted
476
+ :param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
477
+ :return:
478
+ """
479
+ url = self._url_project_users(project_key)
480
+ params = {"permission": permission, "name": username}
481
+ return self.put(url, params=params)
482
+
483
+ def project_remove_user_permissions(self, project_key, username):
484
+ """
485
+ Revoke all permissions for the specified project for a user.
486
+ The authenticated user must have PROJECT_ADMIN permission for
487
+ the specified project or a higher global permission to call this resource.
488
+ In addition, a user may not revoke their own project permissions if they do not have a higher global permission.
489
+ :param project_key: The project key
490
+ :param username: username to be granted
491
+ :return:
492
+ """
493
+ url = self._url_project_users(project_key)
494
+ params = {"name": username}
495
+ return self.delete(url, params=params)
496
+
497
+ def _url_project_groups(self, project_key):
498
+ return "{}/permissions/groups".format(self._url_project(project_key))
499
+
500
+ def project_groups(self, key, start=0, limit=None, filter_str=None):
501
+ """
502
+ Get Project Groups
503
+ :param limit:
504
+ :param limit:
505
+ :param start:
506
+ :param key: The project key
507
+ :param filter_str: OPTIONAL: group filter string
508
+ :return:
509
+ """
510
+ url = self._url_project_groups(key)
511
+ params = {}
512
+ if start:
513
+ params["start"] = start
514
+ if limit:
515
+ params["limit"] = limit
516
+ if filter_str:
517
+ params["filter"] = filter_str
518
+ return self._get_paged(url, params=params)
519
+
520
+ def project_grant_group_permissions(self, project_key, group_name, permission):
521
+ """
522
+ Grant the specified project permission to a specific group
523
+ :param project_key: The project key
524
+ :param group_name: group to be granted
525
+ :param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
526
+ :return:
527
+ """
528
+ url = self._url_project_groups(project_key)
529
+ params = {"permission": permission, "name": group_name}
530
+ return self.put(url, params=params)
531
+
532
+ def project_remove_group_permissions(self, project_key, groupname):
533
+ """
534
+ Revoke all permissions for the specified project for a group.
535
+ The authenticated user must have PROJECT_ADMIN permission for the specified project
536
+ or a higher global permission to call this resource.
537
+ In addition, a user may not revoke a group's permissions
538
+ if it will reduce their own permission level.
539
+ :param project_key: The project key
540
+ :param groupname: group to be granted
541
+ :return:
542
+ """
543
+ url = self._url_project_groups(project_key)
544
+ params = {"name": groupname}
545
+ return self.delete(url, params=params)
546
+
547
+ def project_default_permissions(self, project_key, permission):
548
+ """
549
+ Check if the specified permission is the default permission for a given project
550
+ :param project_key: The project key
551
+ :param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
552
+ :return:
553
+ """
554
+ url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
555
+ return self.get(url)
556
+
557
+ def project_grant_default_permissions(self, project_key, permission):
558
+ """
559
+ Grant the specified project permission to all users for a given project
560
+ :param project_key: The project key
561
+ :param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
562
+ :return:
563
+ """
564
+ url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
565
+ return self.post(url, params={"allow": True})
566
+
567
+ def project_remove_default_permissions(self, project_key, permission):
568
+ """
569
+ Revoke the specified project permission for all users for a given project
570
+ :param project_key: The project key
571
+ :param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
572
+ :return:
573
+ """
574
+ url = "{}/permissions/{}/all".format(self._url_project(project_key), permission)
575
+ return self.post(url, params={"allow": False})
576
+
577
+ def _url_project_repo_hook_settings(self, project_key):
578
+ return "{}/settings/hooks".format(self._url_project(project_key))
579
+
580
+ def all_project_repo_hook_settings(self, project_key, start=0, limit=None, filter_type=None):
581
+ """
582
+ Get all repository hooks for a given project
583
+ :param project_key: The project key
584
+ :param start:
585
+ :param limit: OPTIONAL: The limit of the number of changes to return, this may be restricted by
586
+ fixed system limits. Default by built-in method: None
587
+ :param filter_type: OPTIONAL: PRE_RECEIVE|POST_RECEIVE if present,
588
+ controls how repository hooks should be filtered.
589
+ :return:
590
+ """
591
+ url = self._url_project_repo_hook_settings(project_key)
592
+ params = {}
593
+ if filter_type:
594
+ params["type"] = filter_type
595
+ if start:
596
+ params["start"] = start
597
+ if limit:
598
+ params["limit"] = limit
599
+ return self._get_paged(url, params)
600
+
601
+ def get_project_repo_hook_settings(self, project_key, hook_key):
602
+ """
603
+ Get a repository hook from a given project
604
+ :param project_key: The project key
605
+ :param hook_key: The repository hook key
606
+ :return:
607
+ """
608
+ url = "{}/{}".format(self._url_project_repo_hook_settings(project_key), hook_key)
609
+ return self.get(url)
610
+
611
+ def enable_project_repo_hook_settings(self, project_key, hook_key):
612
+ """
613
+ Enable a repository hook for a given project
614
+ :param project_key: The project key
615
+ :param hook_key: The repository hook key
616
+ :return:
617
+ """
618
+ url = "{}/{}/enabled".format(self._url_project_repo_hook_settings(project_key), hook_key)
619
+ return self.put(url)
620
+
621
+ def disable_project_repo_hook_settings(self, project_key, hook_key):
622
+ """
623
+ Disable a repository hook for a given project
624
+ :param project_key: The project key
625
+ :param hook_key: The repository hook key
626
+ :return:
627
+ """
628
+ url = "{}/{}/enabled".format(self._url_project_repo_hook_settings(project_key), hook_key)
629
+ return self.delete(url)
630
+
631
+ def _url_project_conditions(self, project_key):
632
+ return "{}/conditions".format(
633
+ self._url_project(
634
+ project_key,
635
+ api_root="rest/default-reviewers",
636
+ api_version="1.0",
637
+ )
638
+ )
639
+
640
+ def get_project_conditions(self, project_key):
641
+ """
642
+ Request type: GET
643
+ Return a page of defaults conditions with reviewers list that have been configured for this project.
644
+ For further information visit:
645
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264904368
646
+ :projectKey: str
647
+ :return:
648
+ """
649
+ url = self._url_project_conditions(project_key)
650
+ return self.get(url) or {}
651
+
652
+ def _url_project_condition(self, project_key, id_condition=None):
653
+ url = "{}/condition".format(
654
+ self._url_project(
655
+ project_key,
656
+ api_root="rest/default-reviewers",
657
+ api_version="1.0",
658
+ )
659
+ )
660
+ if id_condition is not None:
661
+ url += "/{}".format(id_condition)
662
+ return url
663
+
664
+ def get_project_condition(self, project_key, id_condition):
665
+ """
666
+ Request type: GET
667
+ Return a specific condition with reviewers list that has been configured for this project.
668
+ For further information visit:
669
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264901504
670
+ :projectKey: str - project key involved
671
+ :idCondition: int - condition id involved
672
+ :return:
673
+ """
674
+ url = self._url_project_condition(project_key, id_condition)
675
+ return self.get(url) or {}
676
+
677
+ def create_project_condition(self, project_key, condition):
678
+ """
679
+ Request type: POST
680
+ Create a new condition for this project.
681
+ For further information visit:
682
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264893584
683
+ :projectKey: str- project key involved
684
+ :data: condition: dictionary object
685
+ :example condition: '{"sourceMatcher":
686
+ {"id":"any",
687
+ "type":{"id":"ANY_REF"}},
688
+ "targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
689
+ "reviewers":[{"id": 12}],"requiredApprovals":"0"
690
+ }'
691
+ :return:
692
+ """
693
+ url = self._url_project_condition(project_key)
694
+ return self.post(url, data=condition) or {}
695
+
696
+ def update_project_condition(self, project_key, condition, id_condition):
697
+ """
698
+ Request type: PUT
699
+ Update a new condition for this project.
700
+ For further information visit:
701
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
702
+ :projectKey: str- project key involved
703
+ :idCondition: int - condition id involved
704
+ :data: condition: dictionary object
705
+ :example condition: '{"sourceMatcher":
706
+ {"id":"any",
707
+ "type":{"id":"ANY_REF"}},
708
+ "targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
709
+ "reviewers":[{"id": 12}],"requiredApprovals":"0"
710
+ }'
711
+ :return:
712
+ """
713
+ url = self._url_project_condition(project_key, id_condition)
714
+ return self.put(url, data=condition) or {}
715
+
716
+ def delete_project_condition(self, project_key, id_condition):
717
+ """
718
+ Delete a specific condition for this repository slug inside project.
719
+ For further information visit:
720
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264896304
721
+ Request type: DELETE
722
+ :projectKey: str- project key involved
723
+ :idCondition: int - condition id involved
724
+ :return:
725
+ """
726
+ url = self._url_project_condition(project_key, id_condition)
727
+ return self.delete(url) or {}
728
+
729
+ def _url_project_audit_log(self, project_key):
730
+ if self.cloud:
731
+ raise Exception("Not supported in Bitbucket Cloud")
732
+
733
+ return "{}/events".format(self._url_project(project_key, api_root="rest/audit"))
734
+
735
+ def get_project_audit_log(self, project_key, start=0, limit=None):
736
+ """
737
+ Get the audit log of the project
738
+ :param start:
739
+ :param limit:
740
+ :param project_key: The project key
741
+ :return: List of events of the audit log
742
+ """
743
+ url = self._url_project_audit_log(project_key)
744
+ params = {}
745
+ if start:
746
+ params["start"] = start
747
+ if limit:
748
+ params["limit"] = limit
749
+ return self._get_paged(url, params=params)
750
+
751
+ def _url_repos(self, project_key, api_root=None, api_version=None):
752
+ return "{}/repos".format(self._url_project(project_key, api_root, api_version))
753
+
754
+ def repo_list(self, project_key, start=0, limit=25):
755
+ """
756
+ Get repositories list from project
757
+
758
+ :param project_key: The project key
759
+ :param start:
760
+ :param limit:
761
+ :return:
762
+ """
763
+ url = self._url_repos(project_key)
764
+ params = {}
765
+ if start:
766
+ params["start"] = start
767
+ if limit:
768
+ params["limit"] = limit
769
+ return self._get_paged(url, params=params)
770
+
771
+ def repo_all_list(self, project_key):
772
+ """
773
+ Get all repositories list from project
774
+ :param project_key:
775
+ :return:
776
+ """
777
+ return self.repo_list(project_key, limit=None)
778
+
779
+ def create_repo(self, project_key, repository_slug, forkable=False, is_private=True):
780
+ """Create a new repository.
781
+
782
+ Requires an existing project in which this repository will be created. The only parameters which will be used
783
+ are name and scmId.
784
+
785
+ The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.
786
+
787
+ :param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
788
+ :type project_key: str
789
+ :param repository_slug: Name of repository to create (i.e. "My repo").
790
+ :param forkable: Set the repository to be forkable or not.
791
+ :type forkable: bool
792
+ :param is_private: Set the repository to be private or not.
793
+ :type is_private: bool
794
+ :return:
795
+ 201 - application/json (repository)
796
+ 400 - application/json (errors)
797
+ 401 - application/json (errors)
798
+ 409 - application/json (errors)
799
+ :rtype: requests.Response
800
+ """
801
+ url = self._url_repos(project_key)
802
+ data = {
803
+ "name": repository_slug,
804
+ "scmId": "git",
805
+ "forkable": forkable,
806
+ "is_private": is_private,
807
+ }
808
+ return self.post(url, data=data)
809
+
810
+ ################################################################################################
811
+ # Functions related to a specific repository
812
+ ################################################################################################
813
+
814
+ def _url_repo(self, project_key, repo, api_root=None, api_version=None):
815
+ return "{}/{}".format(self._url_repos(project_key, api_root, api_version), repo)
816
+
817
+ def reindex_repo(self, project_key, repository_slug):
818
+ """
819
+ Reindex repo
820
+ :param project_key:
821
+ :param repository_slug:
822
+ :return:
823
+ """
824
+ url = "{urlRepo}/sync".format(
825
+ urlRepo=self._url_repo(
826
+ project_key,
827
+ repository_slug,
828
+ api_root="rest/indexing",
829
+ api_version="1.0",
830
+ )
831
+ )
832
+ return self.post(url)
833
+
834
+ def reindex_repo_dev_panel(self, project_key, repository_slug):
835
+ """
836
+ Reindex all the Jira issues related to this repository_slug, including branches and pull requests.
837
+ This automatically happens as part of an upgrade, and calling this manually should only be required
838
+ if something unforeseen happens and the index becomes out of sync.
839
+ The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
840
+ :param project_key:
841
+ :param repository_slug:
842
+ :return:
843
+ """
844
+ url = "{}/reindex".format(self._url_repo(project_key, repository_slug, api_root="rest/jira-dev"))
845
+ return self.post(url)
846
+
847
+ def get_repo(self, project_key, repository_slug):
848
+ """
849
+ Get a specific repository from a project. This operates based on slug not name which may
850
+ be confusing to some users.
851
+ :param project_key: Key of the project you wish to look in.
852
+ :param repository_slug: url-compatible repository identifier
853
+ :return: Dictionary of request response
854
+ """
855
+ url = self._url_repo(project_key, repository_slug)
856
+ return self.get(url)
857
+
858
+ def repo_exists(self, project_key, repository_slug):
859
+ """
860
+ Check if given combination of project and repository exists and available.
861
+ :param project_key: Key of the project where to check for repository.
862
+ :param repository_slug: url-compatible repository identifier to look for.
863
+ :return: False is requested repository doesn't exist in the project or not accessible to the requestor
864
+ """
865
+ exists = False
866
+ try:
867
+ self.get_repo(project_key, repository_slug)
868
+ exists = True
869
+ except HTTPError as e:
870
+ if e.response.status_code in (401, 404):
871
+ pass
872
+ return exists
873
+
874
+ def update_repo(self, project_key, repository_slug, **params):
875
+ """
876
+ Update a repository in a project. This operates based on slug not name which may
877
+ be confusing to some users.
878
+ :param project_key: Key of the project you wish to look in.
879
+ :param repository_slug: url-compatible repository identifier
880
+ :return: The value of the put request.
881
+ """
882
+ url = self._url_repo(project_key, repository_slug)
883
+ return self.put(url, data=params)
884
+
885
+ def delete_repo(self, project_key, repository_slug):
886
+ """
887
+ Delete a specific repository from a project. This operates based on slug not name which may
888
+ be confusing to some users.
889
+ :param project_key: Key of the project you wish to look in.
890
+ :param repository_slug: url-compatible repository identifier
891
+ :return: Dictionary of request response
892
+ """
893
+ url = self._url_repo(project_key, repository_slug)
894
+ return self.delete(url)
895
+
896
+ def fork_repository(self, project_key, repository_slug, new_repository_slug):
897
+ """
898
+ Forks a repository within the same project.
899
+ :param project_key:
900
+ :param repository_slug:
901
+ :param new_repository_slug:
902
+ :return:
903
+ """
904
+ url = self._url_repo(project_key, repository_slug)
905
+ body = {}
906
+ if new_repository_slug is not None:
907
+ body["name"] = new_repository_slug
908
+ body["project"] = {"key": project_key}
909
+ return self.post(url, data=body)
910
+
911
+ def fork_repository_new_project(
912
+ self,
913
+ project_key,
914
+ repository_slug,
915
+ new_project_key,
916
+ new_repository_slug,
917
+ ):
918
+ """
919
+ Forks a repository to a separate project.
920
+ :param project_key: Origin Project Key
921
+ :param repository_slug: Origin repository slug
922
+ :param new_project_key: Project Key of target project
923
+ :param new_repository_slug: Target Repository slug
924
+ :return:
925
+ """
926
+ url = self._url_repo(project_key, repository_slug)
927
+ body = {}
928
+ if new_repository_slug is not None and new_project_key is not None:
929
+ body["name"] = new_repository_slug
930
+ body["project"] = {"key": new_project_key}
931
+ return self.post(url, data=body)
932
+
933
+ def repo_keys(self, project_key, repo_key, start=0, limit=None, filter_str=None):
934
+ """
935
+ Get SSH access keys added to the repository
936
+ :param start:
937
+ :param limit:
938
+ :param project_key: The project key
939
+ :param repo_key: The repository key
940
+ :param filter_str: OPTIONAL: users filter string
941
+ :return:
942
+ """
943
+ url = "{}/ssh".format(self._url_repo(project_key, repo_key, api_root="rest/keys"))
944
+ params = {}
945
+ if start:
946
+ params["start"] = start
947
+ if limit:
948
+ params["limit"] = limit
949
+ if filter_str:
950
+ params["filter"] = filter_str
951
+ return self._get_paged(url, params=params)
952
+
953
+ def _url_repo_users(self, project_key, repo):
954
+ return "{}/permissions/users".format(self._url_repo(project_key, repo))
955
+
956
+ def repo_users(self, project_key, repo_key, start=0, limit=None, filter_str=None):
957
+ """
958
+ Get users with permission in repository
959
+ :param start:
960
+ :param limit:
961
+ :param project_key: The project key
962
+ :param repo_key: The repository key
963
+ :param filter_str: OPTIONAL: Users filter string
964
+ :return:
965
+ """
966
+ url = self._url_repo_users(project_key, repo_key)
967
+ params = {}
968
+ if start:
969
+ params["start"] = start
970
+ if limit:
971
+ params["limit"] = limit
972
+ if filter_str:
973
+ params["filter"] = filter_str
974
+ return self._get_paged(url, params=params)
975
+
976
+ def repo_grant_user_permissions(self, project_key, repo_key, username, permission):
977
+ """
978
+ Grant the specified repository permission to a specific user
979
+ :param project_key: The project key
980
+ :param repo_key: The repository key (slug)
981
+ :param username: username to be granted
982
+ :param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
983
+ :return:
984
+ """
985
+ url = self._url_repo_users(project_key, repo_key)
986
+ params = {"permission": permission, "name": username}
987
+ return self.put(url, params=params)
988
+
989
+ def repo_remove_user_permissions(self, project_key, repo_key, username):
990
+ """
991
+ Revoke all permissions for the specified repository for a user.
992
+ The authenticated user must have REPO_ADMIN permission for the specified repository
993
+ or a higher project or global permission to call this resource.
994
+ In addition, a user may not revoke their own repository permissions
995
+ if they do not have a higher project or global permission.
996
+ :param project_key: The project key
997
+ :param repo_key: The repository key (slug)
998
+ :param username: username to be granted
999
+ :return:
1000
+ """
1001
+ url = self._url_repo_users(project_key, repo_key)
1002
+ params = {"name": username}
1003
+ return self.delete(url, params=params)
1004
+
1005
+ def _url_repo_groups(self, project_key, repo):
1006
+ return "{}/permissions/groups".format(self._url_repo(project_key, repo))
1007
+
1008
+ def repo_groups(self, project_key, repo_key, start=0, limit=None, filter_str=None):
1009
+ """
1010
+ Get repository Groups
1011
+ :param start:
1012
+ :param limit:
1013
+ :param project_key: The project key
1014
+ :param repo_key: The repository key
1015
+ :param filter_str: OPTIONAL: group filter string
1016
+ :return:
1017
+ """
1018
+ url = self._url_repo_groups(project_key, repo_key)
1019
+ params = {}
1020
+ if start:
1021
+ params["start"] = start
1022
+ if limit:
1023
+ params["limit"] = limit
1024
+ if filter_str:
1025
+ params["filter"] = filter_str
1026
+ return self._get_paged(url, params=params)
1027
+
1028
+ def project_groups_with_administrator_permissions(self, key):
1029
+ """
1030
+ Get groups with admin permissions
1031
+ :param key:
1032
+ :return:
1033
+ """
1034
+ return [group["group"]["name"] for group in self.project_groups(key) if group["permission"] == "PROJECT_ADMIN"]
1035
+
1036
+ def repo_users_with_administrator_permissions(self, project_key, repo_key):
1037
+ """
1038
+ Get repository administrators for repository
1039
+ :param project_key: The project key
1040
+ :param repo_key: The repository key
1041
+ :return: List of repo administrators
1042
+ """
1043
+ repo_administrators = []
1044
+ for user in self.repo_users(project_key, repo_key):
1045
+ if user["permission"] == "REPO_ADMIN":
1046
+ repo_administrators.append(user)
1047
+ for group in self.repo_groups_with_administrator_permissions(project_key, repo_key):
1048
+ for user in self.group_members(group):
1049
+ repo_administrators.append(user)
1050
+ for user in self.project_users_with_administrator_permissions(project_key):
1051
+ repo_administrators.append(user)
1052
+ # We convert to a set to ensure uniqueness then back to a list for later useability
1053
+ return list({user["id"]: user for user in repo_administrators}.values())
1054
+
1055
+ def repo_groups_with_administrator_permissions(self, project_key, repo_key):
1056
+ """
1057
+ Get groups with admin permissions
1058
+ :param project_key:
1059
+ :param repo_key:
1060
+ :return:
1061
+ """
1062
+ repo_group_administrators = []
1063
+ for group in self.repo_groups(project_key, repo_key):
1064
+ if group["permission"] == "REPO_ADMIN":
1065
+ repo_group_administrators.append(group["group"]["name"])
1066
+ for group in self.project_groups_with_administrator_permissions(project_key):
1067
+ repo_group_administrators.append(group)
1068
+ # We convert to a set to ensure uniqueness, then back to a list for later useability
1069
+ return list(set(repo_group_administrators))
1070
+
1071
+ def repo_grant_group_permissions(self, project_key, repo_key, groupname, permission):
1072
+ """
1073
+ Grant the specified repository permission to a specific group
1074
+ Promote or demote a group's permission level for the specified repository. Available repository permissions are:
1075
+ REPO_READ
1076
+ REPO_WRITE
1077
+ REPO_ADMIN
1078
+ See the Bitbucket Server documentation for a detailed explanation of what each permission entails.
1079
+ The authenticated user must have REPO_ADMIN permission for the specified repository or a higher project
1080
+ or global permission to call this resource.
1081
+ In addition, a user may not demote a group's permission level
1082
+ if their own permission level would be reduced as a result.
1083
+ :param project_key: The project key
1084
+ :param repo_key: The repository key (slug)
1085
+ :param groupname: group to be granted
1086
+ :param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
1087
+ :return:
1088
+ """
1089
+ url = self._url_repo_groups(project_key, repo_key)
1090
+ params = {"permission": permission, "name": groupname}
1091
+ return self.put(url, params=params)
1092
+
1093
+ def repo_remove_group_permissions(self, project_key, repo_key, groupname, permission):
1094
+ """
1095
+ Revoke all permissions for the specified repository for a group.
1096
+ The authenticated user must have REPO_ADMIN permission for the specified repository
1097
+ or a higher project or global permission to call this resource.
1098
+ In addition, a user may not revoke a group's permissions if it will reduce their own permission level.
1099
+ :param project_key: The project key
1100
+ :param repo_key: The repository key (slug)
1101
+ :param groupname: group to be granted
1102
+ :param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
1103
+ :return:
1104
+ """
1105
+ url = self._url_repo_groups(project_key, repo_key)
1106
+ params = {"name": groupname}
1107
+ if permission:
1108
+ params["permission"] = permission
1109
+ return self.delete(url, params=params)
1110
+
1111
+ def _url_repo_labels(self, project_key, repository_slug):
1112
+ if self.cloud:
1113
+ raise Exception("Not supported in Bitbucket Cloud")
1114
+
1115
+ return "{}/labels".format(self._url_repo(project_key, repository_slug))
1116
+
1117
+ def get_repo_labels(self, project_key, repository_slug):
1118
+ """
1119
+ Get labels for a specific repository from a project. This operates based on slug not name which may
1120
+ be confusing to some users. (BitBucket Server only)
1121
+ :param project_key: Key of the project you wish to look in.
1122
+ :param repository_slug: url-compatible repository identifier
1123
+ :return: Dictionary of request response
1124
+ """
1125
+ url = self._url_repo_labels(project_key, repository_slug)
1126
+ return self.get(url)
1127
+
1128
+ def set_repo_label(self, project_key, repository_slug, label_name):
1129
+ """
1130
+ Sets a label on a repository. (BitBucket Server only)
1131
+ The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
1132
+ :param project_key: Key of the project you wish to look in.
1133
+ :param repository_slug: url-compatible repository identifier
1134
+ :param label_name: label name to apply
1135
+ :return:
1136
+ """
1137
+ url = self._url_repo_labels(project_key, repository_slug)
1138
+ data = {"name": label_name}
1139
+ return self.post(url, data=data)
1140
+
1141
+ def _url_repo_audit_log(self, project_key, repository_slug):
1142
+ if self.cloud:
1143
+ raise Exception("Not supported in Bitbucket Cloud")
1144
+
1145
+ return "{}/events".format(self._url_repo(project_key, repository_slug, api_root="rest/audit"))
1146
+
1147
+ def get_repo_audit_log(self, project_key, repository_slug, start=0, limit=None):
1148
+ """
1149
+ Get the audit log of the repository
1150
+ :param start:
1151
+ :param limit:
1152
+ :param project_key: Key of the project you wish to look in.
1153
+ :param repository_slug: url-compatible repository identifier
1154
+ :return: List of events of the audit log
1155
+ """
1156
+ url = self._url_repo_audit_log(project_key, repository_slug)
1157
+ params = {}
1158
+ if start:
1159
+ params["start"] = start
1160
+ if limit:
1161
+ params["limit"] = limit
1162
+ return self._get_paged(url, params=params)
1163
+
1164
+ def _url_repo_branches(self, project_key, repository_slug, api_root=None):
1165
+ return "{}/branches".format(self._url_repo(project_key, repository_slug, api_root=api_root))
1166
+
1167
+ def get_branches(
1168
+ self,
1169
+ project_key,
1170
+ repository_slug,
1171
+ base=None,
1172
+ filter=None,
1173
+ start=0,
1174
+ limit=None,
1175
+ details=True,
1176
+ order_by="MODIFICATION",
1177
+ boost_matches=False,
1178
+ ):
1179
+ """
1180
+ Retrieve the branches matching the supplied filterText param.
1181
+ The authenticated user must have REPO_READ permission for the specified repository to call this resource.
1182
+ :param start:
1183
+ :param project_key:
1184
+ :param repository_slug:
1185
+ :param base: base branch/tag to compare each branch to (for the metadata providers that uses that information)
1186
+ :param filter:
1187
+ :param limit: OPTIONAL: The limit of the number of branches to return, this may be restricted by
1188
+ fixed system limits. Default by built-in method: None
1189
+ :param details: whether to retrieve plugin-provided metadata about each branch
1190
+ :param order_by: OPTIONAL: ordering of refs either ALPHABETICAL (by name) or MODIFICATION (last updated)
1191
+ :param boost_matches: Controls whether exact and prefix matches will be boosted to the top
1192
+ :return:
1193
+ """
1194
+ url = self._url_repo_branches(project_key, repository_slug)
1195
+ params = {}
1196
+ if start:
1197
+ params["start"] = start
1198
+ if limit:
1199
+ params["limit"] = limit
1200
+ if filter:
1201
+ params["filterText"] = filter
1202
+ if base:
1203
+ params["base"] = base
1204
+ if order_by:
1205
+ params["orderBy"] = order_by
1206
+ params["details"] = details
1207
+ params["boostMatches"] = boost_matches
1208
+ return self._get_paged(url, params=params)
1209
+
1210
+ def _url_repo_default_branche(self, project_key, repository_slug):
1211
+ return "{}/default".format(self._url_repo_branches(project_key, repository_slug))
1212
+
1213
+ def get_default_branch(self, project_key, repository_slug):
1214
+ """
1215
+ Get the default branch of the repository.
1216
+ The authenticated user must have REPO_READ permission for the specified repository to call this resource.
1217
+ :param project_key: The project key
1218
+ :param repository_slug: The repository key
1219
+ :return:
1220
+ """
1221
+ url = self._url_repo_default_branche(project_key, repository_slug)
1222
+ return self.get(url)
1223
+
1224
+ def set_default_branch(self, project_key, repository_slug, ref_branch_name):
1225
+ """
1226
+ Update the default branch of a repository.
1227
+ The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
1228
+ :param project_key: The project key
1229
+ :param repository_slug: The repository key (slug)
1230
+ :param ref_branch_name: ref name like refs/heads/master
1231
+ :return:
1232
+ """
1233
+ url = self._url_repo_default_branche(project_key, repository_slug)
1234
+ data = {"id": ref_branch_name}
1235
+ return self.put(url, data=data)
1236
+
1237
+ def create_branch(self, project_key, repository_slug, name, start_point, message=""):
1238
+ """Creates a branch using the information provided in the request.
1239
+
1240
+ The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
1241
+
1242
+ :param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
1243
+ :type project_key: str
1244
+ :param repository_slug: Name of repository where branch is created (i.e. "my_repo").
1245
+ :param name: Name of branch to create (i.e. "my_branch").
1246
+ :type name: str
1247
+ :param start_point: Name of branch to branch from.
1248
+ :type start_point: str
1249
+ :param message: Branch message.
1250
+ :type message: str
1251
+ :return:
1252
+ 200 - application/json (repository)
1253
+ 401 - application/json (errors)
1254
+ 404 - application/json (errors)
1255
+ :rtype: requests.Response
1256
+ """
1257
+ url = self._url_repo_branches(project_key, repository_slug)
1258
+ data = {"name": name, "startPoint": start_point, "message": message}
1259
+ return self.post(url, data=data)
1260
+
1261
+ def delete_branch(self, project_key, repository_slug, name, end_point=None):
1262
+ """
1263
+ Delete branch from related repo
1264
+ :param self:
1265
+ :param project_key:
1266
+ :param repository_slug:
1267
+ :param name:
1268
+ :param end_point:
1269
+ :return:
1270
+ """
1271
+ url = self._url_repo_branches(project_key, repository_slug, api_root="rest/branch-utils")
1272
+ data = {"name": str(name)}
1273
+ if end_point:
1274
+ data["endPoint"] = end_point
1275
+ return self.delete(url, data=data)
1276
+
1277
+ def _url_repo_tags(self, project_key, repository_slug, api_root=None):
1278
+ if self.cloud:
1279
+ return "{}/refs/tags".format(self._url_repo(project_key, repository_slug, api_root=api_root))
1280
+ else:
1281
+ return "{}/tags".format(self._url_repo(project_key, repository_slug, api_root=api_root))
1282
+
1283
+ def get_tags(
1284
+ self,
1285
+ project_key,
1286
+ repository_slug,
1287
+ filter="",
1288
+ limit=1000,
1289
+ order_by=None,
1290
+ start=0,
1291
+ ):
1292
+ """
1293
+ Retrieve the tags matching the supplied filterText param.
1294
+ The authenticated user must have REPO_READ permission for the context repository to call this resource.
1295
+ :param project_key:
1296
+ :param repository_slug:
1297
+ :param filter:
1298
+ :param start:
1299
+ :param limit: OPTIONAL: The limit of the number of tags to return, this may be restricted by
1300
+ fixed system limits. Default by built-in method: 1000
1301
+ :param order_by: OPTIONAL: ordering of refs either ALPHABETICAL (by name) or MODIFICATION (last updated)
1302
+ :return:
1303
+ """
1304
+ url = self._url_repo_tags(project_key, repository_slug)
1305
+ params = {}
1306
+ if start:
1307
+ params["start"] = start
1308
+ if limit:
1309
+ params["limit"] = limit
1310
+ if filter:
1311
+ params["filterText"] = filter
1312
+ if order_by:
1313
+ params["orderBy"] = order_by
1314
+ return self._get_paged(url, params=params)
1315
+
1316
+ def get_project_tags(self, project_key, repository_slug, tag_name=None):
1317
+ """
1318
+ Retrieve a tag in the specified repository.
1319
+ The authenticated user must have REPO_READ permission for the context repository to call this resource.
1320
+ Search uri is api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}
1321
+ :param project_key:
1322
+ :param repository_slug:
1323
+ :param tag_name: OPTIONAL:
1324
+ :return:
1325
+ """
1326
+ url = self._url_repo_tags(project_key, repository_slug)
1327
+ if tag_name is not None:
1328
+ return self.get("{}/{}".format(url, tag_name))
1329
+
1330
+ return self._get_paged(url)
1331
+
1332
+ def set_tag(
1333
+ self,
1334
+ project_key,
1335
+ repository_slug,
1336
+ tag_name,
1337
+ commit_revision,
1338
+ description=None,
1339
+ ):
1340
+ """
1341
+ Creates a tag using the information provided in the {@link RestCreateTagRequest request}
1342
+ The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
1343
+ :param project_key:
1344
+ :param repository_slug:
1345
+ :param tag_name:
1346
+ :param commit_revision: commit hash
1347
+ :param description: OPTIONAL:
1348
+ :return:
1349
+ """
1350
+ url = self._url_repo_tags(project_key, repository_slug)
1351
+ body = {
1352
+ "name": tag_name,
1353
+ "startPoint": commit_revision,
1354
+ }
1355
+ if description is not None:
1356
+ body["message"] = description
1357
+
1358
+ return self.post(url, data=body)
1359
+
1360
+ def delete_tag(self, project_key, repository_slug, tag_name):
1361
+ """
1362
+ Creates a tag using the information provided in the {@link RestCreateTagRequest request}
1363
+ The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
1364
+ :param project_key:
1365
+ :param repository_slug:
1366
+ :param tag_name:
1367
+ :return:
1368
+ """
1369
+ url = "{}/{}".format(
1370
+ self._url_repo_tags(project_key, repository_slug, api_root="rest/git"),
1371
+ tag_name,
1372
+ )
1373
+ return self.delete(url)
1374
+
1375
+ def _url_repo_hook_settings(self, project_key, repository_slug):
1376
+ return "{}/settings/hooks".format(self._url_repo(project_key, repository_slug))
1377
+
1378
+ def all_repo_hook_settings(
1379
+ self,
1380
+ project_key,
1381
+ repository_slug,
1382
+ start=0,
1383
+ limit=None,
1384
+ filter_type=None,
1385
+ ):
1386
+ """
1387
+ Get all repository hooks for a given repo
1388
+ :param project_key: The project key
1389
+ :param repository_slug: The repository key
1390
+ :param start:
1391
+ :param limit: OPTIONAL: The limit of the number of changes to return, this may be restricted by
1392
+ fixed system limits. Default by built-in method: None
1393
+ :param filter_type: OPTIONAL: PRE_RECEIVE|POST_RECEIVE if present,
1394
+ controls how repository hooks should be filtered.
1395
+ :return:
1396
+ """
1397
+ url = self._url_repo_hook_settings(project_key, repository_slug)
1398
+ params = {}
1399
+ if filter_type:
1400
+ params["type"] = filter_type
1401
+ if start:
1402
+ params["start"] = start
1403
+ if limit:
1404
+ params["limit"] = limit
1405
+ return self._get_paged(url, params)
1406
+
1407
+ def get_repo_hook_settings(self, project_key, repository_slug, hook_key):
1408
+ """
1409
+ Get a repository hook from a given repo
1410
+ :param project_key: The project key
1411
+ :param repository_slug: The repository key
1412
+ :param hook_key: The repository hook key
1413
+ :return:
1414
+ """
1415
+ url = "{}/{}".format(
1416
+ self._url_repo_hook_settings(project_key, repository_slug),
1417
+ hook_key,
1418
+ )
1419
+ return self.get(url)
1420
+
1421
+ def enable_repo_hook_settings(self, project_key, repository_slug, hook_key):
1422
+ """
1423
+ Enable a repository hook for a given repo
1424
+ :param project_key: The project key
1425
+ :param repository_slug: The repository key
1426
+ :param hook_key: The repository hook key
1427
+ :return:
1428
+ """
1429
+ url = "{}/{}/enabled".format(
1430
+ self._url_repo_hook_settings(project_key, repository_slug),
1431
+ hook_key,
1432
+ )
1433
+ return self.put(url)
1434
+
1435
+ def disable_repo_hook_settings(self, project_key, repository_slug, hook_key):
1436
+ """
1437
+ Disable a repository hook for a given repo
1438
+ :param project_key: The project key
1439
+ :param repository_slug: The repository key
1440
+ :param hook_key: The repository hook key
1441
+ :return:
1442
+ """
1443
+ url = "{}/{}/enabled".format(
1444
+ self._url_repo_hook_settings(project_key, repository_slug),
1445
+ hook_key,
1446
+ )
1447
+ return self.delete(url)
1448
+
1449
+ def _url_webhooks(self, project_key, repository_slug):
1450
+ return "{}/webhooks".format(self._url_repo(project_key, repository_slug))
1451
+
1452
+ def get_webhooks(
1453
+ self,
1454
+ project_key,
1455
+ repository_slug,
1456
+ event=None,
1457
+ statistics=False,
1458
+ ):
1459
+ """
1460
+ Get webhooks
1461
+ :param project_key:
1462
+ :param repository_slug:
1463
+ :param event: OPTIONAL: defaults to None
1464
+ :param statistics: OPTIONAL: defaults to False
1465
+ :return:
1466
+ """
1467
+ url = self._url_webhooks(project_key, repository_slug)
1468
+ params = {}
1469
+ if event:
1470
+ params["event"] = event
1471
+ if statistics:
1472
+ params["statistics"] = statistics
1473
+ return self._get_paged(url, params=params)
1474
+
1475
+ def create_webhook(
1476
+ self,
1477
+ project_key,
1478
+ repository_slug,
1479
+ name,
1480
+ events,
1481
+ webhook_url,
1482
+ active,
1483
+ secret=None,
1484
+ ):
1485
+ """Creates a webhook using the information provided in the request.
1486
+
1487
+ The authenticated user must have REPO_ADMIN permission for the context repository to call this resource.
1488
+
1489
+ :param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
1490
+ :param repository_slug:
1491
+ :param name: Name of webhook to create.
1492
+ :param events: List of event. (i.e. ["repo:refs_changed", "pr:merged", "pr:opened"])
1493
+ :param webhook_url:
1494
+ :param active:
1495
+ :param secret: The string is used to verify data integrity between Bitbucket and your endpoint.
1496
+ :return:
1497
+ """
1498
+ url = self._url_webhooks(project_key, repository_slug)
1499
+ body = {
1500
+ "name": name,
1501
+ "events": events,
1502
+ "url": webhook_url,
1503
+ "active": active,
1504
+ }
1505
+ if secret:
1506
+ body["configuration"] = {"secret": secret}
1507
+ return self.post(url, data=body)
1508
+
1509
+ def _url_webhook(self, project_key, repository_slug, webhook_id):
1510
+ return "{}/{}".format(self._url_webhooks(project_key, repository_slug), webhook_id)
1511
+
1512
+ def get_webhook(self, project_key, repository_slug, webhook_id):
1513
+ """
1514
+ Retrieve a webhook.
1515
+ The authenticated user must have REPO_ADMIN permission for the context repository to call this resource.
1516
+ :param project_key:
1517
+ :param repository_slug:
1518
+ :param webhook_id: the ID of the webhook within the repository
1519
+ :return:
1520
+ """
1521
+ url = self._url_webhook(project_key, repository_slug, webhook_id)
1522
+ return self.get(url)
1523
+
1524
+ def update_webhook(self, project_key, repository_slug, webhook_id, **params):
1525
+ """
1526
+ Update a webhook.
1527
+ The authenticated user must have REPO_ADMIN permission for the context repository to call this resource.
1528
+ :param project_key:
1529
+ :param repository_slug:
1530
+ :param webhook_id: the ID of the webhook within the repository
1531
+ :return:
1532
+ """
1533
+ url = self._url_webhook(project_key, repository_slug, webhook_id)
1534
+ return self.put(url, data=params)
1535
+
1536
+ def delete_webhook(self, project_key, repository_slug, webhook_id):
1537
+ """
1538
+ Delete a webhook.
1539
+ The authenticated user must have REPO_ADMIN permission for the context repository to call this resource.
1540
+ :param project_key:
1541
+ :param repository_slug:
1542
+ :param webhook_id: the ID of the webhook within the repository
1543
+ :return:
1544
+ """
1545
+ url = self._url_webhook(project_key, repository_slug, webhook_id)
1546
+ return self.delete(url)
1547
+
1548
+ def _url_pull_request_settings(self, project_key, repository_slug):
1549
+ return "{}/settings/pull-requests".format(self._url_repo(project_key, repository_slug))
1550
+
1551
+ def get_pull_request_settings(self, project_key, repository_slug):
1552
+ """
1553
+ Get pull request settings.
1554
+ :param project_key:
1555
+ :param repository_slug:
1556
+ :return:
1557
+ """
1558
+ url = self._url_pull_request_settings(project_key, repository_slug)
1559
+ return self.get(url)
1560
+
1561
+ def set_pull_request_settings(self, project_key, repository_slug, data):
1562
+ """
1563
+ Set pull request settings.
1564
+ :param project_key:
1565
+ :param repository_slug:
1566
+ :param data: json body
1567
+ :return:
1568
+ """
1569
+ url = self._url_pull_request_settings(project_key, repository_slug)
1570
+ return self.post(url, data=data)
1571
+
1572
+ def _url_pull_requests(self, project_key, repository_slug):
1573
+ if self.cloud:
1574
+ return self.resource_url("repositories/{}/{}/pullrequests".format(project_key, repository_slug))
1575
+ else:
1576
+ return "{}/pull-requests".format(self._url_repo(project_key, repository_slug))
1577
+
1578
+ def get_pull_requests(
1579
+ self,
1580
+ project_key,
1581
+ repository_slug,
1582
+ state="OPEN",
1583
+ order="newest",
1584
+ limit=100,
1585
+ start=0,
1586
+ at=None,
1587
+ ):
1588
+ """
1589
+ Get pull requests
1590
+ :param project_key:
1591
+ :param repository_slug:
1592
+ :param state:
1593
+ :param order: OPTIONAL: defaults to NEWEST the order to return pull requests in, either OLDEST
1594
+ (as in: "oldest first") or NEWEST.
1595
+ :param limit:
1596
+ :param start:
1597
+ :param at:
1598
+ :return:
1599
+ """
1600
+ url = self._url_pull_requests(project_key, repository_slug)
1601
+ params = {}
1602
+ if state:
1603
+ params["state"] = state
1604
+ if limit:
1605
+ params["limit"] = limit
1606
+ if start:
1607
+ params["start"] = start
1608
+ if order:
1609
+ params["order"] = order
1610
+ if at:
1611
+ params["at"] = at
1612
+ return self._get_paged(url, params=params)
1613
+
1614
+ def get_required_reviewers_for_pull_request(
1615
+ self, source_project, source_repo, dest_project, dest_repo, source_branch, dest_branch
1616
+ ):
1617
+ """
1618
+ Get required reviewers for PR creation
1619
+ :param source_project: the project that the PR source is from
1620
+ :param source_repo: the repository that the PR source is from
1621
+ :param source_branch: the branch name of the PR
1622
+ :param dest_project: the project that the PR destination is from
1623
+ :param dest_repo: the repository that the PR destination is from
1624
+ :param dest_branch: where the PR is being merged into
1625
+ :return:
1626
+ """
1627
+ url = "{}/reviewers".format(
1628
+ self._url_repo(
1629
+ dest_project,
1630
+ dest_repo,
1631
+ api_root="rest/default-reviewers",
1632
+ api_version="1.0",
1633
+ )
1634
+ )
1635
+ source_repo_id = self.get_repo(source_project, source_repo)["id"]
1636
+ dest_repo_id = self.get_repo(dest_project, dest_repo)["id"]
1637
+ params = {
1638
+ "sourceRepoId": source_repo_id,
1639
+ "sourceRefId": source_branch,
1640
+ "targetRepoId": dest_repo_id,
1641
+ "targetRefId": dest_branch,
1642
+ }
1643
+ return self.get(url, params=params)
1644
+
1645
+ def open_pull_request(
1646
+ self,
1647
+ source_project,
1648
+ source_repo,
1649
+ dest_project,
1650
+ dest_repo,
1651
+ source_branch,
1652
+ destination_branch,
1653
+ title,
1654
+ description,
1655
+ reviewers=None,
1656
+ include_required_reviewers=False,
1657
+ ):
1658
+ """
1659
+ Create a new pull request between two branches.
1660
+ The branches may be in the same repository_slug, or different ones.
1661
+ When using different repositories, they must still be in the same {@link Repository#getHierarchyId() hierarchy}.
1662
+ The authenticated user must have REPO_READ permission for the "from" and "to"repositories to call this resource.
1663
+ :param source_project: the project that the PR source is from
1664
+ :param source_repo: the repository that the PR source is from
1665
+ :param dest_project: the project that the PR destination is from
1666
+ :param dest_repo: the repository that the PR destination is from
1667
+ :param source_branch: the branch name of the PR
1668
+ :param destination_branch: where the PR is being merged into
1669
+ :param title: the title of the PR
1670
+ :param description: the description of what the PR does
1671
+ :param reviewers: the list of reviewers or a single reviewer of the PR
1672
+ :param include_required_reviewers: OPTIONAL defaults to False, include required reviewers for the PR
1673
+ :return:
1674
+ """
1675
+ body = {
1676
+ "title": title,
1677
+ "description": description,
1678
+ "fromRef": {
1679
+ "id": source_branch,
1680
+ "repository": {
1681
+ "slug": source_repo,
1682
+ "name": source_repo,
1683
+ "project": {"key": source_project},
1684
+ },
1685
+ },
1686
+ "toRef": {
1687
+ "id": destination_branch,
1688
+ "repository": {
1689
+ "slug": dest_repo,
1690
+ "name": dest_repo,
1691
+ "project": {"key": dest_project},
1692
+ },
1693
+ },
1694
+ "reviewers": [],
1695
+ }
1696
+
1697
+ def add_reviewer(reviewer_name):
1698
+ entry = {"user": {"name": reviewer_name}}
1699
+ body["reviewers"].append(entry)
1700
+
1701
+ if not self.cloud and include_required_reviewers:
1702
+ required_reviewers = self.get_required_reviewers_for_pull_request(
1703
+ source_project, source_repo, dest_project, dest_repo, source_branch, destination_branch
1704
+ )
1705
+ for required_reviewer in required_reviewers:
1706
+ add_reviewer(required_reviewer["name"])
1707
+
1708
+ if reviewers is not None:
1709
+ if isinstance(reviewers, str):
1710
+ add_reviewer(reviewers)
1711
+ elif isinstance(reviewers, list):
1712
+ for reviewer in reviewers:
1713
+ add_reviewer(reviewer)
1714
+
1715
+ return self.create_pull_request(dest_project, dest_repo, body)
1716
+
1717
+ def create_pull_request(self, project_key, repository_slug, data):
1718
+ """
1719
+ :param project_key:
1720
+ :param repository_slug:
1721
+ :param data: json body
1722
+ :return:
1723
+ """
1724
+ url = self._url_pull_requests(project_key, repository_slug)
1725
+ return self.post(url, data=data)
1726
+
1727
+ def _url_pull_request(self, project_key, repository_slug, pull_request_id):
1728
+ return "{}/{}".format(
1729
+ self._url_pull_requests(project_key, repository_slug),
1730
+ pull_request_id,
1731
+ )
1732
+
1733
+ def get_pull_request(self, project_key, repository_slug, pull_request_id):
1734
+ """
1735
+ Retrieve a pull request.
1736
+ The authenticated user must have REPO_READ permission
1737
+ for the repository that this pull request targets to call this resource.
1738
+ :param project_key:
1739
+ :param repository_slug:
1740
+ :param pull_request_id: the ID of the pull request within the repository
1741
+ :return:
1742
+ """
1743
+ url = self._url_pull_request(project_key, repository_slug, pull_request_id)
1744
+ return self.get(url)
1745
+
1746
+ @deprecated(version="1.15.1", reason="Use get_pull_request()")
1747
+ def get_pullrequest(self, *args, **kwargs):
1748
+ """
1749
+ Deprecated name since 1.15.1. Let's use the get_pull_request()
1750
+ """
1751
+
1752
+ def update_pull_request(self, project_key, repository_slug, pull_request_id, data):
1753
+ """
1754
+ Update a pull request.
1755
+ The authenticated user must have REPO_WRITE permission
1756
+ for the repository that this pull request targets to call this resource.
1757
+ :param project_key:
1758
+ :param repository_slug:
1759
+ :param pull_request_id: the ID of the pull request within the repository
1760
+ :param data: json body
1761
+ :return:
1762
+ """
1763
+
1764
+ url = self._url_pull_request(project_key, repository_slug, pull_request_id)
1765
+ return self.put(url, data=data)
1766
+
1767
+ def delete_pull_request(
1768
+ self,
1769
+ project_key,
1770
+ repository_slug,
1771
+ pull_request_id,
1772
+ pull_request_version,
1773
+ ):
1774
+ """
1775
+ Delete a pull request.
1776
+
1777
+ :param project_key: the project key
1778
+ :param repository_slug: the repository slug
1779
+ :param pull_request_id: the ID of the pull request within the repository
1780
+ :param pull_request_version: the version of the pull request
1781
+ :return:
1782
+ """
1783
+ url = self._url_pull_request(project_key, repository_slug, pull_request_id)
1784
+ data = {"version": pull_request_version}
1785
+ return self.delete(url, data=data)
1786
+
1787
+ def get_pull_requests_activities(
1788
+ self,
1789
+ project_key,
1790
+ repository_slug,
1791
+ pull_request_id,
1792
+ start=0,
1793
+ limit=None,
1794
+ ):
1795
+ """
1796
+ Get pull requests activities
1797
+ :param limit:
1798
+ :param project_key:
1799
+ :param repository_slug:
1800
+ :param pull_request_id: the ID of the pull request within the repository
1801
+ :param start:
1802
+ :return:
1803
+ """
1804
+ if self.cloud:
1805
+ url = "{}/activity".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
1806
+ else:
1807
+ url = "{}/activities".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
1808
+ params = {}
1809
+ if start:
1810
+ params["start"] = start
1811
+ if limit:
1812
+ params["limit"] = limit
1813
+ return self._get_paged(url, params)
1814
+
1815
+ def get_pull_requests_changes(
1816
+ self,
1817
+ project_key,
1818
+ repository_slug,
1819
+ pull_request_id,
1820
+ start=0,
1821
+ limit=None,
1822
+ ):
1823
+ """
1824
+ Get pull requests changes
1825
+ :param start:
1826
+ :param limit:
1827
+ :param project_key:
1828
+ :param repository_slug:
1829
+ :param pull_request_id: the ID of the pull request within the repository
1830
+ :return:
1831
+ """
1832
+ url = "{}/changes".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
1833
+ params = {}
1834
+ if start:
1835
+ params["start"] = start
1836
+ if limit:
1837
+ params["limit"] = limit
1838
+ return self._get_paged(url, params)
1839
+
1840
+ def get_pull_requests_commits(
1841
+ self,
1842
+ project_key,
1843
+ repository_slug,
1844
+ pull_request_id,
1845
+ start=0,
1846
+ limit=None,
1847
+ ):
1848
+ """
1849
+ Get pull requests commits
1850
+ :param start:
1851
+ :param limit:
1852
+ :param project_key:
1853
+ :param repository_slug:
1854
+ :param pull_request_id: the ID of the pull request within the repository
1855
+ :start
1856
+ :limit
1857
+ :return:
1858
+ """
1859
+ url = "{}/commits".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
1860
+ params = {}
1861
+ if start:
1862
+ params["start"] = start
1863
+ if limit:
1864
+ params["limit"] = limit
1865
+ return self._get_paged(url, params)
1866
+
1867
+ def _url_pull_request_participants(self, project_key, repository_slug, pull_request_id):
1868
+ return "{}/{}/participants".format(
1869
+ self._url_pull_requests(project_key, repository_slug),
1870
+ pull_request_id,
1871
+ )
1872
+
1873
+ def get_pull_requests_participants(
1874
+ self,
1875
+ project_key,
1876
+ repository_slug,
1877
+ pull_request_id,
1878
+ start=0,
1879
+ limit=None,
1880
+ ):
1881
+ """
1882
+ Get all participants of a pull request
1883
+ :param start:
1884
+ :param limit:
1885
+ :param project_key:
1886
+ :param repository_slug:
1887
+ :param pull_request_id:
1888
+ :return:
1889
+ """
1890
+ url = self._url_pull_request_participants(project_key, repository_slug, pull_request_id)
1891
+ params = {}
1892
+ if start:
1893
+ params["start"] = start
1894
+ if limit:
1895
+ params["limit"] = limit
1896
+ return self._get_paged(url, params)
1897
+
1898
+ def get_dashboard_pull_requests(
1899
+ self,
1900
+ start=0,
1901
+ limit=None,
1902
+ closed_since=None,
1903
+ role=None,
1904
+ participant_status=None,
1905
+ state=None,
1906
+ order=None,
1907
+ ):
1908
+ """
1909
+ Get all pull requests where the current authenticated user is
1910
+ involved as either a reviewer, author or a participant
1911
+ :param start:
1912
+ :param limit:
1913
+ :param closed_since: OPTIONAL, defaults to returning pull
1914
+ requests regardless of closed since date. Permits
1915
+ returning only pull requests with a closed timestamp set more
1916
+ recently that (now - closedSince). Units are in seconds. So
1917
+ for example if closed since 86400 is set only pull requests
1918
+ closed in the previous 24 hours will be returned.
1919
+ :param role: OPTIONAL, defaults to returning pull requests for
1920
+ any role. If a role is supplied only pull requests where the
1921
+ authenticated user is a participant in the given role will be
1922
+ returned. Either REVIEWER, AUTHOR or PARTICIPANT.
1923
+ :param participant_status: OPTIONAL, defaults to returning
1924
+ pull requests with any participant status. A comma separated
1925
+ list of participant status. That is, one or more of
1926
+ UNAPPROVED, NEEDS_WORK, or APPROVED.
1927
+ :param state: OPTIONAL, defaults to returning pull requests in
1928
+ any state. If a state is supplied only pull requests in the
1929
+ specified state will be returned. Either OPEN, DECLINED or
1930
+ MERGED. Omit this parameter to return pull request in any
1931
+ state.
1932
+
1933
+ :param order: OPTIONAL, defaults to NEWEST, the order to
1934
+ return pull requests in, either OLDEST (as in: "oldest
1935
+ first"), NEWEST, PARTICIPANT_STATUS, or CLOSED_DATE. Where
1936
+ CLOSED_DATE is specified and the result set includes pull
1937
+ requests that are not in the closed state, these pull requests
1938
+ will appear first in the result set, followed by most recently
1939
+ closed pull requests.
1940
+ :return:
1941
+ """
1942
+ if self.cloud:
1943
+ raise Exception("Not supported in Bitbucket Cloud")
1944
+ url = self.resource_url("dashboard/pull-requests")
1945
+ params = {}
1946
+ if start:
1947
+ params["start"] = start
1948
+ if limit is not None:
1949
+ params["limit"] = limit
1950
+ if closed_since is not None:
1951
+ params["closedSince"] = closed_since
1952
+ if role is not None:
1953
+ params["role"] = role
1954
+ if participant_status is not None:
1955
+ params["participantStatus"] = participant_status
1956
+ if state is not None:
1957
+ params["state"] = state
1958
+ if order is not None:
1959
+ params["order"] = order
1960
+ return self._get_paged(url, params=params)
1961
+
1962
+ def change_reviewed_status(self, project_key, repository_slug, pull_request_id, status, user_slug):
1963
+ """
1964
+ Change the current user's status for a pull request.
1965
+ Implicitly adds the user as a participant if they are not already.
1966
+ If the current user is the author, this method will fail.
1967
+ :param project_key
1968
+ :param repository_slug:
1969
+ :param pull_request_id:
1970
+ :param status:
1971
+ :param user_slug:
1972
+ :return:
1973
+ """
1974
+ url = "{}/{}".format(
1975
+ self._url_pull_request_participants(project_key, repository_slug, pull_request_id),
1976
+ user_slug,
1977
+ )
1978
+ approved = True if status == "APPROVED" else False
1979
+ data = {
1980
+ "user": {"name": user_slug},
1981
+ "approved": approved,
1982
+ "status": status,
1983
+ }
1984
+ return self.put(url, data)
1985
+
1986
+ def _url_pull_request_comments(self, project_key, repository_slug, pull_request_id):
1987
+ url = "{}/comments".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
1988
+ return url
1989
+
1990
+ def add_pull_request_comment(
1991
+ self,
1992
+ project_key,
1993
+ repository_slug,
1994
+ pull_request_id,
1995
+ text,
1996
+ parent_id=None,
1997
+ ):
1998
+ """
1999
+ Add comment into pull request
2000
+ :param project_key:
2001
+ :param repository_slug:
2002
+ :param pull_request_id: the ID of the pull request within the repository
2003
+ :param text comment text
2004
+ :param parent_id parent comment id
2005
+
2006
+ :return:
2007
+ """
2008
+ url = self._url_pull_request_comments(project_key, repository_slug, pull_request_id)
2009
+ body = {"text": text}
2010
+ if parent_id:
2011
+ body["parent"] = {"id": parent_id}
2012
+ return self.post(url, data=body)
2013
+
2014
+ def _url_pull_request_comment(self, project_key, repository_slug, pull_request_id, comment_id):
2015
+ url = "{}/{}".format(
2016
+ self._url_pull_request_comments(project_key, repository_slug, pull_request_id),
2017
+ comment_id,
2018
+ )
2019
+ return url
2020
+
2021
+ def get_pull_request_comment(self, project_key, repository_slug, pull_request_id, comment_id):
2022
+ """
2023
+ Retrieves a pull request comment.
2024
+ The authenticated user must have REPO_READ permission
2025
+ for the repository that this pull request targets to call this resource.
2026
+ :param project_key:
2027
+ :param repository_slug:
2028
+ :param pull_request_id: the ID of the pull request within the repository
2029
+ :param comment_id: the ID of the comment to retrieve
2030
+ :return:
2031
+ """
2032
+ url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
2033
+ return self.get(url)
2034
+
2035
+ def update_pull_request_comment(
2036
+ self,
2037
+ project_key,
2038
+ repository_slug,
2039
+ pull_request_id,
2040
+ comment_id,
2041
+ comment,
2042
+ comment_version,
2043
+ ):
2044
+ """
2045
+ Update the text of a comment.
2046
+ Only the user who created a comment may update it.
2047
+
2048
+ Note: the supplied JSON object must contain a version
2049
+ that must match the server's version of the comment
2050
+ or the update will fail.
2051
+ """
2052
+ url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
2053
+ data = {"version": comment_version, "text": comment}
2054
+ return self.put(url, data=data)
2055
+
2056
+ @deprecated(version="2.4.2", reason="Use delete_pull_request_comment()")
2057
+ def delete_pull_reques_comment(
2058
+ self,
2059
+ project_key,
2060
+ repository_slug,
2061
+ pull_request_id,
2062
+ comment_id,
2063
+ comment_version,
2064
+ ):
2065
+ """
2066
+ Deprecated name since 2.4.2. Let's use the get_pull_request()
2067
+ """
2068
+ return self.delete_pull_request_comment(
2069
+ project_key,
2070
+ repository_slug,
2071
+ pull_request_id,
2072
+ comment_id,
2073
+ comment_version,
2074
+ )
2075
+
2076
+ def delete_pull_request_comment(
2077
+ self,
2078
+ project_key,
2079
+ repository_slug,
2080
+ pull_request_id,
2081
+ comment_id,
2082
+ comment_version,
2083
+ ):
2084
+ """
2085
+ Delete a comment.
2086
+ Only the repository admin or user who created a comment may update it.
2087
+
2088
+ Note: the supplied JSON object must contain a version
2089
+ that must match the server's version of the comment
2090
+ or delete will fail.
2091
+ """
2092
+ url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
2093
+ data = {"version": comment_version}
2094
+ return self.delete(url, params=data)
2095
+
2096
+ def decline_pull_request(self, project_key, repository_slug, pr_id, pr_version):
2097
+ """
2098
+ Decline a pull request.
2099
+ The authenticated user must have REPO_READ permission for the repository
2100
+ that this pull request targets to call this resource.
2101
+
2102
+ :param project_key: PROJECT
2103
+ :param repository_slug: my_shiny_repo
2104
+ :param pr_id: 2341
2105
+ :param pr_version: 12
2106
+ :return:
2107
+ """
2108
+ url = "{}/decline".format(self._url_pull_request(project_key, repository_slug, pr_id))
2109
+ params = {}
2110
+ if not self.cloud:
2111
+ params["version"] = pr_version
2112
+ return self.post(url, params=params)
2113
+
2114
+ def get_tasks(self, project_key, repository_slug, pull_request_id):
2115
+ """
2116
+ Get all tasks for the pull request
2117
+ :param project_key:
2118
+ :param repository_slug:
2119
+ :param pull_request_id: the ID of the pull request within the repository
2120
+ :return:
2121
+ """
2122
+ if self.cloud:
2123
+ raise Exception("Not supported in Bitbucket Cloud")
2124
+ url = "{}/tasks".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
2125
+ return self.get(url)
2126
+
2127
+ def _url_tasks(self):
2128
+ if self.cloud:
2129
+ raise Exception("Not supported in Bitbucket Cloud")
2130
+ return self.resource_url("tasks")
2131
+
2132
+ def add_task(self, anchor, text):
2133
+ """
2134
+ Add task to the comment
2135
+ :param anchor: ID of the comment,
2136
+ :param text: task text
2137
+ :return:
2138
+ """
2139
+ url = self._url_tasks()
2140
+ data = {"anchor": {"id": anchor, "type": "COMMENT"}, "text": text}
2141
+ return self.post(url, data=data)
2142
+
2143
+ def _url_task(self, task_id):
2144
+ return "{}/{}".format(self._url_tasks(), task_id)
2145
+
2146
+ def get_task(self, task_id):
2147
+ """
2148
+ Get task information by ID
2149
+ :param task_id:
2150
+ :return:
2151
+ """
2152
+ url = self._url_task(task_id)
2153
+ return self.get(url)
2154
+
2155
+ def delete_task(self, task_id):
2156
+ """
2157
+ Delete task by ID
2158
+ :param task_id:
2159
+ :return:
2160
+ """
2161
+ url = self._url_task(task_id)
2162
+ return self.delete(url)
2163
+
2164
+ def update_task(self, task_id, text=None, state=None):
2165
+ """
2166
+ Update task by ID. It is possible to update state and/or text of the task
2167
+ :param task_id:
2168
+ :param text:
2169
+ :param state: OPEN, RESOLVED
2170
+ :return:
2171
+ """
2172
+ url = self._url_task(task_id)
2173
+ data = {"id": task_id}
2174
+ if text:
2175
+ data["text"] = text
2176
+ if state:
2177
+ data["state"] = state
2178
+ return self.put(url, data=data)
2179
+
2180
+ def is_pull_request_can_be_merged(self, project_key, repository_slug, pr_id):
2181
+ """
2182
+ Test whether a pull request can be merged.
2183
+ A pull request may not be merged if:
2184
+ - there are conflicts that need to be manually resolved before merging; and/or
2185
+ - one or more merge checks have vetoed the merge.
2186
+ The authenticated user must have REPO_READ permission for the repository
2187
+ that this pull request targets to call this resource.
2188
+
2189
+ :param project_key: PROJECT
2190
+ :param repository_slug: my_shiny_repo
2191
+ :param pr_id: 2341
2192
+ :return:
2193
+ """
2194
+ url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id))
2195
+ return self.get(url)
2196
+
2197
+ def merge_pull_request(self, project_key, repository_slug, pr_id, pr_version):
2198
+ """
2199
+ Merge pull request
2200
+ The authenticated user must have REPO_READ permission for the repository
2201
+ that this pull request targets to call this resource.
2202
+
2203
+ :param project_key: PROJECT
2204
+ :param repository_slug: my_shiny_repo
2205
+ :param pr_id: 2341
2206
+ :param pr_version:
2207
+ :return:
2208
+ """
2209
+ url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id))
2210
+ params = {}
2211
+ if not self.cloud:
2212
+ params["version"] = pr_version
2213
+ return self.post(url, params=params)
2214
+
2215
+ def reopen_pull_request(self, project_key, repository_slug, pr_id, pr_version):
2216
+ """
2217
+ Re-open a declined pull request.
2218
+ The authenticated user must have REPO_READ permission for the repository
2219
+ that this pull request targets to call this resource.
2220
+
2221
+ :param project_key: PROJECT
2222
+ :param repository_slug: my_shiny_repo
2223
+ :param pr_id: 2341
2224
+ :param pr_version: 12
2225
+ :return:
2226
+ """
2227
+ url = "{}/reopen".format(self._url_pull_request(project_key, repository_slug, pr_id))
2228
+ params = {"version": pr_version}
2229
+ return self.post(url, params=params)
2230
+
2231
+ def _url_inbox_pull_requests(self):
2232
+ return "inbox/pull-requests"
2233
+
2234
+ def check_inbox_pull_requests_count(self):
2235
+ url = "{}/count".format(self._url_inbox_pull_requests())
2236
+ return self.get(url)
2237
+
2238
+ def check_inbox_pull_requests(self, start=0, limit=None, role=None):
2239
+ """
2240
+ Get pull request in your inbox
2241
+ :param start:
2242
+ :param limit:
2243
+ :param role:
2244
+ :return:
2245
+ """
2246
+ url = self._url_inbox_pull_requests()
2247
+ params = {"start": start}
2248
+ if limit:
2249
+ params["limit"] = limit
2250
+ if role:
2251
+ params["role"] = role
2252
+ return self._get_paged(url, params=params)
2253
+
2254
+ def _url_repo_compare(self, project_key, repository_slug):
2255
+ url = "{}/compare".format(self._url_repo(project_key, repository_slug))
2256
+ return url
2257
+
2258
+ def get_diff(self, project_key, repository_slug, path, hash_oldest, hash_newest):
2259
+ """
2260
+ Gets a diff of the changes available in the {@code from} commit but not in the {@code to} commit.
2261
+ If either the {@code from} or {@code to} commit are not specified,
2262
+ they will be replaced by the default branch of their containing repository.
2263
+ :param project_key:
2264
+ :param repository_slug:
2265
+ :param path:
2266
+ :param hash_oldest: the source commit (can be a partial/full commit ID or qualified/unqualified ref name)
2267
+ :param hash_newest: the target commit (can be a partial/full commit ID or qualified/unqualified ref name)
2268
+ :return:
2269
+ """
2270
+ url = "{}/diff/{}".format(self._url_repo_compare(project_key, repository_slug), path)
2271
+ params = {}
2272
+ if hash_oldest:
2273
+ params["from"] = hash_oldest
2274
+ if hash_newest:
2275
+ params["to"] = hash_newest
2276
+ return (self.get(url, params=params) or {}).get("diffs")
2277
+
2278
+ def _url_commits(self, project_key, repository_slug, api_root=None, api_version=None):
2279
+ return "{}/commits".format(
2280
+ self._url_repo(
2281
+ project_key,
2282
+ repository_slug,
2283
+ api_root=api_root,
2284
+ api_version=api_version,
2285
+ )
2286
+ )
2287
+
2288
+ def get_commits(
2289
+ self,
2290
+ project_key,
2291
+ repository_slug,
2292
+ hash_oldest=None,
2293
+ hash_newest=None,
2294
+ follow_renames=False,
2295
+ ignore_missing=False,
2296
+ merges="include",
2297
+ with_counts=False,
2298
+ avatar_size=None,
2299
+ avatar_scheme=None,
2300
+ limit=None,
2301
+ until=None,
2302
+ since=None,
2303
+ ):
2304
+ """
2305
+ Get commit list from repo
2306
+ :param project_key:
2307
+ :param repository_slug:
2308
+ :param hash_oldest:
2309
+ :param hash_newest:
2310
+ :param merges: OPTIONAL: include|exclude|only if present, controls how merge commits should be filtered.
2311
+ :param follow_renames: OPTIONAL: if true, the commit history of the specified file will be followed past renames
2312
+ :param ignore_missing: OPTIONAL: true to ignore missing commits, false otherwise
2313
+ :param with_counts: OPTIONAL: optionally include the total number of commits and total number of unique authors
2314
+ :param avatar_size: OPTIONAL: if present the service adds avatar URLs for commit authors.
2315
+ :param avatar_scheme: OPTIONAL: the desired scheme for the avatar URL
2316
+ :param limit: OPTIONAL: The limit of the number of commits to return, this may be restricted by
2317
+ fixed system limits. Default by built-in method: None
2318
+ :param until: OPTIONAL: The commit ID or ref (inclusively) to retrieve commits before
2319
+ :param since: OPTIONAL: The commit ID or ref (exclusively) to retrieve commits after
2320
+ :return:
2321
+ """
2322
+ url = self._url_commits(project_key, repository_slug)
2323
+ params = {"merges": merges}
2324
+ if hash_oldest:
2325
+ params["since"] = hash_oldest
2326
+ if hash_newest:
2327
+ params["until"] = hash_newest
2328
+ if follow_renames:
2329
+ params["followRenames"] = follow_renames
2330
+ if ignore_missing:
2331
+ params["ignoreMissing"] = ignore_missing
2332
+ if with_counts:
2333
+ params["withCounts"] = with_counts
2334
+ if avatar_size:
2335
+ params["avatarSize"] = avatar_size
2336
+ if avatar_scheme:
2337
+ params["avatarScheme"] = avatar_scheme
2338
+ if limit:
2339
+ params["limit"] = limit
2340
+ if self.cloud and (since or until):
2341
+ raise Exception("Not supported in Bitbucket Cloud")
2342
+ if since:
2343
+ params["since"] = since
2344
+ if until:
2345
+ params["until"] = until
2346
+ return self._get_paged(url, params=params)
2347
+
2348
+ def get_commit_changes(self, project_key, repository_slug, hash_newest=None, merges="include", commit_id=None):
2349
+ """
2350
+ Get commit list from repo
2351
+ :param project_key:
2352
+ :param repository_slug:
2353
+ :param hash_newest:
2354
+ :param merges: OPTIONAL: include|exclude|only if present, controls how merge commits should be filtered.
2355
+ :param commit_id
2356
+ :return:
2357
+ """
2358
+ url = self._url_commit_c(project_key, repository_slug, commit_id=commit_id)
2359
+ params = {"merges": merges}
2360
+ if hash_newest:
2361
+ params["until"] = hash_newest
2362
+ return self.get(url, params=params)
2363
+
2364
+ def _url_commit_c(self, project_key, repository_slug, api_root=None, api_version=None, commit_id=None):
2365
+ return "{}/commits/{}/changes".format(
2366
+ self._url_repo(
2367
+ project_key,
2368
+ repository_slug,
2369
+ api_root=api_root,
2370
+ api_version=api_version,
2371
+ ),
2372
+ commit_id,
2373
+ )
2374
+
2375
+ def _url_commit(
2376
+ self,
2377
+ project_key,
2378
+ repository_slug,
2379
+ commit_id,
2380
+ api_root=None,
2381
+ api_version=None,
2382
+ ):
2383
+ return "{}/{}".format(
2384
+ self._url_commits(
2385
+ project_key,
2386
+ repository_slug,
2387
+ api_root=api_root,
2388
+ api_version=api_version,
2389
+ ),
2390
+ commit_id,
2391
+ )
2392
+
2393
+ def get_commit_info(self, project_key, repository_slug, commit, path=None):
2394
+ """
2395
+ Retrieve a single commit identified by its ID>. In general, that ID is a SHA1.
2396
+ From 2.11, ref names like "refs/heads/master" are no longer accepted by this resource.
2397
+ The authenticated user must have REPO_READ permission for the specified repository to call this resource.
2398
+ :param project_key:
2399
+ :param repository_slug:
2400
+ :param commit: the commit ID to retrieve
2401
+ :param path :OPTIONAL an optional path to filter the commit by.
2402
+ If supplied the details returned may not be for the specified commit.
2403
+ Instead, starting from the specified commit, they will be the details for the first commit
2404
+ affecting the specified path.
2405
+ :return:
2406
+ """
2407
+
2408
+ url = self._url_commit(project_key, repository_slug, commit)
2409
+ params = {}
2410
+ if path:
2411
+ params["path"] = path
2412
+ return self.get(url, params=params)
2413
+
2414
+ def _url_commit_pull_requests(self, project_key, repository_slug, commit_id):
2415
+ return "{}/pull-requests".format(self._url_commit(project_key, repository_slug, commit_id))
2416
+
2417
+ def get_pull_requests_contain_commit(self, project_key, repository_slug, commit):
2418
+ url = self._url_commit_pull_requests(project_key, repository_slug, commit)
2419
+ return (self.get(url) or {}).get("values")
2420
+
2421
+ def get_changelog(
2422
+ self,
2423
+ project_key,
2424
+ repository_slug,
2425
+ ref_from,
2426
+ ref_to,
2427
+ start=0,
2428
+ limit=None,
2429
+ ):
2430
+ """
2431
+ Get change log between 2 refs
2432
+ :param start:
2433
+ :param project_key:
2434
+ :param repository_slug:
2435
+ :param ref_from:
2436
+ :param ref_to:
2437
+ :param limit: OPTIONAL: The limit of the number of changes to return, this may be restricted by
2438
+ fixed system limits. Default by built-in method: None
2439
+ :return:
2440
+ """
2441
+ url = "{}/compare/commits".format(self._url_repo(project_key, repository_slug))
2442
+ params = {}
2443
+ if ref_from:
2444
+ params["from"] = ref_from
2445
+ if ref_to:
2446
+ params["to"] = ref_to
2447
+ if start:
2448
+ params["start"] = start
2449
+ if limit:
2450
+ params["limit"] = limit
2451
+ return self._get_paged(url, params=params)
2452
+
2453
+ def _url_code_insights_annotations(self, project_key, repository_slug, commit_id, report_key):
2454
+ return "{}/reports/{}/annotations".format(
2455
+ self._url_commit(
2456
+ project_key,
2457
+ repository_slug,
2458
+ commit_id,
2459
+ api_root="rest/insights",
2460
+ api_version="1.0",
2461
+ ),
2462
+ report_key,
2463
+ )
2464
+
2465
+ def add_code_insights_annotations_to_report(self, project_key, repository_slug, commit_id, report_key, annotations):
2466
+ """
2467
+ Adds annotations to an existing insight report.
2468
+ For further information visit:
2469
+ https://docs.atlassian.com/bitbucket-server/rest/6.6.1/bitbucket-code-insights-rest.html
2470
+ :project_key: str
2471
+ :repository_slug: str
2472
+ :commit_id: str
2473
+ :report_key: str
2474
+ :annotations: list
2475
+ """
2476
+ url = self._url_code_insights_annotations(project_key, repository_slug, commit_id, report_key)
2477
+ data = {"annotations": annotations}
2478
+ return self.post(url, data=data)
2479
+
2480
+ def _url_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
2481
+ return "{}/reports/{}".format(
2482
+ self._url_commit(
2483
+ project_key,
2484
+ repository_slug,
2485
+ commit_id,
2486
+ api_root="rest/insights",
2487
+ api_version="1.0",
2488
+ ),
2489
+ report_key,
2490
+ )
2491
+
2492
+ def get_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
2493
+ """
2494
+ Retrieve the specified code-insights report.
2495
+ :projectKey: str
2496
+ :repositorySlug: str
2497
+ :commit_id: str
2498
+ :report_key: str
2499
+ """
2500
+ url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
2501
+ return self.get(url)
2502
+
2503
+ def delete_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
2504
+ """
2505
+ Delete a report for the given commit. Also deletes any annotations associated with this report.
2506
+ :projectKey: str
2507
+ :repositorySlug: str
2508
+ :commit_id: str
2509
+ :report_key: str
2510
+ """
2511
+ url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
2512
+ return self.delete(url)
2513
+
2514
+ def create_code_insights_report(
2515
+ self,
2516
+ project_key,
2517
+ repository_slug,
2518
+ commit_id,
2519
+ report_key,
2520
+ report_title,
2521
+ **report_params
2522
+ ): # fmt: skip
2523
+ """
2524
+ Create a new insight report, or replace the existing one
2525
+ if a report already exists for the given repository_slug, commit, and report key.
2526
+ A request to replace an existing report will be rejected
2527
+ if the authenticated user was not the creator of the specified report.
2528
+ For further information visit:
2529
+ https://docs.atlassian.com/bitbucket-server/rest/6.6.1/bitbucket-code-insights-rest.html
2530
+ :projectKey: str
2531
+ :repositorySlug: str
2532
+ :commitId: str
2533
+ :report_key: str
2534
+ :report_title: str
2535
+ :report_params:
2536
+ """
2537
+ url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
2538
+ data = {"title": report_title}
2539
+ data.update(report_params)
2540
+ return self.put(url, data=data)
2541
+
2542
+ def get_file_list(
2543
+ self,
2544
+ project_key,
2545
+ repository_slug,
2546
+ sub_folder=None,
2547
+ query=None,
2548
+ start=0,
2549
+ limit=None,
2550
+ ):
2551
+ """
2552
+ Retrieve a page of files from particular directory of a repository.
2553
+ The search is done recursively, so all files from any subdirectory of the specified directory will be returned.
2554
+ The authenticated user must have REPO_READ permission for the specified repository to call this resource.
2555
+ :param start:
2556
+ :param project_key:
2557
+ :param repository_slug:
2558
+ :param sub_folder: a sub folder in the target repository to list the files from.
2559
+ :param query: the commit ID or ref (e.g. a branch or tag) to list the files at.
2560
+ If not specified the default branch will be used instead.
2561
+ :param limit: OPTIONAL
2562
+ :return:
2563
+ """
2564
+ url = "{}/files".format(self._url_repo(project_key, repository_slug))
2565
+ if sub_folder:
2566
+ url = "{}/{}".format(url, sub_folder.lstrip("/"))
2567
+ params = {}
2568
+ if query:
2569
+ params["at"] = query
2570
+ if start:
2571
+ params["start"] = start
2572
+ if limit:
2573
+ params["limit"] = limit
2574
+ return self._get_paged(url, params=params)
2575
+
2576
+ def get_content_of_file(self, project_key, repository_slug, filename, at=None, markup=None):
2577
+ """
2578
+ Retrieve the raw content for a file path at a specified revision.
2579
+ The authenticated user must have REPO_READ permission for the specified repository to call this resource.
2580
+ :param project_key:
2581
+ :param repository_slug:
2582
+ :param filename:
2583
+ :param at: OPTIONAL ref string
2584
+ :param markup: if present or "true", triggers the raw content to be markup-rendered and returned as HTML;
2585
+ otherwise, if not specified, or any value other than "true" the content is streamed without markup.
2586
+ :return:
2587
+ """
2588
+ url = "{}/raw/{}".format(self._url_repo(project_key, repository_slug), filename)
2589
+ params = {}
2590
+ if at is not None:
2591
+ params["at"] = at
2592
+ if markup is not None:
2593
+ params["markup"] = markup
2594
+ headers = self.form_token_headers
2595
+ return self.get(url, params=params, not_json_response=True, headers=headers)
2596
+
2597
+ def _url_branches_permissions(self, project_key, permission_id=None, repository_slug=None):
2598
+ if repository_slug is None:
2599
+ base = self._url_project(
2600
+ project_key,
2601
+ api_root="rest/branch-permissions",
2602
+ api_version="2.0",
2603
+ )
2604
+ else:
2605
+ base = self._url_repo(
2606
+ project_key,
2607
+ repository_slug,
2608
+ api_root="rest/branch-permissions",
2609
+ api_version="2.0",
2610
+ )
2611
+
2612
+ return "{}/restrictions/{}".format(base, "" if permission_id is None else str(permission_id))
2613
+
2614
+ def get_branches_permissions(
2615
+ self,
2616
+ project_key,
2617
+ permission_id,
2618
+ repository_slug=None,
2619
+ start=0,
2620
+ limit=25,
2621
+ ):
2622
+ """
2623
+ Get branches permissions from a given repo
2624
+ :param project_key:
2625
+ :param permission_id:
2626
+ :param repository_slug:
2627
+ :param start:
2628
+ :param limit:
2629
+ :return:
2630
+ """
2631
+ url = self._url_branches_permissions(project_key, permission_id, repository_slug)
2632
+ params = {}
2633
+ if limit:
2634
+ params["limit"] = limit
2635
+ if start:
2636
+ params["start"] = start
2637
+ return self.get(url, params=params)
2638
+
2639
+ def set_branches_permissions(
2640
+ self,
2641
+ project_key,
2642
+ multiple_permissions=False,
2643
+ matcher_type=None,
2644
+ matcher_value=None,
2645
+ permission_type=None,
2646
+ repository_slug=None,
2647
+ except_users=None,
2648
+ except_groups=None,
2649
+ except_access_keys=None,
2650
+ start=0,
2651
+ limit=25,
2652
+ ):
2653
+ """
2654
+ Create a restriction for the supplied branch or set of branches to be applied to the given repository.
2655
+ Allows creating multiple restrictions at once.
2656
+ To use multiple restrictions you should format payload manually -
2657
+ see the bitbucket-branch-restrictions.py example.
2658
+ Reference: https://docs.atlassian.com/bitbucket-server/rest/6.8.0/bitbucket-ref-restriction-rest.html
2659
+ :param project_key:
2660
+ :param multiple_permissions:
2661
+ :param matcher_type:
2662
+ :param matcher_value:
2663
+ :param permission_type:
2664
+ :param repository_slug:
2665
+ :param except_users:
2666
+ :param except_groups:
2667
+ :param except_access_keys:
2668
+ :param start:
2669
+ :param limit:
2670
+ :return:
2671
+ """
2672
+ url = self._url_branches_permissions(project_key=project_key, repository_slug=repository_slug)
2673
+ if except_users is None:
2674
+ except_users = []
2675
+ if except_groups is None:
2676
+ except_groups = []
2677
+ if except_access_keys is None:
2678
+ except_access_keys = []
2679
+ headers = self.default_headers
2680
+ if multiple_permissions:
2681
+ headers = self.bulk_headers
2682
+ restriction = multiple_permissions
2683
+ else:
2684
+ restriction = {
2685
+ "type": permission_type,
2686
+ "matcher": {
2687
+ "id": matcher_value,
2688
+ "displayId": matcher_value,
2689
+ "type": {
2690
+ "id": matcher_type.upper(),
2691
+ "name": matcher_type.capitalize(),
2692
+ },
2693
+ "active": True,
2694
+ },
2695
+ "users": except_users,
2696
+ "groups": except_groups,
2697
+ "accessKeys": except_access_keys,
2698
+ }
2699
+ params = {"start": start, "limit": limit}
2700
+ return self.post(url, data=restriction, params=params, headers=headers)
2701
+
2702
+ def delete_branch_permission(self, project_key, permission_id, repository_slug=None):
2703
+ """
2704
+ Deletes a restriction as specified by a restriction id.
2705
+ The authenticated user must have REPO_ADMIN permission or higher to call this resource.
2706
+
2707
+ :param project_key:
2708
+ :param repository_slug:
2709
+ :param permission_id:
2710
+ :return:
2711
+ """
2712
+ url = self._url_branches_permissions(project_key, permission_id, repository_slug)
2713
+ return self.delete(url)
2714
+
2715
+ def get_branch_permission(self, project_key, permission_id, repository_slug=None):
2716
+ """
2717
+ Returns a restriction as specified by a restriction id.
2718
+ The authenticated user must have REPO_ADMIN permission or higher to call this resource.
2719
+
2720
+ :param project_key:
2721
+ :param repository_slug:
2722
+ :param permission_id:
2723
+ :return:
2724
+ """
2725
+ url = self._url_branches_permissions(project_key, permission_id, repository_slug)
2726
+ return self._get_paged(url)
2727
+
2728
+ def all_branches_permissions(self, project_key, permission_id, repository_slug=None):
2729
+ """
2730
+ Get branches permissions from a given repo
2731
+ :param project_key:
2732
+ :param permission_id
2733
+ :param repository_slug:
2734
+ :return:
2735
+ """
2736
+ url = self._url_branches_permissions(project_key, permission_id, repository_slug)
2737
+ return self._get_paged(url)
2738
+
2739
+ def _url_branching_model(self, project_key, repository_slug):
2740
+ return "{}/branchmodel/configuration".format(
2741
+ self._url_repo(
2742
+ project_key,
2743
+ repository_slug,
2744
+ api_root="rest/branch-utils",
2745
+ api_version="1.0",
2746
+ )
2747
+ )
2748
+
2749
+ def get_branching_model(self, project_key, repository_slug):
2750
+ """
2751
+ Get branching model
2752
+ :param project_key:
2753
+ :param repository_slug:
2754
+ :return:
2755
+ """
2756
+ url = self._url_branching_model(project_key, repository_slug)
2757
+ return self.get(url)
2758
+
2759
+ def set_branching_model(self, project_key, repository_slug, data):
2760
+ """
2761
+ Set branching model
2762
+ :param project_key:
2763
+ :param repository_slug:
2764
+ :param data:
2765
+ :return:
2766
+ """
2767
+ url = self._url_branching_model(project_key, repository_slug)
2768
+ return self.put(url, data=data)
2769
+
2770
+ def enable_branching_model(self, project_key, repository_slug):
2771
+ """
2772
+ Enable branching model by setting it with default configuration
2773
+ :param project_key:
2774
+ :param repository_slug:
2775
+ :return:
2776
+ """
2777
+ default_model_data = {
2778
+ "development": {"refId": None, "useDefault": True},
2779
+ "types": [
2780
+ {
2781
+ "displayName": "Bugfix",
2782
+ "enabled": True,
2783
+ "id": "BUGFIX",
2784
+ "prefix": "bugfix/",
2785
+ },
2786
+ {
2787
+ "displayName": "Feature",
2788
+ "enabled": True,
2789
+ "id": "FEATURE",
2790
+ "prefix": "feature/",
2791
+ },
2792
+ {
2793
+ "displayName": "Hotfix",
2794
+ "enabled": True,
2795
+ "id": "HOTFIX",
2796
+ "prefix": "hotfix/",
2797
+ },
2798
+ {
2799
+ "displayName": "Release",
2800
+ "enabled": True,
2801
+ "id": "RELEASE",
2802
+ "prefix": "release/",
2803
+ },
2804
+ ],
2805
+ }
2806
+ return self.set_branching_model(project_key, repository_slug, default_model_data)
2807
+
2808
+ def disable_branching_model(self, project_key, repository_slug):
2809
+ """
2810
+ Disable branching model
2811
+ :param project_key:
2812
+ :param repository_slug:
2813
+ :return:
2814
+ """
2815
+ return self.delete(self._url_branching_model(project_key, repository_slug))
2816
+
2817
+ def _url_file(self, project_key, repository_slug, filename):
2818
+ return "{}/browse/{}".format(self._url_repo(project_key, repository_slug), filename)
2819
+
2820
+ def upload_file(self, project_key, repository_slug, content, message, branch, filename):
2821
+ """
2822
+ Upload new file for given branch.
2823
+ :param project_key:
2824
+ :param repository_slug:
2825
+ :param content:
2826
+ :param message:
2827
+ :param branch:
2828
+ :param filename
2829
+ :return:
2830
+ """
2831
+ url = self._url_file(project_key, repository_slug, filename)
2832
+ data = {"content": content, "message": message, "branch": branch}
2833
+ return self.put(url, files=data, headers={"Accept": "application/json"})
2834
+
2835
+ def update_file(
2836
+ self,
2837
+ project_key,
2838
+ repository_slug,
2839
+ content,
2840
+ message,
2841
+ branch,
2842
+ filename,
2843
+ source_commit_id,
2844
+ ):
2845
+ """
2846
+ Update existing file for given branch.
2847
+ :param project_key:
2848
+ :param repository_slug:
2849
+ :param content:
2850
+ :param message:
2851
+ :param branch:
2852
+ :param filename:
2853
+ :param source_commit_id:
2854
+ :return:
2855
+ """
2856
+ url = self._url_file(project_key, repository_slug, filename)
2857
+ data = {
2858
+ "content": content,
2859
+ "message": message,
2860
+ "branch": branch,
2861
+ "sourceCommitId": source_commit_id,
2862
+ }
2863
+ return self.put(url, files=data, headers={"Accept": "application/json"})
2864
+
2865
+ def search_code(self, team, search_query, page=1, limit=10):
2866
+ """
2867
+ Search repositories for matching code
2868
+ :team: str
2869
+ :search_query: str
2870
+ """
2871
+ url = self.resource_url("teams/{team}/search/code".format(team=team))
2872
+ return self.get(
2873
+ url,
2874
+ params={"search_query": search_query, "page": page, "pagelen": limit},
2875
+ )
2876
+
2877
+ def get_lfs_repo_status(self, project_key, repo):
2878
+ url = "rest/git-lfs/admin/projects/{projectKey}/repos/{repositorySlug}/enabled".format(
2879
+ projectKey=project_key, repositorySlug=repo
2880
+ )
2881
+ return self.get(url)
2882
+
2883
+ def set_lfs_repo_status(self, project_key, repo, enable=True):
2884
+ url = "rest/git-lfs/admin/projects/{projectKey}/repos/{repositorySlug}/enabled".format(
2885
+ projectKey=project_key, repositorySlug=repo
2886
+ )
2887
+ if enable:
2888
+ return self.put(url)
2889
+ else:
2890
+ return self.delete(url)
2891
+
2892
+ def _url_repo_conditions(self, project_key, repo_key):
2893
+ return "{}/conditions".format(
2894
+ self._url_repo(
2895
+ project_key,
2896
+ repo_key,
2897
+ api_root="rest/default-reviewers",
2898
+ api_version="1.0",
2899
+ )
2900
+ )
2901
+
2902
+ def get_repo_conditions(self, project_key, repo_key):
2903
+ """
2904
+ Request type: GET
2905
+ Return a page of defaults conditions with reviewers list (type REPOSITORY or PROJECT)
2906
+ that have been configured for this repository slug inside project specified.
2907
+ For further information visit:
2908
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
2909
+ :projectKey: str- project key involved
2910
+ :repoKey: str - repo key involved
2911
+ :return:
2912
+ """
2913
+ url = self._url_repo_conditions(project_key, repo_key)
2914
+ return self.get(url) or {}
2915
+
2916
+ def get_repo_project_conditions(self, project_key, repo_key):
2917
+ """
2918
+ Request type: GET
2919
+ Return a page of repository conditions (only type PROJECT) with reviewers list associated
2920
+ that have been configured for this repository slug inside project specified.
2921
+ For further information visit:
2922
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
2923
+ :projectKey: str- project key involved
2924
+ :repoKey: str - repo key involved
2925
+ :return:
2926
+ """
2927
+ response = self.get_repo_conditions(project_key, repo_key)
2928
+ count = 0
2929
+ for condition in response:
2930
+ if condition["scope"]["type"] == "REPOSITORY":
2931
+ del response[count]
2932
+ count += 1
2933
+ return response
2934
+
2935
+ def get_repo_repo_conditions(self, project_key, repo_key):
2936
+ """
2937
+ Request type: GET
2938
+ Return a page of repository conditions (only type REPOSITORY) with reviewers list associated
2939
+ that have been configured for this repository slug inside project specified.
2940
+ For further information visit:
2941
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
2942
+ :projectKey: str- project key involved
2943
+ :repoKey: str - repo key involved
2944
+ :return:
2945
+ """
2946
+ response = self.get_repo_conditions(project_key, repo_key)
2947
+ count = 0
2948
+ for condition in response:
2949
+ if condition["scope"]["type"] == "PROJECT":
2950
+ del response[count]
2951
+ count += 1
2952
+ return response
2953
+
2954
+ def _url_repo_condition(self, project_key, repo_key, id_condition=None):
2955
+ return "{}/condition/{}".format(
2956
+ self._url_repo(
2957
+ project_key,
2958
+ repo_key,
2959
+ api_root="rest/default-reviewers",
2960
+ api_version="1.0",
2961
+ ),
2962
+ "" if id_condition is None else str(id_condition),
2963
+ )
2964
+
2965
+ def get_repo_condition(self, project_key, repo_key, id_condition):
2966
+ """
2967
+ Request type: GET
2968
+ Return a specific condition with reviewers list
2969
+ that have been configured for this repository slug inside project specified.
2970
+ For further information visit:
2971
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
2972
+ :projectKey: str- project key involved
2973
+ :repoKey: str - repo key involved
2974
+ :idCondition: int - condition id involved
2975
+ :return:
2976
+ """
2977
+ url = self._url_repo_condition(project_key, repo_key, id_condition)
2978
+ return self.get(url) or {}
2979
+
2980
+ def create_repo_condition(self, project_key, repo_key, condition):
2981
+ """
2982
+ Request type: POST
2983
+ Create a new condition for this repository slug inside project specified.
2984
+ For further information visit:
2985
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264908128
2986
+ :projectKey: str- project key involved
2987
+ :repoKey: str - repo key involved
2988
+ :data: condition: dictionary object
2989
+ :example condition: '{"sourceMatcher":
2990
+ {"id":"any",
2991
+ "type":{"id":"ANY_REF"}},
2992
+ "targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
2993
+ "reviewers":[{"id": 12}],"requiredApprovals":"0"
2994
+ }'
2995
+ :return:
2996
+ """
2997
+ url = self._url_repo_condition(project_key, repo_key)
2998
+ return self.post(url, data=condition) or {}
2999
+
3000
+ def update_repo_condition(self, project_key, repo_key, condition, id_condition):
3001
+ """
3002
+ Request type: PUT
3003
+ Update a specific condition for this repository slug inside project.
3004
+ For further information visit:
3005
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
3006
+ :projectKey: str- project key involved
3007
+ :repoKey: str - repo key involved
3008
+ :idCondition: int - condition id involved
3009
+ :data: condition: dictionary object
3010
+ :example condition: '{"sourceMatcher":
3011
+ {"id":"any",
3012
+ "type":{"id":"ANY_REF"}},
3013
+ "targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
3014
+ "reviewers":[{"id": 12}],"requiredApprovals":"0"
3015
+ }'
3016
+ :return:
3017
+ """
3018
+ url = self._url_repo_condition(project_key, repo_key, id_condition)
3019
+ return self.put(url, data=condition) or {}
3020
+
3021
+ def delete_repo_condition(self, project_key, repo_key, id_condition):
3022
+ """
3023
+ Delete a specific condition for this repository slug inside project.
3024
+ For further information visit:
3025
+ https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm8287339888
3026
+ Request type: DELETE
3027
+ :projectKey: str- project key involved
3028
+ :repoKey: str - repo key involved
3029
+ :idCondition: int - condition id involved
3030
+ :return:
3031
+ """
3032
+ url = self._url_repo_condition(project_key, repo_key, id_condition)
3033
+ return self.delete(url) or {}
3034
+
3035
+ def download_repo_archive(
3036
+ self,
3037
+ project_key,
3038
+ repository_slug,
3039
+ dest_fd,
3040
+ at=None,
3041
+ filename=None,
3042
+ format=None,
3043
+ path=None,
3044
+ prefix=None,
3045
+ chunk_size=128,
3046
+ ):
3047
+ """
3048
+ Downloads a repository archive.
3049
+ Note that the data is written to the specified file-like object,
3050
+ rather than simply being returned.
3051
+ For further information visit:
3052
+ https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-rest.html#idp199
3053
+ :param project_key:
3054
+ :param repository_slug:
3055
+ :param dest_fd: a file-like object to which the archive will be written
3056
+ :param at: string: Optional, the commit to download an archive of; if not supplied,
3057
+ an archive of the default branch is downloaded
3058
+ :param filename: string: Optional, a filename to include the "Content-Disposition" header
3059
+ :param format: string: Optional, the format to stream the archive in; must be one of: zip, tar, tar.gz or tgz.
3060
+ If not specified, then the archive will be in zip format.
3061
+ :param path: string: Optional, path to include in the streamed archive
3062
+ :param prefix: string: Optional, a prefix to apply to all entries in the streamed archive;
3063
+ if the supplied prefix does not end with a trailing /, one will be added automatically
3064
+ :param chunk_size: int: Optional, download chunk size. Defeault is 128
3065
+ """
3066
+ url = "{}/archive".format(self._url_repo(project_key, repository_slug))
3067
+ params = {}
3068
+ if at is not None:
3069
+ params["at"] = at
3070
+ if filename is not None:
3071
+ params["filename"] = filename
3072
+ if format is not None:
3073
+ params["format"] = format
3074
+ if path is not None:
3075
+ params["path"] = path
3076
+ if prefix is not None:
3077
+ params["prefix"] = prefix
3078
+ headers = {"Accept": "*/*"}
3079
+ response = self.get(url, params=params, headers=headers, advanced_mode=True)
3080
+ for chunk in response.iter_content(chunk_size=chunk_size):
3081
+ dest_fd.write(chunk)
3082
+
3083
+ @deprecated(
3084
+ version="2.0.2",
3085
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3086
+ )
3087
+ def get_repositories(self, workspace, role=None, query=None, sort=None):
3088
+ """
3089
+ Get all repositories in a workspace.
3090
+
3091
+ :param workspace:
3092
+ :param role: Filters the result based on the authenticated user's role on each repository.
3093
+ One of: member, contributor, admin, owner
3094
+ :param query: Query string to narrow down the response.
3095
+ :param sort: Field by which the results should be sorted.
3096
+ """
3097
+ return [
3098
+ r.data
3099
+ for r in Cloud(self.url, **self._new_session_args)
3100
+ .workspaces.get(workspace)
3101
+ .repositories.each(role=role, q=query, sort=sort)
3102
+ ]
3103
+
3104
+ @deprecated(
3105
+ version="2.0.2",
3106
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3107
+ )
3108
+ def get_pipelines(self, workspace, repository_slug, number=10, sort_by="-created_on"):
3109
+ """
3110
+ Get information about latest pipelines runs.
3111
+
3112
+ :param workspace:
3113
+ :param repository_slug:
3114
+ :param sort_by:
3115
+ :param number: number of pipelines to fetch
3116
+ :param :sort_by: optional key to sort available pipelines for
3117
+ :return: List of pipeline data
3118
+ """
3119
+ values = []
3120
+ for p in (
3121
+ Cloud(self.url, **self._new_session_args)
3122
+ .workspaces.get(workspace)
3123
+ .repositories.get(repository_slug)
3124
+ .pipelines.each(sort=sort_by)
3125
+ ):
3126
+ values.append(p.data)
3127
+ if len(values) == number:
3128
+ break
3129
+
3130
+ return values
3131
+
3132
+ @deprecated(
3133
+ version="2.0.2",
3134
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3135
+ )
3136
+ def trigger_pipeline(
3137
+ self,
3138
+ workspace,
3139
+ repository_slug,
3140
+ branch="master",
3141
+ revision=None,
3142
+ name=None,
3143
+ ):
3144
+ """
3145
+ Trigger a new pipeline. The following options are possible (1 and 2
3146
+ trigger the pipeline that the branch is associated with in the Pipelines
3147
+ configuration):
3148
+ 1. Latest revision of a branch (specify ``branch``)
3149
+ 2. Specific revision on a branch (additionally specify ``revision``)
3150
+ 3. Specific pipeline (additionally specify ``name``)
3151
+ :return: the initiated pipeline; or error information
3152
+ """
3153
+ return (
3154
+ Cloud(self.url, **self._new_session_args)
3155
+ .workspaces.get(workspace)
3156
+ .repositories.get(repository_slug)
3157
+ .pipelines.trigger(branch=branch, commit=revision, pattern=name)
3158
+ .data
3159
+ )
3160
+
3161
+ @deprecated(
3162
+ version="2.0.2",
3163
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3164
+ )
3165
+ def get_pipeline(self, workspace, repository_slug, uuid):
3166
+ """
3167
+ Get information about the pipeline specified by ``uuid``.
3168
+ :param workspace:
3169
+ :param repository_slug:
3170
+ :param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
3171
+ """
3172
+ return (
3173
+ Cloud(self.url, **self._new_session_args)
3174
+ .workspaces.get(workspace)
3175
+ .repositories.get(repository_slug)
3176
+ .pipelines.get(uuid)
3177
+ .data
3178
+ )
3179
+
3180
+ @deprecated(
3181
+ version="2.0.2",
3182
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3183
+ )
3184
+ def stop_pipeline(self, workspace, repository_slug, uuid):
3185
+ """
3186
+ Stop the pipeline specified by ``uuid``.
3187
+ :param workspace:
3188
+ :param repository_slug:
3189
+ :param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
3190
+
3191
+ See the documentation for the meaning of response status codes.
3192
+ """
3193
+ return (
3194
+ Cloud(self.url, **self._new_session_args)
3195
+ .workspaces.get(workspace)
3196
+ .repositories.get(repository_slug)
3197
+ .pipelines.get(uuid)
3198
+ .stop()
3199
+ )
3200
+
3201
+ @deprecated(
3202
+ version="2.0.2",
3203
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3204
+ )
3205
+ def get_pipeline_steps(self, workspace, repository_slug, uuid):
3206
+ """
3207
+ Get information about the steps of the pipeline specified by ``uuid``.
3208
+ :param workspace:
3209
+ :param repository_slug:
3210
+ :param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
3211
+ """
3212
+ values = []
3213
+ for s in (
3214
+ Cloud(self.url, **self._new_session_args)
3215
+ .workspaces.get(workspace)
3216
+ .repositories.get(repository_slug)
3217
+ .pipelines.get(uuid)
3218
+ .steps()
3219
+ ):
3220
+ values.append(s.data)
3221
+
3222
+ return values
3223
+
3224
+ @deprecated(
3225
+ version="2.0.2",
3226
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3227
+ )
3228
+ def get_pipeline_step(self, workspace, repository_slug, pipeline_uuid, step_uuid):
3229
+ """
3230
+ Get information about a step of a pipeline, specified by respective UUIDs.
3231
+ :param workspace:
3232
+ :param repository_slug:
3233
+ :param pipeline_uuid: Pipeline identifier (with surrounding {}; NOT the build number)
3234
+ :param step_uuid: Step identifier (with surrounding {})
3235
+ """
3236
+ return (
3237
+ Cloud(self.url, **self._new_session_args)
3238
+ .workspaces.get(workspace)
3239
+ .repositories.get(repository_slug)
3240
+ .pipelines.get(pipeline_uuid)
3241
+ .step(step_uuid)
3242
+ .data
3243
+ )
3244
+
3245
+ @deprecated(
3246
+ version="2.0.2",
3247
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3248
+ )
3249
+ def get_pipeline_step_log(self, workspace, repository_slug, pipeline_uuid, step_uuid):
3250
+ """
3251
+ Get log of a step of a pipeline, specified by respective UUIDs.
3252
+ :param workspace:
3253
+ :param repository_slug:
3254
+ :param pipeline_uuid: Pipeline identifier (with surrounding {}; NOT the build number)
3255
+ :param step_uuid: Step identifier (with surrounding {})
3256
+ :return: byte string log
3257
+ """
3258
+ return (
3259
+ Cloud(self.url, **self._new_session_args)
3260
+ .workspaces.get(workspace)
3261
+ .repositories.get(repository_slug)
3262
+ .pipelines.get(pipeline_uuid)
3263
+ .step(step_uuid)
3264
+ .log()
3265
+ )
3266
+
3267
+ @deprecated(
3268
+ version="2.0.2",
3269
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3270
+ )
3271
+ def create_issue(
3272
+ self,
3273
+ workspace,
3274
+ repository_slug,
3275
+ title,
3276
+ description="",
3277
+ kind="bug",
3278
+ priority="major",
3279
+ ):
3280
+ """
3281
+ Create a new issue in the issue tracker of the given repository.
3282
+ :param workspace:
3283
+ :param repository_slug:
3284
+ :param title:
3285
+ :param description:
3286
+ :param kind: one of: bug, enhancement, proposal, task
3287
+ :param priority: one of: trivial, minor, major, critical, blocker
3288
+ """
3289
+ return (
3290
+ Cloud(self.url, **self._new_session_args)
3291
+ .workspaces.get(workspace)
3292
+ .repositories.get(repository_slug)
3293
+ .issues.create(
3294
+ title=title,
3295
+ description=description,
3296
+ kind=kind,
3297
+ priority=priority,
3298
+ )
3299
+ .data
3300
+ )
3301
+
3302
+ @deprecated(
3303
+ version="2.0.2",
3304
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3305
+ )
3306
+ def get_issues(self, workspace, repository_slug, sort_by=None, query=None):
3307
+ """
3308
+ Get information about the issues tracked in the given repository. By
3309
+ default, the issues are sorted by ID in descending order.
3310
+ :param workspace:
3311
+ :param repository_slug:
3312
+ :param sort_by: optional key to sort available issues for
3313
+ :param query: optional query to filter available issues for. See
3314
+ https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering
3315
+ for an overview
3316
+
3317
+ :return: List of issues (direct, i.e. without the 'values' key)
3318
+ """
3319
+ values = []
3320
+ for p in (
3321
+ Cloud(self.url, **self._new_session_args)
3322
+ .workspaces.get(workspace)
3323
+ .repositories.get(repository_slug)
3324
+ .issues.each(q=query, sort=sort_by)
3325
+ ):
3326
+ values.append(p.data)
3327
+
3328
+ return values
3329
+
3330
+ @deprecated(
3331
+ version="2.0.2",
3332
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3333
+ )
3334
+ def get_issue(self, workspace, repository_slug, id):
3335
+ """
3336
+ Get the issue specified by ``id``.
3337
+ """
3338
+ return (
3339
+ Cloud(self.url, **self._new_session_args)
3340
+ .workspaces.get(workspace)
3341
+ .repositories.get(repository_slug)
3342
+ .issues.get(id)
3343
+ .data
3344
+ )
3345
+
3346
+ @deprecated(
3347
+ version="2.0.2",
3348
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3349
+ )
3350
+ def update_issue(self, workspace, repository_slug, id, **fields):
3351
+ """
3352
+ Update the ``fields`` of the issue specified by ``id``.
3353
+ Consult the official API documentation for valid fields.
3354
+ """
3355
+ return (
3356
+ Cloud(self.url, **self._new_session_args)
3357
+ .workspaces.get(workspace)
3358
+ .repositories.get(repository_slug)
3359
+ .issues.get(id)
3360
+ .update(**fields)
3361
+ .data
3362
+ )
3363
+
3364
+ @deprecated(
3365
+ version="2.0.2",
3366
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3367
+ )
3368
+ def delete_issue(self, workspace, repository_slug, id):
3369
+ """
3370
+ Delete the issue specified by ``id``.
3371
+ """
3372
+ return (
3373
+ Cloud(self.url, **self._new_session_args)
3374
+ .workspaces.get(workspace)
3375
+ .repositories.get(repository_slug)
3376
+ .issues.get(id)
3377
+ .delete()
3378
+ )
3379
+
3380
+ @deprecated(
3381
+ version="2.0.2",
3382
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3383
+ )
3384
+ def add_branch_restriction(
3385
+ self,
3386
+ workspace,
3387
+ repository_slug,
3388
+ kind,
3389
+ branch_match_kind="glob",
3390
+ branch_pattern="*",
3391
+ branch_type=None,
3392
+ users=None,
3393
+ groups=None,
3394
+ value=None,
3395
+ ):
3396
+ """
3397
+ Add a new branch restriction.
3398
+
3399
+ :param workspace:
3400
+ :param repository_slug:
3401
+ :param value:
3402
+ :param kind: One of require_tasks_to_be_completed, force, restrict_merges,
3403
+ enforce_merge_checks, require_approvals_to_merge, delete,
3404
+ require_all_dependencies_merged, push, require_passing_builds_to_merge,
3405
+ reset_pullrequest_approvals_on_change, require_default_reviewer_approvals_to_merge
3406
+ :param branch_match_kind: branching_model or glob, if branching_model use
3407
+ param branch_type otherwise branch_pattern.
3408
+ :param branch_pattern: A glob specifying the branch this restriction should
3409
+ apply to (supports * as wildcard).
3410
+ :param branch_type: The branch type specifies the branches this restriction
3411
+ should apply to. One of: feature, bugfix, release, hotfix, development, production.
3412
+ :param users: List of user objects that are excluded from the restriction.
3413
+ Minimal: {"username": "<username>"}
3414
+ :param groups: List of group objects that are excluded from the restriction.
3415
+ Minimal: {"owner": {"username": "<teamname>"}, "slug": "<groupslug>"}
3416
+ """
3417
+ return (
3418
+ Cloud(self.url, **self._new_session_args)
3419
+ .workspaces.get(workspace)
3420
+ .repositories.get(repository_slug)
3421
+ .branch_restrictions.create(
3422
+ kind,
3423
+ branch_match_kind=branch_match_kind,
3424
+ branch_pattern=branch_pattern,
3425
+ branch_type=branch_type,
3426
+ users=users,
3427
+ groups=groups,
3428
+ value=value,
3429
+ )
3430
+ .data
3431
+ )
3432
+
3433
+ @deprecated(
3434
+ version="2.0.2",
3435
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3436
+ )
3437
+ def get_branch_restrictions(self, workspace, repository_slug, kind=None, pattern=None, number=10):
3438
+ """
3439
+ Get all branch permissions.
3440
+ """
3441
+ values = []
3442
+ for p in (
3443
+ Cloud(self.url, **self._new_session_args)
3444
+ .workspaces.get(workspace)
3445
+ .repositories.get(repository_slug)
3446
+ .branch_restrictions.each(kind=kind, pattern=pattern)
3447
+ ):
3448
+ values.append(p.data)
3449
+ if len(values) == number:
3450
+ break
3451
+
3452
+ return values
3453
+
3454
+ @deprecated(
3455
+ version="2.0.2",
3456
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3457
+ )
3458
+ def update_branch_restriction(self, workspace, repository_slug, id, **fields):
3459
+ """
3460
+ Update an existing branch restriction identified by ``id``.
3461
+ Consult the official API documentation for valid fields.
3462
+ """
3463
+ return (
3464
+ Cloud(self.url, **self._new_session_args)
3465
+ .workspaces.get(workspace)
3466
+ .repositories.get(repository_slug)
3467
+ .branch_restrictions.get(id)
3468
+ .update(**fields)
3469
+ .data
3470
+ )
3471
+
3472
+ @deprecated(
3473
+ version="2.0.2",
3474
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3475
+ )
3476
+ def delete_branch_restriction(self, workspace, repository_slug, id):
3477
+ """
3478
+ Delete an existing branch restriction identified by ``id``.
3479
+ """
3480
+ return (
3481
+ Cloud(self.url, **self._new_session_args)
3482
+ .workspaces.get(workspace)
3483
+ .repositories.get(repository_slug)
3484
+ .branch_restrictions.get(id)
3485
+ .delete()
3486
+ )
3487
+
3488
+ @deprecated(
3489
+ version="2.0.2",
3490
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3491
+ )
3492
+ def add_default_reviewer(self, workspace, repository_slug, user):
3493
+ """
3494
+ Add user as default reviewer to the repository.
3495
+ Can safely be called multiple times with the same user, only adds once.
3496
+
3497
+ :param workspace:
3498
+ :param repository_slug:
3499
+ :param user: The username or account UUID to add as default_reviewer.
3500
+ """
3501
+ return (
3502
+ Cloud(self.url, **self._new_session_args)
3503
+ .workspaces.get(workspace)
3504
+ .repositories.get(repository_slug)
3505
+ .default_reviewers.add(user)
3506
+ .data
3507
+ )
3508
+
3509
+ @deprecated(
3510
+ version="2.0.2",
3511
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3512
+ )
3513
+ def get_default_reviewers(self, workspace, repository_slug, number=10):
3514
+ """
3515
+ Get all default reviewers for the repository.
3516
+ """
3517
+ values = []
3518
+ for p in (
3519
+ Cloud(self.url, **self._new_session_args)
3520
+ .workspaces.get(workspace)
3521
+ .repositories.get(repository_slug)
3522
+ .default_reviewers.each()
3523
+ ):
3524
+ values.append(p.data)
3525
+ if len(values) == number:
3526
+ break
3527
+
3528
+ return values
3529
+
3530
+ @deprecated(
3531
+ version="2.0.2",
3532
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3533
+ )
3534
+ def is_default_reviewer(self, workspace, repository_slug, user):
3535
+ """
3536
+ Check if the user is a default reviewer of the repository.
3537
+
3538
+ :param workspace:
3539
+ :param repository_slug:
3540
+ :param user: The username or account UUID to check.
3541
+ :return: True if present, False if not.
3542
+ """
3543
+ if (
3544
+ Cloud(self.url, **self._new_session_args)
3545
+ .workspaces.get(workspace)
3546
+ .repositories.get(repository_slug)
3547
+ .default_reviewers.get(user)
3548
+ is None
3549
+ ):
3550
+ return False
3551
+
3552
+ return True
3553
+
3554
+ @deprecated(
3555
+ version="2.0.2",
3556
+ reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
3557
+ )
3558
+ def delete_default_reviewer(self, workspace, repository_slug, user):
3559
+ """
3560
+ Remove user as default reviewer from the repository.
3561
+
3562
+ :param repository_slug:
3563
+ :param workspace:
3564
+ :param user: The username or account UUID to delete as default reviewer.
3565
+ """
3566
+ return (
3567
+ Cloud(self.url, **self._new_session_args)
3568
+ .workspaces.get(workspace)
3569
+ .repositories.get(repository_slug)
3570
+ .default_reviewers.get(user)
3571
+ .delete()
3572
+ )