GitHub Webhook Example

Start with this template
66
import logging
from fastapi import FastAPI, Request, responses
from github_webhook_handler import router as github_webhook_router

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)

app = FastAPI()

app.include_router(github_webhook_router, prefix="/github")

@app.get("/", include_in_schema=False)
def root():
    return responses.RedirectResponse(url='/docs')

# Do not remove the main function while updating the app.
if __name__ == "__main__":
    import uvicorn

    # This initialization is essential and must not be removed.
    uvicorn.run(app, host="0.0.0.0", port=8080, log_level="info")
Get full code

GitHub Webhook Example

Created: | Last Updated:

Introduction to the GitHub Webhook Example Template

Welcome to the GitHub Webhook Example Template! This template is designed to help you create an application that can handle GitHub webhooks. It's perfect for developers who want to automate workflows or integrate with other services whenever a specific event occurs in a GitHub repository. The application is built using FastAPI, a modern, fast web framework for building APIs with Python.

By using this template, you'll be able to receive webhook payloads from GitHub, process them as needed, and even trigger other actions or notifications. This could be useful for continuous integration, project management, or any other process that you'd like to automate based on GitHub events.

Clicking Start with this Template

To get started, simply click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Test: Pressing the Test Button

Once you have started with the template, the next step is to press the "Test" button. This will begin the deployment of your application on the Lazy platform. You don't need to worry about installing libraries or setting up your environment, as Lazy handles all of that for you.

Using the App

After pressing the "Test" button, Lazy will print a dedicated server link for you to use the API. Since this template uses FastAPI, you will also be provided with a link to the FastAPI documentation. This documentation is interactive and allows you to test the API endpoints directly from your browser.

To see the application in action, you can set up a webhook on GitHub to point to the server link provided by Lazy. Here's how to do that:

  • Go to the GitHub repository where you want to set up the webhook.
  • Click on "Settings" and then on "Webhooks."
  • Click on "Add webhook."
  • In the "Payload URL" field, enter the server link provided by Lazy, followed by /github/webhook/.
  • Select the content type as "application/json."
  • Choose which events you would like to receive payloads for, or select "Send me everything."
  • Click on "Add webhook" to save your settings.

Now, whenever the selected events occur in your GitHub repository, GitHub will send a POST request to your application with the event's data. Your application will process this data and print out some of its attributes for you to see.

Integrating the App

If you want to integrate this webhook handler into another service or frontend, you can use the server link provided by Lazy as the endpoint to which the other services will send or receive data. For example, you could set up a continuous integration service to trigger a build whenever a "push" event is received from GitHub.

Here's a sample request that GitHub might send to your webhook handler:

`POST /github/webhook/ HTTP/1.1
Host: your-lazy-server-link
Content-Type: application/json
X-GitHub-Event: push
X-GitHub-Delivery: some-delivery-id

{
  "ref": "refs/heads/main",
  "before": "commit-id-before",
  "after": "commit-id-after",
  ...
}` And here's a sample response that your application will send back to GitHub:

`HTTP/1.1 200 OK
Content-Type: application/json

{
  "message": "Webhook data received",
  "data": {
    "ref": "refs/heads/main",
    "before": "commit-id-before",
    "after": "commit-id-after",
    ...
  }
}` By following these steps, you can easily set up and use the GitHub Webhook Example Template to create an application that interacts with GitHub webhooks, all within the Lazy platform.

Technologies

GitHub GitHub
Flask Flask
Python Python