
    4                         d Z ddlmZ ddlmZ ddlmZ ddl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)zUpdate Webhook trigger command.    )absolute_import)division)unicode_literalsN)cloudbuild_util)trigger_config)base)resource_args)concept_parsers)log)
properties)	resourcesc                   T    e Zd ZdZd ej
                  d      iZed        Zd Z	d Z
y)UpdateWebhookz-Update a Webhook trigger used by Cloud Build.EXAMPLESa              To update the branch from which the trigger clones:

              $ {command} my-webhook-trigger --source-to-build-branch=my-branch

            To update the webhook secret:

              $ {command} my-webhook-trigger --secret=projects/my-project/secrets/my-secret/versions/2

            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      }|j                  dt        j                  d      	       t        j                  |dd
       t        j                  |       t        j                  |       y)zRegister 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--secretz
            The full path of the secret version required to validate webhook requests against this trigger.
            For example, projects/my-project/secrets/my-secret/versions/1.
        )help)has_file_sourcerequire_docker_imageN)r
   ConceptParserForResourcer	   GetTriggerResourceSpecAddToParsertrigger_utilsAddTriggerArgsadd_argumenttextwrapdedentAddBuildConfigArgsForUpdateAddRepoSourceForUpdateAddFilterArg)parserflag_configs     -lib/surface/builds/triggers/update/webhook.pyArgszUpdateWebhook.Args3   s     !!--,,.	 . 
 k&..K __     --T ((5{+    c           
      R   t        j                         }t        j                  ||      \  }}|r|S t        j
                  j                  j                  j                  d      }d|d|j                  d}t        j                  ||||||dd       |j                  |_        |S )zParses 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.
    Tr   zgcr.io/z/gcb-z:$COMMIT_SHA)default_imagehas_repo_sourcer   )r   GetMessagesModuler   ParseTriggerArgsForUpdater   VALUEScoreprojectGetr   ParseBuildConfigArgsForUpdatesubscription_filterfilter)	selfargsold_triggerupdate_maskmessagestriggerdoner2   r,   s	            r(   ParseTriggerFromFlagsz#UpdateWebhook.ParseTriggerFromFlagsR   s     002H!;;D(KMGTn$$,,00$0?G6=t||LM//#	 --GN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'                  ||	|
      }|j)                  |j*                        |_        d|
j/                  t        j0                  |             t3        t5        fd	|
D                    }
|j7                  |||d
j9                  |
            }|j                   j;                  |      }t=        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 webhook trigger.
    Tr   )
projectsIdlocationsId
triggersIdz&cloudbuild.projects.locations.triggers)params
collection)name	triggerId)secretsubstitutionsc              3   H   K   | ]  }|j                        rn|  y w)N)
startswith).0msubs     r(   	<genexpr>z$UpdateWebhook.Run.<locals>.<genexpr>   s"     A[1<<$C!+[s   ",)resourceNamerF   buildTrigger
updateMaskbuild_trigger)kind) r   GetClientInstancer.   r   r0   r1   r2   r3   buildsregionDEFAULT_REGIONr   r   REGISTRYParseRelativeNameprojects_locations_triggersMESSAGES_MODULE-CloudbuildProjectsLocationsTriggersGetRequestr>   WebhookConfigrG   webhookConfigextendMessageToFieldPathssortedset/CloudbuildProjectsLocationsTriggersPatchRequestjoinPatchr   UpdatedResource)r7   r8   clientr;   r2   
regionproplocation	triggeridrE   r9   r:   r<   requpdated_triggerrM   s                 @r(   RunzUpdateWebhook.Runt   s    ..0F002H$$,,00$0?G""))00446J{{JjJO,J,JHI##!##

 < $  ln 	 4488LL 	M 	
K K(({KHG$22$++2FG C::7CDA[AAK 
B
B88K(	 C C 88>>sCO	8r*   N)__name__
__module____qualname____doc__r!   r"   detailed_helpstaticmethodr)   r>   ro    r*   r(   r   r   !   sB    5 /(// # - , ,< D7r*   r   )rs   
__future__r   r   r   r!   !googlecloudsdk.api_lib.cloudbuildr   r   r   googlecloudsdk.callioper   %googlecloudsdk.command_lib.cloudbuildr	   (googlecloudsdk.command_lib.util.conceptsr
   googlecloudsdk.corer   r   r   UpdateCommandr   rv   r*   r(   <module>r~      sB    & &  '  = M ( ? D # * )JD&& Jr*   