---
# input is
# * clone_issue_key - key of issue to clone e.g. "RHEL-001"
# * clone_version - version for clone
# * other fields - override fields from cloned issue

- name: Get issue to clone
  community.general.jira:
    operation: fetch
    issue: "{{ clone_issue_key }}"
  register: clone_issue

- name: Get cloned issue if already cloned
  set_fact:
    __outward_issue: "{{ __outward_list[0] if __outward_list | length > 0 else '' }}"
    __inward_issue: "{{ __inward_list[0] if __inward_list | length > 0 else '' }}"
  vars:
    __outward_list: "{{ clone_issue.meta.fields.issuelinks | d([]) |
      selectattr('type.name', 'match', '^Cloners') | map(attribute='outwardIssue.key') |
      list }}"
    __inward_list: "{{ clone_issue.meta.fields.issuelinks | d([]) |
      selectattr('type.name', 'match', '^Cloners') | map(attribute='inwardIssue.key') |
      list }}"

- name: Report if already cloned
  debug:
    msg: Issue {{ clone_issue.meta.key }} is already clone of {{ __outward_issue }}
  when: __outward_issue | length > 0

- name: Report if already cloned
  debug:
    msg: Issue {{ clone_issue.meta.key }} is already clone of {{ __inward_issue }}
  when: __inward_issue | length > 0

- name: Do clone
  when:
    - __outward_issue | length == 0
    - __inward_issue | length == 0
  block:
    - name: Set vars for creating the clone
      set_fact:
        summary: "{{ clone_issue.meta.fields.summary }}"
        description: "{{ clone_issue.meta.fields.description }}"
        issuetype: "{{ clone_issue.meta.fields.issuetype.name }}"
        component: "{{ clone_issue.meta.fields.components.0.name }}"
        labels: "{{ clone_issue.meta.fields.labels }}"
        product: "{{ clone_issue.meta.fields.customfield_12319040.0.value }}"
        doc_text: "{{ clone_issue.meta.fields.customfield_12317313 | d('') }}"
        doc_text_type: "{{ clone_issue.meta.fields.customfield_12320850.value | d('') }}"
        docs_impact: "{{ clone_issue.meta.fields.customfield_12324040.value | d('') }}"
        dev_ack: "{{ 'Dev ack' if clone_issue.meta.fields.customfield_12317366 |
          selectattr('value', 'eq', 'Dev ack') | length > 0 else '' }}"
        qa_ack: "{{ 'QE ack' if clone_issue.meta.fields.customfield_12317366 |
          selectattr('value', 'eq', 'QE ack') | length > 0 else '' }}"
        pool_team: "{{ clone_issue.meta.fields.customfield_12317259.0.value | d('') }}"
        itm: "{{ clone_issue.meta.fields.customfield_12321040.value | d('') | string }}"
        dtm: "{{ clone_issue.meta.fields.customfield_12318141.value | d('') | string }}"
        itr: "{{ clone_version }}"  # e.g. rhel-X.Y.Z
        version: "{{ clone_version }}"
        status: "{{ clone_issue.meta.fields.status.name }}"

    - name: Create the clone issue
      include_tasks: create_issue.yml

    - name: Link clone to original
      community.general.jira:
        operation: link
        linktype: Cloners
        outwardissue: "{{ clone_issue.meta.key }}"
        inwardissue: "{{ new_issue.meta.key }}"

    - name: Clone external links
      include_tasks: add_external_link.yml
      vars:
        clone_issue_key: "{{ new_issue.meta.key }}"
        issue_key: "{{ clone_issue.meta.key }}"
        external_link_url: ""
        external_link_title: ""
