Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forwarder cloud function #1

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn -w 2 -k uvicorn.workers.UvicornWorker main:app
17 changes: 17 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "GitHub Webhook Forwarder",
"description": "Forwards GitHub webhooks to dev instances of Pangeo Forge API.",
"image": "heroku/python",
"repository": "https://github.com/pangeo-forge/github-webhook-forwarder",
"keywords": [],
"addons": [],
"env": {},
"environments": {
"test": {
"scripts": {
"test-setup": "python -m pip install -r dev-requirements.txt",
"test": "pytest test.py -v"
}
}
}
}
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
68 changes: 68 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import httpx
from fastapi import FastAPI, HTTPException, Request, status
from pydantic import BaseModel

app = FastAPI()


class GitHubLabel(BaseModel):
name: str


@app.post("/", status_code=status.HTTP_200_OK)
async def forwarder(request: Request):
"""

"""

event = request.headers.get("X-GitHub-Event")
print(f"{event = }")

request_json = await request.json()
request_bytes = await request.body()

if event == "pull_request":
label_containing_obj = "pull_request"
elif event == "issue_comment":
label_containing_obj = "issue"
else:
raise NotImplementedError

labels = [
GitHubLabel(**label)
for label in request_json[label_containing_obj]["labels"]
]
if not labels:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="No labels."
)

forward_to = [
l.name.split("fwd:")[-1]
for l in labels
if l.name.startswith("fwd:")
]
if not forward_to:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="No labels starting with 'fwd:'",
)

responses = {}
for recipient in forward_to:
print(f"Forwarding GitHub webhook to {recipient = }")
async with httpx.AsyncClient() as client:
r = await client.post(
f"https://{recipient}",
headers={
"X-GitHub-Event": request.headers.get("X-GitHub-Event"),
"X-Hub-Signature-256": request.headers.get("X-Hub-Signature-256"),
"content-type": "application/json",
},
data=request_bytes,
)
print(f"{recipient = } responded with {r.status_code = }")
responses |= {recipient: r.status_code}

return f"Forwarded to {responses}"
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi==0.87.0
gunicorn==20.1.0
httpx==0.23.3
uvicorn==0.20.0
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.11.1
2 changes: 2 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_main():
...