From e261a242b965a01cdc60ee799f091b7306921c49 Mon Sep 17 00:00:00 2001 From: rsuntk Date: Sat, 2 Nov 2024 19:47:04 +0700 Subject: [PATCH] workflows: Add multiple changes - Link signing environment to CI - Upload manager when build was finished (https://t.me/rsukrnlprjkt) - Fix permissions Signed-off-by: rsuntk --- .github/workflows/build-manager.yml | 5 +- .github/workflows/release.yml | 2 + scripts/ksubot.py | 101 ---------------------------- scripts/uploadtg_handler.sh | 15 +++++ 4 files changed, 19 insertions(+), 104 deletions(-) delete mode 100644 scripts/ksubot.py create mode 100644 scripts/uploadtg_handler.sh diff --git a/.github/workflows/build-manager.yml b/.github/workflows/build-manager.yml index 89360617be92..d15420ada7cf 100644 --- a/.github/workflows/build-manager.yml +++ b/.github/workflows/build-manager.yml @@ -45,6 +45,7 @@ jobs: build-manager: needs: build-ksud + environment: signing runs-on: ubuntu-latest defaults: run: @@ -147,7 +148,6 @@ jobs: env: CHAT_ID: ${{ secrets.CHAT_ID }} BOT_TOKEN: ${{ secrets.BOT_TOKEN }} - MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }} COMMIT_MESSAGE: ${{ github.event.head_commit.message }} COMMIT_URL: ${{ github.event.head_commit.url }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} @@ -156,6 +156,5 @@ jobs: if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then export VERSION=$(git rev-list --count HEAD) APK=$(find ./app/build/outputs/apk/release -name "*.apk") - pip3 install telethon - python3 $GITHUB_WORKSPACE/scripts/ksubot.py $APK + bash $GITHUB_WORKSPACE/scripts/uploadtg_handler.sh $APK fi diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dcc0fca8856c..81d65d93406e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -36,6 +36,8 @@ jobs: - build-wsa-kernel - build-arcvm-kernel runs-on: ubuntu-latest + permissions: + contents: write steps: - name: Download artifacts uses: actions/download-artifact@v4 diff --git a/scripts/ksubot.py b/scripts/ksubot.py deleted file mode 100644 index c3b3672772ed..000000000000 --- a/scripts/ksubot.py +++ /dev/null @@ -1,101 +0,0 @@ -import asyncio -import os -import sys -from telethon import TelegramClient -from telethon.tl.functions.help import GetConfigRequest - -API_ID = 611335 -API_HASH = "d524b414d21f4d37f08684c1df41ac9c" - - -BOT_TOKEN = os.environ.get("BOT_TOKEN") -CHAT_ID = os.environ.get("CHAT_ID") -MESSAGE_THREAD_ID = os.environ.get("MESSAGE_THREAD_ID") -COMMIT_URL = os.environ.get("COMMIT_URL") -COMMIT_MESSAGE = os.environ.get("COMMIT_MESSAGE") -RUN_URL = os.environ.get("RUN_URL") -TITLE = os.environ.get("TITLE") -VERSION = os.environ.get("VERSION") -MSG_TEMPLATE = """ -**{title}** -#ci_{version} -``` -{commit_message} -``` -[Commit]({commit_url}) -[Workflow run]({run_url}) -""".strip() - - -def get_caption(): - msg = MSG_TEMPLATE.format( - title=TITLE, - version=VERSION, - commit_message=COMMIT_MESSAGE, - commit_url=COMMIT_URL, - run_url=RUN_URL, - ) - if len(msg) > 1024: - return COMMIT_URL - return msg - - -def check_environ(): - global CHAT_ID, MESSAGE_THREAD_ID - if BOT_TOKEN is None: - print("[-] Invalid BOT_TOKEN") - exit(1) - if CHAT_ID is None: - print("[-] Invalid CHAT_ID") - exit(1) - else: - CHAT_ID = int(CHAT_ID) - if COMMIT_URL is None: - print("[-] Invalid COMMIT_URL") - exit(1) - if COMMIT_MESSAGE is None: - print("[-] Invalid COMMIT_MESSAGE") - exit(1) - if RUN_URL is None: - print("[-] Invalid RUN_URL") - exit(1) - if TITLE is None: - print("[-] Invalid TITLE") - exit(1) - if VERSION is None: - print("[-] Invalid VERSION") - exit(1) - if MESSAGE_THREAD_ID is None: - print("[-] Invaild MESSAGE_THREAD_ID") - exit(1) - else: - MESSAGE_THREAD_ID = int(MESSAGE_THREAD_ID) - - -async def main(): - print("[+] Uploading to telegram") - check_environ() - files = sys.argv[1:] - print("[+] Files:", files) - if len(files) <= 0: - print("[-] No files to upload") - exit(1) - print("[+] Logging in Telegram with bot") - script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) - session_dir = os.path.join(script_dir, "ksubot") - async with await TelegramClient(session=session_dir, api_id=API_ID, api_hash=API_HASH).start(bot_token=BOT_TOKEN) as bot: - caption = [""] * len(files) - caption[-1] = get_caption() - print("[+] Caption: ") - print("---") - print(caption) - print("---") - print("[+] Sending") - await bot.send_file(entity=CHAT_ID, file=files, caption=caption, reply_to=MESSAGE_THREAD_ID, parse_mode="markdown") - print("[+] Done!") - -if __name__ == "__main__": - try: - asyncio.run(main()) - except Exception as e: - print(f"[-] An error occurred: {e}") diff --git a/scripts/uploadtg_handler.sh b/scripts/uploadtg_handler.sh new file mode 100644 index 000000000000..28af3276b2d9 --- /dev/null +++ b/scripts/uploadtg_handler.sh @@ -0,0 +1,15 @@ +#!/bin/env bash +msg="*$TITLE* +\\#ci\\_$VERSION +\`\`\` +$COMMIT_MESSAGE +\`\`\` +[Commit]($COMMIT_URL) +[Workflow run]($RUN_URL) +" +file="$1" +curl -s -F document=@$file "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" \ + -F chat_id="$CHAT_ID" \ + -F "disable_web_page_preview=true" \ + -F "parse_mode=markdownv2" \ + -F caption="$msg" \ No newline at end of file