46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class MattermostApprovalAdapter:
|
||
|
|
def __init__(self, account: dict):
|
||
|
|
self.account = account
|
||
|
|
approval_cfg = account.get("approval", {})
|
||
|
|
self.approval_enabled = approval_cfg.get("enabled", False)
|
||
|
|
self.approvers = approval_cfg.get("approvers", [])
|
||
|
|
|
||
|
|
def is_approval_required(self, action: str, initiator_peer_id: str) -> bool:
|
||
|
|
if not self.approval_enabled:
|
||
|
|
return False
|
||
|
|
if not self.approvers:
|
||
|
|
return False
|
||
|
|
if initiator_peer_id in self.approvers:
|
||
|
|
return False
|
||
|
|
return True
|
||
|
|
|
||
|
|
def get_approver_ids(self) -> list[str]:
|
||
|
|
return self.approvers
|
||
|
|
|
||
|
|
def build_approval_message(
|
||
|
|
self,
|
||
|
|
action: str,
|
||
|
|
initiator_peer_id: str,
|
||
|
|
description: str,
|
||
|
|
context: dict | None = None,
|
||
|
|
) -> dict:
|
||
|
|
return {
|
||
|
|
"action": action,
|
||
|
|
"initiator": initiator_peer_id,
|
||
|
|
"description": description,
|
||
|
|
"context": context or {},
|
||
|
|
"status": "pending",
|
||
|
|
"approvers": self.approvers,
|
||
|
|
"channel_type": "mattermost",
|
||
|
|
}
|
||
|
|
|
||
|
|
def parse_allow_from_approvers(self, allow_from: list[str]) -> list[str]:
|
||
|
|
return [e for e in allow_from if e and e.strip() != "*"]
|