|
8 | 8 |
|
9 | 9 | class Bitbucket(AtlassianRestAPI): |
10 | 10 |
|
| 11 | + bulk_headers = {"Content-Type": "application/vnd.atl.bitbucket.bulk+json"} |
| 12 | + |
11 | 13 | def project_list(self, limit=None): |
12 | 14 | """ |
13 | 15 | Provide the project list |
@@ -783,6 +785,89 @@ def create_pull_request(self, project_key, repository, data): |
783 | 785 | repository=repository) |
784 | 786 | return self.post(url, data=data) |
785 | 787 |
|
| 788 | + def decline_pull_request(self, project_key, repository, pr_id, pr_version): |
| 789 | + """ |
| 790 | + Decline a pull request. |
| 791 | + The authenticated user must have REPO_READ permission for the repository |
| 792 | + that this pull request targets to call this resource. |
| 793 | +
|
| 794 | + :param project_key: PROJECT |
| 795 | + :param repository: my_shiny_repo |
| 796 | + :param pr_id: 2341 |
| 797 | + :param pr_version: 12 |
| 798 | + :return: |
| 799 | + """ |
| 800 | + url = 'rest/api/1.0/projects/{project_key}/repos/{repository}/pull-requests/{pr_id}/decline'.format( |
| 801 | + project_key=project_key, |
| 802 | + repository=repository, |
| 803 | + pr_id=pr_id |
| 804 | + ) |
| 805 | + params = {'version': pr_version} |
| 806 | + |
| 807 | + return self.post(url, params=params) |
| 808 | + |
| 809 | + def is_pull_request_can_be_merged(self, project_key, repository, pr_id): |
| 810 | + """ |
| 811 | + Test whether a pull request can be merged. |
| 812 | + A pull request may not be merged if: |
| 813 | + - there are conflicts that need to be manually resolved before merging; and/or |
| 814 | + - one or more merge checks have vetoed the merge. |
| 815 | + The authenticated user must have REPO_READ permission for the repository |
| 816 | + that this pull request targets to call this resource. |
| 817 | +
|
| 818 | + :param project_key: PROJECT |
| 819 | + :param repository: my_shiny_repo |
| 820 | + :param pr_id: 2341 |
| 821 | + :return: |
| 822 | + """ |
| 823 | + url = 'rest/api/1.0/projects/{project_key}/repos/{repository}/pull-requests/{pr_id}/merge'.format( |
| 824 | + project_key=project_key, |
| 825 | + repository=repository, |
| 826 | + pr_id=pr_id |
| 827 | + ) |
| 828 | + return self.get(url) |
| 829 | + |
| 830 | + def merge_pull_request(self, project_key, repository, pr_id, pr_version): |
| 831 | + """ |
| 832 | + Merge pull request |
| 833 | + The authenticated user must have REPO_READ permission for the repository |
| 834 | + that this pull request targets to call this resource. |
| 835 | +
|
| 836 | + :param project_key: PROJECT |
| 837 | + :param repository: my_shiny_repo |
| 838 | + :param pr_id: 2341 |
| 839 | + :return: |
| 840 | + """ |
| 841 | + url = 'rest/api/1.0/projects/{project_key}/repos/{repository}/pull-requests/{pr_id}/merge'.format( |
| 842 | + project_key=project_key, |
| 843 | + repository=repository, |
| 844 | + pr_id=pr_id |
| 845 | + ) |
| 846 | + params = {'version': pr_version} |
| 847 | + |
| 848 | + return self.post(url, params=params) |
| 849 | + |
| 850 | + def reopen_pull_request(self, project_key, repository, pr_id, pr_version): |
| 851 | + """ |
| 852 | + Re-open a declined pull request. |
| 853 | + The authenticated user must have REPO_READ permission for the repository |
| 854 | + that this pull request targets to call this resource. |
| 855 | +
|
| 856 | + :param project_key: PROJECT |
| 857 | + :param repository: my_shiny_repo |
| 858 | + :param pr_id: 2341 |
| 859 | + :param pr_version: 12 |
| 860 | + :return: |
| 861 | + """ |
| 862 | + url = 'rest/api/1.0/projects/{project_key}/repos/{repository}/pull-requests/{pr_id}/reopen'.format( |
| 863 | + project_key=project_key, |
| 864 | + repository=repository, |
| 865 | + pr_id=pr_id |
| 866 | + ) |
| 867 | + params = {'version': pr_version} |
| 868 | + |
| 869 | + return self.post(url, params=params) |
| 870 | + |
786 | 871 | def check_inbox_pull_requests_count(self): |
787 | 872 | return self.get('rest/api/1.0/inbox/pull-requests/count') |
788 | 873 |
|
@@ -1135,6 +1220,109 @@ def get_branches_permissions(self, project, repository=None, start=0, limit=25): |
1135 | 1220 | params['start'] = start |
1136 | 1221 | return self.get(url, params=params) |
1137 | 1222 |
|
| 1223 | + def set_branches_permissions( |
| 1224 | + self, project_key, |
| 1225 | + multiple_permissions=False, |
| 1226 | + matcher_type=None, |
| 1227 | + matcher_value=None, |
| 1228 | + permission_type=None, |
| 1229 | + repository=None, |
| 1230 | + except_users=[], |
| 1231 | + except_groups=[], |
| 1232 | + except_access_keys=[], |
| 1233 | + start=0, |
| 1234 | + limit=25 |
| 1235 | + ): |
| 1236 | + """ |
| 1237 | + Create a restriction for the supplied branch or set of branches to be applied to the given repository. |
| 1238 | + Allows creating multiple restrictions at once. |
| 1239 | + To use multiple restrictions you should format payload manually - see the bitbucket-branch-restrictions.py example. |
| 1240 | + Reference: https://docs.atlassian.com/bitbucket-server/rest/6.8.0/bitbucket-ref-restriction-rest.html |
| 1241 | +
|
| 1242 | + :param project_key: |
| 1243 | + :param repository: |
| 1244 | + :return: |
| 1245 | + """ |
| 1246 | + headers = self.default_headers |
| 1247 | + if repository: |
| 1248 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/repos/{repository}/restrictions".format( |
| 1249 | + project_key=project_key, |
| 1250 | + repository=repository |
| 1251 | + ) |
| 1252 | + else: |
| 1253 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/restrictions".format( |
| 1254 | + project_key=project_key |
| 1255 | + ) |
| 1256 | + if multiple_permissions: |
| 1257 | + headers = self.bulk_headers |
| 1258 | + restriction = multiple_permissions |
| 1259 | + else: |
| 1260 | + restriction = { |
| 1261 | + "type": permission_type, |
| 1262 | + "matcher": { |
| 1263 | + "id": matcher_value, |
| 1264 | + "displayId": matcher_value, |
| 1265 | + "type": { |
| 1266 | + "id": matcher_type.upper(), |
| 1267 | + "name": matcher_type.capitalize() |
| 1268 | + }, |
| 1269 | + "active": True, |
| 1270 | + }, |
| 1271 | + "users": except_users, |
| 1272 | + "groups": except_groups, |
| 1273 | + "accessKeys": except_access_keys, |
| 1274 | + } |
| 1275 | + params = {"start": start, "limit": limit} |
| 1276 | + return self.post(url, data=restriction, params=params, headers=headers) |
| 1277 | + |
| 1278 | + def delete_branch_permission(self, project_key, permission_id, repository=None): |
| 1279 | + """ |
| 1280 | + Deletes a restriction as specified by a restriction id. |
| 1281 | + The authenticated user must have REPO_ADMIN permission or higher to call this resource. |
| 1282 | +
|
| 1283 | + :param project_key: |
| 1284 | + :param repository: |
| 1285 | + :param permission_id: |
| 1286 | + :return: |
| 1287 | + """ |
| 1288 | + |
| 1289 | + if repository: |
| 1290 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/repos/{repository}/restrictions/{id}".format( |
| 1291 | + project_key=project_key, |
| 1292 | + repository=repository, |
| 1293 | + id=permission_id |
| 1294 | + ) |
| 1295 | + else: |
| 1296 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/restrictions/{id}".format( |
| 1297 | + project_key=project_key, |
| 1298 | + id=permission_id |
| 1299 | + ) |
| 1300 | + return self.delete(url) |
| 1301 | + |
| 1302 | + def get_branch_permission(self, project_key, permission_id, repository=None): |
| 1303 | + """ |
| 1304 | + Returns a restriction as specified by a restriction id. |
| 1305 | + The authenticated user must have REPO_ADMIN permission or higher to call this resource. |
| 1306 | +
|
| 1307 | + :param project_key: |
| 1308 | + :param repository: |
| 1309 | + :param permission_id: |
| 1310 | + :return: |
| 1311 | + """ |
| 1312 | + |
| 1313 | + if repository: |
| 1314 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/repos/{repository}/restrictions/{id}".format( |
| 1315 | + project_key=project_key, |
| 1316 | + repository=repository, |
| 1317 | + id=permission_id |
| 1318 | + ) |
| 1319 | + else: |
| 1320 | + url = "/rest/branch-permissions/2.0/projects/{project_key}/restrictions/{id}".format( |
| 1321 | + project_key=project_key, |
| 1322 | + id=permission_id |
| 1323 | + ) |
| 1324 | + return self.get(url) |
| 1325 | + |
1138 | 1326 | def all_branches_permissions(self, project, repository=None): |
1139 | 1327 | """ |
1140 | 1328 | Get branches permissions from a given repo |
|
0 commit comments