Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
b7baa0f
Update github-telegram.yml
Alexsab Jun 2, 2024
044e3a1
Create update_cars.yml
Alexsab Jun 2, 2024
5bc3061
Create output.txt
Alexsab Jun 2, 2024
cfb4b95
Update output.txt
Alexsab Jun 2, 2024
18cf479
Update output.txt
Alexsab Jun 2, 2024
39b53a7
Update output.txt
Alexsab Jun 2, 2024
6537cbf
Update output.txt
Alexsab Jun 2, 2024
5e07b58
Update output.txt
Alexsab Jun 2, 2024
61fde3c
Update github-telegram.yml
Alexsab Jun 2, 2024
c1fc8da
Update output.txt
Alexsab Jun 2, 2024
c9d6320
Update github-telegram.yml
Alexsab Jun 2, 2024
981efbc
Update output.txt
Alexsab Jun 2, 2024
c481fd2
Update github-telegram.yml
Alexsab Jun 2, 2024
593ee2e
Update output.txt
Alexsab Jun 2, 2024
b8d7e60
Update output.txt
Alexsab Jun 2, 2024
8ab53ce
test
Alexsab Jun 2, 2024
2b65e52
Update output.txt
Alexsab Jun 2, 2024
b7f979c
Update output.txt
Alexsab Jun 2, 2024
30a1c0c
t
Alexsab Jun 2, 2024
ee9bf34
tt
Alexsab Jun 2, 2024
2c13a29
ttt
Alexsab Jun 2, 2024
f111975
tttt
Alexsab Jun 2, 2024
c863639
qq
Alexsab Jun 2, 2024
1ee3d96
ww
Alexsab Jun 2, 2024
c6ab87b
Update github-telegram.yml
Alexsab Jun 2, 2024
615e000
Update github-telegram.yml
Alexsab Jun 2, 2024
a9bd484
qwe
Alexsab Jun 2, 2024
9780d62
qwe
Alexsab Jun 2, 2024
dd825a4
Update github-telegram.yml
Alexsab Jun 2, 2024
3dac517
Update github-telegram.yml
Alexsab Jun 2, 2024
13b4f33
Update github-telegram.yml
Alexsab Jun 2, 2024
6cd26f3
Update output.txt
Alexsab Jun 2, 2024
62cf41e
Update github-telegram.yml
Alexsab Jun 2, 2024
ca4da9f
Update output.txt
Alexsab Jun 2, 2024
b91e2e6
Update output.txt
Alexsab Jun 2, 2024
9714e04
Update github-telegram.yml
Alexsab Jun 2, 2024
9e04f0d
Update github-telegram.yml
Alexsab Jun 2, 2024
8b8c325
Update github-telegram.yml
Alexsab Jun 2, 2024
24bcbb8
Update github-telegram.yml
Alexsab Jun 2, 2024
dbd47eb
Update github-telegram.yml
Alexsab Jun 2, 2024
1a2584e
Update github-telegram.yml
Alexsab Jun 2, 2024
9c02225
Update github-telegram.yml
Alexsab Jun 2, 2024
222c914
Update github-telegram.yml
Alexsab Jun 2, 2024
591f849
Update github-telegram.yml
Alexsab Jun 2, 2024
b0028a4
asd
Alexsab Jun 2, 2024
016001f
Update github-telegram.yml
Alexsab Jun 2, 2024
91d47a0
Update github-telegram.yml
Alexsab Jun 2, 2024
b366f3c
Update github-telegram.yml
Alexsab Jun 2, 2024
6133442
Update github-telegram.yml
Alexsab Jun 5, 2024
5f6da4f
Update github-telegram.yml
Alexsab Jun 5, 2024
3f243d6
Update index.html
Alexsab Oct 11, 2024
0d10e89
Update github-telegram.yml
Alexsab Nov 29, 2024
c062b89
Update index.html
Alexsab Nov 29, 2024
d2b831e
Update index.html
Alexsab Nov 29, 2024
c050310
asdasdasdasd
Alexsab Nov 29, 2024
46991d6
Update index.html
Alexsab Nov 29, 2024
0400e1a
Update index.html
Alexsab Nov 29, 2024
b2d5cce
Merge branch 'develop' of https://github.com/Alexsab/alexsab.github.i…
Alexsab Nov 29, 2024
e98139a
Update index.html
Alexsab Nov 29, 2024
5034fca
Update index.html
Alexsab Nov 29, 2024
776e480
Update index.html
Alexsab Nov 29, 2024
65a3384
Update update_cars.yml
Alexsab Nov 29, 2024
f6d3a7b
Update github-telegram.yml
Alexsab Nov 29, 2024
bdae6f1
Update github-telegram.yml
Alexsab Nov 29, 2024
8757075
Update github-telegram.yml
Alexsab Nov 29, 2024
8a842f7
Update github-telegram.yml
Alexsab Nov 29, 2024
8ee2958
Update update_cars.yml
Alexsab Nov 29, 2024
01af96e
Update index.html
Alexsab Nov 29, 2024
a8c8704
Update index.html
Alexsab Nov 29, 2024
6555aa0
Update github-telegram.yml
Alexsab Nov 29, 2024
fc23010
Update index.html
Alexsab Nov 29, 2024
ba305c3
asd
Alexsab Nov 29, 2024
2df69d6
Update index.html
Alexsab Dec 14, 2024
4daced7
move logic to sh
Alexsab Jan 4, 2025
fe81111
Update prepare_commits.sh
Alexsab Jan 4, 2025
d9068b1
fix use br in telegram
Alexsab Jan 4, 2025
3067c8b
use LENGTH for message
Alexsab Jan 4, 2025
6c63454
add trim_quotes
Alexsab Jan 4, 2025
9eb514b
Update README.md
Alexsab Jan 4, 2025
b3c75b7
Update github-telegram.yml
Alexsab Jan 4, 2025
36aeb08
Update package.json
Alexsab Jan 4, 2025
5d14a26
move script to sh
Alexsab Jan 4, 2025
1e8a4d2
add test.sh
Alexsab Jan 4, 2025
d7a5157
fix trim_quotes
Alexsab Jan 4, 2025
03107fd
fix readarray
Alexsab Jan 4, 2025
be54098
fix readarray
Alexsab Jan 4, 2025
f22198c
Update test.sh
Alexsab Jan 4, 2025
559d028
rename test
Alexsab Jan 4, 2025
5b58218
disable log-github-event-goodies
Alexsab Jan 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .github/scripts/sh/prepare_commits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash
# prepare_commits.sh

