
    Vh,                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ d d	lmZ dZ	 d d
lmZ d dlmZ dZ	 ddZd Zedk(  r e        yy# e$ r  e	j4                         ZdZY 0w xY w)    )absolute_importdivisionprint_functionaq  
module: taiga_issue
short_description: Creates/deletes an issue in a Taiga Project Management Platform
description:
  - Creates/deletes an issue in a Taiga Project Management Platform (U(https://taiga.io)).
  - An issue is identified by the combination of project, issue subject and issue type.
  - This module implements the creation or deletion of issues (not the update).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  taiga_host:
    type: str
    description:
      - The hostname of the Taiga instance.
    default: https://api.taiga.io
  project:
    type: str
    description:
      - Name of the project containing the issue. Must exist previously.
    required: true
  subject:
    type: str
    description:
      - The issue subject.
    required: true
  issue_type:
    type: str
    description:
      - The issue type. Must exist previously.
    required: true
  priority:
    type: str
    description:
      - The issue priority. Must exist previously.
    default: Normal
  status:
    type: str
    description:
      - The issue status. Must exist previously.
    default: New
  severity:
    type: str
    description:
      - The issue severity. Must exist previously.
    default: Normal
  description:
    type: str
    description:
      - The issue description.
    default: ""
  attachment:
    type: path
    description:
      - Path to a file to be attached to the issue.
  attachment_description:
    type: str
    description:
      - A string describing the file to be attached to the issue.
    default: ""
  tags:
    type: list
    elements: str
    description:
      - A lists of tags to be assigned to the issue.
    default: []
  state:
    type: str
    description:
      - Whether the issue should be present or not.
    choices: ["present", "absent"]
    default: present
author: Alejandro Guirao (@lekum)
requirements: [python-taiga]
notes:
  - The authentication is achieved either by the environment variable E(TAIGA_TOKEN) or by the pair of environment variables
    E(TAIGA_USERNAME) and E(TAIGA_PASSWORD).
a   
- name: Create an issue in the my hosted Taiga environment and attach an error log
  community.general.taiga_issue:
    taiga_host: https://mytaigahost.example.com
    project: myproject
    subject: An error has been found
    issue_type: Bug
    priority: High
    status: New
    severity: Important
    description: An error has been found. Please check the attached error log for details.
    attachment: /path/to/error.log
    attachment_description: Error log file
    tags:
      - Error
      - Needs manual check
    state: present

- name: Deletes the previously created issue
  community.general.taiga_issue:
    taiga_host: https://mytaigahost.example.com
    project: myproject
    subject: An error has been found
    issue_type: Bug
    state: absent
#N)getenv)isfile)AnsibleModulemissing_required_lib)	to_native)TaigaAPI)TaigaExceptionTFc           	      N  # d}	 t        d      }|rt        | |      }nHt        |       }t        d      }t        d      }t        ||g      sd|di fS |j                  ||       |j	                         j
                  }t        t        fd	|j                  j                  |
                  }t        |      dk7  r	d|dz  i fS |d   }|j
                  }t        t        fd|j                  j                  |                  }t        |      dk7  rd|ddi fS |d   j
                  }t        t        fd|j                  j                  |                  }t        |      dk7  rd|ddi fS |d   j
                  }t        t        fd|j                                     }t        |      dk7  rd|ddi fS |d   j
                  #t        t        fd|j                                     }t        |      dk7  rd|ddi fS |d   j
                  }||
d}t        t        #fd|j                                     }t        |      }|dk(  rL|dk(  rAd}|s7|j                  ||#||
|      } |r| j!                  ||	       ||d<   |	|d<   d|d |fS d|d!i fS |dk(  r(|d"k(  rd}|s|d   j#                          d|d#i fS d|d$i fS d|d%d&i fS # t$        $ r}!d't'        |!      z  }"d||"i fcY d(}!~!S d(}!~!ww xY w))a  
    Method that creates/deletes issues depending whether they exist and the state desired

    The credentials should be passed via environment variables:
        - TAIGA_TOKEN
        - TAIGA_USERNAME and TAIGA_PASSWORD

    Returns a tuple with these elements:
        - A boolean representing the success of the operation
        - A descriptive message
        - A dict with the issue attributes, in case of issue creation, otherwise empty dict
    FTAIGA_TOKEN)hosttoken)r   TAIGA_USERNAMETAIGA_PASSWORDzMissing credentials)usernamepasswordc                 "    | j                   k(  S Nname)xproject_names    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/taiga_issue.py<lambda>zmanage_issue.<locals>.<lambda>   s    QVV|-C     )member   zUnable to find project %sr   c                 "    | j                   k(  S r   r   )r   issue_prioritys    r   r   zmanage_issue.<locals>.<lambda>       aff.F r   )projectzUnable to find issue priority z for project c                 "    | j                   k(  S r   r   )r   issue_statuss    r   r   zmanage_issue.<locals>.<lambda>   s    AFFl,B r   zUnable to find issue status c                 "    | j                   k(  S r   r   )r   
