openartifacts-publish.ps1 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  2. $OutputEncoding = [System.Text.Encoding]::UTF8
  3. # Publish one HTML file to OpenArtifacts over HTTPS with the license key Copilot
  4. # supplies in the environment.
  5. function Show-Usage {
  6. [Console]::Error.WriteLine('Usage: openartifacts-publish.ps1 publish <html-file> <title> [docId]')
  7. [Console]::Error.WriteLine(' openartifacts-publish.ps1 unshare <docId>')
  8. exit 1
  9. }
  10. $KEY = [Environment]::GetEnvironmentVariable('COPILOT_PLUS_LICENSE_KEY')
  11. if (-not $KEY) {
  12. [Console]::Error.WriteLine('Publishing to OpenArtifacts needs a Copilot Plus license key. Add it in Copilot Settings and try again.')
  13. exit 1
  14. }
  15. $API_HOST = [Environment]::GetEnvironmentVariable('OPENARTIFACTS_API_HOST')
  16. if (-not $API_HOST) { $API_HOST = 'https://api.openartifacts.ai' }
  17. $API_HOST = $API_HOST.TrimEnd('/')
  18. $COMMAND = if ($args.Count -ge 1) { [string]$args[0] } else { '' }
  19. $DOC_ID = ''
  20. switch ($COMMAND) {
  21. 'publish' {
  22. if ($args.Count -ne 3 -and $args.Count -ne 4) { Show-Usage }
  23. $HTML_FILE = [string]$args[1]
  24. $TITLE = [string]$args[2]
  25. if ($args.Count -eq 4) { $DOC_ID = [string]$args[3] }
  26. if (-not (Test-Path -LiteralPath $HTML_FILE -PathType Leaf)) {
  27. [Console]::Error.WriteLine("HTML file not found: $HTML_FILE")
  28. exit 1
  29. }
  30. }
  31. 'unshare' {
  32. if ($args.Count -ne 2) { Show-Usage }
  33. $DOC_ID = [string]$args[1]
  34. }
  35. default { Show-Usage }
  36. }
  37. if ($DOC_ID -and $DOC_ID -notmatch '^[0-9abcdefghjkmnpqrstvwxyz]{16}$') {
  38. [Console]::Error.WriteLine("Invalid OpenArtifacts document id: $DOC_ID")
  39. exit 1
  40. }
  41. $headers = @{ Authorization = "Bearer $KEY" }
  42. try {
  43. if ($COMMAND -eq 'unshare') {
  44. $null = Invoke-WebRequest -UseBasicParsing -Method Delete -Uri "$API_HOST/api/v1/docs/$DOC_ID" -Headers $headers
  45. [Console]::Out.WriteLine('{"docId":"' + $DOC_ID + '","status":"unshared"}')
  46. } else {
  47. $html = [System.IO.File]::ReadAllText($HTML_FILE, (New-Object System.Text.UTF8Encoding($false)))
  48. $body = @{ title = $TITLE; html = $html } | ConvertTo-Json -Compress -Depth 2
  49. $bytes = [System.Text.Encoding]::UTF8.GetBytes($body)
  50. $method = if ($DOC_ID) { 'Put' } else { 'Post' }
  51. $uri = if ($DOC_ID) { "$API_HOST/api/v1/docs/$DOC_ID" } else { "$API_HOST/api/v1/docs" }
  52. $response = Invoke-WebRequest -UseBasicParsing -Method $method -Uri $uri -Headers $headers -ContentType 'application/json; charset=utf-8' -Body $bytes
  53. [Console]::Out.WriteLine([string]$response.Content)
  54. }
  55. } catch {
  56. $status = $null
  57. $detail = ''
  58. if ($_.Exception.Response) {
  59. try { $status = [int]$_.Exception.Response.StatusCode } catch { $status = $null }
  60. try {
  61. $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
  62. $detail = $reader.ReadToEnd()
  63. } catch { $detail = '' }
  64. }
  65. if (-not $detail -and $_.ErrorDetails) { $detail = [string]$_.ErrorDetails.Message }
  66. if ($COMMAND -eq 'unshare' -and $status -eq 404 -and $detail -match '"not_found"') {
  67. # The API's structured not_found means the page is already gone, which is the outcome asked for.
  68. [Console]::Out.WriteLine('{"docId":"' + $DOC_ID + '","status":"unshared"}')
  69. exit 0
  70. }
  71. if ($status) {
  72. [Console]::Error.WriteLine("OpenArtifacts returned HTTP $status")
  73. if ($detail) { [Console]::Error.WriteLine($detail) }
  74. } elseif ($detail) {
  75. [Console]::Error.WriteLine($detail)
  76. } else {
  77. [Console]::Error.WriteLine("Could not reach OpenArtifacts at $API_HOST. " + $_.Exception.Message)
  78. }
  79. exit 1
  80. }
  81. exit 0