Skip to content

Commit

Permalink
tests: add config to automate fixtures (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Sep 21, 2024
1 parent 86052b4 commit bf65d28
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hardcoding all the args for now, can be dynamic later

_default:
@just --list

# Start a server on http://localhost:3000 which receives webhook events and dumps the payloads into the fixtures folder
listen:
python3 server.py

# Requires the following commands to be run first:
# - gh extension install cli/gh-webhook
# - gh auth refresh -h github.com -s admin:org_hook
#
# Create a development GitHub webhook and forward all webhook events to http://localhost:3000
register_webhook:
gh webhook forward --events='*' --org=catppuccin-rfc --url="http://localhost:3000"

# Create a new issue, close and reopen it in catppuccin-rfc/polybar
issues:
#!/usr/bin/env bash
ISSUE_URL=$(gh issue create --title "rockdove-{{datetime_utc("%Y%m%d_%H%M%S")}}" --body "rockdove" --repo catppuccin-rfc/polybar)
gh issue close "$ISSUE_URL"
gh issue reopen "$ISSUE_URL"
39 changes: 39 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from pathlib import Path


class WebhookHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
payload = json.loads(post_data.decode("utf-8"))

event_type = self.headers.get("X-GitHub-Event")
# Worth noting that the action field can be the same for different
# events that we want to track so we may want some custom logic in the
# future to handle this or not bother with auto-saving the payload from
# it.
action = payload.get("action", "default")

filename = Path(f"fixtures/{event_type}/{action}.json")
filename.parent.mkdir(parents=True, exist_ok=True)
with open(filename, "w") as f:
json.dump(payload, f, indent=2)

print(f"Received {event_type} event with action: {action}")
print(f"Payload saved to {filename}")

self.send_response(200)
self.end_headers()


def run_server(port=3000):
server_address = ("", port)
httpd = HTTPServer(server_address, WebhookHandler)
print(f"Server running on http://localhost:{port}")
httpd.serve_forever()


if __name__ == "__main__":
run_server()

0 comments on commit bf65d28

Please sign in to comment.