
    i                       d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZ dd	lmZ  ed
dg          Z ej        e          Zd dZd!dZe                    dddde          d"d            ZdS )#uC  
Nom du fichier : groups.py
Chemin : /var/www/html/gitlab-bridge/app/api/v1/groups.py
Description : Endpoints API de recherche de groupes GitLab compatibles OpenWebUI.
Options éventuelles :
    - POST /api/v1/groups/search
Exemples d'utilisation :
    - curl -X POST "https://gitlab-bridge.tarbouriech.tech/api/v1/groups/search"       -H "Content-Type: application/json"       -d '{
            "query": "developpements",
            "root_hint": "si",
            "limit": 10
          }'
Auteur : Sylvain SCATTOLINI
Date de création / modification : 2026-03-26
Version : 1.2
    )annotationsN)SequenceMatcher)	APIRouterHTTPExceptionstatus)GitLabClient)GitLabApiError)
GroupMatchGroupSearchRequestGroupSearchResponse)normalize_textz/api/v1/groupsgroups)prefixtagsquerystr
group_name	full_pathreturnintc                P   t          |           }t          |          }t          |          }||k    rdS ||v rdS ||v rdS t          d ||                                          }t          d ||                                          }t          t	          ||          dz            S )Nd   _   Z   )r   r   ratior   max)r   r   r   normalized_querynormalized_namenormalized_path
ratio_name
ratio_paths           0/var/www/html/gitlab-bridge/app/api/v1/groups.py_compute_scorer#   '   s    %e,,$Z00O$Y//O?**s?**r?**r '7IIOOQQJ '7IIOOQQJs:z**S0111    	root_hint
list[dict]c                   t                      }	 |                    d          }n1# t          $ r$}t          dt	          |                    |d}~ww xY w| sd |D             S t          |           fd|D             S )uM   Retourne tous les groupes accessibles (toutes pages), filtrés par root_hint.T)all_availablei  status_codedetailNc                <    g | ]}t          |t                    |S  )
isinstancedict).0gs     r"   
<listcomp>z _list_groups.<locals>.<listcomp>A   s'    999aZ4%8%89999r$   c           
         g | ]\}t          |t                    t          t          |                    d d                                                            Z|]S )r    )r.   r/   r   r   get
startswith)r0   r1   normalized_roots     r"   r2   z _list_groups.<locals>.<listcomp>D   so       a 3quu["556677BB?SS	  r$   )r   list_groupsr	   r   r   r   )r%   clientr   excr7   s       @r"   _list_groupsr;   8   s    ^^FG##$#77 G G GCHH===3FG  :9969999$Y//O      s   ( 
AAAz/searchsearch_groupszRechercher des groupes GitLabuH   Recherche des groupes GitLab pertinents à partir d'un libellé naturel.)operation_idsummarydescriptionresponse_modelpayloadr   r   c                   	 | j                                         }| j                                                            d          }t          | j                  }t
                              dt          j        |||dd                     t          |          }g }|D ]}t          |t          |                    dd                    t          |                    dd                              }|d	k     r\|                    t          t          |d
                   t          |d                   t          |d                   |                     |                    d            t!          d||d |                   S # t"          $ r  t$          $ rI}t
                              dt          |                     t#          t(          j        d          |d }~ww xY w)N/u,   Payload reçu sur /api/v1/groups/search : %s)r   r%   limitF)ensure_asciinamer4   r   (   id)rH   rF   r   scorec                     | j          | j        fS )N)rI   r   )items    r"   <lambda>zsearch_groups.<locals>.<lambda>t   s    tzk4>-J r$   )keyT)successr   matchesz)Erreur inattendue dans search_groups : %sz/Erreur interne lors de la recherche de groupes.r)   )r   stripr%   r   rD   loggerinfojsondumpsr;   r#   r   r5   appendr
   sortr   r   	Exception	exceptionr   HTTP_500_INTERNAL_SERVER_ERROR)	rA   
safe_querysafe_root_hint
safe_limitr   scored_matchesgrouprI   r:   s	            r"   r<   r<   K   s   0]((**
 *002288==''
:J$>JWW"  	
 	
 	
 n--+- 	 	E"EIIfb))**EIIk2..// E
 rzz!!5;''U6]++!%"455	      	 J JKKK"";J;/
 
 
 	
       Dc#hhOOO=D
 
 
 	s   FF G+"AG&&G+)r   r   r   r   r   r   r   r   )r%   r   r   r&   )rA   r   r   r   )__doc__
__future__r   rS   loggingdifflibr   fastapir   r   r   app.clients.gitlab_clientr   app.core.exceptionsr	   app.schemas.groupsr
   r   r   app.utils.helpersr   router	getLogger__name__rQ   r#   r;   postr<   r-   r$   r"   <module>rl      sY   & # " " " " "   # # # # # # 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 . . . . . . R R R R R R R R R R , , , , , ,	*(	<	<	<		8	$	$2 2 2 2"   &  +Z&   1 1 1 1 1 1r$   