o nnh'@sdZddlmZddlZddlZddlZddlZddlZ ddl m Z ddl m Z mZddlmZddlmZdd lmZmZd d lmZmZmZd ZGd ddeZGdddeZGdddZd!ddZd"dd ZdS)#z.Support code for working with Azure Pipelines.) annotationsN)to_bytes) CommonConfig TestConfig)Git) HttpClient)displayMissingEnvironmentVariable)ChangeDetectionNotSupported CIProviderCryptographyAuthHelperazpc@seZdZdZd$ddZed%ddZed&d d Zed&d d Z d&ddZ d'ddZ d(ddZ d)ddZ d%ddZd*ddZd+d!d"Zd#S),AzurePipelinesz/CI provider implementation for Azure Pipelines.returnNonecCst|_d|_dS)N)AzurePipelinesAuthHelperauth_changesselfrH/usr/local/lib/python3.10/dist-packages/ansible_test/_internal/ci/azp.py__init__,s zAzurePipelines.__init__boolcCstjdddS)zMReturn True if this provider is supported in the current running environment.SYSTEM_COLLECTIONURIzhttps://dev.azure.com/)osenvironget startswithrrrr is_supported1szAzurePipelines.is_supportedstrcCstS)z0Return a unique code representing this provider.)CODErrrrcode6zAzurePipelines.codecCdS)z*Return descriptive name for this provider.zAzure Pipelinesrrrrrname;r&zAzurePipelines.namec CsRzdtjdtjdtjdf}W|Sty(}z t|jdddd}~ww)z6Return a resource prefix specific to this CI provider.z azp-%s-%s-%s BUILD_BUILDIDSYSTEM_JOBATTEMPTSYSTEM_JOBIDENTIFIERrr(N)rrKeyErrorr args)rprefixexrrrgenerate_resource_prefix@sz'AzurePipelines.generate_resource_prefixr.rcCs||jpdS)z*Return the base commit or an empty string.r) _get_changes base_commitrr.rrrget_base_commitMszAzurePipelines.get_base_commitAzurePipelinesChangescCs|jst||_|jS)zMReturn an AzurePipelinesChanges instance, which will be created on first use.)rr6r4rrrr2Qs zAzurePipelines._get_changesrt.Optional[list[str]]cCsb||}|jr d}nd}td||j|jf|jjs$|j|j |j dur.t d|j S)zInitialize change detection.z pull requestz merge commitz%Processing %s for branch %s commit %sNz7No successful commit found. All tests will be executed.) r2is_prr infobranchcommitmetadatachangespopulate_changesdiffpathswarning)rr.resultjob_typerrrdetect_changesXs   zAzurePipelines.detect_changescCr')z,Return True if Ansible Core CI is supported.Trrrrrsupports_core_ci_authnsz$AzurePipelines.supports_core_ci_authdict[str, t.Any]c Csz%ttjddddtjdttjdtttjdd}Wnt y:}z t |j dd d d }~ww|j |t|d }|S) z2Return authentication details for Ansible Core CI.r/SYSTEM_TEAMPROJECTr)SYSTEM_TASKINSTANCEID)org_name project_namebuild_idtask_idrr,N)r)dictrrstripsplitintr#uuidUUIDr-r r.r sign_request)rrequestr0rrrrprepare_core_ci_authrs    z#AzurePipelines.prepare_core_ci_autht.Optional[dict[str, t.Any]]cCs||}t|j|jd}|S)z4Return details about git in the current environment.)r3r;)r2rOr3r;)rr.r=detailsrrrget_git_detailss zAzurePipelines.get_git_detailsN)rr)rr)rr#)r.rrr#)r.rrr6)r.rrr7)rrF)r.rrrX)__name__ __module__ __qualname____doc__r staticmethodr"propertyr%r(r1r5r2rDrErWrZrrrrr)s          rc@seZdZdZd ddZdS) rz Authentication helper for Azure Pipelines. Based on cryptography since it is provided by the default Azure Pipelines environment. public_key_pemr#rrc Csztjd}Wnty}z t|jdddd}~wwtjddd|d}|t|| Wdn1sszIAzurePipelinesChanges.get_successful_merge_run_commits..value)rOrvrzrurturllibparse urlencoderr.r json Exceptionr rAset)r parametersurlhttpresponserBrrrrr{s&      z6AzurePipelinesChanges.get_successful_merge_run_commitsrt.Optional[str]cs8|jjdd}fdd|D}|r|d}|Sd}|S)zcReturn the last successful commit from git history that is found in the given commit list, or None.r) max_countcsg|]}|vr|qSrr)rr;rrr szDAzurePipelinesChanges.get_last_successful_commit..rN)rs get_rev_list)rrcommit_historyordered_successful_commitslast_successful_commitrrrr|s  z0AzurePipelinesChanges.get_last_successful_commitN)r.rrr)rr)rrrr)r[r\r]r^rr{r|rrrrr6s   3r6 file_typer# file_namepathrrcCstdt||d|dS)z8Upload and attach a file to the current timeline record.ztask.addattachment)typer(N)vsorO)rrrrrrrjsrjr(datadict[str, str]messagec Cs,td|ddd|D|fdS)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[%s %s]%s;css"|] \}}d||fVqdS)=N)join)rkeyrrrrr s zvso..N)r r9ritems)r(rrrrrrs,r)rr#rr#rr#rr)r(r#rrrr#rr) r^ __future__rrrfrStypingt urllib.parserencodingrconfigrrrsrrrutilr r rr r rr$rrr6rjrrrrrs&    i Z