-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessar_issues_memoria.py
More file actions
404 lines (337 loc) · 13.7 KB
/
Copy pathprocessar_issues_memoria.py
File metadata and controls
404 lines (337 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
"""Processa issues GitLab em memoria e gera registros prontos para o Supabase.
Substitui o caminho legado via Excel (process_gitlab_issues_v2 + sync a partir do
.xlsx). Reaproveita os mesmos detectores (area funcional, tipo, dev/git) e a
taxonomia, mas opera sobre dicts e devolve uma lista de registros com as colunas
exatas da tabela public.issues.
Campos manuais (situacao_analise, desenvolvedor_futuro, observacao_geral,
chamado, priorizar) sao deliberadamente omitidos para que o upsert
preserve valores existentes no Supabase. O campo `epico` vem do GitLab.
"""
from __future__ import annotations
import os
from datetime import UTC, date, datetime
from typing import Any
import issue_fields
from gitlab_identities import enrich_records_with_developer_ids
from issue_keys import get_gitlab_repo, repo_display_name
from logging_utils import get_logger
log = get_logger(__name__)
try:
import config as _config
except ImportError:
_config = None
_WSL_GIT_AVAILABLE: bool | None = None
_LOCAL_GIT_AVAILABLE: bool | None = None
def probe_local_git_repos() -> bool:
"""True se pelo menos um repo Git local (config.REPOS) esta acessivel."""
global _LOCAL_GIT_AVAILABLE
if _LOCAL_GIT_AVAILABLE is not None:
return _LOCAL_GIT_AVAILABLE
try:
from coleta_git_contratos import GitColeta
if _config is None or not getattr(_config, "REPOS", None):
_LOCAL_GIT_AVAILABLE = False
return False
for repo_path, repo_name in _config.REPOS:
if GitColeta(repo_path, repo_name).validar_repo():
_LOCAL_GIT_AVAILABLE = True
return True
_LOCAL_GIT_AVAILABLE = False
except Exception:
_LOCAL_GIT_AVAILABLE = False
return _LOCAL_GIT_AVAILABLE
def probe_wsl_git_available(timeout_seconds: int = 8) -> bool:
"""Verifica se WSL Ubuntu + repo Git respondem (cache em memoria)."""
del timeout_seconds # GitColeta valida via WSL com timeout proprio
global _WSL_GIT_AVAILABLE
if _WSL_GIT_AVAILABLE is not None:
return _WSL_GIT_AVAILABLE
_WSL_GIT_AVAILABLE = probe_local_git_repos()
return _WSL_GIT_AVAILABLE
def resolve_enable_git(requested: bool = True) -> bool:
"""True se detectores Git devem rodar (respeita MGI_FAST_REPO_SYNC + repos locais)."""
if not requested:
return False
if os.environ.get("MGI_FAST_REPO_SYNC", "0").lower() not in ("0", "false", "no"):
return False
if not probe_local_git_repos():
return False
return probe_wsl_git_available()
def reset_git_availability_cache() -> None:
"""Limpa cache de disponibilidade (util em testes)."""
global _WSL_GIT_AVAILABLE, _LOCAL_GIT_AVAILABLE
_WSL_GIT_AVAILABLE = None
_LOCAL_GIT_AVAILABLE = None
try:
from detectar_area_funcional import AreaDetection, build_detector
except ImportError: # pragma: no cover - import defensivo
AreaDetection = None # type: ignore
build_detector = None # type: ignore
try:
from inferir_tipo_issue import build_tipo_detector
except ImportError: # pragma: no cover
build_tipo_detector = None # type: ignore
try:
from enriquecer_dev_git import build_dev_enricher, resolve_desenvolvedor
except ImportError: # pragma: no cover
build_dev_enricher = None # type: ignore
resolve_desenvolvedor = None # type: ignore
def _utc_now_iso() -> str:
return datetime.now(UTC).replace(microsecond=0).isoformat().replace("+00:00", "Z")
def build_detectors(enable_git: bool = True):
"""Constroi os detectores Git. Com enable_git=False, usa apenas titulo/labels."""
if not enable_git:
return None, None, None
area = build_detector() if build_detector else None
tipo = build_tipo_detector() if build_tipo_detector else None
dev = build_dev_enricher() if build_dev_enricher else None
return area, tipo, dev
def _resolve_area(issue: dict, title: str, area_detector) -> AreaDetection:
title_area = issue_fields.extract_functional_area(title)
if area_detector is not None:
return area_detector.detect(issue, title_area=title_area)
if AreaDetection is not None:
method = "titulo_explicito" if title_area else "none"
return AreaDetection(title_area, method, 1.0 if title_area else 0.0)
class _Simple:
def __init__(self, area: str, confidence: float) -> None:
self.area = area
self.method = "titulo_explicito" if area else "none"
self.confidence = confidence
return _Simple(title_area, 1.0 if title_area else 0.0)
def _resolve_tipo(issue: dict, label_tipo: str, tipo_detector) -> str:
if label_tipo:
return label_tipo
if tipo_detector is not None:
return tipo_detector.detect(issue).tipo
return ""
def _parse_gitlab_int(value) -> int | None:
if value is None or value == "":
return None
try:
parsed = int(value)
except (TypeError, ValueError):
return None
return parsed if parsed > 0 else None
def _person_meta(person: dict) -> dict[str, Any] | None:
user_id = _parse_gitlab_int(person.get("id"))
if not user_id:
return None
return {
"id": user_id,
"username": (person.get("username") or "").strip() or None,
"name": (person.get("name") or "").strip() or None,
"email": None,
}
def _build_gitlab_identity_fields(issue: dict) -> dict[str, Any]:
author = issue.get("author") if isinstance(issue.get("author"), dict) else {}
participants: list[dict[str, Any]] = []
user_meta: list[dict[str, Any]] = []
assignee_ids: list[int] = []
author_meta = _person_meta(author)
author_id = author_meta["id"] if author_meta else None
if author_meta:
user_meta.append(author_meta)
participants.append(
{
"role": "author",
"gitlab_user_id": author_meta["id"],
"is_primary": True,
"source": "gitlab_api",
"display_name": author_meta.get("name") or "",
}
)
for index, person in enumerate(issue.get("assignees") or []):
if not isinstance(person, dict):
continue
assignee_meta = _person_meta(person)
if not assignee_meta:
continue
user_meta.append(assignee_meta)
assignee_ids.append(assignee_meta["id"])
participants.append(
{
"role": "assignee",
"gitlab_user_id": assignee_meta["id"],
"is_primary": index == 0,
"source": "gitlab_api",
"display_name": assignee_meta.get("name") or "",
}
)
return {
"gitlab_author_id": author_id,
"gitlab_developer_id": None,
"gitlab_assignee_ids": assignee_ids,
"_participants": participants,
"_gitlab_user_meta": user_meta,
}
def _resolve_dev(issue: dict, dev_enricher) -> dict[str, Any]:
mr_count = issue.get("merge_requests_count") or 0
try:
mr_count = int(mr_count)
except (TypeError, ValueError):
mr_count = 0
if dev_enricher is not None:
info = dev_enricher.enrich(issue)
desenvolvedor = (
resolve_desenvolvedor(issue, info) if resolve_desenvolvedor else info.autor_dev
)
return {
"dev_tem_branch": info.tem_branch,
"dev_branch": info.branch,
"dev_commits": info.commits,
"dev_ultimo_commit": info.ultimo_commit.isoformat() if info.ultimo_commit else None,
"dev_autor_dev": info.autor_dev,
"gitlab_mrs": info.mr_gitlab,
"dev_mergeado": info.mergeado,
"desenvolvedor": desenvolvedor,
"_dev_author_email": info.autor_email or None,
}
desenvolvedor = ""
assignee_id = None
for person in issue.get("assignees") or []:
name = person.get("name") if isinstance(person, dict) else str(person)
name = (name or "").strip()
if name and not desenvolvedor:
desenvolvedor = name
if isinstance(person, dict) and assignee_id is None:
assignee_id = _parse_gitlab_int(person.get("id"))
return {
"dev_tem_branch": "Não",
"dev_branch": "",
"dev_commits": 0,
"dev_ultimo_commit": None,
"dev_autor_dev": "",
"gitlab_mrs": mr_count,
"dev_mergeado": "Não",
"desenvolvedor": desenvolvedor,
"_dev_author_email": None,
"_fallback_developer_id": assignee_id,
}
def build_issue_record(
issue: dict,
*,
area_detector=None,
tipo_detector=None,
dev_enricher=None,
synced_at: str | None = None,
today: date | None = None,
) -> dict[str, Any] | None:
"""Constroi um registro Supabase a partir de uma issue crua. None se sem IID."""
iid_raw = str(issue.get("id", "")).strip()
if not iid_raw:
return None
try:
gitlab_iid = int(iid_raw)
except ValueError:
return None
synced_at = synced_at or _utc_now_iso()
title = issue.get("title", "") or ""
repo_slug = get_gitlab_repo(issue)
repo_label = repo_display_name(repo_slug)
issue_key = f"{repo_label}:{gitlab_iid}"
labels = issue_fields.parse_labels(issue.get("labels") or [])
estado = issue_fields.map_estado(issue.get("state", ""))
created_date = issue_fields.parse_date(issue.get("createdDate", ""))
closed_date = issue_fields.parse_date(issue.get("closedDate", ""))
# `modulo_raw` preserva a tag original do titulo (usada so na qualidade).
# A coluna `modulo` gravada usa o valor canonico/bucket (padrao do dashboard),
# evitando dezenas de valores soltos vindos do Contratos v1 nos filtros/RPCs.
modulo_raw = issue_fields.extract_module(title)
modulo_norm = issue_fields.normalized_module(title, modulo_raw)
modulo = modulo_norm
detection = _resolve_area(issue, title, area_detector)
area = detection.area
area_conf = float(getattr(detection, "confidence", 0.0) or 0.0)
tipo = _resolve_tipo(issue, labels["tipo"], tipo_detector)
record: dict[str, Any] = {
"issue_key": issue_key,
"gitlab_repo": repo_label,
"gitlab_iid": gitlab_iid,
"repositorio": repo_label,
"titulo": title,
"modulo": modulo,
"modulo_normalizado": modulo_norm,
"area_funcional": area,
"tipo": tipo,
"estado": estado,
"status": labels["status"],
"prioridade": labels["prioridade"],
"equipe": labels["equipe"],
"parceria": labels["parceria"],
"epico": issue_fields.extract_epico(issue),
"sprint": (issue.get("milestone") or {}).get("title", "") or "",
"assignee": issue_fields.format_assignees(issue),
"autor": (issue.get("author") or {}).get("name", "") or "",
"solicitante": labels["solicitante"],
"alteracao_escopo": labels["alteracao_escopo"],
"mergeado_em": issue.get("mergeado_em") or None,
"synced_at": synced_at,
"updated_at": synced_at,
}
record.update(issue_fields.derive_date_fields(created_date, closed_date, estado, today=today))
record.update(_resolve_dev(issue, dev_enricher))
record.update(issue_fields.quality_fields(title, modulo_raw, area, area_conf))
record["faixa_idade"] = issue_fields.faixa_idade(
record.get("idade_dias"), record.get("aberto", False)
)
record.update(_build_gitlab_identity_fields(issue))
fallback_dev_id = record.pop("_fallback_developer_id", None)
if fallback_dev_id and not record.get("gitlab_developer_id"):
record["gitlab_developer_id"] = fallback_dev_id
participants = list(record.get("_participants") or [])
if not any(
p.get("role") == "developer" and p.get("gitlab_user_id") == fallback_dev_id
for p in participants
):
participants.append(
{
"role": "developer",
"gitlab_user_id": fallback_dev_id,
"is_primary": True,
"source": "assignee_fallback",
"display_name": record.get("desenvolvedor") or "",
}
)
record["_participants"] = participants
return record
def build_issue_records(
issues: list[dict],
*,
enable_git: bool = True,
area_detector=None,
tipo_detector=None,
dev_enricher=None,
today: date | None = None,
) -> list[dict[str, Any]]:
"""Processa a lista de issues e devolve registros unicos (por issue_key).
Se nenhum detector for passado explicitamente, eles sao construidos conforme
enable_git. A ultima ocorrencia de cada issue_key prevalece (paridade com o
dedupe do sync legado)."""
total = len(issues)
git_on = resolve_enable_git(enable_git)
if area_detector is None and tipo_detector is None and dev_enricher is None:
mode = "Git + titulo/labels" if git_on else "rapido - titulo/labels"
log.info(f"OK - Modo: {mode}")
area_detector, tipo_detector, dev_enricher = build_detectors(git_on)
log.info(f"OK - Iniciando processamento de {total} issues...")
synced_at = _utc_now_iso()
seen: dict[str, dict[str, Any]] = {}
progress_step = max(25, total // 20) if total else 1
for index, issue in enumerate(issues, start=1):
record = build_issue_record(
issue,
area_detector=area_detector,
tipo_detector=tipo_detector,
dev_enricher=dev_enricher,
synced_at=synced_at,
today=today,
)
if record:
seen[record["issue_key"]] = record
if index == 1 or index == total or index % progress_step == 0:
log.info(f"OK - Processadas {index}/{total} issues...")
records = list(seen.values())
enrich_records_with_developer_ids(records)
return records