From cfba8ee7d456bae1bce057779b5158bb1bb2db94 Mon Sep 17 00:00:00 2001 From: name Date: Sat, 5 Feb 2022 13:26:03 +0800 Subject: [PATCH] add nofify --- .github/workflows/destversion.yml | 4 +- .github/workflows/notify.yml | 18 +++++++ scripts/destVersionRelease.sh | 18 ++++++- scripts/notify.sh | 82 +++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/notify.yml create mode 100755 scripts/notify.sh diff --git a/.github/workflows/destversion.yml b/.github/workflows/destversion.yml index 8d3122e..c528175 100644 --- a/.github/workflows/destversion.yml +++ b/.github/workflows/destversion.yml @@ -15,8 +15,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Test Github Action Server Time - run: echo `date` + # - name: Test Github Action Server Time + # run: echo `date` - name: Check new version and push env: GHTOKEN: ${{ secrets.GHTOKEN }} diff --git a/.github/workflows/notify.yml b/.github/workflows/notify.yml new file mode 100644 index 0000000..663bc79 --- /dev/null +++ b/.github/workflows/notify.yml @@ -0,0 +1,18 @@ +name: Wechat Release Notify + +on: + release: + types: [published] + + +jobs: + notify_to_tg: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Check new version and push + env: + GHTOKEN: ${{ secrets.GHTOKEN }} + BOTTOKEN: ${{ secrets.BOTTOKEN }} + CHATIDS: ${{ secrets.CHATIDS }} + run: bash -x ./scripts/notify.sh diff --git a/scripts/destVersionRelease.sh b/scripts/destVersionRelease.sh index e5d8faf..bc4bede 100755 --- a/scripts/destVersionRelease.sh +++ b/scripts/destVersionRelease.sh @@ -17,10 +17,21 @@ function install_depends() { echo -e "## \033[1;33mInstalling 7zip, shasum, wget, curl, git\033[0m" printf "#%.0s" {1..60} echo + apt install -y p7zip-full p7zip-rar libdigest-sha-perl wget curl git } function login_gh() { + printf "#%.0s" {1..60} + echo + echo -e "## \033[1;33mLogin to github to use github-cli...\033[0m" + printf "#%.0s" {1..60} + echo + if [ -z $GHTOKEN ]; then + >&2 echo -e "\033[1;31mMissing Github Token! Please get a BotToken from 'Github Settings->Developer settings->Personal access tokens' and set it in Repo Secrect\033[0m" + exit 1 + fi + echo $GHTOKEN > WeChatSetup/temp/GHTOKEN gh auth login --with-token < WeChatSetup/temp/GHTOKEN if [ "$?" -ne 0 ]; then @@ -36,6 +47,7 @@ function download_wechat() { echo -e "## \033[1;33mDownloading the newest WechatSetup...\033[0m" printf "#%.0s" {1..60} echo + wget "$download_link" -O ${temp_path}/WeChatSetup.exe if [ "$?" -ne 0 ]; then >&2 echo -e "\033[1;31mDownload Failed, please check your network!\033[0m" @@ -64,6 +76,7 @@ function prepare_commit() { echo -e "## \033[1;33mPrepare to commit new version\033[0m" printf "#%.0s" {1..60} echo + mkdir -p WeChatSetup/$dest_version cp $temp_path/WeChatSetup.exe WeChatSetup/$dest_version/WeChatSetup-$dest_version.exe echo "DestVersion: $dest_version" > WeChatSetup/$dest_version/WeChatSetup-$dest_version.exe.sha256 @@ -79,6 +92,7 @@ function clean_data() { echo -e "## \033[1;33mClean runtime and exit...\033[0m" printf "#%.0s" {1..60} echo + rm -rfv WeChatSetup/* exit $1 } @@ -103,7 +117,9 @@ function main() { prepare_commit gh release create v$dest_version ./WeChatSetup/$dest_version/WeChatSetup-$dest_version.exe -F ./WeChatSetup/$dest_version/WeChatSetup-$dest_version.exe.sha256 -t "Wechat v$dest_version" - # gh auth logout + + gh auth logout --hostname github.com | echo "y" + clean_data 0 } diff --git a/scripts/notify.sh b/scripts/notify.sh new file mode 100755 index 0000000..a740449 --- /dev/null +++ b/scripts/notify.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +set -eo pipefail + + +if [ -z $GHTOKEN ]; then + >&2 echo -e "\033[1;31mMissing Github Token! Please get a BotToken from 'Github Settings->Developer settings->Personal access tokens' and set it in Repo Secrect\033[0m" + exit 1 +fi + +if [ -z $BOTTOKEN ]; then + >&2 echo -e "\033[1;31mMissing Bot Token! Please get a BotToken from @Botfather on Telegram and set it in Repo Secrect\033[0m" + exit 2 +fi + +if [ -z $CHATIDS ]; then + >&2 echo -e "\033[1;31mMissing ChatIds! Please get ChatId from @GroupIDbot on Telegram Chats(Muti chatids split with comma ',') and set it in Repo Environment Values\033[0m" + exit 2 +fi +chat_ids=`echo $CHATIDS | sed 's/,/ /g'` + +function login_gh() { + printf "#%.0s" {1..60} + echo + echo -e "## \033[1;33mLogin to github to use github-cli...\033[0m" + printf "#%.0s" {1..60} + echo + + echo $GHTOKEN > WeChatSetup/temp/GHTOKEN + gh auth login --with-token < WeChatSetup/temp/GHTOKEN + if [ "$?" -ne 0 ]; then + >&2 echo -e "\033[1;31mLogin Failed, please check your network or token!\033[0m" + clean_data 1 + fi + rm -rfv WeChatSetup/temp/GHTOKEN +} + +### https://kodango.com/sed-and-awk-notes-part-5 +## start=${1:-""} means as follows in general +## if ($1) then +## start=$1 +## else +## start="" +## end +function join_lines() { + local delim=${1:-,} + sed 'H;$!d;${x;s/^\n//;s/\n/\'$delim'/g}' +} + +function clean_data() { + printf "#%.0s" {1..60} + echo + echo -e "## \033[1;33mClean runtime and exit...\033[0m" + printf "#%.0s" {1..60} + echo + + rm -rfv WeChatSetup/* + exit $1 +} + +function main() { + login_gh + + temp_path="WeChatSetup/temp" + mkdir -p ${temp_path} + gh release view --json body --jq ".body" > ${temp_path}/release.info + + release_info=`awk '!/^$|Sha256/ { $1="*"$1"*";sub("UpdateTime", "CheckTime"); if ( match($2, /https?:\/\/([\w\.\/:])*/) ) $2="[Url]("$2")"; print $0 }' ${temp_path}/release.info | join_lines '%0A' | sed 's/ /%20/g'` + dest_version=`awk '/DestVersion/ { print $2 }' ${temp_path}/release.info` + release_info="$release_info%0A%0A*NotifyFrom:*%20[Github](https://github.com/tom-snow/wechat-windows-versions/releases/tag/v$dest_version)" + + for chatid in $chat_ids + do + api_link="https://api.telegram.org/bot$BOTTOKEN/sendMessage?chat_id=$chatid&text=*New%20WeChat%20Windows%20Version!!*%0A%0A$release_info&parse_mode=Markdown&disable_web_page_preview=true" + curl -s -o /dev/null $api_link + done + + gh auth logout --hostname github.com | echo "y" + clean_data 0 +} + +main \ No newline at end of file