Files
fpcollect/scripts/sync-to-gitea.ps1
T
2026-10-25 09:44:15 +08:00

70 lines
3.1 KiB
PowerShell

# Sync fpcollect + REA mirror to Gitea (idempotent, safe to re-run)
# 1) REA : server-side mirror migration (Gitea pulls from GitHub every 30m = continuous sync)
# 2) fpcollect : push local git repo
# Log: sync-to-gitea.log (same folder)
$ErrorActionPreference = 'Continue'
$Gitea = 'http://39.108.95.179:3000'
$User = 'kenyang'
$Pass = 'AIBay666'
$FpDir = 'C:\Users\mian5\Documents\默认项目\fpcollect'
$log = Join-Path $PSScriptRoot 'sync-to-gitea.log'
function L($m) { "$(Get-Date -Format 'MM-dd HH:mm:ss') $m" | Tee-Object -FilePath $log -Append }
$pair = "${User}:${Pass}"
$authHeader = @{ Authorization = "Basic $([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($pair)))" }
# 0) connectivity
$c = New-Object Net.Sockets.TcpClient
if (-not $c.ConnectAsync('39.108.95.179', 3000).Wait(4000)) { L 'Gitea unreachable, skip this round'; exit 1 }
$c.Close()
L 'Gitea reachable, syncing'
# 1) REA mirror
try {
$existing = Invoke-RestMethod -Uri "$Gitea/api/v1/repos/$User/rea" -Headers $authHeader -TimeoutSec 15
L "rea exists (mirror=$($existing.mirror)), skip create"
} catch {
$body = @{
clone_addr = 'https://github.com/morluto/rea.git'
repo_owner = $User
repo_name = 'rea'
mirror = $true
mirror_interval = '30m'
private = $false
description = 'REA: Reverse Engineer Anything (auto mirror from GitHub, 30m interval)'
service = 'git'
} | ConvertTo-Json
try {
Invoke-RestMethod -Uri "$Gitea/api/v1/repos/migrate" -Method Post -Headers $authHeader -ContentType 'application/json' -Body $body -TimeoutSec 120 | Out-Null
L 'rea mirror migration submitted (server-side pull in progress)'
} catch { L "rea migrate failed: $($_.Exception.Message)" }
}
# 2) fpcollect repo + push
try {
Invoke-RestMethod -Uri "$Gitea/api/v1/repos/$User/fpcollect" -Headers $authHeader -TimeoutSec 15 | Out-Null
L 'fpcollect repo exists'
} catch {
$body = @{ name = 'fpcollect'; private = $false; description = 'Fingerprint capture system rebuild (Win agent + Web UI)'; auto_init = $false } | ConvertTo-Json
try {
Invoke-RestMethod -Uri "$Gitea/api/v1/user/repos" -Method Post -Headers $authHeader -ContentType 'application/json' -Body $body -TimeoutSec 30 | Out-Null
L 'fpcollect repo created'
} catch { L "fpcollect create failed: $($_.Exception.Message)" }
}
$git = 'C:\Program Files\Git\cmd\git.exe'
Push-Location $FpDir
if (-not (Test-Path "$FpDir\.git")) { & $git init 2>&1 | Out-Null; L 'fpcollect git init' }
& $git add -A 2>&1 | Out-Null
$hasCommit = & $git rev-parse --verify HEAD 2>$null
if (-not $hasCommit) { & $git commit -m 'fpcollect initial commit' 2>&1 | Out-Null }
else { & $git commit -m "update $(Get-Date -Format 'yyyy-MM-dd HH:mm')" 2>&1 | Out-Null }
$remoteUrl = "http://${pair}@39.108.95.179:3000/${User}/fpcollect.git"
& $git remote remove gitea 2>$null
& $git remote add gitea $remoteUrl
$branch = (& $git branch --show-current).Trim()
$push = (& $git push -u gitea $branch 2>&1 | Out-String).Trim()
L ("fpcollect push [" + $branch + "]: " + ($push -replace "\r?\n", ' | '))
Pop-Location
L 'sync round done'