Skip to content

Merge pull request #854 from rpadesign/patch-1 #7

Merge pull request #854 from rpadesign/patch-1

Merge pull request #854 from rpadesign/patch-1 #7

name: Telegram New Components
on:
workflow_dispatch: {}
push:
branches:
- master
paths:
- "docs/components/**"
jobs:
notify:
if: github.repository_owner == 'modx-pro'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect new components
id: detect
shell: bash
run: |
set -euo pipefail
BEFORE_SHA="${{ github.event.before }}"
AFTER_SHA="${{ github.sha }}"
if [[ -z "$BEFORE_SHA" || "$BEFORE_SHA" == "0000000000000000000000000000000000000000" ]]; then
BEFORE_SHA="$(git rev-parse "${AFTER_SHA}^" 2>/dev/null || true)"
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//</&lt;}"
text="${text//>/&gt;}"
text="${text//\"/&quot;}"
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")"
MESSAGE+="\n<b>${title}</b> — ${description}\n"
MESSAGE+="Автор: ${author}\n"
MESSAGE+="<a href=\"https://docs.modx.pro/components/${slug}/\">Открыть в документации</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 }}