forked from Gitlink/gitlink-cli
143 lines
4.8 KiB
PowerShell
143 lines
4.8 KiB
PowerShell
# Common utilities for gitlink-cli workflow scripts (PowerShell 5.1+)
|
|
|
|
# Force UTF-8 when capturing stdout from native commands.
|
|
# On Windows Chinese locales, PS 5.1 defaults to GBK and corrupts multi-byte
|
|
# JSON (e.g. 紧急/新增), which makes ConvertFrom-Json fail silently.
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
$OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
# CRITICAL: gitlink-cli outputs UTF-8 JSON, but PS 5.1 on Chinese Windows
|
|
# defaults to GBK (codepage 936) for decoding external program output.
|
|
# Without this, multi-byte UTF-8 chars get garbled and JSON parsing fails.
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
$Script:GL = "gitlink-cli"
|
|
|
|
# -- Logging --
|
|
function Log-Step { param([string]$Msg) Write-Host "[STEP] $Msg" -ForegroundColor Blue }
|
|
function Log-Ok { param([string]$Msg) Write-Host "[ OK] $Msg" -ForegroundColor Green }
|
|
function Log-Warn { param([string]$Msg) Write-Host "[WARN] $Msg" -ForegroundColor Yellow }
|
|
function Log-Err { param([string]$Msg) Write-Host "[ ERR] $Msg" -ForegroundColor Red }
|
|
function Log-Info { param([string]$Msg) Write-Host "[INFO] $Msg" -ForegroundColor Cyan }
|
|
function Log-Title { param([string]$Msg) Write-Host ""; Write-Host "====== $Msg ======" -ForegroundColor White; Write-Host "" }
|
|
function Divider { Write-Host "------------------------------------------------" -ForegroundColor Cyan }
|
|
|
|
# -- Auth Check --
|
|
function Check-Auth {
|
|
if ($env:GITLINK_TOKEN) {
|
|
Log-Ok "GITLINK_TOKEN is set"
|
|
return
|
|
}
|
|
$status = & $Script:GL auth status 2>&1
|
|
$statusStr = $status -join " "
|
|
if ($statusStr -match "logged in") {
|
|
Log-Ok "Authenticated"
|
|
return
|
|
}
|
|
Log-Err "Not authenticated. Please login first:"
|
|
Log-Info " gitlink-cli auth login"
|
|
Log-Info ' $env:GITLINK_TOKEN = "your-private-token"'
|
|
exit 1
|
|
}
|
|
|
|
# -- CLI Wrapper --
|
|
# Returns parsed JSON object on success, $null on failure
|
|
# Usage: Invoke-GL @("issue", "+list", "--owner", $Owner, "--repo", $Repo)
|
|
function Invoke-GL {
|
|
<<<<<<< HEAD
|
|
param([string[]]$Arguments)
|
|
# Suppress stderr to keep JSON output clean (errors go to console via error stream)
|
|
$output = & $Script:GL @Arguments --format json 2>$null
|
|
if ($output) { return ($output -join "`n") }
|
|
return ""
|
|
}
|
|
|
|
function Invoke-GLCheck {
|
|
param([string[]]$Arguments)
|
|
$output = Invoke-GL $Arguments
|
|
if (-not $output) {
|
|
Log-Err "Command returned no output: $Script:GL $($Arguments -join ' ')"
|
|
return $null
|
|
}
|
|
try {
|
|
$json = $output | ConvertFrom-Json
|
|
if (-not $json.ok) {
|
|
$errMsg = if ($json.error.message) { $json.error.message } else { "unknown error" }
|
|
Log-Err "Command failed: $Script:GL $($Arguments -join ' ')"
|
|
Log-Err $errMsg
|
|
return $null
|
|
}
|
|
return $json
|
|
} catch {
|
|
Log-Err "Command failed (non-JSON response): $Script:GL $($Arguments -join ' ')"
|
|
Log-Err $output
|
|
=======
|
|
param([string[]]$CmdArgs)
|
|
$allArgs = @($CmdArgs) + @("--format", "json")
|
|
# Capture stdout only; stderr goes to console
|
|
$output = & $Script:GL @allArgs
|
|
$raw = ($output -join "`n")
|
|
if (-not $raw -or $raw.Trim() -eq "") { return $null }
|
|
try {
|
|
return ($raw | ConvertFrom-Json)
|
|
} catch {
|
|
>>>>>>> master
|
|
return $null
|
|
}
|
|
}
|
|
|
|
# Like Invoke-GL but logs error on failure
|
|
function Invoke-GLCheck {
|
|
param([string[]]$CmdArgs)
|
|
$json = Invoke-GL $CmdArgs
|
|
if (-not $json) {
|
|
$argStr = $CmdArgs -join " "
|
|
Log-Err "Command failed (no JSON): $Script:GL $argStr"
|
|
return $null
|
|
}
|
|
if (-not $json.ok) {
|
|
$argStr = $CmdArgs -join " "
|
|
$errMsg = if ($json.error -and $json.error.message) { $json.error.message } else { "unknown error" }
|
|
Log-Err "Command failed: $Script:GL $argStr"
|
|
Log-Err $errMsg
|
|
return $null
|
|
}
|
|
return $json
|
|
}
|
|
|
|
# -- JSON Helpers --
|
|
function Get-JsonOk {
|
|
param($Json)
|
|
return ($Json.ok -eq $true)
|
|
}
|
|
|
|
# -- Owner/Repo Detection --
|
|
function Detect-OwnerRepo {
|
|
$remote = git remote get-url origin 2>$null
|
|
if (-not $remote) {
|
|
Log-Err "No git remote 'origin' found. Use -Owner and -Repo flags."
|
|
exit 1
|
|
}
|
|
if ($remote -match "gitlink\.org\.cn[:/]([^/]+)/([^/.]+)") {
|
|
return @{ Owner = $Matches[1]; Repo = $Matches[2] }
|
|
}
|
|
Log-Err "Cannot parse owner/repo from remote: $remote"
|
|
exit 1
|
|
}
|
|
|
|
function Resolve-OwnerRepo {
|
|
param([string]$Owner, [string]$Repo)
|
|
if (-not $Owner -or -not $Repo) {
|
|
$detected = Detect-OwnerRepo
|
|
if (-not $Owner) { $Owner = $detected.Owner }
|
|
if (-not $Repo) { $Repo = $detected.Repo }
|
|
}
|
|
Log-Info "Using: $Owner/$Repo"
|
|
return @{ Owner = $Owner; Repo = $Repo }
|
|
}
|
|
|
|
# -- Date Helpers --
|
|
function Get-DateToday { return (Get-Date -Format "yyyy-MM-dd") }
|
|
|
|
Export-ModuleMember -Function *
|