# Contributing

This page says what a contribution needs, how to check one before you push
it, and where the design is written down. A vulnerability is reported
privately, as [SECURITY.md](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/SECURITY.md) says, and everyone here keeps the
[code of conduct](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/CODE_OF_CONDUCT.md).

## Setup

- **PHP 8.4 or later, with pcov.** The coverage gate runs under pcov.
- **`composer install`.** It sets `core.hooksPath` to `.githooks` and runs
  `bin/mutation-gate pest:patch`.
- **Python 3** runs the scripts under `.github/scripts`, the action's scripts under
  `resources/action`, and their tests.
- **Node** runs markdownlint, locked in `.github/bot/markdownlint`, and builds
  the docs site, locked in `.site` (ADR-0038).

The hooks in `.githooks` say early what CI says later:

| Hook         | Refuses                                                                                   |
| ------------ | ----------------------------------------------------------------------------------------- |
| `pre-commit` | a staged PHP file Pint would change                                                       |
| `commit-msg` | a subject that is not conventional, and a `Spec:` number that is no decision              |
| `pre-push`   | a push straight to `main`, and a push that would leave a branch with no commit of its own |

## The local gates

Every CI job has an entry in [`.github/gates.json`](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.github/gates.json). Its
`reproduce` is the command that runs the job here, and its `fix`, where a fix
is mechanical, the command that makes it. This prints the one for `checks`:

```sh
jq -r '.gates.checks.reproduce' .github/gates.json
```

A job that runs several tools, as `hygiene/tools` does, holds a line for each.

Before each push, run the entries for what you changed:

| You changed                                                                                                              | Run                                                                                         |
| ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------- |
| anything                                                                                                                 | `typos` and `python3 .github/scripts/line_cap.py --root .`                                  |
| PHP, `composer.json`, `phpstan.neon`, `phpstan/layers.neon`, `rector.php` or `pint.json`                                 | `checks` and `rules`, and the tests of the code you changed: `vendor/bin/pest <test files>` |
| Markdown                                                                                                                 | the markdownlint line of `hygiene/tools` and `lychee --config lychee.toml .`                |
| `ARCHITECTURE.md` or `.docs/using/`                                                                                      | the Markdown entries, and `composer test`, because tests read them                          |
| `.docs/`, `.site/`, `README.md`, `CONTRIBUTING.md`, `ARCHITECTURE.md`, `SECURITY.md` or what prints a link into the docs | `site/build`                                                                                |
| a workflow                                                                                                               | `actionlint` and `zizmor .`                                                                 |
| `.github/scripts` or `resources/action`                                                                                  | `scripts`                                                                                   |

`tests/Unit` mirrors `src`, so the tests of `src/Core/Plan/Cut.php` are in
`tests/Unit/Core/Plan/CutTest.php`, and each plugin's are under
`plugins/<name>/tests`. Where they would pass the line cap, they are split by
concern into `tests/Unit/Core/Plan/Cut/`, beside the first file, with what the
files share in a class of `tests/Support`. A test that starts a real process
goes in `tests/Process` instead, mirroring `src` the same way. Its file holds
the units its tests kill with a `holds:` group on each test, since the gate's
own config lists `Process` in `tests.holding`
([holding tests](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/features/holding-tests.md)).

The whole suite runs locally too, more slowly:

| Command                  | Runs                                                                                                      |
| ------------------------ | --------------------------------------------------------------------------------------------------------- |
| `composer test`          | every suite but Guards, in parallel                                                                       |
| `composer test:coverage` | the same, with every line of `src` and of each plugin's `src` covered (`tests`)                           |
| `composer test:guards`   | a planted violation of every rule, which each rule must refuse (`guards`)                                 |
| `composer ci`            | validate, normalize, audit, lint, analyse, refactor, deps, `test:guards` and `test:report`, in that order |

