
    Vh2                         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  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functionag
  
module: gitlab_hook
short_description: Manages GitLab project hooks
description:
  - Adds, updates and removes project hook.
author:
  - Marcus Watkins (@marwatk)
  - Guillaume Martinez (@Lunik)
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:
  project:
    description:
      - ID or Full path of the project in the form of group/name.
    required: true
    type: str
  hook_url:
    description:
      - The URL that you want GitLab to post to, this is used as the primary key for updates and deletion.
    required: true
    type: str
  state:
    description:
      - When V(present) the hook will be updated to match the input or created if it does not exist.
      - When V(absent) hook will be deleted if it exists.
    default: present
    type: str
    choices: ["present", "absent"]
  push_events:
    description:
      - Trigger hook on push events.
    type: bool
    default: true
  push_events_branch_filter:
    description:
      - Branch name of wildcard to trigger hook on push events.
    type: str
    version_added: '0.2.0'
    default: ''
  issues_events:
    description:
      - Trigger hook on issues events.
    type: bool
    default: false
  merge_requests_events:
    description:
      - Trigger hook on merge requests events.
    type: bool
    default: false
  tag_push_events:
    description:
      - Trigger hook on tag push events.
    type: bool
    default: false
  note_events:
    description:
      - Trigger hook on note events or when someone adds a comment.
    type: bool
    default: false
  job_events:
    description:
      - Trigger hook on job events.
    type: bool
    default: false
  pipeline_events:
    description:
      - Trigger hook on pipeline events.
    type: bool
    default: false
  wiki_page_events:
    description:
      - Trigger hook on wiki events.
    type: bool
    default: false
  releases_events:
    description:
      - Trigger hook on release events.
    type: bool
    version_added: '8.4.0'
  hook_validate_certs:
    description:
      - Whether GitLab will do SSL verification when triggering the hook.
    type: bool
    default: false
    aliases: [enable_ssl_verification]
  token:
    description:
      - Secret token to validate hook messages at the receiver.
      - If this is present it will always result in a change as it cannot be retrieved from GitLab.
      - Will show up in the X-GitLab-Token HTTP request header.
    required: false
    type: str
ar  
- name: "Adding a project hook"
  community.general.gitlab_hook:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    project: "my_group/my_project"
    hook_url: "https://my-ci-server.example.com/gitlab-hook"
    state: present
    push_events: true
    tag_push_events: true
    token: "my-super-secret-token-that-my-ci-server-will-check"

- name: "Delete the previous hook"
  community.general.gitlab_hook:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    project: "my_group/my_project"
    hook_url: "https://my-ci-server.example.com/gitlab-hook"
    state: absent

- name: "Delete a hook by numeric project id"
  community.general.gitlab_hook:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    project: 10
    hook_url: "https://my-ci-server.example.com/gitlab-hook"
    state: absent
a  
msg:
  description: Success or failure message.
  returned: always
  type: str
  sample: "Success"

result:
  description: JSON parsed response from the server.
  returned: always
  type: dict

error:
  description: The error message returned by the GitLab API.
  returned: failed
  type: str
  sample: "400: path is already in use"

