Skip to content

Instantly share code, notes, and snippets.

@BlueSlimee
Last active January 7, 2019 14:05
Show Gist options
  • Save BlueSlimee/f2651a4d8ca447230ba52380f2fa30da to your computer and use it in GitHub Desktop.
Save BlueSlimee/f2651a4d8ca447230ba52380f2fa30da to your computer and use it in GitHub Desktop.
Travis CI webhook melhorado, em português
#!/bin/bash
if [ -z "$2" ]; then
echo -e "WARNING!!\nYou need to pass the WEBHOOK_URL environment variable as the second argument to this script.\nFor details & guide, visit: https://github.com/DiscordHooks/travis-ci-discord-webhook" && exit
fi
echo -e "[Webhook]: Sending webhook to Discord...\\n";
case $1 in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Sucesso"
AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-blue.png"
;;
"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Falha"
AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-red.png"
;;
* )
EMBED_COLOR=0
STATUS_MESSAGE="Desconhecido"
AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-1.png"
;;
esac
AUTHOR_NAME="$(git log -1 "$TRAVIS_COMMIT" --pretty="%aN")"
COMMITTER_NAME="$(git log -1 "$TRAVIS_COMMIT" --pretty="%cN")"
COMMIT_SUBJECT="$(git log -1 "$TRAVIS_COMMIT" --pretty="%s")"
COMMIT_MESSAGE="$(git log -1 "$TRAVIS_COMMIT" --pretty="%b")"
RANDOM=$$$(date +%s)
TRAVIS_LOGOS=("https://travis-ci.com/images/logos/TravisCI-Mascot-1.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-2.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-3.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-4.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-grey.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-blue.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-red.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-pride.png", "https://travis-ci.com/images/logos/TravisCI-Mascot-pride-4.png", "https://travis-ci.com/images/logos/Tessa-1.png", "https://travis-ci.com/images/logos/Tessa-2.png", "https://travis-ci.com/images/logos/Tessa-3.png", "https://travis-ci.com/images/logos/Tessa-4.png", "https://travis-ci.com/images/logos/Tessa-pride.png", "https://travis-ci.com/images/logos/Tessa-pride-4.png")
TRAVIS_CHOOSEN_LOGO=${TRAVIS_LOGOS[$RANDOM % ${#TRAVIS_LOGOS[@]} ]}
if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then
CREDITS="$AUTHOR_NAME foi responsável pela build & deu commit"
else
CREDITS="$AUTHOR_NAME foi o responsável pela build & $COMMITTER_NAME deu commit"
fi
if [[ $TRAVIS_PULL_REQUEST != false ]]; then
URL="https://github.com/$TRAVIS_REPO_SLUG/pull/$TRAVIS_PULL_REQUEST"
else
URL=""
fi
TIMESTAMP=$(date --utc +%FT%TZ)
WEBHOOK_DATA='{
"username": "Travis CI",
"avatar_url": "'"$TRAVIS_CHOOSEN_LOGO"'",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "Job #'"$TRAVIS_JOB_NUMBER"' (Build #'"$TRAVIS_BUILD_NUMBER"') '"$STATUS_MESSAGE"' - '"$TRAVIS_REPO_SLUG"'",
"url": "'"$TRAVIS_BUILD_WEB_URL"'",
"icon_url": "'$AVATAR'"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "Commit",
"value": "'"[\`${TRAVIS_COMMIT:0:7}\`](https://github.com/$TRAVIS_REPO_SLUG/commit/$TRAVIS_COMMIT)"'",
"inline": true
},
{
"name": "Branch/Tag",
"value": "'"[\`$TRAVIS_BRANCH\`](https://github.com/$TRAVIS_REPO_SLUG/tree/$TRAVIS_BRANCH)"'",
"inline": true
},
{
"name": "Versão do node.js usada",
"value": "'"$TRAVIS_NODE_VERSION"'",
"inline": true
}
],
"timestamp": "'"$TIMESTAMP"'"
} ]
}'
(curl --fail --progress-bar -A "TravisCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$2" \
&& echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment