Skip to content

Commit

Permalink
ci: Add /demo command
Browse files Browse the repository at this point in the history
  • Loading branch information
amis92 committed Jun 11, 2020
1 parent aeda15d commit a35b360
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/chatops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ jobs:
"command": "tag",
"permission": "admin",
"named_args": true
},
{
"command": "demo",
"permission": "read"
}
]
90 changes: 90 additions & 0 deletions .github/workflows/demo-command.yml
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"

0 comments on commit a35b360

Please sign in to comment.