---
- hosts: all
  tasks:
    - name: Read project docs configuration
      ansible.builtin.include_vars: "{{ docs_update_data_file }}"

    - name: Save doc var
      ansible.builtin.set_fact:
        doc_exports_var: "{{ categories[docs_service_category] | default({}) }}"
        cacheable: true

    - name: Detect list of changes
      ansible.builtin.command: "git log -1 --name-only --pretty="
      args:
        chdir: "{{ zuul.project.src_dir }}"
      register: git_log
      ignore_errors: true
      changed_when: false

    - name: Generate RSTs
      ansible.builtin.include_role:
        name: "convert_doc"
      vars:
        doc_label: "{{ doc.0.service_type }}_{{ doc.1.type }}"
        dest: "{{ ansible_user_dir }}/{{ doc.0.repository }}/{{ doc.1.rst_location }}"
        source: "{{ zuul.project.src_dir }}/{{ doc.1.html_location }}"
        doc_title: "{{ [ doc.0.service_title, doc.1.title ] | join(' - ') }}"
        doc_service: "{{ doc.0.service_title | default(omit) }}"
        doc_repo_name: "{{ doc.0.repository | default(omit) }}"
        doc_pdf_name: "{{ [ doc.1.type, doc.0.service_type ] | join('-') }}"
      loop: "{{ doc_exports_var | subelements('docs') }}"
      loop_control:
        loop_var: "doc"
        label: "{{ doc.0.service_type }}_{{ doc.1.type }}"
      when:
        - "doc.1.html_location is defined"
        - "doc.1.rst_location is defined"
        - |
          git_log.stdout is search(doc.1.html_location)
          or git_log.stdout is search('otc_doc_convertor')
          or git_log.stdout is search('playbooks')
          or git_log.stdout is search('templates')
