Skip to content

Commit

Permalink
logging_sender.py: permit to choose a matrix chan where to send the n…
Browse files Browse the repository at this point in the history
…otification
  • Loading branch information
OniriCorpe committed Mar 15, 2024
1 parent c45563d commit f3f3eec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
37 changes: 30 additions & 7 deletions tools/appslib/logging_sender.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
#!/usr/bin/env python3

import subprocess
from shutil import which
import logging
import logging.handlers


def send_to_matrix(message: str) -> None:
if which("sendxmpppy") is None:
logging.warning("Could not send error via xmpp.")
return
subprocess.call(["sendxmpppy", message], stdout=subprocess.DEVNULL)
def notify(message, channel):
print(f"{channel} -> {message}")

chan_list = ["dev", "apps", "doc"]

try:
any(channel in x for x in chan_list)
except False:
logging.warning(
f"Provided chan '{channel}' is not part of the available options ('dev', 'apps', 'doc')."
)

for char in ["'", "`", "!", ";", "$"]:
message = message.replace(char, "")

try:
subprocess.call(
[
"/var/www/webhooks/matrix-commander",
"--markdown -m '{message}' -c /var/www/webhooks/credentials.json --store /var/www/webhooks/store --room 'yunohost-{channel}'",
],
stdout=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as e:
logging.warning(
f"""Could not send a notification on {channel}.
Message: {message}
Error: {e}"""
)


class LogSenderHandler(logging.Handler):
Expand All @@ -20,7 +43,7 @@ def __init__(self):

def emit(self, record):
msg = f"[Apps tools error] {record.msg}"
send_to_matrix(msg)
notify(msg, "dev")

@classmethod
def add(cls, level=logging.ERROR):
Expand Down
20 changes: 10 additions & 10 deletions tools/autoupdate_app_sources/autoupdate_app_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@


@cache
def get_github() -> tuple[
Optional[tuple[str, str]],
Optional[github.Github],
Optional[github.InputGitAuthor],
]:
def get_github() -> (
tuple[
Optional[tuple[str, str]],
Optional[github.Github],
Optional[github.InputGitAuthor],
]
):
try:
github_login = (
(REPO_APPS_ROOT / ".github_login")
Expand Down Expand Up @@ -286,7 +288,6 @@ def run(
def relevant_versions(
tags: list[str], app_id: str, version_regex: Optional[str]
) -> tuple[str, str]:

def apply_version_regex(tag: str) -> Optional[str]:
# First preprocessing according to the manifest version_regex…
if version_regex:
Expand Down Expand Up @@ -452,8 +453,8 @@ def get_latest_version_and_asset(

api: Union[GithubAPI, GitlabAPI, GiteaForgejoAPI]
if remote_type == "github":
assert upstream and upstream.startswith(
"https://github.com/"
assert (
upstream and upstream.startswith("https://github.com/")
), f"When using strategy {strategy}, having a defined upstream code repo on github.com is required"
api = GithubAPI(upstream, auth=get_github()[0])
if remote_type == "gitlab":
Expand Down Expand Up @@ -594,7 +595,6 @@ def paste_on_haste(data):


class StdoutSwitch:

class DummyFile:
def __init__(self) -> None:
self.result = ""
Expand Down Expand Up @@ -728,7 +728,7 @@ def main() -> None:
paste_url = paste_on_haste(paste_message)
matrix_message += f"\nSee the full log here: {paste_url}"

appslib.logging_sender.send_to_matrix(matrix_message)
appslib.logging_sender.notify(matrix_message, "apps")
print(paste_message)


Expand Down

0 comments on commit f3f3eec

Please sign in to comment.