Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Perforce Discord Webhook
This is a script that posts a message to Discord every time a new changelist is submitted from the Perforce version control system.

![image](https://user-images.githubusercontent.com/23201434/79544589-a1a1a900-8065-11ea-8795-1c95aba5b91a.png)
![image](./discord_message.png)

## Requirements:
1. This was only tested with p4d running on a linux system (Ubuntu 18.04)
Expand Down Expand Up @@ -37,7 +37,7 @@ That's the easy part.
1. Clone the repo, or [download the script](https://raw.githubusercontent.com/saadbruno/perforce-discord-webhook/master/perforce_discord_webhook.sh) and save it somewhere where the perforce user (or whatever user your server is running) has access to.
2. Make sure the script is executable by running `chmod u+x perforce_discord_webhook.sh`

At this point, you should already be able to run the script manually with `./perforce_discord_webhook.sh <changelist number> <discord webhook link>`
At this point, you should already be able to run the script manually with `./perforce_discord_webhook.sh <changelist number> <discord webhook link> <issue tracker link>`

### 3. Setting up the trigger
Perforce has a Triggers system, where you can configure the server to do actions based on triggers. We are gonna create a trigger that runs this script every time a cahngelist is submitted successfully
Expand All @@ -46,11 +46,11 @@ Perforce has a Triggers system, where you can configure the server to do actions
2. Add a new trigger with these settings:
```
Triggers:
discord change-commit //depot/... "/bin/bash <perforce_discord_webhook.sh location> %changelist% <discord webhook link>
discord change-commit //depot/... "/bin/bash <perforce_discord_webhook.sh location> %changelist% <discord webhook link> <issue tracker link>
```
And replace the location and the webhook links according to your setup. Example:
```
discord change-commit //depot/... "/bin/bash /home/perforce/perforce_discord_webhook.sh %changelist% https://discordapp.com/api/webhooks/<id>/<auth>"
discord change-commit //depot/... "/bin/bash /home/perforce/perforce_discord_webhook.sh %changelist% https://discordapp.com/api/webhooks/<id>/<auth> https://https://<your-company>.atlassian.net/browse"
```
You can also customize this to trigger only on specific directories by changing the `//depot/...` bit.

Expand Down
Binary file added discord_message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 19 additions & 7 deletions perforce_discord_webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

# This script sends a changelist from perforce to a Discord webhook
# USAGE:
# perforce_discord_webhook.sh <changelist number> <discord webhook link>
# perforce_discord_webhook.sh <changelist number> <discord webhook link> <issue tracker 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>"
# discord change-commit //depot/... "/bin/bash /home/perforce/perforce_discord_webhook.sh %changelist% https://discordapp.com/api/webhooks/<id>/<auth> https://<your-company>.atlassian.net/browse"
#
# 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.

Expand All @@ -27,15 +27,24 @@ DESC=$(echo "$OUTPUT" | awk '/^[[:blank:]]/' | sed s/[\'\"]/\\\'/g | awk '{print
# 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)

# parses issue number (like XXX-123) and appends it to tracker url
if [[ "$DESC" =~ ([A-Z]{2,10}-[0-9]{1,7}) ]]
then
ISSUE="${BASH_REMATCH[1]}"
JIRA="$3/$ISSUE"
else
ISSUE="<unknown>"
fi

# 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} ]}]}'
EMBED='{ "username":"P4V","avatar_url":"https://i.imgur.com/unlgXvg.png","embeds":[{ "title":"Change '"$1"' | user: '"$USER"' | issue: '"$ISSUE"'","url":"'"$JIRA"'","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
-X POST \
-d "$EMBED" \
$2

printf "\n\n===== DEBUG =====
:: Linux user:
Expand Down Expand Up @@ -64,4 +73,7 @@ $1

:: Arg 2:
$2
"

:: Arg 3:
$3
"