`composer phar` builds the gate's PHAR into `build/mutation-gate.phar` as a
release builds it (ADR-0022), with Box at the release `scripts/phar.sh` pins.
Its smoke suite, which CI's `phar` job runs, runs against it with
`vendor/bin/pest --group=phar`, once `composer install --working-dir=tests/Fixtures/Archive` has installed the project it judges.

Leave these to CI:

- `lowest dependencies`, because it rewrites `composer.lock` and `vendor`;
- `runner contracts`, because each of its legs installs a fixture library at
  one end of a supported range first. On a pull request, a runner's legs run
  only where its adapter, its fixture, the test support its tests use or what
  every runner shares changed; `end-to-end / warm workers` runs only where
  the PHPUnit runner, its worker or what every runner shares changed
  (`.github/scripts/scope.py`). Each leg's test support is listed in
  `.github/scripts/legs.json`, and the leg fails where a test of its uses
  support the list does not hold: `python3 .github/scripts/leg_support.py
  write` lists it again. Every leg runs on `main` and every night;
- `sonarcloud` and `gate`, which ask SonarCloud;
- `analyze`, CodeQL's analysis of the workflows.

The benchmark, `bench.yml`, runs on demand and monthly (ADR-0017).
`composer bench -- <project> [cold|scenarios|sharded]` runs one project of it
here, which takes hours and needs what the project's suite needs, such as
MySQL for `pinkary`.

## Commits and pull requests

- **Conventional subjects.** Every commit subject, and the pull request's
  title, is `type(scope): subject`. The type is one of `feat`, `fix`, `docs`,
  `refactor`, `test`, `chore`, `ci`, `perf`, `build`, `style` and `revert`.
  The scope is optional and lowercase, and a `!` before the colon marks a
  breaking change. `pr` checks them.
- **The title and body become the commit on `main`.** `main` takes only
  squash merges, whose subject is the pull request's title and whose message
  is its body.
- **`Spec:` in the body.** A change that follows a decision names it on a
  line of its own, `Spec: 0006`, or several numbers, `Spec: 0002, 0011`. Each
  number must be a decision in `.docs/decisions`, which `description` checks.
  A commit may carry the same trailer, and the `commit-msg` hook checks its
  numbers.
- **A breaking change says how to move.** A title with `!` needs a
  `## Migration` section in the body, saying what a user changes.
- **Signing is not required, and there is no sign-off.** Every commit on
  `main` is signed, because `main` takes only squash commits and GitHub signs
  each one. Your own commits need not be signed, and need no
  `Signed-off-by` line.
- **Open the pull request as a draft,** and mark it ready for review once its
  checks are green.

CI on a pull request from someone who is neither a member of the organisation
nor a collaborator on this repository starts once a maintainer approves the
run. The pull request template holds the checklist.

### The bot's comment

Each pull request gets one comment from the bot, which follows it. It says
which gates failed and why, at the file and line, in words from
`.github/gates.json` and `.github/bot/`, with the command that reproduces each
one and the one that fixes it where a fix is mechanical. A mechanical fix
inside the diff is also posted as suggested changes, which you can commit from
the pull request. The comment also holds a checklist, the line the changelog
will take from the title, and, where the title or the `Spec:` line is
missing, a suggestion for it.

### Commands in comments

A comment whose first line is one of these runs it:

| Command         | Does                                                                                                                                                                                                               | Who                                                     |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------- |
| `/update`       | GitHub merges the base branch into the pull request's branch, and signs the merge commit, which the squash leaves out of `main`                                                                                    | someone with write access, or the pull request's author |
| `/rebase`       | the same as `/update`, because GitHub cannot sign rebased commits                                                                                                                                                  | someone with write access, or the pull request's author |
| `/retest`       | re-runs the failed jobs of the latest CI run on the pull request's head, at most three times per head                                                                                                              | someone with write access, or the pull request's author |
| `/fix`          | commits the fix the pull request's own latest CI run computed (Pint, Rector, `composer normalize`, markdownlint) to its branch, where the branch is in this repository and neither its base nor a protected branch | someone with write access, or the pull request's author |
| `/docs <words>` | replies with at most three links to headings under `.docs` that match the words                                                                                                                                    | anyone, on an issue or a pull request                   |

