o
    ȳgwJ                     @  sx   d Z ddlmZ ddlZddlmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZ er2ddlmZ G dd	 d	eZdS )
zUtil that calls gitlab.    )annotationsN)TYPE_CHECKINGAnyDictListOptional)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)Issuec                   @  sJ  e Zd ZU dZdZded< dZded< dZded< 	 dZded< 	 dZ	ded	< 	 dZ
ded
< 	 dZded< 	 eddZeddedLddZdMddZdNddZdOd d!ZdPd#d$ZdQd&d'ZdRd)d*ZdSd,d-ZdRd.d/ZdSd0d1ZdNd2d3ZdNd4d5ZdTd7d8ZdUdVd;d<ZdUdWd>d?ZdXdAdBZdNdCdDZ dYdFdGZ!dZdJdKZ"dS )[GitLabAPIWrapperzWrapper for GitLab API.Nr   gitlabgitlab_repo_instancezOptional[str]
gitlab_urlgitlab_repositorygitlab_personal_access_tokengitlab_branchgitlab_base_branchforbid)extrabefore)modevaluesr   returnc           	      C  s   t |dddd}t |dd}t |dd}t |d	d
dd}t |dddd}zddl}W n ty5   tdw |j||dd}|  ||d< |j||d< ||d< ||d< ||d< ||d	< ||d< |S )z?Validate that api key and python package exists in environment.r   
GITLAB_URLzhttps://gitlab.com)defaultr   GITLAB_REPOSITORYr   GITLAB_PERSONAL_ACCESS_TOKENr   GITLAB_BRANCHmainr   GITLAB_BASE_BRANCHr   NzRpython-gitlab is not installed. Please install it with `pip install python-gitlab`T)urlprivate_tokenkeep_base_urlr   r   )r   r   ImportErrorGitlabauthprojectsget)	clsr   r   r   r   r   r   r   g r,   `/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/gitlab.pyvalidate_environment'   sH   z%GitLabAPIWrapper.validate_environmentissuesList[Issue]
List[dict]c                 C  s.   g }|D ]}|j }|j}|||d q|S )z
        Extracts title and number from each Issue and puts them in a dictionary
        Parameters:
            issues(List[Issue]): A list of gitlab Issue objects
        Returns:
            List[dict]: A dictionary of issue titles and numbers
        )titlenumber)r2   iidappend)selfr/   parsedissuer2   r3   r,   r,   r-   parse_issuesY   s   zGitLabAPIWrapper.parse_issuesstrc                 C  sJ   | j jjdd}t|dkr#| |}dtt| d t| }|S dS )z
        Fetches all open issues from the repo

        Returns:
            str: A plaintext report containing the number of issues
            and each issue's title and number.
        opened)stater   Found z	 issues:
zNo open issues available)r   r/   listlenr9   r:   )r6   r/   parsed_issuesparsed_issues_strr,   r,   r-   
get_issuesh   s   
zGitLabAPIWrapper.get_issuesissue_numberintDict[str, Any]c                 C  s   | j j|}d}g }t|dkrA|jj|d}t|dkrn"|D ]}|j|j}||j|j	d d q!|d7 }t|dks|j
|jt|dS )a  
        Fetches a specific issue and its first 10 comments
        Parameters:
            issue_number(int): The number for the gitlab issue
        Returns:
            dict: A dictionary containing the issue's title,
            body, and comments as a string
        r   
   )pageusername)bodyuser   )r2   rI   comments)r   r/   r)   r?   notesr>   idr5   rI   authorr2   descriptionr:   )r6   rC   r8   rG   rL   comments_pagecommentr,   r,   r-   	get_issuez   s$   	zGitLabAPIWrapper.get_issuepr_queryc              
   C  s   | j | jkrdS z(|dd }|t|d d }| jj| j| j ||dgd}d|j W S  tyH } zd	t	| W  Y d}~S d}~ww )
a  
        Makes a pull request from the bot's branch to the base branch
        Parameters:
            pr_query(str): a string which contains the PR title
            and the PR body. The title is the first line
            in the string, and the body are the rest of the string.
            For example, "Updated README
made changes to add info"
        Returns:
            str: A success or failure message
        zXCannot make a pull request because 
            commits are already in the master branch
r      Nzcreated-by-agent)source_branchtarget_branchr2   rP   labelszSuccessfully created PR number z*Unable to make pull request due to error:
)
r   r   splitr?   r   mergerequestscreater4   	Exceptionr:   )r6   rT   r2   rI   prer,   r,   r-   create_pull_request   s$   	z$GitLabAPIWrapper.create_pull_requestcomment_queryc              
   C  s   t |dd }|tt|d d }z| jj|}|jd|i dt| W S  t	yC } zdt| W  Y d}~S d}~ww )af  
        Adds a comment to a gitlab issue
        Parameters:
            comment_query(str): a string which contains the issue number,
            two newlines, and the comment.
            for example: "1

