Skip to content

Commit de027fd

Browse files
authored
Merge branch 'main' into dependabot/github_actions/actions/setup-python-7
2 parents f690167 + 6acb54a commit de027fd

38 files changed

Lines changed: 1951 additions & 255 deletions

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ GITLAB_URL=https://gitlab.com
99
GITLAB_TOKEN=
1010
# GITLAB_TOKEN_CONTRATOS_V2=
1111
# GITLAB_TOKEN_CONTRATOS=
12+
# Grupo dos epicos (catalogo do filtro Épico). Padrao: comprasnet
13+
# GITLAB_GROUP_PATH=comprasnet
1214

1315
# --- Repositorios Git locais (coleta de commits/branches/releases) ---
1416
# Formato: caminho_local=slug;outro_caminho=slug

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ venv/
1616

1717
# Artefatos gerados pela pipeline
1818
gitlab_issues_raw.json
19+
gitlab_epics_raw.json
20+
gitlab_tipo_labels_raw.json
1921
gitlab_issues_sync_state.json
2022
gitlab_fiscalizacao_data.json
2123
gitlab_git_data.json
2224
*.log
25+
JSON
2326

2427
# Excel gerado (fica na raiz do workspace ou em templates/)
2528
*.xlsx

agendar_pull_repos.ps1

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#Requires -RunAsAdministrator
2+
<#
3+
.SYNOPSIS
4+
Agenda pull condicional nos repos contratos* (WSL):
5+
terca e quinta as 09:00.
6+
7+
.PARAMETER Time
8+
Horario (HH:mm). Padrao: 09:00.
9+
10+
.PARAMETER DaysOfWeek
11+
Dias da semana. Padrao: Tuesday, Thursday.
12+
13+
.PARAMETER Test
14+
Executa executar_pull_repos.bat apos criar/atualizar a tarefa.
15+
16+
.PARAMETER Force
17+
Substitui a tarefa existente sem perguntar.
18+
#>
19+
param(
20+
[string]$Time = "09:00",
21+
[ValidateSet("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")]
22+
[System.DayOfWeek[]]$DaysOfWeek = @([System.DayOfWeek]::Tuesday, [System.DayOfWeek]::Thursday),
23+
[switch]$Test,
24+
[switch]$Force
25+
)
26+
27+
$ErrorActionPreference = "Stop"
28+
29+
$colors = @{
30+
Success = "Green"
31+
Error = "Red"
32+
Warning = "Yellow"
33+
}
34+
35+
Write-Host ""
36+
Write-Host "================================================================"
37+
Write-Host " AGENDAMENTO - Pull condicional (contratos* / WSL)"
38+
Write-Host "================================================================"
39+
Write-Host ""
40+
41+
$admin = [Security.Principal.WindowsIdentity]::GetCurrent()
42+
$principalCheck = New-Object Security.Principal.WindowsPrincipal($admin)
43+
if (-not $principalCheck.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
44+
Write-Host "ERRO - Execute como Administrador" -ForegroundColor $colors.Error
45+
exit 1
46+
}
47+
48+
$WORKSPACE_DIR = Split-Path -Parent $PSScriptRoot
49+
$BATCH_FILE = Join-Path $WORKSPACE_DIR "executar_pull_repos.bat"
50+
$TASK_NAME = "MGI-Pull-Repos-Main"
51+
$RUN_AS_USER = "$env:USERDOMAIN\$env:USERNAME"
52+
$dayLabels = ($DaysOfWeek | ForEach-Object { $_.ToString() }) -join ", "
53+
54+
Write-Host "Workspace: $WORKSPACE_DIR"
55+
Write-Host "Script: executar_pull_repos.bat"
56+
Write-Host "Tarefa: $TASK_NAME"
57+
Write-Host "Horario: $Time"
58+
Write-Host "Dias: $dayLabels"
59+
Write-Host "Fluxo: fetch + detecta branch (origin/HEAD / master) + pull --ff-only se remoto a frente"
60+
Write-Host "Usuario: $RUN_AS_USER"
61+
Write-Host ""
62+
63+
if (-not (Test-Path $BATCH_FILE)) {
64+
Write-Host "ERRO - Arquivo nao encontrado: $BATCH_FILE" -ForegroundColor $colors.Error
65+
exit 1
66+
}
67+
68+
$existingTask = Get-ScheduledTask -TaskName $TASK_NAME -ErrorAction SilentlyContinue
69+
if ($existingTask) {
70+
if ($Force) {
71+
Unregister-ScheduledTask -TaskName $TASK_NAME -Confirm:$false
72+
Write-Host "OK - Tarefa anterior removida" -ForegroundColor $colors.Success
73+
} else {
74+
$choice = Read-Host "Tarefa ja existe. Atualizar? (S/N)"
75+
if ($choice -notmatch "^[Ss]") {
76+
Write-Host "Cancelado."
77+
exit 0
78+
}
79+
Unregister-ScheduledTask -TaskName $TASK_NAME -Confirm:$false
80+
}
81+
}
82+
83+
$action = New-ScheduledTaskAction `
84+
-Execute "cmd.exe" `
85+
-Argument "/c `"$BATCH_FILE`"" `
86+
-WorkingDirectory $WORKSPACE_DIR
87+
88+
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -At $Time
89+
90+
$settings = New-ScheduledTaskSettingsSet `
91+
-AllowStartIfOnBatteries `
92+
-DontStopIfGoingOnBatteries `
93+
-StartWhenAvailable `
94+
-RunOnlyIfNetworkAvailable `
95+
-MultipleInstances IgnoreNew `
96+
-ExecutionTimeLimit (New-TimeSpan -Minutes 30)
97+
98+
$principal = New-ScheduledTaskPrincipal `
99+
-UserId $RUN_AS_USER `
100+
-LogonType Interactive `
101+
-RunLevel Highest
102+
103+
Register-ScheduledTask `
104+
-TaskName $TASK_NAME `
105+
-Action $action `
106+
-Trigger $trigger `
107+
-Settings $settings `
108+
-Principal $principal `
109+
-Description "Pull condicional nos repos contratos* (ter/qui 09:00; branch origin/HEAD)" `
110+
-Force | Out-Null
111+
112+
Write-Host ""
113+
Write-Host "OK - Tarefa criada!" -ForegroundColor $colors.Success
114+
115+
$taskInfo = Get-ScheduledTaskInfo -TaskName $TASK_NAME
116+
Write-Host "Proxima execucao: $($taskInfo.NextRunTime)"
117+
Write-Host "Logs: $WORKSPACE_DIR\logs\pull_repos_*.log"
118+
Write-Host ""
119+
120+
if ($Test -or ((Read-Host "Testar agora? (S/N)") -match "^[Ss]")) {
121+
Write-Host "Executando teste..."
122+
$proc = Start-Process -FilePath "cmd.exe" `
123+
-ArgumentList "/c `"$BATCH_FILE`"" `
124+
-WorkingDirectory $WORKSPACE_DIR `
125+
-Wait -PassThru -NoNewWindow
126+
if ($proc.ExitCode -eq 0) {
127+
Write-Host "OK - Teste concluido com sucesso" -ForegroundColor $colors.Success
128+
} else {
129+
Write-Host "AVISO - Teste retornou codigo $($proc.ExitCode)" -ForegroundColor $colors.Warning
130+
Write-Host "Consulte o log mais recente em logs\pull_repos_*.log"
131+
}
132+
}
133+
134+
Write-Host ""
135+
Write-Host "Gerenciar: taskschd.msc -> $TASK_NAME"
136+
Write-Host "Remover: desagendar_pull_repos.bat"
137+
Write-Host "Manual: executar_pull_repos.bat"
138+
Write-Host ""

atualizar_gitlab_issues.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
except ImportError:
3131
config = None
3232

33+
from gitlab_epics import coletar_e_salvar_epicos, mapear_epic_api
34+
from gitlab_merges import enriquecer_issues_com_merge_dates
3335
from issue_filters import filtrar_issues_fechadas_antigas, parse_issue_datetime
3436
from issue_keys import make_issue_key
3537
from logging_utils import get_logger
@@ -76,6 +78,7 @@ def _mapear_issue_api(issue: dict, gitlab_repo: str) -> dict:
7678
author = issue.get("author") or {}
7779
assignees = issue.get("assignees") or []
7880
milestone = issue.get("milestone") or {}
81+
epic = mapear_epic_api(issue.get("epic"), issue.get("epic_iid"))
7982
return {
8083
# IID = numero visivel no GitLab (#1289). NAO usar issue['id'] global.
8184
"id": str(issue["iid"]),
@@ -102,6 +105,7 @@ def _mapear_issue_api(issue: dict, gitlab_repo: str) -> dict:
102105
if assignee.get("name") or assignee.get("id")
103106
],
104107
"milestone": {"title": milestone.get("title", "") if milestone else ""},
108+
"epic": epic,
105109
"labels": issue.get("labels", []) or [],
106110
"merge_requests_count": issue.get("merge_requests_count", 0) or 0,
107111
}
@@ -342,9 +346,9 @@ def _ensure_tokens(destino: Path) -> bool:
342346
return True
343347

344348
log.warning("AVISO: Nenhum token GitLab definido.")
345-
log.warning(" Global: setx GITLAB_TOKEN \"<token>\"")
346-
log.warning(" Por repo: setx GITLAB_TOKEN_CONTRATOS_V2 \"<token>\"")
347-
log.warning(" setx GITLAB_TOKEN_CONTRATOS \"<token>\"")
349+
log.warning(' Global: setx GITLAB_TOKEN "<token>"')
350+
log.warning(' Por repo: setx GITLAB_TOKEN_CONTRATOS_V2 "<token>"')
351+
log.warning(' setx GITLAB_TOKEN_CONTRATOS "<token>"')
348352
log.warning(" Continuando com gitlab_issues_raw.json existente.")
349353
validar_json_local(destino)
350354
return False
@@ -387,6 +391,16 @@ def atualizar_issues(
387391

388392
issues, _ = _aplicar_filtro_fechadas(issues)
389393

394+
try:
395+
coletar_e_salvar_epicos(issues=issues, dry_run=dry_run)
396+
except Exception as exc:
397+
log.warning(f"AVISO - falha ao coletar epicos do grupo: {exc}")
398+
399+
try:
400+
enriquecer_issues_com_merge_dates(issues)
401+
except Exception as exc:
402+
log.warning(f"AVISO - falha ao coletar datas de merge: {exc}")
403+
390404
if dry_run:
391405
log.info(f"OK - Dry-run: {len(issues)} issues seriam gravadas (modo completo)")
392406
return True
@@ -449,6 +463,16 @@ def atualizar_issues_incremental(
449463
if removed_old_closed:
450464
log.info(f"OK - {removed_old_closed} issues removidas do JSON por filtro de fechadas")
451465

466+
try:
467+
coletar_e_salvar_epicos(issues=merged, dry_run=dry_run)
468+
except Exception as exc:
469+
log.warning(f"AVISO - falha ao coletar epicos do grupo: {exc}")
470+
471+
try:
472+
enriquecer_issues_com_merge_dates(merged)
473+
except Exception as exc:
474+
log.warning(f"AVISO - falha ao coletar datas de merge: {exc}")
475+
452476
if dry_run:
453477
log.info(
454478
f"OK - Dry-run: JSON final teria {len(merged)} issues "

backfill_profile_gitlab_ids.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def _headers(service_key: str) -> dict[str, str]:
4545
}
4646

4747

48-
def _fetch_profiles_without_gitlab_id(
49-
supabase_url: str, service_key: str
50-
) -> list[dict[str, Any]]:
48+
def _fetch_profiles_without_gitlab_id(supabase_url: str, service_key: str) -> list[dict[str, Any]]:
5149
response = requests.get(
5250
f"{supabase_url}/rest/v1/profiles",
5351
headers=_headers(service_key),
@@ -73,9 +71,7 @@ def _fetch_gitlab_users_table(supabase_url: str, service_key: str) -> list[dict[
7371
return response.json()
7472

7573

76-
def _fetch_profiles_with_gitlab_id(
77-
supabase_url: str, service_key: str
78-
) -> dict[int, str]:
74+
def _fetch_profiles_with_gitlab_id(supabase_url: str, service_key: str) -> dict[int, str]:
7975
response = requests.get(
8076
f"{supabase_url}/rest/v1/profiles",
8177
headers=_headers(service_key),
@@ -222,11 +218,11 @@ def run_backfill(*, dry_run: bool, from_gitlab_only: bool) -> int:
222218
log.info(f"{email:<42} {name:<28} {gitlab_id:<10} VINCULADO")
223219
linked += 1
224220

225-
log.info(
226-
f"\nResumo: {linked} vinculado(s), {skipped} ignorado(s)/sem match."
227-
)
221+
log.info(f"\nResumo: {linked} vinculado(s), {skipped} ignorado(s)/sem match.")
228222
if unmatched:
229-
log.info("\nSem correspondencia por e-mail (vincule manualmente em Admin > Usuarios > ID GitLab):")
223+
log.info(
224+
"\nSem correspondencia por e-mail (vincule manualmente em Admin > Usuarios > ID GitLab):"
225+
)
230226
for email in unmatched:
231227
log.info(f" - {email}")
232228

0 commit comments

Comments
 (0)