
    6                         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 Bitbucket Server 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)CreateBitbucketServerz9Create a build trigger for a Bitbucket Server repository.EXAMPLESaY              To create a push trigger for all branches:

              $ {command} --name="my-trigger" --service-account="projects/my-project/serviceAccounts/my-byosa@my-project.iam.gserviceaccount.com" --project-key="GoogleCloudPlatform" --repo-slug="cloud-builders" --bitbucket-server-config-resource="projects/1234/locations/global/bitbucketServerConfigs/5678" --branch-pattern=".*" --build-config="cloudbuild.yaml"

            To create a pull request trigger for main:

              $ {command} --name="my-trigger" --service-account="projects/my-project/serviceAccounts/my-byosa@my-project.iam.gserviceaccount.com" --project-key="GoogleCloudPlatform" --repo-slug="cloud-builders" --bitbucket-server-config-resource="projects/1234/locations/global/bitbucketServerConfigs/5678" --pull-request-pattern="^main$" --build-config="cloudbuild.yaml"
          c                    t        j                  |       }|j                  ddd       |j                  ddd       |j                  ddd       |j                  d	      }t        j                  |       t        j
                  |       |j                  d
      }|j                  dddd       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.
    z--repo-slugz!Bitbucket Server repository slug.T)helprequiredz--project-keyzBitbucket Server project key.z"--bitbucket-server-config-resourcez&Bitbucket Server config resource name.r   zPull Request settings)r   z--pull-request-patternREGEXa  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_argumentadd_mutually_exclusive_groupAddBranchPatternAddTagPatternadd_argument_groupAddCommentControlArgAddBuildConfigArgsAddRepoEventArgs)parserflag_config
ref_config	pr_configs       5lib/surface/builds/triggers/create/bitbucketserver.pyArgszCreateBitbucketServer.Args-   s      ..v6K?$  P=  N,5   9949HJ"":.
+--3J-KI 
	   &&y1$$[1"";/    c                    t         j                  j                  j                  j	                  d      }t        j                         }t        j                  ||      \  }}|r|S |j                  |j                  |j                  |j                        }|j                  rb|j                  |j                        |_        |j                   ra|j                  j#                  |j                         |j                  _        n,|j'                  |j(                  |j*                        |_        ||_        d|d|j                  d|j                  d	}t        j0                  ||||       t        j2                  ||       |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   )repoSlug
projectKeybitbucketServerConfigResource)branch)r*   tagzgcr.io/z/bitbucketserver-/z:$COMMIT_SHA)r
   VALUEScoreprojectGetr   GetMessagesModuler   ParseTriggerArgsBitbucketServerTriggerConfig	repo_slugproject_key bitbucket_server_config_resourcepull_request_patternPullRequestFilterpullRequestcomment_controlCommentControlValueValuesEnumcommentControl
PushFilterbranch_patterntag_patternpushbitbucketServerTriggerConfigParseBuildConfigArgsParseRepoEventArgs)selfargsr/   messagestriggerdonebbsdefault_images           r#   ParseTriggerFromFlagsz+CreateBitbucketServer.ParseTriggerFromFlagsU   s_    $$,,00$0?G002H!224BMGTn 
/
/##&*&K&K 0 C
    22** 3 co 
		&&DD$$ 	& $$$$$*:*: % ch ,/G( 	M
 &&whN$$Wd3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)rN   rO   
triggersId)rM   api_versionparams)r   GetClientInstancer1   rK   r
   r-   r.   r/   r0   buildsregionDEFAULT_REGIONr   REGISTRYCreateRelativeNameprojects_locations_triggers0CloudbuildProjectsLocationsTriggersCreateRequestParseidr	   CreatedResource)rD   rE   clientrF   rG   r/   
regionproplocationrP   created_triggertrigger_resources              r#   RunzCreateBitbucketServer.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$   rK   rg    r%   r#   r   r      s6    A - %0 %0N3j'r%   r   N)rk   
__future__r   r   r   !googlecloudsdk.api_lib.cloudbuildr   r   r   googlecloudsdk.callioper   googlecloudsdk.corer	   r
   r   CreateCommandr   rn   r%   r#   <module>rt      s9    / &  ' = M ( # * )TD.. Tr%   