# Test code for the github_issue module.
# (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>

# 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 <http://www.gnu.org/licenses/>.
#

- 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

# Testcase 0002: Check if issue exists
- name: Check if GitHub issue is closed or not
  github_issue:
    organization: "{{ organization }}"
    repo: "{{ repo }}"
    issue: "{{ issue }}"
    action: get_status
  register: get_status_0002

- assert:
    that:
      - "{{ 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 }}"
    repo: "{{ repo }}"
    issue: "{{ non_existent_issue }}"
    action: get_status
  register: get_status_0003
  ignore_errors: True

- assert:
    that:
      - "{{ get_status_0003.changed == False }}"
      - "{{ get_status_0003.failed == True }}"
      - "{{ 'Failed' in get_status_0003.msg }}"