-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
149 lines (146 loc) · 4.89 KB
/
action.yaml
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: 'Composite Discord Webhooks'
description: "A composite Github Action for sending messages through Discord's Webhooks"
author: MineBartekSA
branding:
icon: 'message-square'
color: 'orange'
inputs:
webhook:
description: 'Webhook URL'
required: true
content:
description: 'Message content'
required: false
default: ''
username:
description: 'Message author username'
required: false
default: ''
avatar_url:
description: 'Message author avatar'
required: false
default: ''
tts:
description: 'Message TTS flag'
required: false
default: 'false'
allowed_mentions:
description: 'Comma-separated list of allowed mention types'
required: false
default: ''
allowed_user_mentions:
description: 'Comma-separated list of user id, that are allowed to be mentioned'
required: false
default: ''
allowed_role_mentions:
description: 'Comma-separated list of role id, that are allowed to be mentioned'
required: false
default: ''
embed:
description: 'JSON or YAML string of the embed object or array'
required: false
default: ''
component:
description: 'JSON or YAML string of the component object or array'
required: false
default: ''
attachment:
description: 'JSON or YAML string of the attachment object or array with aditional file field indicating the path to the file'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: 'Prepare'
shell: bash
run: echo "DATA={" > $GITHUB_ENV
- name: 'Handle username'
if: inputs.username != ''
shell: bash
env:
USERNAME: ${{ inputs.username }}
run: echo "DATA=$DATA\"username\":\"$USERNAME\"," > $GITHUB_ENV
- name: 'Handle avatar_url'
if: inputs.avatar_url != ''
shell: bash
env:
AVATAR_URL: ${{ inputs.avatar_url }}
run: echo "DATA=$DATA\"avatar_url\":\"$(echo $AVATAR_URL)\"," > $GITHUB_ENV
- name: 'Handle tts'
if: inputs.tts == 'true'
shell: bash
run: echo "DATA=$DATA\"tts\":true," > $GITHUB_ENV
- name: 'Handle allowed_mentions'
if: inputs.allowed_mentions != '' || inputs.allowed_user_mentions != '' || inputs.allowed_role_mentions != ''
shell: bash
env:
ALLOWED_MENTIONS: ${{ inputs.allowed_mentions }}
ALLOWED_USERS: ${{ inputs.allowed_user_mentions }}
ALLOWED_ROLES: ${{ inputs.allowed_role_mentions }}
run: |
function split {
TMP="\"$1\":["
for allow in ${2//,/ }
do
TMP="$TMP\"$allow\","
done
if [ "${TMP: -1}" != "[" ]
then
TMP="${TMP::-1}"
fi
echo "$TMP]"
}
TMP="{"
[ "$ALLOWED_MENTIONS" != "" ] && TMP="$TMP$(split "parse" "$ALLOWED_MENTIONS"),"
[ "$ALLOWED_USERS" != "" ] && TMP="$TMP$(split "users" "$ALLOWED_USERS"),"
[ "$ALLOWED_ROLES" != "" ] && TMP="$TMP$(split "roles" "$ALLOWED_ROLES"),"
[ "$TMP" == "{" ] && TMP="{}" || TMP="${TMP:0:-1}}"
echo "DATA=$DATA\"allowed_mentions\":$TMP," > $GITHUB_ENV
- name: 'Handle embed'
if: inputs.embed != ''
shell: bash
env:
EMBED: ${{ inputs.embed }}
run: |
EMBED=$(ruby $GITHUB_ACTION_PATH/utils.ru handleJson "$EMBED")
echo "DATA=$DATA\"embeds\":$EMBED," > $GITHUB_ENV
- name: 'Handle component'
if: inputs.component != ''
shell: bash
env:
COMPONENT: ${{ inputs.component }}
run: |
COMPONENT=$(ruby $GITHUB_ACTION_PATH/utils.ru handleJson "$COMPONENT" comp)
echo "DATA=$DATA\"components\":$COMPONENT," > $GITHUB_ENV
- name: 'Handle attachment'
if: inputs.attachment != ''
shell: bash
env:
ATTACHMENT: ${{ inputs.attachment }}
run: |
IFS=$'\n' FILE_DATA=( $(ruby $GITHUB_ACTION_PATH/utils.ru handleJson "$ATTACHMENT" file) )
echo "DATA=$DATA\"attachments\":${FILE_DATA[0]}," > $GITHUB_ENV
echo "FORM=${FILE_DATA[1]}" >> $GITHUB_ENV
- name: 'Send webhook message'
shell: bash
env:
CONTENT: ${{ inputs.content }}
run: |
DATA="$DATA\"content\":\"$(ruby $GITHUB_ACTION_PATH/utils.ru literalNewline "$CONTENT")\"}"
TMP=$(mktemp)
OUT=$(curl -w "%{stderr}HTTP/%{http_version} %{http_code}\n" \
-F payload_json="$DATA" \
$FORM \
${{ inputs.webhook }} 2> $TMP)
cat $TMP
if [[ "$(tail -n 1 $TMP | cut -d" " -f2)" != 2* ]]
then
echo "::error title=Faild to send message::Discord did not respond with expected status."
echo "## Discord Error" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
ERR=$(ruby $GITHUB_ACTION_PATH/utils.ru prettyJson "$OUT")
echo "$ERR"
echo "$ERR" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
exit 22
fi