
    Vhk@                         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mZmZmZ d dlmZ  G d	 d
e      Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: gitlab_milestone
short_description: Creates/updates/deletes GitLab Milestones belonging to project or group
version_added: 8.3.0
description:
  - When a milestone does not exist, it will be created.
  - When a milestone does exist, its value will be updated when the values are different.
  - Milestones can be purged.
author:
  - "Gabriele Pongelli (@gpongelli)"
requirements:
  - python-gitlab python module
extends_documentation_fragment:
  - community.general.auth_basic
  - community.general.gitlab
  - community.general.attributes

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

options:
  state:
    description:
      - Create or delete milestone.
    default: present
    type: str
    choices: ["present", "absent"]
  purge:
    description:
      - When set to V(true), delete all milestone which are not mentioned in the task.
    default: false
    type: bool
    required: false
  project:
    description:
      - The path and name of the project. Either this or O(group) is required.
    required: false
    type: str
  group:
    description:
      - The path of the group. Either this or O(project) is required.
    required: false
    type: str
  milestones:
    description:
      - A list of dictionaries that represents gitlab project's or group's milestones.
    type: list
    elements: dict
    required: false
    default: []
    suboptions:
      title:
        description:
          - The name of the milestone.
        type: str
        required: true
      due_date:
        description:
          - Milestone due date in YYYY-MM-DD format.
        type: str
        required: false
        default: null
      start_date:
        description:
          - Milestone start date in YYYY-MM-DD format.
        type: str
        required: false
        default: null
      description:
        description:
          - Milestone's description.
        type: str
        default: null
a  
# same project's task can be executed for group
- name: Create one milestone
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    milestones:
      - title: milestone_one
        start_date: "2024-01-04"
    state: present

- name: Create many group milestones
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    group: "group1"
    milestones:
      - title: milestone_one
        start_date: "2024-01-04"
        description: this is a milestone
        due_date: "2024-02-04"
      - title: milestone_two
    state: present

- name: Create many project milestones
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    milestones:
      - title: milestone_one
        start_date: "2024-01-04"
        description: this is a milestone
        due_date: "2024-02-04"
      - title: milestone_two
    state: present

- name: Set or update some milestones
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    milestones:
      - title: milestone_one
        start_date: "2024-05-04"
    state: present

- name: Add milestone in check mode
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    milestones:
      - title: milestone_one
        start_date: "2024-05-04"
    check_mode: true

- name: Delete milestone
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    milestones:
      - title: milestone_one
    state: absent

- name: Purge all milestones
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    purge: true

- name: Delete many milestones
  community.general.gitlab_milestone:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    state: absent
    milestones:
      - title: milestone-abc123
      - title: milestone-two
aN  
milestones:
  description: Four lists of the milestones which were added, updated, removed or exist.
  returned: success
  type: dict
  contains:
    added:
      description: A list of milestones which were created.
      returned: always
      type: list
      sample: ['abcd', 'milestone-one']
    untouched:
      description: A list of milestones which exist.
      returned: always
      type: list
      sample: ['defg', 'new-milestone']
    removed:
      description: A list of milestones which were deleted.
      returned: always
      type: list
      sample: ['defg', 'new-milestone']
    updated:
      description: A list pre-existing milestones whose values have been set.
      returned: always
      type: list
      sample: ['defg', 'new-milestone']
milestones_obj:
  description: API object.
  returned: success
  type: dict
)AnsibleModule)basic_auth_argument_spec)auth_argument_specgitlab_authenticationensure_gitlab_package
find_groupfind_project)datetimec                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	GitlabMilestonesc                 L    || _         |r|n|| _        |rdnd| _        || _        y )NTF)_gitlabgitlab_objectis_group_milestone_module)selfmodulegitlab_instancegroup_id
project_ids        v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_milestone.py__init__zGitlabMilestones.__init__   s(    &)1Xz*2$    c                     d}g }| j                   j                  j                  |      }t        |      dkD  r?||z  }|dz  }| j                   j                  j                  |      }t        |      dkD  r?|S )N   )pager   )r   
milestoneslistlen)r   page_nbr    	vars_pages       r   list_all_milestonesz$GitlabMilestones.list_all_milestones   s    
&&1166G6D	)nq )#JqLG**55:::HI )nq  r   c                    | j                   j                  ryd|j                  d      i}|j                  d      |j                  d      |d<   |j                  d      #| j                  |j                  d            |d<   |j                  d      #| j                  |j                  d            |d<   | j                  j
                  j                  |      }d|j                         fS NTTtitledescription
