
    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mZ dZ	 d dlmZmZmZ d dlmZ d	ZddZddZddZddZd Zedk(  r e        yy# e$ r  e	j,                         Zd
ZY ;w xY w)    )absolute_importdivisionprint_functionu  
module: github_repo
short_description: Manage your repositories on Github
version_added: 2.2.0
description:
  - Manages Github repositories using PyGithub library.
  - Authentication can be done with O(access_token) or with O(username) and O(password).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  username:
    description:
      - Username used for authentication.
      - This is only needed when not using O(access_token).
    type: str
    required: false
  password:
    description:
      - Password used for authentication.
      - This is only needed when not using O(access_token).
    type: str
    required: false
  access_token:
    description:
      - Token parameter for authentication.
      - This is only needed when not using O(username) and O(password).
    type: str
    required: false
  name:
    description:
      - Repository name.
    type: str
    required: true
  description:
    description:
      - Description for the repository.
      - Defaults to empty if O(force_defaults=true), which is the default in this module.
      - Defaults to empty if O(force_defaults=false) when creating a new repository.
      - This is only used when O(state) is V(present).
    type: str
    required: false
  private:
    description:
      - Whether the repository should be private or not.
      - Defaults to V(false) if O(force_defaults=true), which is the default in this module.
      - Defaults to V(false) if O(force_defaults=false) when creating a new repository.
      - This is only used when O(state=present).
    type: bool
    required: false
  state:
    description:
      - Whether the repository should exist or not.
    type: str
    default: present
    choices: [absent, present]
    required: false
  organization:
    description:
      - Organization for the repository.
      - When O(state=present), the repository will be created in the current user profile.
    type: str
    required: false
  api_url:
    description:
      - URL to the GitHub API if not using github.com but you own instance.
    type: str
    default: 'https://api.github.com'
    version_added: "3.5.0"
  force_defaults:
    description:
      - Overwrite current O(description) and O(private) attributes with defaults if set to V(true), which currently is the
        default.
      - The default for this option will be deprecated in a future version of this collection, and eventually change to V(false).
    type: bool
    default: true
    required: false
    version_added: 4.1.0
requirements:
  - PyGithub>=1.54
notes:
  - For Python 3, PyGithub>=1.54 should be used.
  - 'For Python 3.5, PyGithub==1.54 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-54-november-30-2020).'
  - 'For Python 2.7, PyGithub==1.45 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-45-december-29-2019).'
author:
  - Álvaro Torres Cogollo (@atorrescogollo)
a  
- name: Create a Github repository
  community.general.github_repo:
    access_token: mytoken
    organization: MyOrganization
    name: myrepo
    description: "Just for fun"
    private: true
    state: present
    force_defaults: false
  register: result

- name: Delete the repository
  community.general.github_repo:
    username: octocat
    password: password
    organization: MyOrganization
    name: myrepo
    state: absent
  register: result
z
repo:
  description: Repository information as JSON. See U(https://docs.github.com/en/rest/reference/repos#get-a-repository).
  returned: success and O(state=present)
  type: dict
N)AnsibleModulemissing_required_lib)GithubGithubExceptionGithubObject)UnknownObjectExceptionTFc                 B    |sy |rt        ||      S t        || |      S )N)base_urllogin_or_token)r   r   password)r   usernamer   access_tokenapi_urls       q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/github_repo.pyauthenticater      s'    w|DDwx(SS    c                    t        dt                     }|r| j                  |      }n| j                         }d }	 |j                  |      }|j                  |d<   i }	|||j                  d   |k7  r||	d<   |||j                  d	   ||xs d fvr||	d	<   |	r`|s |j                  di |	 |d   j                  |s|j                  j                  n||s|j                  j                  n|d
       d|d<   |S # t
        $ rQ |sG|j                  ||t        j                  n||t        j                  n|      }|j                  |d<   d|d<   Y w xY w)NF)changedreponamer   )r   privatedescriptionTr   r   r   )r   r    )dictget_organizationget_userget_reporaw_datar   create_repor
   NotSeteditupdate_privatevalue_description)
ghr   organizationr   r   
check_moderesulttargetr   changess
             r   r$   r$      s~   VF $$\2D!D)v G<4==3w>!(GI<4==7[M`\`?aa%0GM"DII  v2<t}}**':D4,,22+
 	 !yM; " 	!%%/6++G3>3FL//K & D
 "]]F6N y	!s   !C? ?AEEc                     t        d      }|r| j                  |      }n| j                         }	 |j                  |      }|s|j	                          d|d<   |S # t
        $ r Y |S w xY w)NF)r   r   Tr   )r   r    r!   r"   deleter   )r+   r   r,   r-   r.   r/   r   s          r   delete_repor3      su    % F$$\2D)KKM y M " Ms   )A 	A*)A*c                     | d   r| d   xs d| d<   | d   xs d| d<   t        | d   | d   | d   | d	   
      }| d   dk(  rt        || d   | d   |      S t        || d   | d   | d   | d   |      S )Nforce_defaultsr    r   Fr   r   r   r   r   stateabsentr   r,   )r+   r   r,   r-   )r+   r   r,   r   r   r-   )r   r3   r$   )paramsr-   r+   s      r   
run_moduler:      s     &} 5 ;}"9-6y	
#fZ.@vVdOey!
#B g("/!	
 	
 /9%}-!
 	
r   c                     t        t        d      t        dd      t        dd      t        dd      t        dddddg	      t        ddd 
      t        d      t        d      t        ddd
      t        dd      
      } t        | ddgdgdg      }t        s |j                  t	        d      t
               	 t        |j                  |j                        } |j                  di | y # t        $ r4}|j                  dj                  t        |                   Y d }~y d }~wt        $ r4}|j                  dj                  t        |                   Y d }~y d }~ww xY w)Nstr)typeT)r=   no_log)r=   requiredFpresentr8   )r=   r?   defaultchoices)r=   r?   rA   boolzhttps://api.github.com)r=   rA   )
r   r   r   r   r7   r,   r   r   r   r5   )r   r   )r   r   )argument_specsupports_check_moderequired_togetherrequired_one_ofmutually_exclusivePyGithub)msg	exceptionzGithub error. {0})rJ   zUnexpected error. {0}r   )r   r   HAS_GITHUB_PACKAGE	fail_jsonr   GITHUB_IMP_ERRr:   r9   r-   	exit_jsonr	   formatrepr	Exception)module_argsmoduler.   es       r   mainrV      sX   5!5.uT2ut,y%x02uudC&!e$%%9QR6K ! 345689F 1#1 	 	3FFMM6+<+<="6" B077Q@AA F4;;DGDEEFs$   2C4 4	E,=*D,,E,8*E''E,__main__)NNNN)NNNF)NF)F)
__future__r   r   r   r=   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackansible.module_utils.basicr   r   rN   githubr   r	   r
   github.GithubExceptionr   rL   rR   
format_excr   r$   r3   r:   rV   __name__r   r   r   <module>rc      s    A @Zx,
  J<<=T*Z"
4 FF zF U  )Y))+Ns   A A0/A0