hook:
  description: API object.
  returned: always
  type: dict
)basic_auth_argument_spec)AnsibleModule)auth_argument_specfind_projectgitlab_authenticationlist_all_kwargsc                   @    e Zd Zd Z	 d Z	 d Z	 d Z	 d Z	 d Zd Z	y)	
GitLabHookc                 .    || _         || _        d | _        y N)_module_gitlabhook_object)selfmodulegitlab_instances      q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_hook.py__init__zGitLabHook.__init__   s    &    c                 `   d}| j                   G| j                  |||d   |d   |d   |d   |d   |d   |d   |d	   |d
   |d   |d   |d   d      }d}nP| j                  | j                   |d   |d   |d   |d   |d   |d   |d   |d	   |d
   |d   |d   |d   d      \  }}|| _         |rI| j                  j                  r | j                  j                  dd|z         	 |j                          |S |S # t        $ r*}| j                  j                  d|z         Y d }~|S d }~ww xY w)NFpush_eventspush_events_branch_filterissues_eventsmerge_requests_eventstag_push_eventsnote_events
job_eventspipeline_eventswiki_page_eventsreleases_eventsenable_ssl_verificationtoken)urlr   r   r   r   r   r   r    r!   r"   r#   r$   r%   Tr   r   r   r   r   r   r    r!   r"   r#   r$   r%   +Successfully created or updated the hook %schangedmsgzFailed to update hook: %s r+   )	r   create_hookupdate_hookr   
check_mode	exit_jsonsave	Exception	fail_json)r   projecthook_urloptionsr*   hookes          r   create_or_update_hookz GitLabHook.create_or_update_hook   s    ###G&}5-45P-Q!(!9)01H)I#*+<#=&}5%l3#*+<#=$+,>$?#*+<#=+23L+M ). D G ,,T-=-=&}5-45P-Q!(!9)01H)I#*+<#=&}5%l3#*+<#=$+,>$?#*+<#=+23L+M )@ MGT  ||&&&&t9fiq9q&rM		 w  M&&+G!+K&LLMs   &C: :	D-D((D-c                 j    | j                   j                  ry|j                  j                  |      }|S )NT)r   r/   hookscreate)r   r4   	argumentsr7   s       r   r-   zGitLabHook.create_hook   s+    <<""}}##I.r   c                     d}|j                         D ](  \  }}|	t        ||d       |k7  st        |||       d}* ||fS )NFT)itemsgetattrsetattr)r   r7   r=   r*   arg_key	arg_values         r   r.   zGitLabHook.update_hook  sV    "+//"3 	#GY$4$/9<D'95"G		# r   c                 t     |j                   j                  di t        D ]  }|j                  |k(  s|c S  y )N )r;   listr   r&   r   r4   r5   r7   s       r   	find_hookzGitLabHook.find_hook  s6    &GMM&&99 	DH$	r   c                 <    | j                  ||      }|r|| _        yy)NTF)rH   r   rG   s       r   exists_hookzGitLabHook.exists_hook  s#    ~~gx0#Dr   c                 f    | j                   j                  s| j                  j                          y y r   )r   r/   r   delete)r   s    r   delete_hookzGitLabHook.delete_hook!  s&    ||&&##% 'r   N)
__name__
__module____qualname__r   r9   r-   r.   rH   rJ   rM   rE   r   r   r   r      s?     
/b	
&r   r   c                     t               } | j                  t                      | j                  t        t        ddddg      t        dd      t        dd      t        dd      t        dd	      t        dd
      t        dd
      t        dd
      t        dd
      t        dd
      t        dd
      t        dd
      t        dd       t        dd
dg      t        dd                   t	        | ddgddgddgddgddggddggg dgd      }t        |      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }|j                  d    }|j                  d!   }|j                  d"   }|j                  d#   }|j                  d$   }t        ||      }t        ||      }||j                  d%|z  &       |j                  ||      }|dk(  r<|r'|j                          |j                  dd'|z  (       n|j                  d
d)(       |dk(  rx|j                  ||||||	|
|||||||d*      r,|j                  dd+|z  |j                  j                  ,       y |j                  d
d-|z  |j                  j                  ,       y y ).Nstrpresentabsent)typedefaultchoicesT)rU   requiredbool)rU   rV    Fr$   )rU   rV   aliases)rU   no_log)stater4   r5   r   r   r   r   r   r   r    r!   r"   r#   hook_validate_certsr%   api_username	api_tokenapi_oauth_tokenapi_job_tokenapi_password)r_   r`   ra   rb   )argument_specmutually_exclusiverequired_togetherrequired_one_ofsupports_check_moder]   r4   r5   r   r   r   r   r   r   r    r!   r"   r#   r^   r%   z0Failed to create hook: project %s doesn't existsr,   zSuccessfully deleted hook %sr)   zHook deleted or does not existsr'   r(   )r*   r+   r7   zNo need to update the hook %s)r   updater   dictr   r
   paramsr   r	   r3   rJ   rM   r0   r9   r   _attrs)rd   r   r   r]   project_identifierr5   r   r   r   r   r   r   r    r!   r"   r#   r$   
hook_tokengitlab_hookr4   hook_existss                        r   mainrq   &  s[   ,.M+-.y8Y:OP%$/540fd3"&E2">6">&%8fe4VU3&%8659&$7 feF_E`ad+ $ #[)./_-+,/*
 ^,
 N
 !F& ,F3OMM'"Ey1}}Z(H--.K &.I JMM/2M"MM*ABmm$56O--.K|,Jmm$56O}}%78mm$56O$mm,ABw'JV_5K?,>?GORdde))'8<K##%T/MPX/XYU0QR	,,Wh&)B*%:.&$. 0.'>A
  T/\_g/gny  oF  oF  oM  oM  NU0ORZ0Zalaxaxaa  A% r   __main__N)
__future__r   r   r   rU   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.apir   ansible.module_utils.basicr   Aansible_collections.community.general.plugins.module_utils.gitlabr   r	   r
   r   objectr   rq   rN   rE   r   r   <module>r|      sg    A @gR:
0 > 4 
o& o&d\A~ zF r   