Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014Ad9rqAB8pXXvyzh7f4X8v
47 lines
3.2 KiB
PowerShell
47 lines
3.2 KiB
PowerShell
# Gitea issue wrapper for the Grok /loop session (Windows). The allowlist permits
|
|
# only this script, not arbitrary PowerShell, so the session cannot read the
|
|
# token or run other commands through it. ASCII only (PowerShell 5.1 reads
|
|
# BOM-less scripts as ANSI); Chinese label names arrive as arguments.
|
|
# gitea.ps1 whoami
|
|
# gitea.ps1 issues <label1,label2> open issues carrying ALL labels, oldest first (JSON)
|
|
# gitea.ps1 issue <N> number, title, body (body only if written by the owner)
|
|
# gitea.ps1 labels <N> label names, one per line
|
|
# gitea.ps1 add-label <N> <name>
|
|
# gitea.ps1 remove-label <N> <name>
|
|
# gitea.ps1 comments <N> owner's comments only (JSON: created_at, body)
|
|
# gitea.ps1 comment <N> <file> post a UTF-8 file as a comment
|
|
param([Parameter(Mandatory = $true)][string]$Cmd, [string]$A1, [string]$A2)
|
|
$ErrorActionPreference = 'Stop'
|
|
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
$Utf8 = New-Object Text.UTF8Encoding $false
|
|
$Base = 'https://git.copse.top/api/v1'
|
|
$Api = "$Base/repos/root/Jyotisha"
|
|
$Token = ([IO.File]::ReadAllText((Join-Path $HOME '.config\jyotisha-agent\gitea-token'))).Trim()
|
|
|
|
function Call([string]$Method, [string]$Url, $Body = $null) {
|
|
$p = @{ Uri = $Url; Method = $Method; UseBasicParsing = $true; Headers = @{ Authorization = "token $Token" } }
|
|
if ($null -ne $Body) {
|
|
$p.Body = $Utf8.GetBytes(($Body | ConvertTo-Json -Depth 10 -Compress))
|
|
$p.ContentType = 'application/json; charset=utf-8'
|
|
}
|
|
$text = $Utf8.GetString((Invoke-WebRequest @p).RawContentStream.ToArray())
|
|
if ($text) { $text | ConvertFrom-Json } else { $null }
|
|
}
|
|
function Owner { (Call GET "$Base/user").login }
|
|
function LabelId([string]$name) { (@(Call GET "$Api/labels?limit=100") | Where-Object { $_.name -eq $name } | Select-Object -First 1).id }
|
|
function Json($o) { $o | ConvertTo-Json -Depth 10 }
|
|
|
|
switch ($Cmd) {
|
|
'whoami' { Owner }
|
|
'issues' { Json @(@(Call GET "$Api/issues?state=open&type=issues&limit=50&labels=$([uri]::EscapeDataString($A1))") |
|
|
Sort-Object created_at | ForEach-Object { [ordered]@{ number = $_.number; title = $_.title; labels = @($_.labels | ForEach-Object { $_.name }) } }) }
|
|
'issue' { $o = Owner; $i = Call GET "$Api/issues/$A1"
|
|
Json ([ordered]@{ number = $i.number; title = $i.title; body = $(if ($i.user.login -eq $o) { $i.body } else { '' }) }) }
|
|
'labels' { @(Call GET "$Api/issues/$A1/labels") | ForEach-Object { $_.name } }
|
|
'add-label' { Call POST "$Api/issues/$A1/labels" @{ labels = @($A2) } | Out-Null; "added $A2" }
|
|
'remove-label' { $id = LabelId $A2; if ($id -and (@(Call GET "$Api/issues/$A1/labels") | Where-Object { $_.id -eq $id })) { Call DELETE "$Api/issues/$A1/labels/$id" | Out-Null }; "removed $A2" }
|
|
'comments' { $o = Owner; Json @(@(Call GET "$Api/issues/$A1/comments") | Where-Object { $_.user.login -eq $o } | ForEach-Object { [ordered]@{ created_at = $_.created_at; body = $_.body } }) }
|
|
'comment' { Call POST "$Api/issues/$A1/comments" @{ body = [IO.File]::ReadAllText($A2, [Text.Encoding]::UTF8) } | Out-Null; "commented on #$A1" }
|
|
default { throw "unknown command: $Cmd" }
|
|
}
|