
    Vh'                        d Z ddlmZ ddlZddlZddlZddlZddlZ	ddl
mZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZ d
dlmZmZmZ dZ G d de      Z G d de      Z G d d      ZddZddZy)z.Support code for working with Azure Pipelines.    )annotationsN   )to_bytes)CommonConfig
TestConfig)Git)
HttpClient)displayMissingEnvironmentVariable   )ChangeDetectionNotSupported
CIProviderCryptographyAuthHelperazpc                      e Zd ZdZddZedd       Zedd       Zedd       Z	ddZ
ddZddZdd	Zdd
ZddZddZy)AzurePipelinesz/CI provider implementation for Azure Pipelines.c                0    t               | _        d | _        y )N)AzurePipelinesAuthHelperauth_changesselfs    M/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/ci/azp.py__init__zAzurePipelines.__init__,   s    ,.	6:    c                 `    t         j                  j                  dd      j                  d      S )zMReturn True if this provider is supported in the current running environment.SYSTEM_COLLECTIONURI zhttps://dev.azure.com/)osenvironget
startswith r   r   is_supportedzAzurePipelines.is_supported1   s&     zz~~4b9DDE]^^r   c                    t         S )z0Return a unique code representing this provider.)CODEr   s    r   codezAzurePipelines.code6   s	     r   c                     y)z*Return descriptive name for this provider.zAzure Pipelinesr#   r   s    r   namezAzurePipelines.name;   s     !r   c                    dt         j                  d   dt         j                  d   dt         j                  d   }|S # t        $ r}t        |j                  d         dd}~ww xY w)	z6Return a resource prefix specific to this CI provider.zazp-BUILD_BUILDID-SYSTEM_JOBATTEMPTSYSTEM_JOBIDENTIFIERr   r)   N)r   r    KeyErrorr   args)r   prefixexs      r   generate_resource_prefixz'AzurePipelines.generate_resource_prefix@   se    	H

?+

./

12F   	H,"''!*=4G	Hs   >A 	A)
A$$A)c                @    | j                  |      j                  xs dS )z*Return the base commit or an empty string.r   )_get_changesbase_commitr   r1   s     r   get_base_commitzAzurePipelines.get_base_commitM   s      &228b8r   c                R    | j                   st        |      | _         | j                   S )zMReturn an AzurePipelinesChanges instance, which will be created on first use.)r   AzurePipelinesChangesr8   s     r   r6   zAzurePipelines._get_changesQ   s    }}1$7DM}}r   c                z   | j                  |      }|j                  rd}nd}t        j                  d|d|j                  d|j
                         |j                  j                  s%|j                  j                  |j                         |j                  t        j                  d       |j                  S )zInitialize change detection.zpull requestzmerge commitzProcessing z for branch z commit z7No successful commit found. All tests will be executed.)r6   is_prr
   infobranchcommitmetadatachangespopulate_changesdiffpathswarning)r   r1   resultjob_types       r   detect_changeszAzurePipelines.detect_changesX   s    ""4(<<%H%H&--Y_YfYfgh}}$$MM**6;;7<< OOUV||r   c                     y)z,Return True if Ansible Core CI is supported.Tr#   r   s    r   supports_core_ci_authz$AzurePipelines.supports_core_ci_authn   s    r   c                   	 t        t        j                  d   j                  d      j	                  d      d   t        j                  d   t        t        j                  d         t        t        j                  t        j                  d                     }| j                  j                  |       t        |      }|S # t        $ r}t        |j                  d   	      d
d
}~ww xY w)z2Return authentication details for Ansible Core CI.r   /SYSTEM_TEAMPROJECTr+   SYSTEM_TASKINSTANCEID)org_nameproject_namebuild_idtask_idr   r/   N)r   )dictr   r    stripsplitintstruuidUUIDr0   r   r1   r   sign_request)r   requestr3   r   s       r   prepare_core_ci_authz#AzurePipelines.prepare_core_ci_authr   s    	H$:;AA#FLLSQRTUZZ(<=RZZ89DIIbjj1H&IJK	G 			w'
   	H,"''!*=4G	Hs   BC 	C,C''C,c                j    | j                  |      }t        |j                  |j                        }|S )z4Return details about git in the current environment.)r7   r@   )r6   rU   r7   r@   )r   r1   rB   detailss       r   get_git_detailszAzurePipelines.get_git_details   s3    ##D)++>>

 r   N)returnNone)rb   bool)rb   rY   )r1   r   rb   rY   )r1   r   rb   r;   )r1   r   rb   zt.Optional[list[str]])rb   zdict[str, t.Any])r1   r   rb   zt.Optional[dict[str, t.Any]])__name__
__module____qualname____doc__r   staticmethodr$   propertyr'   r)   r4   r9   r6   rI   rK   r^   ra   r#   r   r   r   r   )   sk    9;
 _ _   ! !9,(	r   r   c                      e Zd ZdZddZy)r   z
    Authentication helper for Azure Pipelines.
    Based on cryptography since it is provided by the default Azure Pipelines environment.
    c                `   	 t         j                  d   }t        j                  ddd|      5 }|j                  t        |             |j                          ddd       t        d	d
j                         y# t        $ r}t        |j                  d         dd}~ww xY w# 1 sw Y   LxY w)zPublish the given public key.AGENT_TEMPDIRECTORYr   r/   Nzpublic-key-z.pemF)r2   suffixdeletedirzansible-core-cizpublic-key.pem)r   r    r0   r   r1   tempfileNamedTemporaryFilewriter   flushvso_add_attachmentr)   )r   public_key_pemagent_temp_directoryr3   public_key_files        r   publish_public_keyz+AzurePipelinesAuthHelper.publish_public_key   s    	H#%::.C#D  ((fUZ`tu 	$  zI!!(>":;!!#	$
 	,.>@T@TU  	H,"''!*=4G	H
	$ 	$s"   A9 +B$9	B!BB!$B-N)rv   rY   rb   rc   )re   rf   rg   rh   ry   r#   r   r   r   r      s    
