
                             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  G d de
j                        Zy)zCreate GitHub trigger command.    )absolute_import)division)unicode_literals)cloudbuild_util)trigger_config)base)log)
properties)	resourcesc                   4    e Zd ZdZddiZed        Zd Zd Zy)CreateGitHubz/Create a build trigger for a GitHub repository.EXAMPLESa              To create a push trigger with a 1st-gen repository for all branches:

              $ {command} --name="my-trigger" --service-account="projects/my-project/serviceAccounts/my-byosa@my-project.iam.gserviceaccount.com" --repo-owner="GoogleCloudPlatform" --repo-name="cloud-builders" --branch-pattern=".*" --build-config="cloudbuild.yaml"

            To create a pull request trigger with a 1st-gen repository for master:

              $ {command} --name="my-trigger" --service-account="projects/my-project/serviceAccounts/my-byosa@my-project.iam.gserviceaccount.com" --repo-owner="GoogleCloudPlatform" --repo-name="cloud-builders" --pull-request-pattern="^master$" --build-config="cloudbuild.yaml"

            To create a pull request trigger with a 2nd gen repository for master:

              $ {command} --name="my-trigger"  --repository=projects/my-project/locations/us-central1/connections/my-conn/repositories/my-repo --pull-request-pattern="^master$" --build-config="cloudbuild.yaml" --region=us-central1

          c                    t        j                  |       }|j                  d      }|j                  dd       |j	                  d      }|j                  ddd	       |j                  d
dd	       |j                  dd       |j                  d      }t        j
                  |       t        j                  |       |j	                  d      }|j                  dddd       t        j                  |       t        j                  |       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.
    Trequiredz--repositoryzkRepository resource (2nd gen) to use, in the format
"projects/*/locations/*/connections/*/repositories/*".
)helpz1st-gen repository settings.z--repo-ownerz)Owner of the GitHub Repository (1st gen).)r   r   z--repo-namez(Name of the GitHub Repository (1st gen).z--enterprise-configzResource name of the GitHub Enterprise config that should be applied to this
installation.

For example: "projects/{$project_id}/locations/{$location_id}/githubEnterpriseConfigs/{$config_id}
        zPull Request settingsz--pull-request-patternREGEXa  A regular expression specifying which base git branch to match for
pull request events.

This pattern is used as a regex search for the base branch (the branch you are
trying to merge into) for pull request updates.
For example, --pull-request-pattern=foo will match "foo", "foobar", and "barfoo".

The syntax of the regular expressions accepted is the syntax accepted by
RE2 and described at https://github.com/google/re2/wiki/Syntax.
)metavarr   r   N)trigger_utilsAddTriggerArgsadd_mutually_exclusive_groupadd_argumentadd_argument_groupAddBranchPatternAddTagPatternAddCommentControlArgAddBuildConfigArgsAddRepoEventArgsAddIncludeLogsArgs)parserflag_config
gen_config	v1_config
ref_config	pr_configs         ,lib/surface/builds/triggers/create/github.pyArgszCreateGitHub.Args2   sS     ..v6K9949HJ   --+ . -I8   7      9949HJ"":.
+--3J-KI 
	   &&y1$$[1"";/$$[1    c                    t         j                  j                  j                  j	                  d      }t        j                         }t        j                  ||      \  }}|r|S |j                  rP|j                  rD|j                  |j                  |j                  |j                        |_        |j                  }n-|j                  |j                        |_        |j                   }|j"                  rb|j%                  |j"                        |_        |j(                  ra|j$                  j+                  |j(                        |j&                  _        n,|j/                  |j0                  |j2                        |_        d|d|j                  d	|j                  d
}t        j6                  ||||       t        j8                  ||       t        j:                  |||       |S )a  Parses command line arguments into a build trigger.

    Args:
      args: An argparse arguments object.

    Returns:
      A build trigger object.

    Raises:
      RequiredArgumentException: If comment_control is defined but
      pull_request_pattern isn't.
    Tr   )ownernameenterpriseConfigResourceName)
repository)branch)r.   tagzgcr.io/z/github.com//z:$COMMIT_SHA)r
   VALUEScoreprojectGetr   GetMessagesModuler   ParseTriggerArgs
repo_owner	repo_nameGitHubEventsConfigenterprise_configgithubRepositoryEventConfigr-   repositoryEventConfigpull_request_patternPullRequestFilterpullRequestcomment_controlCommentControlValueValuesEnumcommentControl
PushFilterbranch_patterntag_patternpushParseBuildConfigArgsParseRepoEventArgsParseIncludeLogsWithStatus)selfargsr3   messagestriggerdonercfgdefault_images           r&   ParseTriggerFromFlagsz"CreateGitHub.ParseTriggerFromFlagsp   s    $$,,00$0?G002H!224BMGTn4>>22~~'+'='= 3 ?gn ^^d&.&D&D__ 'E '&g#**d  !33** 4 ,d			&&DD$$ 	' %%$$$*:*: & <di 	$..2M&&whN$$Wd3,,WdHENr(   c           	         t        j                         }t        j                         }| j                  |      }t        j
                  j                  j                  j                  d      }t        j
                  j                  j                  j                         }|j                  xs |xs t         j                  }t        j                  j                  d||      j                         }|j                   j                  |j#                  ||            }	t        j                  j%                  ddd|||	j&                  d	
      }
t)        j*                  |
       |	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:
      Some value that we want to have printed later.
    Tr   zcloudbuild.projects.locations)
collection
projectsIdlocationsId)parentbuildTriggerNz&cloudbuild.projects.locations.triggersv1)rU   rV   
triggersId)rT   api_versionparams)r   GetClientInstancer5   rR   r
   r1   r2   r3   r4   buildsregionDEFAULT_REGIONr   REGISTRYCreateRelativeNameprojects_locations_triggers0CloudbuildProjectsLocationsTriggersCreateRequestParseidr	   CreatedResource)rK   rL   clientrM   rN   r3   
regionproplocationrW   created_triggertrigger_resources              r&   RunzCreateGitHub.Run   sC    ..0F002H((.G $$,,00$0?G""))00446J{{JjJO,J,JH&&2 '  +ln  88??AA 	B 	12O !))//;!#),,
	 0  ()r(   N)	__name__
__module____qualname____doc__detailed_helpstaticmethodr'   rR   rn    r(   r&   r   r      s6    7 -$ ;2 ;2z3j'r(   r   N)rr   
__future__r   r   r   !googlecloudsdk.api_lib.cloudbuildr   r   r   googlecloudsdk.callioper   googlecloudsdk.corer	   r
   r   CreateCommandr   ru   r(   r&   <module>r{      s9    % &  ' = M ( # * )o4%% or(   