Skip to content
This repository has been archived by the owner on Aug 12, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from symboxtra/more-build-information
Browse files Browse the repository at this point in the history
More build information
  • Loading branch information
jmcker authored Jul 14, 2018
2 parents 4008842 + 2c35fb4 commit 95ac74f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 26 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ just follow the guide below and stay notified of your build status.

```yaml
after_success:
- wget https://raw.githubusercontent.com/k3rn31p4nic/travis-ci-discord-webhook/master/send.sh
- wget https://raw.githubusercontent.com/symboxtra/travis-ci-discord-webhook/master/send.sh
- chmod +x send.sh
- ./send.sh success $WEBHOOK_URL
after_failure:
- wget https://raw.githubusercontent.com/k3rn31p4nic/travis-ci-discord-webhook/master/send.sh
- wget https://raw.githubusercontent.com/symboxtra/travis-ci-discord-webhook/master/send.sh
- chmod +x send.sh
- ./send.sh failure $WEBHOOK_URL
```
Expand All @@ -50,4 +50,5 @@ please consider opening an issue and I'll fix it ASAP.
- If you want to improve the scripts, feel free to open a pull request.
### See Also
- [AppVeyor -> Discord Webhook](https://github.com/k3rn31p4nic/appveyor-discord-webhook)
- [AppVeyor -> Discord Webhook](https://github.com/symboxtra/appveyor-discord-webhook)
- [Jenkins -> Discord Webhook](https://github.com/symboxtra/jenkins-discord-webhook)
122 changes: 99 additions & 23 deletions send.sh
Original file line number Diff line number Diff line change
@@ -1,73 +1,140 @@
#!/bin/bash
# Modified: Symboxtra Software
# Author: Sankarsan Kampa (a.k.a. k3rn31p4nic)
# License: MIT

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/symboxtra/travis-ci-discord-webhook" && exit
WEBHOOK_VERSION="2.0.0.0"

STATUS="$1"
WEBHOOK_URL="$2"
CURRENT_TIME=`date +%s`

unamestr=`uname`
if [[ "$unamestr" == 'Darwin' ]]; then
OS_NAME="OSX"
else
OS_NAME="Linux"
fi

if [ -z "$STATUS" ]; then
echo -e "WARNING!!"
echo -e "You need to pass the WEBHOOK_URL environment variable as the second argument to this script."
echo -e "For details & guide, visit: https://github.com/symboxtra/travis-ci-discord-webhook"
exit
fi

echo -e "[Webhook]: Sending webhook to Discord...\\n";
echo -e "[Webhook]: Sending webhook to Discord..."

case $1 in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Passed"
AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-blue.png"
;;

"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Failed"
AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-red.png"
;;

* )
EMBED_COLOR=0
STATUS_MESSAGE="Status Unknown"
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")"
if [ -z "$TRAVIS_COMMIT" ]; then
TRAVIS_COMMIT="$(git log -1 --pretty="%H")"
fi

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")"
COMMIT_TIME="$(git log -1 "${TRAVIS_COMMIT}" --pretty="%ct")"


if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then
CREDITS="$AUTHOR_NAME authored & committed"
else
CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed"
fi

if [ "$TRAVIS_OS_NAME" == "osx" ]; then
OS_NAME="OSX"
elif [ "$TRAVIS_OS_NAME" == "linux" ]; then
OS_NAME="Linux"
# Calculate approximate build time based on commit
DISPLAY_TIME=$(date -u -d "0 $CURRENT_TIME seconds - $COMMIT_TIME seconds" +"%M:%S")


# Regex match co-author names
if [[ "$COMMIT_MESSAGE" =~ Co-authored-by: ]]; then
IFS=$'\n'
CO_AUTHORS=($(echo "$COMMIT_MESSAGE" | grep -o "\([A-Za-z]\+ \)\+[A-Za-z]\+"))

if [ ${#CO_AUTHORS[@]} -gt 0 ]; then
IFS=","
CO_AUTHORS="${CO_AUTHORS[*]}"
CO_AUTHORS="${CO_AUTHORS//,/, }"
fi
unset IFS
else
OS_NAME="$TRAVIS_OS_NAME"
CO_AUTHORS="None"
fi

if [[ $TRAVIS_PULL_REQUEST != false ]]; then
URL="https://github.com/$TRAVIS_REPO_SLUG/pull/$TRAVIS_PULL_REQUEST"
REPO_SLUG=$TRAVIS_REPO_SLUG
# Replace git hashes in merge commits
IFS=$'\n'
MATCHES=($(echo "$COMMIT_SUBJECT" | grep -o "Merge \w\{40\}\b into \w\{40\}\b"))
if [ "${#MATCHES[@]}" -gt 0 ]; then
IS_PR=true
MATCHES=($(echo "$COMMIT_SUBJECT" | grep -o "\w\{40\}\b"))
for MATCH in "${MATCHES[@]}"
do
HASH="$MATCH"
BRANCH_NAME="$(git name-rev $HASH --name-only)"
COMMIT_SUBJECT="${COMMIT_SUBJECT//$HASH/${BRANCH_NAME:-$HASH}}"
done
fi
unset IFS

# Remove repo owner: symboxtra/project -> project
REPO_NAME=${TRAVIS_REPO_SLUG#*/}

#Create appropriate link
if [[ $TRAVIS_PULL_REQUEST != false ]] || [[ -n "$IS_PR" ]]; then
URL="https://github.com/$TRAVIS_REPO_SLUG/pull/$TRAVIS_PULL_REQUEST"
else
URL="https://github.com/$TRAVIS_REPO_SLUG/commit/$TRAVIS_COMMIT"
REPO_SLUG=${TRAVIS_REPO_SLUG#*/} # Remove repo owner: symboxtra/project -> project
URL="https://github.com/$TRAVIS_REPO_SLUG/commit/$TRAVIS_COMMIT"
fi


TIMESTAMP=$(date --utc +%FT%TZ)
WEBHOOK_DATA='{
"username": "",
"avatar_url": "https://travis-ci.org/images/logos/TravisCI-Mascot-1.png",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "#'"$TRAVIS_BUILD_NUMBER"' - '"$REPO_SLUG"' - '"$OS_NAME"' - '"$STATUS_MESSAGE"'",
"name": "#'"$TRAVIS_BUILD_NUMBER"' - '"$REPO_NAME"' - '"$STATUS_MESSAGE"'",
"url": "https://travis-ci.org/'"$TRAVIS_REPO_SLUG"'/builds/'"$TRAVIS_BUILD_ID"'",
"icon_url": "'$AVATAR'"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"description": "'"${COMMIT_MESSAGE}"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "OS",
"value": "'"$OS_NAME"'",
"inline": true
},
{
"name": "Build Time",
"value": "'"~$DISPLAY_TIME"'",
"inline": true
},
{
"name": "Build ID",
"value": "'"${TRAVIS_BUILD_NUMBER}CI"'",
"inline": true
},
{
"name": "Commit",
"value": "'"[\`${TRAVIS_COMMIT:0:7}\`](https://github.com/$TRAVIS_REPO_SLUG/commit/$TRAVIS_COMMIT)"'",
Expand All @@ -77,12 +144,21 @@ WEBHOOK_DATA='{
"name": "Branch/Tag",
"value": "'"[\`$TRAVIS_BRANCH\`](https://github.com/$TRAVIS_REPO_SLUG/tree/$TRAVIS_BRANCH)"'",
"inline": true
},
{
"name": "Co-Authors",
"value": "'"$CO_AUTHORS"'",
"inline": true
}
],
"footer": {
"text": "'"v$WEBHOOK_VERSION"'"
},
"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."


(curl -v --fail --progress-bar -A "TravisCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$WEBHOOK_URL" \
&& echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook."

0 comments on commit 95ac74f

Please sign in to comment.