-
-
Notifications
You must be signed in to change notification settings - Fork 128
165 lines (145 loc) · 5.45 KB
/
telegram-new-components.yml
File metadata and controls
165 lines (145 loc) · 5.45 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
153
154
155
156
157
158
159
160
161
162
163
164
165
name: Telegram New Components
on:
workflow_dispatch: {}
workflow_run:
workflows:
- Deploy
types:
- completed
jobs:
notify:
if: |
github.repository_owner == 'modx-pro' &&
(
github.event_name != 'workflow_run' ||
(
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'master'
)
)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
- name: Detect new components
id: detect
shell: bash
run: |
set -euo pipefail
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
AFTER_SHA="${{ github.event.workflow_run.head_sha }}"
BEFORE_SHA="$(git rev-parse "${AFTER_SHA}^" 2>/dev/null || true)"
else
AFTER_SHA="${{ github.sha }}"
BEFORE_SHA="${{ github.event.before }}"
if [[ -z "$BEFORE_SHA" || "$BEFORE_SHA" == "0000000000000000000000000000000000000000" ]]; then
BEFORE_SHA="$(git rev-parse "${AFTER_SHA}^" 2>/dev/null || true)"
fi
fi
if [[ -z "$BEFORE_SHA" ]]; then
echo "has_new_components=false" >> "$GITHUB_OUTPUT"
exit 0
fi
ADDED_COMPONENTS="$(
git diff --name-status "$BEFORE_SHA" "$AFTER_SHA" -- docs/components \
| awk '$1 == "A" {print $2}' \
| while IFS= read -r file; do
slug=""
if [[ "$file" =~ ^docs/components/([^/]+)/index\.md$ ]]; then
slug="${BASH_REMATCH[1]}"
elif [[ "$file" =~ ^docs/components/([^/]+)\.md$ ]]; then
slug="${BASH_REMATCH[1]}"
fi
if [[ -n "$slug" && "$slug" != "index" ]]; then
printf "%s|%s\n" "$slug" "$file"
fi
done \
| awk -F'|' '!seen[$1]++' \
| sort -t'|' -k1,1 || true
)"
if [[ -z "$ADDED_COMPONENTS" ]]; then
echo "has_new_components=false" >> "$GITHUB_OUTPUT"
exit 0
fi
trim_quotes() {
local value="$1"
value="${value%\"}"
value="${value#\"}"
value="${value%\'}"
value="${value#\'}"
printf "%s" "$value"
}
extract_frontmatter() {
local key="$1"
local file="$2"
awk -v key="$key" '
BEGIN { in_fm = 0 }
/^---[[:space:]]*$/ {
if (in_fm == 0) { in_fm = 1; next }
exit
}
in_fm == 1 {
line = $0
sub(/^[[:space:]]+/, "", line)
if (line ~ ("^" key ":[[:space:]]*")) {
sub("^" key ":[[:space:]]*", "", line)
print line
exit
}
}
' "$file"
}
escape_html() {
local text="$1"
text="${text//&/&}"
text="${text//</<}"
text="${text//>/>}"
text="${text//\"/"}"
printf "%s" "$text"
}
count=0
MESSAGE="Добавлен новый компонент в документацию\n"
while IFS='|' read -r slug file; do
[[ -z "$slug" ]] && continue
[[ -z "$file" ]] && continue
title="$(extract_frontmatter "title" "$file" || true)"
description="$(extract_frontmatter "description" "$file" || true)"
author="$(extract_frontmatter "author" "$file" || true)"
title="$(trim_quotes "$title")"
description="$(trim_quotes "$description")"
author="$(trim_quotes "$author")"
[[ -z "$title" ]] && title="$slug"
[[ -z "$description" ]] && description="Описание не указано"
[[ -z "$author" ]] && author="Автор не указан"
title="$(escape_html "$title")"
description="$(escape_html "$description")"
author="$(escape_html "$author")"
if [[ "$file" =~ /index\.md$ ]]; then
doc_url="https://docs.modx.pro/components/${slug}/"
else
doc_url="https://docs.modx.pro/components/${slug}"
fi
MESSAGE+="\n<b>${title}</b> — ${description}\n"
MESSAGE+="Автор: ${author}\n"
MESSAGE+="<a href=\"${doc_url}\">Ознакомиться с документацией</a>\n"
count=$((count + 1))
done <<< "$ADDED_COMPONENTS"
if [[ "$count" -gt 1 ]]; then
MESSAGE="Добавлены новые компоненты в документацию\n${MESSAGE#*$'\n'}"
fi
echo "has_new_components=true" >> "$GITHUB_OUTPUT"
{
echo "message<<EOF"
printf "%b\n" "$MESSAGE"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Send telegram notification
if: steps.detect.outputs.has_new_components == 'true'
uses: appleboy/telegram-action@master
with:
to: "-1001051277497"
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
format: html
message: ${{ steps.detect.outputs.message }}