demo-command #32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Demo command | |
# trigger by chatops '/demo' followed by a PowerShell script in the comment body | |
# example: | |
# /demo | |
# ```powershell | |
# Write-Host "doesn't show up in a result comment, only in workflow logs" | |
# Write-Output "shows up in a result comment" | |
# ``` | |
on: | |
repository_dispatch: | |
types: [demo-command] | |
jobs: | |
run-demo: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Add run link to command comment | |
uses: peter-evans/create-or-update-comment@v3 | |
with: | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
body: "[Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" | |
- uses: actions/checkout@v4 | |
- name: Get script text | |
uses: ./ | |
id: get-script-text | |
with: | |
script: | | |
# get body lines, skip first (command) | |
$bodyLines = $github.event.client_payload.github.payload.comment.body -split '\r?\n' | Select-Object -Skip 1 | |
$body = $bodyLines -join "`n" | |
$md = $body | ConvertFrom-Markdown | |
$codeBlocks = [Markdig.Syntax.MarkdownObjectExtensions]::Descendants($md.Tokens) | Where-Object { $_ -is [Markdig.Syntax.CodeBlock] } | |
if ($codeBlocks) { | |
$scriptAst = $codeBlocks | Select-Object -First 1 | |
$script = $scriptAst.Lines.ToString() | |
} | |
else { | |
$script = $body | |
} | |
return $script | |
- name: Execute user script | |
uses: ./ | |
id: user-script | |
with: | |
script: | | |
$github.token = $null | |
$github.event.client_payload = $null | |
${{ steps.get-script-text.outputs.result }} | |
- name: Prettify result json | |
uses: ./ | |
id: pretty-result | |
env: | |
RESULT_JSON: ${{ steps.user-script.outputs.result }} | |
with: | |
script: | | |
$result = $env:RESULT_JSON | |
if ($result -and $result -match '^[\[\{]') { | |
try { | |
return ConvertFrom-Json $result -AsHashtable -NoEnumerate | ConvertTo-Json -Depth 100 | |
} | |
catch { | |
Write-Host "Error converting, fallback to returning plain result" | |
} | |
} | |
return $result | |
- name: Comment with script result in code fence | |
uses: peter-evans/create-or-update-comment@v3 | |
if: always() | |
with: | |
issue-number: ${{ github.event.client_payload.github.payload.issue.number }} | |
body: | | |
`result` output: | |
``` | |
${{ steps.pretty-result.outputs.result }} | |
``` | |
`error` output: | |
``` | |
${{ steps.user-script.outputs.error }} | |
``` | |
- name: Add reaction to command comment on success | |
uses: peter-evans/create-or-update-comment@v3 | |
with: | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
reactions: hooray | |
- name: Add reaction to command comment on failure | |
uses: peter-evans/create-or-update-comment@v3 | |
if: failure() | |
with: | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
reactions: "-1" |