-
Notifications
You must be signed in to change notification settings - Fork 4
/
github_webhook_event_logger.py
90 lines (77 loc) · 2.47 KB
/
github_webhook_event_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Usage
*****
.. code-block:: console
$ python github_webhook_event_logger.py -R org/repo
"""
import os
import sys
import json
import pathlib
import argparse
import subprocess
import http.server
import httptest
class GitHubWebhookLogger(http.server.BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", 3)
self.end_headers()
self.wfile.write(b"OK\n")
payload = json.loads(self.rfile.read())
payload_path = pathlib.Path(
".".join(
[
self.headers["X-github-event"],
*(
[
":".join([key, value])
for key, value in payload.items()
if isinstance(value, str) and not os.sep in value
]
+ ["json"]
),
],
)
)
payload_path.write_text(json.dumps(payload, indent=4, sort_keys=True))
@classmethod
def cli(cls, argv=None):
parser = argparse.ArgumentParser(description="Log GitHub webhook events")
parser.add_argument(
"-R",
"--repo",
dest="org_and_repo",
required=True,
type=str,
help="GitHub repo in format org/repo",
)
parser.add_argument(
"--state-dir",
dest="state_dir",
type=pathlib.Path,
help="Directory to cache requests in",
default=pathlib.Path(os.getcwd(), ".cache", "httptest"),
)
args = parser.parse_args(argv)
if not args.state_dir.is_dir():
args.state_dir.mkdir(parents=True)
with httptest.Server(cls) as logging_server:
with httptest.Server(
httptest.CachingProxyHandler.to(
logging_server.url(), state_dir=str(args.state_dir.resolve())
)
) as cache_server:
subprocess.check_call(
[
"gh",
"webhook",
"forward",
f"--repo={args.org_and_repo}",
"--events=*",
f"--url={cache_server.url()}",
]
)
if __name__ == "__main__":
GitHubWebhookLogger.cli(sys.argv[1:])