From 4c14df6d8848618bf7d24c4eb14d24a49041da86 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 17 Dec 2020 12:27:07 +0530 Subject: [PATCH] bitbucket_pipeline_variable: Change pagination logic (#1498) Bitbucket's pagination for pipeline variables is flawed. Refactor bitbucket_pipeline_variable code to correct this logic. Fixes: #1425 Signed-off-by: Abhijeet Kasurde --- .../1425_bitbucket_pipeline_variable.yml | 2 ++ .../bitbucket/bitbucket_pipeline_variable.py | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/1425_bitbucket_pipeline_variable.yml diff --git a/changelogs/fragments/1425_bitbucket_pipeline_variable.yml b/changelogs/fragments/1425_bitbucket_pipeline_variable.yml new file mode 100644 index 0000000000..b284ca54d3 --- /dev/null +++ b/changelogs/fragments/1425_bitbucket_pipeline_variable.yml @@ -0,0 +1,2 @@ +bugfixes: +- bitbucket_pipeline_variable - change pagination logic for pipeline variable get API (https://github.com/ansible-collections/community.general/issues/1425). diff --git a/plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py b/plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py index d0eb86e6da..c4ca59a3f1 100644 --- a/plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py +++ b/plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py @@ -72,7 +72,7 @@ EXAMPLES = r''' secured: '{{ item.secured }}' state: present with_items: - - { name: AWS_ACCESS_KEY, value: ABCD1234 } + - { name: AWS_ACCESS_KEY, value: ABCD1234, secured: False } - { name: AWS_SECRET, value: qwe789poi123vbn0, secured: True } - name: Remove pipeline variable @@ -119,17 +119,16 @@ def get_existing_pipeline_variable(module, bitbucket): The `value` key in dict is absent in case of secured variable. """ - content = { - 'next': BITBUCKET_API_ENDPOINTS['pipeline-variable-list'].format( - username=module.params['username'], - repo_slug=module.params['repository'], - ) - } - + variables_base_url = BITBUCKET_API_ENDPOINTS['pipeline-variable-list'].format( + username=module.params['username'], + repo_slug=module.params['repository'], + ) # Look through the all response pages in search of variable we need - while 'next' in content: + page = 1 + while True: + next_url = "%s?page=%s" % (variables_base_url, page) info, content = bitbucket.request( - api_url=content['next'], + api_url=next_url, method='GET', ) @@ -139,6 +138,11 @@ def get_existing_pipeline_variable(module, bitbucket): if info['status'] != 200: module.fail_json(msg='Failed to retrieve the list of pipeline variables: {0}'.format(info)) + # We are at the end of list + if 'pagelen' in content and content['pagelen'] == 0: + return None + + page += 1 var = next(filter(lambda v: v['key'] == module.params['name'], content['values']), None) if var is not None: