1
0

simplify action

This commit is contained in:
Denys Konovalov 2023-10-21 13:12:47 +02:00
parent 27c2d8ab13
commit 14e86d7a30
7 changed files with 35 additions and 137 deletions

@ -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

@ -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.

@ -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'

@ -1,6 +0,0 @@
ICON = {
"failure": "",
"cancelled": "",
"success": "",
}

@ -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

54
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'''[<a href="https://git.cantorgymnasium.de/{github_repository}/actions/runs/{github_run_number}">{github_repository}</a>] {ICON[status]} <b>{github_workflow}</b>'''
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()

@ -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'''[<a href="https://git.cantorgymnasium.de/{self.envs.github_repository}/actions/runs/{self.envs.github_run_id}">{self.envs.github_repository}</a>] {constant.ICON[self.envs.status]} <b>{self.envs.github_workflow}</b>'''
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()