Skip to content

Commit a35b360

Browse files
committed
ci: Add /demo command
1 parent aeda15d commit a35b360

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

.github/workflows/chatops.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ jobs:
1717
"command": "tag",
1818
"permission": "admin",
1919
"named_args": true
20+
},
21+
{
22+
"command": "demo",
23+
"permission": "read"
2024
}
2125
]

.github/workflows/demo-command.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Demo command
2+
# trigger by chatops '/demo' followed by a PowerShell script in the comment body
3+
# example:
4+
# /demo
5+
# ```powershell
6+
# Write-Host "shows up in a log"
7+
# ```
8+
on:
9+
repository_dispatch:
10+
types: [demo-command]
11+
jobs:
12+
run-demo:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Add run link to command comment
16+
uses: peter-evans/create-or-update-comment@v1
17+
with:
18+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
19+
body: '[Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})'
20+
- name: Get script text
21+
uses: Amadevus/pwsh-script@v1
22+
id: get-script-text
23+
with:
24+
script: |
25+
# get body lines, skip first (command)
26+
$bodyLines = $github.event.client_payload.github.payload.comment.body -split '\r?\n' | Select-Object -SkipFirst 1
27+
$body = $bodyLines -join "`n"
28+
$md = ConvertFrom-Markdown $body
29+
using namespace Markdig.Syntax
30+
$codeBlocks = [MarkdownObjectExtensions]::Descendants($md.Tokens) | Where-Object { $_ -is [CodeBlock] }
31+
if ($codeBlocks) {
32+
$scriptAst = $codeBlocks | Select-Object -First 1
33+
$script = $scriptAst.Lines.ToString()
34+
}
35+
else {
36+
$script = $body
37+
}
38+
# construct safe github ctx without token
39+
$githubSafe = $github.Clone()
40+
$githubSafe.Remove('token')
41+
Set-ActionOutput githubSafe $githubSafe
42+
return $script
43+
- name: Execute user script
44+
uses: Amadevus/pwsh-script@v1
45+
id: user-script
46+
with:
47+
github: ${{ steps.get-script-text.outputs.githubSafe }}
48+
script: ${{ steps.get-script-text.outputs.result }}
49+
- name: Prettify result json
50+
uses: Amadevus/pwsh-script@v1
51+
id: pretty-result
52+
env:
53+
RESULT_JSON: ${{ steps.user-script.outputs.result }}
54+
with:
55+
script: |
56+
$result = $env:RESULT_JSON
57+
if ($result -and $result -match '^[\[\{]') {
58+
try {
59+
return ConvertFrom-Json $result -AsHashtable -NoEnumerate | ConvertTo-Json -Depth 100
60+
}
61+
catch {
62+
Write-Host "Error converting, fallback to returning plain result"
63+
}
64+
}
65+
return $result
66+
- name: Comment with script result in code fence
67+
uses: peter-evans/create-or-update-comment@v1
68+
if: always()
69+
with:
70+
issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
71+
body: |
72+
`result` output:
73+
```
74+
${{ steps.pretty-result.outputs.result }}
75+
```
76+
`error` output:
77+
```
78+
${{ steps.user-script.outputs.error }}
79+
```
80+
- name: Add reaction to command comment on success
81+
uses: peter-evans/create-or-update-comment@v1
82+
with:
83+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
84+
reactions: hooray
85+
- name: Add reaction to command comment on failure
86+
uses: peter-evans/create-or-update-comment@v1
87+
if: failure()
88+
with:
89+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
90+
reactions: "-1"

0 commit comments

Comments
 (0)