From 5ef2c5314edf5e006e6180adf792d979e7dc6db0 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 15 May 2019 12:05:11 +0530 Subject: [PATCH] Fixed error handling in github_issue module (#39652) * Fixed error handling in github_issue module Due to recent changes in github3.py library module stopped working. This fix adds extra error handling for new changes in library. Fixes: #39627 Signed-off-by: Abhijeet Kasurde * Check version Signed-off-by: Abhijeet Kasurde * Refactor github_issue Signed-off-by: Abhijeet Kasurde --- .../modules/source_control/github_issue.py | 63 +++++++++---------- test/integration/targets/github_issue/aliases | 1 - .../github_issue/tasks/FreeBSD-python2.yml | 3 - .../github_issue/tasks/MacOSX-python2.yml | 1 - .../github_issue/tasks/default-python2.yml | 3 - .../github_issue/tasks/default-python3.yml | 3 - .../targets/github_issue/tasks/main.yml | 53 +--------------- test/sanity/validate-modules/ignore.txt | 4 +- 8 files changed, 34 insertions(+), 97 deletions(-) delete mode 100644 test/integration/targets/github_issue/tasks/FreeBSD-python2.yml delete mode 100644 test/integration/targets/github_issue/tasks/MacOSX-python2.yml delete mode 100644 test/integration/targets/github_issue/tasks/default-python2.yml delete mode 100644 test/integration/targets/github_issue/tasks/default-python3.yml diff --git a/lib/ansible/modules/source_control/github_issue.py b/lib/ansible/modules/source_control/github_issue.py index 4ac47917e2a..80f5db30afe 100644 --- a/lib/ansible/modules/source_control/github_issue.py +++ b/lib/ansible/modules/source_control/github_issue.py @@ -1,21 +1,25 @@ #!/usr/bin/python -# (c) 2017, Abhijeet Kasurde -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Copyright: (c) 2017-18, Abhijeet Kasurde +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type -ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'community'} +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} DOCUMENTATION = ''' module: github_issue short_description: View GitHub issue. description: - - View GitHub issue for a given repository. + - View GitHub issue for a given repository and organization. version_added: "2.4" options: repo: @@ -35,12 +39,9 @@ options: - Get various details about issue depending upon action specified. default: 'get_status' choices: - - ['get_status'] - + - 'get_status' author: - Abhijeet Kasurde (@Akasurde) -requirements: - - "github3.py >= 1.0.0a4" ''' RETURN = ''' @@ -66,17 +67,10 @@ EXAMPLES = ''' when: r.issue_status == 'open' ''' -import traceback +import json -GITHUB_IMP_ERR = None -try: - import github3 - HAS_GITHUB_PACKAGE = True -except ImportError: - GITHUB_IMP_ERR = traceback.format_exc() - HAS_GITHUB_PACKAGE = False - -from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.urls import fetch_url def main(): @@ -84,16 +78,12 @@ def main(): argument_spec=dict( organization=dict(required=True), repo=dict(required=True), - issue=dict(required=True), - action=dict(required=False, choices=['get_status']), + issue=dict(type='int', required=True), + action=dict(choices=['get_status'], default='get_status'), ), supports_check_mode=True, ) - if not HAS_GITHUB_PACKAGE: - module.fail_json(msg=missing_required_lib('github3.py >= 1.0.0a4'), - exception=GITHUB_IMP_ERR) - organization = module.params['organization'] repo = module.params['repo'] issue = module.params['issue'] @@ -101,17 +91,26 @@ def main(): result = dict() - gh_obj = github3.issue(organization, repo, issue) - if gh_obj is None: - module.fail_json(msg="Failed to get details about issue specified. " - "Please check organization, repo and issue " - "details and try again.") + headers = { + 'Content-Type': 'application/json', + 'Accept': 'application/vnd.github.v3+json', + } + + url = "https://api.github.com/repos/%s/%s/issues/%s" % (organization, repo, issue) + + response, info = fetch_url(module, url, headers=headers) + if not (200 <= info['status'] < 400): + if info['status'] == 404: + module.fail_json(msg="Failed to find issue %s" % issue) + module.fail_json(msg="Failed to send request to %s: %s" % (url, info['msg'])) + + gh_obj = json.loads(response.read()) if action == 'get_status' or action is None: if module.check_mode: result.update(changed=True) else: - result.update(changed=True, issue_status=gh_obj.state) + result.update(changed=True, issue_status=gh_obj['state']) module.exit_json(**result) diff --git a/test/integration/targets/github_issue/aliases b/test/integration/targets/github_issue/aliases index b1e2213f2d0..a4c92ef8538 100644 --- a/test/integration/targets/github_issue/aliases +++ b/test/integration/targets/github_issue/aliases @@ -1,3 +1,2 @@ destructive shippable/posix/group1 -disabled diff --git a/test/integration/targets/github_issue/tasks/FreeBSD-python2.yml b/test/integration/targets/github_issue/tasks/FreeBSD-python2.yml deleted file mode 100644 index 6168af0426f..00000000000 --- a/test/integration/targets/github_issue/tasks/FreeBSD-python2.yml +++ /dev/null @@ -1,3 +0,0 @@ -- name: install py27-requests to avoid install via pip later - package: - name: py27-requests diff --git a/test/integration/targets/github_issue/tasks/MacOSX-python2.yml b/test/integration/targets/github_issue/tasks/MacOSX-python2.yml deleted file mode 100644 index 687b02517fa..00000000000 --- a/test/integration/targets/github_issue/tasks/MacOSX-python2.yml +++ /dev/null @@ -1 +0,0 @@ -# nothing to do here, allow requests to be installed by pip later diff --git a/test/integration/targets/github_issue/tasks/default-python2.yml b/test/integration/targets/github_issue/tasks/default-python2.yml deleted file mode 100644 index 33a27651215..00000000000 --- a/test/integration/targets/github_issue/tasks/default-python2.yml +++ /dev/null @@ -1,3 +0,0 @@ -- name: install python-requests to avoid install via pip later - package: - name: python-requests diff --git a/test/integration/targets/github_issue/tasks/default-python3.yml b/test/integration/targets/github_issue/tasks/default-python3.yml deleted file mode 100644 index d776a675c6d..00000000000 --- a/test/integration/targets/github_issue/tasks/default-python3.yml +++ /dev/null @@ -1,3 +0,0 @@ -- name: install python3-requests to avoid install via pip later - package: - name: python3-requests diff --git a/test/integration/targets/github_issue/tasks/main.yml b/test/integration/targets/github_issue/tasks/main.yml index eed2de34f91..36fe9aeffd1 100644 --- a/test/integration/targets/github_issue/tasks/main.yml +++ b/test/integration/targets/github_issue/tasks/main.yml @@ -1,56 +1,8 @@ # Test code for the github_issue module. -# (c) 2017, Abhijeet Kasurde - -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . -# - -- name: make sure github3.py is not installed - pip: - name: github3.py - state: absent - -# Testcase 0001: Check if dependency is installed -- name: Check if github3.py is installed or not - github_issue: - organization: "{{ organization }}" - repo: "{{ repo }}" - issue: "{{ non_existent_issue }}" - action: get_status - register: lib_missing - ignore_errors: True - -- assert: - that: - - "{{ lib_missing.failed == True }}" - - "{{ lib_missing.changed == False }}" - - "{{ 'Missing required github3 module.' in lib_missing.msg }}" - -- include: '{{ item }}' - with_first_found: - - files: - - '{{ ansible_distribution }}-{{ ansible_distribution_version }}-python{{ ansible_python_version.split(".")[0] }}.yml' - - '{{ ansible_distribution }}-python{{ ansible_python_version.split(".")[0] }}.yml' - - '{{ ansible_os_family }}-python{{ ansible_python_version.split(".")[0] }}.yml' - - 'default-python{{ ansible_python_version.split(".")[0] }}.yml' - -- name: make sure github3.py is installed - pip: - name: github3.py +# Copyright: (c) 2017-2018, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# Testcase 0002: Check if issue exists - name: Check if GitHub issue is closed or not github_issue: organization: "{{ organization }}" @@ -64,7 +16,6 @@ - "{{ get_status_0002.changed == True }}" - "{{ get_status_0002.issue_status == 'closed' }}" -# Testcase 0003: Check non-existent issue status - name: Check if GitHub issue is closed or not github_issue: organization: "{{ organization }}" diff --git a/test/sanity/validate-modules/ignore.txt b/test/sanity/validate-modules/ignore.txt index 14ac159d015..65efa15ec2f 100644 --- a/test/sanity/validate-modules/ignore.txt +++ b/test/sanity/validate-modules/ignore.txt @@ -738,8 +738,6 @@ lib/ansible/modules/remote_management/ucs/ucs_vsans.py E322 lib/ansible/modules/remote_management/ucs/ucs_wwn_pool.py E322 lib/ansible/modules/remote_management/ucs/ucs_wwn_pool.py E323 lib/ansible/modules/source_control/github_deploy_key.py E336 -lib/ansible/modules/source_control/github_issue.py E324 -lib/ansible/modules/source_control/github_issue.py E326 lib/ansible/modules/source_control/subversion.py E322 lib/ansible/modules/storage/infinidat/infini_export.py E323 lib/ansible/modules/storage/infinidat/infini_export.py E324 @@ -800,4 +798,4 @@ lib/ansible/modules/web_infrastructure/gunicorn.py E322 lib/ansible/modules/web_infrastructure/htpasswd.py E326 lib/ansible/modules/web_infrastructure/jenkins_plugin.py E322 lib/ansible/modules/web_infrastructure/jenkins_plugin.py E324 -lib/ansible/modules/web_infrastructure/jira.py E322 +lib/ansible/modules/web_infrastructure/jira.py E322 \ No newline at end of file