-
-
Notifications
You must be signed in to change notification settings - Fork 16
152 lines (139 loc) · 5.31 KB
/
Copy pathdiscord-activity.yml
File metadata and controls
152 lines (139 loc) · 5.31 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: Discord Activity Notification
on:
issues:
types: [opened, closed]
pull_request:
types: [opened, closed]
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send Discord Notification
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
EVENT_TYPE="${{ github.event_name }}"
ACTION="${{ github.event.action }}"
REPO_NAME="${{ github.repository }}"
REPO_URL="https://github.com/${{ github.repository }}"
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
TITLE=""
DESCRIPTION=""
URL=""
AUTHOR_NAME=""
AUTHOR_ICON=""
AUTHOR_URL=""
COLOR=0
STATUS_TEXT=""
LABELS_RAW=""
if [ "$EVENT_TYPE" == "issues" ]; then
TITLE="${{ github.event.issue.title }}"
NUMBER="${{ github.event.issue.number }}"
URL="${{ github.event.issue.html_url }}"
DESCRIPTION="${{ github.event.issue.body }}"
AUTHOR_NAME="${{ github.event.issue.user.login }}"
AUTHOR_ICON="${{ github.event.issue.user.avatar_url }}"
AUTHOR_URL="${{ github.event.issue.user.html_url }}"
LABELS_RAW='${{ toJson(github.event.issue.labels) }}'
if [ "$ACTION" == "opened" ]; then
STATUS_TEXT="Issue Opened"
COLOR=5793266
elif [ "$ACTION" == "closed" ]; then
STATUS_TEXT="Issue Closed"
COLOR=15548997
fi
elif [ "$EVENT_TYPE" == "pull_request" ]; then
TITLE="${{ github.event.pull_request.title }}"
NUMBER="${{ github.event.pull_request.number }}"
URL="${{ github.event.pull_request.html_url }}"
DESCRIPTION="${{ github.event.pull_request.body }}"
AUTHOR_NAME="${{ github.event.pull_request.user.login }}"
AUTHOR_ICON="${{ github.event.pull_request.user.avatar_url }}"
AUTHOR_URL="${{ github.event.pull_request.user.html_url }}"
LABELS_RAW='${{ toJson(github.event.pull_request.labels) }}'
if [ "$ACTION" == "opened" ]; then
STATUS_TEXT="Pull Request Opened"
COLOR=5793266
elif [ "$ACTION" == "closed" ]; then
IS_MERGED="${{ github.event.pull_request.merged }}"
if [ "$IS_MERGED" == "true" ]; then
STATUS_TEXT="Pull Request Merged"
COLOR=10181046
else
STATUS_TEXT="Pull Request Closed"
COLOR=9807270
fi
fi
fi
LABELS_STRING=$(echo "$LABELS_RAW" | jq -r 'if length > 0 then map("`" + .name + "`") | join(" ") else "None" end')
MAX_DESC_LENGTH=2000
if [ -z "$DESCRIPTION" ]; then
DESCRIPTION="No description provided."
fi
if [ ${#DESCRIPTION} -gt $MAX_DESC_LENGTH ]; then
DESCRIPTION_TRUNCATED="${DESCRIPTION:0:$MAX_DESC_LENGTH}... [Read more]($URL)"
else
DESCRIPTION_TRUNCATED="$DESCRIPTION"
fi
DISCORD_PAYLOAD=$(jq -n \
--arg title "$STATUS_TEXT: #$NUMBER $TITLE" \
--arg description "$DESCRIPTION_TRUNCATED" \
--arg url "$URL" \
--arg color "$COLOR" \
--arg author_name "$AUTHOR_NAME" \
--arg author_icon "$AUTHOR_ICON" \
--arg author_url "$AUTHOR_URL" \
--arg repo "$REPO_NAME" \
--arg repo_url "$REPO_URL" \
--arg timestamp "$TIMESTAMP" \
--arg status "$STATUS_TEXT" \
--arg labels "$LABELS_STRING" \
'{
embeds: [{
title: $title,
description: $description,
url: $url,
color: ($color | tonumber),
fields: [
{
name: "Status",
value: $status,
inline: true
},
{
name: "Author",
value: ("[" + $author_name + "](" + $author_url + ")"),
inline: true
},
{
name: "Labels",
value: $labels,
inline: true
},
{
name: "Repository",
value: ("[" + $repo + "](" + $repo_url + ")"),
inline: false
}
],
author: {
name: $author_name,
icon_url: $author_icon,
url: $author_url
},
timestamp: $timestamp,
footer: {
text: ("Notification from " + $repo)
}
}]
}')
RESPONSE=$(curl -s -w "\n%{http_code}" -H "Content-Type: application/json" \
-d "$DISCORD_PAYLOAD" \
"$DISCORD_WEBHOOK_URL")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
if [ "$HTTP_CODE" -eq 204 ] || [ "$HTTP_CODE" -eq 200 ]; then
echo "Notification sent successfully."
else
echo "Failed to send notification. HTTP Code: $HTTP_CODE"
exit 1
fi