# Check a pull request in CI

Source: https://watership.ca/api-docs/docs/tasks/check-a-pull-request

Check the Markdown files a pull request changes, annotate each finding on the diff, and fail the build on errors.

This guide sets up a GitHub Actions job that sends every changed Markdown file to `POST /v1/checks`. Each finding appears as an annotation on the pull request, up to the limit GitHub sets, and the job fails if any finding is an error. Warnings and suggestions show up but don't block the merge.

<Callout type="warn" title="Design preview">
  Until the hosted API exists, the workflow below starts the mock server inside the job, so you can run it end to end today. The mock responds with the same example findings for every file. When the service is live, remove the mock step and set the API address and key as repository secrets.
</Callout>

## Add the check script [#add-the-check-script]

Save this script in your repository as `scripts/watership-check.mjs`. It takes file paths as arguments, checks each one, and prints [GitHub workflow commands](https://docs.github.com/actions/reference/workflows-and-actions/workflow-commands) that turn findings into annotations.

```js title="scripts/watership-check.mjs"
import { readFile } from 'node:fs/promises';

const API_URL = process.env.WATERSHIP_API_URL ?? 'http://127.0.0.1:4010';
const API_KEY = process.env.WATERSHIP_API_KEY;
const COMMAND = { error: 'error', warning: 'warning', suggestion: 'notice' };

// Workflow commands need %, line breaks, and (in properties) : and , escaped.
const esc = (s) => String(s).replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');
const prop = (s) => esc(s).replace(/:/g, '%3A').replace(/,/g, '%2C');

if (!API_KEY) {
  console.error('Set WATERSHIP_API_KEY.');
  process.exit(2);
}

let errors = 0;

for (const file of process.argv.slice(2)) {
  const text = await readFile(file, 'utf8');
  if (!text.trim()) continue;

  const response = await fetch(`${API_URL}/v1/checks`, {
    method: 'POST',
    headers: { Authorization: `Bearer ${API_KEY}`, 'Content-Type': 'application/json' },
    body: JSON.stringify({ text, format: 'markdown' }),
  });
  if (!response.ok) {
    const problem = await response.json();
    console.log(`::error file=${prop(file)},title=Watership check failed::${esc(problem.detail)}`);
    errors++;
    continue;
  }

  const result = await response.json();
  for (const f of result.findings) {
    const title = prop(f.rule_ids.join(', ') || f.check);
    console.log(`::${COMMAND[f.level]} file=${prop(file)},line=${f.span.line},col=${f.span.column},title=${title}::${esc(f.message)} ${f.link}`);
  }
  if (result.truncated) console.log(`::warning file=${prop(file)}::Only the first 500 findings are shown.`);
  errors += result.counts.error;
}

if (errors) {
  console.log(`${errors} error${errors === 1 ? '' : 's'}. Errors are never-tier rules and block the merge.`);
  process.exit(1);
}
```

The script fails the job on `counts.error`, not on the number of findings in the list, so a truncated response still counts every error.

## Add the workflow [#add-the-workflow]

Save this as `.github/workflows/watership.yml`:

```yaml title=".github/workflows/watership.yml"
name: watership

on:
  pull_request:
    paths: ['**/*.md', '**/*.mdx']

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: 24

      # Design preview: run the mock server from the Watership spec.
      # Remove these two steps when the hosted API is live.
      - run: curl -fsSL -o watership.yaml "$WATERSHIP_SPEC_URL"
        env:
          WATERSHIP_SPEC_URL: ${{ vars.WATERSHIP_SPEC_URL }}
      - run: |
          npx --yes @stoplight/prism-cli@5.16.0 mock watership.yaml --port 4010 &
          # Any HTTP response, even 401, means the mock is up.
          for i in $(seq 1 60); do curl -s -o /dev/null http://127.0.0.1:4010/v1/voices && break; sleep 1; done

      - name: Check changed Markdown
        env:
          WATERSHIP_API_URL: ${{ vars.WATERSHIP_API_URL || 'http://127.0.0.1:4010' }}
          WATERSHIP_API_KEY: ${{ secrets.WATERSHIP_API_KEY || 'wsk_test_ci' }}
        run: |
          files=$(git diff --name-only --diff-filter=AM "origin/${{ github.base_ref }}...HEAD" -- '*.md' '*.mdx')
          if [ -z "$files" ]; then echo "No Markdown changed."; exit 0; fi
          node scripts/watership-check.mjs $files
```

Set the repository variable `WATERSHIP_SPEC_URL` to the address of `openapi.yaml` on the published docs, or commit a copy of the spec and point the mock at that file instead.

## Check that it works [#check-that-it-works]

Open a pull request that changes a Markdown file. In the pull request, select **Files changed**. Each finding appears beside its line, with the rule id as the title. Select the link in an annotation to read the rule.

GitHub shows at most 10 annotations of each level (error, warning, and notice) from one step, and 50 from one job, and drops the rest without saying so. The job log keeps every line the script printed, so on a large change, read the full list there: open the **watership** check and select its **Check changed Markdown** step.

If any finding is an error, the **watership** check fails. Fix the text, or, if the rule doesn't apply here, ask the team that owns your style guide to adjust it. Your review policy says whether a never-tier finding can ever stay with a recorded reason. This workflow fails on every one.

## Related [#related]

- To report only some findings, see [Filter findings](https://watership.ca/api-docs/docs/tasks/filter-findings).
- To retry when the API is busy, see [Handle errors and rate limits](https://watership.ca/api-docs/docs/tasks/handle-errors-and-rate-limits).
- For what errors, warnings, and suggestions mean, see [Tiers and check types](https://watership.ca/api-docs/docs/concepts/tiers-and-check-types).
