# GitHub Action

> Check every pull request against the other open pull requests, build risky pairs together, and get a merge order.

The Action runs in your own workflow; your code never leaves it. `lk init --ci` adds this file:

```yaml
# .github/workflows/lanekeeper.yml
name: Lanekeeper

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
  push:
    branches: [main, master]

concurrency:
  group: lanekeeper-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  pull-requests: write
  issues: write
  checks: write

jobs:
  lanekeeper:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - uses: UpwanSingh/lanekeeper-action@v1
```

## What it does on a pull request

1. Lists the other open pull requests into the same base branch, the 50 most recently updated (drafts only with
   `include-drafts`).
2. Fetches their heads and works out each one's changes from its merge base.
3. Runs the rules between this pull request and each other one, using `git merge-tree` to confirm real text
   conflicts. Claims (R5) live on developers' machines, so they aren't checked here.
4. If you set `build-command` or `test-command`, builds and tests the riskiest pairs together on top of the base
   branch. A pair is reported (R10) only when it fails together while each pull request passes on its own.
5. Posts one comment that it edits in place, adds a check run named "Lanekeeper" with annotations on the lines
   involved, and writes the job summary.

On a push to the base branch it re-checks the open pull requests and edits a comment only when the result changed.

## Inputs

| Input | Default | Description |
|---|---|---|
| `token` | `github.token` | Token for pull requests, comments and check runs. |
| `mode` | `both` | `comment`, `check`, or `both`. |
| `build-command` | none | Builds the project, including the dependency install, in a temporary checkout. |
| `test-command` | none | Tests the project after the build. |
| `max-pairs` | `6` | Most pairs to build and test together, riskiest first. |
| `fail-on` | `high` | Fail the job on `high` clashes, on `warn` too, or `never`. |
| `include-drafts` | `false` | Compare against draft pull requests too. |
| `command-timeout-minutes` | `20` | Time limit for each build or test command. |
| `config` | `.lanekeeper/config.yml` | Path to the config file. |

Outputs: `verdict` (`safe`, `coordinate`, `breaks`), `clashes` (a count) and `state` (a fingerprint of the result).

## Safety

- Code from a fork's pull request is never built. With `build-command` or `test-command`, the Action builds this
  pull request together with other open pull requests from the same repository, so their code runs in this job.
  That code was pushed by someone with write access to the repository.
- Build and test commands run in a throwaway clone outside the workspace, with git hooks off and a short list of
  environment variables (`PATH`, `HOME`, `CI`, locale and toolchain locations) and a temp directory of their own.
  The Action's inputs and token, the runner's internal tokens, the workspace path and the locations of the files
  that set later steps' environment and outputs (`GITHUB_ENV`, `GITHUB_PATH`, `GITHUB_OUTPUT`, and
  `RUNNER_TEMP`, where hosted runners keep them) aren't passed to them, and the clone has no credentials in its
  git config.
- That keeps tokens out of the commands' environment, but it isn't a sandbox: the commands run as the same OS
  user as the job, which can read the workspace and the job's other processes, and can still write those files
  if it goes looking for them. Don't give a job that runs build
  or test commands secrets or permissions you wouldn't give every writer to the repository; set
  `persist-credentials: false` on `actions/checkout` there.
- The config and repository policy come from the base branch, never from the pull request being checked, so a
  pull request can't ignore files or switch rules off to pass. A change to them applies once it merges.
- Pull requests from forks get a read-only token from GitHub. The Action then annotates the job log instead of
  creating a check run.
- If Lanekeeper itself fails, the job gets a warning and passes; `fail-on` only applies to clashes.