start_datedue_dateT)r   
check_modeget
check_dater   r    createasdict)r   var_objvar_objs       r   create_milestonez!GitlabMilestones.create_milestone   s    <<"" W[[)
 ;;}%1!(]!;C;;|$0 $L0I JC;;z"."oogkk*.EFC
O!!,,33C8T[[]""r   c                    | j                   j                  ry| j                  j                  j	                  | j                  |j	                  d                  }|j	                  d      |j	                  d      |_        |j	                  d      |j	                  d      |_        |j	                  d      |j	                  d      |_        |j                          d|j                         fS r'   )r   r-   r   r    r.   get_milestone_idr*   r+   r,   saver1   r   r2   
_milestones      r   update_milestonez!GitlabMilestones.update_milestone   s    <<""''2266t7L7LW[[Y`Ma7bc
;;}%1%,[[%?J";;|$0$+KK$=J!;;z".")++j"9J 	Z&&(((r   c                     | j                   j                  j                         }t        t        fd|            }|r|d   j                  S | j
                  j                  dz         y )Nc                 "    | j                   k(  S N)r)   )x_titles    r   <lambda>z3GitlabMilestones.get_milestone_id.<locals>.<lambda>  s    qww&'8 r   r   zmilestone '%s' not found.msg)r   r    r!   filteridr   	fail_json)r   r@   _milestone_list_founds    `  r   r7   z!GitlabMilestones.get_milestone_id  s[    ,,77<<>f8/JK!9<<LL""'BV'K"Lr   c                     	 t        j                  |d       |S # t        $ r# | j                  j	                  d|z         Y |S w xY w)Nz%Y-%m-%dz,milestone's date '%s' not in correct format.rB   )r   strptime
ValueErrorr   rF   )r   _dates     r   r/   zGitlabMilestones.check_date  sS    	_eZ0   	_LL""'UX]']"^	_s    (AAc                     | j                   j                  ry| j                  j                  j	                  | j                  |j	                  d                  }|j                          d|j                         fS )Nr(   r)   T)r   r-   r   r    r.   r7   deleter1   r9   s      r   delete_milestonez!GitlabMilestones.delete_milestone  sc    <<""''2266t7L7LW[[Y`Ma7bc
Z&&(((r   N)
__name__
__module____qualname__r   r%   r5   r;   r7   r/   rO    r   r   r   r      s&    #()$M)r   r   c                 f   t               }t               }t               }|dk(  rt               }|D ]$  }|j                  d|j                  d      i       & | D ]R  }||v r|j                  |       d|j                  d      i}	|	|v r|j                  |       B|j                  |       T |||fS )Npresentr)   )r!   appendr.   )
requested_milestonesexisting_milestonesstate	untouchedupdatedadded_existing_milestonesitemr3   compare_items
             r   comparer`   '  s     IfGFE	#v' 	FD ''$((72C(DE	F ( 	&C))  % ')9:#77NN3'LL%	& gu$$r   c                 &   d}t        g g g g       t        g g g       }| j                         D cg c]  }|j                          }}|D ]E  }	|	j                  d      d|	d<   |	j                  d      d |	d<   |	j                  d      Ad |	d<   G |D ]  }	|	j	                  d       |	j	                  d	       |	j	                  d
       |	j	                  d       |	j	                  d       |	j	                  d       |	j	                  d       d|	v r|	j	                  d       d|	v s|	j	                  d        |dk(  r|D cg c]	  }||vs| }
}|
D ]A  }		 | j                  |	      \  }}|r(d   j                  |	       |d   j                  |       C |r| j                         }|D ]A  }	| j                  |	      \  }}|sd   j                  |	       |d   j                  |       C n|dk(  r|sp|D cg c]  }|d   	 }}|D cg c]  }|d   |v s| }}|D ]A  }	| j                  |	      \  }}|sd   j                  |	       |d   j                  |       C nF|D ]A  }	| j                  |	      \  }}|sd   j                  |	       |d   j                  |       C |j                  r#t        |||      \  }}}t        ||d   |      t        fddD              rd}| j                         D cg c]  }|j                          }}||||fS c c}w c c}w # t        $ rB | j                  |	      \  }}|r(d   j                  |	       |d   j                  |       Y &w xY wc c}w c c}w c c}w )NFr\   r[   removedrZ   )r\   r[   rc   r*    r,   r+   rE   iid
created_atexpiredrY   
updated_atweb_urlr   r   rU   r\   r[   rc   absentr)   c              3   (   K   | ]	  }|     y wr>   rS   ).0r?   return_values     r   	<genexpr>z%native_python_main.<locals>.<genexpr>  s     
Dq<?
Ds   )r\   rc   r[   T)dictr%   r1   r.   popr5   rV   	Exceptionr;   rO   r-   r`   any)this_gitlabpurgerW   rY   r   change
return_objr?   milestones_beforer^   add_or_update_rvr4   _milestones_milestone_titles_requestedremove_requested
_untouched_updated_addedmilestones_afterrm   s                       @r   native_python_mainr   E  s   Fb"bBGLBB7J-8-L-L-NOOO % &88M"*"$D88J'#D88L!)!%D& " #HHZ 4HH\"# 	$8WqAEV<VWW! 	7D
7'88>	T )006w'..t4	7 %99;K) 7'88>	T +2248y)006	7 
(	?S*T!1W:*T'*T+<ja'
Ni@ijj( 7'88>	T +2248y)006	7 * 7'88>	T +2248y)006	7 './CEVX]'^$
Hf&(LQZD[gqr

D$C
DD,7,K,K,MNq
NN<!24DjPP[ P8 X  7'88>	T +2248y)0067& +Uj( Os<   L,	L1L1>L6NN	"N	N6AN Nc                  @   t               } | j                  t                      | j                  t        ddd       t        ddd       t        ddd      t        dddt	               t        t        dd      t        dd      t        dd      t        dd      	      
      t        ddddg             t        | ddgddgddgddgddgddggddggg dddggd      }t        |       |j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        |d      }t        ||      }t        ||      }	|s4|	s2|r|s|j                  d|z         |r|	s|j                  d|z         t        |||	|       }
