HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/github.cpython-310.pyc
o

�N�g���@stdZddlZddlZddlmZdZzddlZddlZddl	Zddl
mZdZWn	ey1Ynwe�
e�ZdZdd	�Zd
d�Zdd
�Zd`dd�Zdadd�Zdbdd�Zdbdd�Zdcdd�Zdcdd�Zdddd�Z	dedd�Z									 	!			dfd"d#�Z				$	%		dgd&d'�Z	dhd(d)�Zd*d+�Zdbd,d-�Z dcd.d/�Z!dcd0d1�Z"dcd2d3�Z#dcd4d5�Z$										did6d7�Z%							djd8d9�Z&dcd:d;�Z'dcd<d=�Z(					dkd>d?�Z)dld@dA�Z*dcdBdC�Z+dbdDdE�Z,dmdFdG�Z-dcdHdI�Z.dbdJdK�Z/dbdLdM�Z0dcdNdO�Z1dcdPdQ�Z2dcdRdS�Z3dbdTdU�Z4						 	!		dndVdW�Z5dXdY�Z6dZd[�Z7				\			]	dod^d_�Z8dS)pa
Module for interacting with the GitHub v3 API.

.. versionadded:: 2016.3.0

:depends: PyGithub python module

Configuration
-------------

Configure this module by specifying the name of a configuration
profile in the minion config, minion pillar, or master config. The module
will use the 'github' key by default, if defined.

For example:

.. code-block:: yaml

    github:
      token: abc1234
      org_name: my_organization

      # optional: some functions require a repo_name, which
      # can be set in the config file, or passed in at the CLI.
      repo_name: my_repo

      # optional: it can be dangerous to change the privacy of a repository
      # in an automated way. set this to True to allow privacy modifications
      allow_repo_privacy_changes: False
�N)�CommandExecutionErrorF)�UnknownObjectExceptionT�githubcCstrtSdS)zH
    Only load this module if PyGithub is installed on this minion.
    )FzPThe github execution module cannot be loaded: PyGithub library is not installed.)�HAS_LIBS�__virtualname__�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/github.py�__virtual__7sr	cCsDtd|�}|std�|���|�|�}|dur td�||���|S)a!
    Helper function that returns a profile's configuration value based on
    the supplied configuration name.

    profile
        The profile name that contains configuration information.

    config_name
        The configuration item's name to use to return configuration values.
    z
config.optionzCAuthentication information could not be found for the '{}' profile.Nz5The '{}' parameter was not found in the '{}' profile.)Z__salt__r�format�get)�profileZconfig_nameZconfigZconfig_valuerrr�_get_config_valueDs�
��r
cCs>t|d�}d�|t|d��}|tvrtj|dd�t|<t|S)zK
    Return the GitHub client, cached into __context__ for performance
    �tokenzgithub.{}:{}�org_name�d)�per_page)r
r
�__context__rZGithub)rr�keyrrr�_get_clientas

rcCstj�tjj|j|jd|�S)Nz/members)r�
PaginatedListZ	NamedUser�
_requester�url)�organization�paramsrrr�_get_membersms�rcCs�t|d�}d|�d�}|tvs|s|durZt|d�}t|�}|�|�}tj�tjj|j|jd|�}|dur8|Sg}|D]}	|�	|	�d|�d|	j
���d�}
t|	�t|
<q<|t|<t|S)Nr�github.z:repos�/repos�:z
:repo_info)
r
rr�get_organizationrrZ
Repositoryrr�append�name�lower�
_repo_to_dict)rr�ignore_cacherr�clientr�resultZnext_result�repoZrepo_keyrrr�
_get_reposvs*


�
r'cCsTt|d�}d|�d�}|tvs|r&t|�}|�|�}dd�t|d�D�t|<t|S)a�
    List all users within the organization.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    ignore_cache
        Bypasses the use of cached users.

        .. versionadded:: 2016.11.0

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_users
        salt myminion github.list_users profile='my-github-profile'
    rrz:userscS�g|]}|j�qSr)�login��.0�memberrrr�
<listcomp>��zlist_users.<locals>.<listcomp>N)r
rrrr)rr#rrr$rrrr�
list_users�s

r/c	Cs"|s
|t|�vr
dSi}t|�}|�t|d��}z|�|�}Wnty.t�d�YdSw|j|d<|j	|d<|j
|d<|j|d<|j|d	<|j
|d
<|j|d<|j|d<|j|d
<z|j�d|jd|j�\}}Wnty�d|d<d|d<|YSw|�|�|d<|�d�|d<|S)a�
    Get a GitHub user by name.

    name
        The user for which to obtain information.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    user_details
        Prints user information details. Defaults to ``False``. If the user is
        already in the organization and user_details is set to False, the
        get_user function returns ``True``. If the user is not already present
        in the organization, user details will be printed by default.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_user github-handle
        salt myminion github.get_user github-handle user_details=true

    Tr�Resource not foundF�company�
created_at�email�html_url�idr)r �typer�GET�
/memberships/ZnonexistentZmembership_stateZin_org�state)r/rrr
�get_userr�log�	exceptionr1r2r3r4r5r)r r6rr�requestJsonAndCheck�	_identity�has_in_membersr)	r rZuser_details�responser$r�user�headers�datarrrr:�s@
�








��r:cCsrt|�}|�t|d��}z|�|�}Wnty"t�d�YdSw|j�d|j	d|j
�\}}|�d�dkS)a
    Add a GitHub user.

    name
        The user for which to obtain information.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_user github-handle
    rr0F�PUTr8r9�pending)rrr
r:rr;r<rr=rr>r)r rr$rZgithub_named_userrBrCrrr�add_user�s
��rFcCsft|�}|�t|d��}z|�|�}Wnty"t�d�YdSw|�|�r-|�|�|�|�S)a"
    Remove a Github user by name.

    name
        The user for which to obtain information.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.remove_user github-handle
    rr0F)	rrr
r:rr;r<r?�remove_from_members)r rr$rZgit_userrrr�remove_users
�

rH�minc
Csxt|d�}|durt|d�}d�d||g�}dt|�}i}t|||d�}|�d�}	|d	kr4|||	<|St|�||	<|S)
aU
    Return information about a single issue in a named repository.

    .. versionadded:: 2016.11.0

    issue_number
        The number of the issue to retrieve.

    repo_name
        The name of the repository from which to get the issue. This argument is
        required, either passed via the CLI, or defined in the configured
        profile. A ``repo_name`` passed as a CLI argument will override the
        repo_name defined in the configured profile, if provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    output
        The amount of data returned by each issue. Defaults to ``min``. Change
        to ``full`` to see all issue output.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_issue 514
        salt myminion github.get_issue 514 repo_name=salt
    rN�	repo_name�/�reposzissues/��action�commandr5�full)r
�join�str�_queryr�
_format_issue)
�issue_numberrJr�outputrrNrO�retZ
issue_data�issue_idrrr�	get_issue6s


�rYc
Cs�t|d�}|durt|d�}d�d||g�}d�dt|�dg�}i}|r(||d<t||||d	�}	i}
|	D](}|�d
�}|dkrD||
|<q4|�d
�|�d�|�d
�|�d��d�d�|
|<q4|
S)a
    Return information about the comments for a given issue in a named repository.

    .. versionadded:: 2016.11.0

    issue_number
        The number of the issue for which to retrieve comments.

    repo_name
        The name of the repository to which the issue belongs. This argument is
        required, either passed via the CLI, or defined in the configured
        profile. A ``repo_name`` passed as a CLI argument will override the
        repo_name defined in the configured profile, if provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    since
        Only comments updated at or after this time are returned. This is a
        timestamp in ISO 8601 format: ``YYYY-MM-DDTHH:MM:SSZ``.

    output
        The amount of data returned by each issue. Defaults to ``min``. Change
        to ``full`` to see all issue output.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_issue_comments 514
        salt myminion github.get_issue 514 repo_name=salt
    rNrJrKrL�issues�comments�since�rNrO�argsr5rPr2�
updated_atrAr))r5r2r_Z
user_login)r
rQrRrSr)
rUrJrr\rVrrNrOr^r[rWZcommentZ
comment_idrrr�get_issue_commentsfs(
#


�r`�open�created�descc
Cst|d�}
|durt|d�}d�d|
|g�}i}|r||d<|r$||d<|r*||d<|r0||d	<|r6||d
<|
r<|
|d<|rB||d<|rL|d
krL||d<|rV|dkrV||d<|	r`|	dkr`|	|d<i}t||d|d�}|D]}|�d�rtql|�d�}|dkr�|||<qlt|�||<ql|S)a+	
    Returns information for all issues in a given repository, based on the search options.

    .. versionadded:: 2016.11.0

    repo_name
        The name of the repository for which to list issues. This argument is
        required, either passed via the CLI, or defined in the configured
        profile. A ``repo_name`` passed as a CLI argument will override the
        repo_name defined in the configured profile, if provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    milestone
        The number of a GitHub milestone, or a string of either ``*`` or
        ``none``.

        If a number is passed, it should refer to a milestone by its number
        field. Use the ``github.get_milestone`` function to obtain a milestone's
        number.

        If the string ``*`` is passed, issues with any milestone are
        accepted. If the string ``none`` is passed, issues without milestones
        are returned.

    state
        Indicates the state of the issues to return. Can be either ``open``,
        ``closed``, or ``all``. Default is ``open``.

    assignee
        Can be the name of a user. Pass in ``none`` (as a string) for issues
        with no assigned user or ``*`` for issues assigned to any user.

    creator
        The user that created the issue.

    mentioned
        A user that's mentioned in the issue.

    labels
        A string of comma separated label names. For example, ``bug,ui,@high``.

    sort
        What to sort results by. Can be either ``created``, ``updated``, or
        ``comments``. Default is ``created``.

    direction
        The direction of the sort. Can be either ``asc`` or ``desc``. Default
        is ``desc``.

    since
        Only issues updated at or after this time are returned. This is a
        timestamp in ISO 8601 format: ``YYYY-MM-DDTHH:MM:SSZ``.

    output
        The amount of data returned by each issue. Defaults to ``min``. Change
        to ``full`` to see all issue output.

    per_page
        GitHub paginates data in their API calls. Use this value to increase or
        decrease the number of issues gathered from GitHub, per page. If not set,
        GitHub defaults are used. Maximum is 100.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_issues my-github-repo
    rNrJrKrL�	milestone�assignee�creator�	mentioned�labelsr\rrar9rb�sortrc�	directionrZr]Zpull_requestr5rP)r
