U
    ޣ5hF                     @   sN   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	 G dd dZ
dS )    N)BeautifulSoup)AZURE_DEVOPS_URLAZURE_DEVOPS_ORGAZURE_DEVOPS_PROJECTAZURE_DEVOPS_PATc                   @   s    e Zd ZdddZdddZdS )AzureClientNc                 C   s   |r| dtnt| _|r&| dtnt| _|r<| dtnt| _|rR| dtnt| _| jr| j	dsvd| j | _| j
d| _d S )NurlorgZprojectpat)zhttp://https://r   /)getr   	azure_urlr   	azure_orgr   azure_projectr   	azure_pat
startswithrstrip)selfazure_config r   K/var/www/html/testcasegenerator.evdpl.com/azure_integration/azure_client.py__init__   s    zAzureClient.__init__c                 C   s  |s$t ddd}dd |D }|s4td d S | jsFtd d S | jsXtd d S | jsjtd	 d S g }|D ] }| j d
| j d
| j d| d}ddtd| j	 
   d}ztj||d}|jdkr>| }dd }|di dd}	||	}
|di dd}||||
d td|  ntd| d|j  W qr tk
r } ztd| dt|  W 5 d }~X Y qrX qr|S )NZAZURE_DEVOPS_WORKITEM_IDS ,c                 S   s   g | ]}|  r|  qS r   )strip).0idr   r   r   
<listcomp>   s      z6AzureClient.fetch_azure_work_items.<locals>.<listcomp>uW   ⚠️ Work item IDs not found. Please set AZURE_DEVOPS_WORKITEM_IDS in your .env file.u$   ❌ Azure DevOps URL cannot be emptyu-   ❌ Azure DevOps organization cannot be emptyu(   ❌ Azure DevOps project cannot be emptyr   z/_apis/wit/workitems/z?api-version=6.0zapplication/jsonzBasic :)AcceptAuthorization)headers   c                 S   s   t | d}| S )Nzhtml.parser)r   Zget_text)textZsoupr   r   r   
clean_html<   s    
z6AzureClient.fetch_azure_work_items.<locals>.clean_htmlfieldszSystem.DescriptionzNo Description FoundzSystem.TitlezNo Title Found)r   titledescriptionu#   ✅ Successfully fetched work item u   ❌ Failed to fetch work item z: u   ❌ Error processing work item )osgetenvsplitprintr   r   r   base64	b64encoder   encodedecoderequestsr   status_codejsonappend	Exceptionstr)r   Zwork_item_idsresultsZwork_item_idr   r"   response	work_itemr%   r(   Zdescription_cleanedr'   er   r   r   fetch_azure_work_items   sN    
",z"AzureClient.fetch_azure_work_items)N)N)__name__
__module____qualname__r   r;   r   r   r   r   r      s   
r   )r)   r1   r-   Zbs4r   config.settingsr   r   r   r   r   r   r   r   r   <module>   s
   