## Finding your way

- **[ARCHITECTURE.md](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/ARCHITECTURE.md) is the contract.** It lays out the
  layers, `Core`, `Attribute`, `Port`, `Mutator`, `Config`, `Extension`,
  `Adapter` and `Cli`, each naming only itself and those before it. Every rule
  in it names what enforces it, and `tests/Guards` plants a violation of each
  one.
- **[The decisions](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/decisions/README.md) are the design.** Each ADR
  gives a decision, the alternatives and why they lost. A commit or pull
  request that implements one names it in `Spec:`.
- **The tests are laid out by kind.** `tests/Unit` mirrors `src`, and so
  does `tests/Process`, which holds the tests that start real processes.
  `tests/Contract` holds one suite per port, which runs against the port's
  fake in `tests/Fakes` and every adapter. `tests/Arch` holds the rules the
  suite enforces, and `tests/Docs` holds the documentation to the code. The
  gate judges its own mutants by `Unit` and `Plugins`. `Process` and
  `Contract` judge only the units they hold.
- **CI, the docs and releases** each have a page of their own:
  [how CI works](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/contributing/ci.md), [the docs](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/contributing/docs.md)
  and [releasing](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/contributing/releasing.md).
- **The reusable workflow is generated.** Edit its template,
  `scripts/workflows/mutation-gate/`, and run
  `python3 scripts/workflows/generate.py`, which writes
  `.github/workflows/mutation-gate.yml` with every download retried once
  (ADR-0072). The `scripts` job fails where the two differ. Every file
  `scripts/generated.py` lists is written by its own recipe in the same way.

## Adding an adapter, a preset or an extension

- **An adapter** implements one of the interfaces in `src/Port`, in a
  directory of its own under `src/Adapter`, and names no other adapter.
  `src/Cli/FirstParty.php` registers it under the name a config chooses it
  by. Its port's suite in `tests/Contract` is run against it, and its unit
  tests mirror it under `tests/Unit/Adapter`.
- **A preset** is a layer of config. The presets this package ships are in
  `src/Cli/Config/Presets.php`, named by `Core\Config\BuiltinPreset`. An
  extension registers its own with `Extensions::withPreset()`.
- **An extension** is a class implementing
  `NightWorksIO\MutationGate\Extension\Extension`, constructed with no
  arguments and named in its package's `composer.json` under
  `extra.mutation-gate.extensions`. Its `extend()` answers the registry with
  its adapters, presets and mutator sets added. A first-party mutator set is
  a plugin, a package of its own under `plugins/<name>`, as `plugins/default`
  is ([ADR-0021](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/decisions/0021-mutators-are-written-once-and-first-party-sets-can-leave.md)).

[Extending the gate](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/extending/README.md) lists every extension point
with its contract, and [adapters](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/extending/adapters.md) says how a
config names an adapter, and how an adapter reads its options.

## Maintainers and decisions

- **There is one maintainer,** as [`.github/CODEOWNERS`](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.github/CODEOWNERS)
  says.
- **Design is decided by ADR.** Each decision is recorded in
  `.docs/decisions`. An accepted decision changes only through a new ADR that
  supersedes it, linked both ways, as
  [the decisions' rules](https://github.com/nightworksio/php-mutation-gate/blob/HEAD/.docs/decisions/README.md) say.
- **A proposal becomes an ADR by pull request.** It adds a decision record
  under the next unused number, written as the others are. The docs site
  builds the index from it, and
  `python3 .github/scripts/decisions_index.py validate` says whether it reads
  as one. The maintainer accepts it by merging it.