prepare_commits() {
# Очищаем входные параметры от кавычек
local repository_name=$(trim_quotes "$1")
local ref_name=$(trim_quotes "$2")
local before_sha=$(trim_quotes "$3")
local after_sha=$(trim_quotes "$4")
local actor=$(trim_quotes "$5")
local repository=$(trim_quotes "$6")

# Проверка наличия всех параметров
if [ -z "$repository_name" ] || [ -z "$ref_name" ] || [ -z "$before_sha" ] || [ -z "$after_sha" ] || [ -z "$actor" ] || [ -z "$repository" ]; then
echo "Error: Missing required parameters" >&2
echo "Usage: prepare_commits repository_name ref_name before_sha after_sha actor repository" >&2
return 1
fi

# Максимальная длина сообщения в Telegram
local MAX_LENGTH=4096
# Длина для эллипсиса и дополнительного пробела
local ELLIPSIS_LENGTH=4

# Инициализируем массив
declare -a readarray

if [ "$before_sha" = "0000000000000000000000000000000000000000" ]; then
git fetch origin HEAD || { echo "Error: Failed to fetch git repository" >&2; return 1; }
git checkout HEAD || { echo "Error: Failed to checkout HEAD" >&2; return 1; }

# Читаем вывод git log в массив
mapfile -t readarray < <(git log --pretty=format:"<code>%h</code> - %an, %ar : %s" "HEAD..${after_sha}")
COMPARE_HASH="${after_sha}"
else
# Читаем вывод git log в массив
mapfile -t readarray < <(git log --pretty=format:"<code>%h</code> - %an, %ar : %s" "${before_sha}..${after_sha}")
COMPARE_HASH="${before_sha}..${after_sha}"
fi

TOTAL_COMMITS=${#readarray[@]}

if [ "$TOTAL_COMMITS" -eq 1 ]; then
COMMITS_TEXT="$TOTAL_COMMITS new commit"
else
COMMITS_TEXT="$TOTAL_COMMITS new commits"
fi

# Подготовка заголовка
HEADER="<b>[${repository_name}:${ref_name}]</b> <b><a href=\"https://github.com/${repository}/compare/${COMPARE_HASH}\">$COMMITS_TEXT</a></b> by <b><a href=\"https://github.com/${actor}\">${actor}</a></b>"

# Создаем временную директорию для сообщений
mkdir -p ./tmp_messages || { echo "Error: Failed to create tmp_messages directory" >&2; return 1; }

# Разбиваем сообщение на части по количеству символов
PART_INDEX=0
CHUNK="$HEADER\n\n"
CURRENT_LENGTH=${#CHUNK}

# Максимальная длина для одного коммита (с учетом заголовка и отступов)
local MAX_COMMIT_LENGTH=$((MAX_LENGTH - ${#HEADER} - 2))

for COMMIT in "${readarray[@]}"; do
# Добавляем перевод строки к коммиту
COMMIT_WITH_NEWLINE="$COMMIT\n"
COMMIT_LENGTH=${#COMMIT_WITH_NEWLINE}

# Проверяем длину коммита
if [ $COMMIT_LENGTH -gt $MAX_COMMIT_LENGTH ]; then
echo "Warning: Commit message is too long and will be truncated" >&2
# Обрезаем коммит с учетом места под эллипсис
COMMIT_WITH_NEWLINE="${COMMIT_WITH_NEWLINE:0:$((MAX_COMMIT_LENGTH - ELLIPSIS_LENGTH))} ...\n"
COMMIT_LENGTH=${#COMMIT_WITH_NEWLINE}
fi

# Проверяем, поместится ли следующий коммит
if ((CURRENT_LENGTH + COMMIT_LENGTH > MAX_LENGTH)); then
# Сохраняем текущий чанк
printf "%b" "$CHUNK" > "./tmp_messages/part_${PART_INDEX}.txt" || { echo "Error: Failed to write to file" >&2; return 1; }
PART_INDEX=$((PART_INDEX + 1))

# Начинаем новый чанк с заголовка
CHUNK="$HEADER\n\n$COMMIT_WITH_NEWLINE"
CURRENT_LENGTH=$((${#HEADER} + 2 + COMMIT_LENGTH))
else
# Добавляем коммит к текущему чанку
CHUNK+="$COMMIT_WITH_NEWLINE"
CURRENT_LENGTH=$((CURRENT_LENGTH + COMMIT_LENGTH))
fi
done

# Сохраняем последний чанк, если он не пустой
if [ "$CHUNK" != "$HEADER\n\n" ]; then
printf "%b" "$CHUNK" > "./tmp_messages/part_${PART_INDEX}.txt" || { echo "Error: Failed to write to file" >&2; return 1; }
PART_INDEX=$((PART_INDEX + 1))
fi

echo $PART_INDEX
}
38 changes: 38 additions & 0 deletions .github/scripts/sh/send_telegram.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# send_telegram.sh

send_telegram_messages() {
# Очищаем входные параметры от кавычек
local token=$(trim_quotes "$1")
local chat_id=$(trim_quotes "$2")
local total_parts=$(trim_quotes "$3")

if [ -z "$token" ] || [ -z "$chat_id" ] || [ -z "$total_parts" ]; then
echo "Error: Missing required parameters" >&2
echo "Usage: send_telegram_messages token chat_id total_parts" >&2
return 1
fi

for i in $(seq 0 $((total_parts - 1))); do
if [ ! -f "./tmp_messages/part_${i}.txt" ]; then
echo "Error: Message file part_${i}.txt not found" >&2
continue
fi

MESSAGE=$(cat "./tmp_messages/part_${i}.txt")
echo "Sending part $i to Telegram"

RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot${token}/sendMessage" \
-d "chat_id=${chat_id}" \
-d "parse_mode=HTML" \
-d "text=${MESSAGE}" \
-d "disable_web_page_preview=true")

if ! echo "$RESPONSE" | grep -q '"ok":true'; then
echo "Error sending message part $i: $RESPONSE" >&2
fi

# Добавляем небольшую задержку между отправками сообщений
sleep 1
done
}
54 changes: 54 additions & 0 deletions .github/scripts/sh/telegram_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Функция для отображения справки
function show_help() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Опции:"
echo " -p, --prepare_commits Подготовить коммиты для мержа."
echo " -s, --send_telegram Отправить сообщения в Telegram."
echo " -h, --help Показать эту справку."
echo
exit 0
}

# Переменные для флагов
prepare=false
send=false

# Обрабатываем аргументы командной строки
for arg in "$@"; do
case $arg in
-h|--help)
show_help
;;
-p|--prepare_commits)
prepare=true
;;
-s|--send_telegram)
send=true
;;
*)
echo "Неизвестный аргумент: $arg"
show_help
;;
esac
done

if $prepare; then
source ./.github/scripts/sh/utils.sh
source ./.github/scripts/sh/prepare_commits.sh
prepare_commits "repo-name" "main" "13adf7b" "2df69d6" "username" "org/repo"
fi

if $send; then
if [ -z "$TELEGRAM_TOKEN" ] && [ -f .env ]; then
export TELEGRAM_TOKEN=$(grep '^TELEGRAM_TOKEN=' .env | awk -F'=' '{print substr($0, index($0,$2))}' | sed 's/^"//; s/"$//')
fi
if [ -z "$TELEGRAM_TO" ] && [ -f .env ]; then
export TELEGRAM_TO=$(grep '^TELEGRAM_TO=' .env | awk -F'=' '{print substr($0, index($0,$2))}' | sed 's/^"//; s/"$//')
fi
source ./.github/scripts/sh/utils.sh
source ./.github/scripts/sh/send_telegram.sh
send_telegram_messages $TELEGRAM_TOKEN $TELEGRAM_TO 2
fi
14 changes: 14 additions & 0 deletions .github/scripts/sh/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
# utils.sh

# Функция для удаления кавычек из строки
trim_quotes() {
local string="$1"
# Удаляем двойные кавычки с начала и конца строки
string="${string#\"}"
string="${string%\"}"
# Удаляем одинарные кавычки с начала и конца строки
string="${string#\'}"
string="${string%\'}"
echo "$string"
}
91 changes: 34 additions & 57 deletions .github/workflows/github-telegram.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
# This workflow contains a job called "build" adn "log-github-event-goodies"
push:

if: ${{ github.event.commits != null }}
if: ${{ github.event.commits != null && inputs.additional-text == null }}

# The type of runner that the job will run on
runs-on: ubuntu-latest
Expand All @@ -41,65 +41,34 @@ jobs:

- name: Get commits
id: get_commits
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# checkout full tree
fetch-depth: 0

- name: Work with commits
id: with_commits
- name: Prepare commit messages
id: prepare
run: |
if [ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]; then
git fetch origin HEAD
git checkout HEAD
mapfile -t COMMITS_ARRAY < <(git log --pretty=format:"'<code>%h</code>' - %an, %ar : %s" HEAD..${{ github.sha }})
echo "before=${{ github.sha }}" >> $GITHUB_OUTPUT
else
mapfile -t COMMITS_ARRAY < <(git log --pretty=format:"'<code>%h</code>' - %an, %ar : %s" ${{ github.event.before }}..${{ github.sha }})
echo "before=${{ github.event.before }}..${{ github.sha }}" >> $GITHUB_OUTPUT
fi

# Process each commit
echo "full_list<<EOF" >> $GITHUB_OUTPUT
for COMMIT in "${COMMITS_ARRAY[@]}"; do
echo "$COMMIT" >> $GITHUB_OUTPUT
done
echo "EOF" >> $GITHUB_OUTPUT

LINES_COUNT=${#COMMITS_ARRAY[*]}
if [ "$LINES_COUNT" == "1" ]; then
echo "lines_count=$LINES_COUNT new commit" >> $GITHUB_OUTPUT
else
echo "lines_count=$LINES_COUNT new commits" >> $GITHUB_OUTPUT
fi


- name: Set outputs
id: set_outputs
# The part of this solution to focus on is that we’re substituting the %, \n, and \r characters
source ./.github/scripts/sh/utils.sh
source ./.github/scripts/sh/prepare_commits.sh
total_parts=$(prepare_commits \
"${{ github.event.repository.name }}" \
"${{ github.ref_name }}" \
"${{ github.event.before }}" \
"${{ github.sha }}" \
"${{ github.actor }}" \
"${{ github.repository }}")
echo "total_parts=${total_parts}" >> $GITHUB_OUTPUT

- name: Send messages to Telegram
if: ${{ steps.prepare.outputs.total_parts != '0' }}
run: |
additional_text="${{ inputs.additional-text || '' }}"
if [ "$additional_text" != "" ]; then
echo "additional_text<<EOF" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo $additional_text >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi

- name: Send message to Telegram
if: ${{ steps.with_commits.outputs.full_list != '' }}
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
disable_web_page_preview: true
format: html
message: |
<b>[${{ github.event.repository.name }}:${{ github.ref_name }}]</b> <b><a href="https://github.com/${{ github.repository }}/compare/${{ steps.with_commits.outputs.before }}">${{ steps.with_commits.outputs.lines_count }}</a></b> by <b><a href="https://github.com/${{ github.actor }}">${{ github.actor }}</a></b>
${{ steps.set_outputs.outputs.additional_text }}
${{ steps.with_commits.outputs.full_list }}

source ./.github/scripts/sh/utils.sh
source ./.github/scripts/sh/send_telegram.sh
send_telegram_messages \
"${{ secrets.TELEGRAM_TOKEN }}" \
"${{ secrets.TELEGRAM_TO }}" \
"${{ steps.prepare.outputs.total_parts }}"

create:

Expand Down Expand Up @@ -173,7 +142,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Update repository
run: git pull
Expand All @@ -192,11 +161,19 @@ jobs:
id: set_outputs
# The part of this solution to focus on is that we’re substituting the %, \n, and \r characters
run: |
additional_text="${{ inputs.additional-text || '' }}"
additional_text=$(echo "${{ inputs.additional-text || '' }}" | base64 -d)
if [ "$additional_text" != "" ]; then
# Форматируем текст с переносами строк
additional_text=$(echo -e "$additional_text")

# Debug: print the formatted text
echo "Formatted additional_text:"
echo "$additional_text"

echo "additional_text<<EOF" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo $additional_text >> $GITHUB_OUTPUT
echo "<pre>$additional_text" >> $GITHUB_OUTPUT
echo "</pre>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
Expand Down
Loading
Loading