shared-hosting/check/alert.py

82 lines
2.4 KiB
Python

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from enum import Enum, unique
from .entity import User, Repo, Org
@unique
class MessageCode(Enum):
"""
Message code associated with alerts. All alert messages should contain
a message code
"""
USER_NO_PURCHASE_FORK_HISTORY = "The fork history doesn't contain a paying customer"
USER_NO_PURCHASE_NON_FORK_REPO = "Gratis customer has a non-fork repository"
ORG_MAJORITY_NO_PURCHASE = (
"Majority of the members of an organisation are non-paying customers"
)
def __str__(self) -> str:
return self.name
class Message:
"""
An alert message
"""
def __init__(
self,
code: MessageCode,
customer: User = None,
repo: Repo = None,
forks: [Repo] = None,
org: Org = None,
):
self.code = code
self.repo = repo
self.org = org
self.customer = customer
self.forks = forks
def __str__(self) -> str:
if self.code == MessageCode.USER_NO_PURCHASE_NON_FORK_REPO:
return f"{self.customer} is not a paying customer; non-fork repository: {self.repo}"
if self.code == MessageCode.USER_NO_PURCHASE_FORK_HISTORY:
return f"forks found without a paying customer at origin: {self.forks}"
if self.code == MessageCode.ORG_MAJORITY_NO_PURCHASE:
return f"org with more non-paying members than paying members: {self.org}"
raise Exception(f"Unknown message code {self.code}")
def __repr__(self) -> str:
return f"[{self.code}] {self}"
class Alert:
"""
Send Alert
"""
def alert(self, msg: Message):
"""
Ping admin: user|repository is over gratis quota
"""
print(msg)