
    H                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  G d de
j,                        Zy)z1Update Cloud Source Repositories trigger command.    )absolute_import)division)unicode_literals)cloudbuild_util)trigger_config)base)resource_args)concept_parsers)presentation_specs)log)
properties)	resourcesc                   4    e Zd ZdZddiZed        Zd Zd Zy)	UpdateCSRz>Updates Cloud Source Repositories trigger used by Cloud Build.EXAMPLESa              To update the branch pattern of the trigger:

              $ {command} my-trigger --branch-pattern=".*"

            To update the build config of the trigger:

              $ {command} my-trigger --build-config="cloudbuild.yaml"

            To update the substitutions of the trigger:

              $ {command} my-trigger --update-substitutions=_REPO_NAME=my-repo,_BRANCH_NAME=master
        c                 *   t         j                  j                  dt        j                         dd      j                  |        t        j                  | dd      }t        j                  dt        j                         dd	      }t        j                  |g      j                  |       |j                         }t        j                  |       t        j                  |       t        j                  |dd
       t        j                   |       y)zRegisters flags for this command.

    Args:
      parser: An argparse.ArgumentParser-like object. It is mocked out in order
        to capture some information, but behaves like an ArgumentParser.
    TRIGGERzBuild Trigger.TrequiredF)add_region_flagadd_namez--repozCloud Source Repository.)prefixes)has_build_configrequire_docker_imageN)r
   ConceptParserForResourcer	   GetTriggerResourceSpecAddToParsertrigger_utilsAddTriggerArgsr   ResourcePresentationSpecrepo_resourceGetRepoResourceSpecadd_mutually_exclusive_groupAddBranchPatternAddTagPatternAddBuildConfigArgsForUpdateAddRepoEventArgs)parserflag_config	repo_spec
ref_configs       ?lib/surface/builds/triggers/update/cloud_source_repositories.pyArgszUpdateCSR.Args5   s     !!--,,.4 . ) *5V)<..7K";;))+"	I
 !!9+.::;G99;J"":.
+--dG"";/    c           	         t        j                         }t        j                  ||      \  }}|r|S |j                  j
                  j                         }d}|r|j                  }|j                  |j                  |j                  |j                  ||j                  |j                              }t        j                  |||       t        j                  |||||dd       t        j                   ||       |S )ao  Parses command line arguments into a build trigger.

    Args:
      args: An argparse arguments object.
      old_trigger: The existing trigger to be updated.
      update_mask: The fields to be updated.

    Returns:
      A build trigger object.

    Raises:
      RequiredArgumentException: If comment_control is defined but
      pull_request_pattern isn't.
    N)repoName
branchNametagName)descriptionserviceAccounttriggerTemplate T)r   )r   GetMessagesModuler   ParseTriggerArgsForUpdateCONCEPTSrepoParsereposIdBuildTriggerr4   service_account
RepoSourcebranch_patterntag_patternParseRequireApprovalParseBuildConfigArgsForUpdateParseRepoEventArgs)	selfargsold_triggerupdate_maskmessagestriggerdonerepo_refr;   s	            r-   ParseTriggerFromFlagszUpdateCSR.ParseTriggerFromFlagsR   s     002H!;;D(KMGTn}}!!'')HDd##$$++ ++**$$ , 
 $ G &&wh? //
 $$Wd3Nr/   c                    t        j                         }t        j                         }t        j                  j
                  j                  j                  d      }t        j                  j                  j                  j                         }|j                  xs |xs t         j                  }|j                  }t        j                  j                  ||||dd      j                         }|j                   j                  |j"                  j%                  ||            }	g }
| j'                  ||	|
      }d|
j)                  t        j*                  |             t-        t/        t1        fd|
                  }
|j3                  |||d	j5                  |
      
      }|j                   j7                  |      }t9        j:                  |d       |S )zThis is what gets called when the user runs this command.

    Args:
      args: An argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated CSR trigger.
    Tr   )
projectsIdlocationsId
triggersIdz&cloudbuild.projects.locations.triggers)params
collection)name	triggerIdsubstitutionsc                 .    | j                        rS | S )N)
startswith)msubs    r-   <lambda>zUpdateCSR.Run.<locals>.<lambda>   s    c!2#99r/   ,)resourceNamerV   buildTrigger
updateMaskbuild_trigger)kind)r   GetClientInstancer8   r   VALUEScoreprojectGetbuildsregionDEFAULT_REGIONr   r   REGISTRYr<   RelativeNameprojects_locations_triggersMESSAGES_MODULE-CloudbuildProjectsLocationsTriggersGetRequestrN   extendMessageToFieldPathslistsetmap/CloudbuildProjectsLocationsTriggersPatchRequestjoinPatchr   UpdatedResource)rF   rG   clientrJ   rf   
regionproplocation	triggeridrU   rH   rI   rK   requpdated_triggerr[   s                 @r-   RunzUpdateCSR.Run   s    ..0F002H$$,,00$0?G""))00446J{{JjJO,J,JHI##!##

 < $ = >J\^ 	 4488LL 	M 	,-K K(({KHG C::7CDC9;GHJK 
B
B88K(	 C *C 88>>sCO	8r/   N)	__name__
__module____qualname____doc__detailed_helpstaticmethodr.   rN   r    r/   r-   r   r   !   s5    F -" 0 080d1r/   r   N)r   
__future__r   r   r   !googlecloudsdk.api_lib.cloudbuildr   r   r   googlecloudsdk.callioper   %googlecloudsdk.command_lib.cloudbuildr	   !googlecloudsdk.command_lib.sourcer"   (googlecloudsdk.command_lib.util.conceptsr
   r   googlecloudsdk.corer   r   r   UpdateCommandr   r   r/   r-   <module>r      sE    8 &  ' = M ( ? L D G # * )T"" Tr/   