Record GitHub pushes from the organization webhook
Ruby · Rails · advanced · modification
Adds the receiver for GitHub's organization webhook, so the dashboard lists pushes as they happen. `config/routes.rb` gains `post "github"` under `namespace :webhooks`, and `config/initializers/github_webhook.rb` reads the webhook secret from `GITHUB_WEBHOOK_SECRET` outside development and test. The migration adds `pushes` (repository, delivery id, ref, after SHA) with a unique index on `delivery_id`; `Push` belongs to a `Repository`, which gains `has_many :pushes`. `Webhooks::GithubController` turns off forgery protection for `create` only; a `before_action` checks `X-Hub-Signature-256` against the HMAC of `request.raw_post` with `secure_compare`, and `create` ignores events other than `push`, validates the fields it stores, including the bigint range of `repository.id` (400 otherwise), records the push for a tracked repository and treats a duplicate delivery id (`ActiveRecord::RecordNotUnique` from the index) as already done. `github_controller_test.rb` adds integration tests for a recorded push, a pretty-printed body, wrong and missing signatures, a redelivery, ignored events and repositories, and bodies the endpoint cannot read, an out-of-range repository id among them.
Ruby 4.0, Rails 8.1 (`load_defaults 8.1`); Shiplog is Acme's internal deploy dashboard. SQLite in development and test, PostgreSQL 16 in production; the code relies only on the unique index raising `ActiveRecord::RecordNotUnique`, which Rails does for a unique violation on both. Existing and unchanged: an identity-aware proxy in front of the app signs employees in with a session cookie on every path except `/webhooks/`, so the app has no sign-in code of its own, and browser forms rely on Rails' CSRF protection; `ApplicationController < ActionController::Base` with `allow_browser versions: :modern` and Rails' default `protect_from_forgery with: :exception` (a form post without a valid token answers 422); `RepositoriesController` (`index`, and `update` for the default branch); `Repository` (`github_id` bigint `NOT NULL` with a unique index, `full_name`, `default_branch`), added by admins on the dashboard and never deleted. The webhook is configured in GitHub with content type `application/json`, the `push` event, and a secret that production reads from `GITHUB_WEBHOOK_SECRET`. Per GitHub's "Validating webhook deliveries" page the signature is the `sha256=`-prefixed HMAC hex digest of the payload and covers the body only (no timestamp, headers unsigned); deliveries reach the app over HTTPS, and a captured request re-sent under a new delivery id is out of scope for this PR. GitHub's "Best practices for using webhooks" page asks for a 2xx within 10 seconds. The test environment keeps Rails' default `allow_forgery_protection = false`; tests are Minitest integration tests with inline records.
Requirements
- `POST /webhooks/github` (new) receives the deliveries of Acme's GitHub organization webhook (content type `application/json`). GitHub calls it, not a signed-in user, so it needs no sign-in and no CSRF token; every other action of the app keeps Rails' CSRF protection.
- A delivery is accepted only when its `X-Hub-Signature-256` header is exactly `sha256=` followed by the lowercase hex HMAC-SHA256 of the raw request body, byte for byte as received, keyed with the webhook secret, compared in constant time. A missing header, a malformed one or a wrong signature answers 401 and records nothing. If the webhook secret is not configured (unset or empty), every delivery answers 500, records nothing and logs an error: an empty key is never used.
- A verified `push` event (`X-GitHub-Event: push`) for a tracked repository (its `repository.id` equals a `Repository`'s `github_id`) records one `Push` with the delivery id from `X-GitHub-Delivery`, the `ref` and the `after` commit SHA as sent (a branch deletion is recorded too, with an `after` of forty zeros), and answers 204. Other events (`ping` included) and pushes to untracked repositories answer 204 and record nothing. A verified push that has no `X-GitHub-Delivery`, or whose body is not a JSON object with an integer `repository.id` from 1 to 2^63 - 1 (the signed 64-bit range of `github_id`), a non-blank string `ref` and an `after` of exactly 40 hex characters, answers 400 and records nothing.
- GitHub can deliver the same delivery more than once (a redelivery keeps the original `X-GitHub-Delivery`), and two copies can arrive at the same time. Each delivery is recorded at most once; every copy after the first answers 204 and records nothing.
Files touched
- config/routes.rb
- config/initializers/github_webhook.rb
- db/migrate/20261103090000_create_pushes.rb
- app/models/push.rb
- app/models/repository.rb
- app/controllers/webhooks/github_controller.rb
- test/controllers/webhooks/github_controller_test.rb