Skip to content

Commit a8ff52c

Browse files
committed
Directory import fix
1 parent 983e600 commit a8ff52c

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ However due to the open ended nature of the blueprint this behavior can be easil
3131
pip install -r gitWebhook/requirements.txt
3232
```
3333

34+
While installing the pip package functions as any other package the thing with this repo folder is that it functions like a local package.
35+
That means that cloning this repo into your project folder will allow you to import ```gitWebhook``` as if it was installed via pip.
36+
Same applies with adding as submodule with the added benefit of git understanding what is going on better,
37+
3438
3. Create an instance of ```webhookBlueprint``` (or either of it's subclasses) with your settings
3539
3640
```python3

Diff for: __init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Locally defined gitWebhook module."""
22

3-
from gitWebhook import *
3+
from .gitWebhook import webhookBlueprint, gitWebhookBlueprintABC, pullerWebhookBlueprint, functionWebhookBlueprint
44

55
__all__ = ["webhookBlueprint", "gitWebhookBlueprintABC", "pullerWebhookBlueprint", "functionWebhookBlueprint"]

Diff for: gitWebhook/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Python module providing Flask blueprints for handling GitHub and GitLab webhooks."""
22

3-
from gitWebhook.webhook import webhookBlueprint
4-
from gitWebhook.abstractWebhook import gitWebhookBlueprintABC
5-
from gitWebhook.pullerWebhook import pullerWebhookBlueprint
6-
from gitWebhook.functionWebhook import functionWebhookBlueprint
3+
from .webhook import webhookBlueprint
4+
from .abstractWebhook import gitWebhookBlueprintABC
5+
from .pullerWebhook import pullerWebhookBlueprint
6+
from .functionWebhook import functionWebhookBlueprint
77

88
__all__ = ["webhookBlueprint", "gitWebhookBlueprintABC", "pullerWebhookBlueprint", "functionWebhookBlueprint"]

Diff for: gitWebhook/functionWebhook.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from gitWebhook.webhook import webhookBlueprint
1+
from .webhook import webhookBlueprint
22
from typing import Callable, Any
33

44
class functionWebhookBlueprint(webhookBlueprint):
55
"""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."""
6+
67
def __init__(self, webhookToken: str | None, functions: list[Callable[[dict[str, Any]], bool | str]], *args, **kwargs):
78
super().__init__(webhookToken, *args, **kwargs)
89
self.functions = functions
10+
911
def processWebhook(self, data: dict[str, Any]) -> tuple[int, str]:
1012
if self.log is not None:
1113
self.log.debug(f"Processing webhook: {data}")
@@ -30,4 +32,3 @@ def processWebhook(self, data: dict[str, Any]) -> tuple[int, str]:
3032
self.log.error(f"Function {function.__name__} returned an invalid type")
3133
return 500, f"Function {function.__name__} returned an invalid type"
3234
return 200, str(output)
33-

Diff for: gitWebhook/pullerWebhook.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
from logging import Logger
22
from typing import Any
33
from unittest import TestSuite
4-
from gitWebhook.webhook import webhookBlueprint
4+
from .webhook import webhookBlueprint
55
from subprocess import run
66
from unittest import TestSuite, TestResult
77

88
class pullerWebhookBlueprint(webhookBlueprint):
99
"""A subclass of webhookBlueprint that processes the webhook data by pulling from a git repository and running tests."""
10+
1011
def __init__(self, webhookToken: str | None, tests: TestSuite | None = None, log: Logger | None = None, name: str = "webhook", gitCommand: str = "/usr/bin/git", commandEnv: dict[str, str] | None = None, *args, **kwargs):
1112
super().__init__(webhookToken, log, name, *args, **kwargs)
1213
self.tests = tests
1314
self.gitCommand = gitCommand
1415
if commandEnv is None:
1516
commandEnv = dict(GIT_SSH_COMMAND="/usr/bin/ssh")
1617
self.commandEnv = commandEnv
18+
1719
def processWebhook(self, data: dict[str, Any]) -> tuple[int, str]:
1820
if self.log is not None:
1921
self.log.debug(f"Processing webhook: {data}")

Diff for: gitWebhook/webhook.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from hmac import new as hmacNew
44
from typing import Any
55
from logging import Logger
6-
from gitWebhook.abstractWebhook import gitWebhookBlueprintABC
6+
from .abstractWebhook import gitWebhookBlueprintABC
77

88
GITHUB_HEADER = "X-Hub-Signature-256"
99

@@ -24,6 +24,7 @@ def verifyGitlabRequest(request: Request, token:str) -> bool:
2424

2525
class webhookBlueprint(Blueprint, gitWebhookBlueprintABC):
2626
"""Wrapper over the flask blueprint that creates an endpoint for receiving and processing git webhooks. Overwrite the processWebhook method to process the webhook data."""
27+
2728
def __init__(self, webhookToken:str | None, log:Logger | None = None, name:str="webhook", *args, **kwargs):
2829
super().__init__(name, self.__class__.__name__, *args, **kwargs)
2930
self.log = log
@@ -32,6 +33,7 @@ def __init__(self, webhookToken:str | None, log:Logger | None = None, name:str="
3233
self.log.warning("No webhook token provided. THIS IS VERY UNSAFE")
3334
self.webhookToken = webhookToken
3435
self.route("/", methods=["POST"])(self.receiveWebhook)
36+
3537
def receiveWebhook(self) -> Response:
3638
"""Receive webhook from GitHub and process it using the processWebhook method."""
3739
if self.log is not None:
@@ -67,6 +69,7 @@ def receiveWebhook(self) -> Response:
6769
if self.log is not None:
6870
self.log.info(f"Webhook processed with status code {ret[0]} and message: {ret[1]}")
6971
return Response(ret[1], status=ret[0])
72+
7073
def processWebhook(self, data:dict[str, Any]) -> tuple[int, str]:
7174
"""Process the webhook. Return a tuple of (status code, message)"""
7275
return (200, "OK")

0 commit comments

Comments
 (0)