3adf0c5211
Канонический источник для клонирования в $HOME\CursorRules на новых ПК.
64 lines
1.8 KiB
PowerShell
64 lines
1.8 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Создать репозиторий на home Gitea через API, если его ещё нет.
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$RepoName,
|
|
[string]$Owner = 'PapaTramp',
|
|
[string]$ConfigPath = "$HOME\CursorRules\local\papatramp-gitea.conf",
|
|
[string]$Description = ''
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
if (-not (Test-Path -LiteralPath $ConfigPath)) {
|
|
throw "Config not found: $ConfigPath"
|
|
}
|
|
|
|
$cfg = @{}
|
|
Get-Content -LiteralPath $ConfigPath | ForEach-Object {
|
|
if ($_ -match '^([^#=]+)=(.*)$') {
|
|
$cfg[$Matches[1].Trim()] = $Matches[2].Trim()
|
|
}
|
|
}
|
|
|
|
$base = $cfg.GiteaApiUrl.TrimEnd('/')
|
|
$pair = '{0}:{1}' -f $cfg.GiteaApiUser, $cfg.GiteaApiPassword
|
|
$auth = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($pair))
|
|
$headers = @{ Authorization = $auth; 'Content-Type' = 'application/json' }
|
|
|
|
$checkUrl = "$base/api/v1/repos/$Owner/$RepoName"
|
|
try {
|
|
$existing = Invoke-RestMethod -Method Get -Uri $checkUrl -Headers $headers -TimeoutSec 15
|
|
Write-Host "[+] EXISTS $($existing.full_name)" -ForegroundColor Green
|
|
exit 0
|
|
}
|
|
catch {
|
|
if ($_.Exception.Response.StatusCode.value__ -ne 404) {
|
|
throw "check failed: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
|
|
$body = (@{
|
|
name = $RepoName
|
|
private = $true
|
|
auto_init = $false
|
|
description = $Description
|
|
} | ConvertTo-Json)
|
|
|
|
foreach ($url in @("$base/api/v1/orgs/$Owner/repos", "$base/api/v1/user/repos")) {
|
|
try {
|
|
$created = Invoke-RestMethod -Method Post -Uri $url -Headers $headers -Body $body -TimeoutSec 30
|
|
Write-Host "[+] CREATED $($created.full_name)" -ForegroundColor Green
|
|
exit 0
|
|
}
|
|
catch {
|
|
Write-Warning "create via $url : $($_.Exception.Message)"
|
|
}
|
|
}
|
|
|
|
throw 'failed to create repository on home Gitea'
|