-
Notifications
You must be signed in to change notification settings - Fork 201
113 lines (98 loc) · 4.31 KB
/
command_dispatch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
on:
issue_comment: # applies to PR and issue comments
types: [created, edited]
name: Slash command dispatch
jobs:
preprocessing:
name: Check for slash command
runs-on: ubuntu-latest
permissions:
# keep this limited to read permissions for the content only!
contents: read
outputs:
react: ${{ steps.parse.outputs.react }}
command_type: ${{ steps.parse.outputs.command_type }}
steps:
- uses: actions/checkout@v4
- id: parse
run: |
if [ "${COMMENT_BODY:0:1}" == "/" ]; then
echo "react=true" >> $GITHUB_OUTPUT
json_config=`cat .github/workflows/config/command_dispatch_config.json | sed 's/{{.*}}//g'`
entries=`echo "$json_config" | jq -c '.[]'`
for entry in $entries; do
command_name=`echo $entry | jq -r '.command'`
if [ "${COMMENT_BODY#/$command_name}" != "$COMMENT_BODY" ]; then
command_suffix=`echo $entry | jq -r '.event_type_suffix'`
echo "command_type=${command_name}${command_suffix:--command}" >> $GITHUB_OUTPUT
fi
done
fi
env:
COMMENT_BODY: ${{ github.event.comment.body }}
command_dispatch:
name: Dispatch slash commands
needs: preprocessing
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Update configuration
id: config
run: |
pr_url=$(printf '%s\n' "${{ github.event.issue.pull_request.url }}" | sed -e 's/[\/&]/\\&/g')
github_repo=$(printf '%s\n' "${{ github.repository }}" | sed -e 's/[\/&]/\\&/g')
cat .github/workflows/config/command_dispatch_config.json \
| sed "s/.{{[ ]*github.event.issue.pull_request.url[ ]*}}/$pr_url/g" \
| sed "s/.{{[ ]*github.event.comment.id[ ]*}}/${{ github.event.comment.id }}/g" \
| sed "s/.{{[ ]*github.repository[ ]*}}/$github_repo/g" > /tmp/command_dispatch_config.json
- name: React to comment
if: needs.preprocessing.outputs.react == 'true'
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ github.event.comment.id }}
body: |
> ${{ github.event.comment.body }}
**Command Bot:** Processing...
edit-mode: replace
reactions: eyes
- name: Dispatch
id: dispatch
uses: peter-evans/slash-command-dispatch@v3
with:
token: ${{ github.token }}
config-from-file: /tmp/command_dispatch_config.json
continue-on-error: true
- name: Edit comment with error message
if: steps.dispatch.outputs.error-message
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ github.event.comment.id }}
body: |
> ${{ github.event.comment.body }}
**Command Bot:** Could not process command.
${{ steps.dispatch.outputs.error-message }}
# We replace the original comment to avoid an infinite loop
edit-mode: replace
reactions-edit-mode: replace
reactions: |
- name: Indicate dispatch failure
if: ${{ needs.preprocessing.outputs.react == 'true' && steps.dispatch.outcome == 'failure' && ! steps.dispatch.outputs.error-message }}
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ github.event.comment.id }}
body: |
> ${{ github.event.comment.body }}
**Command Bot:** Failed to dispatch command. For more information about available commands, use the command /help.
# We replace the original comment to avoid an infinite loop
edit-mode: replace
reactions-edit-mode: replace
reactions: |
- name: Indicate successful dispatch
if: ${{ needs.preprocessing.outputs.command_type != '' && steps.dispatch.outcome == 'success' && ! steps.dispatch.outputs.error-message }}
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ github.event.comment.id }}
body: |
The launched workflow can be found [here](https://github.com/${{ github.event.repository.full_name }}/actions/workflows/${{ needs.preprocessing.outputs.command_type }}.yml).
edit-mode: append