
    Vh6                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZmZmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona+	  
module: gitlab_issue
short_description: Create, update, or delete GitLab issues
version_added: '8.1.0'
description:
  - Creates an issue if it does not exist.
  - When an issue does exist, it will be updated if the provided parameters are different.
  - When an issue does exist and O(state=absent), the issue will be deleted.
  - When multiple issues are detected, the task fails.
  - Existing issues are matched based on O(title) and O(state_filter) filters.
author:
  - zvaraondrej (@zvaraondrej)
requirements:
  - python-gitlab >= 2.3.0
extends_documentation_fragment:
  - community.general.auth_basic
  - community.general.gitlab
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  assignee_ids:
    description:
      - A list of assignee usernames omitting V(@) character.
      - Set to an empty array to unassign all assignees.
    type: list
    elements: str
  description:
    description:
      - A description of the issue.
      - Gets overridden by a content of file specified at O(description_path), if found.
    type: str
  description_path:
    description:
      - A path of file containing issue's description.
      - Accepts MarkDown formatted files.
    type: path
  issue_type:
    description:
      - Type of the issue.
    default: issue
    type: str
    choices: ["issue", "incident", "test_case"]
  labels:
    description:
      - A list of label names.
      - Set to an empty array to remove all labels.
    type: list
    elements: str
  milestone_search:
    description:
      - The name of the milestone.
      - Set to empty string to unassign milestone.
    type: str
  milestone_group_id:
    description:
      - The path or numeric ID of the group hosting desired milestone.
    type: str
  project:
    description:
      - The path or name of the project.
    required: true
    type: str
  state:
    description:
      - Create or delete issue.
    default: present
    type: str
    choices: ["present", "absent"]
  state_filter:
    description:
      - Filter specifying state of issues while searching.
    type: str
    choices: ["opened", "closed"]
    default: opened
  title:
    description:
      - A title for the issue. The title is used as a unique identifier to ensure idempotency.
    type: str
    required: true
aI  
- name: Create Issue
  community.general.gitlab_issue:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    title: "Ansible demo Issue"
    description: "Demo Issue description"
    labels:
      - Ansible
      - Demo
    assignee_ids:
      - testassignee
    state_filter: "opened"
    state: present

- name: Delete Issue
  community.general.gitlab_issue:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    title: "Ansible demo Issue"
    state_filter: "opened"
    state: absent
z
msg:
  description: Success or failure message.
  returned: always
  type: str
  sample: "Success"

issue:
  description: API object.
  returned: success
  type: dict
)AnsibleModule)basic_auth_argument_spec)	to_nativeto_text)auth_argument_specgitlab_authenticationgitlabfind_project
find_groupc                   R    e Zd Zd Z	 d Z	 d Z	 d Z	 d Z	 d Z	 d Z		 d Z
	 d	 Zy
)GitlabIssuec                 .    || _         || _        || _        y N)_gitlab_moduleproject)selfmoduler   gitlab_instances       r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_issue.py__init__zGitlabIssue.__init__   s    &    c                    g }	 |j                   j                  |      }t        |      dkD  r| j
                  j                  d       t        |      dk  r| j
                  j                  d       t        |      dk(  r*	 |j                   j                  |d   j                        S y # t        j                  j                  $ r2}| j
                  j                  dt        |      z         Y d }~d }~ww xY w# t        j                  j                  $ r2}| j
                  j                  d	t        |      z         Y d }~y d }~ww xY w)
N)searchz!Failed to list the Milestones: %smsg   z,Multiple Milestones matched search criteria.z&No Milestones matched search criteria.r   idz Failed to get the Milestones: %s)
milestoneslistr   
exceptionsGitlabGetErrorr   	fail_jsonr   lengetr"   )r   milestone_idgroupr#   es        r   get_milestonezGitlabIssue.get_milestone   s.   
	[))..l.CJ z?QLL""'U"Vz?QLL""'O"Pz?a^''++z!}/?/?+@@     // 	[LL""'JYWX\'Y"ZZ	[ $$33 ^&&+MPYZ[P\+\&]]^s/   B- (C? -C<
(C77C<?E(E		Ec                 ~   g }	 | j                   j                  j                  |d|d      }t        |      dkD  r| j                  j                  d       t        |      dk(  r4	 | j                   j                  j                  |d   j                  	      S y # t        j                  j
                  $ r2}| j                  j                  dt        |      z         Y d }~d }~ww xY w# t        j                  j
                  $ r2}| j                  j                  d
