|
1 | 1 | from .webhook import webhookBlueprint
|
2 | 2 | from typing import Callable, Any
|
3 | 3 | from logging import Logger
|
| 4 | +import json |
4 | 5 |
|
5 | 6 | class functionWebhookBlueprint(webhookBlueprint):
|
6 | 7 | """A subclass of webhookBlueprint that processes the webhook data using a list of functions. The functions should return True if the webhook data is valid, and False otherwise. If the function returns a string, it will be included in the output."""
|
7 | 8 |
|
8 |
| - def __init__(self, webhookToken: str | None, functions: list[Callable[[dict[str, Any]], bool | str]], log:Logger | None = None, name:str="webhook", github:bool=True, gitlab:bool=True, gitea:bool=True, ipWhitelist:list[str] | None = None, *args, **kwargs): |
| 9 | + def __init__(self, webhookToken: str | None, functions: list[Callable[[dict[str, Any]], bool | Any]], log:Logger | None = None, name:str="webhook", github:bool=True, gitlab:bool=True, gitea:bool=True, ipWhitelist:list[str] | None = None, *args, **kwargs): |
9 | 10 | """Initialize the webhook blueprint with a list of functions to process the webhook data.
|
10 | 11 |
|
11 | 12 | Args:
|
@@ -38,24 +39,36 @@ def processWebhook(self, data: dict[str, Any]) -> tuple[int, str]:
|
38 | 39 | """
|
39 | 40 | if self.log is not None:
|
40 | 41 | self.log.debug(f"Processing webhook: {data}")
|
41 |
| - output = [] |
| 42 | + success = True |
| 43 | + output:dict[str, str | bool] = {} |
42 | 44 | for function in self.functions:
|
43 |
| - res = function(data) |
44 |
| - if isinstance(res, str): |
| 45 | + try: |
| 46 | + res = function(data) |
| 47 | + except Exception as e: |
| 48 | + output[function.__name__] = str(e) |
45 | 49 | if self.log is not None:
|
46 |
| - self.log.debug(f"Function {function.__name__} returned a string") |
47 |
| - output.append(res) |
48 |
| - elif isinstance(res, bool): |
| 50 | + self.log.error(f"Function {function.__name__} raised an exception: {e}") |
| 51 | + success = False |
| 52 | + continue |
| 53 | + if isinstance(res, bool): |
49 | 54 | if not res:
|
50 | 55 | if self.log is not None:
|
51 | 56 | self.log.error(f"Function {function.__name__} returned false")
|
52 |
| - return 400, f"Function {function.__name__} returned false" |
| 57 | + success = False |
53 | 58 | else:
|
54 | 59 | if self.log is not None:
|
55 | 60 | self.log.debug(f"Function {function.__name__} returned true")
|
56 |
| - output.append(str(res)) |
| 61 | + output[function.__name__] = res |
57 | 62 | else:
|
58 | 63 | if self.log is not None:
|
59 |
| - self.log.error(f"Function {function.__name__} returned an invalid type") |
60 |
| - return 500, f"Function {function.__name__} returned an invalid type" |
61 |
| - return 200, str(output) |
| 64 | + self.log.debug(f"Function {function.__name__} returned a string") |
| 65 | + try: |
| 66 | + output[function.__name__] = str(res) |
| 67 | + except Exception as e: |
| 68 | + if self.log is not None: |
| 69 | + self.log.error(f"Function {function.__name__} returned an invalid type") |
| 70 | + return 500, f"Function {function.__name__} returned an invalid type" |
| 71 | + if success: |
| 72 | + return 200, str(output) |
| 73 | + else: |
| 74 | + return 400, json.dumps(output) |
0 commit comments