Check or block a pull request before it opens

The example sends any pull request that touches database migrations to a DBA's review service before the agent opens it. You need:

  • Terraform set up with the Spotlight provider, using a token that has the configure scope. See Manage Spotlight with Terraform.
  • An HTTPS endpoint you control that can check a signature or a token.

Create the hook

Fire the hook on pull-request.opening, narrow it with match, and send one request step to your endpoint. Set failure = "block" so an endpoint that fails or does not answer in time also stops the pull request.

data "spotlight_workspace" "acme" {
  slug = "acme"
}

data "spotlight_team" "core" {
  workspace_id = data.spotlight_workspace.acme.id
  key          = "core"
}

resource "spotlight_hook" "migrations" {
  team_id = data.spotlight_team.core.id
  key     = "migrations-need-a-dba"
  on      = "pull-request.opening"
  match = {
    paths = ["db/migrations/**"]
  }
  vars = { migrations = "db/migrations/" }
  steps = [
    {
      name = "review"
      request = {
        url  = "https://dba.acme.example/review"
        body = "$${{ {'pr': event.title, 'files': event.changed.filter(path, path.startsWith(vars.migrations))} }}"
      }
    },
  ]
  signing = {
    oidc = { audience = "https://dba.acme.example" }
  }
  failure         = "block"
  timeout_seconds = 20
}

To limit the hook to some repositories or lines, add repositories or lines to match. Without body, the request carries the hook's key, the event, the task and the run.

Spotlight sends the request from its own servers. On a self-hosted Spotlight with an outbound allow-list, the apply rejects the hook if the list does not include your endpoint's host. To send it from inside your network instead, set from = "network" on the request, and a host of the workspace sends it.

Choose how the request is signed

Every request step must be signed. Pick one:

Keep the oidc block above. Each request carries authorization: Bearer <token>, a short-lived token whose issuer is https://app.usespotlight.ai/api, whose audience is the one you set, and whose subject is hook:<workspace id>/<team id>/migrations-need-a-dba. Check it against the keys at https://app.usespotlight.ai/api/.well-known/jwks.json. On a self-hosted Spotlight, use your deployment's address in place of app.usespotlight.ai.

If the apply answers that this Spotlight has no key to sign OIDC tokens with, use a shared secret, or ask whoever runs Spotlight to set HOOK_SIGNING_KEY.

Run terraform apply.

Answer from your endpoint

To let the pull request open, answer with any 2xx. To stop it, answer with a 2xx and a JSON body that has a deny string, the reason:

{ "deny": "Migrations need a DBA's review first: ask in #dba." }

Check a denial

Have an agent open a pull request that touches db/migrations/ while your endpoint denies it. No pull request is opened. The agent's pull-request call fails with A hook stopped the pull request. No pull request was opened. followed by Hook migrations-need-a-dba denied the event. Reason: and your reason, and the agent carries on from that answer.

On this page