Complete Gitea wrapper host resolution
This commit is contained in:
@@ -55,6 +55,154 @@ function Get-GitRepoInfo {
|
||||
return $repoPath
|
||||
}
|
||||
|
||||
function Get-GitRemoteHost {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$remoteUrl = git remote get-url origin 2>$null
|
||||
|
||||
if ([string]::IsNullOrEmpty($remoteUrl)) {
|
||||
Write-Error "Not a git repository or no origin remote"
|
||||
return $null
|
||||
}
|
||||
|
||||
if ($remoteUrl -match "^https?://([^/]+)/") {
|
||||
$remoteHost = $Matches[1]
|
||||
return ($remoteHost -replace "^.*@", "")
|
||||
}
|
||||
|
||||
if ($remoteUrl -match "^git@([^:]+):") {
|
||||
return $Matches[1]
|
||||
}
|
||||
|
||||
return $null
|
||||
}
|
||||
|
||||
function Get-TeaLoginList {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$json = tea login list --output json 2>$null
|
||||
if (-not $json) {
|
||||
return @()
|
||||
}
|
||||
|
||||
try {
|
||||
$items = $json | ConvertFrom-Json
|
||||
} catch {
|
||||
return @()
|
||||
}
|
||||
|
||||
if ($null -eq $items) {
|
||||
return @()
|
||||
}
|
||||
|
||||
return @($items)
|
||||
}
|
||||
|
||||
function Test-GiteaUrlMatchesHost {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$Url,
|
||||
[string]$GiteaHost
|
||||
)
|
||||
|
||||
if ([string]::IsNullOrEmpty($Url) -or [string]::IsNullOrEmpty($GiteaHost)) {
|
||||
return $false
|
||||
}
|
||||
|
||||
try {
|
||||
$uri = [Uri]$Url
|
||||
return $uri.Host -eq $GiteaHost
|
||||
} catch {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Find-TeaLoginForHost {
|
||||
[CmdletBinding()]
|
||||
param([Parameter(Mandatory=$true)][string]$GiteaHost)
|
||||
|
||||
foreach ($login in Get-TeaLoginList) {
|
||||
$name = if ($login.name) { [string]$login.name } elseif ($login.Name) { [string]$login.Name } else { "" }
|
||||
$url = if ($login.url) { [string]$login.url } elseif ($login.URL) { [string]$login.URL } else { "" }
|
||||
if ([string]::IsNullOrEmpty($name) -or [string]::IsNullOrEmpty($url)) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
$uri = [Uri]$url
|
||||
if ($uri.Host -eq $GiteaHost) {
|
||||
return $name
|
||||
}
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return $null
|
||||
}
|
||||
|
||||
function Test-TeaLoginMatchesHost {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)][string]$LoginName,
|
||||
[Parameter(Mandatory=$true)][string]$GiteaHost
|
||||
)
|
||||
|
||||
foreach ($login in Get-TeaLoginList) {
|
||||
$name = if ($login.name) { [string]$login.name } elseif ($login.Name) { [string]$login.Name } else { "" }
|
||||
$url = if ($login.url) { [string]$login.url } elseif ($login.URL) { [string]$login.URL } else { "" }
|
||||
if ($name -ne $LoginName -or [string]::IsNullOrEmpty($url)) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
$uri = [Uri]$url
|
||||
return $uri.Host -eq $GiteaHost
|
||||
} catch {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
return $false
|
||||
}
|
||||
|
||||
function Get-GiteaLoginForHost {
|
||||
[CmdletBinding()]
|
||||
param([string]$GiteaHost)
|
||||
|
||||
if ([string]::IsNullOrEmpty($GiteaHost)) {
|
||||
$GiteaHost = Get-GitRemoteHost
|
||||
}
|
||||
if ([string]::IsNullOrEmpty($GiteaHost)) {
|
||||
return $null
|
||||
}
|
||||
|
||||
if ($env:GITEA_LOGIN) {
|
||||
if ((Test-GiteaUrlMatchesHost -Url $env:GITEA_URL -GiteaHost $GiteaHost) -or (Test-TeaLoginMatchesHost -LoginName $env:GITEA_LOGIN -GiteaHost $GiteaHost)) {
|
||||
return $env:GITEA_LOGIN
|
||||
}
|
||||
}
|
||||
|
||||
return Find-TeaLoginForHost -GiteaHost $GiteaHost
|
||||
}
|
||||
|
||||
function Get-GiteaRepoArgs {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$repo = Get-GitRepoInfo
|
||||
$hostName = Get-GitRemoteHost
|
||||
$login = Get-GiteaLoginForHost -GiteaHost $hostName
|
||||
|
||||
if ([string]::IsNullOrEmpty($repo) -or [string]::IsNullOrEmpty($login)) {
|
||||
return @()
|
||||
}
|
||||
|
||||
return @("--repo", $repo, "--login", $login)
|
||||
}
|
||||
|
||||
function Get-GitRepoOwner {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
Reference in New Issue
Block a user