
    }                     \    d Z ddlZddlZddlZddlmZ ddlmZ d Zd Z	d Z
d Zd	 Zd
 Zy)zEHelper functions to interact with git and github for iac remediation.    N)errors)filesc                     | d   t        j                  d      | d   t        j                  d      | d   t        j                  d      y)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            	         	 d} t        j                  | ddt        j                         t         j                  t         j                  d      }d|j
                  j                         fS # t         j                  $ r Y yw xY w)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                     t        j                  ddd| |gdt        j                         t         j                  t         j                  d      }t        |j                  j                               S )a  Helper 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   sU     >>k9k;?ryy{__Z__	& 
fmm!!#	$$r   c                 x   d}t        j                  g d      j                  d      }|j                         D ]  }||v s|j	                         d   } n |t        j                         }t        j                  ddd|d|gd	t        j                         t         j                  t         j                  
       t        | |      s;t        j                  ddd| |gd|t         j                  t         j                  
       t        j                  ddgd|t         j                  t         j                  
       |S )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dZZ\!_l	 .
 ##%LNN	
E<{C		z  [9nn&*K
E< NN	z
 
r   c                 j   t               \  }}~t        ||      }| j                         D ]  \  }}t        j                  j                  ||      }	t        j                  j                  ||	      }
t        j                  |
|       t        j                  dd|
gd|t        j                  t        j                          t        j                  ddd|gd|t        j                  t        j                         t        j                  ddgd|t        j                  t        j                         y	)
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x8M	M95NN	}%z	 1 ..hn-|__Z__ ..fo|__Z__r   c           	         t        ||      }d| d| d|  d| d	}t        j                  |dd|t        j                  t        j                         t        j                  d	d
dd|gd|t        j                  t        j                         y)aD  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   