t        |      z         Y d }~y d }~ww xY w)Ntitle)r   instate)query_parameterszFailed to list the Issues: %sr   r    z(Multiple Issues matched search criteria.r   r!   zFailed to get the Issue: %s)r   issuesr$   r   r%   r&   r   r'   r   r(   r)   iid)r   r/   state_filterr3   r,   s        r   	get_issuezGitlabIssue.get_issue   s   	W\\((--%W^iu?v-wF v;?LL""'Q"Rv;!Y||**..&)--.@@    // 	WLL""'FST'U"VV	W $$33 Y&&+H9UV<+W&XXYs/   *B '2C- C*8(C%%C*-D<
(D77D<c                    g }	 | j                   j                  j                  |d      D cg c]  }|j                  |k(  s| }}t        |      dkD  r| j                  j                  d       y t        |      dk  r| j                  j                  d       y |d   S c c}w # t        j
                  j                  $ r2}| j                  j                  dt        |      z         Y d }~d }~ww xY w)	NT)usernameallzFailed to list the users: %sr   r    z'Multiple Users matched search criteria.z No User matched search criteria.r   )r   usersr$   r8   r   r%   r&   r   r'   r   r(   )r   r8   r:   userr,   s        r   get_userzGitlabIssue.get_user   s    	V&*ll&8&8&=&=xUY&=&Zxd^b^k^kow^wTxEx u:>LL""'P"QZ!^LL""'I"J8O y  // 	VLL""'E	RS'T"UU	Vs.   )B( B#B#B( #B( (C7(C22C7c                 ^    |D cg c]  }| j                  |      j                   c}S c c}w r   )r<   r"   )r   r:   r;   s      r   get_user_idszGitlabIssue.get_user_ids   s%    3894d#&&999s   "*c                 d   | j                   j                  r#| j                   j                  dd|d   z         	 | j                  j                  j                  |      S # t        j                  j                  $ r2}| j                   j                  dt        |      z         Y d }~y d }~ww xY w)NTz Successfully created Issue '%s'.r/   changedr   zFailed to create Issue: %s r   )r   
check_mode	exit_jsonr   r3   creater   r%   GitlabCreateErrorr'   r   )r   optionsr,   s      r   create_issuezGitlabIssue.create_issue   s    <<""LL""45WZabiZj5j"k	U<<&&--g66  22 	ULL""'DyQR|'S"TT	Us   $A   B/=(B**B/c                 :   | j                   j                  r#| j                   j                  dd|d   z         	 |j                         S # t        j
                  j                  $ r2}| j                   j                  dt        |      z         Y d }~y d }~ww xY w)NTz Successfully deleted Issue '%s'.r/   r@   zFailed to delete Issue: '%s'.r   )	r   rB   rC   deleter   r%   GitlabDeleteErrorr'   r   )r   issuer,   s      r   delete_issuezGitlabIssue.delete_issue   s    <<""LL""45WZ_`gZh5h"i	W<<>!  22 	WLL""'FST'U"VV	Ws   A B((BBc                 z   | j                   j                  r#| j                   j                  dd|d   z         	 | j                  j                  j                  |j                  |      S # t        j                  j                  $ r2}| j                   j                  dt        |      z         Y d }~y d }~ww xY w)NTz Successfully updated Issue '%s'.r/   r@   zFailed to update Issue %s.r   )r   rB   rC   r   r3   updater4   r   r%   GitlabUpdateErrorr'   r   )r   rK   rF   r,   s       r   update_issuezGitlabIssue.update_issue   s    <<""LL""45WZ_`gZh5h"i	T<<&&--eiiAA  22 	TLL""'CiPQl'R"SS	Ts   /A+ +B:(B55B:c           	      \   |j                         D ]  \  }}|	|dk(  r%t        |d      rt        |d      d   nd}||k7  s1 y|dk(  r,|t        t        |d      D cg c]  }|d   	 c}      k7  sb y|dk(  r|t        t        ||            k7  s yt        ||      |k7  s y y	c c}w )
Nr*   	milestoner"    Tassignee_ids	assigneeslabelsF)itemsgetattrsorted)r   rK   rF   keyvalueold_milestoner;   s          r   issue_has_changedzGitlabIssue.issue_has_changed  s    !--/ 	 JC .(IPQVXcIdGE;$?$EjlM-#N*wuk?Z'[tT
'[ \\#H_wuc': ;;#UC(E1!	 $  (\s   B)N)__name__
__module____qualname__r   r-   r6   r<   r>   rG   rL   rP   r]    r   r   r   r      s\    
^"Y:UWTr   r   c                     t               } | j                  t                      | j                  t        ddd      t        dd      t        dd      t        ddg dd	      t        ddd      t        dd      t        dd      t        dd
      t        ddddg      t        ddddg      t        dd
             t	        | ddgddgddgddgddgddggddgddggg dgd
      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }	|j
                  d    }
|j
                  d!   }|j
                  d"   }t        |d#$      }t        ||	      }||j                  d%|	z  &       t        |||'      }|rA|r?t        ||      }||j                  d(|z  &       |j                  ||      j                  }|j                  ||      }|
dk(  r+|r1	 t        |d)      5 }t        |j!                         d*+      }d d d        |rt'        |j)                  |            n|}|rt'        |      n|}||||||d-}|sD|j+                  |      }|j-                  d
d.j%                  |/      |j/                         0       y |j1                  ||      r7|j3                  ||      }|j-                  d
d1j%                  |/      |0       y |j-                  dd2j%                  |/      |j/                         0       y |
dk(  r\|s$|j-                  dd3j%                  |/      4       y |j5                  |      }|j-                  d
d5j%                  |/      |0       y y # 1 sw Y   dxY w# t"        $ r-}|j                  d,j%                  ||      &       Y d }~d }~ww xY w)6Nr$   strF)typeelementsrequired)rd   rf   pathrK   )rK   incident	test_case)rd   defaultchoicesrf   Tpresentabsent)rd   rj   rk   openedclosed)rT   descriptiondescription_path
issue_typerV   milestone_searchmilestone_group_idr   r1   r5   r/   api_username	api_tokenapi_oauth_tokenapi_job_tokenrp   rq   api_passwordrs   rt   )ru   rv   rw   rx   )argument_specmutually_exclusiverequired_togetherrequired_one_ofsupports_check_moderT   rr   rV   r   r1   r5   r/   z2.3.0)min_versionzFailed to get the project: %sr   )r   r   r   zFailed to get the group: %srbsurrogate_or_strict)errorszCannot open {0}: {1})r/   rp   rV   rr   r*   rT   zCreated Issue '{t}'.)t)rA   r   rK   zUpdated Issue '{t}'.zIssue '{t}' already existsz7Issue '{t}' does not exist or has already been deleted.r@   zIssue '{t}' deleted.)r   rN   r
   dictr   paramsr   r   r'   r   r   r-   r"   r6   openr	   readIOErrorformatrY   r>   rG   rC   asdictr]   rP   rL   )rz   r   rT   rp   rq   rr   rV   r*   rt   r   r1   r5   r/   r   this_projectthis_gitlab
this_group
this_issuefr,   rF   rK   s                         r   mainr     sc   ,.M+-.vFee46E:UG=_jop%%@559UU;%$/y8Y:OPuh8@TU-   #[)./_-+,/*./
 ^,!56

 N
 !#F( ==0L--.K}}%78|,J]]8$F==!34L';<mmI&GMM'"E==0LMM'"E ,FHO9L<wFGV\SbcK*1CD
!>AS!ST"00zJMM&&ul;J	Y*D1 RQ")!&&(;P"QKR JVvk66|DE[g#)v &$((
 ,,W5E"8"?"?%"?"Hlln  
 ,,ZA#00WE   &<&C&Ce&C&L ! 
   !'C'J'JU'J'S$++- !  
(	U0i0p0psx0p0yz,,Z8E"8"?"?%"?"H   
KR R Y  %;%B%BCSUV%W XXYs0   1O =N;O ;O O 	O>"O99O>__main__N)
__future__r   r   r   rd   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.apir   +ansible.module_utils.common.text.convertersr   r	   Aansible_collections.community.general.plugins.module_utils.gitlabr
   r   r   r   r   objectr   r   r^   ra   r   r   <module>r      si    A @Up4
 5 = J 
A& AHsl zF r   