rQrSrrT)rJrrdr9rerfrgrhrirjr\rVrrrNr^rWrZ�issuerXrrr�
get_issues�sF
U



rl�due_on�asccCs�t|d�}|durt|d�}d�d||g�}i}	|r||	d<|r(|dkr(||	d<|r2|d	kr2||	d
<|r<|dkr<||	d<i}
t||d
|	d�}|D]#}|�d�}
|dkrX||
|
<qH|�d�|�d�|�d�||
|
<qH|
S)a�
    Return information about milestones for a given repository.

    .. versionadded:: 2016.11.0

    repo_name
        The name of the repository for which to list issues. This argument is
        required, either passed via the CLI, or defined in the configured
        profile. A ``repo_name`` passed as a CLI argument will override the
        repo_name defined in the configured profile, if provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    state
        The state of the milestone. Either ``open``, ``closed``, or ``all``.
        Default is ``open``.

    sort
        What to sort results by. Either ``due_on`` or ``completeness``. Default
        is ``due_on``.

    direction
        The direction of the sort. Either ``asc`` or ``desc``. Default is ``asc``.

    output
        The amount of data returned by each issue. Defaults to ``min``. Change
        to ``full`` to see all issue output.

    per_page
        GitHub paginates data in their API calls. Use this value to increase or
        decrease the number of issues gathered from GitHub, per page. If not set,
        GitHub defaults are used.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_milestones

    rNrJrKrLrrar9rmrirnrj�
