forked from Amadevus/pwsh-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
name: Demo command | ||
# trigger by chatops '/demo' followed by a PowerShell script in the comment body | ||
# example: | ||
# /demo | ||
# ```powershell | ||
# Write-Host "shows up in a log" | ||
# ``` | ||
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@v1 | ||
with: | ||
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | ||
body: '[Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})' | ||
- name: Get script text | ||
uses: Amadevus/pwsh-script@v1 | ||
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 -SkipFirst 1 | ||
$body = $bodyLines -join "`n" | ||
$md = ConvertFrom-Markdown $body | ||
using namespace Markdig.Syntax | ||
$codeBlocks = [MarkdownObjectExtensions]::Descendants($md.Tokens) | Where-Object { $_ -is [CodeBlock] } | ||
if ($codeBlocks) { | ||
$scriptAst = $codeBlocks | Select-Object -First 1 | ||
$script = $scriptAst.Lines.ToString() | ||
} | ||
else { | ||
$script = $body | ||
} | ||
# construct safe github ctx without token | ||
$githubSafe = $github.Clone() | ||
$githubSafe.Remove('token') | ||
Set-ActionOutput githubSafe $githubSafe | ||
return $script | ||
- name: Execute user script | ||
uses: Amadevus/pwsh-script@v1 | ||
id: user-script | ||
with: | ||
github: ${{ steps.get-script-text.outputs.githubSafe }} | ||
script: ${{ steps.get-script-text.outputs.result }} | ||
- name: Prettify result json | ||
uses: Amadevus/pwsh-script@v1 | ||
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@v1 | ||
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@v1 | ||
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@v1 | ||
if: failure() | ||
with: | ||
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | ||
reactions: "-1" |