-
Notifications
You must be signed in to change notification settings - Fork 15
/
send.sh
107 lines (91 loc) · 3.48 KB
/
send.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# This original source of this code: https://github.com/DiscordHooks/travis-ci-discord-webhook
# The same functionality from TravisCI is needed for Github Actions
#
# For info on the GITHUB prefixed variables, visit:
# https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables
AVATAR="https://github.com/actions.png"
# More info: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
case ${1,,} in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Passed"
;;
"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Failed"
;;
* )
STATUS_MESSAGE="Status Unknown"
EMBED_COLOR=0
;;
esac
shift
if [ $# -lt 1 ]; 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/github-actions-discord-webhook" && exit
fi
AUTHOR_NAME="$(git log -1 "$GITHUB_SHA" --pretty="%aN")"
COMMITTER_NAME="$(git log -1 "$GITHUB_SHA" --pretty="%cN")"
COMMIT_SUBJECT="$(git log -1 "$GITHUB_SHA" --pretty="%s")"
COMMIT_MESSAGE="$(git log -1 "$GITHUB_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g'
COMMIT_URL="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
# If, for example, $GITHUB_REF = refs/heads/feature/example-branch
# Then this sed command returns: feature/example-branch
BRANCH_NAME="$(echo $GITHUB_REF | sed 's/^[^/]*\/[^/]*\///g')"
REPO_URL="https://github.com/$GITHUB_REPOSITORY"
BRANCH_OR_PR="Branch"
BRANCH_OR_PR_URL="$REPO_URL/tree/$BRANCH_NAME"
ACTION_URL="$COMMIT_URL/checks"
COMMIT_OR_PR_URL=$COMMIT_URL
if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then
CREDITS="$AUTHOR_NAME authored & committed"
else
CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed"
fi
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
BRANCH_OR_PR="Pull Request"
PR_NUM=$(sed 's/\/.*//g' <<< $BRANCH_NAME)
BRANCH_OR_PR_URL="$REPO_URL/pull/$PR_NUM"
BRANCH_NAME="#${PR_NUM}"
# Call to GitHub API to get PR title
PULL_REQUEST_ENDPOINT="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUM"
WORK_DIR=$(dirname ${BASH_SOURCE[0]})
PULL_REQUEST_TITLE=$(ruby $WORK_DIR/get_pull_request_title.rb $PULL_REQUEST_ENDPOINT)
COMMIT_SUBJECT=$PULL_REQUEST_TITLE
COMMIT_MESSAGE="Pull Request #$PR_NUM"
ACTION_URL="$BRANCH_OR_PR_URL/checks"
COMMIT_OR_PR_URL=$BRANCH_OR_PR_URL
fi
TIMESTAMP=$(date -u +%FT%TZ)
WEBHOOK_DATA='{
"avatar_url": "'$AVATAR'",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "'"$STATUS_MESSAGE"': '"$WORKFLOW_NAME"' ('"${HOOK_OS_NAME}"') - '"$GITHUB_REPOSITORY"'",
"url": "'$ACTION_URL'",
"icon_url": "'$AVATAR'"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$COMMIT_OR_PR_URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "Commit",
"value": "'"[\`${GITHUB_SHA:0:7}\`](${COMMIT_URL})"'",
"inline": true
},
{
"name": "'"$BRANCH_OR_PR"'",
"value": "'"[\`${BRANCH_NAME}\`](${BRANCH_OR_PR_URL})"'",
"inline": true
}
],
"timestamp": "'"$TIMESTAMP"'"
} ]
}'
for ARG in "$@"; do
echo -e "[Webhook]: Sending webhook to Discord...\\n";
(curl --fail --progress-bar -A "GitHub-Actions-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "${WEBHOOK_DATA// / }" "$ARG" \
&& echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook."
done