
    (                     @   S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SS	KJ
r
  SS
KJr  SSKJr  SSKJr  \R"                  " \R$                  R&                  5      \R(                   " S S\R*                  \R,                  5      5       5       rg)7Command for semi-automatic remediation of SCC findings.    N)const)
converters)extended_service)git)sps_api)	terraform)
validators)base)flags)logc                   <    \ rS rSrSrSSS.r\S 5       rS
S jrS	r	g)AutoRemediate    r   z
        Orchestrates the semi-automatic remediation process for SCC findings
        by calling the Remediation Intent APIs.
        a`  
          Sample usage:
          Remediate a SCC finding for the organization 1234567890, in the
          terraform repository located at ./terraform-repo.
          $ {{command}} scc remediation-intents auto-remediate \
            --org-id=1234567890 \
            --root-dir-path=./terraform-repo \
            --git-config-path=./git-config.yaml)DESCRIPTIONEXAMPLESc                     [         R                  R                  U 5        [         R                  R                  U S5        [         R                  R                  U 5        [         R
                  R                  U 5        g )N.)r   ROOT_DIR_PATH_FLAGAddToParser
SetDefaultORG_ID_FLAGGIT_CONFIG_FILE_PATH_FLAG)parsers    5lib/surface/scc/remediation_intents/auto_remediate.pyArgsAutoRemediate.Args6   sT    	((0	''4	!!&)	##//7    Nc                 "   UR                   nUR                  nUR                  n[        R                  " U[
        R                  R                  5      n[        R                  " [
        R                  R                  5      n[        R                  " [
        R                  R                  5      n[        R                  " U5        [        R                  " U5        UR                  5       nUc:  UR!                  5         UR                  5       nUc  ["        R$                  " S5        gUR&                  n	[(        R*                  " U5      n
U
(       d  ["        R$                  " S5        g[(        R,                  " XHR.                  5      n["        R$                  " S5        [0        R2                  " U5      nUR4                  R6                  R8                  Ul        UR=                  UR?                  URA                  U
5      US9S9Ul!        SnURE                  XU5      nUR:                  UR4                  R6                  RF                  :X  a  ["        R$                  " S5        gS	nS
nU(       Gd4  U[H        RJ                  :  Ga  ["        R$                  " SU5        URM                  URN                  RP                  S
   RR                  RT                  5      n[(        RV                  " U5      nUc  SnOURY                  US9URB                  l-        SnURE                  XU5      nUR:                  UR4                  R6                  RF                  :X  a  ["        R$                  " S5        gUS-  n["        R$                  " S5        U(       d  U[H        RJ                  :  a  GM  U(       dP  ["        R$                  " S5        UR4                  R6                  RF                  Ul        SnURE                  XU5      ng["        R$                  " S5        UR4                  R6                  R\                  Ul        SURB                  l-        SnURE                  XU5      n["        R$                  " S5        URM                  URN                  RP                  S
   RR                  RT                  5      nUS==   [_        [`        Rb                  " 5       5      -  ss'   [d        Rf                  " U[H        Rh                  Rk                  UR.                  Rl                  Ro                  S5      S   UR.                  Rl                  Ro                  S5      S   UR.                  Rp                  S9US   US   5        ["        R$                  " S5        [d        Rr                  " [H        Rt                  Rk                  UR.                  Rl                  Ro                  S5      S   UR.                  Rl                  Ro                  S5      S   UR.                  Rp                  S9[H        Rv                  Rk                  URN                  Rx                  R{                  SS5      SR}                  S [d        R~                  " U5      R                  5        5       5      SR}                  S  [d        R~                  " USS!9R                  5        5       5      S"9US   US   US#   US$   5      u  nnU(       dd  ["        R$                  " S%5        UR4                  R6                  R                  Ul        URY                  US9Ul-        S&nURE                  XU5      ng["        R$                  " S'5        UR4                  R6                  R                  Ul        UR                  UR                  U[        [d        R~                  " USS!9R                  5       5      [        [d        R~                  " USS!9R                  5       5      S(9S)9UlH        S*nURE                  XU5      ng)+zThe main function which is called when the user runs this command.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    command invocation.
Nz9No remediation intent found to be remediated, exitting...zNo TF files found, exitting...zRemediation started....)fileDatatfStateInfo)tfDatazstate,remediation_inputzRemediation failed, exitting...Fr   zRemediation retry count: T)reasonzremediation_input.error_details   zRemediation failed, retrying...z,Remediation failed: Max retry limit reached.statez#Remediation completed successfully.z%state,remediation_input.error_detailsz!Starting PR generation process...zbranch-prefix/)
project_id
finding_idcategoryremotezCommit pushed successfully.`z\`
c              3   4   #    U  H  u  pU S U 3v   M     g7fz: N .0fpeas      r   	<genexpr>$AutoRemediate.Run.<locals>.<genexpr>   s#      %PFB $bP   c              3   4   #    U  H  u  pU S U 3v   M     g7fr/   r0   r1   s      r   r5   r6      s'      "FB $br7   )first)remediation_explanationfile_modifiersfile_ownerszmain-branch-name	reviewerszPR creation failed, exitting...zstate,error_detailszPR created successfully.)urlmodifiedFileOwnersmodifiedFilePaths)prDatazstate,remediation_artifacts)Iorg_idgit_config_pathroot_dir_pathr   ExtendedSPSClientr   ReleaseTrackALPHAr   RemediationIntentConverterr   GetMessagesModuler
   validate_git_configvalidate_relative_dir_path!fetch_enqueued_remediation_intent)create_semi_autonomous_remediation_intentr   Printnamer	   fetch_tf_filesparse_tf_filefindingDatacopydeepcopyRemediationIntentStateValueValuesEnumREMEDIATION_IN_PROGRESSr%   RemediationInputTfDataDictFilesToMessageremediationInputupdate_remediation_intentREMEDIATION_FAILEDr   REMEDIATION_RETRY_COUNTMessageFilesToDictremediatedOutput
outputDatar"   r    validate_tf_filesErrorDetailserrorDetailsREMEDIATION_SUCCESSstruuiduuid4r   push_commit
COMMIT_MSGformatfindingNamesplitr*   	create_prPR_TITLEPR_DESCremediationExplanationreplacejoinget_file_modifiersitemsPR_GENERATION_FAILEDPR_GENERATION_SUCCESSRemediationArtifactsPullRequestlistvalueskeysremediationArtifacts)selfargsrB   git_config_datarD   client	convertermessagesintent_dataintent_nametf_filestfstate_dataintent_updatedupdate_maskis_remediatedretry_countupdated_tf_files	error_msg_	pr_statuspr_msgs                        r   RunAutoRemediate.Run=   s     [[F**O&&M//8I8I8O8OPF55d6G6G6M6MNI(():):)@)@AH ""?3))-8 ::<K668<<>k				MN""K''6H	ii01**..L II'(]];/N""77OO  '/&?&?11(;$  
 '@ 'N# ,K55.N 	%%::MM	N 
ii12 MKe.K.K K	ii+[9"55

)
)
4
4Q
7
>
>
G
G --.>?i		5=5J5J 6K 6n%%2 6k77
Nn 

''<<OOP 			34Qk	ii121 e.K.K K4 	ii>?

$
$
9
9
L
L  k

*
*
Na II34""77KK  48N##09K55.N
 II12 33''2215<<EE O$DJJL(99$OO%11==CCCHK%11==CCCHL#//88 	  	

 	!(	 II+,%11==CCCHK%11==CCCHL#//88 	 	

 	$2$C$C$Z$Z$b$bU%  99 %"556FGNNP%  		 " **+;4H%'"  	 	
 	!(*+$1Iv6 	ii12

$
$
9
9
N
N  %-$9$9$9$Hn!)k

*
*
Na  II()""77MM  +3*G*G###''(8EMMO  #''(8EKKM $ 
 +H 
+N' 0K((.	Ar   r0   )returnN)
__name__
__module____qualname____firstlineno____doc__detailed_helpstaticmethodr   r   __static_attributes__r0   r   r   r   r       s3    
 @
3-  8 8Br   r   )r   rS   rg   .googlecloudsdk.api_lib.scc.remediation_intentsr   r   r   r   r   r	   r
   googlecloudsdk.callioper   2googlecloudsdk.command_lib.scc.remediation_intentsr   googlecloudsdk.corer   ReleaseTracksrF   rG   UniverseCompatibleSilentCommandCacheCommandr   r0   r   r   <module>r      s    >   @ E K > B D E ( D # D%%++,[D&&(9(9 [  -[r   