
    }                     \    S r SSKrSSKrSSKrSSKJr  SSKJr  S rS r	S r
S rS	 rS
 rg)zEHelper functions to interact with git and github for iac remediation.    N)errors)filesc                     U S   c  [         R                  " S5      eU S   c  [         R                  " S5      eU S   c  [         R                  " S5      eg)zValidates the git config file.remoteNzmain-branch-namezbranch-prefix)r   InvalidGitConfigError)git_config_files    5lib/googlecloudsdk/api_lib/scc/iac_remediation/git.pyvalidate_git_configr
      s]    X&

&
&x
00'(0

&
&'9
::_%-

&
&
77 .    c            
          Sn [         R                  " U SS[        R                  " 5       [         R                  [         R                  SS9nSUR
                  R                  5       4$ ! [         R                   a     gf = f)zCheck whether the current directory is a git repo or not.

Returns:
  True, repo_root_path if the current directory is a git repo
  False, None otherwise.
zgit rev-parse --show-toplevelT)shellcheckcwdstdoutstderrtext)FN)
subprocessrunosgetcwdPIPEr   stripCalledProcessError)git_check_cmdresults     r	   is_git_repor   %   sl    
4M^^$BIIKz	F $$&&&		&	& s   A&A) )B ?B c           	          [         R                  " SSSX/S[        R                  " 5       [         R                  [         R                  SS9n[        UR                  R                  5       5      $ )zHelper function to check if a branch exists in the remote.

Args:
  remote_name: Name of the remote of the repo at which to check.
  branch_name: Name of the branch to check.

Returns:
  Boolean indicating whether the branch exists in the remote.
gitz	ls-remotez--headsFT)r   r   r   r   r   )r   r   r   r   r   boolr   r   )remote_namebranch_namer   s      r	   branch_remote_existsr"   9   sS     >>k9k?ryy{__Z__	& 
fmm!!#	$$r   c                    Sn[         R                  " / SQ5      R                  S5      nUR                  5        H  nX;   d  M
  UR	                  5       S   n  O   Uc  [
        R                  " 5       n[         R                  " SSSUSU/S	[        R                  " 5       [         R                  [         R                  S
9  [        X5      (       d8  [         R                  " SSSX/SU[         R                  [         R                  S
9  [         R                  " SS/SU[         R                  [         R                  S
9  U$ )a	  Returns the working tree directory for the branch.

Args:
  remote_name: Name of the remote of the repo at which to check.
  branch_name: Name of the branch for which the working tree directory is
  required.

Returns:
  Working tree directory path for the branch.
N)r   worktreelistzutf-8r   r   r$   addz-BTr   r   r   r   pushz--set-upstreamFpull)r   check_outputdecode
splitlinessplittempfilemkdtempr   r   r   r   r"   )r    r!   worktree_direxisting_worktreeslines        r	   get_working_tree_dirr3   L   s    ,!..!
F7O  !++-dZZ\!_l	 .
 ##%LNN	
E<{C		z  99nn&*K
E< NN	z
 
r   c           	      X   [        5       u  pEA[        X#5      nU R                  5        H  u  px[        R                  R                  Xu5      n	[        R                  R                  Xi5      n
[        R                  " X5        [        R                  " SSU
/SU[        R                  [        R                  S9  M     [        R                  " SSSU/SU[        R                  [        R                  S9  [        R                  " SS/SU[        R                  [        R                  S9  g	)
a  Pushes the commit to the given branch with the given files data and commit message.

Args:
  files_data: Dictionary of file path (absolute path of the files in original
  repo) and file data to be written
  commit_message: Message to be added to the commit
  remote_name: Name of the remote of the repo at which to check.
  branch_name: Name of the branch where commit needs to be pushed
r   r&   Tr'   commitz-mFr(   N)r   r3   itemsr   pathrelpathjoinr   WriteFileContentsr   r   r   )
files_datacommit_messager    r!   is_repo_flagrepo_root_dirr0   	file_path	file_datarel_pathabs_file_paths              r	   push_commitrC   x   s     !,,%k?,(..0iwwy8HGGLL8M	M5NN	}%z	 1 ..hn-|__Z__ ..fo|__Z__r   c           	         [        X#5      nSU SU SU  SU S3	n[        R                  " USSU[        R                  [        R                  S9  [        R                  " S	S
SSU/SU[        R                  [        R                  S9  g)a6  Creates a PR for the given branch to the main base branch.

Args:
  pr_title: PR title
  pr_desc: PR description
  remote_name: Name of the remote of the repo at which to check.
  branch_name: The branch from which PR needs to be created.
  base_branch: The main branch name to be which PR needs to be merged.
zgh pr create --base z --head z
 --title "z
" --body ""TF)r   r   r   r   r   r   r$   removez--forcer'   N)r3   r   r   r   )pr_titlepr_descr    r!   base_branchr0   
pr_commands          r	   raise_prrK      s     &k?,[M+ ?*Jwiq*  ..E|__Z__ ..j(I|<|__Z__r   )__doc__r   r   r.   .googlecloudsdk.command_lib.scc.iac_remediationr   googlecloudsdk.core.utilr   r
   r   r"   r3   rC   rK    r   r	   <module>rP      s:    L 	   A *	8(%&)X"Jr   