From 460d69e75c9255459fc8a395df182d63e340c0e5 Mon Sep 17 00:00:00 2001 From: Adris Date: Fri, 7 Feb 2020 12:04:58 +0100 Subject: [PATCH 1/3] Added default reviewers methods Create standar methods get, create, update and delete conditions (reviewers) for projects and repositories --- atlassian/bitbucket.py | 165 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/atlassian/bitbucket.py b/atlassian/bitbucket.py index 2d9718069..2b699de4a 100644 --- a/atlassian/bitbucket.py +++ b/atlassian/bitbucket.py @@ -1940,3 +1940,168 @@ def _get_paged(self, url, params): response = self.get(url, params=params) values += response.get('values') return values + + def get_project_conditions(self, project_key): + """ + Request type: GET + Return a page of defaults conditions with reviewers list that have been configured for this project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264904368 + :projectKey: str + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/conditions'.format( + projectKey=key) + return (self.get(url)) + + def get_project_condition(self, project_key, id_condition): + """ + Request type: GET + Return a specific condition with reviewers list that has been configured for this project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264901504 + :projectKey: str - project key involved + :idCondition: str - condition id involved + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/conditions/{idCondition}'.format( + projectKey=key, + idCondition=id_condition) + return (self.get(url) or {}) + + def create_project_condition(self, project_key, condition): + """ + Request type: POST + Create a new condition for this project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264893584 + :projectKey: str- project key involved + :data: condition: dictionary object + :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition'.format( + projectKey=project_key) + return (self.post(url, data=condition) or {}) + + def update_project_condition(self, project_key, condition, id_condition): + """ + Request type: PUT + Update a new condition for this project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 + :projectKey: str- project key involved + :idCondition: str - condition id involved + :data: condition: dictionary object + :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition/{idCondition}'.format( + projectKey=project_key, + idCondition = id_condition) + return (self.put(url, data=condition) or {}) + + def delete_project_condition(self, project_key, condition, id_condition): + """ + Request type: DELETE + Delete a specific condition for this repository slug inside project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264896304 + :projectKey: str- project key involved + :idCondition: str - condition id involved + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition/{idCondition}'.format( + projectKey=project_key, + idCondition = id_condition) + return (self.delete(url) or {}) + + def get_repo_conditions(self, project_key, repo_key): + """ + Request type: GET + Return a page of defaults conditions with reviewers list that have been configured for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions'.format( + projectKey=project_key, + repoKey=repo_key) + return (self.get(url) or {}) + + def get_repo_condition(self, project_key, repo_key, id_condition): + """ + Request type: GET + Return a specific condition with reviewers list that have been configured for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :idCondition: str - condition id involved + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions/{idCondition}'.format( + projectKey=project_key, + repoKey=repo_key, + idCondition = id_condition) + return (self.get(url) or {}) + + def create_repo_condition(self, project_key, repo_key, condition): + """ + Request type: POST + Create a new condition for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264908128 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :data: condition: dictionary object + :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition'.format( + projectKey=project_key, + repoKey=repo_key) + return (self.post(url, data=condition) or {}) + + def update_repo_condition(self, project_key, repo_key, condition, id_condition): + """ + Request type: PUT + Update a specific condition for this repository slug inside project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :idCondition: str - condition id involved + :data: condition: dictionary object + :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition/{idCondition}'.format( + projectKey=project_key, + repoKey=repo_key, + idCondition = id_condition) + return (self.put(url, data=condition) or {}) + + def delete_repo_condition(self, project_key, repo_key, condition, id_condition): + """ + Request type: DELETE + Delete a specific condition for this repository slug inside project. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm8287339888 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :idCondition: str - condition id involved + :return: + """ + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition/{idCondition}'.format( + projectKey=project_key, + repoKey=repo_key, + idCondition = id_condition) + return (self.delete(url) or {}) + + def get_repo_details(self, project_key, repo_key): + """ + Return repository details for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8287366304 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :return: + """ + url = 'rest/api/1.0/projects/{key}/repos/{repoKey}'.format( + projectKey=project_key, + repoKey = repo_key) + return (self.get(url) or {}) + + From 08d29a0e9ae788fb2446df984f594ec8fdaf6f9f Mon Sep 17 00:00:00 2001 From: Adris Date: Fri, 7 Feb 2020 12:22:14 +0100 Subject: [PATCH 2/3] Added default reviewers methods 460d69e Create standar methods get, create, update and delete conditions (reviewers) for projects and repositories --- atlassian/bitbucket.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/atlassian/bitbucket.py b/atlassian/bitbucket.py index 2b699de4a..bf2bbcbb3 100644 --- a/atlassian/bitbucket.py +++ b/atlassian/bitbucket.py @@ -1940,8 +1940,8 @@ def _get_paged(self, url, params): response = self.get(url, params=params) values += response.get('values') return values - - def get_project_conditions(self, project_key): + + def get_project_conditions(self, project_key): """ Request type: GET Return a page of defaults conditions with reviewers list that have been configured for this project. @@ -1950,8 +1950,8 @@ def get_project_conditions(self, project_key): :return: """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/conditions'.format( - projectKey=key) - return (self.get(url)) + projectKey=project_key) + return (self.get(url) or {}) def get_project_condition(self, project_key, id_condition): """ @@ -1994,7 +1994,7 @@ def update_project_condition(self, project_key, condition, id_condition): """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition/{idCondition}'.format( projectKey=project_key, - idCondition = id_condition) + idCondition=id_condition) return (self.put(url, data=condition) or {}) def delete_project_condition(self, project_key, condition, id_condition): @@ -2008,7 +2008,7 @@ def delete_project_condition(self, project_key, condition, id_condition): """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition/{idCondition}'.format( projectKey=project_key, - idCondition = id_condition) + idCondition=id_condition) return (self.delete(url) or {}) def get_repo_conditions(self, project_key, repo_key): @@ -2038,7 +2038,7 @@ def get_repo_condition(self, project_key, repo_key, id_condition): url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions/{idCondition}'.format( projectKey=project_key, repoKey=repo_key, - idCondition = id_condition) + idCondition=id_condition) return (self.get(url) or {}) def create_repo_condition(self, project_key, repo_key, condition): @@ -2072,7 +2072,7 @@ def update_repo_condition(self, project_key, repo_key, condition, id_condition): url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition/{idCondition}'.format( projectKey=project_key, repoKey=repo_key, - idCondition = id_condition) + idCondition=id_condition) return (self.put(url, data=condition) or {}) def delete_repo_condition(self, project_key, repo_key, condition, id_condition): @@ -2088,7 +2088,7 @@ def delete_repo_condition(self, project_key, repo_key, condition, id_condition): url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition/{idCondition}'.format( projectKey=project_key, repoKey=repo_key, - idCondition = id_condition) + idCondition=id_condition) return (self.delete(url) or {}) def get_repo_details(self, project_key, repo_key): @@ -2099,9 +2099,7 @@ def get_repo_details(self, project_key, repo_key): :repoKey: str - repo key involved :return: """ - url = 'rest/api/1.0/projects/{key}/repos/{repoKey}'.format( + url = 'rest/api/1.0/projects/{projectKey}/repos/{repoKey}'.format( projectKey=project_key, - repoKey = repo_key) - return (self.get(url) or {}) - - + repoKey=repo_key) + return (self.get(url) or {}) \ No newline at end of file From 3c918df0da4727db6d1a10fb1c619934772ca694 Mon Sep 17 00:00:00 2001 From: Adris Date: Fri, 7 Feb 2020 13:31:30 +0100 Subject: [PATCH 3/3] Complete docs contribution Complete docs contribution and add two new methods for filter get conditios by type repository or project contidition --- atlassian/bitbucket.py | 78 +++++++++++++++++++++++++++++------------- docs/bitbucket.rst | 45 ++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 24 deletions(-) diff --git a/atlassian/bitbucket.py b/atlassian/bitbucket.py index bf2bbcbb3..dc344d353 100644 --- a/atlassian/bitbucket.py +++ b/atlassian/bitbucket.py @@ -521,7 +521,6 @@ def get_repo(self, project_key, repository_slug): :param repository_slug: url-compatible repository identifier :return: Dictionary of request response """ - if not self.cloud: url = 'rest/api/1.0/projects/{project}/repos/{repository}' \ .format(project=project_key, repository=repository_slug) @@ -1959,7 +1958,7 @@ def get_project_condition(self, project_key, id_condition): Return a specific condition with reviewers list that has been configured for this project. For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264901504 :projectKey: str - project key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :return: """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/conditions/{idCondition}'.format( @@ -1987,7 +1986,7 @@ def update_project_condition(self, project_key, condition, id_condition): Update a new condition for this project. For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 :projectKey: str- project key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :data: condition: dictionary object :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' :return: @@ -1997,13 +1996,13 @@ def update_project_condition(self, project_key, condition, id_condition): idCondition=id_condition) return (self.put(url, data=condition) or {}) - def delete_project_condition(self, project_key, condition, id_condition): + def delete_project_condition(self, project_key, id_condition): """ Request type: DELETE Delete a specific condition for this repository slug inside project. For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264896304 :projectKey: str- project key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :return: """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/condition/{idCondition}'.format( @@ -2014,7 +2013,7 @@ def delete_project_condition(self, project_key, condition, id_condition): def get_repo_conditions(self, project_key, repo_key): """ Request type: GET - Return a page of defaults conditions with reviewers list that have been configured for this repository slug inside project specified. + Return a page of defaults conditions with reviewers list (type REPOSITORY or PROJECT) that have been configured for this repository slug inside project specified. For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992 :projectKey: str- project key involved :repoKey: str - repo key involved @@ -2025,6 +2024,50 @@ def get_repo_conditions(self, project_key, repo_key): repoKey=repo_key) return (self.get(url) or {}) + def get_repo_project_conditions(self, project_key, repo_key): + """ + Request type: GET + Return a page of repository conditions (only type PROJECT) with reviewers list associated that have been configured for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :return: + """ + TYPE_REPO = 'REPOSITORY' + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions'.format( + projectKey=project_key, + repoKey=repo_key) + + response = (self.get(url) or {}) + count = 0 + for condition in response: + if condition['scope']['type'] == TYPE_REPO: + del response[count] + count+=1 + return response + + def get_repo_repo_conditions(self, project_key, repo_key): + """ + Request type: GET + Return a page of repository conditions (only type REPOSITORY) with reviewers list associated that have been configured for this repository slug inside project specified. + For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992 + :projectKey: str- project key involved + :repoKey: str - repo key involved + :return: + """ + TYPE_PROYECT = 'PROJECT' + url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions'.format( + projectKey=project_key, + repoKey=repo_key) + + response = (self.get(url) or {}) + count = 0 + for condition in response: + if condition['scope']['type'] == TYPE_PROYECT: + del response[count] + count+=1 + return response + def get_repo_condition(self, project_key, repo_key, id_condition): """ Request type: GET @@ -2032,7 +2075,7 @@ def get_repo_condition(self, project_key, repo_key, id_condition): For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 :projectKey: str- project key involved :repoKey: str - repo key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :return: """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/conditions/{idCondition}'.format( @@ -2064,7 +2107,7 @@ def update_repo_condition(self, project_key, repo_key, condition, id_condition): For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632 :projectKey: str- project key involved :repoKey: str - repo key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :data: condition: dictionary object :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' :return: @@ -2075,31 +2118,18 @@ def update_repo_condition(self, project_key, repo_key, condition, id_condition): idCondition=id_condition) return (self.put(url, data=condition) or {}) - def delete_repo_condition(self, project_key, repo_key, condition, id_condition): + def delete_repo_condition(self, project_key, repo_key, id_condition): """ Request type: DELETE Delete a specific condition for this repository slug inside project. For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm8287339888 :projectKey: str- project key involved :repoKey: str - repo key involved - :idCondition: str - condition id involved + :idCondition: int - condition id involved :return: """ url = 'rest/default-reviewers/1.0/projects/{projectKey}/repos/{repoKey}/condition/{idCondition}'.format( projectKey=project_key, repoKey=repo_key, idCondition=id_condition) - return (self.delete(url) or {}) - - def get_repo_details(self, project_key, repo_key): - """ - Return repository details for this repository slug inside project specified. - For further information visit: https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8287366304 - :projectKey: str- project key involved - :repoKey: str - repo key involved - :return: - """ - url = 'rest/api/1.0/projects/{projectKey}/repos/{repoKey}'.format( - projectKey=project_key, - repoKey=repo_key) - return (self.get(url) or {}) \ No newline at end of file + return (self.delete(url) or {}) \ No newline at end of file diff --git a/docs/bitbucket.rst b/docs/bitbucket.rst index 71374a36c..f1840ac07 100644 --- a/docs/bitbucket.rst +++ b/docs/bitbucket.rst @@ -206,3 +206,48 @@ Pull Request management # Reopen pull request bitbucket.reopen_pull_request(project_key, repository, pr_id, pr_version) + +Conditions-Reviewers management +----------------------- + +.. code-block:: python + + # Get all project conditions with reviewers list for specific project + bitbucket.get_project_conditions(project_key) + + # Get a project condition with reviewers list for specific project + bitbucket.get_project_condition(project_key, id_condition) + + # Create project condition with reviewers for specific project + # :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + bitbucket.create_project_condition(project_key, condition) + + # Update a project condition with reviewers for specific project + # :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + bitbucket.update_project_condition(project_key, condition, id_condition) + + # Delete a project condition for specific project + bitbucket.delete_project_condition(project_key, id_condition) + + # Get all repository conditions with reviewers list for specific repository in project + bitbucket.get_repo_conditions(project_key, repo_key) + + # Get repository conditions with reviewers list only only conditions type PROJECT for specific repository in project + bitbucket.get_repo_project_conditions(project_key, repo_key) + + # Get repository conditions with reviewers list only conditions type REPOSITORY for specific repository in project + bitbucket.get_repo_repo_conditions(project_key, repo_key) + + # Get a project condition with reviewers list for specific repository in project + bitbucket.get_repo_condition(project_key, repo_key, id_condition) + + # Create project condition with reviewers for specific repository in project + # :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + bitbucket.create_repo_condition(project_key, repo_key, condition) + + # Update a project condition with reviewers for specific repository in project + # :example condition: '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id": 12}],"requiredApprovals":"0"}' + bitbucket.update_repo_condition(project_key, repo_key, condition, id_condition) + + # Delete a project condition for specific repository in project + bitbucket.delete_repo_condition(project_key, repo_key, id_condition) \ No newline at end of file