t        |
||||      \  }}}}}|j                  s|D cg c]	  }||v s| c}|d!<   |d"   D cg c]  }|j                  d#       }}|d$   D cg c]  }|j                  d#       }}|d%   D cg c]  }|j                  d#       }}|d!   D cg c]  }|j                  d#       }}t        ||||&      }|j!                  |||'       y c c}w c c}w c c}w c c}w c c}w )(NstrF)typerequireddefaultboolr!   ro   T)r   r   )r)   r*   r,   r+   )r   elementsr   r   optionsrU   rj   )r   r   choices)projectgrouprt   r    rY   api_username	api_tokenapi_oauth_tokenapi_job_tokenr   r   api_password)r   r   r   r   )argument_specmutually_exclusiverequired_togetherrequired_one_ofsupports_check_modert   r    rY   z3.2.0)min_versionzproject '%s' not found.rB   zgroup '%s' not found.)r   r   r   r   rZ   r\   r)   r[   rc   rb   )changedr    milestones_obj)r   updater   ro   r!   r   r
   paramsr	   r   r   rF   r   r   r-   r.   	exit_json)r   r   gitlab_projectgitlab_grouprt   milestone_listrY   r   gitlab_project_idgitlab_group_idrs   ru   raw_return_valuebeforeafterr4   r?   r\   r[   rc   rZ   rm   s                         r   mainr     s   ,.M+-.%%>t<>Vfudf $"&ED"A(,%%(H%)uu%E'+'G	!J y8Y:OP   #[)./_-+,/* 
 ^,
 N 
 !#F& &!]]9-N==)LMM'"E]]<0NMM'"E+FHO %_nE !,?O _"3!:^!KL!8<!GH"&/\k.?AK 5G{TY[ikpGM5O1FfeT 4:(Iqa5j(I%%5g%>?QUU7^?E?'7	'BC!quuW~CGC'7	'BC!quuW~CGC)9+)FGAwGIGeWgQZ[L
VTR )J?CCGs$   	JJ&JJ(J	J__main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.apir   Aansible_collections.community.general.plugins.module_utils.gitlabr   r	   r
   r   r   r   objectr   r`   r   r   rP   rS   r   r   <module>r      s|    A @L^Sj
@ 5 =  M)v M)`%<RQjJSZ zF r   