-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperforce_discord_webhook.sh
executable file
·67 lines (51 loc) · 1.87 KB
/
perforce_discord_webhook.sh
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
#!/bin/bash
# This script sends a changelist from perforce to a Discord webhook
# USAGE:
# perforce_discord_webhook.sh <changelist number> <discord webhook link>
#
# This chan be used in conjunction with the p4 triggers, so everytime a new changelist is send, this script is run automatically
# p4 triggers example config:
# Triggers:
# discord change-commit //depot/... "/bin/bash /home/perforce/perforce_discord_webhook.sh %changelist% https://discordapp.com/api/webhooks/<id>/<auth>"
#
# Note that for the p4 triggers command to work, the linux user running the p4d needs to have access to the script, and the p4 user running "p4 describe" needs read access to the depot.
# Uncomment this to enable debugging
exec &> $(dirname "$0")/output.log
printf ":: Running perforce_discord_webhook.sh\n\n"
OUTPUT=$(p4 describe -s $1)
# echoes the output
# | only select the indented lines (which is basically the description)
# | escapes quotes
# | Converts new lines to \n (so they are sent as proper line breaks in discord)
# collapses all tabs and spaces to a single space
DESC=$(echo "$OUTPUT" | awk '/^[[:blank:]]/' | sed s/[\'\"]/\\\'/g | awk '{printf "%s\\n", $0}' | tr -s [:space:] ' ')
# this selects the user of the commit, which is basicaly the 4th column of the first line of the changelist
USER=$(echo "$OUTPUT" | head -n 1 | cut -d" " -f4)
# builds the embed
EMBED='{ "username":"P4V","avatar_url":"https://i.imgur.com/unlgXvg.png","embeds":[{ "title":"Change '"$1"' by '"$USER"'","color":"701425","fields":[{ "name":"Description","value":"'"$DESC"'","inline":false} ]}]}'
# sends it
printf ":: Sending webhook...\n\n"
curl -H "Content-Type: application/json" \
-X POST \
-d "$EMBED" \
$2
printf "\n\n===== DEBUG =====
:: Linux user:
$(whoami)
:: Linux path:
$(pwd)
:: p4 info:
$(p4 info)
:: output:
$OUTPUT
:: desc:
$DESC
:: user:
$USER
:: embed:
$EMBED
:: Arg 1:
$1
:: Arg 2:
$2
"