Metadata-Version: 2.5
Name: whatships
Version: 0.1.0
Summary: What ships with this promotion: the pull requests, database migrations, infrastructure changes and new settings between staging and production.
Project-URL: Homepage, https://github.com/GebleaAlex/whatships
Project-URL: Documentation, https://github.com/GebleaAlex/whatships#readme
Project-URL: Issues, https://github.com/GebleaAlex/whatships/issues
Project-URL: Changelog, https://github.com/GebleaAlex/whatships/blob/main/CHANGELOG.md
Author: Alex Geblea
License-Expression: MIT
License-File: LICENSE
Keywords: changelog,deployment,devops,github-actions,migrations,promotion,release,terraform
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Version Control :: Git
Classifier: Topic :: System :: Software Distribution
Classifier: Typing :: Typed
Requires-Python: >=3.11
Provides-Extra: dev
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# whatships

[![CI](https://github.com/GebleaAlex/whatships/actions/workflows/ci.yml/badge.svg)](https://github.com/GebleaAlex/whatships/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/GebleaAlex/whatships/blob/main/LICENSE)
![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-blue.svg)

**What ships with this promotion.** Before `stage` is merged into `main`, whatships reads the
commits between them and writes down what is about to reach production: the pull requests, the
database migrations and what each one does, the infrastructure changes, and the settings the new
code expects. It runs in a second, needs nothing but git, and posts the answer on the pull request.

```console
$ whatships --base main --head stage
What ships: stage -> main
3 pull requests, 1 commit without one, 2 migrations, 6 infrastructure changes, 2 new settings

Needs a look before promoting (2)
  !! apps/orders/migrations/0002_drop_legacy_ref.py: Drops the column of `order.legacy_ref` with its data. Code still running on production from before this promotion fails once the migration has run.
  !! aws_s3_bucket.invoices: Looks renamed to `aws_s3_bucket.invoice_archive` without a moved block: Terraform destroys this one and creates the other. A `moved` block from `aws_s3_bucket.invoices` to `aws_s3_bucket.invoice_archive` keeps it. It holds data.
 ? `main` has 1 commit that `stage` does not (a hotfix, most likely). What was tested on `stage` did not include it: merge `main` back first.
 ? 1 commit reached `stage` without a pull request.
...
```

The same report as the comment it leaves on the pull request
([all of it](https://github.com/GebleaAlex/whatships/blob/main/docs/example-report.md)):

![The report as a pull request comment: a warning with two points to look at, the pull requests, and the migrations with what each operation does](https://raw.githubusercontent.com/GebleaAlex/whatships/main/docs/report.png)

## Why

A team that promotes to production often stops reading what it promotes. The pull request from
`stage` to `main` says "47 commits, 212 files changed", and the questions that matter are somewhere
in there:

* **Which changes are these?** Not the commits: the pull requests, with their authors and tickets,
  including the ones that came in through `develop` three merges ago.
* **Does a migration drop or rename something the running code still uses?** Does one build an index
  on the big table, or run a data fix whose duration nobody measured?
* **Does the infrastructure change, and does anything get destroyed?**
* **Does the new code need a setting that production does not have yet?**
* **Did anything skip review?** Is production ahead of what was tested?

Each of these is in the diff for whoever goes looking. whatships goes looking every time.

## Install and run

```bash
pipx run whatships --base main --head stage        # or: uvx whatships ...
pip install whatships                              # to keep it
```

It has no dependencies and reads the repository through the `git` command: nothing is checked out,
fetched or written. Inside a clone:

```bash
whatships                                  # the current branch against production
whatships --base main --head stage         # both named
whatships --last                           # what the last promotion merged into production shipped
whatships --merge 4f2a91c                  # what one merge commit brought in
whatships --base v1.4.0 --head v1.5.0      # between two tags
whatships --format markdown -o report.md   # also: json, slack, text
whatships --plan plan.json                 # with what `terraform show -json` says an apply will do
whatships --plan envs/prod=prod.json       # one plan per stack, each for its own directory
whatships --fail-on attention              # exit 1 when something needs a look
```

## On every pull request into production

```yaml
# .github/workflows/whatships.yml
name: What ships
on:
  pull_request:
    branches: [main]

permissions:
  contents: read
  pull-requests: write          # for the comment

jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0        # the report is made from history: a shallow clone has none
      - uses: GebleaAlex/whatships@v0
```

The action writes one comment and keeps it up to date as the pull request changes, puts the same
report in the job summary, and sets outputs (`level`, `pull-requests`, `migrations`, `attention`, …)
for later steps. Inputs: `base`, `head`, `comment`, `last`, `plan`, `fail-on`, `title`, `config`,
`token`, `working-directory` ([action.yml](https://github.com/GebleaAlex/whatships/blob/main/action.yml)).
On a pull request from a fork, where the token may not write, the report goes to the job summary
and the job still passes.

On any other CI, run the command and do what you like with the output; `--format json` has
everything, and `--format slack` is a ready message for a webhook.

## What it reads

### Pull requests

From the commits alone: GitHub merge and squash commits, GitLab merge requests, Bitbucket and Azure
DevOps merges. No API is needed.

* A merge **between long-lived branches** (`develop` into `stage`) is a promotion, not a change. It
  is skipped, and the pull requests it carries are listed. The whole branch name counts: `fix/test`
  is a pull request like any other, and so is `main` from somebody's fork.
* A commit belongs to the **innermost** pull request that brought it; a pull request merged into
  another one is shown inside it.
* Commits that reached the branch **without any pull request** are listed apart.
* A pull request **reverted** inside the same promotion is struck through.
* **Tickets** named in branches and titles (`ABC-123`) are linked when you say where they live.
* When production already has some of the work under another commit (a promotion that was
  **squashed**, a cherry-pick), only what still differs between the two branches is reported, and
  the pull requests that bring nothing new are named.

With a token (`GITHUB_TOKEN`), GitHub is asked for what git does not record: the title as it is
now, the author's login, labels, and which pull request a rebased commit came from. What GitHub
says about a number is used only when it was merged as the very commit that was read: `#12` in a
commit message can be the number of a pull request somewhere else.

### Database migrations

| | Read how | Understood |
| --- | --- | --- |
| **Django** | the syntax tree; never imported or run | every built-in operation, `RunSQL`, `RunPython`, `SeparateDatabaseAndState` |
| **Alembic** | the syntax tree of `upgrade()` and of the functions it calls | `op.*` under any name, batch operations, SQL passed to `execute` as text |
| **SQL files** | Flyway, dbmate, goose, golang-migrate, Prisma, plain directories | statement by statement |
| Rails, Knex, Sequelize, TypeORM | by the names of the calls | drops, renames, type changes, indexes, raw SQL |

Three levels, and only the first goes to the top of the report:

* **Attention**: data goes away or running code breaks. A dropped table or column, a rename, a
  `NOT NULL` column without a default, a `DELETE` without `WHERE`; a migration **edited after it was
  applied**, renamed, or left in **conflict** with another (two migrations ending the same app).
* **Check**: fine or not depending on something only you know, usually the size of a table. An
  index or constraint built without `CONCURRENTLY`, a type change, `SET NOT NULL`, a data migration.
* **Note**: for the record. No reverse, runs outside a transaction, a squash.

What could not be read is said, not passed over: a migration this Python cannot parse, one that
is not text or is larger than 2 MB, a list of operations put together at run time, SQL built in
code, a `DO` block, a stored procedure, a helper imported from another module. Each is a "check"
that says to open the file. What a procedure, function or trigger *defines* is not what the
migration runs: statements inside such a body are not reported as if they ran.

Two things keep the list honest. For Django, the earlier migrations of the app are replayed, so
`AlterField` is reported for what it changes: *"choices (no change to the table)"*, or
*"max_length 100 → 30: fails if a longer value exists"*. And a table **created by the same
promotion** is empty and unknown to production, so nothing done to it is flagged; "created" is
judged against what production has, so a squashed migration that creates a table again does not
make it new.

### Infrastructure

Terraform and OpenTofu (`.tf` and `.tofu` files), in two ways.

**From the files**, always: which resources, modules, variables, outputs, providers and backend
settings were added, changed or removed, attribute by attribute (`instance_class: "db.t3.medium" →
"db.r6g.large"`, `tags.Environment`, `lifecycle.prevent_destroy`). No state, credentials or
`terraform` binary. It flags what the files alone can show:

* a resource **gone from the configuration** (it will be destroyed), louder when it holds data;
  the same for a `removed` block, unless it says `destroy = false`, and for `count = 0`;
* a resource **renamed without a `moved` block**: same type, same content, new name, which
  Terraform carries out as destroy and create. `moved` blocks are followed, through chains and
  into modules;
* `prevent_destroy` removed, `count` / `for_each` changed, a module or provider version moved, the
  state's backend moved, a variable that now needs a value in every environment;
* a file it **could not read**: nothing in that file is compared, and the report says so.

What `terraform fmt` does to a file is not a change: spacing, the order of nothing, `:` for `=`.

**From a plan**, when you give one (`--plan plan.json`, from `terraform show -json`): what an apply
will actually do, including what the files cannot know, such as a changed attribute that forces a
replacement, and drift. A plan stands in for the files of the stack it was made in and of no
other: with several stacks, give one plan each, and a stack without one keeps the reading of its
files. A plan finds its directory by what it declares (the directory whose resources and module
calls are the plan's); when two stacks declare the same, say which it is for with
`--plan DIR=plan.json`. Even there the files give way only where the plan accounts for them: a
plan that does not destroy a resource the files remove was made from other files (make it from
the commit being promoted), and the report keeps the files' reading and says the two disagree.

Secrets stay out: values in `.tfvars` are never shown (only that a variable changed); anything
marked `sensitive`, named like a secret anywhere in its path or inside its value (a `password` key
in `jsonencode({...})`), held by a resource that is a secret (`azurerm_key_vault_secret`,
`kubernetes_secret`), or shaped like a credential (a URL with a password in it, a private key, a
cloud access key) is hidden; and a plan's sensitive marks are respected.

### Settings

Environment variables the code **now reads and did not before**, in Python, JavaScript and
TypeScript, Go, Ruby, Rust, PHP, Java, Kotlin and C#, with whether the code has a fallback.
A variable the program sets or clears itself is not one somebody has to provide.
It is a search for the usual ways of reading the environment, not an understanding of the program:
a list to check. A name counts as new only if nothing in the previous tree mentions it, and test
files are left out.

### Also changed

Dependency manifests and lock files, pipelines, Dockerfiles and deployment manifests are named
when they change. Add your own groups.

## Configuration

Nothing is required. `.whatships.toml`, or `[tool.whatships]` in `pyproject.toml`, is for what
cannot be guessed:

```toml
base = "main"                       # what production runs
head = "stage"                      # what gets promoted
ticket_url = "https://example.atlassian.net/browse/{ticket}"
ignore = ["vendor/**", "**/*.snap"]

[areas]                             # names for parts of the code, instead of directory names
Billing = ["apps/billing/**", "apps/invoices/**"]

[terraform]
paths = ["infra/prod"]              # only what this promotion applies

[watch]
"Feature flags" = ["config/flags/*.yaml"]
```

Every key, with its default: [docs/configuration.md](https://github.com/GebleaAlex/whatships/blob/main/docs/configuration.md).

## What it does not do

* **It does not connect to your database or your cloud.** It cannot know that a table has two
  hundred million rows, which is why index builds and type changes are "check" and not a verdict.
* **The Terraform reading from files is a reading of files.** It does not evaluate expressions,
  follow modules into their source, or know the state. For what an apply will do, give it a plan.
* **Rebase merges leave no trace in git.** Without a token, commits merged by rebase are listed as
  arriving without a pull request. With one, GitHub is asked.
* **Settings are found by pattern**, so a variable read through a wrapper of your own is missed.
* **Migrations in Rails and JavaScript are read by name**, not parsed.
* **A very long range is read from its newest end.** Past 20,000 commits the report says so; a
  promotion is far shorter.
* **One repository at a time.** A promotion that spans several repositories is several reports.
* **The comment is for GitHub.** Elsewhere the report is yours to post: Markdown, JSON or Slack.

## Development

```bash
python -m venv .venv && . .venv/bin/activate      # .venv\Scripts\activate on Windows
pip install -e ".[dev]"
pytest && ruff check . && ruff format --check . && mypy
python examples/demo.py                           # a repository with a promotion in it, and its report
```

The tests build real repositories with real merges; nothing about git is mocked. See
[CONTRIBUTING.md](https://github.com/GebleaAlex/whatships/blob/main/CONTRIBUTING.md). Licensed under
the [MIT License](https://github.com/GebleaAlex/whatships/blob/main/LICENSE).
