diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34b9ba1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +.testenvs \ No newline at end of file diff --git a/constant.py b/constant.py new file mode 100644 index 0000000..9ca6286 --- /dev/null +++ b/constant.py @@ -0,0 +1,6 @@ +ICON = { + "failure": "🔴", + "cancelled": "⚪", + "success": "🟢", +} + diff --git a/environments.py b/environments.py new file mode 100644 index 0000000..127af7f --- /dev/null +++ b/environments.py @@ -0,0 +1,46 @@ +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, + 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.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://github.com/{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 + def get_version(self): + '''GITHUB_REF contain "refs/tags/v0.0.2" or "refs/heads/main". If second part is "tags", + return tag with version number, else - None''' + tags = self.tag.split("/") + return tags[2] if tags[1] == "tags" else None diff --git a/main.py b/main.py new file mode 100644 index 0000000..93621c1 --- /dev/null +++ b/main.py @@ -0,0 +1,39 @@ +import os +from message import Message +from environments import Environment + +chat_id = os.getenv('INPUT_CHAT_ID') +if chat_id == "": + raise SystemExit('Variable chat_id is required. Exit.') +token = os.getenv('INPUT_TOKEN') +if token == "": + raise SystemExit('Variable chat_id is required. Exit.') +status = os.getenv('INPUT_STATUS') +commit = os.getenv('INPUT_COMMIT_MESSAGE') +include_commit_info = os.getenv('INPUT_INCLUDE_COMMIT_INFO') +docker_tags = os.getenv('INPUT_DOCKER_TAGS') +custom_message = os.getenv('INPUT_MESSAGE') +#GitHub environment variables +github_workflow = os.getenv('GITHUB_WORKFLOW') +github_repository = os.getenv('GITHUB_REPOSITORY') +github_sha = os.getenv('GITHUB_SHA') +github_actor = os.getenv('GITHUB_ACTOR') +tag = os.getenv('GITHUB_REF') + +envs = Environment( + chat_id, + token, + status, + commit, + include_commit_info, + docker_tags, + custom_message, + github_workflow, + github_repository, + github_sha, + github_actor, + tag +) +message = Message(envs) + +message.send_message() diff --git a/message.py b/message.py new file mode 100644 index 0000000..0f7fd02 --- /dev/null +++ b/message.py @@ -0,0 +1,43 @@ +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'''{constant.ICON[self.envs.status]} {self.envs.status}: {self.envs.github_workflow}\n + Repository: {self.envs.github_repository}''' + + version = self.envs.get_version() + if version is not None: + message = f'''{message} + Version: {version}''' + + if self.envs.docker_tags is not None: + message = f'''{message} + Docker image tags: {self.envs.docker_tags}''' + + if self.envs.include_commit_info == "true": + message = f'''{message} + Author: {self.envs.github_actor} + Commit message: {self.envs.commit} + See changes''' + + return message + + def send_message(self): + '''Sending message to telegram chat''' + message = self.__build_message() if self.envs.custom_message is None 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() diff --git a/send_message.py b/send_message.py deleted file mode 100644 index a639d88..0000000 --- a/send_message.py +++ /dev/null @@ -1,75 +0,0 @@ -import os -import requests - -chat_id = os.getenv('INPUT_CHAT_ID') -if chat_id == "": - raise SystemExit('Variable chat_id is required. Exit.') -token = os.getenv('INPUT_TOKEN') -if token == "": - raise SystemExit('Variable chat_id is required. Exit.') -status = os.getenv('INPUT_STATUS') -commit = os.getenv('INPUT_COMMIT_MESSAGE') -include_commit_info = os.getenv('INPUT_INCLUDE_COMMIT_INFO') -docker_tags = os.getenv('INPUT_DOCKER_TAGS') -custom_message = os.getenv('INPUT_MESSAGE') - - -link=f'https://api.telegram.org/bot{token}/sendMessage' - -icon = { - "failure": "🔴", - "cancelled": "⚪", - "success": "🟢", -} - -#GitHub environment variables -github_workflow = os.getenv('GITHUB_WORKFLOW') -github_repository = os.getenv('GITHUB_REPOSITORY') -github_sha = os.getenv('GITHUB_SHA') -github_actor = os.getenv('GITHUB_ACTOR') -tag = os.getenv('GITHUB_REF') - -commit_link = f'https://github.com/{github_repository}/commit/{github_sha}' - -def get_version(tag_string): - '''GITHUB_REF contain "refs/tags/v0.0.2" or "refs/heads/main". If second part is "tags", - return tag with version number, else - None''' - tags = tag_string.split("/") - return tags[2] if tags[1] == "tags" else None - -def build_message(): - '''Building message from different parts''' - message = f'''{icon[status]} {status}: {github_workflow}\n - Repository: {github_repository}''' - - version = get_version(tag) - if version is not None: - message = f'''{message} - Version: {version}''' - - if docker_tags != "": - message = f'''{message} - Docker image tags: {docker_tags}''' - - if include_commit_info == "true": - message = f'''{message} - Author: {github_actor} - Commit message: {commit} - See changes''' - - return message - -def send_message(): - '''Sending message to telegram chat''' - message = build_message() if custom_message == "" else custom_message - parameters = { - 'chat_id': chat_id, - 'text': message, - 'parse_mode': 'HTML', - 'disable_web_page_preview': True - } - - request = requests.get(link, params = parameters) - request.raise_for_status() - -send_message()