Skip to content

Commit

Permalink
Add GitHub webhook support for automatic updates
Browse files Browse the repository at this point in the history
- Implemented a `/update-webhook` endpoint to trigger the updater when a new release is created on GitHub, enabling automatic updates from the webhook.
- Added a `/webhook-debug` endpoint to log incoming webhook requests, helping with debugging and validating webhook payloads.
  • Loading branch information
Lenochxd committed Sep 9, 2024
1 parent 132803a commit 0b702f7
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from starlette.middleware.base import BaseHTTPMiddleware
from time import time
from collections import defaultdict
import updater

# Change directory to the main.py dir
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -27,6 +28,7 @@ def load_config():
return config

config = load_config()
DEBUG = False

# Check if 'database-path' is valid and existing
if 'database-path' not in config or not os.path.exists(config['database-path']):
Expand Down Expand Up @@ -330,6 +332,61 @@ async def get_nx(platform: str, tid: str, asset_type: str = None, screen_id = 1)
def uptime():
return Response(status_code=200)


@app.post('/update-webhook')
async def update_webhook(request: Request):
# Get the JSON payload from the request
payload = await request.json()
headers = dict(request.headers)

# Check if the request is from GitHub
if headers.get('user-agent', '').startswith('GitHub-Hookshot'):
# Check if the action is a release
if payload.get('action') == 'created' and 'release' in payload:
updater.auto_update()

return JSONResponse(content={"message": "Webhook received and processed successfully"}, status_code=200)
else:
raise HTTPException(status_code=400, detail="Invalid webhook payload")


if DEBUG:
@app.post('/webhook-debug')
async def webhook(request: Request):
# Get the raw request body
body = await request.body()

# Get all headers
headers = dict(request.headers)

# Get query parameters
query_params = dict(request.query_params)

# Get the client's IP address
client_ip = request.client.host

# Get the HTTP method
http_method = request.method

# Get the full URL
url = str(request.url)

# Prepare the response content
content = {
"method": http_method,
"url": url,
"client_ip": client_ip,
"headers": headers,
"query_params": query_params,
"body": body.decode('utf-8') # Decode bytes to string
}

# Print everything
print(json.dumps(content, indent=2))

return JSONResponse(content=content)


if __name__ == '__main__':
import uvicorn
uvicorn.run(
Expand Down

0 comments on commit 0b702f7

Please sign in to comment.