---
# input is
# * update_issue_key - key of issue to update e.g. "RHEL-001"
# fields to update
- name: Update fields
  community.general.jira:
    operation: edit
    issue: "{{ update_issue_key }}"
  when: __fields | length > 0
  args:
    fields: "{{ __fields }}"
  vars:
    # the key is the name of the variable used in the playbooks
    # the fieldname is the field used in jira corresponding to the variable
    # updatetype is how to pass the update - see the __fields code below
    __field_map:
      product:
        fieldname: customfield_12319040
        updatetype: list_of_value_dict
      itr:
        fieldname: fixVersions
        updatetype: list_of_name_dict
      itm:
        fieldname: customfield_12321040
        updatetype: value_dict
      dtm:
        fieldname: customfield_12318141
        updatetype: value_dict
      doc_text:
        fieldname: customfield_12317313
        updatetype: string
      doc_text_type:
        fieldname: customfield_12320850
        updatetype: value_dict
      pool_team:
        fieldname: customfield_12317259
        updatetype: list_of_value_dict
      dev_ack:
        fieldname: customfield_12317366
        updatetype: list_of_value_dict
      qa_ack:
        fieldname: customfield_12317366
        updatetype: list_of_value_dict
      docs_impact:  # choice of No, Yes
        fieldname: customfield_12324040
        updatetype: value_dict
      request_clone_version:  # e.g. "RHEL 10"
        fieldname: customfield_12323242
        updatetype: value_dict
      fixed_in_build:
        fieldname: customfield_12318450
        updatetype: string
      sprint:
        fieldname: customfield_12310940
        updatetype: int
      story_points:
        fieldname: customfield_12310243
        updatetype: float
      preliminary_testing:
        fieldname: customfield_12321540
        updatetype: value_dict
    __roles_list: "{{ [] if labels | d([]) | length > 0
      else roles | d('') | split(',') | sort }}"
    __labels: "{{ labels if labels | d([]) | length > 0
        else ['system_role_'] | product(__roles_list) | map('join')
        if roles | d('') else [] }}"
    __lsr_label: "{{ ['RHEL-System-Roles']
      if not 'RHEL-System-Roles' in labels | d([]) else [] }}"
    __merged_label: "{{ ['fixed-upstream']
      if gh_pr_merged | d(false) and not 'fixed-upstream' in labels | d([])
      else [] }}"
    __field_names: "{{ __field_map.keys() | sort }}"
    __fields_defined: "{{ q('varnames', *__field_names) }}"
    __fields: |
      {% set rv = {} %}
      {% for var in __fields_defined %}
      {%   set val = lookup("vars", var) %}
      {%   if val is not none and val | length > 0 %}
      {%     if __field_map[var]["updatetype"] == "string" %}
      {%       set item = val %}
      {%       set _ = rv.update({__field_map[var]["fieldname"]: item}) %}
      {%     elif __field_map[var]["updatetype"] == "value_dict" %}
      {%       set item = {"value": val} %}
      {%       set _ = rv.update({__field_map[var]["fieldname"]: item}) %}
      {%     elif __field_map[var]["updatetype"] == "list_of_name_dict" %}
      {%       set item = {"name": val} %}
      {%       set _ = rv.setdefault(__field_map[var]["fieldname"], []).append(item) %}
      {%     elif __field_map[var]["updatetype"] == "list_of_value_dict" %}
      {%       set item = {"value": val} %}
      {%       set _ = rv.setdefault(__field_map[var]["fieldname"], []).append(item) %}
      {%     elif __field_map[var]["updatetype"] == "list" %}
      {%       set item = val %}
      {%       set _ = rv.setdefault(__field_map[var]["fieldname"], []).append(item) %}
      {%     elif __field_map[var]["updatetype"] == "float" %}
      {%       set item = val | float %}
      {%       set _ = rv.update({__field_map[var]["fieldname"]: item}) %}
      {%     elif __field_map[var]["updatetype"] == "int" %}
      {%       set item = val | int %}
      {%       set _ = rv.update({__field_map[var]["fieldname"]: item}) %}
      {%     endif %}
      {%   endif %}
      {% endfor %}
      {% if __labels %}
      {%   set _ = rv.update({"labels": __labels + __lsr_label + __merged_label}) %}
      {% endif %}
      {{ rv }}

- name: Add comment
  community.general.jira:
    operation: comment
    issue: "{{ update_issue_key }}"
    comment: "{{ comment }}"
  when: comment | d("") | length > 0

- name: Change issue status
  community.general.jira:
    operation: transition
    issue: "{{ update_issue_key }}"
    status: "{{ status }}"
  when: status | d("") | length > 0
  register: __result
  until: __result is success
