|
6 | 6 | Consolidação de múltiplos repositórios na mesma saída |
7 | 7 | """ |
8 | 8 |
|
| 9 | +import os |
9 | 10 | import subprocess |
10 | 11 | import json |
11 | 12 | from datetime import datetime |
12 | 13 | from pathlib import Path |
13 | 14 | from typing import Dict, List, Optional, Tuple |
14 | 15 | import re |
15 | 16 |
|
| 17 | +try: |
| 18 | + from issue_keys import wsl_path_for_repo |
| 19 | +except ImportError: |
| 20 | + def wsl_path_for_repo(repo: str) -> str: |
| 21 | + paths = { |
| 22 | + "contratos_v2": "/root/MGI/contratos_v2", |
| 23 | + "contratos": "/root/MGI/contratos", |
| 24 | + } |
| 25 | + return paths.get(repo, "/root/MGI/contratos_v2") |
| 26 | + |
| 27 | +WSL_DISTRO = os.environ.get("MGI_WSL_DISTRO", "Ubuntu") |
| 28 | + |
| 29 | + |
16 | 30 | class GitColeta: |
17 | 31 | def __init__(self, repo_path: str, repo_name: Optional[str] = None): |
18 | 32 | self.repo_path = Path(repo_path) |
19 | 33 | self.repo_name = repo_name or Path(repo_path).name |
| 34 | + self.wsl_repo_path = wsl_path_for_repo(self.repo_name) |
20 | 35 | self.data: Dict = { |
21 | 36 | 'timestamp': datetime.now().isoformat(), |
22 | 37 | 'repositorio': self.repo_name, |
23 | 38 | 'caminho': str(repo_path), |
| 39 | + 'wsl_caminho': self.wsl_repo_path, |
24 | 40 | 'commits': [], |
25 | 41 | 'branches': [], |
26 | 42 | 'releases': [], |
27 | 43 | 'stats': {} |
28 | 44 | } |
29 | 45 |
|
30 | | - def run_git(self, cmd: str, timeout: int = 30) -> str: |
31 | | - """Executa comando git no repositório. |
32 | | -
|
33 | | - Verifica o returncode e loga stderr para evitar falhas silenciosas. |
34 | | - """ |
| 46 | + def _run_git(self, git_args: str, timeout: int = 30) -> str: |
| 47 | + """Executa git dentro do WSL Ubuntu (repos em /root/MGI/...).""" |
| 48 | + cmd = [ |
| 49 | + "wsl", |
| 50 | + "-d", |
| 51 | + WSL_DISTRO, |
| 52 | + "bash", |
| 53 | + "-lc", |
| 54 | + f"cd {self.wsl_repo_path} && git {git_args}", |
| 55 | + ] |
35 | 56 | try: |
36 | 57 | result = subprocess.run( |
37 | 58 | cmd, |
38 | | - cwd=self.repo_path, |
39 | 59 | capture_output=True, |
40 | 60 | text=True, |
41 | | - shell=True, |
42 | 61 | timeout=timeout, |
43 | 62 | ) |
44 | 63 | if result.returncode != 0: |
45 | | - stderr = (result.stderr or '').strip() |
46 | | - print(f"❌ Git retornou codigo {result.returncode} em {self.repo_name}: {stderr}") |
| 64 | + stderr = (result.stderr or "").strip() |
| 65 | + if stderr: |
| 66 | + print(f"ERRO Git ({self.repo_name}): {stderr}") |
47 | 67 | return "" |
48 | 68 | return result.stdout.strip() |
49 | 69 | except subprocess.TimeoutExpired: |
50 | | - print(f"❌ Timeout ({timeout}s) executando git em {self.repo_name}: {cmd}") |
| 70 | + print(f"ERRO Timeout ({timeout}s) git em {self.repo_name}: {git_args[:80]}") |
51 | 71 | return "" |
52 | 72 | except Exception as e: |
53 | | - print(f"❌ Erro executando git em {self.repo_name}: {e}") |
| 73 | + print(f"ERRO executando git em {self.repo_name}: {e}") |
54 | 74 | return "" |
55 | 75 |
|
| 76 | + def run_git(self, cmd: str, timeout: int = 30) -> str: |
| 77 | + """Executa comando git no repositório (aceita 'git ...' ou args diretos).""" |
| 78 | + git_args = cmd.strip() |
| 79 | + if git_args.startswith("git "): |
| 80 | + git_args = git_args[4:] |
| 81 | + return self._run_git(git_args, timeout=timeout) |
| 82 | + |
56 | 83 | def validar_repo(self) -> bool: |
57 | | - """Verifica se o repositório é acessível e é um repositório Git válido.""" |
58 | | - try: |
59 | | - result = subprocess.run( |
60 | | - 'git rev-parse --git-dir', |
61 | | - cwd=self.repo_path, |
62 | | - capture_output=True, |
63 | | - text=True, |
64 | | - shell=True, |
65 | | - timeout=10, |
66 | | - ) |
67 | | - return result.returncode == 0 |
68 | | - except Exception as e: |
69 | | - print(f"❌ Repositorio inacessivel {self.repo_name}: {e}") |
70 | | - return False |
| 84 | + """Verifica se o repositório WSL é acessível e é um repositório Git válido.""" |
| 85 | + return bool(self._run_git("rev-parse --git-dir", timeout=10)) |
71 | 86 |
|
72 | 87 | def coleta_commits(self, since_days: int = 30) -> List[Dict[str, str]]: |
73 | 88 | """Extrai commits dos últimos N dias""" |
|
0 commit comments