Vr   r   c                  (    e Zd ZdZddZddZddZy)	r;   z0Change information for an Azure Pipelines build.c                T   || _         t               | _        	 t        j                  d   | _        t        j                  d   | _        t        j                  d   | _        t        j                  d   | _        t        j                  d   | _	        t        j                  j                  d      | _        | j                  j                  d	      rt        d
      | j
                  j!                  d      j#                  d      d   | _        | j                  d u| _        | j&                  r | j                  | _        d| _        d| _        n>| j/                         }| j                  | _        | j1                  |      | _        d| _        | j                  j3                  d| j,                  g      j!                         | _        | j*                  r| j                  j3                  d| j*                  g      j!                         | _        | j*                  d| j,                  }t5        | j                  j7                  |g            | _        | j                  j;                  |g      | _        y d | _        g | _        y # t        $ r}t        |j                   d         d d }~ww xY w)Nr   rO   BUILD_REPOSITORY_PROVIDERBUILD_SOURCEBRANCHBUILD_SOURCEBRANCHNAMESYSTEM_PULLREQUEST_TARGETBRANCHr   r/   z
refs/tags/z+Change detection is not supported for tags.rM   rN   zHEAD^1zHEAD^2HEADz	rev-parsez...)r1   r   gitr   r    org_uriproject	repo_typesource_branchsource_branch_namer!   pr_branch_namer0   r   r"   r   rV   rW   orgr=   r?   r7   r@    get_successful_merge_run_commitsget_last_successful_commitrun_gitsortedget_diff_namesrE   get_diffrD   )r   r1   r3   commits	dot_ranges        r   r   zAzurePipelinesChanges.__init__   s   	5	H::&<=DL::&:;DLZZ(CDDN!#,@!AD&(jj1I&JD#"$**..1R"SD ((6-.[\\<<%%c*005b9((4
::
 --DK'D"DK;;=G11DK#>>wGD DKhh&&T[['ABHHJ#xx//d>N>N0OPVVXD
 &*%5%5t{{CI 7 7 DEDJ))9+6DIDJDIM  	H,"''!*=4G	Hs   BI? ?	J'J""J'c           
        t        dddd| j                  | j                  d| j                        }| j                  | j                  dt
        j                  j                  |      }t        | j                  d	      }|j                  |      }	 |j                         }t        d |d   D              }|S # t        $ r" t        j                  d
       t               cY S w xY w)zEReturn a set of recent successful merge commits from Azure Pipelines.d   queueTimeDescending	succeeded	batchedCIrM   )maxBuildsPerDefinition
queryOrderresultFilterreasonFilterrepositoryTyperepositoryIdz$/_apis/build/builds?api-version=6.0&T)alwayszMUnable to find project. Cannot determine changes. All tests will be executed.c              3  &   K   | ]	  }|d      yw)sourceVersionNr#   ).0builds     r   	<genexpr>zIAzurePipelinesChanges.get_successful_merge_run_commits.<locals>.<genexpr>   s     JeO,Js   value)rU   r   r   r   r   urllibparse	urlencoder	   r1   r!   json	Exceptionr
   rF   set)r   
parametersurlhttpresponserG   r   s          r   r   z6AzurePipelinesChanges.get_successful_merge_run_commits   s    #&,$$>>$(HHdll;

 ?CllDLLZ`ZfZfZpZpq{Z|}$))D188C=	]]_F J&/JJ  	OOkl5L	s   B? ?(C*)C*c                    | j                   j                  d      }|D cg c]	  }||v s| }}|r|d   }|S d}|S c c}w )zcReturn the last successful commit from git history that is found in the given commit list, or None.r   )	max_countr   N)r   get_rev_list)r   r   commit_historyr@   ordered_successful_commitslast_successful_commits         r   r   z0AzurePipelinesChanges.get_last_successful_commit   s_    ...=;I%_VW^M^f%_"%_B\!;A!>%% cg%% &`s   	>>N)r1   r   rb   rc   )rb   set[str])r   r   rb   zt.Optional[str])re   rf   rg   rh   r   r   r   r#   r   r   r;   r;      s    :1f8&r   r;   c                4    t        dt        | |      |       y)z8Upload and attach a file to the current timeline record.ztask.addattachment)typer)   N)vsorU   )	file_type	file_namepaths      r   ru   ru     s    d		BDIr   c           
         t        j                  d| ddj                  d |j                         D              d|       y)z
    Write a logging command for the Azure Pipelines agent to process.
    See: https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash
    z##vso[ ;c              3  H   K   | ]  \  }}d j                  ||f        yw)=N)join)r   keyr   s      r   r   zvso.<locals>.<genexpr>  s"     3jzsTYCHHc5\4J3js    "]N)r
   r>   r   items)r)   datamessages      r   r   r     s1    
 LLT3883j]a]g]g]i3j+jlstur   )r   rY   r   rY   r   rY   rb   rc   )r)   rY   r   zdict[str, str]r   rY   rb   rc   ) rh   
__future__r   r   rq   rZ   typingturllib.parser   encodingr   configr   r   r   r   r   r	   utilr
   r   r   r   r   r   r&   r   r   r;   ru   r   r#   r   r   <module>r      s{    4 " 	    

  fZ fRV5 V.W& W&tJ
vr   