100 lines
2.6 KiB
PowerShell
100 lines
2.6 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Create a Gitea repository (idempotent).
|
|
|
|
.DESCRIPTION
|
|
Mirrors deploy_skill/scripts/New-GitHubRepo.ps1. If the repo already exists,
|
|
prints its URL and returns the existing object instead of failing.
|
|
|
|
.PARAMETER Name
|
|
Repository name (required).
|
|
|
|
.PARAMETER Description
|
|
Short description.
|
|
|
|
.PARAMETER Private
|
|
Switch. If set, creates a private repo. Default public.
|
|
|
|
.PARAMETER Owner
|
|
Optional. User (default) or org to create under.
|
|
|
|
.PARAMETER NoAutoInit
|
|
Skip the Gitea-side README init (use when you will push an existing local
|
|
repo with its own history).
|
|
|
|
.EXAMPLE
|
|
.\New-GiteaRepo.ps1 -Name my-app -Description "demo" -Private
|
|
.\New-GiteaRepo.ps1 -Name my-app -NoAutoInit
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[ValidatePattern('^[A-Za-z0-9._-]+$')]
|
|
[string]$Name,
|
|
|
|
[string]$Description = "",
|
|
|
|
[switch]$Private,
|
|
|
|
[string]$Owner,
|
|
|
|
[switch]$NoAutoInit
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$api = Join-Path $here "Invoke-GiteaApi.ps1"
|
|
|
|
$me = & $api -Path "/user"
|
|
$effectiveOwner = if ($Owner) { $Owner } else { $me.login }
|
|
Write-Host "Authenticated Gitea user: $($me.login)" -ForegroundColor Cyan
|
|
|
|
$existing = $null
|
|
try {
|
|
$existing = & $api -Path "/repos/$effectiveOwner/$Name"
|
|
} catch {
|
|
if ($_.Exception.Message -notmatch "404") { throw }
|
|
$existing = $null
|
|
}
|
|
if ($existing) {
|
|
Write-Host "Repository already exists: $($existing.html_url)" -ForegroundColor Yellow
|
|
[PSCustomObject]@{
|
|
full_name = $existing.full_name
|
|
html_url = $existing.html_url
|
|
clone_url = $existing.clone_url
|
|
ssh_url = $existing.ssh_url
|
|
private = $existing.private
|
|
default_branch = $existing.default_branch
|
|
} | Format-List
|
|
return $existing
|
|
}
|
|
|
|
$body = [ordered]@{
|
|
name = $Name
|
|
description = $Description
|
|
private = [bool]$Private
|
|
auto_init = (-not $NoAutoInit)
|
|
has_issues = $true
|
|
has_wiki = $false
|
|
default_branch = "main"
|
|
} | ConvertTo-Json -Compress
|
|
|
|
$endpoint = if ($Owner -and $Owner -ne $me.login) {
|
|
"/orgs/$Owner/repos"
|
|
} else {
|
|
"/user/repos"
|
|
}
|
|
|
|
Write-Host "Creating repository at: $endpoint" -ForegroundColor Cyan
|
|
$created = & $api -Method POST -Path $endpoint -BodyJson $body
|
|
|
|
[PSCustomObject]@{
|
|
full_name = $created.full_name
|
|
html_url = $created.html_url
|
|
clone_url = $created.clone_url
|
|
ssh_url = $created.ssh_url
|
|
private = $created.private
|
|
default_branch = $created.default_branch
|
|
} | Format-List
|
|
|
|
$created
|