issue_types    r   r   zmanage_issue.<locals>.<lambda>   s    !&&J*> r   zUnable to find issue type c                 "    | j                   k(  S r   r   )r   issue_severitys    r   r   zmanage_issue.<locals>.<lambda>   r#   r   zUnable to find severity )r$   subjectprioritystatustypeseveritydescriptiontagsc                 D    | j                   k(  xr | j                  k(  S r   )r+   r.   )r   issue_subjecttype_ids    r   r   zmanage_issue.<locals>.<lambda>   s"    AII4N4dSTSYSY]dSd r   presentT)r1   r0   )r0   
attachmentattachment_descriptionzIssue createdzIssue does not existabsentzIssue deletedzIssue already existsz!More than one issue with subject z in project zAn exception happened: %sN)r   r   anyauthmeidlistfilterprojectslen
prioritiesissue_statuseslist_issue_typeslist_severitieslist_issues	add_issueattachdeleter   r   )$
taiga_hostr   r3   r"   r&   r(   r*   issue_descriptionissue_attachmentissue_attachment_description
issue_tagsstate
check_modechangedr   apir   r   user_idproject_listr$   
project_idpriority_listpriority_idstatus_list	status_id	type_listseverity_listseverity_idissuematching_issue_listmatching_issue_list_len	new_issueexcmsgr4   s$    ``````                            @r   manage_issuerb      s   " G]'}%
%8C
+C./H./H(+,g'<b@@HHhH:&&(++F#CS\\EVEV^eEVEfgh|!'#>#MrQQq/ZZ
V$FH[H[dnH[Hopq}"'Xfht#uwyyy#A&))6"BCDVDVD[D[dnD[Dopq{q 'Vbdp#qsuuuN%%	 >@X@X@Z[\	y>Q'T^`l#moqqqA,//V$FH_H_Habc}"'R`bn#oqsss#A&)) $$&"&,	
 #6*dfmfyfyf{#|}"%&9":"a'	!! ' 1 1-iY`bmt~>O !2 !QI'!(()9Gc(d.>l+:V67Wou<< W&<b@@$) !'*113Wor99 W&<b@@ 'Zgiu#vxzzz ')IcN:gsB&&'s`   AK= A5K= AK= .AK= 	AK= AK= 'BK= >K= &K= +K= 1K= =	L$LL$L$c                  2   t        t        t        ddd      t        dd      t        dd      t        dd      t        ddd      t        ddd      t        ddd      t        ddd	      t        d
dd       t        ddd	      t        dg dd      t        ddddgd            d      } t        s | j                  t	        d      t
               | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
|	r"t        |	      sd|	z  }| j                  |       | j                  d    }| j                  d!   }t        |||||||||	|
||| j                  "      \  }}}}|r+|r| j                  |||#       y | j                  ||$       y | j                  |       y )%NstrFzhttps://api.taiga.io)r.   requireddefaultT)r.   re   NormalNew pathr=   )re   rf   r.   elementsr5   r8   )r.   re   choicesrf   )rI   r$   r+   r(   r,   r-   r/   r0   r6   r7   r1   rN   )argument_specsupports_check_modezpython-taiga)ra   	exceptionrI   r$   r+   r,   r-   r(   r/   r0   r6   r7   z%s is not a file)ra   r1   rN   )rO   )rP   ra   r\   )rP   ra   )r	   dictTAIGA_MODULE_IMPORTED	fail_jsonr
   TAIGA_IMP_ERRparamsr   rb   rO   	exit_json)modulerI   r   r3   r"   r&   r(   r*   rJ   rK   rL   ra   rM   rN   return_statusrP   issue_attr_dicts                    r   mainry      s!   @VWed3ed36uuhGUUEBuuhG%%D%F#'UUB#OubvNEEIx;PZcd
 !F$ !1.A][|,J==+LMM),M]]:.N==*L|,J]]:.Nm4}}\2#)==1I#J &'$'77C%v&JMM'"E3?$$$40M7C W#_MW#6S!r   __main__)F)
__future__r   r   r   r.   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackosr   os.pathr   ansible.module_utils.basicr	   r
   +ansible.module_utils.common.text.convertersr   rs   taigar   taiga.exceptionsr   rq   ImportError
format_excrb   ry   __name__ r   r   <module>r      s    A @Qf6 
    J A"/  05p'f<"~ zF o  "(I((*M!"s   A A32A3