From 841d46b8528a5cd23d17f77712ecfb533e2c8340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20G=C3=B3mez=20Zarzosa?= Date: Thu, 25 Jul 2024 00:44:30 +0200 Subject: [PATCH 1/3] Bitbucket: update merge_pull_request method to add merge_message, close_source_branch and merge_strategy parameters --- atlassian/bitbucket/__init__.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/atlassian/bitbucket/__init__.py b/atlassian/bitbucket/__init__.py index 722257a7c..5dc1d4440 100644 --- a/atlassian/bitbucket/__init__.py +++ b/atlassian/bitbucket/__init__.py @@ -1,5 +1,7 @@ # coding=utf-8 import logging +from enum import Enum +from typing import Optional, Union from deprecated import deprecated from requests import HTTPError @@ -10,6 +12,16 @@ log = logging.getLogger(__name__) +class MergeStrategy(Enum): + """ + Merge strategies used by the merge_pull_request method. + """ + + MERGE_COMMIT = "merge_commit" + SQUASH = "squash" + FAST_FORWARD = "fast_forward" + + class Bitbucket(BitbucketBase): def __init__(self, url, *args, **kwargs): if "cloud" not in kwargs and ("bitbucket.org" in url): @@ -2194,7 +2206,16 @@ def is_pull_request_can_be_merged(self, project_key, repository_slug, pr_id): url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id)) return self.get(url) - def merge_pull_request(self, project_key, repository_slug, pr_id, pr_version): + def merge_pull_request( + self, + project_key: str, + repository_slug: str, + pr_id: int, + pr_version: Optional[int], + merge_message: str, + close_source_branch: bool = False, + merge_strategy: Union[str, MergeStrategy] = MergeStrategy.MERGE_COMMIT, + ): """ Merge pull request The authenticated user must have REPO_READ permission for the repository @@ -2204,10 +2225,17 @@ def merge_pull_request(self, project_key, repository_slug, pr_id, pr_version): :param repository_slug: my_shiny_repo :param pr_id: 2341 :param pr_version: + :param merge_message: "feat: add new file handler" + :param close_source_branch: True + :param merge_strategy: "squash" :return: """ url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id)) - params = {} + params = { + "message": merge_message, + "close_source_branch": close_source_branch, + "merge_strategy": MergeStrategy(merge_strategy).value, + } if not self.cloud: params["version"] = pr_version return self.post(url, params=params) From 7d2df4b6fc00ca47e35fc92a21287cc04c04a2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20G=C3=B3mez=20Zarzosa?= Date: Tue, 30 Jul 2024 12:02:50 +0200 Subject: [PATCH 2/3] fix(bitbucket): merge_pull_request params should be data --- atlassian/bitbucket/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/atlassian/bitbucket/__init__.py b/atlassian/bitbucket/__init__.py index 5dc1d4440..fb71c6845 100644 --- a/atlassian/bitbucket/__init__.py +++ b/atlassian/bitbucket/__init__.py @@ -2211,10 +2211,10 @@ def merge_pull_request( project_key: str, repository_slug: str, pr_id: int, - pr_version: Optional[int], merge_message: str, close_source_branch: bool = False, merge_strategy: Union[str, MergeStrategy] = MergeStrategy.MERGE_COMMIT, + pr_version: Optional[int] = None, ): """ Merge pull request @@ -2224,21 +2224,22 @@ def merge_pull_request( :param project_key: PROJECT :param repository_slug: my_shiny_repo :param pr_id: 2341 - :param pr_version: :param merge_message: "feat: add new file handler" + :param pr_version: :param close_source_branch: True :param merge_strategy: "squash" :return: """ url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id)) params = { + "type": "pullrequest", "message": merge_message, "close_source_branch": close_source_branch, "merge_strategy": MergeStrategy(merge_strategy).value, } if not self.cloud: params["version"] = pr_version - return self.post(url, params=params) + return self.post(url, data=params) def reopen_pull_request(self, project_key, repository_slug, pr_id, pr_version): """ From 70c0da4b0efa4a84b33d0ccd6869d34dfec180a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20G=C3=B3mez=20Zarzosa?= Date: Tue, 30 Jul 2024 12:10:13 +0200 Subject: [PATCH 3/3] fix(bitbucket): support Bitbucket server request --- atlassian/bitbucket/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/atlassian/bitbucket/__init__.py b/atlassian/bitbucket/__init__.py index fb71c6845..e2ae5fd61 100644 --- a/atlassian/bitbucket/__init__.py +++ b/atlassian/bitbucket/__init__.py @@ -2231,7 +2231,8 @@ def merge_pull_request( :return: """ url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id)) - params = { + params = {} + data = { "type": "pullrequest", "message": merge_message, "close_source_branch": close_source_branch, @@ -2239,7 +2240,7 @@ def merge_pull_request( } if not self.cloud: params["version"] = pr_version - return self.post(url, data=params) + return self.post(url, data=data, params=params) def reopen_pull_request(self, project_key, repository_slug, pr_id, pr_version): """