Working on it now"
            adds the comment "working on it now" to issue 1
        Returns:
            str: A success or failure message
        z

r   rV   NrI   zCommented on issue z%Unable to make comment due to error:
)
rD   rZ   r?   r:   r   r/   r)   rM   r\   r]   )r6   ra   rC   rR   r8   r_   r,   r,   r-   comment_on_issue   s   z!GitLabAPIWrapper.comment_on_issue
file_queryc                 C  s   | j | jkrd| j dS |dd }|t|d d }z| jj|| j  d| dW S  tyL   | j d	| ||d
}| jj| d|  Y S w )a  
        Creates a new file on the gitlab repo
        Parameters:
            file_query(str): a string which contains the file path
            and the file contents. The file path is the first line
            in the string, and the contents are the rest of the string.
            For example, "hello_world.md
# Hello World!"
        Returns:
            str: A success or failure message
        +You're attempting to commit directlyto the F branch, which is protected. Please create a new branch and try again.rU   r   rV   NzFile already exists at z. Use update_file insteadCreate )branchcommit_message	file_pathcontentzCreated file )	r   r   rZ   r?   r   filesr)   r]   r\   )r6   rc   ri   file_contentsdatar,   r,   r-   create_file   s&   zGitLabAPIWrapper.create_fileri   c                 C  s    | j j|| j}| dS )z
        Reads a file from the gitlab repo
        Parameters:
            file_path(str): the file path
        Returns:
            str: The file decoded as a string
        zutf-8)r   rk   r)   r   decode)r6   ri   filer,   r,   r-   	read_file   s   zGitLabAPIWrapper.read_filec           	   
   C  s   | j | jkrd| j dS zP|dd }|dd dd  }|dd d	d  }| |}|||}||krD	 W d
S | j d| d||dgd}| jj| d| W S  t	yu } zdt
| W  Y d}~S d}~ww )a8  
        Updates a file with new content.
        Parameters:
            file_query(str): Contains the file path and the file contents.
                The old file contents is wrapped in OLD <<<< and >>>> OLD
                The new file contents is wrapped in NEW <<<< and >>>> NEW
                For example:
                test/hello.txt
                OLD <<<<
                Hello Earth!
                >>>> OLD
                NEW <<<<
                Hello Mars!
                >>>> NEW
        Returns:
            A success or failure message
        rd   re   rU   r   zOLD <<<<rK   z>>>> OLDzNEW <<<<z>>>> NEWzFile content was not updated because old content was not found.It may be helpful to use the read_file action to get the current file contents.rf   update)actionri   rj   )rg   rh   actionszUpdated file z$Unable to update file due to error:
N)r   r   rZ   striprq   replacer   commitsr\   r]   r:   )	r6   rc   ri   old_file_contentsnew_file_contentsfile_contentupdated_file_contentcommitr_   r,   r,   r-   update_file   s@   

zGitLabAPIWrapper.update_filec              
   C  sn   | j | jkrd| j dS z| jj|| j d|  d| W S  ty6 } zdt| W  Y d}~S d}~ww )z
        Deletes a file from the repo
        Parameters:
            file_path(str): Where the file is
        Returns:
            str: Success or failure message
        rd   re   zDelete zDeleted file z$Unable to delete file due to error:
N)r   r   r   rk   deleter]   r:   )r6   ri   r_   r,   r,   r-   delete_file5  s   
zGitLabAPIWrapper.delete_filec                 C     | j du rdS | | j S )z
        Get the list of files in the main branch of the repository

        Returns:
            str: A plaintext report containing the list of files
            in the repository in the main branch
        Nz-No base branch set. Please set a base branch.)r   _list_filesr6   r,   r,   r-   list_files_in_main_branchK     
z*GitLabAPIWrapper.list_files_in_main_branchc                 C  r   )z
        Get the list of files in the active branch of the repository

        Returns:
            str: A plaintext report containing the list of files
            in the repository in the active branch
        N*No active branch set. Please set a branch.r   r   r   r,   r,   r-   list_files_in_bot_branchW  r   z)GitLabAPIWrapper.list_files_in_bot_branchpathc                 C  s   | j du rdS | j| j |dS )a  
        Get the list of files in the active branch of the repository
        from a specific directory

        Returns:
            str: A plaintext report containing the list of files
            in the repository in the active branch from the specified directory
        Nr   rg   r   r   )r6   r   r,   r,   r-   list_files_from_directoryc  s   
	z*GitLabAPIWrapper.list_files_from_directory rg   c              
   C  sv   z#| j ||d}|rd|}dt| d| d| W S d| dW S  ty: } zd| W  Y d }~S d }~ww )	Nr   rU   r=   z files in branch `z`:
