Files
solutions/Set-GiteaOrgActionItem.ps1
2026-03-11 19:48:23 +01:00

39 lines
1.4 KiB
PowerShell

param(
[ValidateNotNullOrEmpty()] [string]$BaseUrl = "https://code.wynenterprise.io/",
[ValidateNotNullOrEmpty()] [string]$Token = $Env:AdminGiteaManagePAT,
[ValidateNotNullOrEmpty()] [string]$Org = "devops",
[Parameter(Mandatory=$true)] [string]$VarName,
[string]$Value,
[string]$Password
)
if ([string]::IsNullOrWhiteSpace($Value) -and [string]::IsNullOrWhiteSpace($Password)) {
throw "Specify either -Value or -Password"
}
if (-not [string]::IsNullOrWhiteSpace($Value) -and -not [string]::IsNullOrWhiteSpace($Password)) {
throw "Specify only one of -Value or -Password"
}
$BaseUrl = $BaseUrl.TrimEnd('/')
$Headers = @{
Authorization = "token $Token"
Accept = "application/json"
"Content-Type" = "application/json"
}
if (-not [string]::IsNullOrWhiteSpace($Password)) { $Uri = "$BaseUrl/api/v1/orgs/$Org/actions/secrets"; $Body = (@{data = $Password} | ConvertTo-Json -Compress); $Method="Put"}
else { $Uri = "$BaseUrl/api/v1/orgs/$Org/actions/variables"; $Body = (@{name = $VarName; value = $Value} | ConvertTo-Json -Compress); $Method="Post"}
$vars = Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers -ErrorAction Stop
$FoundVar = $vars | Where-Object { $_.name -eq $VarName }
if (-not [bool]$FoundVar) {
Invoke-RestMethod -Method $Method -Uri "$Uri/$VarName" -Headers $Headers -Body $Body
} else {
Invoke-RestMethod -Method Put -Uri "$Uri/$VarName" -Headers $Headers -Body $Body
}