
    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 d d	l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_branch
short_description: Create or delete a branch
version_added: 4.2.0
description:
  - This module allows to create or delete branches.
author:
  - paytroff (@paytroff)
requirements:
  - python-gitlab >= 2.3.0
extends_documentation_fragment:
  - community.general.auth_basic
  - community.general.gitlab
  - community.general.attributes

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

options:
  state:
    description:
      - Create or delete branch.
    default: present
    type: str
    choices: ["present", "absent"]
  project:
    description:
      - The path or name of the project.
    required: true
    type: str
  branch:
    description:
      - The name of the branch that needs to be created.
    required: true
    type: str
  ref_branch:
    description:
      - Reference branch to create from.
      - This must be specified if O(state=present).
    type: str
a  
- name: Create branch branch2 from main
  community.general.gitlab_branch:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    branch: branch2
    ref_branch: main
    state: present

- name: Delete branch branch2
  community.general.gitlab_branch:
    api_url: https://gitlab.com
    api_token: secret_access_token
    project: "group1/project1"
    branch: branch2
    state: absent

N)AnsibleModule)basic_auth_argument_spec)LooseVersion)auth_argument_specgitlab_authenticationgitlabc                   *    e Zd Zd Zd Zd Zd Zd Zy)GitlabBranchc                 L    || _         || _        | j                  |      | _        y N)repo_moduleget_projectproject)selfmoduler   gitlab_instances       s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_branch.py__init__zGitlabBranch.__init__[   s"    #	''0    c                 z    	 | j                   j                  j                  |      S # t        $ r
}Y d }~yd }~ww xY wNF)r   projectsget	Exception)r   r   es      r   r   zGitlabBranch.get_project`   s5    	99%%))'22 		   $' 	::c                 z    	 | j                   j                  j                  |      S # t        $ r
}Y d }~yd }~ww xY wr   )r   branchesr   r   )r   branchr    s      r   
get_branchzGitlabBranch.get_branchf   s5    	<<((,,V44 		r!   c                 R    | j                   j                  j                  ||d      S )N)r$   ref)r   r#   create)r   r$   
ref_branchs      r   create_branchzGitlabBranch.create_branchl   s"    ||$$++vj,QRRr   c                 "    |j                         S r   )delete)r   r$   s     r   delete_branchzGitlabBranch.delete_brancho   s    }}r   N)__name__
__module____qualname__r   r   r%   r*   r-    r   r   r   r   Y   s    1
Sr   r   c                     t               } | j                  t                      | j                  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gd	dggg dgdddgdggd      }t        |      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        j                  }t        |      t        d      k  r|j                  d|z         t        |||      }|j                  |      }	|	sp|dk(  rk|j                  |      }
|
s"|j                  dj                  |             |j                  ||       |j                  ddj                  |             y |	r)|dk(  r$|j                  ddj                  |             y |	r;|dk(  r6	 |j!                  |	       |j                  ddj                  |             y |j                  dd        y # t"        $ r/}|j                  dt%        j&                                Y d }~y d }~ww xY w)!NstrT)typerequiredFpresentabsent)r4   defaultchoices)r   r$   r)   stateapi_username	api_tokenapi_oauth_tokenapi_job_tokenapi_password)r;   r<   r=   r>   r:   r)   )argument_specmutually_exclusiverequired_togetherrequired_one_ofrequired_ifsupports_check_moder   r$   z2.3.0zcommunity.general.gitlab_proteched_branch requires python-gitlab Python module >= 2.3.0 (installed version: [%s]). Please upgrade python-gitlab to version 2.3.0 or above.)msg)r   r   r   zRef branch {b} not exist.)bzCreated the branch {b}.)changedrF   zBranch {b} already existzBranch {b} deleted.zError delete branch.)rF   	exceptionzNo changes are needed.)r   updater
   dictr   r   paramsr   __version__r	   	fail_jsonr   r%   formatr*   	exit_jsonr-   r   	traceback
format_exc)r@   r   r   r   r$   r)   r:   gitlab_versionthis_gitlabthis_branchr_branchr    s               r   mainrW   s   s   ,.M+-.%$/.UU3y8Y:OP	   #[)./_-+,/*
 ^,
 N
 i,6
 "%F, ,F3OmmI&G]]8$F|,JMM'"E''NN#l7&;; XZhi 	j fg_K((0K5I-))*5!<!C!Cj!C!QR!!&*5+D+K+Kf+K+UV	)+,F,M,MPV,M,WX	(*	[%%k2T/D/K/Kf/K/UV 	,DE  	[!79CWCWCYZZ	[s   4H$ $	I-%II__main__)
__future__r   r   r   r4   __metaclass__DOCUMENTATIONEXAMPLESRETURNrQ   ansible.module_utils.basicr   ansible.module_utils.apir   Bansible_collections.community.general.plugins.module_utils.versionr	   Aansible_collections.community.general.plugins.module_utils.gitlabr
   r   r   objectr   rW   r.   r1   r   r   <module>rc      sh    A @+\&
  4 = [ 
6 4?FD zF r   