65 lines
2.9 KiB
PowerShell
65 lines
2.9 KiB
PowerShell
# 将本仓库创建/推送到 Gitea(默认目标 http://210.126.232.10:3000)
|
||
# 用法:
|
||
# $env:GITEA_TOKEN = '<你的 Gitea 访问令牌>'
|
||
# $env:GITEA_URL = 'http://210.126.232.10:3000' # 可选
|
||
# $env:GITEA_REPO = 'classhelper-island-refactor-prompt' # 可选
|
||
# pwsh -File scripts/push-to-gitea.ps1
|
||
# 说明:令牌只从环境变量读取,不写入仓库文件;推送完成后会把 origin 还原为不含凭据的 URL。
|
||
param(
|
||
[string]$RepoName = $(if ($env:GITEA_REPO) { $env:GITEA_REPO } else { 'classhelper-island-refactor-prompt' }),
|
||
[string]$BaseUrl = $(if ($env:GITEA_URL) { $env:GITEA_URL } else { 'http://210.126.232.10:3000' }),
|
||
[switch]$Public
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
if (-not $env:GITEA_TOKEN) { throw '缺少环境变量 GITEA_TOKEN(Gitea 访问令牌)。' }
|
||
$token = $env:GITEA_TOKEN.Trim()
|
||
$base = $BaseUrl.TrimEnd('/')
|
||
$headers = @{ Authorization = "token $token"; 'Content-Type' = 'application/json' }
|
||
|
||
Write-Host "[1/4] 校验令牌:GET $base/api/v1/user"
|
||
$user = Invoke-RestMethod -Uri "$base/api/v1/user" -Headers $headers -Method Get -TimeoutSec 20
|
||
if (-not $user.login) { throw '无法从 Gitea 获取当前用户,令牌可能无效。' }
|
||
Write-Host " 当前用户:$($user.login)"
|
||
|
||
Write-Host "[2/4] 创建仓库:$RepoName($(if ($Public) {'公开'} else {'私有'}))"
|
||
$exists = $false
|
||
try {
|
||
Invoke-RestMethod -Uri "$base/api/v1/repos/$($user.login)/$RepoName" -Headers $headers -Method Get -TimeoutSec 15 | Out-Null
|
||
$exists = $true
|
||
Write-Host ' 仓库已存在,直接推送。'
|
||
} catch {
|
||
Write-Host " 仓库不存在或探测失败($($_.Exception.Message)),继续尝试创建。"
|
||
}
|
||
if (-not $exists) {
|
||
$body = @{ name = $RepoName; private = (-not $Public); auto_init = $false; description = 'ClassIsland / ClassHelper 灵动岛提示词套件(静态分析产物)' } | ConvertTo-Json
|
||
try {
|
||
$repo = Invoke-RestMethod -Uri "$base/api/v1/user/repos" -Headers $headers -Method Post -Body $body -TimeoutSec 20
|
||
Write-Host " 已创建:$($repo.full_name)"
|
||
Write-Host " 网页地址:$($repo.html_url)"
|
||
} catch {
|
||
if ($_.Exception.Response -and $_.Exception.Response.StatusCode.value__ -eq 409) {
|
||
Write-Host ' 仓库已存在(409),继续推送。'
|
||
} else {
|
||
throw "创建仓库失败:$($_.Exception.Message)"
|
||
}
|
||
}
|
||
}
|
||
|
||
Write-Host '[3/4] 推送 main 分支'
|
||
$scheme = ($base -split '://')[0]
|
||
$hostPart = ($base -split '://')[1]
|
||
$cleanUrl = "$base/$($user.login)/$RepoName.git"
|
||
$authUrl = "${scheme}://$([uri]::EscapeDataString($user.login)):$([uri]::EscapeDataString($token))@$hostPart/$($user.login)/$RepoName.git"
|
||
git remote remove origin 2>$null
|
||
git remote add origin $authUrl
|
||
try {
|
||
git push -u origin HEAD:main
|
||
} finally {
|
||
git remote set-url origin $cleanUrl
|
||
Write-Host " 已将 origin 还原为不含凭据的地址:$cleanUrl"
|
||
}
|
||
|
||
Write-Host '[4/4] 完成'
|
||
Write-Host "仓库:$cleanUrl"
|