
    Wyi,              
          S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	J	r	  SSK
JrJr  SSKJr   SSKJr  SS	KJr  SS
KJr  \R,                  " \R.                  S\R0                  " S5      \R2                  " \R4                  5      /S9  \R6                  " \5      rSS\S\S\S\\   4S jjrS\S\S\S\4S jr S r!\S:X  a  \!" 5         gg! \ a    SSKJr  SS	KJr  SS
KJr   Nf = f)zU
Script to check if top 20 active repositories are checked out and have latest pull.
    N)Path)ListDictTuple)datetime)RepoGitCommandError)InvalidGitRepositoryError)ConfigManager)BitbucketClient)
GitManager   z4%(asctime)s - %(name)s - %(levelname)s - %(message)sztop_repos_check.log)levelformathandlers	bitbucketconfiglimitreturnc                    [         R                  S5        U R                  5       =(       d    / nU(       d  [         R                  S5        / $ [         R                  S[	        U5       SU S35        / nU GH^  nUR                  S5      =(       d    UR                  S5      nU(       d  M6  UR                  S0 5      R                  S5      nXqR                  S	5      :w  a  Mm   U R                  U5      nU(       a  UR                  S
5      (       a   UR                  S
5      n	[        R                  " U	R                  SS5      5      n
U
R                  (       a  U
R                  SS9n
UR                  X45        [         R                  SU SU
 35        GM$  [         R                  SU S35        UR                  [        R                  U45        GMa     UR                  S SS9  USU  VVs/ s H  u  pUPM	     nnn[         R                  S[	        U5       S35        U$ ! [         aH  n[         R                  SU SU 35        UR                  [        R                  U45         SnAGM  SnAff = f! [         aH  n[         R                  SU SU 35        UR                  [        R                  U45         SnAGMS  SnAff = fs  snnf )a  
Get top N repositories sorted by most recent commit date.

Args:
    bitbucket: BitbucketClient instance
    config: ConfigManager instance
    limit: Maximum number of repos to return
    
Returns:
    List of top N repositories sorted by commit date (most recent first)
z+Fetching all repositories from Bitbucket...z"No repositories found in workspacezFound z- repositories, fetching commit dates for top ...nameslug	workspacebitbucket.workspacedateZz+00:00N)tzinfozFound commit date for : z Failed to parse commit date for z Could not fetch commit date for z, using fallbackzError fetching commit date for c                     U S   $ )Nr    )xs    /C:\wamp64\www\sonar_scan\src\check_top_repos.py<lambda>&get_top_active_repos.<locals>.<lambda>_   s    !    T)keyreversez	Selected z& repositories with most recent commits)loggerinfoget_all_repositorieswarninglengetget_latest_commitr   fromisoformatreplacer   appenddebug	Exceptionminsort)r   r   r   	all_reposrepos_with_dates	repo_info	repo_slugworkspace_sluglatest_commitcommit_date_strcommit_datee_selected_reposs                 r#   get_top_active_reposrB   #   s    KK=?..06BI;<	
KK&Y((UV[U\\_`a46	MM&)BY]]6-B	 #{B7;;FCZZ(=>>	?%77	BM!2!26!:!:G&3&7&7&?O"*"8"89P9PQTV^9_"`K"))&1&9&9&9&F$++[,DELL#9)B{m!TU !A)L\]^ ''y(AB? J nd; 5EVe4LM4LLAi4LNM
KK)C/00VWX% ! GNN%Ei[PRSTRU#VW$++X\\9,EFFG  	?NN<YKr!MN##X\\9$=>>	? NsI   $.I<BH':I<2K'
I91<I4-I<4I99I<<
K<K		Kr9   	repos_dirc                    U R                  S5      =(       d    U R                  S5      nX-  nUSSSSSS.nUR                  5       (       d  SUS'   U$ US-  R                  5       (       d  S	US'   U$ S
US'    [        U5      n UR                  U5      nU(       a  UR                  S5      US'    UR                  SS9n	U	R                  5          UR                  R                  n
UR                  R                  R                  US'   SU
 3nXR                  ;   a0  UR                  U   R                  R                  nUS   U:H  US'   U$ US   US   :H  US'    U$ ! [         a/  n[
        R                  SU SU 35        SU 3US'   Us SnA$ SnAff = f! [         a.  n[
        R                  SU SU 35        SU 3US'    SnAU$ SnAff = f! [         a.  n[
        R                  SU SU 35        SU 3US'    SnAU$ SnAff = f! [         a	    SUS'    U$ [         a  nSU 3US'    SnAU$ SnAff = f)z
Check if repository is checked out and up to date.

Args:
    repo_info: Repository information dictionary
    repos_dir: Path to repositories directory
    bitbucket: BitbucketClient instance
    
Returns:
    Dictionary with status information
r   r   FN)r:   checked_out
up_to_datelocal_commitremote_commiterrorz5Repository not checked out (directory does not exist)rI   z.gitz.Repository not checked out (no .git directory)TrE   hashrH   z"Could not fetch remote commit for r   zCould not fetch remote commit: origin)r   rG   zorigin/rF   zCould not get local commit for zCould not get local commit: z Error checking local status for zError checking local status: zInvalid git repositoryUnexpected error: )r.   existsr   r/   r4   r)   r,   remotefetchactive_branchr   headcommithexsharefsr
   )r9   rC   r   r:   	repo_pathstatusrepor<   r?   rK   current_branch
remote_refrH   s                r#   check_repo_statusrZ   h   s    f%>v)>I%I F Qw&&((Jw F=,3I	%77	BM*7*;*;F*C'	B[[h[/FLLNE!%!3!3!8!8)-)9)9)@)@~&  '~&67
*$(IIj$9$@$@$G$GM,2>,Bm,SF<(  M -3>,Bf_F],]F<( MK  	NN?	{"QCPQ ?sCF7OM	2  E!@2aSQR$@"Dw ME  	BNN=i[1#NO =aSAF7O M	B % 32w M  3.qc2wM3s   8H ,E 1G A;F F 
F)$FFH FH 
G%#GG GG 
H #H	H 	HH H?#	H?,H::H?c            
      x
   [         R                  S5        [         R                  S5        [         R                  S5         [        5       n U R                  S5      (       d+  [         R	                  S5        [
        R                  " S5        [        U R                  S5      5      n[        U R                  S5      5      nU R                  S5      (       d+  [         R	                  S	5        [
        R                  " S5        U(       d2  U(       d+  [         R	                  S
5        [
        R                  " S5        [        U R                  S5      U R                  S5      U(       d  U R                  S5      OSU R                  S5      S9n[        U R                  S5      5      n[        X0SS9nU(       d  [         R                  S5        g[         R                  S5        [         R                  S[        U5       S35        [         R                  S5        / nSnSnSn	Sn
[        US5       GH:  u  pUR                  S5      =(       d    UR                  S5      n[         R                  SU S[        U5       SU S35        [        XU5      nUR                  U5        US   (       a  US-  nUS   (       a  US-  n[         R                  S5        M  U	S-  n	[         R                  S5        [         R                  SUS   (       a  US   SS  OS! 35        [         R                  S"US#   (       a  US#   SS  OS! 35        GM  U
S-  n
[         R	                  S$US%    35        GM=     [         R                  S&5        [         R                  S'5        [         R                  S5        [         R                  S([        U5       35        [         R                  S)U 35        [         R                  S*U	 35        [         R                  S$U
 35        [         R                  S5        U	S:  d  U
S:  as  [         R                  S+5        U HX  nUS   (       d#  [         R                  S,US-    S.US%    35        M0  US   (       a  M<  [         R                  S/US-    S035        MZ     U
S:  d  U	S:  a  [
        R                  " S5        g[         R                  S15        [
        R                  " S5        g! [          a7  n[         R	                  S2U 3S3S49  [
        R                  " S5         SnAgSnAff = f)5z2Main function to check top 20 active repositories.z<============================================================z#Checking Top 20 Active Repositoriesr   z"Bitbucket workspace not configuredr   zbitbucket.api_tokenzbitbucket.app_passwordzbitbucket.usernamez'Bitbucket username/email not configuredz'Bitbucket authentication not configuredN)r   usernameapp_password	api_tokenzscanning.repos_dir   )r   zNo repositories foundzChecking status of z repositories...r   r   r   z
[/z] Checking r   rE   rF   u      ✓ Checked out and up to dateu"     ⚠ Checked out but needs updatez    Local:  rG      zN/Az    Remote: rH   u     ✗ Not checked out: rI   z=
============================================================SummaryzTotal repositories checked: u"     ✓ Checked out and up to date: u$     ⚠ Checked out but needs update: z 
Repositories needing attention:u     ✗ r:   r   u     ⚠ z: Needs updateu<   
✓ All top 20 repositories are checked out and up to date!rL   T)exc_info)r)   r*   r   r.   rI   sysexitboolr   r   rB   r,   r-   	enumeraterZ   r2   r4   )r   has_api_tokenhas_app_passwordr   rC   	top_reposresultschecked_out_countup_to_date_countneeds_update_countnot_checked_out_countir9   r:   rV   r?   s                   r#   mainrq      sB   
KK
KK56
KKa zz/00LL=>HHQKVZZ(=>?

+C DEzz.//LLBCHHQK%5LLBCHHQK $jj!67ZZ 45ER$<=X\jj!67	
	 $89:	 )"E	NN23H)#i.)99IJKH  !%i3LA!f-Fv1FIKK#aS#i.!1YKsKL&yYGFNN6"m$!Q&!,'$)$KK"BD&!+&NN%GINN\PVWePf&2H!2Llq1r#stNN\QWXgQh&2I"12Mns1t#uv%*%6vg6GHI' 4, 	O$IH23y>2BCD89I8JKL:;M:NOP-.C-DEFH !%:Q%>KK;<!m,KK&)<(=Rw?P QR--KK&)<(=^ LM	 " !1$(:Q(>HHQKKKWXHHQK )!-=s-   F
S8 J9S8 	AS8 +S8 8
T9-T44T9__main__)r_   )"__doc__rd   loggingpathlibr   typingr   r   r   r   gitr   r	   git.excr
   config_managerr   bitbucket_clientr   git_managerr   ImportErrorbasicConfigINFOFileHandlerStreamHandlerstdout	getLogger__name__r)   intrB   rZ   rq   r!   r&   r#   <module>r      s      $ $  % -(,0&   
,,A12cjj) 
		8	$BO B] BSV B`dei`j BJQ Q$ Q? QW[ QhgT zF k  (-1'(s   C C('C(