|
| 1 | +# 01 — Prerequisites and repo overview |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +You need: |
| 6 | + |
| 7 | +- A GCP project with billing enabled. |
| 8 | +- `gcloud` installed and authenticated (`gcloud auth login`). |
| 9 | +- Terraform >= 1.6. |
| 10 | +- Docker (and ideally Buildx for `linux/amd64` builds). |
| 11 | +- A GitHub account that can create and install GitHub Apps on the target org/user. |
| 12 | + |
| 13 | +Recommended: |
| 14 | + |
| 15 | +- `pre-commit` installed. |
| 16 | +- Permissions in GCP to create Cloud Run, API Gateway, Artifact Registry, Secret Manager secrets, IAM bindings, and KMS keys. |
| 17 | + |
| 18 | +## Repo overview (what code does what) |
| 19 | + |
| 20 | +### Runtime (FastAPI) |
| 21 | + |
| 22 | +- `src/app.py` |
| 23 | + - `POST /webhooks/github` is the GitHub webhook endpoint. |
| 24 | + - Verifies the webhook signature. |
| 25 | + - Handles `pull_request.opened` by posting a comment back on the PR. |
| 26 | + - `GET /healthz` is a health check. |
| 27 | + |
| 28 | +- `src/webhook.py` |
| 29 | + - Computes an HMAC SHA-256 digest of the request body using the webhook secret. |
| 30 | + - Compares it to the `X-Hub-Signature-256` header. |
| 31 | + |
| 32 | +- `src/github_app.py` |
| 33 | + - Creates a GitHub App JWT (signed with the app’s private key). |
| 34 | + - Exchanges it for an **installation access token**. |
| 35 | + - Uses the installation token to call GitHub’s API (post PR comment). |
| 36 | + |
| 37 | +### Secrets strategy (best practice) |
| 38 | + |
| 39 | +- **Local dev**: you can pass secrets via environment variables (e.g. `docker run --env-file .env ...`). |
| 40 | +- **Cloud Run**: secrets should not be plain env vars. |
| 41 | + - Store secret values in **Secret Manager**. |
| 42 | + - Mount them into the container as **files**. |
| 43 | + - The app reads them via: |
| 44 | + - `GITHUB_PRIVATE_KEY_FILE` |
| 45 | + - `GITHUB_WEBHOOK_SECRET_FILE` |
| 46 | + |
| 47 | +This keeps secret values out of container images, build logs, and Terraform state. |
| 48 | + |
| 49 | +## Install and run repo tooling (optional but recommended) |
| 50 | + |
| 51 | +From the repo root: |
| 52 | + |
| 53 | +```bash |
| 54 | +python -m venv .venv |
| 55 | +source .venv/bin/activate |
| 56 | +pip install -r requirements.txt |
| 57 | +pip install pre-commit |
| 58 | +pre-commit install |
| 59 | +``` |
0 commit comments