milestonesr]r5rPrfr4�
labels_url)r
rQrSr�pop)rJrr9rirjrVrrrNr^rWrord�milestone_idrrr�get_milestones*s0
2






rscCs�i}t||g�std��t|d�}|durt|d�}d�d||g�}|rUdt|�}t|||d�}	|	�d	�}
|d
kr@|	||
<|S|	�d�|	�d�|	�d
�|	||
<|St|||d�}|�	�D]\}}
|
�d�|krs|
||<|Sq`|S)a�
    Return information about a single milestone in a named repository.

    .. versionadded:: 2016.11.0

    number
        The number of the milestone to retrieve. If provided, this option
        will be favored over ``name``.

    name
        The name of the milestone to retrieve.

    repo_name
        The name of the repository for which to list issues. This argument is
        required, either passed via the CLI, or defined in the configured
        profile. A ``repo_name`` passed as a CLI argument will override the
        repo_name defined in the configured profile, if provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    output
        The amount of data returned by each issue. Defaults to ``min``. Change
        to ``full`` to see all issue output.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_milestone 72
        salt myminion github.get_milestone name=my_milestone

    z7Either a milestone 'name' or 'number' must be provided.rNrJrKrLzmilestones/rMr5rPrfr4rp)rJrrV�title)
�anyrr
rQrRrSrrqrs�items)�numberr rJrrVrWrrNrOZmilestone_datarrror�valrrr�
get_milestone~s8$�



�

�rycCs�i}|j|d<|j|d<|j|d<|jj|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<|j|d<|j|d<|j|d<|S)Nr5r �	full_name�owner�privater4�description�fork�homepage�size�stargazers_count�watchers_count�language�open_issues_count�forks�open_issues�watchers�default_branch�
has_issues�has_wiki�
has_downloads)r5r rzr{r)r|r4r}r~rr�r�r�r�r�r�r�r�r�r�r�r�)r&rWrrrr"�s.



















r"cCs�t|d�}d�t|d�|���}|tvs|rGt|�}z|�d�||g��}|s*iWSt|�}|t|<Wt|Stj	yFt
d�||���wt|S)a�
    Return information for a given repo.

    .. versionadded:: 2016.11.0

    repo_name
        The name of the repository.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_repo_info salt
        salt myminion github.get_repo_info salt profile='my-github-profile'
    rzgithub.{}:{}:repo_inforK�CThe '{}' repository under the '{}' organization could not be found.)r
r
r!rr�get_reporQr"rrr)rJrr#rrr$r&rWrrr�
get_repo_info�s(
�
���r�cCs�g}t|d�}t|�}z|�d�||g��}Wntjy'td�||���wz|��}|D]}|�	|j
|j|jd��q/W|StjyPtd�||���w)a�
    Return teams belonging to a repository.

    .. versionadded:: 2017.7.0

    repo_name
        The name of the repository from which to retrieve teams.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_repo_teams salt
        salt myminion github.get_repo_teams salt profile='my-github-profile'
    rrKr�)r5r �
permissionzIUnable to retrieve teams for repository '{}' under the '{}' organization.)
r
rr�rQrrrr
�	get_teamsrr5r r�)rJrrWrr$r&�teams�teamrrr�get_repo_teamss2
����	���r�cCsdd�t|�D�S)a�
    List all repositories within the organization. Includes public and private
    repositories within the organization Dependent upon the access rights of
    the profile token.

    .. versionadded:: 2016.11.0

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_repos
        salt myminion github.list_repos profile='my-github-profile'
    cSr(r)r )r+r&rrrr-Lr.zlist_repos.<locals>.<listcomp>)r'�rrrr�
list_repos:sr�cC�,g}t|�D]
}|jdur|�|j�q|S)a�
    List private repositories within the organization. Dependent upon the access
    rights of the profile token.

    .. versionadded:: 2016.11.0

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_private_repos
        salt myminion github.list_private_repos profile='my-github-profile'
    T�r'r|rr �rrLr&rrr�list_private_reposOs
�r�cCr�)a^
    List public repositories within the organization.

    .. versionadded:: 2016.11.0

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_public_repos
        salt myminion github.list_public_repos profile='my-github-profile'
    Fr�r�rrr�list_public_reposgs
�r�c
Cs�z<t|
�}|�t|
d��}|||||||||	d�	}
d|i}|
��D]\}}|dur-|||<q!|jjd|jd|d�WdStjyLt	�
d	�Yd
Sw)a$
    Create a new github repository.

    name
        The name of the team to be created.

    description
        The description of the repository.

    homepage
        The URL with more information about the repository.

    private
        The visiblity of the repository. Note that private repositories require
        a paid GitHub account.

    has_issues
        Whether to enable issues for this repository.

    has_wiki
        Whether to enable the wiki for this repository.

    has_downloads
        Whether to enable downloads for this repository.

    auto_init
        Whether to create an initial commit with an empty README.

    gitignore_template
        The desired language or platform for a .gitignore, e.g "Haskell".

    license_template
        The desired LICENSE template to apply, e.g "mit" or "mozilla".

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_repo 'repo_name'

    .. versionadded:: 2016.11.0
    r)	r}rr|r�r�r��	auto_init�gitignore_template�license_templater N�POSTr��inputTzError creating a repoF)rrr
rvrr=rr�GithubExceptionr;r<)r r}rr|r�r�r�r�r�r�rr$r�given_params�
parameters�
param_name�param_valuerrr�add_repo~s4:���
�r�cCs�zt|d�}Wntyd}Ynw|dur |s td�|���zCt|�}	|	�t|d��}
|
�|�}||||||d�}d|i}
|��D]\}}|durO||
|<qC|
jjd|j	|
d	�t
||d
d�Wd
Stjyst
�d�YdSw)
a
    Updates an existing Github repository.

    name
        The name of the team to be created.

    description
        The description of the repository.

    homepage
        The URL with more information about the repository.

    private
        The visiblity of the repository. Note that private repositories require
        a paid GitHub account.

    has_issues
        Whether to enable issues for this repository.

    has_wiki
        Whether to enable the wiki for this repository.

    has_downloads
        Whether to enable downloads for this repository.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_repo 'repo_name'

    .. versionadded:: 2016.11.0
    Zallow_repo_privacy_changesFNzaThe private field is set to be changed for repo {} but allow_repo_privacy_changes disallows this.r)r}rr|r�r�r�r �PATCHr�T�rr#zError editing a repo)r
rr
rrr�rvrr=rr�rr�r;r<)r r}rr|r�r�r�rZallow_private_changer$rr&r�r�r�r�rrr�	edit_repo�sB/��
��
�r�cCs~t||d�}|st�d|�dSzt|�}|�t|d��}|�|�}|��t|dd�WdSt	j
y>t�d�YdSw)a=
    Remove a Github repository.

    name
        The name of the repository to be removed.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.remove_repo 'my-repo'

    .. versionadded:: 2016.11.0
    r�z%Repo %s to be removed does not exist.FrTr�zError deleting a repo)r�r;�errorrrr
r��deleter'rr�r<)r rZ	repo_infor$rr&rrr�remove_repo)s

�r�cCst|��|�S)aZ
    Returns the team details if a team with the given name exists, or None
    otherwise.

    name
        The team name for which to obtain information.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_team 'team_name'
    )�
list_teamsr)r rrrr�get_teamKsr�c	Cs�zFt|�}|�t|d��}i}||d<|dur||d<|dur#||d<|dur+||d<|dur3||d<|jjd|jd	|d
�tdd�WdStjyVt	�
d
�YdSw)a�
    Create a new Github team within an organization.

    name
        The name of the team to be created.

    description
        The description of the team.

    repo_names
        The names of repositories to add the team to.

    privacy
        The level of privacy for the team, can be 'secret' or 'closed'.

    permission
        The default permission for new repositories added to the team, can be
        'pull', 'push' or 'admin'.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_team 'team_name'

    .. versionadded:: 2016.11.0
    rr Nr}�
repo_namesr��privacyr�z/teamsr�T)r#zError creating a teamF)rrr
rr=rr�rr�r;r<)	r r}r�r�r�rr$rr�rrr�add_team_s,&�

�r�c	Cs�t||d�}|st�d|�dSzBt|�}|�t|d��}|�|d�}i}|dur.||d<|dur6||d<|dur>||d	<|durF||d
<|jjd|j|d�Wd
St	yat�
d�YdSw)af
    Updates an existing Github team.

    name
        The name of the team to be edited.

    description
        The description of the team.

    privacy
        The level of privacy for the team, can be 'secret' or 'closed'.

    permission
        The default permission for new repositories added to the team, can be
        'pull', 'push' or 'admin'.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.edit_team 'team_name' description='Team description'

    .. versionadded:: 2016.11.0
    r��Team %s does not existFrr5Nr r}r�r�r�r�Tr0)r�r;r�rrr
rr=rrr<)	r r}r�r�rr�r$rr�rrr�	edit_team�s.
�r�cCs�t||d�}|st�d|�dSz#t|�}|�t|d��}|�|d�}|��td|d��|�duWSt	j
yCt�d	�YdSw)
a3
    Remove a github team.

    name
        The name of the team to be removed.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.remove_team 'team_name'

    .. versionadded:: 2016.11.0
    r�z%Team %s to be removed does not exist.Frr5T)r#rNzError deleting a team)r�r;r�rrr
r�r�rrr�r<)r rZ	team_infor$rr�rrr�remove_team�s
�r�c
Cs�t||d�}|st�d|�dS|�d�r|s|�d�Szt|�}|�t|d��}|�|d�}WntyBt�d|d�Ynwz(i}|�	�D]}d}	|j
jrUd	}	n|j
jr[d
}	d|	i||j
��<qJ||d<|WSty~t�d|d�gYSw)a�
    Gets the repo details for a given team as a dict from repo_name to repo details.
    Note that repo names are always in lower case.

    team_name
        The name of the team from which to list repos.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    ignore_cache
        Bypasses the use of cached team repos.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_team_repos 'team_name'

    .. versionadded:: 2016.11.0
    r��Team %s does not exist.FrLrr5�Resource not found: %sZpull�admin�pushr�)r�r;r�rrrr
rr<Z	get_reposZpermissionsr�r�r r!)
�	team_namerr#�cached_teamr$rr�rLr&r�rrr�list_team_repos�s8
��r�cCs�t||d�}|st�d|�dSzt|�}|�t|d��}|�|d�}|�|�}Wnty<t�d|d�YdSwd}|durGd|i}|j	j
d	|jd
|j|d�\}	}
t
||dd
�dS)a�
    Adds a repository to a team with team_name.

    repo_name
        The name of the repository to add.

    team_name
        The name of the team of which to add the repository.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    permission
        The permission for team members within the repository, can be 'pull',
        'push' or 'admin'. If not specified, the default permission specified on
        the team will be used.

        .. versionadded:: 2017.7.0

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_team_repo 'my_repo' 'team_name'

    .. versionadded:: 2016.11.0
    r�r�Frr5r�Nr�rDz/repos/r�Tr�)r�r;r�rrr
r�rr<rr=rr>r�)rJr�rr�r�r$rr&rrBrCrrr�
add_team_repo+s*�
�r�cCs�t||d�}|st�d|�dSzt|�}|�t|d��}|�|d�}|�|�}Wnty<t�d|d�YdSw|�	|�|t
||dd�vS)	a�
    Removes a repository from a team with team_name.

    repo_name
        The name of the repository to remove.

    team_name
        The name of the team of which to remove the repository.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.remove_team_repo 'my_repo' 'team_name'

    .. versionadded:: 2016.11.0
    r�r�Frr5r�Tr�)r�r;r�rrr
r�rr<Zremove_from_reposr�)rJr�rr�r$rr&rrr�remove_team_repo_s"�
�r�cCs�t||d�}|st�d|�dS|�d�r|s|�d�Szt|�}|�t|d��}|�|d�}WntyBt�d|d�Ynwzdd	�|�	�D�|d<|dWStyft�d|d�gYSw)
a�
    Gets the names of team members in lower case.

    team_name
        The name of the team from which to list members.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    ignore_cache
        Bypasses the use of cached team members.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_team_members 'team_name'

    .. versionadded:: 2016.11.0
    r�r�F�membersrr5r�cS�g|]}|j���qSr�r)r!r*rrrr-�sz%list_team_members.<locals>.<listcomp>)
r�r;r�rrrr
rr<Zget_members)r�rr#r�r$rr�rrr�list_team_members�s(
�
�r�cCsnd�t|d��}|tvs|r3t|�}|�t|d��}d}ttjjd�r%d}dd�t||di�D�t|<t|S)	aa
    List all members (in lower case) without MFA turned on.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    ignore_cache
        Bypasses the use of cached team repos.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_members_without_mfa

    .. versionadded:: 2016.11.0
    zgithub.{}:non_mfa_usersr�filterZ
membershipZfilter_cSr�rr�)r+�mrrrr-�s��z,list_members_without_mfa.<locals>.<listcomp>Z2fa_disabled)	r
r
rrr�hasattrrZTeamr)rr#rr$rZ
filter_keyrrr�list_members_without_mfa�s
�r�cCs|��t||d�vS)a�
    Returns True if the github user is in the team with team_name, or False
    otherwise.

    name
        The name of the user whose membership to check.

    team_name
        The name of the team to check membership in.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.is_team_member 'user_name' 'team_name'

    .. versionadded:: 2016.11.0
    r�)r!r�)r r�rrrr�is_team_member�sr�c	Cs�t||d�}|st�d|�dSzt|�}|�t|d��}|�|d�}|�|�}Wnty<t�d|d�YdSwz|j	j
d|jd|jd	d
id	d
id�\}}Wd
St
jyft�d�YdSw)a�
    Adds a team member to a team with team_name.

    name
        The name of the team member to add.

    team_name
        The name of the team of which to add the user.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.add_team_member 'user_name' 'team_name'

    .. versionadded:: 2016.11.0
    r�r�Frr5r�rDr8Zroler,)r�r�z"Error in adding a member to a teamT)r�r;r�rrr
r:rr<rr=rr>rr�)	r r�rr�r$rr,rBrCrrr�add_team_member�s2��	�
�r�cCs�t||d�}|st�d|�dSzt|�}|�t|d��}|�|d�}|�|�}Wnty<t�d|d�YdSwt	|d�sDdS|�
|�|�|�S)	a�
    Removes a team member from a team with team_name.

    name
        The name of the team member to remove.

    team_name
        The name of the team from which to remove the user.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    CLI Example:

    .. code-block:: bash

        salt myminion github.remove_team_member 'user_name' 'team_name'

    .. versionadded:: 2016.11.0
    r�r�Frr5r�rG)FzKPyGithub 1.26.0 or greater is required for team management, please upgrade.)r�r;r�rrr
r:rr<r�rGr?)r r�rr�r$rr,rrr�remove_team_member s"�

r�cCs�d�t|d��}|tvs|r=t|�}|�t|d��}|��}i}|D]}|j|j|jd|j	|jdd�||j
<q"|t|<t|S)a=
    Lists all teams with the organization.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    ignore_cache
        Bypasses the use of cached teams.

    CLI Example:

    .. code-block:: bash

        salt myminion github.list_teams

    .. versionadded:: 2016.11.0
    zgithub.{}:teamsrr}r�)r5�slugr}r�r�)r
r
rrrr�r5r�Z_rawDatar�r )rr#rr$rZ
teams_datar�r�rrrr�Ns�r�c	Cs�t|d�}	|durt|d�}d�d|	|g�}
i}|r||d<|r$||d<|r*||d<|r4|d	kr4||d
<|r>|dkr>||d<|rH|d
krH||d<i}t||
d|d�}
|
D]}|�d�}|dkrd|||<qTt|�||<qT|S)aN
    Returns information for all pull requests in a given repository, based on
    the search options provided.

    .. versionadded:: 2017.7.0

    repo_name
        The name of the repository for which to list pull requests. This
        argument is required, either passed via the CLI, or defined in the
        configured profile. A ``repo_name`` passed as a CLI argument will
        override the ``repo_name`` defined in the configured profile, if
        provided.

    profile
        The name of the profile configuration to use. Defaults to ``github``.

    state
        Indicates the state of the pull requests to return. Can be either
        ``open``, ``closed``, or ``all``. Default is ``open``.

    head
        Filter pull requests by head user and branch name in the format of
        ``user:ref-name``. Example: ``'github:new-script-format'``. Default
        is ``None``.

    base
        Filter pulls by base branch name. Example: ``gh-pages``. Default is
        ``None``.

    sort
        What to sort results by. Can be either ``created``, ``updated``,
        ``popularity`` (comment count), or ``long-running`` (age, filtering
        by pull requests updated within the last month). Default is ``created``.

    direction
        The direction of the sort. Can be either ``asc`` or ``desc``. Default
        is ``desc``.

    output
        The amount of data returned by each pull request. Defaults to ``min``.
        Change to ``full`` to see all pull request output.

    per_page
        GitHub paginates data in their API calls. Use this value to increase or
        decrease the number of pull requests gathered from GitHub, per page. If
        not set, GitHub defaults are used. Maximum is 100.

    CLI Example:

    .. code-block:: bash

        salt myminion github.get_prs
        salt myminion github.get_prs base=2016.11
    rNrJrKrL�head�baserrar9rbrircrjZpullsr]r5rP)r
rQrSr�
_format_pr)rJrr9r�r�rirjrVrrrNr^rWZprs�pr_Zpr_idrrr�get_prsxs2
A


r�c	CsN|�d�|�d�|�d�|�d�|�d��d�|�d�|�d��d	�d
�}|S)z�
    Helper function to format API return information into a more manageable
    and useful dictionary for pull request information.

    pr_
        The pull request to format.
    r5rwr9rtrAr)r4r��ref)r5Z	pr_numberr9rtrAr4Zbase_branch)r)r�rWrrrr��s	�
r�cCs�|�d�|�d�|�d�|�d�|�d��d�|�d�d�}|�d	�}|r*|�d�}|�d
�}g}|D]
}|�|�d��q3|�d�}|rJ|�d�}||d	<||d
<||d<|S)
z�
    Helper function to format API return information into a more manageable
    and useful dictionary for issue information.

    issue
        The issue to format.
    r5rwr9rtrAr)r4)r5rUr9rtrAr4rerhr rd)rr)rkrWrerhZlabel_namesZlabelrdrrrrT�s*	�
	



rTr7�https://api.github.com/c	Cs�t|t�si}|r
||7}|r|d|��7}t�d|�d|��vr)t|d�|d<|r5d|��vr5||d<|dur;i}|dkrCd|d	<d
}	|dkrKd}	g}
d
}d
}|d
ur�|r[||d<tjjj	||||||	dd
d
d
dgt
d�}
t�d|
d�|
ddkr�t|
dt�r�|
dS|
|
d}
n
td�|
�
d����z|
�
d��
d��d�d}Wnty�d}YqQwd|vr�|�d�d�d�d}nd}|d
usU|
S)zL
    Make a web call to the GitHub API and deal with paginated results.
    rKzGitHub URL: %sZaccess_tokenrrNr�zapplication/jsonZAcceptTZDELETEF�Zpage�json)
rrC�header_dict�decodeZdecode_typerB�status�textZhide_fields�optszGitHub Response Status Code: %sr����dictzGitHub Response Error: {}r�rBZLink�,r�next�>z&page=�)�
isinstancer�r;�debug�keysr
�salt�utils�http�queryZ__opts__rr
r�split�AttributeError)rrNrOr^�methodr�rCrrr�Zcomplete_resultZ	next_pageZpage_numberr%Z	link_inforrrrSsr
����.rS)N)NF)rF)r)NrrI)NrNrI)
NrNraNNNNrbrcNrIN)NrrarmrnrIN)NNNrrI)
NNNNNNNNNr)NNNNNNr)NNNNr)NNNr)rN)	NrraNNrbrcrIN)NNNr7NNr�N)9�__doc__�loggingZsalt.utils.httpr�Zsalt.exceptionsrrrZgithub.NamedUserZgithub.PaginatedListZgithub.GithubExceptionr�ImportError�	getLogger�__name__r;rr	r
rrr'r/r:rFrHrYr`rlrsryr"r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rTrSrrrr�<module>s��



	
#

@
!
 1
�@
�
�U
�G

/
,


�X
�
U
"
�
?
6
!
6
4
'
+
%

1
.+
�e'