
    dhJ                        S r SSKJr  SSKrSSKJrJrJrJrJ	r	  SSK
Jr  SSKJrJrJr  \(       a  SSKJr   " S S	\5      rg)
zUtil that calls gitlab.    )annotationsN)TYPE_CHECKINGAnyDictListOptional)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)Issuec                  t   \ rS rSr% SrSrS\S'   SrS\S'   SrS\S'    Sr	S\S	'    Sr
S\S
'    SrS\S'    SrS\S'    \" SS9r\" SS9\S%S j5       5       rS&S jrS'S jrS(S jrS)S jrS*S jrS+S jrS,S jrS+S jrS,S jrS'S jrS'S jrS-S jrS.S/S jjrS.S0S jjrS1S  jr S'S! jr!S2S" jr"S3S# jr#S$r$g)4GitLabAPIWrapper   z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)modec                l   [        USSSS9n[        USS5      n[        USS5      n[        US	S
SS9n[        USSSS9n SSKnUR                  UUSS9nUR	                  5         XS'   UR
                  R                  U5      US'   X!S'   X1S'   XAS'   XQS	'   XaS'   U$ ! [         a    [        S5      ef = f)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)	clsvaluesr   r   r   r   r   r   gs	            \/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/gitlab.pyvalidate_environment%GitLabAPIWrapper.validate_environment'   s   
 *L,8L

 1')<
 (<24R(
$ -O_f
 2(*>
	 MM6  
 	
x)*8I)J%&)|&7"#1M-."/'9#$-  	E 	s    B B3c                r    / nU H.  nUR                   nUR                  nUR                  XES.5        M0     U$ )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)r3   iidappend)selfissuesparsedissuer3   r4   s         r/   parse_issuesGitLabAPIWrapper.parse_issuesY   s;     EKKEYYFMME<=      c                    U R                   R                  R                  SS9n[        U5      S:  a9  U R	                  U5      nS[        [        U5      5      -   S-   [        U5      -   nU$ g)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   r8   listlenr;   str)r7   r8   parsed_issuesparsed_issues_strs       r/   
get_issuesGitLabAPIWrapper.get_issuesh   so     **1166X6Fv;? --f5M3s=122\ACDVV  %$-r=   c                   U R                   R                  R                  U5      nSn/ n[        U5      S::  a  UR                  R                  US9n[        U5      S:X  a  OoU HS  nUR                  R                  UR                  5      nUR                  UR                  UR                  S   S.5        MU     US-  n[        U5      S::  a  M  UR                  UR                  [        U5      S.$ )z
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   )r3   rM   comments)r   r8   r+   rC   notesrB   idr6   rM   authorr3   descriptionrD   )r7   issue_numberr:   rK   rP   comments_pagecomments          r/   	get_issueGitLabAPIWrapper.get_issuez   s     ))0044\B!(mr!!KK,,$,7M=!Q&(++//'**5 ' 'z : ) AID (mr! [[%%H
 	
r=   c                l   U R                   U R                  :X  a  g UR                  S5      S   nU[        U5      S-   S nU R                  R
                  R                  U R                  U R                   UUS/S.5      nSUR                   3$ ! [         a  nS	[        U5      -   s SnA$ SnAff = f)
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_branchr3   rT   labelszSuccessfully created PR number z*Unable to make pull request due to error:
)
r   r   splitrC   r   mergerequestscreater5   	ExceptionrD   )r7   pr_queryr3   rM   pres         r/   create_pull_request$GitLabAPIWrapper.create_pull_request   s     ""d&8&888N t,Q/E
Q 01..<<CC)-););)-)@)@!&'+#5"6 9AA NDs1vMMNs   A3B 
B3B.(B3.B3c                \   [        UR                  S5      S   5      nU[        [        U5      5      S-   S n U R                  R
                  R                  U5      nUR                  R                  SU05        S[        U5      -   $ ! [         a  nS[        U5      -   s SnA$ SnAff = f)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   r\   NrM   zCommented on issue z%Unable to make comment due to error:
)
intr`   rC   rD   r   r8   r+   rQ   rb   rc   )r7   comment_queryrU   rW   r:   rf   s         r/   comment_on_issue!GitLabAPIWrapper.comment_on_issue   s     =..v6q9:C$5 6 : <=	E--4488FEKK01(3|+<<< 	E;c!fDD	Es   AB	 	
B+B& B+&B+c                   U R                   U R                  :X  a  SU R                   S3$ UR                  S5      S   nU[        U5      S-   S n U R                  R
                  R                  X R                   5        SU S3$ ! [         aA    U R                   S	U-   UUS
.nU R                  R
                  R                  U5        SU-   s $ f = f)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.r[   r   r\   NzFile already exists at z. Use update_file insteadCreate )branchcommit_message	file_pathcontentzCreated file )	r   r   r`   rC   r   filesr+   rc   rb   )r7   
file_queryrt   file_contentsdatas        r/   create_fileGitLabAPIWrapper.create_file   s     !8!88112 3<<
 $$T*1-	"3y>A#5#78	/%%++//	;M;MN,YK7PQQ 
	/,,"+i"7&(	D %%++2248"Y..
	/s   4B ACCc                    U R                   R                  R                  XR                  5      nUR	                  5       R	                  S5      $ )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   rv   r+   r   decode)r7   rt   files      r/   	read_fileGitLabAPIWrapper.read_file   s=     ((..229>P>PQ{{}##G,,r=   c                p   U R                   U R                  :X  a  SU R                   S3$  UR                  S5      S   nUR                  S5      S   R                  S5      S   R                  5       nUR                  S5      S   R                  S	5      S   R                  5       nU R	                  U5      nUR                  X45      nXV:X  a   g
U R                   SU-   SUUS./S.nU R                  R                  R                  U5        SU-   $ ! [         a  nS[        U5      -   s SnA$ SnAff = f)a  
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
ro   rp   r[   r   zOLD <<<<rO   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.rq   update)actionrt   ru   )rr   rs   actionszUpdated file z$Unable to update file due to error:
N)r   r   r`   stripr   replacer   commitsrb   rc   rD   )	r7   rw   rt   old_file_contentsnew_file_contentsfile_contentupdated_file_contentcommitrf   s	            r/   update_fileGitLabAPIWrapper.update_file   sd   $ !8!88112 3<<
$	D"((.q1I  ,Q/55jA!DJJL    ,Q/55jA!DJJL   >>)4L#/#7#7!$  31 ,,"+i"7 #+%.#7
F %%--44V<"Y.. 	D:SVCC	Ds%   B$D AD 
D5D0*D50D5c                   U R                   U R                  :X  a  SU R                   S3$  U R                  R                  R	                  XR                   SU-   5        SU-   $ ! [
         a  nS[        U5      -   s SnA$ SnAff = f)z}
Deletes a file from the repo
Parameters:
    file_path(str): Where the file is
Returns:
    str: Success or failure message
ro   rp   zDelete zDeleted file z$Unable to delete file due to error:
N)r   r   r   rv   deleterc   rD   )r7   rt   rf   s      r/   delete_fileGitLabAPIWrapper.delete_file8  s     !8!88112 3<<
	D%%++22--y9/D #Y.. 	D:SVCC	Ds   7A$ $
B.B;BBc                T    U R                   c  gU R                  U R                   5      $ )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
z-No base branch set. Please set a base branch.)r   _list_filesr7   s    r/   list_files_in_main_branch*GitLabAPIWrapper.list_files_in_main_branchN  s*     ""*B 7 788r=   c                T    U R                   c  gU R                  U R                   5      $ )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
*No active branch set. Please set a branch.r   r   r   s    r/   list_files_in_bot_branch)GitLabAPIWrapper.list_files_in_bot_branchZ  s*     %? 2 233r=   c                R    U R                   c  gU R                  U R                   US9$ )z
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
r   rr   pathr   )r7   r   s     r/   list_files_from_directory*GitLabAPIWrapper.list_files_from_directoryf  s7     %?%%   
 	
r=   c                     U R                  UUS9nU(       a%  SR                  U5      nS[        U5       SU SU 3$ SU S3$ ! [         a  nSU 3s S nA$ S nAff = f)	Nr   r[   rA   z files in branch `z`:
zNo files found in branch: ``zError: )_get_repository_filesjoinrC   rc   )r7   rr   r   rv   	files_strrf   s         r/   r   GitLabAPIWrapper._list_filesv  s~    	!.. / E  IIe,	E
|+=fXT)UU4VHA>> 	!QC= 	!s"   ;A A 
AAAAc                    U R                   R                  XS9n/ nU HE  nUS   S:X  a%  UR                  U R                  XS   5      5        M1  UR	                  US   5        MG     U$ )N)refr   typetreer   )r   repository_treeextendr   r6   )r7   rr   r   repo_contentsrv   ru   s         r/   r   &GitLabAPIWrapper._get_repository_files  sh    11AAfAX$Gv&(T77PQWV_-	 % r=   c                   SSK Jn  SnUn[        U5       HW  n U R                  R                  R                  UU R                  S.5      nUR                  U l        SUR                   S3s  $    SU SU S3$ ! U aS  nUR                  S:X  a"  SUR                  ;   a  US	-  nU S
U 3n SnAM  [        SU 35        [        SU 35      eSnAff = f)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   )rr   r   zBranch 'z9' created successfully, and set as current active branch.i  zBranch already existsrO   _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   branchesrb   r   nameresponse_codeerror_messageprintrc   )r7   proposed_branch_namer   max_attemptsnew_branch_nameiresponserf   s           r/   create_branchGitLabAPIWrapper.create_branch  s    	-.|$A44==DD"1#11 &.]]"x}}o .N N %> 1 ?&&:%;1>	
 % OOs*/1??BFA)=(>b&DO <QC@A#R/02 s   AA::C ,C2CCc                    U R                   R                  R                  SS9 Vs/ sH  oR                  PM     nnU(       a+  SR	                  U5      nS[        [        U5      5       SU 3$ gs  snf )z
Get the list of branches in the repository

Returns:
    str: A plaintext report containing the number of branches
    and each branch name
Tallr[   rA   z branches in the repository:
z#No branches found in the repository)r   r   rB   r   r   rD   rC   )r7   rr   r   branches_strs       r/   list_branches_in_repo&GitLabAPIWrapper.list_branches_in_repo  s     '+&?&?&H&H&M&MRV&M&W
&WFKK&W 	 
 99X.LS]+, -!N$ 5
s   A/c                    U R                   R                  R                  SS9 Vs/ sH  nUR                  PM     nnX;   a  Xl        SU S3$ SU S[        U5       3$ s  snf )zEquivalent to `git checkout branch_name` for this Agent.
Clones formatting from Gitlab.

Returns an Error (as a string) if branch doesn't exist.
Tr   zSwitched to branch `r   zError z/ does not exist,in repo with current branches: )r   r   rB   r   r   rD   )r7   branch_namerr   curr_branchess       r/   set_active_branch"GitLabAPIWrapper.set_active_branch  s     33<<AA B 
 KK 	 
 '!,)+a88  &225m2D1EG
s   Ac                   US:X  a  U R                  5       $ US:X  a.  [        R                  " U R                  [	        U5      5      5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US	:X  a  U R                  U5      $ US
:X  a  U R                  5       $ US:X  a  U R                  U5      $ US:X  a  U R                  5       $ US:X  a  U R                  5       $ US:X  a  U R!                  U5      $ [#        SU-   5      e)NrG   rX   rl   rz   rg   r   r   r   r   r   r   r   r   r   zInvalid mode)rG   jsondumpsrX   rj   rl   rz   rg   r   r   r   r   r   r   r   r   r   
ValueError)r7   r   querys      r/   runGitLabAPIWrapper.run  sv   <??$$[ ::dnnSZ899''((//]"##E****++E22[ >>%((]"##E**]"##E**_$%%e,,,,--//(())%00001133//00220011%88^d233r=   )r   )r-   r   returnr   )r8   zList[Issue]r   z
List[dict])r   rD   )rU   rj   r   zDict[str, Any])rd   rD   r   rD   )rk   rD   r   rD   )rw   rD   r   rD   )rt   rD   r   rD   )r   rD   r   rD   ) )rr   rD   r   rD   r   rD   )rr   rD   r   rD   r   z	List[str])r   rD   r   rD   )r   rD   r   rD   )r   rD   r   rD   r   rD   )%__name__
__module____qualname____firstlineno____doc__r   __annotations__r   r   r   r   r   r   r   model_configr   classmethodr0   r;   rG   rX   rg   rl   rz   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r=   r/   r   r      s    !FC $#$ $J$)'+}+P26 -6P#'M=' )-, L (#.  $.`.$
@N>E( /D	-<D|D,
9
4
 !
/
b5&*4r=   r   )r   
__future__r   r   typingr   r   r   r   r   langchain_core.utilsr	   pydanticr
   r   r   gitlab.v4.objectsr   r   r   r=   r/   <module>r      s1     "  ; ; 5 ; ;'x4y x4r=   