zNo files found in branch: ``zError: )_get_repository_filesjoinr?   r]   )r6   rg   r   rk   	files_strr_   r,   r,   r-   r   s  s   
zGitLabAPIWrapper._list_files	List[str]c                 C  sT   | j j||d}g }|D ]}|d dkr || ||d  q||d  q|S )N)refr   typetreer   )r   repository_treeextendr   r5   )r6   rg   r   repo_contentsrk   rj   r,   r,   r-   r     s   z&GitLabAPIWrapper._get_repository_filesproposed_branch_namec                 C  s   ddl m} d}|}t|D ]Q}z| jj|| jd}|j| _d|j dW   S  |y_ } z*|jdkrGd|j	v rG|d	7 }| d
| }nt
d|  td| W Y d}~qd}~ww d| d| dS )z
        Create a new branch in the repository and set it as the active branch

        Parameters:
            proposed_branch_name (str): The name of the new branch to be created
        Returns:
            str: A success or failure message
        r   )GitlabCreateErrord   )rg   r   zBranch 'z9' created successfully, and set as current active branch.i  zBranch already existsrK   _vz Failed to create branch. Error: z8Unable to create branch name from proposed_branch_name: Nz"Unable to create branch. At least z? branches exist with named derived from proposed_branch_name: `r   )r   r   ranger   branchesr\   r   nameresponse_codeerror_messageprintr]   )r6   r   r   max_attemptsnew_branch_nameiresponser_   r,   r,   r-   create_branch  s>   	

zGitLabAPIWrapper.create_branchc                 C  sD   dd | j jjddD }|r d|}dtt| d| S dS )	z
        Get the list of branches in the repository

        Returns:
            str: A plaintext report containing the number of branches
            and each branch name
        c                 S     g | ]}|j qS r,   r   .0rg   r,   r,   r-   
<listcomp>  s    z:GitLabAPIWrapper.list_branches_in_repo.<locals>.<listcomp>TallrU   r=   z branches in the repository:
z#No branches found in the repository)r   r   r>   r   r:   r?   )r6   r   branches_strr,   r,   r-   list_branches_in_repo  s   
z&GitLabAPIWrapper.list_branches_in_repobranch_namec                 C  sH   dd | j jjddD }||v r|| _d| dS d| dt| S )	zEquivalent to `git checkout branch_name` for this Agent.
        Clones formatting from Gitlab.

        Returns an Error (as a string) if branch doesn't exist.
        c                 S  r   r,   r   r   r,   r,   r-   r     s    z6GitLabAPIWrapper.set_active_branch.<locals>.<listcomp>Tr   zSwitched to branch `r   zError z/ does not exist,in repo with current branches: )r   r   r>   r   r:   )r6   r   curr_branchesr,   r,   r-   set_active_branch  s   z"GitLabAPIWrapper.set_active_branchr   queryc                 C  s
  |dkr|   S |dkrt| t|S |dkr| |S |dkr(| |S |dkr1| |S |dkr:| |S |dkrC| 	|S |dkrL| 
|S |d	krU| |S |d
kr]|  S |dkrf| |S |dkrn|  S |dkrv|  S |dkr| |S td| )NrB   rS   rb   rn   r`   rq   r}   r   r   r   r   r   r   r   zInvalid mode)rB   jsondumpsrS   rD   rb   rn   r`   rq   r}   r   r   r   r   r   r   r   
ValueError)r6   r   r   r,   r,   r-   run  s:   








zGitLabAPIWrapper.run)r   r   r   r   )r/   r0   r   r1   )r   r:   )rC   rD   r   rE   )rT   r:   r   r:   )ra   r:   r   r:   )rc   r:   r   r:   )ri   r:   r   r:   )r   r:   r   r:   )r   )rg   r:   r   r:   r   r:   )rg   r:   r   r:   r   r   )r   r:   r   r:   )r   r:   r   r:   )r   r:   r   r:   r   r:   )#__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r
   model_configr   classmethodr.   r9   rB   rS   r`   rb   rn   rq   r}   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   r      sL   
 
0





"

>




1
r   )r   
__future__r   r   typingr   r   r   r   r   langchain_core.utilsr   pydanticr	   r
   r   gitlab.v4.objectsr   r   r,   r,   r,   r-   <module>   s    