-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify-slack
executable file
·78 lines (70 loc) · 2.17 KB
/
notify-slack
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
#!/bin/bash
set -e
test -n "$SLACK_WEBHOOK" || {
echo "No SLACK_WEBHOOK defined, skipping." 1>&2
exit 0
}
case "$STATUS" in
success) COLOR="#41aa58" ; VERB="Passed" ;;
failure) COLOR="#ff0000" ; VERB="Failed" ;;
cancelled) COLOR="#ff5800" ; VERB="Cancelled" ;;
esac
GITHUB_REPO_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
if [ -n "$PR_NUMBER" ]; then
TEXT=$( printf "%s _%s_ check <%s|#%s> for <%s|\`%s\`> (<%s|*\`%s\`*>) *by %s*" \
"$VERB" \
"$GITHUB_JOB" \
"$GITHUB_REPO_URL/pull/$PR_NUMBER/checks?sha=$COMMIT_SHA" \
"$GITHUB_RUN_NUMBER" \
"$GITHUB_REPO_URL/commit/${COMMIT_SHA}" \
"${COMMIT_SHA:0:8}" \
"$GITHUB_REPO_URL/tree/$GITHUB_HEAD_REF" \
"$GITHUB_HEAD_REF" \
"$GITHUB_ACTOR"
)
else
TEXT=$( printf "%s _%s_ check <%s|#%s> for <%s|\`%s\`> *by %s*" \
"$VERB" \
"$GITHUB_JOB" \
"$GITHUB_REPO_URL/actions" \
"$GITHUB_RUN_NUMBER" \
"$GITHUB_REPO_URL/commit/${GITHUB_SHA}" \
"${GITHUB_SHA:0:8}" \
"$GITHUB_ACTOR"
)
fi
if [ -n "$TEST_RESULTS" ]; then
TEXT="${TEXT}\n_Tests:_ ${TEST_RESULTS}"
fi
if [ -n "$COVERAGE_RESULTS" ]; then
TEXT="${TEXT}\n_Coverage:_ ${COVERAGE_RESULTS}"
fi
# github actions doesn't expose current job's numeric id, so look it up
GITHUB_JOB_ID=$(
curl -s \
-u "$GITHUB_ACTOR:$TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs" |
# parsing json with simple unix tools is fun :/
sed -nEe 's/^ "(id|name)": "?([^"]*)"?,/\2/p' | tr '\n' ' ' |
sed -Ee 's/([0-9]+) ([^ ]+) ?/\1 \2\n/g' |
awk -v GITHUB_JOB="$GITHUB_JOB" '{ if ($2 == GITHUB_JOB) print $1 }'
)
STARTED=$(
curl -s \
-u "$GITHUB_ACTOR:$TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/jobs/$GITHUB_JOB_ID" |
sed -nEe 's/^ "started_at": "(.*)",/\1/p'
)
ELAPSED=$(( `date +%s` - `date -d "$STARTED" +%s` ))
TEXT="${TEXT}\n_Elapsed:_ ${ELAPSED}s"
curl -f -d @- -H "Content-Type: application/json" "$SLACK_WEBHOOK" <<EOSLACKJSON
{
"attachments": [{
"text": "$TEXT",
"color": "$COLOR",
"footer": "<$GITHUB_REPO_URL|$GITHUB_REPOSITORY>"
}]
}
EOSLACKJSON