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

Files touched

Review this PR

Ruby practice