Files
smeserver-gitutils/TestPrograms/test_webhook_http.py

46 lines
1.7 KiB
Python

#!/usr/bin/env python3
import os, json, hmac, hashlib, httpx
WEBHOOK_URL = os.environ.get("WEBHOOK_URL", "https://githubpr.bjsystems.co.uk/webhook/github")
SECRET = os.environ.get("WEBHOOK_SECRET", "devsecret")
def sign(secret: str, body: bytes) -> str:
return "sha256=" + hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
def payload(action="opened"):
return {
"action": action,
"repository": {
"full_name": "Koozali-SME-Server/smeserver-manager",
"owner": {"login": "Koozali-SME-Server"},
"name": "smeserver-manager",
},
"pull_request": {
"number": 52,
"html_url": "https://github.com/Koozali-SME-Server/smeserver-manager/pull/42",
"title": "Test PR from harness",
"body": "Harness test body.",
"user": {"login": "octocat", "html_url": "https://github.com/octocat"},
"base": {"ref": "master", "sha": "9f8e7d6cafebabe0000deadbeef0000000000000"},
"head": {"ref": "feature/test", "sha": "a1b2c3ddeeddbb0000deadbeef0000000000000", "repo": {"owner": {"login": "octocat"}}},
"created_at": "2025-09-17T12:34:56Z",
"labels": [{"name": "enhancement"}],
},
}
def post_event(action="opened"):
body = json.dumps(payload(action)).encode("utf-8")
headers = {
"X-GitHub-Event": "pull_request",
"X-Hub-Signature-256": sign(SECRET, body),
"Content-Type": "application/json",
}
r = httpx.post(WEBHOOK_URL, data=body, headers=headers, timeout=30)
print(f"{action} ->", r.status_code, r.text)
if __name__ == "__main__":
# Send an "opened" event then a "synchronize" event
post_event("opened")
post_event("synchronize")