Last active
December 28, 2020 17:01
-
-
Save ObserverOfTime/b6265136119757c75a932ebb3246dda0 to your computer and use it in GitHub Desktop.
Travis CI Discord webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ -z $1 ]]; then | |
printf '[Webhook]: ERROR! Missing webhook URL argument.\n' | |
exit 1 | |
fi | |
if [[ -n $2 ]]; then | |
LANG_VAL="$2" | |
LANG_REF="TRAVIS_${2^^}_VERSION" | |
else | |
LANG_VAL='Bash' | |
LANG_REF='BASH_VERSION' | |
fi | |
printf '[Webhook]: Sending webhook to Discord...\n' | |
if ((TRAVIS_TEST_RESULT == 0)); then | |
STATUS='passed' | |
COLOR=$((0x2ECC71)) | |
ICON='TravisCI-Mascot-grey' | |
else | |
STATUS='failed' | |
COLOR=$((0xE74C3C)) | |
ICON='TravisCI-Mascot-red' | |
fi | |
SLUG="${TRAVIS_PULL_REQUEST_REPO_SLUG:-$TRAVIS_REPO_SLUG}" | |
COMMIT="${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT}" | |
BRANCH="${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}" | |
URL="https://github.com/$SLUG/commit/$COMMIT" | |
TREE="https://github.com/$SLUG/tree/$BRANCH" | |
COMMIT_SUBJECT="$(git log -1 "$COMMIT" --pretty='%s')" | |
COMMIT_MESSAGE="$(git log -1 "$COMMIT" --pretty='%b')" | |
COMMITTER_NAME="$(git log -1 "$COMMIT" --pretty='%cN')" | |
AVATAR="TravisCI-Mascot-$((RANDOM % 4 + 1))" | |
WEBHOOK_DATA=$'{ | |
"username": "Travis CI", | |
"avatar_url": "https://travis-ci.com/images/logos/'$AVATAR'.png", | |
"embeds": [{ | |
"color": "'$COLOR'", | |
"author": { | |
"name": "'$TRAVIS_REPO_SLUG' #'$TRAVIS_JOB_NUMBER'", | |
"url": "'$TRAVIS_JOB_WEB_URL'", | |
"icon_url": "https://travis-ci.com/images/logos/'$ICON'.png" | |
}, | |
"title": "'$COMMIT_SUBJECT'", | |
"url": "'$URL'", | |
"description": "'${COMMIT_MESSAGE//$'\n'/$'\\n'}'", | |
"footer": { | |
"text": "'$COMMITTER_NAME'", | |
"icon_url": "https://github.com/'$COMMITTER_NAME'.png?s=40" | |
}, | |
"fields": [{ | |
"name": "Status", | |
"value": "'$STATUS'", | |
"inline": true | |
}, { | |
"name": "Branch", | |
"value": "[`'${BRANCH}'`]('$TREE')", | |
"inline": true | |
}, { | |
"name": "'$LANG_VAL'", | |
"value": "'${!LANG_REF:-N/A}'", | |
"inline": true | |
}], | |
"timestamp": "'$(date --utc +%FT%TZ)'" | |
}] | |
}' | |
GIST='https://gist.github.com/ObserverOfTime/b6265136119757c75a932ebb3246dda0' | |
if curl -Sf "$1" -d "$WEBHOOK_DATA" \ | |
-A "TravisCI-Webhook ($GIST, v0.2)" \ | |
-H 'Content-Type: application/json'; then | |
printf '[Webhook]: Successfully sent the webhook.\n' | |
else | |
ecode=$? | |
printf '[Webhook]: Failed to send the webhook.\n' | |
exit $ecode | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script expects 1 or 2 arguments:
Here's an example: