From 14e86d7a30cbe5297e034203acbee0a5c2ba54ef Mon Sep 17 00:00:00 2001 From: Denys Konovalov Date: Sat, 21 Oct 2023 13:12:47 +0200 Subject: [PATCH] simplify action --- LICENSE | 1 + README.md | 13 ++++-------- action.yaml | 28 ++++--------------------- constant.py | 6 ------ environments.py | 43 --------------------------------------- main.py | 54 ++++++++++++++++++++++++------------------------- message.py | 27 ------------------------- 7 files changed, 35 insertions(+), 137 deletions(-) delete mode 100644 constant.py delete mode 100644 environments.py delete mode 100644 message.py diff --git a/LICENSE b/LICENSE index ac7aaf7..01e8f33 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ MIT License +Copyright (c) 2023 Denys Konovalov Copyright (c) 2022 yamaks2306 Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/README.md b/README.md index d1dbd1d..b414e38 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,15 @@ # telegram-notification -Notification to Telegram chat about GitHub Action workflow status with customizable messages +Notification to Telegram chat about Gitea Action workflow status ## Usage To be notified in the Telegram chat about the results of a wokflow, add the next step to the end of your wokflow: + ```yaml - name: Send message - uses: yamaks2306/telegram-notification@main + uses: action/telegram-notification@main if: always() with: chat_id: ${{ secrets.TG_CHAT_ID }} token: ${{ secrets.TG_TOKEN }} ``` -Where ```chat_id``` is the chat ID and ```token``` is the token of the telegram bot - -You can specify additional parameters to customize the messages: -- ```include_commit_info``` - string "true" or "false" ("true" by default). If true, message to Telegram will contain information about commit - author, commit message and link to commit page. -- ```commit_message``` - the default setting is ```github.event.head_commit.message```. ```github.event.commits[0].message``` can be used instead. In the first case, if there were several commits, the message of the last commit will be displayed, in the second - the first one. -- ```docker_tags``` - if the previous step was to build docker images, you can specify docker tags, for example ```steps.docker_meta.outputs.tags``` -- ```message``` - custom message, if specified, will be used instead of the standard message. \ No newline at end of file +Where ```chat_id``` is the chat ID and ```token``` is the token of the telegram bot \ No newline at end of file diff --git a/action.yaml b/action.yaml index 4669d7a..2d406f0 100644 --- a/action.yaml +++ b/action.yaml @@ -1,6 +1,6 @@ -name: 'GitHub workflow Telegram notification' -description: 'Get notification to Telegram chat about GitHub Action workflow status ' -author: 'yamaks2306' +name: 'Gitea Actions Telegram notification' +description: 'Get notification to Telegram chat about Gitea Action workflow status ' +author: 'gcg' inputs: chat_id: @@ -13,20 +13,6 @@ inputs: description: 'Job status' required: false default: ${{ job.status }} - commit_message: - description: 'Commit message' - required: false - default: ${{ github.event.head_commit.message }} - docker_tags: - description: 'For example, steps.docker_meta.outputs.tags' - required: false - include_commit_info: - description: "If true, include commit information to message. Default - true" - required: false - default: 'true' - message: - description: "Custom message" - required: false runs: using: 'composite' @@ -38,15 +24,9 @@ runs: CHAT_ID: ${{ inputs.chat_id }} TOKEN: ${{ inputs.token }} STATUS: ${{ inputs.status }} - COMMIT_MESSAGE: ${{ inputs.commit_message }} - DOCKER_TAGS: ${{ inputs.docker_tags }} - INCLUDE_COMMIT_INFO: ${{ inputs.include_commit_info }} - MESSAGE: ${{ inputs.message }} WORKFLOW: ${{ github.workflow }} REPOSITORY: ${{ github.repository }} - SHA: ${{ github.sha }} - ACTOR: ${{ github.actor }} - TAG: ${{ github.ref }} + RUN_NUMBER: ${{ github.run_number }} branding: icon: 'send' diff --git a/constant.py b/constant.py deleted file mode 100644 index a2b5c0c..0000000 --- a/constant.py +++ /dev/null @@ -1,6 +0,0 @@ -ICON = { - "failure": "❌", - "cancelled": "⚪", - "success": "✅", -} - diff --git a/environments.py b/environments.py deleted file mode 100644 index d1e9e30..0000000 --- a/environments.py +++ /dev/null @@ -1,43 +0,0 @@ -class Environment: - '''Class containing all the necessary variables for building and sending notification''' - def __init__( - self, - chat_id, - token, - status, - commit, - include_commit_info, - docker_tags, - custom_message, - github_workflow, - github_repository, - github_sha, - github_actor, - github_run_id, - tag - ): - self.chat_id = chat_id - self.token = token - self.status = status - self.commit = self.__fix_commit_message(commit) - self.include_commit_info = include_commit_info - self.docker_tags = docker_tags - self.custom_message = custom_message - self.github_workflow = github_workflow - self.github_repository = github_repository - self.github_sha = github_sha - self.github_actor = github_actor - self.github_run_id = github_run_id - self.tag = tag - - def get_link(self): - '''Get link for sending message''' - return f'https://api.telegram.org/bot{self.token}/sendMessage' - def get_commit_link(self): - '''Get link to commit''' - return f'https://git.cantorgymnasium.de/{self.github_repository}/commit/{self.github_sha}' - def __fix_commit_message(self, message): - '''Replacing unsupported characters''' - replace_dict = {'<':'(', '>':')', '\n':'\n '} - result = ''.join(i if i not in replace_dict else replace_dict[i] for i in message) - return result \ No newline at end of file diff --git a/main.py b/main.py index fb004ac..934aa34 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import os -from message import Message -from environments import Environment +import requests +# Telegram configuration chat_id = os.getenv('CHAT_ID') if chat_id == "" or chat_id is None: raise SystemExit('Variable chat_id is required. Exit.') @@ -9,33 +9,31 @@ token = os.getenv('TOKEN') if token == "" or token is None: raise SystemExit('Variable token is required. Exit.') status = os.getenv('STATUS') -commit = os.getenv('COMMIT_MESSAGE') -include_commit_info = os.getenv('INCLUDE_COMMIT_INFO') -docker_tags = os.getenv('DOCKER_TAGS') -custom_message = os.getenv('MESSAGE') -#GitHub environment variables + +# Gitea environment variables github_workflow = os.getenv('WORKFLOW') github_repository = os.getenv('REPOSITORY') -github_sha = os.getenv('SHA') -github_actor = os.getenv('ACTOR') -github_run_id = os.getenv('GITHUB_RUN_NUMBER') -tag = os.getenv('TAG') +github_run_number = os.getenv('RUN_NUMBER') -envs = Environment( - chat_id, - token, - status, - commit, - include_commit_info, - docker_tags, - custom_message, - github_workflow, - github_repository, - github_sha, - github_actor, - github_run_id, - tag -) -message = Message(envs) +ICON = { + "failure": "❌", + "cancelled": "⚪", + "success": "✅", +} -message.send_message() +def get_link(): + return f'https://api.telegram.org/bot{token}/sendMessage' + +def send_message(): + message = f'''[{github_repository}] {ICON[status]} {github_workflow}''' + parameters = { + 'chat_id': chat_id, + 'text': message, + 'parse_mode': 'HTML', + 'disable_web_page_preview': True + } + + request = requests.get(get_link(), params = parameters) + request.raise_for_status() + +send_message() diff --git a/message.py b/message.py deleted file mode 100644 index 099b369..0000000 --- a/message.py +++ /dev/null @@ -1,27 +0,0 @@ -import requests -import constant -import environments - -class Message: - '''A class that builds and sends messages''' - def __init__(self, envs: environments.Environment): - self.envs = envs - - def __build_message(self): - '''Building message from different parts''' - message = f'''[{self.envs.github_repository}] {constant.ICON[self.envs.status]} {self.envs.github_workflow}''' - - return message - - def send_message(self): - '''Sending message to telegram chat''' - message = self.__build_message() if self.envs.custom_message == "" else self.envs.custom_message - parameters = { - 'chat_id': self.envs.chat_id, - 'text': message, - 'parse_mode': 'HTML', - 'disable_web_page_preview': True - } - - request = requests.get(self.envs.get_link(), params = parameters) - request.raise_for_status()