Merge branch 'main' into fix/etcd-alert-scope-20261102

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-16 13:28:42 +03:00
commit ca0fcb1c05
757 files changed
+69007 -12741

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@ Similarly, never propose edits to:
Each commit must follow [Conventional Commits](https://www.conventionalcommits.org/) format: `type(scope): brief description`.
Valid types: `feat`, `fix`, `docs`, `style`, `refactor`, `perf`, `test`, `build`, `ci`, `chore`.
Valid types: `feat`, `fix`, `security` (a fix that comes through a security advisory), `docs`, `style`, `refactor`, `perf`, `test`, `build`, `ci`, `chore`.
Valid scopes:
+3 -2
View File
@@ -1,6 +1,6 @@
<!-- Thank you for making a contribution! Here are some tips for you:
- Use Conventional Commits for the PR title: `type(scope): description`
- Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore
- Types: feat, fix, security (a fix that comes through a security advisory), docs, style, refactor, perf, test, build, ci, chore
- Scopes are not an exhaustive list — pick the most specific scope for the change and extend the list when a genuinely new area appears. Examples:
- System components: dashboard, platform, operator, cilium, kube-ovn, linstor, fluxcd, cluster-api
- Managed apps: postgres, mariadb, redis, kafka, clickhouse, virtual-machine, kubernetes
@@ -8,11 +8,12 @@
- Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or add a `BREAKING CHANGE:` footer
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats, even if it's still a draft.
- Add the label `kind/backport` if it's a bugfix that needs to be backported to a previous version.
- Fixes merged to `main` reach the release lines the backport batch runs for automatically, so do not open backport PRs by hand: https://github.com/cozystack/cozystack/blob/main/docs/release/backports.md
-->
## What this PR does
<!-- Link the issues this PR addresses. For a complete fix targeting the default branch, use Closes #N (Closes owner/repository#N for another repository). Otherwise use a non-closing reference; release-branch PRs need manual issue linking and closure when appropriate. -->
### Screenshots
+15 -12
View File
@@ -22,6 +22,7 @@
# do-not-merge/ PR merge blockers
# security/ security-finding severity and status (Cozystack-specific)
# size/ PR size (auto-applied)
# backport/ maintainer overrides of the backport batch rules (Cozystack-specific)
#
# `aliases:` lets EndBug/label-sync rename existing labels without losing
# references on already-tagged issues and PRs.
@@ -78,18 +79,6 @@
color: 'e11d21'
description: Indicates the change introduces a breaking API or behaviour change
# kind/backport-previous has no upstream Kubernetes equivalent (k8s has no
# two-release-line model); it stays under kind/ per the file convention.
- name: kind/backport
color: 'fbca04'
description: Categorizes issue or PR as requiring a backport to the current release line
aliases: ['backport']
- name: kind/backport-previous
color: 'fbd876'
description: Categorizes issue or PR as requiring a backport to the previous release line
aliases: ['backport-previous']
# ──────────────────────────────────────────────
# priority/ — urgency
# ──────────────────────────────────────────────
@@ -411,3 +400,17 @@
- name: security/fixed
color: '0e8a16'
description: Fix released
# ──────────────────────────────────────────────
# backport/ — maintainer overrides of the backport batch rules
# ──────────────────────────────────────────────
# Read by cmd/backport-batch from the merged pull request on main;
# docs/release/backports.md describes both.
- name: backport/include
color: 'c2e0c6'
description: The backport batch takes this PR though rules 2-4 keep it out; it must still pick and pass CI
- name: backport/skip
color: 'f9d0c4'
description: The backport batch keeps this PR out although the rules take it
+107
View File
@@ -0,0 +1,107 @@
name: Backport batch
# Builds the backport batch for each line `backport-batch lines` names, the
# newest released lines and any line in its rc window, newest line first, as
# docs/release/backports.md describes. The steps live in
# hack/backport-batch-scheduled.sh, which a maintainer can also run by hand.
#
# The batch runs as the cozystack-ci App, with a token minted for this
# repository alone with Contents and Pull requests write. It has no
# workflows permission, so an original that changes .github/workflows/
# waits on the batch for a maintainer (--no-workflow-files). Its pushes,
# unlike GITHUB_TOKEN's, start CI on the batch pull request.
#
# Repository settings it reads; none is created here:
# secret COZYSTACK_CI_APP_ID, COZYSTACK_CI_PRIVATE_KEY
# the cozystack-ci App, shared with the
# release workflows. Required.
# variable BACKPORT_ALERT_TEAM the team or person the batch's comment
# mentions, org/team or a login.
#
# A batch open since the previous run, deferred conflicts and originals that
# change workflows wait for a maintainer: the batch's comment lists them. A
# failed batch CI waits too, and shows on the batch pull request's checks.
on:
schedule:
# Monday and Thursday: each run leaves working days to cut an rc, run its
# e2e and promote it before the weekend. 06:23 UTC is after the nightly
# and the daily sweeps.
- cron: '23 6 * * 1,4'
workflow_dispatch: {}
permissions: {}
concurrency:
group: backport-batch
cancel-in-progress: false
jobs:
batch:
name: Backport batch
if: github.repository == 'cozystack/cozystack'
runs-on: ubuntu-latest
# The App token lives an hour from its mint, a few minutes in; a run takes
# minutes, so the job ends well before the token does.
timeout-minutes: 30
permissions:
contents: read
env:
# The pipe falls back to direct on any proxy error (hack/common-envs.mk).
GOPROXY: "https://proxy.golang.org|direct"
steps:
- name: Require the cozystack-ci App
env:
APP_ID: ${{ secrets.COZYSTACK_CI_APP_ID }}
APP_KEY: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
run: |
if [ -z "$APP_ID" ] || [ -z "$APP_KEY" ]; then
echo "::error::COZYSTACK_CI_APP_ID or COZYSTACK_CI_PRIVATE_KEY is not set. The batch pushes and opens pull requests as the cozystack-ci App only; see docs/release/backports.md#the-scheduled-run."
exit 1
fi
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
fetch-depth: 0
persist-credentials: false
- name: Set up Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version-file: go.mod
cache: false
- name: Build backport-batch
run: go build -o "$RUNNER_TEMP/backport-batch" ./cmd/backport-batch
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
with:
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
owner: cozystack
repositories: cozystack
permission-contents: write
permission-pull-requests: write
# git pushes through gh, which reads the token from the environment of
# each call, so the token is never written into the checkout.
- name: Use the App's bot as git identity and credential
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
run: |
gh auth setup-git
id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)
git config --global user.name "${APP_SLUG}[bot]"
git config --global user.email "${id}+${APP_SLUG}[bot]@users.noreply.github.com"
- name: Run the batch
env:
BACKPORT_BATCH: ${{ runner.temp }}/backport-batch
BACKPORT_BATCH_TOKEN: ${{ steps.app-token.outputs.token }}
BACKPORT_BATCH_BOT: ${{ steps.app-token.outputs.app-slug }}[bot]
BACKPORT_ALERT_TEAM: ${{ vars.BACKPORT_ALERT_TEAM }}
run: hack/backport-batch-scheduled.sh --no-workflow-files
-305
View File
@@ -1,305 +0,0 @@
name: Automatic Backport
on:
pull_request_target:
types: [closed, labeled] # fires when PR is closed (merged) or labeled
# Two mechanisms here, answering two different failures.
#
# A run joins its concurrency group before any job-level `if` is evaluated, so
# `prepare`'s guard below cannot stop a label event from disturbing the run
# already backporting this PR; it only decides what the label run does after the
# damage. Labels set with the default GITHUB_TOKEN start no run, but labels set
# by third-party GitHub Apps do, and they arrive in bursts. Under a plain
# `cancel-in-progress: true` each one cancelled the backport the merge had just
# started, and nothing reports that beyond a cancelled run sitting next to a
# green one.
#
# The delivery survived anyway, because the old guard admitted a run on the PR's
# cumulative labels: the last label of a burst requalified and redid the work.
# `prepare`'s narrowing below removes that accidental safety net, so the two
# changes land together — narrowing alone would turn a noisy delivery into an
# absent one.
#
# So the group key moves a label event that is NOT a backport request aside, and
# `cancel-in-progress` stops one that IS from cancelling rather than queuing.
# Both are load-bearing. `backport` and `backport-previous` are separate requests
# that both have to complete, so they share the main group and must not cancel
# each other; every other label does no work here and only has to stay out of
# the way.
#
# That is also why this file does not simply copy pull-requests.yaml, which
# splits the key and leaves cancellation unconditional: a discarded label run
# there publishes nothing, so isolating it is the whole fix.
#
# Queuing protects a pending run from the run ahead of it and never from the one
# behind: the group holds a single pending run, and a newer one evicts it. With
# the split in place that costs nothing, and for the same reason the fan-out
# below is harmless — the only events left in this group are backport requests,
# the evictor is by definition the newer of the two, and `prepare`'s `Check which
# labels are present` step reads the cumulative label set. So the survivor covers
# a superset of what the evicted run would have delivered, for any sequence that
# only adds labels. Removing one in between is the exception and needs no cover:
# the evictor then carries less because the request was withdrawn.
#
# That is exactly what the split buys. Without it the evictor could be any label
# at all, and a run that does not qualify delivers nothing in place of the
# request it displaced.
#
# `queue: max` raises the single pending slot to a hundred and is the obvious
# thing to reach for instead of the split. Taking it means giving up the
# conditional cancel, because `queue: max` and `cancel-in-progress: true` are a
# validation error together and this key still evaluates to true on a merge.
# The trade is available and was not taken: eviction is what keeps a burst to one
# surviving run rather than a hundred queued ones, and the feature only shipped
# 2026-05-07, on a path that delivers fixes to release lines.
concurrency:
group: backport-${{ github.workflow }}-${{ github.event.pull_request.number }}${{ (github.event.action == 'labeled' && github.event.label.name != 'backport' && github.event.label.name != 'backport-previous' && github.event.label.name != 'kind/backport' && github.event.label.name != 'kind/backport-previous') && '-label' || '' }}
# Stated positively, matching the group key above. `!= 'labeled'` is equivalent
# only while `types:` holds exactly these two, and that form fails open: adding
# `unlabeled` or `reopened` there would hand those events cancellation in the
# main group, which is the behaviour this file exists to remove.
#
# The positive form removes the cancelling, not the occupying. A third type
# still lands in the main group, still takes its single pending slot, and its
# `prepare` still skips — so it can evict a genuine backport request and
# deliver nothing in its place. `types:` is load-bearing for that reason and is
# pinned in hack/release-freeze-contract.bats; widening it needs the group key
# widened with it.
cancel-in-progress: ${{ github.event.action == 'closed' }}
# Top-level token defaults to read-only. A job-level block replaces this one
# rather than adding to it, so each job restates every scope it needs.
permissions:
contents: read
jobs:
# Determine which backports are needed
prepare:
# This guard admits each action on the labels it is entitled to. `closed`
# carries no label of its own, so it reads the set the PR ended up with.
# `labeled` reads only the label just added: matching the cumulative set
# here means every later unrelated label on a merged PR that still carries
# `backport` re-enters this job to redo a backport already delivered.
#
# Scope, so this is not read as covering the whole job: the `Check which
# labels are present` step below still reads the cumulative set, so a
# `backport-previous` event on a PR also carrying `backport` fans out to
# both matrix legs. That is deliberate, and each state a re-entered leg can
# find is already accounted for: delivered is skipped by the merged-backport
# guard, still open is skipped by backport-action on the 422, and closed
# unmerged gets a fresh backport PR, which is the retry that guard
# deliberately preserves rather than an accident. What the narrowing here
# decides is whether the job RUNS, not which targets it picks.
if: |
github.event.pull_request.merged == true &&
github.event.pull_request.base.ref == 'main' &&
(
(github.event.action == 'closed' && (contains(github.event.pull_request.labels.*.name, 'backport') || contains(github.event.pull_request.labels.*.name, 'backport-previous') || contains(github.event.pull_request.labels.*.name, 'kind/backport') || contains(github.event.pull_request.labels.*.name, 'kind/backport-previous'))) ||
(github.event.action == 'labeled' && (github.event.label.name == 'backport' || github.event.label.name == 'backport-previous' || github.event.label.name == 'kind/backport' || github.event.label.name == 'kind/backport-previous'))
)
# GitHub API calls only — no docker, no repo toolchain: GitHub-hosted.
runs-on: ubuntu-latest
# Queuing gives a stuck run a way to hurt the next one that a cancelling key
# did not: the label event that used to kill it now waits behind it rather
# than clearing it.
#
# This ceiling bounds a stuck JOB, which is the case it can reach. A run can
# also wedge at the RUN level with every job already finished, and no
# job-level key expires that one; it holds the group until GitHub retires the
# run. Rare but real: run 31232645133 has been `in_progress` since
# 2026-08-08 with `prepare` and `backport` both skipped. Nothing here
# reaches that state, so docs/release.md tells an operator to look for a run
# still in flight and clear it before re-applying the label.
#
# What it bounds is EXECUTION, not the wait for a runner. GitHub's limits
# page says "job execution time" for the 6-hour job cap while spelling out
# that the 35-day run cap "includes execution duration, and time spent on
# waiting and approval" — the contrast is deliberate — and
# orgs/community#50926 shows a job sitting six hours on "Waiting for a runner
# to pick up this job" under `timeout-minutes: 5`. So queue time is a third
# unbounded case alongside the run-level wedge, not something this covers.
#
# 30 is therefore generous rather than calculated. Execution on run
# 29844315422 was 6s for `prepare` and 2s and 16s for the two matrix legs,
# against 9m24s, 5m28s and 5m45s of queue for the same three jobs. Every
# execution measured on this workflow has been seconds, and no maximum is
# quoted on purpose: an absolute maximum over a growing set is stale as soon
# as a slower run lands. The value leaves room for a cherry-pick far larger
# than anything seen while replacing the 6-hour default, and nothing here is
# sized against the queue because nothing here can be.
timeout-minutes: 30
permissions:
contents: read
outputs:
backport_current: ${{ steps.labels.outputs.backport }}
backport_previous: ${{ steps.labels.outputs.backport_previous }}
current_branch: ${{ steps.branches.outputs.current_branch }}
previous_branch: ${{ steps.branches.outputs.previous_branch }}
steps:
- name: Check which labels are present
id: labels
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const pr = context.payload.pull_request;
const labels = pr.labels.map(l => l.name);
// TRANSITIONAL: both spellings are accepted so the rename can land
// in any order relative to the org-level dosubot, which still
// applies the legacy names and is today the dominant applier on
// real backports. Once dosubot's PR labelling is switched off and
// the in-repo labeler is the only applier, the legacy arms here,
// in the `if:` gate above and in the concurrency group can all be
// deleted in one pass.
const isBackport = labels.includes('kind/backport') || labels.includes('backport');
const isBackportPrevious = labels.includes('kind/backport-previous') || labels.includes('backport-previous');
core.setOutput('backport', isBackport ? 'true' : 'false');
core.setOutput('backport_previous', isBackportPrevious ? 'true' : 'false');
console.log(`backport label: ${isBackport}, backport-previous label: ${isBackportPrevious}`);
- name: Determine target branches
id: branches
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
// Enumerate the real release-X.Y branches rather than deriving them
// from the latest published release.
//
// Since cutting an rc freezes the line (cut-prerelease.yaml),
// release-X.Y is created when the first vX.Y.0-rc.N is cut — before
// vX.Y.0 is ever published. getLatestRelease returns the newest
// published STABLE, so throughout the freeze window it still named
// the previous line: a `backport` on a fix for the release being
// stabilised would have landed on release-X.(Y-1) and missed the
// release it was written for. That window is exactly when backports
// matter most, since the frozen branch is the only way in.
//
// Enumerating branches also drops the contiguous-minor assumption:
// `previous` used to be computed as min-1, which named a
// non-existent branch whenever a minor was skipped.
const branches = await github.paginate(github.rest.repos.listBranches, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
const lines = branches
.map(b => /^release-(\d+)\.(\d+)$/.exec(b.name))
.filter(Boolean)
.map(m => ({ name: m[0], maj: Number(m[1]), min: Number(m[2]) }))
.sort((a, b) => (b.maj - a.maj) || (b.min - a.min));
if (lines.length === 0) {
core.setFailed('No release-X.Y branches found; cannot determine backport target.');
return;
}
const currentBranch = lines[0].name;
const previousBranch = lines.length > 1 ? lines[1].name : '';
core.setOutput('current_branch', currentBranch);
core.setOutput('previous_branch', previousBranch);
console.log(`Release lines found: ${lines.map(l => l.name).join(', ')}`);
console.log(`Current branch: ${currentBranch}, Previous branch: ${previousBranch || 'N/A'}`);
// Both targets come from the enumeration above, so they exist by
// construction — no getBranch probe needed. What CAN still fail is
// asking for a previous line when only one exists.
if (!previousBranch && '${{ steps.labels.outputs.backport_previous }}' === 'true') {
core.setFailed('backport-previous requested, but only one release line exists.');
return;
}
backport:
needs: prepare
if: |
github.event.pull_request.merged == true &&
github.event.pull_request.base.ref == 'main' &&
(needs.prepare.outputs.backport_current == 'true' || needs.prepare.outputs.backport_previous == 'true')
# git cherry-picks via backport-action — no docker, no repo toolchain: GitHub-hosted.
runs-on: ubuntu-latest
# See `prepare` above: a queued request waits on this one, so it needs a
# ceiling rather than the 6-hour default. Bounds this job's execution only;
# the wait for a runner is outside it either way.
timeout-minutes: 30
permissions:
contents: write
pull-requests: write
strategy:
matrix:
backport_type: [current, previous]
steps:
# 1. Determine target branch based on matrix
- name: Set target branch
id: target
if: |
(matrix.backport_type == 'current' && needs.prepare.outputs.backport_current == 'true') ||
(matrix.backport_type == 'previous' && needs.prepare.outputs.backport_previous == 'true')
run: |
if [ "${{ matrix.backport_type }}" == "current" ]; then
echo "branch=${{ needs.prepare.outputs.current_branch }}" >> $GITHUB_OUTPUT
echo "Target branch: ${{ needs.prepare.outputs.current_branch }}"
else
echo "branch=${{ needs.prepare.outputs.previous_branch }}" >> $GITHUB_OUTPUT
echo "Target branch: ${{ needs.prepare.outputs.previous_branch }}"
fi
# 2. Skip if this target already has a merged backport.
#
# The action's backport branch name is deterministic,
# backport-<pull_number>-to-<target_branch>. A merged PR with that head
# means an earlier run already delivered this change to the target — a
# second label event (e.g. backport-previous added after backport
# already merged) would re-enter this job and cherry-pick commits the
# target branch already has. Git reports that as an empty pick (exit 1)
# rather than a conflict, which draft_commit_conflicts below misreads
# as a real conflict and reports as a failed backport on the original
# PR. Skipping here avoids the false report; it does not touch the
# empty-pick behavior itself, which lives in the action.
- name: Check for existing merged backport
id: guard
if: steps.target.outcome == 'success'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const targetBranch = '${{ steps.target.outputs.branch }}';
const head = `backport-${context.payload.pull_request.number}-to-${targetBranch}`;
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
head: `${context.repo.owner}:${head}`,
});
const alreadyMerged = prs.some(p => p.merged_at !== null);
core.setOutput('already_merged', alreadyMerged ? 'true' : 'false');
if (alreadyMerged) {
console.log(`Skipping backport to ${targetBranch}: ${head} already merged.`);
}
# 3. Checkout (required by backport‑action)
- name: Checkout repository
if: steps.target.outcome == 'success' && steps.guard.outputs.already_merged != 'true'
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
# 4. Create the back‑port pull request
- name: Create back‑port PR
id: backport
if: steps.target.outcome == 'success' && steps.guard.outputs.already_merged != 'true'
uses: korthout/backport-action@0193454f0c5947491d348f33a275c119f30eb736 # v3.2.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
label_pattern: '' # don't read labels for targets
target_branches: ${{ steps.target.outputs.branch }}
merge_commits: skip
# conflict_resolution lives inside the experimental JSON input, not as a
# top-level key. Passed top-level it is silently ignored and the action
# falls back to the experimental default ("fail"), which on a conflict
# creates no PR and only comments. Nesting it here makes a conflicting
# backport open a draft PR with the conflict committed for manual fixup.
experimental: |
{
"conflict_resolution": "draft_commit_conflicts"
}
+8 -8
View File
@@ -154,6 +154,7 @@ jobs:
sparse-checkout: |
hack
docs/agents
docs/release
persist-credentials: false
# The agent reads docs/agents/changelog.md from the WORKING TREE, which is
@@ -163,18 +164,17 @@ jobs:
# detached rc-tag checkout is exactly that, so a COMPLIANT agent would
# silently decline to generate on every promotion of an existing rc.
# Overlay the current instructions so the agent reads today's contract.
# changelog.md sends the agent to the checks in docs/release.md and to
# hack/validate-changelog.sh, so those are overlaid too: the rc tag's
# copies predate any check added since. docs/release.md reaches
# .release-tooling only because the sparse checkout above is in cone
# mode (the action's default), which carries the files of every parent
# directory of docs/agents.
# changelog.md sends the agent to the checks in docs/release/changelogs.md
# and to hack/validate-changelog.sh, so those are overlaid too: the rc
# tag's copies predate any check added since, and an rc cut before the
# release docs were split into docs/release/ has no such directory.
- name: Refresh agent instructions
run: |
cp .release-tooling/docs/agents/changelog.md docs/agents/changelog.md
cp .release-tooling/docs/release.md docs/release.md
mkdir -p docs/release
cp .release-tooling/docs/release/changelogs.md docs/release/changelogs.md
cp .release-tooling/hack/validate-changelog.sh hack/validate-changelog.sh
echo "Refreshed docs/agents/changelog.md, docs/release.md and hack/validate-changelog.sh from ${GITHUB_REF_NAME}."
echo "Refreshed docs/agents/changelog.md, docs/release/changelogs.md and hack/validate-changelog.sh from ${GITHUB_REF_NAME}."
# Skip-priority + validate-then-fall-through: use a pre-existing changelog
# only if it is USABLE, so an invalid higher-priority source can never
+1 -1
View File
@@ -6,7 +6,7 @@ name: Cut Pre-release Tag
# This is the single entry point for creating a pre-release. Stable vX.Y.Z tags
# are never cut here (or by hand) — they are created write-once by
# pull-requests-release.yaml finalize at a promote PR's merge commit. See
# docs/release.md and #2677.
# docs/release/stable-tags.md and #2677.
#
# Dispatch from the branch the tag belongs to: `main` for a new minor's
# pre-release (vX.Y.0-rc.N), or `release-X.Y` for a patch line (vX.Y.Z-rc.N).
+15 -23
View File
@@ -61,7 +61,8 @@ jobs:
# base-repo PR file list — NOT from the presence/absence of an artifact the
# fork's own build controls. Fail-closed if the fork build failed or the
# pr-patch artifact is missing; green-and-stop only for a PR whose file list is
# entirely under docs/ (verified against the base-repo API).
# entirely under docs/ or the CI-only tools cmd/backport-batch/ and
# cmd/api-gate/ (verified against the base-repo API).
resolve:
name: Resolve fork PR
# Only fork PRs: head_repository differs from the base repository. Same-repo
@@ -266,15 +267,21 @@ jobs:
await decide('failure', 'PR file list hit the 3000-file API cap — cannot verify docs-only');
return;
}
// Paths with nothing to build and nothing for e2e to install: docs,
// and the CI-only Go tools no image builds. pull-requests.yaml's
// plan job answers the same question with the same prefixes, and
// hack/gate-concurrency-contract.bats holds the two lists equal.
//
// A rename reports the NEW path in `filename` and the old one in
// `previous_filename`. Checking only `filename` would read a
// packages/… -> docs/… rename as docs-only, when it in fact removes
// a code path. BOTH sides must be under docs/ to count as docs-only.
const isDocs = p => typeof p === 'string' && p.startsWith('docs/');
const hasNonDocs = files.some(f =>
!isDocs(f.filename) || (f.previous_filename && !isDocs(f.previous_filename)));
if (!hasNonDocs) {
await decide('success', 'docs-only PR (verified via PR file list) — e2e not required');
// a code path. BOTH sides must be under these prefixes to count.
const noRuntimePrefixes = ['docs/', 'cmd/backport-batch/', 'cmd/api-gate/'];
const isNoRuntime = p => typeof p === 'string' && noRuntimePrefixes.some(x => p.startsWith(x));
const hasRuntime = files.some(f =>
!isNoRuntime(f.filename) || (f.previous_filename && !isNoRuntime(f.previous_filename)));
if (!hasRuntime) {
await decide('success', 'docs or CI-only tools (verified via PR file list) — e2e not required');
return;
}
@@ -543,7 +550,7 @@ jobs:
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
# running into its node-join deadline, so a red there can end as a
# cancelled job. Held to one figure across every lane that runs the suite.
timeout-minutes: 215
timeout-minutes: 265
permissions:
contents: read
packages: read
@@ -771,21 +778,6 @@ jobs:
name: cozyreport
path: _out/cozyreport.tgz
if-no-files-found: warn
# Both halves, and in this order: the compose project owns the nodes and
# the zpools outlive it.
- name: Tear down sandbox
if: always()
run: |
# Annotated, not fatal: same reasoning as the in-tree lane. The runner
# is discarded after the job, so a dirty host has no next tenant, and
# failing here would post the required "E2E Tests" status as a failure
# indistinguishable from a real one.
cleanup_rc=0
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete-cluster-container || cleanup_rc=$?
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete || true
if [ "$cleanup_rc" -ne 0 ]; then
echo "::error title=sandbox teardown::teardown exited $cleanup_rc; the host may hold a stale compose project or zpool"
fi
# Flip the pending "E2E Tests" commit status posted by `resolve` to its
# terminal state. Runs on failure too, so a failed publish or e2e turns the
+2 -2
View File
@@ -137,7 +137,7 @@ jobs:
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
# running into its node-join deadline, so a red there can end as a
# cancelled job. Held to one figure across every lane that runs the suite.
timeout-minutes: 215
timeout-minutes: 265
permissions:
contents: read
checks: write # Breakpoint action updates its dedicated "Breakpoint Open" Check Run.
@@ -292,7 +292,7 @@ jobs:
# collect-report packages cozyreport plus per-failed-test crust-gather
# snapshots captured before Chainsaw cleanup.
#
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
# is the next step and the collector writes its tarball at the very end of
# its run, so a read that never returns spends what is left of the job
# budget, the job is cancelled, and the upload never runs -- the artifact
+2 -2
View File
@@ -250,7 +250,7 @@ jobs:
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
# running into its node-join deadline, so a red there can end as a
# cancelled job. Held to one figure across every lane that runs the suite.
timeout-minutes: 215
timeout-minutes: 265
permissions:
contents: read
packages: read
@@ -385,7 +385,7 @@ jobs:
path: _out/chainsaw-report.xml
if-no-files-found: ignore
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
# is the next step and the collector writes its tarball at the very end of
# its run, so a read that never returns spends what is left of the job
# budget, the job is cancelled, and the upload never runs -- the artifact
+81 -2
View File
@@ -24,7 +24,7 @@ jobs:
with:
script: |
// Conventional Commits types accepted by Cozystack (per docs/agents/contributing.md):
// feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
// feat, fix, security, docs, style, refactor, perf, test, build, ci, chore, revert
// Mapping below maps a subset to kind/* — types not listed do not produce a kind/*.
const typeToKind = {
feat: 'kind/feature',
@@ -32,7 +32,7 @@ jobs:
docs: 'kind/documentation',
chore: 'kind/cleanup',
refactor: 'kind/cleanup',
// style, perf, test, build, ci, revert — no kind mapping
// security, style, perf, test, build, ci, revert — no kind mapping
};
// scope -> area/* mapping. Keys are the scopes observed in cozystack issues
@@ -296,3 +296,82 @@ jobs:
labels: newLabels,
});
core.info(`Applied labels: ${newLabels.join(', ')}`);
# Backports are automatic (docs/release/backports.md), so a pull request
# against a release branch from outside the team is closed with a pointer to
# the policy. Only when it is opened: a maintainer keeps a fix that exists
# only on a release line by reopening it.
#
# pull_request_target hands a write token to a fork's pull request, so this
# job never touches the head: actions/checkout takes the event's own ref,
# which is the base repository, the script reads only the event JSON and the
# author's permission on the repository, and no `${{ }}` expression reaches
# the shell. The permission read needs repository metadata read access, which
# a GITHUB_TOKEN always carries and `permissions:` has no key for.
close-manual-backport:
if: >-
github.event.action == 'opened' &&
startsWith(github.event.pull_request.base.ref, 'release-')
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read # check out hack/ from the base repository
pull-requests: write # close the pull request with a comment
steps:
- name: Check out the base repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
sparse-checkout: hack
- name: Close a backport opened by hand from outside the team
env:
GH_TOKEN: ${{ github.token }}
run: hack/close-manual-backport.sh "$GITHUB_EVENT_PATH"
# A batch pull request carries each original in the shape cmd/backport-batch
# gives it. fold checks one resolution, and only when whoever resolved the
# conflict runs it; this checks every original on the batch on each push.
#
# pull_request_target takes the workflow from main for a pull request into
# any branch, so the check reaches release-* lines, which do not carry it; a
# pull_request job would run the line's own workflows. The tool is built from
# main, since the lines do not carry it either. Nothing from the pull request
# is checked out or run: its head is fetched as data and read with git, and
# no `${{ }}` expression reaches the shell. Only a branch of this repository
# named like a batch is checked.
backport-batch-verify:
name: Backport batch shape
if: >-
startsWith(github.event.pull_request.base.ref, 'release-') &&
startsWith(github.event.pull_request.head.ref, 'backport-batch/') &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
env:
# The pipe falls back to direct on any proxy error (hack/common-envs.mk).
GOPROXY: "https://proxy.golang.org|direct"
steps:
- name: Check out main
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
ref: main
fetch-depth: 0
persist-credentials: false
- name: Set up Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version-file: go.mod
cache: false
- name: Build backport-batch from main
run: go build -o "$RUNNER_TEMP/backport-batch" ./cmd/backport-batch
- name: Fetch the batch head as data
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: git fetch --no-tags --quiet origin "$HEAD_SHA"
- name: Verify every original on the batch
env:
BASE_REF: ${{ github.event.pull_request.base.ref }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: '"$RUNNER_TEMP/backport-batch" verify --line "$BASE_REF" --head "$HEAD_SHA"'
+7 -9
View File
@@ -89,16 +89,14 @@ jobs:
runs-on: ubuntu-22.04
permissions:
contents: read
# A backport PR is the one shape whose author cannot fix a finding: the
# backport action cherry-picks merged commits verbatim, so their messages
# A backport batch is the one shape whose author cannot fix a finding:
# cmd/backport-batch cherry-picks merged commits verbatim, so their messages
# are history, and roughly a quarter of the commits on main predate the
# rule. Matched on the branch name the action generates,
# backport-<pr>-to-<branch>, rather than on the prefix alone; the label that
# asks for a backport sits on the original PR, not on the generated one, so
# a branch name is the only signal a pull_request event carries. That makes
# the exemption a convention rather than something enforced: anyone can name
# a branch this way. Every other PR, fork included, is checked.
if: ${{ !(startsWith(github.head_ref, 'backport-') && contains(github.head_ref, '-to-')) }}
# rule. Matched on the branch name the batch uses,
# backport-batch/<line>-<date>, and only from a branch of this repository,
# which takes write access to push: a fork can name a branch the same way.
# Every other PR, fork included, is checked.
if: ${{ !(startsWith(github.head_ref, 'backport-batch/') && github.event.pull_request.head.repo.full_name == github.repository) }}
steps:
- name: Checkout head
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+17 -12
View File
@@ -327,9 +327,12 @@ jobs:
// A maintenance branch created before the candidate pipeline landed
// would execute its old pull_request workflows after merge. Refuse
// before the first registry write unless the actual target base has
// both trusted verification gates and their helpers. Backport this
// change to an existing release-X.Y line before promoting its next
// patch; the rc-derived head does not need to carry the tooling.
// both trusted verification gates and their helpers. An existing
// release-X.Y line gets them before its next patch is promoted,
// through the backport batch with backport/include on the pull
// request that brought them to main, or through a maintainer's pull
// request against the line; the rc-derived head does not need to
// carry the tooling.
//
// Every file the base-checked-out verifier reaches for belongs in
// the list below, its own sourced libraries included: those are
@@ -363,7 +366,7 @@ jobs:
});
} catch (e) {
if (e.status === 404) {
core.setFailed(`Target base '${baseBranch}' lacks '${path}'. Backport the candidate-aware promotion pipeline before dispatch.`);
core.setFailed(`Target base '${baseBranch}' lacks '${path}'. Bring the candidate-aware promotion pipeline to it before dispatch: backport/include on the pull request that added it to main, or a maintainer pull request against the line.`);
return;
}
throw e;
@@ -374,7 +377,7 @@ jobs:
}
const content = Buffer.from(file.data.content, 'base64').toString('utf8');
if (!content.includes(marker)) {
core.setFailed(`Target base '${baseBranch}' has an old '${path}'. Backport the candidate-aware promotion pipeline before dispatch.`);
core.setFailed(`Target base '${baseBranch}' has an old '${path}'. Bring the candidate-aware promotion pipeline to it before dispatch: backport/include on the pull request that added it to main, or a maintainer pull request against the line.`);
return;
}
}
@@ -603,10 +606,11 @@ jobs:
tar -xzf "$syft_dir/syft.tar.gz" -C "$syft_dir" syft
rm -f "$syft_dir/syft.tar.gz"
echo "$syft_dir" >> "$GITHUB_PATH"
# cosign is the version docs/release.md asks users for, so the job checks
# its own signature the way they will. The action verifies that binary
# against cosign's keyless and KMS bundles with its digest-pinned
# bootstrap cosign. Bump with tags.yaml; the contract test holds them equal.
# cosign is the version docs/release/signatures-and-sbom.md asks users
# for, so the job checks its own signature the way they will. The action
# verifies that binary against cosign's keyless and KMS bundles with its
# digest-pinned bootstrap cosign. Bump with tags.yaml; the contract test
# holds them equal.
- name: Set up cosign
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
with:
@@ -1044,9 +1048,10 @@ jobs:
PR_STATE="$(gh pr view "${STABLE_BRANCH}" --json state --jq .state 2>/dev/null || echo "")"
if [ "${PR_STATE}" = "OPEN" ]; then
# Refresh only while the body is still the one this workflow wrote.
# docs/release.md tells reviewers to paste upstream breaking-change
# notes into this body before merge; a re-dispatch that overwrites it
# would destroy that with no trace and no way to recover it.
# docs/release/pre-release-checklist.md tells reviewers to paste
# upstream breaking-change notes into this body before merge; a
# re-dispatch that overwrites it would destroy that with no trace
# and no way to recover it.
CURRENT="$(gh pr view "${STABLE_BRANCH}" --json body --jq .body 2>/dev/null || echo "")"
if [ "${CURRENT}" = "${BODY}" ]; then
echo "PR already open for ${STABLE_BRANCH} with an identical body; nothing to do."
+48 -36
View File
@@ -75,6 +75,8 @@ jobs:
statuses: write
outputs:
code: ${{ steps.p.outputs.code }}
docs: ${{ steps.p.outputs.docs }}
backport_batch: ${{ steps.p.outputs.backport_batch }}
matrix: ${{ steps.p.outputs.matrix }}
any: ${{ steps.p.outputs.any }}
touched: ${{ steps.p.outputs.touched }}
@@ -167,11 +169,27 @@ jobs:
# alone here rather than widened by a side effect.
git diff --name-only --no-renames "origin/${BASE_REF}...HEAD" > /tmp/changed_norenames.txt
echo "Changed files:"; sed 's/^/ /' /tmp/changed_norenames.txt
if grep -qvE '^docs/' /tmp/changed_norenames.txt; then
# `code` is false when every path is under docs/ or in a CI-only Go
# tool that no image builds (cmd/backport-batch, cmd/api-gate): there
# is nothing to build and nothing for e2e to install. e2e-fork.yaml
# answers the same question from the PR file list with the same
# prefixes, and hack/gate-concurrency-contract.bats holds the two lists
# equal.
if grep -qvE '^(docs/|cmd/backport-batch/|cmd/api-gate/)' /tmp/changed_norenames.txt; then
echo "code=true" >> "$GITHUB_OUTPUT"
else
echo "code=false" >> "$GITHUB_OUTPUT"
fi
# The Bats contracts read docs, so a docs change runs them even when
# `code` is false; a change confined to the CI-only tools does not.
if grep -qE '^docs/' /tmp/changed_norenames.txt; then
echo "docs=true" >> "$GITHUB_OUTPUT"
else
echo "docs=false" >> "$GITHUB_OUTPUT"
fi
backport_batch="$(hack/backport-batch-tests-needed.sh /tmp/changed_norenames.txt)"
echo "backport-batch tests needed: $backport_batch"
echo "backport_batch=$backport_batch" >> "$GITHUB_OUTPUT"
matrix="$(hack/build-matrix.sh /tmp/changed.txt)"
echo "Build matrix: $matrix"
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
@@ -293,9 +311,11 @@ jobs:
contents: read
needs: ["plan"]
# Unit contracts read both code and documentation. Run this job for every
# non-release PR: code changes take the complete unit path, while docs-only
# changes run the Bats lane that owns those cross-tree contracts. Builds and
# E2E remain gated on `code` below.
# non-release PR: code changes take the complete unit path, docs changes run
# the Bats lane that owns those cross-tree contracts, and the backport-batch
# tests run whenever one of their inputs changed and at no other time. A
# change confined to the CI-only tools runs no other lane. Builds and E2E
# remain gated on `code` below.
if: |
!contains(github.event.pull_request.labels.*.name, 'release')
steps:
@@ -306,20 +326,21 @@ jobs:
# The module cache is restored from the key build-main.yaml writes on
# main, which every PR run can read, so a PR's first run starts warm. The
# download fetches whatever the cache lacks, the whole set on a miss. The
# Go steps are for code changes only: a docs-only run is the Bats lane alone.
# Go steps are for code changes and the backport-batch tests only: a
# docs-only run is the Bats lane alone.
- name: Locate Go module cache
if: needs.plan.outputs.code == 'true'
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
id: gomodcache
run: echo "dir=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT"
- name: Cache Go modules
if: needs.plan.outputs.code == 'true'
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ steps.gomodcache.outputs.dir }}
key: go-mod-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
restore-keys: go-mod-${{ runner.os }}-
- name: Download Go modules
if: needs.plan.outputs.code == 'true'
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
run: |
for mod in $(git ls-files go.mod '*/go.mod' | xargs -n1 dirname); do
(cd "$mod" && "$GITHUB_WORKSPACE/hack/go-mod-download.sh")
@@ -327,8 +348,9 @@ jobs:
# Ephemeral runners don't carry the helm-unittest plugin the persistent
# self-hosted runner accreted. The plugin embeds its own Helm, so its
# version is pinned: a plugin release must not change test results. Bats is
# installed on every run, docs-only ones included.
# installed whenever a Bats lane runs, docs-only ones included.
- name: Set up test toolchain
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.docs == 'true'
env:
CODE_CHANGED: ${{ needs.plan.outputs.code }}
HELM_UNITTEST_VERSION: "1.1.2"
@@ -374,8 +396,20 @@ jobs:
if: needs.plan.outputs.code == 'true'
run: make unit-tests test-controllers test-controllers-envtest -j4 -k --output-sync=target
- name: Run Bats unit tests for docs-only changes
if: needs.plan.outputs.code == 'false'
if: needs.plan.outputs.code == 'false' && needs.plan.outputs.docs == 'true'
run: make bats-unit-tests
# Not part of `make unit-tests`: hack/backport-batch-tests-needed.sh
# decides from cmd/backport-batch/test-inputs.txt, whatever `code` is.
# `!cancelled()` for the reason the unit lane runs with -k: a red step
# above must not hide this suite's own result.
- name: Run backport-batch tests
if: ${{ !cancelled() && needs.plan.outputs.backport_batch == 'true' }}
run: make test-backport-batch
- name: Check new Helm document indices
if: ${{ !cancelled() }}
env:
BASE_REF: ${{ github.event.pull_request.base.ref }}
run: python3 hack/check-helm-document-indices.py "$(git merge-base HEAD "origin/$BASE_REF")"
- name: Upload unit test report
if: always()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
@@ -953,7 +987,7 @@ jobs:
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
# running into its node-join deadline, so a red there can end as a
# cancelled job. Held to one figure across every lane that runs the suite.
timeout-minutes: 215
timeout-minutes: 265
permissions:
contents: read
packages: read
@@ -1224,7 +1258,7 @@ jobs:
# ▸ Collect debug information (always runs)
#
# A ceiling of its own, not the job's 215 minutes. `Upload
# A ceiling of its own, not the job's 265 minutes. `Upload
# cozyreport.tgz` is the next step and the collector writes its tarball
# at the very end of its run, so a read that never returns spends what
# is left of the job budget, the job is cancelled, and the upload never
@@ -1235,7 +1269,7 @@ jobs:
# 30 minutes is measured against the step: it takes 3-42s across sampled
# runs, on failed e2e jobs as well as green ones. It RESERVES nothing --
# `timeout-minutes` caps a step, it does not carve budget out of the job,
# so a suite that has already burned 210 of the 215 leaves this step five
# so a suite that has already burned 260 of the 265 leaves this step five
# minutes and the upload is cancelled with everything else. What the cap
# buys is that a hung read cannot be the reason.
# `continue-on-error` because the ceiling must not change what the job
@@ -1348,28 +1382,6 @@ jobs:
Resume from inside: `breakpoint resume`. Otherwise the breakpoint
exits 10 minutes after the last SSH session disconnects.
# ▸ Tear down environment (always runs)
#
# Both halves, and in this order: the compose project owns the nodes and
# the zpools outlive it. A leftover pool makes the next run on a reused
# host adopt storage that already carries a dead cluster's volumes.
- name: Tear down sandbox
if: always()
run: |
# Loud, but never the job's verdict. A failed teardown means a dirty
# host, and this runner is discarded after the job, so there is no next
# run to protect -- while failing the job here posts the required
# "E2E Tests" status as a failure that no reader can tell apart from a
# real one. The annotation is what a reused host (a developer machine)
# needs; `delete-cluster-container` returning non-zero is not evidence
# about the product.
cleanup_rc=0
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete-cluster-container || cleanup_rc=$?
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete || true
if [ "$cleanup_rc" -ne 0 ]; then
echo "::error title=sandbox teardown::teardown exited $cleanup_rc; the host may hold a stale compose project or zpool"
fi
# Report the required "E2E Tests" gate as a commit status rather than relying
# on a job named "E2E Tests". A required *job* that is skipped counts
# as PASSED by branch protection — desired for docs-only PRs, but the exact
@@ -1449,7 +1461,7 @@ jobs:
description = `release packages candidate did not verify (result: ${CANDIDATE_RESULT})`;
} else if (CODE !== 'true') {
state = 'success';
description = 'docs-only PR — e2e not required';
description = 'docs or CI-only tools — e2e not required';
} else if (IS_RELEASE === 'true' && IS_FULL_E2E !== 'true') {
// Release promotion PRs deliberately do not repeat e2e: the rc was
// already verified against the immutable candidate whose digests
+1 -2
View File
@@ -39,8 +39,7 @@ jobs:
security/triage-needed,security/in-progress,
triage/accepted,kind/regression
exempt-pr-labels: >-
lifecycle/frozen,do-not-merge/hold,
kind/backport,kind/backport-previous
lifecycle/frozen,do-not-merge/hold
days-before-stale: 60
days-before-close: 14
operations-per-run: 100
+2 -2
View File
@@ -556,8 +556,8 @@ jobs:
tar -xzf "$syft_dir/syft.tar.gz" -C "$syft_dir" syft
rm -f "$syft_dir/syft.tar.gz"
echo "$syft_dir" >> "$GITHUB_PATH"
# cosign is the version docs/release.md asks users for, as in
# promote-rc.yaml. Bump with it.
# cosign is the version docs/release/signatures-and-sbom.md asks users
# for, as in promote-rc.yaml. Bump with it.
- name: Set up cosign
if: needs.build-amd64.outputs.release_exists == 'false'
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
+2 -1
View File
@@ -11,7 +11,7 @@ _repos
# root-level binary waiting to happen — each ~80 MB, and nothing else here
# matched them, leaving `git add .` one keystroke from committing one.
/api-gate
/backport-audit
/backport-batch
/backup-controller
/backupstrategy-controller
/check-readiness
@@ -23,6 +23,7 @@ _repos
/flux-shard-operator
/kubeovn-plunger
/lineage-controller-webhook
/observability-proxy
/securitygroup-controller
# User-specific stuff
+1
View File
@@ -33,3 +33,4 @@ This list is sorted in chronological order, based on the submission date.
| [Hidora](https://hikube.cloud) | @matthieu-robin | 2025-09-17 | Hidora is a Swiss cloud provider delivering managed services and infrastructure solutions through datacenters located in Switzerland, ensuring data sovereignty and reliability. Its sovereign cloud platform, Hikube, is designed to run workloads with high availability across multiple datacenters, providing enterprises with a secure and scalable foundation for their applications based on Cozystack. |
| [QOSI](https://qosi.kz) | @tabu-a | 2025-10-04 | QOSI is a non-profit organization driving open-source adoption and digital sovereignty across Kazakhstan and Central Asia. We use Cozystack as a platform for deploying sovereign, GPU-enabled clouds and educational environments under the National AI Program. Our goal is to accelerate the region’s transition toward open, self-hosted cloud-native technologies |
| [Cloupard](https://cloupard.kz/) | @serjiott | 2025-12-18 | Cloupard is a public cloud provider offering IaaS and PaaS services via datacenters in Kazakhstan and Uzbekistan. Uses CozyStack on bare metal to extend its managed PaaS offerings. |
| [CageForge](https://cageforge.com/) | @themoriarti | 2026-11-03 | CageForge is a short-cycle R&D company providing independent expertise in cloud native, Cozystack and security for customers in finance, telecom, hosting and cloud providers. We run Cozystack both as a bare-metal installation and on Proxmox VE through the Proxmox integration, and contribute the Proxmox-related work back upstream. |
+21 -2
View File
@@ -16,7 +16,11 @@ This file provides structured guidance for AI coding assistants and agents worki
- **Release creation** (e.g., "create release", "prepare release", "tag release", "make a release")
- Read: [`releasing.md`](./docs/agents/releasing.md)
- Action: Read the file and follow the referenced release process in `docs/release.md`
- Action: Read the file and follow the referenced release process: [`docs/release.md`](./docs/release.md) indexes one page per topic under `docs/release/`
- **Backports, patch content** (e.g., "backport this fix", "will this reach 1.6", "what goes into the next patch", "open a PR against release-X.Y")
- Read: [`backports.md`](./docs/release/backports.md)
- Action: Read the entire file. Backports are automatic: a fix merged to `main` with a `fix` title, or a `security` one for a fix that comes through a security advisory, reaches the release lines the batch runs for, the newest released lines and any line in its rc window, so never open a backport PR by hand. A pull request against `release-*` from outside the team is closed automatically when it is opened; a maintainer keeps a fix with no counterpart on `main` by reopening it
- **Image references, tags and digests** (e.g., "why is this image tagged rc", "add an image to a package", "change how an image is stamped", "promotion/nightly missed an image", "where do image refs live")
- Read: [`image-refs.md`](./docs/agents/image-refs.md)
@@ -32,7 +36,7 @@ This file provides structured guidance for AI coding assistants and agents worki
- **E2E tests and E2E CI** (e.g., "write/fix an e2e test", "stabilize a flaky test", "add a bats test", "change the e2e workflow", "why did e2e fail")
- Read: [`e2e-testing.md`](./docs/agents/e2e-testing.md)
- Action: Read the entire file and follow the conventions exactly — no new retries on deterministic steps, event-driven backstops before every `kubectl wait`, no test-level `EXIT`/`RETURN` traps (a self-contained one inside a Chainsaw `script` step or an explicit subshell is fine — see the doc), no assertion beginning with `!` (errexit exempts an inverted status, so it cannot fail — write `if cmd; then echo "FAIL: …"; false; fi`), fail-fast on HelmRelease readiness
- Action: Read the entire file and follow the conventions exactly — no new retries on deterministic steps, event-driven backstops before every `kubectl wait`, no test-level `EXIT`/`RETURN` traps (a self-contained one inside a Chainsaw `script` step or an explicit subshell is fine — see the doc), no assertion beginning with `!` (errexit exempts an inverted status, so it can be silently ignored — write `if cmd; then echo "FAIL: …"; false; fi`), fail-fast on HelmRelease readiness
- **General questions about contributing**
- Read: [`contributing.md`](./docs/agents/contributing.md)
@@ -47,10 +51,22 @@ This file provides structured guidance for AI coding assistants and agents worki
- PR titles: a Conventional Commits header (`type(scope): description`, types from [`contributing.md`](./docs/agents/contributing.md)) auto-applies `kind/*` and `area/*` via `.github/workflows/pr-labeler.yaml`. Append `!` (or add a `BREAKING CHANGE:` footer) to apply `kind/breaking-change`
**Important rules:**
- **Match scope, then read first.** Load a process file only when the task genuinely falls in its scope, and load it before starting work — not midway through. Tasks outside a file's scope should not pull it into context.
- **Follow it exactly.** Do not assume you already know the process, and do not skip steps (`changelog.md` especially). Where the documentation and your instinct disagree, the documentation wins.
- **[`overview.md`](./docs/agents/overview.md) is the standing reference** for structure and conventions, useful even when no route above points at it.
## Working Principles
These hold for every task, whichever document the routes above point at.
- **A causal claim needs a mechanism.** A failure that appeared after a commit, a bump or a rerun was not thereby caused by it, and matching symptoms prove nothing either: unrelated faults often look identical from outside. Back a root cause with a log line, a reproducer, a bisect, or a ruled-out alternative; without one, name at least two hypotheses and what would tell them apart. Before tying a problem to a change, check whether it predates the change (an older run, a condition's `lastTransitionTime`, `git blame`). Close a hypothesis when evidence rules it out. Missing evidence means inconclusive, not coincidence: record that limit, stop treating the hypothesis as a cause, and revisit it only with new evidence or a concrete discriminating check.
- **Verify, do not recall.** Versions, CLI flags, API fields and defaults drift faster than a model's training data. Check the upstream source, the documentation at the pinned version, or `--help` before writing code or advice that depends on them. When a problem is not solved after two attempts, stop and research it instead of trying a third variation.
- **Long flag names.** New and changed commands in docs, scripts, CI and PR text spell options out (`--namespace`, `--output`, `--filename`, `--message`), so that a reader does not need to know what `-f` means to this particular tool. An option with no long form is the only exception.
- **Name the cluster.** Management and tenant clusters are different targets. Every `kubectl` call, including reads, carries `--context`; cluster-facing Helm commands carry `--kube-context`. Use the human-approved target and the task's kubeconfig where one was supplied, not the ambient current context. Before running wrappers or cluster-facing tests, verify that they propagate the chosen target. Tests that need no cluster must not inherit live cluster credentials.
- **Respect the reader's time.** Lead with the answer or decision; prefer direct wording over formalities, filler and repeated conclusions. Put necessary detail after a short summary or in a linked document.
- **Public text.** Everything that reaches GitHub (commits, PR and issue bodies, review comments, code comments) is in English. It names no private infrastructure (cluster, customer or internal namespace names) and none of an agent's private tooling (custom skills, slash commands). Nothing is published on someone's behalf without their approval, unless they have delegated it.
## Project Overview
**Cozystack** is a Kubernetes-based platform for building cloud infrastructure with managed services (databases, VMs, K8s clusters), multi-tenancy, and GitOps delivery.
@@ -58,6 +74,7 @@ This file provides structured guidance for AI coding assistants and agents worki
## Quick Reference
### Code Structure
- `packages/core/` - Core platform charts (installer, platform)
- `packages/system/` - System components (CSI, CNI, operators)
- `packages/apps/` - User-facing applications in catalog
@@ -66,6 +83,7 @@ This file provides structured guidance for AI coding assistants and agents worki
- `api/` - Kubernetes CRDs
### Conventions
- **Helm Charts**: Umbrella pattern, vendored upstream charts in `charts/`
- **Go Code**: Controller-runtime patterns, kubebuilder style
- **Git Commits**: Conventional Commits (`type(scope): description`) with `--signoff`; an agent-assisted commit adds one `Assisted-by: LLM` trailer
@@ -73,6 +91,7 @@ This file provides structured guidance for AI coding assistants and agents worki
- **Prose**: One continuous line per paragraph — see [Prose Formatting](#prose-formatting)
### What NOT to Do
- ❌ Edit `/vendor/`, `zz_generated.*.go`, upstream charts directly
- ❌ Modify `go.mod`/`go.sum` manually (use `go get`)
- ❌ Force push to main/master
+1 -1
View File
@@ -88,7 +88,7 @@
### Rollout, Upgrade and Rollback Planning
* **Compatibility with Kubernetes & frequency.** Cozystack ships a curated, tested Kubernetes version per release; tenant clusters are Certified Kubernetes following upstream skew/support. Platform upgrades are delivered as new Cozystack releases. The authoritative list of what has shipped is the [GitHub Releases page](https://github.com/cozystack/cozystack/releases); which release lines are still receiving security fixes is defined as a rolling rule in [SECURITY.md](https://github.com/cozystack/cozystack/blob/main/SECURITY.md): the latest stable minor and the two preceding minors are supported, older `1.x` minors receive critical and upgrade-blocking fixes at maintainer discretion, `v0.41.x` is end of life and anything earlier is unsupported. Specific version numbers are deliberately not restated here, because they drift with every release. Note that being in a supported line does not entail receiving every fix: the project has no documented rule for which supported lines a given fix is backported to, and that is recorded in `SECURITY.md` rather than presented as a policy.
* **Compatibility with Kubernetes & frequency.** Cozystack ships a curated, tested Kubernetes version per release; tenant clusters are Certified Kubernetes following upstream skew/support. Platform upgrades are delivered as new Cozystack releases. The authoritative list of what has shipped is the [GitHub Releases page](https://github.com/cozystack/cozystack/releases); which release lines are still receiving security fixes is defined as a rolling rule in [SECURITY.md](https://github.com/cozystack/cozystack/blob/main/SECURITY.md): the two latest minor releases are supported, older `1.x` minors receive critical and upgrade-blocking fixes at maintainer discretion, `v0.41.x` is end of life and anything earlier is unsupported. Specific version numbers are deliberately not restated here, because they drift with every release. Note that being in a supported line does not entail receiving every fix: which fixes reach a supported line is the [backport policy](https://github.com/cozystack/cozystack/blob/main/docs/release/backports.md), which keeps breaking changes, platform migrations and the like for the next minor.
* **Upgrade procedure** ([upgrade docs](https://cozystack.io/docs/v1.6/operations/cluster/upgrade/)): run the troubleshooting/health checklist; annotate the `cozy-system` namespace and `cozystack-version` ConfigMap with `helm.sh/resource-policy=keep`; `helm upgrade cozystack oci://ghcr.io/cozystack/cozystack/cozy-installer --version X.Y.Z` (**do not** use `--reuse-values` — the chart pins OCI repo/package versions); verify pods and `kubectl get hr -A`.
* **Rollback.** GitOps/Helm-managed with versioned releases; roll back by targeting the previous release (previous HelmRelease revisions) via the same mechanism. Stateful data (databases, VM disks) persists on replicated storage across control-plane rollbacks. **No dedicated rollback runbook is published**, and version-skip constraints for downgrades are not documented; the [upgrade docs](https://cozystack.io/docs/v1.6/operations/cluster/upgrade/) currently cover the forward path only.
* **How a rollout/rollback can fail & impact.** Risks: incompatible CRD schema change, storage/CNI component upgrades requiring node reboots (on the Talos path), or a database-operator major bump. Impact is scoped to the component; running tenant workloads generally continue, though storage/CNI upgrades can transiently affect networking or volume attachment during node rolls. `[maintainers: confirm whether upgrades trigger Talos node reboots]`
+18 -15
View File
@@ -1,4 +1,4 @@
.PHONY: manifests assets prepare-env prepare-env-container unit-tests helm-unit-tests bats-unit-tests bats-unit-files-check bats-posix-compat-tests print-bats-unit-files print-bats-posix-compat-files print-bats-jobs rd-presets-check migrations-target-check test test-controllers test-controllers-envtest test-backport-audit preflight
.PHONY: manifests assets prepare-env prepare-env-container unit-tests helm-unit-tests bats-unit-tests bats-unit-files-check bats-posix-compat-tests print-bats-unit-files print-bats-posix-compat-files print-bats-jobs rd-presets-check migrations-target-check test test-controllers test-controllers-envtest test-backport-batch preflight
include hack/common-envs.mk
@@ -17,6 +17,7 @@ build: build-deps
make -C packages/system/cozystack-api image
make -C packages/system/cozystack-controller image
make -C packages/system/backup-controller image
make -C packages/system/observability-proxy image
make -C packages/system/backupstrategy-controller image
make -C packages/system/lineage-controller-webhook image
make -C packages/system/migration-controller image
@@ -119,7 +120,7 @@ test:
make -C packages/core/testing apply
make -C packages/core/testing e2e
unit-tests: helm-unit-tests bats-unit-tests bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness test-backport-audit migrations-target-check
unit-tests: helm-unit-tests bats-unit-tests bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness migrations-target-check
helm-unit-tests:
hack/helm-unit-tests.sh
@@ -138,13 +139,14 @@ rd-presets-check:
migrations-target-check:
hack/check-migrations-target.sh
# Scoped go test over the cozystack-api surface that this repo owns. Kept
# narrow intentionally - running `go test ./...` pulls in generated code
# round-trip suites whose behavior depends on tool versions outside this
# repo's control (kubebuilder, openapi-gen, etc.) and is better exercised
# from their generator workflows.
# Go tests of the cozystack-api surface and of the standalone packages and
# commands listed here, whose tests need no cluster. Kept narrow
# intentionally - running `go test ./...` pulls in generated code round-trip
# suites whose behavior depends on tool versions outside this repo's control
# (kubebuilder, openapi-gen, etc.) and is better exercised from their
# generator workflows.
go-unit-tests:
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/...
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/... ./pkg/lineage/... ./pkg/ovnstatus/... ./pkg/reservation/... ./cmd/cozypkg/... ./cmd/check-readiness/...
# The nested Go modules (image sources and the published API types) are
# outside ./... of the root module, so no other target reaches their tests.
@@ -157,9 +159,7 @@ go-module-tests:
done
# Go tests for the controllers and supporting packages under ./internal.
# Excludes ./pkg/... and ./cmd/... — those are run separately by
# go-unit-tests above (pkg subset) and skipped (cmd) until their tests
# stabilise. CI schedules this target in the same four-slot make invocation as
# CI schedules this target in the same four-slot make invocation as
# unit-tests; locally invoke it directly or chain the two targets.
test-controllers:
go test ./internal/... -count=1
@@ -194,9 +194,12 @@ test-controllers-envtest: $(SETUP_ENVTEST)
test-check-readiness:
go test ./test/check-readiness/ -count=1
# ./cmd/... is excluded from go-unit-tests, so the audit needs its own target.
test-backport-audit:
go test ./cmd/backport-audit/ -count=1
# The batch tool's suite drives real git repositories for minutes, so it is
# kept out of go-unit-tests in a target of its own. It is not a prerequisite
# of unit-tests: the checks job in pull-requests.yaml runs it only when the
# diff touches a path listed in cmd/backport-batch/test-inputs.txt.
test-backport-batch:
go test ./cmd/backport-batch/... -count=1
# Unit discovery is one level deep. Live-cluster files need an invocation in
# packages/core/testing; hack/bats-runner-coverage.bats checks that boundary.
@@ -294,7 +297,7 @@ bats-posix-compat-tests:
# own too; hack/unit-lane-no-cluster.bats fails when one is missing.
NO_CLUSTER_TARGETS := unit-tests test-controllers helm-unit-tests bats-unit-tests \
bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check \
test-check-readiness test-backport-audit migrations-target-check
test-check-readiness test-backport-batch migrations-target-check
$(NO_CLUSTER_TARGETS): export KUBECONFIG := /dev/null
$(NO_CLUSTER_TARGETS): export KUBERNETES_SERVICE_HOST :=
+5 -5
View File
@@ -8,7 +8,7 @@ Cozystack integrates and ships many upstream cloud native components. If you bel
## Supported Versions
The Cozystack project maintains three release lines at a time: **the latest stable minor and the two minors preceding it.** That is a rolling rule rather than a list of version numbers, so it does not go stale between releases — read it against the GitHub Releases page to see which concrete lines it currently names:
The Cozystack project supports two release lines at a time: **the two latest minor releases**, the latest stable minor and the one before it. When a new minor ships its first stable release it becomes the latest, and the older of the two drops out. That is a rolling rule rather than a list of version numbers, so it does not go stale between releases — read it against the GitHub Releases page to see which concrete lines it currently names:
<https://github.com/cozystack/cozystack/releases>
@@ -16,16 +16,16 @@ Note that the rule is keyed to release lines, not to the existence of a `release
| Version line | Status | Notes |
| --- | --- | --- |
| Latest stable minor | Supported | Current stable release line. Security fixes land here first, and patch releases are cut from it. |
| The two minors preceding the latest | Supported | Receive security and important maintenance fixes, backported from the current line. |
| Latest stable minor | Supported | Current stable release line, the first supported line a fix reaches. Patch releases are cut from it. |
| The minor before the latest | Supported | Receives the same security and maintenance fixes, backported once the latest line carries them. |
| Older `1.x` minors | Limited support | Critical and upgrade-blocking fixes may be backported at maintainer discretion. Adopters are encouraged to move to a supported line. |
| `v0.41.x` | End of life | The final pre-v1 line. It received patch releases through March 2026 during the v0 to v1 transition and no longer does; no further fixes are planned. |
| Everything before `v0.41` | Not supported | Long superseded. Upgrade to a `1.x` line. |
| `alpha`, `beta`, `rc` releases | Not supported | Pre-release builds are for testing and evaluation only. |
Being in a supported row does not mean a line receives every fix. **The project does not have a documented rule for choosing which supported lines a given fix is backported to** — that decision is taken per fix by the maintainers, weighing severity against the risk and effort of the backport. This is stated rather than dressed up as a policy because no such policy exists; adopters who need a specific fix on a specific line should ask.
Being in a supported row does not mean a line receives every fix. Fixes reach the supported lines through the backport batch, by the rules in [`docs/release/backports.md`](docs/release/backports.md): a fix merged into `main` with a `fix` or `security` title goes onto both lines, unless the policy keeps it for the next minor, a breaking change or a platform migration for instance, and a maintainer can override that for a single fix. Adopters who need a specific fix on a specific line should ask.
Reporting a vulnerability against a line with limited support or at end of life is still welcome. We will confirm whether the issue also affects a supported line and fix it there; whether the fix is additionally backported to the older line is a per-fix maintainer decision on the same basis.
Reporting a vulnerability against a line with limited support or at end of life is still welcome. We will confirm whether the issue also affects a supported line and fix it there; whether the fix is additionally backported to the older line is a per-fix maintainer decision.
## Reporting a Vulnerability
+3
View File
@@ -25,6 +25,9 @@ type ConfigSpec struct {
// Deploys the web UI (s3manager) with its Service and its Ingress or HTTPRoute at `<bucket>.<tenant host>`. Set to `false` to run the bucket without them.
// +kubebuilder:default:=true
Ui bool `json:"ui"`
// Serves the bucket's objects to anyone without credentials at `<endpoint>/<bucket>/<key>`. Anonymous callers can read objects but cannot list the bucket or write to it.
// +kubebuilder:default:=false
PublicRead bool `json:"publicRead"`
// Users configuration map.
// +kubebuilder:default:={}
Users map[string]User `json:"users,omitempty"`
+3 -24
View File
@@ -25,10 +25,10 @@ type ConfigSpec struct {
// Kubernetes major.minor version to deploy
// +kubebuilder:default:="v1.35"
Version Version `json:"version"`
// External hostname for Kubernetes cluster. Defaults to `<cluster-name>.<tenant-host>` if empty.
// External hostname for Kubernetes cluster. Defaults to `kubernetes-<cluster-name>.<tenant-host>` if empty.
// +kubebuilder:default:=""
Host string `json:"host"`
// Which infrastructure provider backs this cluster's worker VMs. `kubevirt` runs them inside this cluster; `proxmox` runs them on an external Proxmox VE cluster through capmox. The control plane is Kamaji either way — this selects the infrastructure half only. Must match the `substrate` of every KubernetesNodes pool attached to this cluster: the pools reference this cluster's infrastructure object by kind, and a mismatch leaves their Machines unreconciled. Switching an existing cluster is not supported; create a new one.
// Which infrastructure provider backs this cluster's worker VMs. `kubevirt` runs them inside this cluster; `proxmox` runs them on an external Proxmox VE cluster through capmox. The control plane is Kamaji either way — this selects the infrastructure half only. Every KubernetesNodes pool of this cluster must set the same `substrate`: a pool set to the other one refuses to render once this cluster exists. Switching an existing cluster is not supported; create a new one.
// +kubebuilder:default:="kubevirt"
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="substrate is immutable"
Substrate string `json:"substrate"`
@@ -41,9 +41,6 @@ type ConfigSpec struct {
// Kubernetes control-plane configuration.
// +kubebuilder:default:={}
ControlPlane ControlPlane `json:"controlPlane"`
// Talos worker image configuration.
// +kubebuilder:default:={}
Talos Talos `json:"talos"`
// OIDC authentication and per-user RBAC for the tenant kube-apiserver. See https://cozystack.io/docs/v1.6/kubernetes/oidc-authentication/ for the operator guide.
// +kubebuilder:default:={}
Oidc OIDC `json:"oidc"`
@@ -53,7 +50,7 @@ type APIServer struct {
// Admission plugins the tenant kube-apiserver enables, passed through to KamajiControlPlane `spec.admissionControllers`, which the control plane renders as `--enable-admission-plugins`. A non-empty list replaces the control plane's default set instead of adding to it. The kube-apiserver enables these plugins in addition to its own default ones, so leaving a plugin out does not turn it off; to turn one off, name it in `--disable-admission-plugins` in `controlPlane.apiServer.extraArgs` and keep it out of this list, since the apiserver refuses to start with a plugin in both, and the control plane's default set names several. Empty by default, which keeps that default set, taken from the TenantControlPlane CRD: CertificateApproval, CertificateSigning, CertificateSubjectRestriction, DefaultIngressClass, DefaultStorageClass, DefaultTolerationSeconds, LimitRanger, MutatingAdmissionWebhook, NamespaceLifecycle, PersistentVolumeClaimResize, Priority, ResourceQuota, RuntimeClass, ServiceAccount, StorageObjectInUseProtection, TaintNodesByCondition, ValidatingAdmissionWebhook. `NodeRestriction` is not in it.
// +kubebuilder:default:={}
AdmissionControllers []AdmissionController `json:"admissionControllers,omitempty"`
// Extra command-line flags appended to the tenant kube-apiserver, passed through to KamajiControlPlane `spec.apiServer.extraArgs`. For OIDC use `spec.oidc.mode` — this passthrough is the escape hatch for unrelated apiserver flags. The tenant control plane sets each of the following flags itself and drops an entry naming one before the apiserver sees it. The chart moves an `--enable-admission-plugins=` entry into `controlPlane.apiServer.admissionControllers` and a `--kubelet-preferred-address-types=` entry into `controlPlane.kubelet.preferredAddressTypes` while that field is empty, and refuses any other entry naming one of them: `--advertise-address`, `--client-ca-file`, `--enable-admission-plugins`, `--service-cluster-ip-range`, `--kubelet-client-certificate`, `--kubelet-client-key`, `--kubelet-preferred-address-types`, `--proxy-client-cert-file`, `--proxy-client-key-file`, `--requestheader-allowed-names`, `--requestheader-client-ca-file`, `--secure-port`, `--service-account-key-file`, `--service-account-signing-key-file`, `--tls-cert-file`, `--tls-private-key-file`, `--etcd-servers`, `--etcd-cafile`, `--etcd-certfile`, `--etcd-keyfile`, `--etcd-prefix`, `--etcd-compaction-interval`, `--egress-selector-config-file`. When `spec.oidc.mode` is not `None` the chart also renders OIDC flags here, reads what you have written on them, and refuses some combinations outright; the Aggregated API servers section of the Kubernetes OIDC guide on cozystack.io carries which flags, which combinations, and why. Do NOT add legacy `--oidc-*` flags here when `spec.oidc.mode` is not `None`; the chart injects `--authentication-config` and the apiserver refuses to boot with both. Empty by default (no change to current behavior).
// Extra command-line flags appended to the tenant kube-apiserver, passed through to KamajiControlPlane `spec.apiServer.extraArgs`. For OIDC use `spec.oidc.mode` — this passthrough is the escape hatch for unrelated apiserver flags. The tenant control plane sets each of the following flags itself and drops an entry naming one before the apiserver sees it. The chart moves an `--enable-admission-plugins=` entry into `controlPlane.apiServer.admissionControllers` and a `--kubelet-preferred-address-types=` entry into `controlPlane.kubelet.preferredAddressTypes` while that field is empty, and refuses any other entry naming one of them: `--advertise-address`, `--client-ca-file`, `--enable-admission-plugins`, `--service-cluster-ip-range`, `--kubelet-client-certificate`, `--kubelet-client-key`, `--kubelet-preferred-address-types`, `--proxy-client-cert-file`, `--proxy-client-key-file`, `--requestheader-allowed-names`, `--requestheader-client-ca-file`, `--secure-port`, `--service-account-key-file`, `--service-account-signing-key-file`, `--tls-cert-file`, `--tls-private-key-file`, `--etcd-servers`, `--etcd-cafile`, `--etcd-certfile`, `--etcd-keyfile`, `--etcd-prefix`, `--etcd-compaction-interval`, `--egress-selector-config-file`. When `spec.oidc.mode` is not `None` the chart also renders OIDC flags here, reads what you have written on them, and refuses some combinations outright; the Aggregated API servers section of the Kubernetes OIDC guide on cozystack.io carries which flags, which combinations, and why. Do NOT add legacy `--oidc-*` flags here when `spec.oidc.mode` is not `None`; the chart injects `--authentication-config` and the apiserver refuses to boot with both. Empty by default.
// +kubebuilder:default:={}
ExtraArgs []string `json:"extraArgs,omitempty"`
// Extra volume mounts added to the tenant kube-apiserver container, passed through to KamajiControlPlane `spec.apiServer.extraVolumeMounts`. Each `name` must reference a volume declared in `extraVolumes`; the chart-managed talos secret volumes cannot be mounted. Each item is a core/v1 VolumeMount. Empty by default.
@@ -349,24 +346,6 @@ type Scheduler struct {
ResourcesPreset ResourcesPreset `json:"resourcesPreset"`
}
type Talos struct {
// Base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash.
// +kubebuilder:default:="https://factory.talos.dev"
ImageFactoryURL string `json:"imageFactoryURL"`
// OCI repository prefix for the Talos installer image used by the in-guest `talos-reconcile` upgrade Job. Resolved as `<installerRepository>/<schematicID>:<version>`. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
// +kubebuilder:default:="factory.talos.dev/installer"
InstallerRepository string `json:"installerRepository"`
// Talos `machine.registries.mirrors` passthrough for worker nodes: a map of upstream registry host to `{ endpoints: [ ... ] }`. Empty by default, so workers pull container images (the Talos `kubelet` image included) directly from the upstream registry. Point a host such as `ghcr.io` at an in-cluster pull-through mirror for air-gapped, rate-limited, or flaky-egress environments so a worker's boot does not depend on live public egress. Talos still falls back to the upstream registry unless a host also sets `skipFallback: true`, so a mirror alone does not enforce air-gap.
// +kubebuilder:default:={}
RegistryMirrors k8sRuntime.RawExtension `json:"registryMirrors"`
// Talos image-factory schematic ID. Defaults to the cozystack-tested vanilla schematic. Operators using custom schematics (system extensions, kernel args) override here.
// +kubebuilder:default:="ce4c980550dd2ab1b17bbf2b08801c7eb59418eafe8f279833297925d67c7515"
SchematicID string `json:"schematicID"`
// Talos release used for worker OS image and installer. Must satisfy the chart's Talos<->Kubernetes support matrix against the chosen `version`.
// +kubebuilder:default:="v1.13.6"
Version string `json:"version"`
}
type VeleroAddon struct {
// Enable Velero.
// +kubebuilder:default:=false
-17
View File
@@ -154,7 +154,6 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
in.Proxmox.DeepCopyInto(&out.Proxmox)
in.Addons.DeepCopyInto(&out.Addons)
in.ControlPlane.DeepCopyInto(&out.ControlPlane)
in.Talos.DeepCopyInto(&out.Talos)
in.Oidc.DeepCopyInto(&out.Oidc)
}
@@ -570,22 +569,6 @@ func (in *Scheduler) DeepCopy() *Scheduler {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Talos) DeepCopyInto(out *Talos) {
*out = *in
in.RegistryMirrors.DeepCopyInto(&out.RegistryMirrors)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Talos.
func (in *Talos) DeepCopy() *Talos {
if in == nil {
return nil
}
out := new(Talos)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *VeleroAddon) DeepCopyInto(out *VeleroAddon) {
*out = *in
+27 -23
View File
@@ -18,11 +18,11 @@ type Config struct {
}
type ConfigSpec struct {
// Name of the parent Kubernetes cluster (the `Kubernetes` CR) in the same namespace this node pool attaches to. Required. The pool's CAPI objects are wired to the parent cluster `kubernetes-<cluster>`; the KubernetesNodes CR must be named `<cluster>-<pool>`. The pool name (the part after `<cluster>-`) must not collide with a nodeGroup still managed by the parent kubernetes chart (e.g. the default `md0`), or the render fails on ownership conflict.
// Name of the parent Kubernetes cluster (the `Kubernetes` CR) in the same namespace this node pool attaches to. Required. The pool's CAPI objects are wired to the parent cluster `kubernetes-<cluster>`; the KubernetesNodes CR must be named `<cluster>-<pool>`. The pool's objects are named `kubernetes-<cluster>-<pool>`; when a MachineDeployment of that name already exists and belongs to another Helm release, the render fails instead of taking it over.
// +kubebuilder:default:=""
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster is immutable"
Cluster string `json:"cluster"`
// StorageClass for the worker node system disk. When empty, the cluster default applies. Worker VMs live-migrate, so their disks need ReadWriteMany — the RWX access mode is supplied by the chosen StorageClass's CDI StorageProfile — and linstor-csi rejects RWX volumes that are not on a DRBD-backed StorageClass, so prefer a replicated/DRBD class. On an `osImage.builtin` pool this field also decides whether the pool renders at all: CDI takes the storage-layer clone path only when the worker disk and the golden share a StorageClass, and falls back to a host-assisted copy over the pod network across classes, so the chart refuses a pool whose class differs from its golden's. Leaving it empty resolves to the cluster's default StorageClass, and the same comparison is made against that. On a `builtin` pool the resolved class is then written onto the worker disk rather than left for the cluster to fill in at bind time, so a pool that has already cloned keeps binding on the class its disks are on even after an admin moves the default annotation; on the HTTP path an empty class is still left to the cluster.
// StorageClass for the worker node system disk. When empty, the cluster default applies. Worker VMs live-migrate, so their disks need ReadWriteMany — the RWX access mode is supplied by the chosen StorageClass's CDI StorageProfile — and linstor-csi rejects RWX volumes that are not on a DRBD-backed StorageClass, so prefer a replicated/DRBD class. On an `osImage.builtin` pool this field also decides whether the pool renders at all: CDI takes the storage-layer clone path only when the worker disk and the golden share a StorageClass, and falls back to a host-assisted copy over the pod network across classes, so the chart refuses a pool whose class differs from its golden's. Leaving it empty resolves to the cluster's default StorageClass, and the same comparison is made against that. On a `builtin` pool the resolved class is then written onto the worker disk rather than left for the cluster to fill in at bind time, so a pool that has already cloned keeps binding on the class its disks are on even after an admin moves the default annotation; on the HTTP path an empty class is still left to the cluster. Has no effect on `substrate: proxmox`, where the worker disk is cloned from the VM template onto the template's storage or `proxmox.storage`.
// +kubebuilder:default:="replicated"
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="storageClass is immutable"
StorageClass string `json:"storageClass"`
@@ -32,10 +32,10 @@ type ConfigSpec struct {
// Maximum number of replicas in this pool. Used as the cluster-autoscaler ceiling.
// +kubebuilder:default:=10
MaxReplicas int `json:"maxReplicas"`
// Virtual machine instance type.
// KubeVirt instance type that sizes each worker VM when `resources` is not set. Ignored when `resources.cpu` and `resources.memory` are both set, and therefore on `substrate: proxmox`, which requires them.
// +kubebuilder:default:="u1.medium"
InstanceType string `json:"instanceType"`
// System disk size for the worker VM. Carries the Talos OS image, kubelet state, containerd image cache, and any local-path PVCs. How the OS image reaches the disk follows this pool's `osImage`: the default and `osImage.factory` stream the Image Factory raw artifact in over HTTP, while `osImage.builtin` makes the disk a storage-layer CDI clone of a shared golden image, with no HTTP fetch. A `builtin` pool must keep diskSize at or above the golden's storage size (6Gi in the shipped worker image catalog), because a CDI clone target cannot be smaller than its source -- the chart rejects a smaller value at render time rather than letting the clone stall. Sizing a `builtin` pool also has to allow for transient headroom the finished disk does not show: CDI populates a larger target by cloning the golden at the source size and then growing it, so a worker being provisioned occupies a temporary clone PVC at the golden's size on top of this diskSize, and both are multiplied by the replica count of a replicated StorageClass. The golden itself is a separate one-time cost for the whole cluster, not a per-worker one. So the peak is the golden, plus `diskSize + golden storage` for each worker cloning at that moment. A pool whose storage backend cannot spare that peak does not fail: the clone waits for space, the VM never boots, and the reason is visible only in the storage layer's own events.
// System disk size for the worker VM. Carries the Talos OS image, kubelet state, containerd image cache, and any local-path PVCs. How the OS image reaches the disk follows this pool's `osImage`: the default and `osImage.factory` stream the Image Factory raw artifact in over HTTP, while `osImage.builtin` makes the disk a storage-layer CDI clone of a shared golden image, with no HTTP fetch. A `builtin` pool must keep diskSize at or above the golden's storage size (6Gi in the shipped worker image catalog), because a CDI clone target cannot be smaller than its source -- the chart rejects a smaller value at render time rather than letting the clone stall. Sizing a `builtin` pool also has to allow for transient headroom the finished disk does not show: CDI populates a larger target by cloning the golden at the source size and then growing it, so a worker being provisioned occupies a temporary clone PVC at the golden's size on top of this diskSize, and both are multiplied by the replica count of a replicated StorageClass. The golden itself is a separate one-time cost for the whole cluster, not a per-worker one. So the peak is the golden, plus `diskSize + golden storage` for each worker cloning at that moment. A pool whose storage backend cannot spare that peak does not fail: the clone waits for space, the VM never boots, and the reason is visible only in the storage layer's own events. Has no effect on `substrate: proxmox`, where the worker disk is a clone of the VM template's disk and keeps its size.
// +kubebuilder:default:="20Gi"
DiskSize resource.Quantity `json:"diskSize"`
// List of node roles. Each role `r` labels nodes with `node-role.kubernetes.io/<r>`. Use `[ingress-nginx]` for a pool that hosts the tenant ingress-nginx controller.
@@ -44,16 +44,20 @@ type ConfigSpec struct {
// Explicit CPU and memory for each worker node, as an alternative to `instanceType` sizing. Optional: when omitted, the node is sized by `instanceType`. When both `cpu` and `memory` are set, they take precedence and `instanceType` is ignored (the instancetype is omitted from the VM, since KubeVirt cannot override an instancetype's CPU/memory). Set both `cpu` and `memory` together or neither; setting only one is rejected at render time. On the kubevirt substrate `cpu` is written into the VM as spelled and decoded as YAML, so a leading zero is read as octal, as in any Kubernetes manifest: `"010"` is eight cores, and the autoscaler capacity counts eight. The kubelet reservations still read the value as a decimal, so `"010"` reserves what `"10"` does.
// +kubebuilder:default:={}
Resources Resources `json:"resources,omitempty"`
// CPU limit for the worker VM's virt-launcher `compute` container, which is the ceiling the guest vCPU threads, the QEMU emulator thread and the IO threads share. Distinct from `resources.cpu`, which sizes the guest itself. Cannot be combined with `instanceType` sizing: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.limits.cpu`, so the chart rejects that combination at render time. Sizing the pool by `resources` (both `cpu` and `memory`) is what drops the instancetype from the VM, which is where the limit is usable. When unset, KubeVirt derives the limit from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so a worker sized at N vCPUs runs under a ceiling of N with the emulation overhead taken out of the same N. Setting a value declares the limit on the VM directly, which is both what lands it on the container and what stops KubeVirt deriving one. It moves the CPU request with it: KubeVirt copies a declared CPU limit into the CPU request when the VM declares no request of its own, so a pool that sets only this field asks the scheduler for its whole ceiling instead of the fraction of the vCPU count it asked for before, and a ceiling the nodes cannot spare leaves the workers Pending on `Insufficient cpu`. Pair it with `podCpuRequest` to move the ceiling without moving the request. Zero means no ceiling at all rather than the tightest one, since the kubelet turns a zero CPU limit into no CFS quota while the declared key still stops KubeVirt deriving one — but it is only accepted together with `podCpuRequest: 0`, because KubeVirt keeps deriving a request from the vCPU count when the VM declares none and a request above a declared limit of zero is rejected when the worker's Pod is created. A negative value is refused at render time. Changing it rolls the pool, because the worker VM template is named by a hash of its content. Keep in sync with the parent kubernetes chart.
// CPU limit for the worker VM's virt-launcher `compute` container, which is the ceiling the guest vCPU threads, the QEMU emulator thread and the IO threads share. Distinct from `resources.cpu`, which sizes the guest itself. Cannot be combined with `instanceType` sizing: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.limits.cpu`, so the chart rejects that combination at render time. Sizing the pool by `resources` (both `cpu` and `memory`) is what drops the instancetype from the VM, which is where the limit is usable. When unset, KubeVirt derives the limit from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so a worker sized at N vCPUs runs under a ceiling of N with the emulation overhead taken out of the same N. Setting a value declares the limit on the VM directly, which is both what lands it on the container and what stops KubeVirt deriving one. It moves the CPU request with it: KubeVirt copies a declared CPU limit into the CPU request when the VM declares no request of its own, so a pool that sets only this field asks the scheduler for its whole ceiling instead of the fraction of the vCPU count it asked for before, and a ceiling the nodes cannot spare leaves the workers Pending on `Insufficient cpu`. Pair it with `podCpuRequest` to move the ceiling without moving the request. Zero means no ceiling at all rather than the tightest one, since the kubelet turns a zero CPU limit into no CFS quota while the declared key still stops KubeVirt deriving one — but it is only accepted together with `podCpuRequest: 0`, because KubeVirt keeps deriving a request from the vCPU count when the VM declares none and a request above a declared limit of zero is rejected when the worker's Pod is created. A negative value is refused at render time. Changing it rolls the pool, because the worker VM template is named by a hash of its content.
PodCpuLimit resource.Quantity `json:"podCpuLimit,omitempty"`
// CPU request for the worker VM's virt-launcher `compute` container, which is what the scheduler reserves for a worker on its host and what weights it against neighbours on a contended one. Distinct from `resources.cpu`, which sizes the guest itself. It exists to be paired with `podCpuLimit`: KubeVirt copies a declared CPU limit into the request when the VM declares no request, so raising the ceiling alone raises what every worker in the pool asks the scheduler for, and this field is what holds the request still while the ceiling moves. Cannot be combined with `instanceType` sizing, for the same reason as `podCpuLimit`: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.requests.cpu`, so the chart rejects that combination at render time, as it does a value above `podCpuLimit`. Usable without a limit, where it only changes the scheduler's weighting: KubeVirt still derives the ceiling from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so keep the request at or below that count. A request above that derived ceiling is refused only when the worker's Pod is created, and deliberately not at render time: the ceiling exists only where such a ResourceQuota does, so refusing here would take away a configuration that works in a namespace without one. Zero is accepted on its own and reserves nothing, but zero beside a `podCpuLimit` is refused, because KubeVirt cannot tell an explicit zero request from an absent one and replaces both with the limit — which is the outcome this field exists to avoid, arrived at silently. A negative value is refused as well. When unset, KubeVirt computes the request from the guest vCPU count and the cluster CPU allocation ratio. Changing it rolls the pool, because the worker VM template is named by a hash of its content. Keep in sync with the parent kubernetes chart.
// CPU request for the worker VM's virt-launcher `compute` container, which is what the scheduler reserves for a worker on its host and what weights it against neighbours on a contended one. Distinct from `resources.cpu`, which sizes the guest itself. It exists to be paired with `podCpuLimit`: KubeVirt copies a declared CPU limit into the request when the VM declares no request, so raising the ceiling alone raises what every worker in the pool asks the scheduler for, and this field is what holds the request still while the ceiling moves. Cannot be combined with `instanceType` sizing, for the same reason as `podCpuLimit`: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.requests.cpu`, so the chart rejects that combination at render time, as it does a value above `podCpuLimit`. Usable without a limit, where it only changes the scheduler's weighting: KubeVirt still derives the ceiling from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so keep the request at or below that count. A request above that derived ceiling is refused only when the worker's Pod is created, and deliberately not at render time: the ceiling exists only where such a ResourceQuota does, so refusing here would take away a configuration that works in a namespace without one. Zero is accepted on its own and reserves nothing, but zero beside a `podCpuLimit` is refused, because KubeVirt cannot tell an explicit zero request from an absent one and replaces both with the limit — which is the outcome this field exists to avoid, arrived at silently. A negative value is refused as well. When unset, KubeVirt computes the request from the guest vCPU count and the cluster CPU allocation ratio. Changing it rolls the pool, because the worker VM template is named by a hash of its content.
PodCpuRequest resource.Quantity `json:"podCpuRequest,omitempty"`
// List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).
// +kubebuilder:default:={}
Gpus []GPU `json:"gpus,omitempty"`
// Kernel modules loaded on every worker in this pool, emitted as Talos `machine.kernel.modules`. A Talos system extension installs a module but does not load it, so an extension-provided driver needs its modules declared here. Leave unset to let the chart decide: a pool holding at least one `nvidia.com/*` GPU gets `nvidia` (with `NVreg_NvLinkDisable=1`, which the gpu-operator driver container applies on non-Talos workers; on Talos that container has to be turned off or it clashes with the system extension, and nothing then mounts the ConfigMap carrying the parameter), `nvidia_uvm`, `nvidia_drm`, `nvidia_modeset` (that order — Talos loads the list in sequence and the last three depend on the first), and any other pool gets nothing. Set a non-empty list to replace the chart's choice entirely, or `[]` to opt out and emit no modules even on a GPU pool. The field carries no default, and unset is not the same value as `[]`: a generated parameter table that shows `[]` in its Default column is printing what it prints for any array with no default, not the effective value — leaving the field out selects the automatic set, writing `[]` disables it. The module still has to be in the image, and which extensions the image carries is the pool's schematic: a GPU pool points `osImage.factory.schematicID` (or `osImage.builtin.schematicID`) at a schematic with the NVIDIA extensions, and on Blackwell (GB202) that must be the open-kernel-modules extension. The automatic set keys on the `nvidia.com/` resource prefix alone and nothing cross-checks it against that schematic; on Talos v1.13 a declared module the image does not ship leaves its controller retrying rather than failing the boot, so the symptom is a missing driver, not a dead node. This value is part of the worker machine config, whose content hash names the `TalosConfigTemplate`, so changing it rolls every existing worker in the pool rather than only affecting nodes created later. The automatic set is itself such a change, on the upgrade that introduces this field, for every existing pool that holds an `nvidia.com/*` GPU and leaves the field unset: that pool rolls.
// Workers a rollout (instance type, image or version change) may remove before their replacement is up. Unset, it is 0, so each replacement joins before an old worker is drained, and 1 on a pool with `gpus`, whose replacement may need the very GPU the old worker holds. With 0, a rollout needs room for one extra VM: a pool short of quota or capacity for it stalls, with the new Machine Pending, until room is made or `maxUnavailable` is set to 1. Set 1 on a Proxmox pool whose template passes a GPU through: the chart cannot see that GPU, and with 0 the rollout would wait for it forever.
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=2.147483647e+09
MaxUnavailable *int `json:"maxUnavailable,omitempty"`
// Kernel modules loaded on every worker in this pool, emitted as Talos `machine.kernel.modules`. A Talos system extension installs a module but does not load it, so an extension-provided driver needs its modules declared here. Leave unset to let the chart decide: a pool holding at least one `nvidia.com/*` GPU gets `nvidia` (with `NVreg_NvLinkDisable=1`, which the gpu-operator driver container applies on non-Talos workers; on Talos that container has to be turned off or it clashes with the system extension, and nothing then mounts the ConfigMap carrying the parameter), `nvidia_uvm`, `nvidia_drm`, `nvidia_modeset` (that order — Talos loads the list in sequence and the last three depend on the first), and any other pool gets nothing. Set a non-empty list to replace the chart's choice entirely, or `[]` to opt out and emit no modules even on a GPU pool. The field carries no default, and unset is not the same value as `[]`: a generated parameter table that shows `[]` in its Default column is printing what it prints for any array with no default, not the effective value — leaving the field out selects the automatic set, writing `[]` disables it. The module still has to be in the image, and which extensions the image carries is the pool's schematic: a GPU pool points `osImage.factory.schematicID` (or `osImage.builtin.schematicID`) at a schematic with the NVIDIA extensions, and on Blackwell (GB202) that must be the open-kernel-modules extension. The automatic set keys on the `nvidia.com/` resource prefix alone and nothing cross-checks it against that schematic; on Talos v1.13 a declared module the image does not ship leaves its controller retrying rather than failing the boot, so the symptom is a missing driver, not a dead node. This value is part of the worker machine config, whose content hash names the `TalosConfigTemplate`, so changing it rolls every existing worker in the pool rather than only affecting nodes created later.
KernelModules []KernelModule `json:"kernelModules,omitempty"`
// Kubelet resource reservations for this pool.
// Kubelet resource reservations for this pool. An empty reservation is computed from the worker's size: `resources` when set, otherwise the `instanceType`.
// +kubebuilder:default:={}
Kubelet Kubelet `json:"kubelet,omitempty"`
// Stream each worker VM's guest serial console into a `guest-console-log` container beside virt-launcher, readable with `kubectl logs`. The only view of a worker that stalls during boot before Talos apid answers, when neither a Node nor a certificate request exists to diagnose from. Two costs the field name does not show. Changing the value rolls the pool, because it is part of the worker VM template whose name is a hash of its content -- including the stuck VM whose console was wanted, so it buys visibility into the next boot rather than the current one. And it overrides the platform's cluster-wide `disableSerialConsoleLog`, set because that container has been seen holding virt-launcher in `PodInitializing` (kubevirt/kubevirt#15989), which would take the pool down rather than explain it -- so check that virt-launcher Pods reach Running after enabling it.
@@ -62,25 +66,25 @@ type ConfigSpec struct {
// Maximum number of unhealthy nodes tolerated in this pool before remediation is paused. The MachineHealthCheck admission webhook accepts either a bare integer ("0", "1", ...) or a percentage ("0%", "50%"); bare numeric strings are rejected, so the safer default is a percentage. Drop to "0%" once the pool is stable. Watch how a percentage behaves in a small group: the MachineHealthCheck controller scales it against the number of machines the check covers and rounds down, then remediates only while the unhealthy count is at or below that result. At "50%" a group of one therefore never remediates, because half of one rounds down to zero, and a group of two remediates one unhealthy machine but not two.
// +kubebuilder:default:="50%"
MaxUnhealthy string `json:"maxUnhealthy"`
// Maximum time a Machine is allowed to spend reaching the Ready condition before it is remediated. Raise for slow first boots (Talos image fetch from factory.talos.dev or a busy storage class on the kubevirt-csi PVC populator). The MachineHealthCheck measures it from the later of the Machine's creation and its InfrastructureReady transition, so the clock restarts when the infrastructure reports ready: a worker gets this long for the disk image import and the VM to come up, and then this long again for the guest to boot and its kubelet to register with the tenant apiserver. Either leg stretches on a host under load, and a Machine remediated here is deleted before it ever joins, so its replacement starts the same sequence from the beginning.
// Maximum time a Machine is allowed to spend reaching the Ready condition before it is remediated. Raise for slow first boots (the Talos image import from the Image Factory, or a storage class slow to provision or clone the worker disk). The MachineHealthCheck measures it from the later of the Machine's creation and its InfrastructureReady transition, so the clock restarts when the infrastructure reports ready: a worker gets this long for the disk image import and the VM to come up, and then this long again for the guest to boot and its kubelet to register with the tenant apiserver. Either leg stretches on a host under load, and a Machine remediated here is deleted before it ever joins, so its replacement starts the same sequence from the beginning.
// +kubebuilder:default:="20m"
NodeStartupTimeout string `json:"nodeStartupTimeout"`
// How long a worker Node may report `Ready=Unknown` before the MachineHealthCheck remediates the Machine, which for a KubeVirt worker means deleting the VM. `Ready=Unknown` is what the node lifecycle controller writes once it stops hearing from a kubelet, on a grace period of its own, so the wall clock from a guest going quiet to a deleted worker is this value plus that grace period. A guest starved of CPU stops answering long before it is dead, so a value of a few tens of seconds deletes workers that would have come back on their own. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips. Keep in sync with the parent kubernetes chart.
// How long a worker Node may report `Ready=Unknown` before the MachineHealthCheck remediates the Machine, which for a KubeVirt worker means deleting the VM. `Ready=Unknown` is what the node lifecycle controller writes once it stops hearing from a kubelet, on a grace period of its own, so the wall clock from a guest going quiet to a deleted worker is this value plus that grace period. A guest starved of CPU stops answering long before it is dead, so a value of a few tens of seconds deletes workers that would have come back on their own. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips.
// +kubebuilder:default:="5m"
ReadyUnknownTimeout string `json:"readyUnknownTimeout"`
// How long a worker Node may report `Ready=False` before the MachineHealthCheck remediates the Machine. A Node that has registered but has no working CNI yet reports `Ready=False`, so this value is the whole budget a first boot gets for the CNI rollout to reach that node, and a worker deleted mid-rollout takes the rollout with it while its replacement starts over. Raising it delays replacing a worker that is genuinely dead, but not rescheduling its workload: Pods leave an unreachable Node on the node lifecycle controller's taint-based eviction and the toleration that carries, not on this timer. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips. Keep in sync with the parent kubernetes chart.
// How long a worker Node may report `Ready=False` before the MachineHealthCheck remediates the Machine. A Node that has registered but has no working CNI yet reports `Ready=False`, so this value is the whole budget a first boot gets for the CNI rollout to reach that node, and a worker deleted mid-rollout takes the rollout with it while its replacement starts over. Raising it delays replacing a worker that is genuinely dead, but not rescheduling its workload: Pods leave an unreachable Node on the node lifecycle controller's taint-based eviction and the toleration that carries, not on this timer. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips.
// +kubebuilder:default:="10m"
ReadyFalseTimeout string `json:"readyFalseTimeout"`
// How long the cluster-autoscaler waits for a newly created worker to register as a Node before it treats that machine as long-unregistered. Rendered onto this pool's MachineDeployment as the `cluster.x-k8s.io/autoscaling-options-maxnodeprovisiontime` annotation, which overrides the autoscaler's built-in default for this pool only. Two things change when the timer fires, and both make a slow join worse rather than better: the machine stops counting towards the pool's upcoming capacity, so the autoscaler asks for a replacement for capacity that is already on its way, and the machine becomes eligible for removal. Size it above the slowest join a healthy worker can take, counting the Talos image import, the guest boot and the CNI rollout. Accepted as a whole number of seconds, minutes or hours ("30m", "1h30m"), which is narrower than Go's duration syntax on purpose: the autoscaler ignores a value it cannot parse and falls back to its own default without failing, and the narrower shape cannot express a value that overflows the parser or a fraction it rounds to zero. The canonical form a duration serializes back to ("20m0s", "1h0m0s") is accepted. Keep in sync with the parent kubernetes chart.
// How long the cluster-autoscaler waits for a newly created worker to register as a Node before it treats that machine as long-unregistered. Rendered onto this pool's MachineDeployment as the `cluster.x-k8s.io/autoscaling-options-maxnodeprovisiontime` annotation, which overrides the autoscaler's built-in default for this pool only. Two things change when the timer fires, and both make a slow join worse rather than better: the machine stops counting towards the pool's upcoming capacity, so the autoscaler asks for a replacement for capacity that is already on its way, and the machine becomes eligible for removal. Size it above the slowest join a healthy worker can take, counting the Talos image import, the guest boot and the CNI rollout. Accepted as a whole number of seconds, minutes or hours ("30m", "1h30m"), which is narrower than Go's duration syntax on purpose: the autoscaler ignores a value it cannot parse and falls back to its own default without failing, and the narrower shape cannot express a value that overflows the parser or a fraction it rounds to zero. The canonical form a duration serializes back to ("20m0s", "1h0m0s") is accepted.
// +kubebuilder:default:="30m"
MaxNodeProvisionTime string `json:"maxNodeProvisionTime"`
// Kubernetes major.minor version the pool joins. Must not be ahead of the parent cluster's minor (workers may not lead the apiserver); it may lag behind during a rolling upgrade -- bump the parent Kubernetes CR first, then each pool -- and must satisfy the Talos<->Kubernetes support matrix against `talos.version`.
// Kubernetes major.minor version the pool joins. Must not be ahead of the parent cluster's minor (workers may not lead the apiserver); it may lag behind during a rolling upgrade -- bump the parent Kubernetes CR first, then each pool. The render fails when it is ahead. Its pairing with the Talos release the pool resolves (`talos.version`, or the `osImage` override) is checked against the chart's Talos<->Kubernetes support matrix: for a Talos minor the matrix lists, a Kubernetes minor outside that minor's list is refused; a Talos minor the matrix does not list is not checked.
// +kubebuilder:default:="v1.35"
Version Version `json:"version"`
// Worker OS image source for this pool. Default (omitted): import over HTTP from the `talos.*` Image Factory. Set `osImage.builtin` to clone a golden from the opt-in worker image catalog instead. Editing this on an existing pool re-images that pool.
// +kubebuilder:default:={}
OsImage WorkerImage `json:"osImage,omitempty"`
// Talos worker image configuration. Keep in sync with the parent cluster's `talos`.
// Talos worker image configuration for this pool.
// +kubebuilder:default:={}
Talos Talos `json:"talos"`
// Which infrastructure provider backs this pool's worker VMs. `kubevirt` runs them inside this cluster on KubeVirt, sized by `instanceType` and booted from a Talos disk image CDI streams from the image factory. `proxmox` runs them on an external Proxmox VE cluster through capmox, sized by `resources`, and cloned from a Talos VM template that already exists on the hypervisor. The two substrates take different Talos platform images and different sizing inputs, so switching an existing pool is refused: the chart reads the live MachineDeployment's template kind and fails the render rather than rolling every worker to the other hypervisor and deleting the template the draining MachineSet still references. Create a new pool on the other substrate and scale this one down.
@@ -111,13 +115,13 @@ type Kubelet struct {
// Soft eviction threshold for memory (absolute like 1Gi or percentage like 10%).
// +kubebuilder:default:="10%"
EvictionSoftMemory string `json:"evictionSoftMemory,omitempty"`
// CPU reserved for kubelet and container runtime. Auto-computed from instanceType if empty.
// CPU reserved for kubelet and container runtime. When empty, 5% of the worker's CPU, clamped to 50m..500m.
KubeReservedCpu string `json:"kubeReservedCpu,omitempty"`
// Memory reserved for kubelet and container runtime. Auto-computed from instanceType if empty.
// Memory reserved for kubelet and container runtime. When empty, 5% of the worker's memory, clamped to 256Mi..1Gi.
KubeReservedMemory string `json:"kubeReservedMemory,omitempty"`
// CPU reserved for host OS. Auto-computed from instanceType if empty.
// CPU reserved for host OS. When empty, 5% of the worker's CPU, clamped to 50m..500m.
SystemReservedCpu string `json:"systemReservedCpu,omitempty"`
// Memory reserved for host OS. Auto-computed from instanceType if empty.
// Memory reserved for host OS. When empty, 5% of the worker's memory, clamped to 256Mi..1Gi.
SystemReservedMemory string `json:"systemReservedMemory,omitempty"`
}
@@ -160,13 +164,13 @@ type Resources struct {
}
type Talos struct {
// Complete PEM certificate block(s) CDI must trust when importing this pool's worker OS disk image over HTTPS — the no-osImage default and `osImage.factory` path. Empty by default, so the importer uses only its system trust store, which is what the public factory needs. Set this when the factory in use (`imageFactoryURL` or `osImage.factory.imageFactoryURL`) is a self-hosted factory or mirror served by an internal CA, which the importer otherwise rejects with `x509: certificate signed by unknown authority`. The chart renders it into a ConfigMap in the release namespace — CDI requires certConfigMap to sit alongside the DataVolume — and references that from the worker DataVolume's `source.http.certConfigMap`; CDI appends it to the system pool rather than replacing it, so a publicly-trusted endpoint verifies either way. A pool on `osImage.builtin` clones a golden image and builds no HTTP source, so this field has no effect there.
// Complete PEM certificate block(s) CDI must trust when importing this pool's worker OS disk image over HTTPS — the no-osImage default and `osImage.factory` path. Empty by default, so the importer uses only its system trust store, which is what the public factory needs. Set this when the factory in use (`imageFactoryURL` or `osImage.factory.imageFactoryURL`) is a self-hosted factory or mirror served by an internal CA, which the importer otherwise rejects with `x509: certificate signed by unknown authority`. The chart renders it into a ConfigMap in the release namespace — CDI requires certConfigMap to sit alongside the DataVolume — and references that from the worker DataVolume's `source.http.certConfigMap`; CDI appends it to the system pool rather than replacing it, so a publicly-trusted endpoint verifies either way. A pool on `osImage.builtin` clones a golden image and builds no HTTP source, so this field has no effect there. On `substrate: proxmox` it has no effect on the workers either: the ConfigMap is still rendered, but nothing references it.
// +kubebuilder:default:=""
ImageFactoryCA string `json:"imageFactoryCA,omitempty"`
// Default base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Applies only when this pool imports over HTTP — the default and `osImage.factory`, which overrides it via `osImage.factory.imageFactoryURL`. A pool on `osImage.builtin` clones a golden image from the worker image catalog and does not use this field at all. Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash. It is interpolated into the worker DataVolume source URL, so on those HTTP paths it must be a plain `http(s)` URL and the render refuses anything else; a pool on `osImage.builtin` builds no such URL and is not held to it, so blanking this field there is legal.
// Default base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Applies only when this pool imports over HTTP — the default and `osImage.factory`, which overrides it via `osImage.factory.imageFactoryURL`. A pool on `osImage.builtin` clones a golden image from the worker image catalog and does not use this field at all. Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash. It is interpolated into the worker DataVolume source URL, so on those HTTP paths it must be a plain `http(s)` URL and the render refuses anything else; a pool on `osImage.builtin` builds no such URL and is not held to it, so blanking this field there is legal. On `substrate: proxmox` the workers clone a VM template that already carries the OS, so this field has no effect there beyond that format check.
// +kubebuilder:default:="https://factory.talos.dev"
ImageFactoryURL string `json:"imageFactoryURL"`
// OCI repository prefix for the Talos installer image used by the in-guest `talos-reconcile` upgrade Job. Resolved as `<installerRepository>/<schematicID>:<version>` against whatever this pool's `osImage` pins, falling back to `talos.schematicID` and `talos.version` — so the installer matches the OS the pool actually booted. The prefix itself has no `osImage.*` override, so an air-gapped pool needs it pointed at a reachable registry as well. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
// OCI repository prefix for the Talos installer image, written into the worker machine config as `machine.install.image`. Resolved as `<installerRepository>/<schematicID>:<version>` against whatever this pool's `osImage` pins, falling back to `talos.schematicID` and `talos.version` — so the installer matches the OS the pool actually booted. The prefix itself has no `osImage.*` override, so an air-gapped pool needs it pointed at a reachable registry as well. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
// +kubebuilder:default:="factory.talos.dev/installer"
InstallerRepository string `json:"installerRepository"`
// Talos `machine.registries.mirrors` passthrough for worker nodes: a map of upstream registry host to `{ endpoints: [ ... ] }`. Empty by default, so workers pull container images (the Talos `kubelet` image included) directly from the upstream registry. Point a host such as `ghcr.io` at an in-cluster pull-through mirror for air-gapped, rate-limited, or flaky-egress environments so a worker's boot does not depend on live public egress. Talos still falls back to the upstream registry unless a host also sets `skipFallback: true`, so a mirror alone does not enforce air-gap. This value is part of the worker machine config, whose content hash names the TalosConfigTemplate, so changing it rolls every existing worker in the pool rather than only affecting nodes created later.
@@ -175,7 +179,7 @@ type Talos struct {
// Talos image-factory schematic ID. Defaults to the cozystack-tested vanilla schematic. Operators using custom schematics (system extensions, kernel args) override here. The pool can pin its own via `osImage.builtin.schematicID` or `osImage.factory.schematicID`; when `osImage` omits it, this value applies. Whichever of the two applies is interpolated into an image URL and a DataVolume name, so it must be a plain lowercase alphanumeric identifier -- the render refuses anything else. A custom schematic on `osImage.builtin` also needs its own catalog entry, since the golden is named from the schematic ID.
// +kubebuilder:default:="ce4c980550dd2ab1b17bbf2b08801c7eb59418eafe8f279833297925d67c7515"
SchematicID string `json:"schematicID"`
// Default Talos release for this pool's worker OS image and in-guest installer. The pool can pin its own release via `osImage.builtin.version` or `osImage.factory.version`; when `osImage` omits it, this value applies. Both ways in are checked against the chart's Talos<->Kubernetes support matrix, against whichever release the pool actually resolves to, so an override cannot slip past a check `talos.version` would have failed. The matrix rejects a pairing it lists as unsupported; it lists the Talos minors this chart ships and passes any other minor unchecked, so pointing either field at a Talos release the chart does not know about is the operator's call, not a checked one. Whichever release applies is interpolated into an image URL and a DataVolume name, so it must be a full `vMAJOR.MINOR.PATCH` release, optionally with a pre-release suffix -- the render refuses anything else, a minor-only `v1.13` included. The values schema types this as a bare string, so a malformed one is accepted by the CR and fails the pool's HelmRelease instead.
// Default Talos release for this pool's worker OS image and for the installer image written into the worker machine config. The pool can pin its own release via `osImage.builtin.version` or `osImage.factory.version`; when `osImage` omits it, this value applies. Both ways in are checked against the chart's Talos<->Kubernetes support matrix, against whichever release the pool actually resolves to, so an override cannot slip past a check `talos.version` would have failed. The matrix lists, for each Talos minor the chart knows, the Kubernetes minors it supports and refuses a pool `version` outside that list; a Talos minor it does not list passes unchecked, so pointing either field at a Talos release the chart does not know about is the operator's call, not a checked one. Whichever release applies is interpolated into an image URL and a DataVolume name, so it must be a full `vMAJOR.MINOR.PATCH` release, optionally with a pre-release suffix -- the render refuses anything else, a minor-only `v1.13` included. The values schema types this as a bare string, so a malformed one is accepted by the CR and fails the pool's HelmRelease instead.
// +kubebuilder:default:="v1.13.6"
Version string `json:"version"`
}
@@ -195,7 +199,7 @@ type WorkerImageBuiltin struct {
}
type WorkerImageFactory struct {
// Base URL of the Image Factory serving the boot raw image. Defaults to `talos.imageFactoryURL`. No trailing slash. NOTE: this redirects only the worker boot-disk import; the in-guest upgrade installer image is always sourced from `talos.installerRepository`, so an air-gapped pool must also have `talos.installerRepository` pointed at a reachable registry.
// Base URL of the Image Factory serving the boot raw image. Defaults to `talos.imageFactoryURL`. No trailing slash. This redirects only the worker boot-disk import; the installer image in the worker machine config is always sourced from `talos.installerRepository`, so an air-gapped pool must also have `talos.installerRepository` pointed at a reachable registry.
ImageFactoryURL string `json:"imageFactoryURL,omitempty"`
// Talos schematic ID. Defaults to `talos.schematicID`.
SchematicID string `json:"schematicID,omitempty"`
@@ -67,6 +67,11 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
*out = make([]GPU, len(*in))
copy(*out, *in)
}
if in.MaxUnavailable != nil {
in, out := &in.MaxUnavailable, &out.MaxUnavailable
*out = new(int)
**out = **in
}
if in.KernelModules != nil {
in, out := &in.KernelModules, &out.KernelModules
*out = make([]KernelModule, len(*in))
+1 -1
View File
@@ -84,7 +84,7 @@ type Backup struct {
// Cron schedule for automated backups. Ignored when useSystemBucket is true (scheduling moves to the BackupClass Plan).
// +kubebuilder:default:="0 2 * * *"
Schedule string `json:"schedule,omitempty"`
// Opt-in: back up to the platform system bucket without supplying S3 credentials. The cozy-default MongoDB BackupClass injects the storage at BackupJob time; tenants do not set s3AccessKey/s3SecretKey/destinationPath/endpointURL, and backups scope to `<namespace>/<application>`. Scheduling moves to a BackupClass Plan; `schedule`, `retentionPolicy` and PITR are ignored while this is on, and this flow has no retention yet (see docs/operations/backup-classes.md). Requires `backup.enabled: true` (the pbm agents run on that flag); the chart refuses the combination with `enabled: false` at render time; the schema carries the same rule as a marker for the UI and for the day the apiserver enforces CEL on application schemas (cozystack/cozystack#2657), which it does not yet. A release that has not yet rendered this flag (its cluster still carries the chart's scheduled tasks and PITR) holds a BackupJob until it does, so the tenant's own storage is never injected over.
// Opt-in: back up to the platform system bucket without supplying S3 credentials. The cozy-default MongoDB BackupClass injects the storage at BackupJob time; tenants do not set s3AccessKey/s3SecretKey/destinationPath/endpointURL, and backups scope to `<namespace>/<application>`. Scheduling moves to a BackupClass Plan; `schedule`, `retentionPolicy` and PITR are ignored while this is on, and this flow has no retention yet (see https://cozystack.io/docs/v1/operations/services/backup-classes/). Requires `backup.enabled: true` (the pbm agents run on that flag); the chart refuses the combination with `enabled: false` at render time; the schema carries the same rule as a marker for the UI and for the day the apiserver enforces CEL on application schemas (cozystack/cozystack#2657), which it does not yet. A release that has not yet rendered this flag (its cluster still carries the chart's scheduled tasks and PITR) holds a BackupJob until it does, so the tenant's own storage is never injected over.
// +kubebuilder:default:=false
UseSystemBucket bool `json:"useSystemBucket,omitempty"`
}
+2 -2
View File
@@ -58,7 +58,7 @@ type ConfigSpec struct {
// Users configuration map. Passwords are always auto-generated and stored in the `<release>-credentials` Secret; they cannot be set from values — read a user's password from that Secret. A `password` left over in values from before the field was removed is ignored by the render and draws an admission warning, but it is not inert on an upgraded release: the chart preserves whatever password is already in the Secret, which on the first upgrade is the value that was set before removal, so that value stays the live credential until it is rotated (dedicated rotation is tracked in cozystack/community#72). The live password lives only in the Secret.
// +kubebuilder:default:={}
Users map[string]User `json:"users,omitempty"`
// Databases configuration map.
// Databases configuration map. The PostgreSQL system databases `postgres`, `template0` and `template1` are refused.
// +kubebuilder:default:={}
Databases map[string]Database `json:"databases,omitempty"`
// Backup configuration.
@@ -124,7 +124,7 @@ type Backup struct {
// DEPRECATED. See `s3AccessKey`.
// +kubebuilder:default:=""
S3SecretKey string `json:"s3SecretKey,omitempty"`
// Legacy. Cron schedule (CNPG 6-field format) for the chart-emitted ScheduledBackup. Empty means no chart-managed schedule, which is the recommended setup when a `BackupClass` from `backups.cozystack.io` already drives backup orchestration. In the legacy chart-managed flow `spec.plugins` plus the barman-cloud ObjectStore is rendered when `backup.enabled=true` AND `useSystemBucket=false` AND `destinationPath` is non-empty AND inline-or-external creds are supplied; in the platform `useSystemBucket=true` flow the chart skips emitting `spec.plugins` and the CNPG driver SSA-applies the ObjectStore and patches `spec.plugins` onto the live Cluster at first BackupJob time.
// Legacy. Cron schedule for the chart-emitted ScheduledBackup, either a standard five-field cron such as `0 2 * * *` for every day at 02:00, or six fields with leading seconds such as `0 0 2 * * *` for the same time. Empty means no chart-managed schedule, which is the recommended setup when a `BackupClass` from `backups.cozystack.io` already drives backup orchestration. In the legacy chart-managed flow `spec.plugins` plus the barman-cloud ObjectStore is rendered when `backup.enabled=true` AND `useSystemBucket=false` AND `destinationPath` is non-empty AND inline-or-external creds are supplied; in the platform `useSystemBucket=true` flow the chart skips emitting `spec.plugins` and the CNPG driver SSA-applies the ObjectStore and patches `spec.plugins` onto the live Cluster at first BackupJob time.
// +kubebuilder:default:=""
Schedule string `json:"schedule,omitempty"`
// Opt-in: when true, the chart-emitted `<release>-s3-creds` Secret is skipped AND `spec.plugins` (plus the barman-cloud ObjectStore) is left UNSET in the chart-rendered Cluster — the cozy-default BackupClass driver SSA-applies an ObjectStore (carrying destinationPath/endpointURL/credentials) and patches `spec.plugins` on the live Cluster when the first BackupJob runs. Consequence: plugin WAL archiving is NOT active until that first BackupJob fires; WAL accumulates on the PVC in the meantime, so fire an ad-hoc BackupJob immediately after enabling the flag on existing releases. Use together with the platform `cozy-default` BackupClass — tenants do not need to fill `s3AccessKey`/`s3SecretKey` or `destinationPath`/`endpointURL`. The destination path automatically scopes to `s3://cozy-backups/<namespace>/<release>/`.
+13 -2
View File
@@ -17,7 +17,7 @@ type Config struct {
}
type ConfigSpec struct {
// Number of Redis replicas.
// Number of Redis replicas. Three Sentinels watch them, or a single one when `replicas` is 1, since a lone pod has no replica to fail over to.
// +kubebuilder:default:=2
Replicas int `json:"replicas"`
// Explicit CPU and memory configuration for each Redis replica. When omitted, the preset defined in `resourcesPreset` is applied.
@@ -51,7 +51,15 @@ type ConfigSpec struct {
// Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it.
// +kubebuilder:validation:Minimum=0
MinReplicasMaxLag *int `json:"minReplicasMaxLag,omitempty"`
// Sentinel configuration. When `sentinel.resources` is empty and `sentinel.resourcesPreset` is unset, the three Sentinel pods are sized like the Redis pods (`resources` and `resourcesPreset`). An empty `sentinel.resources` map counts as unset, as an empty top-level `resources` does.
// Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers.
Maxmemory resource.Quantity `json:"maxmemory,omitempty"`
// Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset.
// +kubebuilder:default:="noeviction"
MaxmemoryPolicy MaxmemoryPolicy `json:"maxmemoryPolicy"`
// Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save ""` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty.
// +kubebuilder:default:=true
Snapshots bool `json:"snapshots"`
// Sentinel configuration. When `sentinel.resources` is empty and `sentinel.resourcesPreset` is unset, the Sentinel pods are sized like the Redis pods (`resources` and `resourcesPreset`). An empty `sentinel.resources` map counts as unset, as an empty top-level `resources` does.
// +kubebuilder:default:={}
Sentinel Sentinel `json:"sentinel,omitempty"`
}
@@ -80,6 +88,9 @@ type TLS struct {
// +kubebuilder:validation:Enum="no";"optional";"yes"
type AuthClients string
// +kubebuilder:validation:Enum="noeviction";"allkeys-lru";"allkeys-lfu";"allkeys-random";"volatile-lru";"volatile-lfu";"volatile-random";"volatile-ttl"
type MaxmemoryPolicy string
// +kubebuilder:validation:Enum="t1.nano";"t1.micro";"t1.small";"t1.medium";"t1.large";"t1.xlarge";"t1.2xlarge";"t1.4xlarge";"c1.nano";"c1.micro";"c1.small";"c1.medium";"c1.large";"c1.xlarge";"c1.2xlarge";"c1.4xlarge";"s1.nano";"s1.micro";"s1.small";"s1.medium";"s1.large";"s1.xlarge";"s1.2xlarge";"s1.4xlarge";"u1.nano";"u1.micro";"u1.small";"u1.medium";"u1.large";"u1.xlarge";"u1.2xlarge";"u1.4xlarge";"m1.nano";"m1.micro";"m1.small";"m1.medium";"m1.large";"m1.xlarge";"m1.2xlarge";"m1.4xlarge";"nano";"micro";"small";"medium";"large";"xlarge";"2xlarge"
type ResourcesPreset string
+1
View File
@@ -66,6 +66,7 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
*out = new(int)
**out = **in
}
out.Maxmemory = in.Maxmemory.DeepCopy()
in.Sentinel.DeepCopyInto(&out.Sentinel)
}
+3
View File
@@ -74,6 +74,9 @@ type ConfigSpec struct {
}
type Disk struct {
// Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.
// +kubebuilder:validation:Minimum=1
BootOrder *int `json:"bootOrder,omitempty"`
// Disk bus type (e.g. "sata").
Bus string `json:"bus,omitempty"`
// Disk name.
+8 -1
View File
@@ -61,7 +61,9 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
if in.Disks != nil {
in, out := &in.Disks, &out.Disks
*out = make([]Disk, len(*in))
copy(*out, *in)
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.Networks != nil {
in, out := &in.Networks, &out.Networks
@@ -100,6 +102,11 @@ func (in *ConfigSpec) DeepCopy() *ConfigSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Disk) DeepCopyInto(out *Disk) {
*out = *in
if in.BootOrder != nil {
in, out := &in.BootOrder, &out.BootOrder
*out = new(int)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Disk.
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: Apache-2.0
// Package v1alpha1 defines strategy.backups.cozystack.io API types.
//
// Group: strategy.backups.cozystack.io
// Version: v1alpha1
package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func init() {
SchemeBuilder.Register(func(s *runtime.Scheme) error {
s.AddKnownTypes(GroupVersion,
&Kubernetes{},
&KubernetesList{},
)
return nil
})
}
const (
KubernetesStrategyKind = "Kubernetes"
)
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// Kubernetes defines a backup strategy for a tenant Kubernetes application.
// A tenant cluster keeps its state in two places the platform can reach: the
// cluster's key prefix in the tenant's shared etcd DataStore, and host-side
// objects (control-plane PKI Secrets, the application HelmReleases, and the
// DataVolumes that back the tenant's PersistentVolumes). The strategy captures
// the first with a one-shot Job and the second with Velero. The two are taken
// one after another, so a backup is crash-consistent at best.
type Kubernetes struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec KubernetesSpec `json:"spec,omitempty"`
Status KubernetesStatus `json:"status,omitempty"`
}
// +kubebuilder:object:root=true
// KubernetesList contains a list of Kubernetes backup strategies.
type KubernetesList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Kubernetes `json:"items"`
}
// KubernetesSpec specifies the desired behavior of a Kubernetes backup strategy.
type KubernetesSpec struct {
// Template holds a PodTemplateSpec for the Job that dumps the cluster's
// key prefix from its etcd DataStore to object storage, writes it back on
// restore, and deletes the object on cleanup. The Job runs in the
// application namespace. Helm-like Go templates are supported in every
// string field and are rendered against:
// - `.Release.Name` and `.Release.Namespace` — the application's name and
// namespace; `.Release.Prefix` is `kubernetes-`.
// - `.Mode` — `"backup"`, `"restore"` or `"cleanup"`.
// - `.ObjectKey` — the object-storage key of this backup's dump.
// - `.Datastore.Prefix` — the cluster's key prefix, without slashes.
// - `.Datastore.Endpoints` — comma-separated etcd client URLs.
// - `.Datastore.CertificateSecret` — the Secret holding the cluster's
// etcd client certificate (`ca.crt`, `server.crt`, `server.key`).
// - `.Parameters` — the resolved BackupClass strategy parameters.
//
// Stored schemaless for the same CRD-size reason as redis_types.go.
//
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
Template corev1.PodTemplateSpec `json:"template"`
// Velero holds the Velero Backup and Restore specs for the host-side
// objects and volumes. Rendered against `.Application` and `.Parameters`,
// like the Velero strategy.
//
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
Velero VeleroTemplate `json:"velero"`
// CertificateWarningThreshold is how close to expiry a control-plane
// certificate may be before a backup records a warning. Defaults to 720h.
// +optional
CertificateWarningThreshold *metav1.Duration `json:"certificateWarningThreshold,omitempty"`
}
type KubernetesStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
}
+103
View File
@@ -839,6 +839,109 @@ func (in *KafkaStatus) DeepCopy() *KafkaStatus {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Kubernetes) DeepCopyInto(out *Kubernetes) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
in.Status.DeepCopyInto(&out.Status)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Kubernetes.
func (in *Kubernetes) DeepCopy() *Kubernetes {
if in == nil {
return nil
}
out := new(Kubernetes)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Kubernetes) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *KubernetesList) DeepCopyInto(out *KubernetesList) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ListMeta.DeepCopyInto(&out.ListMeta)
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Kubernetes, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesList.
func (in *KubernetesList) DeepCopy() *KubernetesList {
if in == nil {
return nil
}
out := new(KubernetesList)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *KubernetesList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *KubernetesSpec) DeepCopyInto(out *KubernetesSpec) {
*out = *in
in.Template.DeepCopyInto(&out.Template)
in.Velero.DeepCopyInto(&out.Velero)
if in.CertificateWarningThreshold != nil {
in, out := &in.CertificateWarningThreshold, &out.CertificateWarningThreshold
*out = new(v1.Duration)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesSpec.
func (in *KubernetesSpec) DeepCopy() *KubernetesSpec {
if in == nil {
return nil
}
out := new(KubernetesSpec)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *KubernetesStatus) DeepCopyInto(out *KubernetesStatus) {
*out = *in
if in.Conditions != nil {
in, out := &in.Conditions, &out.Conditions
*out = make([]v1.Condition, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesStatus.
func (in *KubernetesStatus) DeepCopy() *KubernetesStatus {
if in == nil {
return nil
}
out := new(KubernetesStatus)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *MariaDB) DeepCopyInto(out *MariaDB) {
*out = *in
+1 -1
View File
@@ -31,7 +31,7 @@ Tenants reference `cozy-default` from `BackupJob`/`Plan`/`RestoreJob`; they neve
The platform-managed flow is layered *on top of* — not in place of — the core API. Custom `BackupClass`/Strategy CRs targeting other buckets continue to work; tenants picking `cozy-default` simply opt into the platform's pre-wired stack.
**Open gap (FoundationDB):** the `cozy-default-foundationdb` Strategy CR is shipped but **not** bound by `cozy-default`. Restore for FoundationDB runs `fdbrestore` from inside the `cozy-foundationdb-operator` deployment, which does not yet mount `cozy-backups-creds` with the blob credentials file. Admins who want platform-default FDB backups today must either wire the operator deployment manually or use a custom BackupClass — see [Backup Classes](../../../docs/operations/backup-classes.md) for the follow-up plan.
**Open gap (FoundationDB):** the `cozy-default-foundationdb` Strategy CR is shipped but **not** bound by `cozy-default`. Restore for FoundationDB runs `fdbrestore` from inside the `cozy-foundationdb-operator` deployment, which does not yet mount `cozy-backups-creds` with the blob credentials file. Admins who want platform-default FDB backups today must either wire the operator deployment manually or use a custom BackupClass — see [Backup Classes](https://cozystack.io/docs/v1/operations/services/backup-classes/) for the follow-up plan.
**Tenant visibility:** projected Secrets carry no `internal.cozystack.io/tenantresource` label, so the lineage-controller webhook does not promote them to a `TenantSecret` view. Combined with the default RBAC (no core/v1.Secret verbs on tenant ServiceAccounts), the projected credentials are unreadable from a tenant kubeconfig even when they sit in the tenant's own namespace; the operator Pods mount the Secret via kubelet, which bypasses tenant RBAC.
+8 -1
View File
@@ -37,7 +37,14 @@ type BackupArtifact struct {
// For example: s3://bucket/prefix/file.tar.gz
URI string `json:"uri"`
// SizeBytes is the size of the artifact in bytes, if known.
// SizeBytes is the stored size in bytes of the files this backup owns,
// if known: the dump of a MariaDB backup, for example, or the base backup
// of a CNPG one, which leaves out the WAL archive the server's backups
// share and a point-in-time restore also reads. Strategies that cannot
// attribute stored bytes to a single backup, such as Velero's, whose
// repository deduplicates across backups, leave it unset. Drivers never
// write 0 for a size they cannot measure, so an absent value means
// "unknown", not "empty".
// +optional
SizeBytes int64 `json:"sizeBytes,omitempty"`
+4
View File
@@ -39,6 +39,10 @@ type WorkloadMonitorSpec struct {
// that is not operational because a DataVolume it selects is not ready.
const WorkloadMonitorReasonDataVolumeNotReady = "DataVolumeNotReady"
// WorkloadMonitorReasonBucketClaimNotReady is the status Reason of a monitor
// that is not operational because a BucketClaim it selects is not provisioned.
const WorkloadMonitorReasonBucketClaimNotReady = "BucketClaimNotReady"
// WorkloadMonitorStatus defines the observed state of WorkloadMonitor
type WorkloadMonitorStatus struct {
// Operational indicates if the workload meets all operational requirements
-226
View File
@@ -1,226 +0,0 @@
# backport-audit
Answers one question, before a release or an rc is cut: did everything labelled for backport actually land on the release branch? It prints the URLs of the ones that did not, and exits non-zero, so it can gate the cut.
The manual alternative is `gh pr list --search "is:merged label:kind/backport"`, which lists every PR ever labelled and says nothing about whether the change reached the branch. Answering that by hand means reading the branch history per PR, which is where backports quietly go missing.
```console
$ go run ./cmd/backport-audit release-1.5
=== release-1.5 === 43 candidate PRs: MISSING=2 pending=3 dropped=5 backported=33
MISSING -- labelled for this line, no trace of it here (2):
https://github.com/cozystack/cozystack/pull/3449
#3449 ci(release): move release validation to rc time — e2e lane, changelog button, promote gate, docs at promote
label=kind/backport-previous author=myasnikovdaniil merged=2026-07-29 -- no backport PR, nothing on branch
```
The URL is always the **original PR on main** — the thing you decide about. The evidence line names the backport PR when one exists. A labelled PR already on the branch is counted in the header and never listed, so silence under the outstanding headings means done. Two more sections, described in [Backports the labels do not account for](#backports-the-labels-do-not-account-for), start from the backport PRs on the branch instead of from labels: `DUPLICATE`, which is outstanding, and `UNLABELLED`, which is informational.
## Usage
```bash
go run ./cmd/backport-audit # the two newest release lines
go run ./cmd/backport-audit release-1.5 # one line
go run ./cmd/backport-audit release-1.6 release-1.5 --json
```
Requires `git` and an authenticated `gh` on PATH. The audit does not modify GitHub or working-tree files. Unless `--no-fetch` is set, it updates local Git refs and objects while fetching.
| Flag | Meaning |
|------|---------|
| `--remote NAME` | Git remote holding the release branches (default `origin`) |
| `--limit N` | Max merged PRs scanned per label spelling (default 1000, which is also the ceiling — see [Exit code](#exit-code)) |
| `--no-fetch` | Skip `git fetch`; both main and release refs must be current so shared history stays out of backport evidence |
| `--json` | Machine-readable output |
| `--no-color` | Disable color (auto-disabled on a non-TTY and under `--json`) |
Arguments and flags may be given in any order.
## Exit code
`0` when nothing is outstanding, `1` when something is, `2` when the audit could not be completed. Outstanding means a `MISSING`, `partial`, `unverified`, `pending` or `dropped` verdict, or any `DUPLICATE` entry; `UNLABELLED` entries never move the exit code. That is the point of the tool, so it holds in `--json` mode too:
```bash
go build ./cmd/backport-audit
./backport-audit release-1.5
case $? in
0) echo "clean" ;;
1) echo "do not cut yet" ;;
*) echo "the audit itself failed; no answer yet" ;;
esac
```
Use the built binary (`go build` as above, or `go install ./cmd/backport-audit`) wherever the exit code matters. `go run` does not preserve it: any non-zero exit of the program comes back as `go run`'s own `1`, after an `exit status N` line on stderr, so under `go run` an audit that could not be completed looks exactly like one that found outstanding work. `go run` is fine for reading the report.
Exit `2` covers the cases where an answer cannot be trusted rather than merely being bad news, and a saturated listing is one of them: `gh` truncates at `--limit` in silence, and a truncated list does not make the audit partial, it makes it wrong — the PRs past the cut are reported nowhere and the exit code says clean. Each of the three listings is checked against its cap and fails instead, naming the cap to raise. The label listings go through GitHub search, which never returns more than 1000 results however far it is paginated, so they are checked against the lower of `--limit` and 1000: a `--limit` above 1000 cannot make a listing cut at 1000 pass for complete. A git read that a verdict depends on is the other case: when one fails, a merge commit missing from the local repository for instance, the audit stops with `2` instead of judging the PR without it.
## Statuses
| Status | Meaning | Action |
|--------|---------|--------|
| `in-branch` | The PR merged before the branch was cut, so its change is already there | none, no backport was ever needed |
| `backported` | Every commit of the PR is on the branch, or, for a PR of a single commit, a backport PR merged | none |
| `confirmed` | Would be `partial` or `unverified`, but a maintainer attested the backport complete on the merged backport PR; the report names who and links the comment | none |
| `partial` | Some of the PR's commits are on the branch and some are not, listed with SHA and subject — a merged bot conflict draft, which stops at the first commit that did not apply, or a backport squashed by hand | backport the missing commits, or [attest the backport complete](#attesting-a-backport-complete) |
| `unverified` | A backport PR merged, but none of the PR's several commits is on the branch under its own SHA, `-x` reference or subject — typically a backport squashed or reworded by hand | check it, then attest it complete or backport what is missing |
| `pending` | A backport PR exists and is still open, including the drafts the bot opens with the conflict committed | merge it, or finish the draft |
| `dropped` | A backport PR was closed unmerged, reported with whatever reason someone left on it | none if the reason still holds |
| `MISSING` | No backport PR ever existed and nothing on the branch matches | cherry-pick it by hand, or drop it deliberately |
`dropped` is usually healthy — a maintainer deciding a fix does not apply to that line, e.g. because the feature it repairs never shipped there. The recorded reason is printed so the next release does not re-open the same investigation. A `dropped` entry reading `no reason recorded` is the one that needs a human.
## Backports the labels do not account for
The verdicts start from labels, so on their own they cannot see a backport of a PR nobody labelled. Two further sections start from the other side: every PR on the release branch, and the originals it names by the bot's head branch or in a `Backport of` phrase (see [How landing is established](#how-landing-is-established)). The header carries their counts after a `|`, e.g. `backported=42 | DUPLICATE=3 unlabelled=13`.
`UNLABELLED` lists each original that backport PRs on the branch claim although it is not a candidate for the branch, with every backport PR claiming it and that PR's state. Each entry also says what those PRs amount to: `backport PR merged` once one of them merged, `claimed here, not merged` while the only live one is still open, and `claimed here, closed unmerged` when every one was closed. A merged claim is only a merged PR, since an unlabelled original is not checked commit by commit; it reads `backport PR merged, confirmed by @login` when the PR carries the [attestation marker](#attesting-a-backport-complete). Most are hand backports of PRs that never carried a label, merged on purpose. An original that does carry a backport request, one that resolved to a different line at merge time, is listed with its labels. The section is informational and never moves the exit code: the gate answers whether everything labelled landed, and an unlabelled backport can only add to a branch, never leave a labelled change off it.
`DUPLICATE` lists each original claimed by more than one live backport PR, labelled or not:
| Kind | Meaning | Action |
|------|---------|--------|
| `open-twice` | Two or more backport PRs are open for it — typically the bot's conflict draft next to a hand backport, or a fork PR next to its reopening from a branch in this repository | merge one, close the rest |
| `open-after-merge` | A backport already merged and another one is still open | close the leftover, or merge it if it is the second half of a split backport |
Both kinds make the audit exit `1`. Neither reaches a verdict, which judges the commits on the branch and names at most one open backport PR, and in both a human has to decide what happens to the open PR before the cut. A closed PR next to an open one is the normal shape of a redone backport and is not flagged, and neither are two merged ones, which are history the branch already carries.
```console
DUPLICATE -- one original, more than one live backport PR (3):
https://github.com/cozystack/cozystack/pull/3936
#3936 fix(rabbitmq): right-size the default resources preset to s1.nano
label=kind/backport -- a backport PR still open after another one merged
backport #4372 OPEN (draft) https://github.com/cozystack/cozystack/pull/4372
backport #4393 MERGED https://github.com/cozystack/cozystack/pull/4393
UNLABELLED -- backport PRs here for originals not labelled for this line (13, informational):
https://github.com/cozystack/cozystack/pull/4280
#4280 fix(kafka): keep the pre-delete hook's credentials as long as its Job
backport PR merged
backport #4421 CLOSED https://github.com/cozystack/cozystack/pull/4421
backport #4456 MERGED https://github.com/cozystack/cozystack/pull/4456
```
The titles of unlabelled originals are not in any listing the audit already makes, so they come from one GraphQL request covering the whole run. If it fails, the report goes out without them and says so on stderr; the URLs are derived locally and the verdicts and exit code do not depend on it.
## How a PR is matched to a release line
A `kind/backport` label does not name a branch. The audit selects merged PRs that currently carry a backport label, then assigns each to the release line selected at its merge time: `kind/backport` means the current line, and `kind/backport-previous` means the previous line. A label added after merge still makes the PR a candidate. [`backport.yaml`](../../.github/workflows/backport.yaml) also runs when a label is added and selects its target at that time, so a late label can make the audit and the bot disagree; see [Limits](#limits).
What "the current line" means changed once, so the audit carries both rules and picks by the PR's merge date:
| PR merged | rule `backport.yaml` was running | a line exists from |
|-----------|----------------------------------|--------------------|
| before 2026-08-03 | `getLatestRelease` | its first non-prerelease release publishing |
| on or after 2026-08-03 | the two newest existing `release-X.Y` branches | its `release-X.Y` branch being pushed |
The cutover is a single moment because both halves landed in one push: 53a7fc4dc made cutting an rc freeze the line into `release-X.Y`, and 6ff5b98d5 pointed the bot at the branch list. Before that a release branch was created at promote time, so the newest branch and the newest published stable named the same line and the two rules could not disagree. After it they disagree for the length of every freeze window — `release-X.Y` is created when the first `vX.Y.0-rc.N` is cut, potentially weeks before `vX.Y.0` publishes, and everything labelled in between belongs to the line being stabilised. That window is exactly when a backport has to reach the branch, because the frozen branch is the only way into the release.
The audit estimates branch opening from the earliest `vX.Y.0-rc.N` tagged commit dated on or after the cutover. It ignores earlier tags individually; a line with no remaining tag falls back to its first stable release date. [`cut-prerelease.yaml`](../../.github/workflows/cut-prerelease.yaml) creates the branch at the tagged commit, but the commit timestamp is not the branch-push timestamp. PRs merged in that interval can be assigned to the new line before the bot could target it. The rc release's `publishedAt` is later still, after the build finishes.
Lines are then ranked by version and never by when they opened, matching the numeric descending sort the workflow applies to its branch list. The two orders come apart as soon as a freeze overlaps the previous line's stabilisation, and version order is also what keeps `release-1.10` above `release-1.9`.
The labels themselves were namespaced under `kind/` in 7b71053a0, which renamed them in place via label-sync aliases, so every historical PR reads back under the new name. The bare `backport` and `backport-previous` are still queried alongside them, because the org-level dosubot goes on applying the old spelling to new PRs — the same transitional allowance [`backport.yaml`](../../.github/workflows/backport.yaml) makes, and it retires at the same time. A PR found under either spelling is reported under the `kind/` one.
## How landing is established
Evidence is weighed per commit, because a commit is what a backport can lose:
1. **Reachability.** The PR's merge commit is reachable from the release branch, i.e. it merged before the branch was cut (or `main` was later merged in). Nothing was ever needed: `in-branch`.
2. **A linked backport PR.** Found by the bot's head branch `backport-<N>-to-release-X.Y`, or by a `Backport of #N` reference in the body, which is what a hand-written backport carries. It never outranks the commits (item 3): a merged backport PR with one of the PR's commits missing is `partial`, because the bot's conflict drafts stop at the first commit that does not apply and drop the rest, so a merged draft looks finished at PR level. `MERGED` settles it alone only for a PR of a single commit — including on a `--no-fetch` run whose local refs predate the backport's merge. For a PR of several commits, none of them found and a merged backport PR is `unverified`. `OPEN` is `pending`; `CLOSED` is `dropped`.
A backport carrying several changes links to every original its `Backport of` phrase names: a list (`Backport of #3938 and #4280`, `Backport of #1, #2, and #3`), and the `Backport of #4253 to release-1.6, together with #3460` form used for a dependency pulled along. Only that phrase is read, so the issue a backport fixes or a CI run it cites further on is never taken for an original. A list counts in full only when it visibly ends — at the end of the line or the sentence, or where the `to release-X.Y` clause starts. One that runs on into anything else (`Backport of #10, #20 is not included`) may be saying something about its later items, so only its first reference counts. A reference qualified with this repository (`cozystack/cozystack#N`, any case) counts like a bare `#N`, and one qualified with any other repository is skipped, because reading `other/repo#20` as local #20 would let an unrelated backport vouch for whichever local PR carries that number.
3. **The branch's own history, commit by commit.** Every commit the PR contributed has to be on the branch; merge commits and commits that change nothing are skipped, since the backport bot drops both. A commit counts when it is reachable, when an `-x` cherry-pick reference names it, or when a commit with its subject is there. Subject and `-x` evidence comes only from commits on the release branch that are not reachable from `<remote>/main`; shared history still counts for direct reachability. Subjects are matched one to one: two commits with the same subject, `fix tests` say, need two branch commits of that name, and a branch commit whose `-x` reference names any other commit than the PR's merge commit is evidence only through that reference. Nothing weaker is taken: not an `-x` reference to the PR's merge commit, which survives a cherry-pick later amended to drop a commit, and not matching lines, which an unrelated line of the same text satisfies. A wrong `backported` is the one answer a release gate must not give, while a false alarm costs a look and can be settled by an attestation. All commits is `backported`, some is `partial`. This is also what catches a hand backport nobody linked.
### Attesting a backport complete
Some complete backports cannot be shown commit by commit, most often a fork contributor's backport squashed into one commit that carries only the first commit's subject. A maintainer who has checked such a backport can say so on the merged backport PR, with a comment that consists of this single line and nothing else:
```text
backport-audit: complete
```
That turns the candidate's `partial` or `unverified` into `confirmed`, which does not fail the gate; the report names the author, links the comment, and keeps listing the commits nothing on the branch names.
The marker is an explicit attestation by whoever wrote it, never something the audit infers. It counts only when it is the whole comment: exactly `backport-audit: complete`, in that case, starting at the beginning of its line, with nothing but blank lines before it and nothing but whitespace after it. Any other text makes the comment something other than an attestation, so a sentence such as "do not post backport-audit: complete yet", a quote or a code block never counts, and neither does the marker with an explanation next to it; post the explanation as a separate comment. It counts only on a backport PR that merged, since an open or closed one put nothing on the branch to vouch for. And it counts only from someone with a hand in the repository: the comment's GitHub author association has to be `OWNER`, `MEMBER` or `COLLABORATOR`, and comments from automation are ignored whatever their association — logins ending in `[bot]`, and the review and dependency bots gh reports without that suffix, such as `coderabbitai`, `dependabot`, `renovate` and `copilot`. A review bot quoting the marker, or a contributor vouching for their own backport, confirms nothing.
## Machine-readable output
```bash
# URLs of what never landed, ready to paste into a tracking issue
go run ./cmd/backport-audit --json release-1.5 | jq -r '.[].candidates[] | select(.status=="MISSING") | .url'
# every open backport PR the audit links, labelled or not, to merge or close, across lines
go run ./cmd/backport-audit --json release-1.6 release-1.5 \
| jq -r '[.[] | (.candidates[], .unlabelled[]) | .backport_prs[] | select(.state=="OPEN") | .url] | unique[]'
# dropped items with the reason someone recorded
go run ./cmd/backport-audit --json release-1.4 \
| jq -r '.[].candidates[] | select(.status=="dropped") | "#\(.number)\t\(.reason)"'
# commits a partial backport still lacks, one per line
go run ./cmd/backport-audit --json release-1.6 \
| jq -r '.[].candidates[] | select(.status=="partial") | "#\(.number)\t\(.missing_commits[] | "\(.oid[0:9]) \(.subject)")"'
# open backport PRs competing for one original, to settle before the cut
go run ./cmd/backport-audit --json release-1.6 \
| jq -r '.[].duplicates[] | "#\(.number) \(.kind): \([.backport_prs[] | select(.state=="OPEN") | .url] | join(" "))"'
```
Output is an object keyed by branch, each holding three arrays: `candidates`, `unlabelled` and `duplicates`, each always present and possibly empty. `partial`, `unverified` and `confirmed` candidates carry `missing_commits`, each an `oid` and a `subject`, and a `confirmed` one also carries `confirmation`: `by`, `url` and `backport_pr`. An `unlabelled` record carries the same `confirmation` when its merged backport PR has the marker. A `candidates` record:
```json
{
"number": 3963,
"title": "fix(clickhouse): scheme the backup S3_ENDPOINT on the system-bucket flow",
"url": "https://github.com/cozystack/cozystack/pull/3963",
"author": "androndo",
"mergedAt": "2026-08-28T11:05:15Z",
"label": "kind/backport",
"backport_prs": [{"number": 3987, "state": "OPEN", "url": "..."}],
"status": "pending",
"evidence": "backport PR #3987 open"
}
```
An `unlabelled` record, where `labels` lists the backport requests the original carries for other lines and is empty when it carries none:
```json
{
"number": 4280,
"title": "fix(kafka): keep the pre-delete hook's credentials as long as its Job",
"url": "https://github.com/cozystack/cozystack/pull/4280",
"labels": [],
"backport_prs": [
{"number": 4421, "state": "CLOSED", "url": "..."},
{"number": 4456, "state": "MERGED", "url": "..."}
]
}
```
A `duplicates` record, where `label` is the request the original was audited under on this line and is empty when it is not a candidate here:
```json
{
"number": 4254,
"title": "feat(kubevirt): expose migration configuration through platform values",
"url": "https://github.com/cozystack/cozystack/pull/4254",
"label": "kind/backport",
"kind": "open-twice",
"backport_prs": [
{"number": 4322, "state": "OPEN", "url": "...", "draft": true},
{"number": 4339, "state": "OPEN", "url": "..."},
{"number": 4402, "state": "OPEN", "url": "..."}
]
}
```
## Limits
A `MISSING`, `partial` or `unverified` verdict is a prompt to check, not proof of absence. A hand backport squashed or reworded without `-x` reads as `partial` or `unverified` until someone attests it complete, and as `MISSING` when nothing links it to its original; confirm at diff level before redoing the work. And the audit reports *that* something is missing, never *why* — a run of `MISSING` entries clustered in time usually means the bot itself was failing during that window, which is worth checking before cherry-picking them one by one.
The audit reads labels as they stand when it runs. A label added afterwards is seen only by the next run, and until then its PR is either absent or, if someone already backported it, listed as `UNLABELLED` rather than audited, so run it right before the cut rather than once at the start of the day.
A label is always read against the release lines as they stood when the PR merged, however much later the label was added, because nothing the audit lists records when that happened. A PR merged on 2026-07-01 and labelled `kind/backport` a month later, after `v1.6.0` published, is audited as a `release-1.5` candidate. [`backport.yaml`](../../.github/workflows/backport.yaml) runs again when the label is added and resolves its targets at that moment, so it opens the backport against `release-1.6`. When the line has moved on between the merge and the label, the two therefore disagree: the audit expects the change on the merge-time line and reports it `MISSING` there unless it arrived some other way, and lists the bot's backport under `UNLABELLED` on the newer one. `TestCandidateLabel` pins the audit's side of this.
A backport PR that names its original neither by the bot's head branch nor in a `Backport of` phrase links to nothing, so it is invisible to both cross-checks: it is not listed as `UNLABELLED` and does not count towards a `DUPLICATE`. For a labelled original the branch history can still prove the change landed, through an identical commit subject or an `-x` cherry-pick reference. For an unlabelled one those are not read at all, so a backport of an unlabelled PR that only a cherry-pick trailer points to, or nothing does, appears nowhere in the report.
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+151
View File
@@ -0,0 +1,151 @@
# backport-batch
Builds the automatic backport batch for a release line and gates a patch cut on it. What goes into a batch is decided by the rules of the [backport policy](../../docs/release/backports.md) alone; this page describes how the tool applies them and does not restate them.
## Usage
```bash
go build ./cmd/backport-batch
./backport-batch run --line release-1.7 [--onto SHA] [--date YYYY-MM-DD] [--no-workflow-files] [--push] [--policy REF]
./backport-batch run --line release-1.7 --continue [--no-workflow-files] [--push] [--policy REF]
./backport-batch run --line release-1.7 --defer 4321[,4322] --reason "fails the e2e of the app" [--push] [--policy REF]
./backport-batch run --line release-1.7 --drop 4321[,4322] --reason "builds on the 1.8 storage rework" [--push] [--policy REF]
./backport-batch run --line release-1.7 --undrop 4321[,4322] [--push] [--policy REF]
./backport-batch check --line release-1.7 [--cut] [--policy REF]
./backport-batch fold --line release-1.7 --original 4321 --commit SHA [--policy REF]
./backport-batch verify --line release-1.7 [--head SHA]
./backport-batch notify --line release-1.7 [--mention org/team|login] [--push]
./backport-batch report --pr 5246 [--id ID] [--json]
./backport-batch lines [--policy REF]
```
[`hack/backport-batch-scheduled.sh`](../../hack/backport-batch-scheduled.sh) runs `lines`, then `run --continue --push`, `check` and `notify` for each line; [`backport-batch.yaml`](../../.github/workflows/backport-batch.yaml) runs it on schedule as the cozystack-ci App, with `--no-workflow-files`, which it hands to `run`.
Requires `git` and an authenticated `gh` on PATH, and a remote called `origin`. Every command but `notify` and `report` fetches `origin` first; `notify` reads only GitHub, and `report` reads GitHub and, for a report that keeps drops, the clone's history. The batch runs only for the lines `lines` prints.
`run`, `check`, `fold` and `lines` read the `backport-policy` block of the policy page from `origin/main`, or from the ref `--policy` names, which is how a change to the page is tried before it merges; `verify` and `notify` do not read it and take no `--policy`. A line the block lists takes nothing merged into `main` before the day it gives, unless the original carries `backport/include`; any other line starts at its fork point. A page without exactly one readable block stops the run with exit 2.
Build the binary wherever an exit code matters: `go run` turns any non-zero exit into its own `1`, so under it a check that could not be completed looks the same as one that found something missing.
Output is written before a push or a publish. Once a write to stdout or stderr fails, a closed pipe included, the run changes nothing more: no push, no GitHub write, no local branch move, and no new pick; a pick already in flight still finishes its rollback. It exits 2 where it would have exited 0, saying on stderr, while it still can, that the output was lost. A confirmation lost after a push or a publish fails the run the same way.
### run
Switches the current working tree to `backport-batch/<line>-<yyyymmdd>`, created at `origin/<line>`, and picks onto it one original at a time, in the order they merged into `main`, as one unit each on the branch's first-parent line. A pull request merged into `main` with a merge commit `M` goes on as its own commits, `git rev-list --reverse --topo-order --no-merges M^1..M^2`, each picked with `git cherry-pick -x --signoff`, so each keeps its author, message and sign-offs and gains the `(cherry picked from commit …)` line; the merges that brought `main` into its branch are left out, since `main` already has what they brought. A merge commit then joins the picks to the batch: its first parent is the batch before the original, its second the last pick, and its message is `M`'s with a paragraph of its own, `Backported-from: M`. A pull request squash-merged into `main` goes on as one pick. When any commit of an original conflicts, the pick is aborted, the branch goes back to where it was before that original, and the original is recorded with the commit that failed and its files; the run goes on with the next one. A commit the branch already carries is skipped as empty, and an original none of whose commits changes anything is reported as carried. The tree has to be clean. A local branch of that name is reset to where the run starts; when that drops commits only it has, the run first prints its tip and the `git branch` command that restores them, and so does `--defer`. Signing follows your git configuration; hooks are off for the picks and the merge, so each commit carries its message as git writes it.
The pull request body goes to stdout, built from the line's own `.github/PULL_REQUEST_TEMPLATE.md`: the originals on the branch, each with how many commits it brought and which of them differ from the commit they were picked from by `git patch-id`, a resolution or a pick git had to fit, the deferred conflicts with the commit that failed and its files, what is parked and why, the skipped pull requests counted by rule with the lists collapsed, the originals' release notes, and two blocks of JSON. An original whose unit differs from it, a pick whose patch-id differs from its commit's or a unit whose net patch-id differs from the original merge's, as when a first commit came out empty because the line already had that change, keeps its release note out of the release-note block: it goes under "Check these notes", with a `git range-diff --creation-factor=999 <commit>^! <pick>^!` for each pick that differs, which pairs the two however much the pick lost, and the `git diff` of the original's merge and of its unit, and the record and the report mark it `changed`. The two blocks of JSON are the [line's record](#the-lines-record-and-reports) in `backport-batch`, which later runs, `check` and `notify` act on, and the run's report in `backport-batch-report`. The title is `fix(backport): batch for <line>, <date>`. A body that would pass GitHub's 65536-byte limit leaves out its longest skipped lists, and the run prints them on stderr instead; one that what it has to carry still puts over the limit is refused before anything is pushed.
`--onto SHA` continues a batch already pushed instead of starting a new one. SHA has to be the head the remote has for a batch branch of the line right now, read with `git ls-remote`, so a head someone pushed onto since is refused. What that branch carries stays, and only what it does not carry yet is picked on top, so the next push is a fast-forward. `--continue` takes SHA from the open batch pull request, and starts a new batch when none is open, which is what the scheduled run does.
`--no-workflow-files` is for a token that may not push a change to `.github/workflows/`, which the scheduled run's App token may not. An original that changes anything there is not picked: the body lists it under "Waiting for a maintainer" with those files, and the `backport-batch` block records it with `"workflows": true`, so `check`, `notify` and a run that continues the batch know it waits. It is never a conflict. A run without the flag, a maintainer's with their own token, picks it like any other original, onto the open batch when it continues one.
A clean pick can still carry only part of its original: git deletes a file the line never had without a word, and leaves alone a file the line lacks. So every path the original modifies or deletes, `M` or `D` in `git diff --name-status --no-renames M^1 M`, is judged at its counterpart on the line: the path itself, or the one a rename `git diff -M` finds between the line's fork point and `M^1` on `main`, between the fork point and the line, or both in turn. A modified path counts as left without effect when its counterpart exists neither before nor after the pick; when the pick leaves an existing file as it was, the file counts as there only when its blob is the original's, binary files included, or when `git merge-file` of the original's preimage, the line's file and the original's postimage merges cleanly back into the line's file. A deleted path counts as left without effect when its counterpart is absent before a pick that changes something, a deletion the line already made being no change of that pick, or still there after it, whatever the pick changed in it; when the original adds that path itself, as when the line made the same rename, it is judged as a modified file against what the original makes of that path. Picks that change nothing carry the original only when the line already has what it does to every file it modifies or deletes, so a change made where its branch merged `main`, which no pick brings, is still missing. What cannot be judged, a mode or type change, content `merge-file` refuses, or a rename git skipped looking for, is shown with the original and never counts against it. An original whose net merge onto the line, `git merge-tree --merge-base=M^1`, leaves something without effect is not tried, and one whose picks do is taken back off the branch: either is parked as partial, listed under "Parked" with those paths, and its release note stays out of the body. A conflict is never called partial. `backport/include` lifts no park.
Before it tries an original, `run` looks at what the original [builds on](#what-an-original-builds-on) that the branch lacks, in merge order, so what became of each prerequisite in this run is known. A prerequisite the line or the open batch carries, or that this run picked or found empty, counts as there; once earlier picks moved the branch, the original is judged at the branch's head, so a line another unit brought counts too. Of what is left, a direct push, an original rules 2 to 4, the line's start, `backport/skip` or a drop keep out, and one that is itself parked never come: the original is parked, not tried, and listed under "Parked" with what it builds on. In the scheduled mode an original that changes a workflow, whose missing dependencies that never come are only tests or docs and whose workflow lines build on nothing missing, waits for a maintainer instead, with a note saying so. Prerequisites still to come, a conflict, a deferral, a wait for a maintainer, one rule 6 holds, make the original wait: it is listed under "Waiting on another original", recorded with `needs`, the prerequisites of the kind that decides, in the order not on the line, waiting for a maintainer, deferred, rule 6, and tried again by a later run. A reason given with `--defer` comes before all of this, and a partial park before any of it.
`--defer N[,N] --reason TEXT` takes originals out of the open batch pull request, usually the ones a failing check was pinned on. It builds the batch branch again off `origin/<line>` without their units: while the batch still sits on the line's head, the units before the first one it drops stay as they are, and every later one, a pushed resolution included, is picked again in its own shape, a merge's picks one by one and then the merge with its message. When dropping one leaves a later commit that no longer applies, the run stops and names that commit's original, to defer as well. It adds no new original. It also takes out an original that got `backport/skip` after the batch picked it, which a run never removes on its own and the body lists as still on the branch. The body records each deferred original with the reason in the `backport-batch` block; a run that continues this batch keeps them out, and the next batch tries them again. A batch that carries a merge commit other than an original's, as GitHub's update-branch button makes, is refused, since picking such a merge again would lose what it resolved. When it takes out everything the batch carries, there is nothing left to push, and `--push` closes the batch pull request instead, as [the line's record](#the-lines-record-and-reports) describes.
`--drop N[,N] --reason TEXT` drops originals from the line: where `--defer` holds one out of this batch, a drop keeps it out of every later batch of the line too, until `--undrop N[,N]` withdraws it. A drop is recorded in the [line's record](#the-lines-record-and-reports), so both need an open batch pull request that carries one, and exit 2 without it; once a batch merges, its drops are carried into the next batch's record. A dropped original the open batch carries is taken off it as `--defer` would, and the rebuild is pushed the same way; any other drop or undrop changes only the record, so the batch stays as it is even when the line moved since. An undropped original is missing until the next run tries it. A drop names the original's merge commit on `main` as well as its number, and a record whose drop does not match what that merge commit merged stops every command with exit 2. An original the line already carries cannot be dropped, and neither can a number that merged into `main` before the line branched. The order is the rules', then the drop: what `backport/skip`, rules 2 to 4 or the line itself decide comes first, with the drop kept on record beside it, and `backport/include` does not lift a drop. The body lists dropped originals under their own heading among the skipped ones, `check` prints a note for each, and `fold` refuses one.
`--push` pushes the branch, refusing anything but a fast-forward of what the remote has, then opens the draft pull request against the line with `gh pr create --draft`, or updates the open batch pull request with `gh pr edit`, and prints its URL on stderr. On the open batch it first posts the run's report as a comment, so a body never names a report nobody can read; a new batch carries the report in the body it opens with, and the comment follows. Before every write to the pull request it reads the pull request again, and stops with exit 2 once it is merged or closed. When the branch it refuses to push over has no pull request open on it, as a run that stopped between its push and opening the pull request leaves it, the refusal names the `run --onto <its head> --push` that continues it. While a batch pull request is open on another branch it refuses, so a line never has two. It never pushes to or edits a batch pull request whose body has no `backport-batch` block either: someone wrote that body by hand, and the batch is theirs to merge or close. After `--defer` alone it replaces the branch, with `--force-with-lease=refs/heads/<branch>:<head>`, where head is what `git ls-remote` reads for the branch and has to be the head the rebuild started from: a commit pushed since is never dropped, by the tool's check or, if one lands between that check and the push, by the remote. A record that says the batch is being closed is acted on before anything else, and only by `run --push`.
### check
Exits 1 and prints the URL of every pull request the rules take that is neither on the line nor on the open batch pull request, 0 when there is none, and 2 when it cannot tell. A pull request that the [line's record](#the-lines-record-and-reports) lists as deferred, a conflict or one taken out with `--defer`, prints as a warning and does not fail the check: deferring is a maintainer's call. That holds once the batch that recorded it merged or closed too. So does one the open batch lists as waiting for a maintainer because it changes workflows; once that batch is gone, nothing lists it any more, and it is missing. One a maintainer dropped from the line prints as a note, `#N is dropped from <line> on #M (<reason>)`.
`--cut` makes it the gate before a patch release or an rc is cut from a line. A cut tags the line, so a pull request that is only on the open batch pull request fails it too, with one line each: `#N is only on open batch PR #M, merge it before the cut`. One that waits there for a maintainer fails it as well, since nobody decided to leave it out, also when it is all the batch has left: bring it over, or take it out with `run --defer` and a reason. Everything else is as without it, and a cut that passes says `no change the policy takes is missing before the cut`. The scheduled run checks without `--cut`, since its batch is open by design.
An original with `backport/skip` that the open batch picked before the label came prints a warning in either mode, naming the batch and `run --defer`, which takes it out; it fails neither.
Every other original the policy takes that neither the line nor the open batch carries is decided in merge order, as [`run`](#run) decides it, with the open batch's found picks counting as there and the mode the open record saved, the scheduled run's when no batch is open, whose workflow waits on a closed record wait on nothing any more, so the original is judged as the scheduled run judges it: deferred for a reason, a warning; parked, a note naming what it builds on; in the scheduled mode a workflow original whose tests alone build on what never comes, waiting as the open batch lists it, or missing with none; and one that waits on other originals by the prerequisite that decides it: missing, which fails both modes, waiting for a maintainer, which warns and fails `--cut`, deferred, which warns, or held by rule 6, a note. A wait on one that waits itself inherits what decides that one.
A unit on the open batch that leaves part of its original without effect, judged as [`run`](#run) judges a pick, at the unit's own first parent and whatever its original's rule, label or drop, prints a warning naming what it leaves out, and fails `--cut` ahead of any skip, drop or stale warning: take it out with `run --defer`. An original that is parked as partial prints as a note, `#N is parked: only part of it applies`, and fails neither. That includes one the open record parks as partial that the net merge at the line's head finds whole, since `run`'s picker found it after the picks before it: `check` and the operations that only change the record keep it parked while its net merge onto what the batch carries now still leaves something without effect, and call it missing once that went, as when the pick that made it partial was taken off. A park only the commit-by-commit replay sees, which no net merge shows, reads as missing in `check` until the next run.
### fold
Checks a hand-made resolution of a deferred conflict before it is pushed onto the batch, in the shape the run gives an original. For a merged pull request `--commit` is a merge whose first parent is the head of the open batch pull request and whose message names the original in exactly one `Backported-from:` trailer; its second parent ends a straight line from that head of picks of the pull request's own commits, in their order, each ending in the `(cherry picked from commit …)` line of the commit it came from, none claiming another commit of the same original, a commit of another pull request never among them. For a squash-merged one it is a single commit on that head whose `(cherry picked from commit …)` line names the original's commit on `main`. An original parked for what it builds on folds like any other when the resolution is complete. Either has to change something, and only files the original changes or the paths a rename on `main` or on the line gave them, and has to leave nothing the original modifies or deletes without effect, judged as [`run`](#run) judges a pick: a resolution that ports what the line has and leaves out what it lacks is refused, since such a port goes to the line as a pull request of its own. The [conflict task](../../docs/release/backport-tasks/resolve-conflict.md) gives the commands. fold prints `ok` and exits 0, or prints why not and exits 1. It never commits or pushes; push one checked resolution at a time, since the next one has to sit on the new head.
### verify
Holds every unit of a batch to the shape fold checks for one, the clean picks as well as resolutions: it walks the batch's first-parent line from `origin/<line>` to `--head`, or to the head of the open batch pull request, and requires each unit to be one original, merged into `main` after the line branched, in that shape, and no original twice. A commit that carries no original fails it, and so does a merge of the line into the batch, and a unit that leaves part of its original without effect, judged as [`run`](#run) judges a pick. It prints one line per unit, and exits 0 when all pass, 1 when one does not, and 2 when it cannot tell. With `--head` it reads git alone, nothing from GitHub. fold runs only where someone resolved a conflict and chose to run it; [`pr-labeler.yaml`](../../.github/workflows/pr-labeler.yaml) runs verify on every push to a batch pull request, from a build of `main`, with the batch head fetched as data.
### notify
Reads the [line's record](#the-lines-record-and-reports) first, as the other commands do, and exits 2 when it cannot read it or when it says the open batch is being closed, with a batch open or not. Then it reads the open batch pull request of the line, trusted as described below, and prints one line for each thing that waits on it: a batch open for three days or more, the shortest gap between two scheduled runs, so one the previous run already saw; the conflicts it deferred; and the originals waiting for a maintainer because they change workflows. Nothing open prints nothing.
An original the record has wait on others is listed once, as `#M, after #F`, where what decides its wait is listed: after a deferred conflict with the conflicts, after a workflow wait with the waits; one waiting on what is missing or held by rule 6 is not listed. A unit on the batch that carries only part of its original is listed apart, to be taken out before the cut.
Each park the record of the open batch holds is named once. `run` marks a park `announced` when the line's record it found, open or closed, holds the same park announced: the same merge, kind, prerequisites, commits and files; a park that changed is news again. `notify` lists the parks not yet marked, and with `--push`, once the comment is written, marks them in the record with `gh pr edit --body-file`, only while the pull request is open and its body still carries the record `notify` read. A mark that fails after the comment exits 2, and the next `notify` names those parks once more. With no batch open nothing is named, and the next batch's first `notify` names what it parks.
With `--push` it writes the same into one comment on the batch pull request: the conflicts, each to be resolved as the [conflict task](../../docs/release/backport-tasks/resolve-conflict.md) describes, and the originals that wait because they change workflows.
The comment starts with `<!-- backport-batch:notify -->` and is one per pull request: a later run edits it in place rather than adding another, and once nothing waits it says so. Only a comment whose author the batch trusts counts as it, so one someone else wrote with the same first line is neither edited nor believed. It mentions `--mention`, an `org/team` or a login, with or without `@`, only while something waits. A failed batch CI is not among what it reports: the run has no trigger on the batch's CI finishing.
### report
Prints a report a batch pull request keeps: the one its body carries, or the one `--id` names. It reads the report comments the batch trusts and falls back to the body, recomputes the report's ID from what it says, and exits 2 when a trusted comment does not match its ID, when a report says anything a run does not write or is for another line than the pull request's, or when the pull request keeps no such report. A report comment counts only when its author is one the batch trusts with a record, so anyone else's comment, whatever ID it carries, is not read. It prints what the classification was made of, whether the pull request's head or body shows the report was published, which it calls unconfirmed when neither does, and one line per original with its category and what the report recorded about it; `--json` prints the report as stored. A report whose comment the run never got to post is printed from the body, with a note that the next `run --push` posts it, or, on a merged or closed batch, that it is kept in the body only. It writes nothing. It reads GitHub, and for a report that keeps drops also the clone's history, without fetching: each drop is bound to `main` as the record's drops are, over the heads of `main` and the line the report was made from, so a drop of an original the report classified has to name that original's merge, and any other a merge of its number on `main`'s first-parent line. A clone without those heads exits 2, asking for a fetch.
### lines
Prints the release lines the batch runs for, newest first: of the `release-X.Y` branches on `origin`, the newest that have a stable `vX.Y.Z` tag there, as many as `released-lines` in the policy block says, two when it says nothing, and every branch newer than the newest of them. The tags are read with `git ls-remote`; a tag with a pre-release suffix does not count. `run`, `check` and `fold` refuse any other line, and rule 6 holds each line to the next newer one among them.
## Rule 4 and the API review gate
A change to the API is held back only when the [API review gate](../../.github/workflows/api-review-gate.yaml) would call it sizeable. The gate is asked whenever an original changes something it parses: `pkg/apiserver/apiserver.go`, an ApplicationDefinition under `packages/system/*-rd/cozyrds/`, or a `.yaml` or `.yml` file under `packages/` or `internal/` that holds `CustomResourceDefinition` or `APIService` before or after the merge. An edit to the Go types under `api/` alone reaches none of them, so the gate would see no change. The batch calls the gate's own code, `apigate.LoadSnapshot` and `apigate.Classify` from `internal/apigate`, on the trees of the original's first parent and of its merge commit. It reads only what the gate reads, from `git ls-tree` and `git cat-file` into a temporary directory, so no attribute or export rule changes what the gate sees. A reworded description or a new optional field passes; a new or removed group or resource, or a schema change that can reject what the old schema accepted, does not. Migrations, `targetVersion` and CRDs keep their own checks: the gate sees only first-party `cozystack.io` CRDs, and only their sizeable changes, while a vendored operator's CRD is just as unsafe in a patch.
The CRD check works document by document. In every changed manifest, a YAML, JSON or `.tpl` file or anything under `templates/` or `crds/`, outside `tests/` and `testdata/`, every document that names `CustomResourceDefinition` is read as YAML, which takes JSON too, and the CRDs among them are compared between the parent and the merge by `metadata.name`, compact JSON and quoted keys included. The change counts only when one was added, removed or edited, so an edit to the DaemonSet beside a CRD in one manifest does not, and neither does a sentence or a `lookup` call that merely names the kind. A document that declares `kind: CustomResourceDefinition` on a line of its own but cannot be read as an object, a CRD behind a Helm template guard for one, is compared by its text, so the check fails closed on it, and so does a `List` that holds a CRD.
## Maintainer overrides
`run`, `check` and `fold` read the merged pull requests that carry `backport/include` or `backport/skip`, through GitHub search, which answers at most 1000 per label; a listing that long is an error rather than a complete answer. `backport/skip` keeps a pull request out whatever the rules say, and wins over `backport/include`, which lifts rules 2 to 4 and nothing else: what the line already carries and, on the older line, what the newer one does not carry yet still decide. The batch pull request lists every overridden original and what became of it. Adding a label takes triage access to the repository, which is the trust the labels rest on; who added one is not checked.
## What counts as already there
An original is on a line when any of these holds, and the batch does not pick it again:
- its merge commit is reachable from the line;
- picks on the line name its merge commit, or every commit of its own, in the `(cherry picked from commit …)` line git appended to them;
- picking it onto the line would change nothing.
A pick is known by the line git appends: the last `(cherry picked from commit …)` line of the message's last paragraph, with nothing after it but trailers such as a sign-off. A picked commit keeps its message, and a batch merge carries the original's merge message, so whatever else a message says was written by someone the batch does not answer for: a line in it naming an original, as an `-x` line, a `Backported-from:` trailer or `Backport of #N`, does not count. On commits from before the policy's `batch-only` day it does, since those record the hand backports and earlier batches:
- a `(cherry picked from commit …)` line anywhere in the trailer block naming its merge commit or, across commits, every commit of its own;
- a `Backported-from:` trailer naming its merge commit;
- `Backport of #N`, which is what a hand backport merged as one commit carries.
An original is on the open batch pull request when its commits picked there show it the same way. The same rules on the newer line decide rule 6.
Only the line's own commits count, the ones after the point where it branched from `main`, never a pull request against it: an open backport pull request, its title and its body are anyone's to write. The tool trusts the line's history because only maintainers merge into `release-*`; if that ever changes, a commit that names an original without carrying all of it would hide the rest. The batch pull request counts only when it comes from a `backport-batch/<line>-<yyyymmdd>` branch of this repository, not a fork, and was opened by the bot the scheduled batch runs as, the exact login `BACKPORT_BATCH_BOT` names, `cozystack-ci[bot]` when it is unset and no bot when it is set empty, or by someone whose permission on the repository GitHub reports as write or admin, and only at the head GitHub reports for it. No other bot counts, GitHub Actions' own included, since anything holding a workflow's token writes as it. So a maintainer's run by hand continues the scheduled batch with nothing set, rather than taking its pull request for a look-alike and opening a second one. The notify comment is trusted the same way. A permission GitHub does not answer stops the command with exit 2 rather than count the pull request or the comment out, which would open a second of either. The author association is not enough: an organization member or an invited collaborator can hold read access only. With more than one such pull request open the tool refuses to go on.
Titles and bodies are read from the merge commits on `main`, which hold them as a maintainer merged them; the live pull request stays editable by its author.
## What an original builds on
An original the policy takes can build on code its line does not have: another pull request merged into `main` after the line branched that the line never got, or a commit pushed to `main` directly. For each original the policy takes that neither the line nor the open batch carries, `run` reads what it builds on from git alone, in two ways:
- A file it modifies or deletes whose counterpart, as [`run`](#run) follows renames, the line does not have is named by the first-parent commit on `main` that added it since the line branched: `git --literal-pathspecs log --first-parent --diff-merges=first-parent --no-renames --diff-filter=A -1 <fork point>..M^1 -- <path>`. A file nothing added since the fork point is shown with the original, since no other pull request brought it.
- A line it removes or changes in a file the line has is given by `git blame --porcelain -w`, at `M^1` named by its full commit, to a commit, which counts through the first-parent commit on `main` that brought it: each commit of `main`'s first-parent line since the fork point brings itself and every commit of the range reachable from its other parents that no earlier one brought. A commit from before the fork point, which the line has, and the original's own count for nothing. The commit counts only while one of its lines, with its edges trimmed and nothing else, is absent from the line's version of the file, so a hand backport under another commit clears it. A file the line already has as the original makes it, the same blob or a clean `git merge-file` back into the line's file, builds on nothing.
Lines a tool wrote are not blamed: a generated file whole, `zz_generated*`, one with a `Code generated … DO NOT EDIT` marker in its first 100 lines, `go.sum` and the package-manager lock files, and in an ApplicationDefinition under `packages/system/*-rd/cozyrds/` the keys `hack/update-crd.sh` writes, found by their indentation. Controller-gen CRDs and `values.schema.json` are blamed like any other file. Each `git log`, `git diff` and `git blame` the detector and the rename detection run has two minutes, and rename detection that runs out of time counts as skipped, and an original has at most 256 files, missing or blamed, 20 000 blamed lines and 8 MiB per file read for it. Evidence it cannot read, past one of those bounds, a binary file, a symlink, a submodule, a commit blame names that did not come through `main`, a path that is not UTF-8, or a rename git skipped looking for, is shown with the original, whatever becomes of it, under its line in the body and in the report's `diag`. Output of git the tool cannot read whole stops it with exit 2, and is never taken for an absent dependency. A dependency carried only by lines an original adds is not seen. `run` prints how long reading the line took on stderr. What follows from a dependency, a park or a wait, is in [`run`](#run) and [`check`](#check).
## The line's record and reports
The line's record is the `backport-batch` block of its newest batch pull request: the newest pull request against the line from a `backport-batch/<line>-<yyyymmdd>` branch of this repository, opened by an author the batch trusts as above, whether it is open, merged or closed. `run`, `check`, `fold` and `notify` find it in GitHub's list of the line's pull requests, newest first, reading at most ten pages of 100. A batch pull request a person opened without the block is passed over. A block whose author the batch does not trust, a bot's batch without its block, a block that cannot be read whole, a closed record newer than the open batch, or ten pages with no record stop the command with exit 2, naming the pull request, rather than fall back to an older record.
The record is written only while its pull request is open: a merged or closed batch pull request is never edited or commented on again. What it recorded keeps counting, so a conflict or a deferral a merged batch recorded still prints as a warning. A workflow original it listed as waiting is missing, since no open batch lists it. A run that picks nothing while no batch is open has nowhere to write: it prints the body, says that its classification is not retained, and exits 0.
The record is only as trustworthy as the body it sits in. Anyone with write access can edit any body, the bot's included, and neither the bot's authorship nor anything in the block proves who edited it last. A hand edit that leaves a readable record is believed; one that breaks it stops every command with exit 2, naming the pull request. A reason or a file name in the record counts only in the form a run writes it, one line with no control, zero-width or bidi character, so text someone typed into the body never reaches a terminal, a body or a comment as they typed it. When the newest batch pull request is deleted and no later one exists, nothing shows it was there: the older record governs, and what was recorded since is lost.
The record holds only what later commands act on. Version 3, which this tool writes, adds what is parked and why, with the merge commit each park is bound to, the units on the batch that do only part of their original, the picks that differ from theirs, and what each wait on another original waits on; it reads versions 1 to 3, and a tool that reads only 1 and 2 refuses version 3 with exit 2. Every run that classifies the line also makes a report, one line of JSON in a `backport-batch-report` block: every original merged into `main` since the line branched in exactly one category, picked, missing, deferred with what it was deferred for, or skipped with the rule that skipped it, each drop the record keeps with the merge commit it is bound to, its reason and the batch pull request that recorded it, the commits on `main` that merged no pull request, the labels, and what the classification was made of: the heads of `main`, the line and the newer line, the policy, the batch head it describes, the tool's commit, the Actions run and the flags. Its ID is a hash of all that but the time and the run, so two runs that find the same make the same report, and it names no pull request, so a new batch's report is whole before GitHub numbers the pull request. The body carries the latest report beside the record, and a comment on the open batch pull request keeps each one: `<!-- backport-batch:report <id> -->` and the same block, folded away. A report already posted is not posted again, and the tool never edits or deletes such a comment, so a report the next run replaces in the body is still read back by `report`, also after the batch merged. A comment that looks like one but whose author the batch does not trust is not read. When a run stops after writing the body but before posting its comment, the next `run --push` on the open batch posts that report first, byte for byte; once the batch is closed nothing is written to it, and the report stays in its body. A run that picks nothing while no batch is open keeps no report.
A `--defer` that leaves the batch carrying nothing closes it, and writes everything while the pull request is still open: the report comment, then the body, whose record keeps the deferrals and says the batch is closing, with its number, branch and the head the rebuild started from. It then reads the pull request and the branch again, closes the pull request while both are at that head, and deletes the branch with `--force-with-lease` on it, printing `closed <url>`. The next batch tries what was deferred again. A close that stops part way is finished by the next `run --push`, before it does anything else: it closes and deletes when the pull request is open at the frozen head, deletes the branch when the pull request is closed and the branch still at that head, and goes on as usual once both are gone. A run that finishes a close stops there, with exit 0, or with exit 2 when it was asked for `--defer`, `--drop` or `--undrop`, which it does not apply. It never takes a head it did not freeze: with the branch moved, it stops with exit 2 and names both heads. `run` without `--push`, `check` and `fold` stop with exit 2 while the pull request is still open; once it is closed they go on, warning about a branch the close left, and `notify` says nothing. A closed pull request is never written to, so its record says it was closing for good, and a closed pull request whose branch is gone counts as closed.
One way for the branch to move is a new batch of the same day: it reuses the branch name, and if its push succeeds and opening its pull request fails, the closed batch is still the newest record while its branch now holds the new batch. The next run stops on it, naming the branch, and so does `--onto` with its head. Check what it carries before deleting it with `git push origin --delete backport-batch/<line>-<yyyymmdd>`: the next run picks again what the tool put there from the line, but not a commit someone pushed onto it, such as a resolved conflict. Separate writes to GitHub are not atomic: the re-reads and the leases refuse a race they see, but cannot prevent one.
## What CI verifies
The tool does not build or test anything. CI on the batch pull request runs the line's own build and tests against what was picked, and `verify` against its shape, and a maintainer reviews and merges it. A deferred conflict waits for the next batch unless someone resolves it, checks the resolution with `fold` and pushes it, and then CI runs again.
## Untrusted text
Titles, release notes and file names in the body come from whoever opened the originals, and a reason from whoever ran `--defer`. A title, a reason or a file name goes into a code span whose fence no backtick inside can close, and the release notes go into the `release-note` block with every run of three backticks or tildes defused, so GitHub acts on no mention, reference, link or HTML in them. Inside that block, in case a note is copied out of it, links lose any target outside this repository, and closing keywords, cross-repository references and mentions have their `@`, `#` and `://` swapped for fullwidth signs, which act on nothing however the backticks around them pair up. The record and the report are one line of JSON each, whose encoding leaves no newline, `<`, `>` or `&` in them, so nothing in a file name or a reason ends their block or opens HTML, and a report comment names no one: numbers are bare, and its only text outside the block is the line, the date and the report's ID. Zero-width, bidi and control characters are dropped first, and everything the tool prints passes the same filter, so nothing it relays can drive a terminal. Commit messages are read through `git cat-file --batch`, which gives each object's length, and end at their first NUL as `git log` shows them, so a message cannot pass for a second commit or hide a trailer from a reader of the log. Every value handed to git comes after `--end-of-options` or `--`, refs pass `git check-ref-format`, SHAs have to be 40 hex digits, and nothing runs through a shell.
+188
View File
@@ -0,0 +1,188 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/cozystack/cozystack/internal/apigate"
)
// Rule 4's API clause is the API review gate's own verdict: the same
// apigate.LoadSnapshot and apigate.Classify that cmd/api-gate runs in
// api-review-gate.yaml, on the trees of the original's first parent and of its
// merge, so a change the gate passes as additive or descriptive is no reason to
// hold a fix back.
const (
apiserverGo = "pkg/apiserver/apiserver.go"
// apigate reads an ApplicationDefinition wherever this matches, whatever
// the file holds.
cozyRDGlob = "packages/system/*-rd/cozyrds/*.yaml"
// apigate skips a YAML file larger than this; reading it would be wasted.
apiFileLimit = 8 << 20
)
// apiYAMLRoots hold all YAML apigate.LoadSnapshot parses: the
// ApplicationDefinitions and every first-party CRD or APIService.
var apiYAMLRoots = []string{"packages", "internal"}
// apiInputs are what apigate.LoadSnapshot reads of a checkout: the YAML roots
// and the apiserver's storage registrations.
var apiInputs = append(slices.Clone(apiYAMLRoots), apiserverGo)
// apiMarkers are mightBeAPIManifest's: apigate parses no other YAML file that
// holds neither.
var apiMarkers = []string{"-e", "CustomResourceDefinition", "-e", "APIService"}
var apiSnapshots = map[string]apigate.Snapshot{}
// apiSnapshot is apigate's snapshot of commit, built from a temporary tree
// holding only what it reads. The blobs come from ls-tree and cat-file rather
// than a checkout or git archive: no attribute filters or export rules apply,
// and a symlink is never followed out of the tree.
func apiSnapshot(commit string) (apigate.Snapshot, error) {
if snap, ok := apiSnapshots[commit]; ok {
return snap, nil
}
out, err := git(append([]string{"ls-tree", "-r", "-z", "-l", "--full-tree", "--end-of-options", commit, "--"}, apiInputs...)...)
if err != nil {
return nil, err
}
paths := map[string]string{}
var blobs []string
for _, entry := range splitNUL(out) {
meta, name, ok := strings.Cut(entry, "\t")
f := strings.Fields(meta)
if !ok || len(f) != 4 {
return nil, fmt.Errorf("ls-tree %.12s: unreadable entry %q", commit, entry)
}
mode, kind, blob := f[0], f[1], f[2]
size, _ := strconv.Atoi(f[3])
ext := path.Ext(name)
switch {
case kind != "blob" || (mode != "100644" && mode != "100755"):
case name != apiserverGo && ext != ".yaml" && ext != ".yml":
case size > apiFileLimit:
case checkSHA(blob) != nil || !filepath.IsLocal(filepath.FromSlash(name)):
return nil, fmt.Errorf("ls-tree %.12s: unreadable entry %q", commit, entry)
default:
paths[name] = blob
blobs = append(blobs, blob)
}
}
contents, err := readObjects(dedupe(blobs), "blob")
if err != nil {
return nil, err
}
dir, err := os.MkdirTemp("", "backport-batch-api-")
if err != nil {
return nil, err
}
defer func() { _ = os.RemoveAll(dir) }()
for name, blob := range paths {
file := filepath.Join(dir, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
return nil, err
}
if err := os.WriteFile(file, contents[blob], 0o600); err != nil {
return nil, err
}
}
snap, err := apigate.LoadSnapshot(dir)
if err != nil {
return nil, fmt.Errorf("the API at %.12s: %w", commit, err)
}
apiSnapshots[commit] = snap
return snap, nil
}
// readsAPI reports whether the change reaches anything apigate.LoadSnapshot
// parses at either end. A change to anything else leaves both snapshots alike,
// so the gate is not asked. A binary file is searched as well, since apigate
// reads one like any other; a file over its size limit is still asked about,
// which costs a snapshot and changes no verdict.
func readsAPI(parent, merge string, changes []fileChange) (bool, error) {
var yamls []string
for _, c := range changes {
p := c.path
if p == apiserverGo {
return true, nil
}
if ext := path.Ext(p); ext != ".yaml" && ext != ".yml" {
continue
}
if !slices.ContainsFunc(apiYAMLRoots, func(root string) bool { return strings.HasPrefix(p, root+"/") }) {
continue
}
if rd, _ := path.Match(cozyRDGlob, p); rd {
return true, nil
}
yamls = append(yamls, p)
}
for _, rev := range []string{parent, merge} {
found, err := grepFiles(rev, yamls, apiMarkers...)
if err != nil || len(found) > 0 {
return len(found) > 0, err
}
}
return false, nil
}
// sizeableAPIChange says why the API review gate would call the original's
// change to the API sizeable, or "" when it would not.
func sizeableAPIChange(o *original) (string, error) {
parent, err := resolve(o.merge + "^1")
if err != nil {
return "", err
}
base, err := apiSnapshot(parent)
if err != nil {
return "", err
}
head, err := apiSnapshot(o.merge)
if err != nil {
return "", err
}
findings := apigate.Classify(base, head)
if len(findings) == 0 {
return "", nil
}
why := fmt.Sprintf("%s: %s", findings[0].Category, findings[0].Detail)
if len(findings) > 1 {
why += fmt.Sprintf(", and %d more", len(findings)-1)
}
return why, nil
}
func dedupe(list []string) []string {
seen := map[string]bool{}
var out []string
for _, s := range list {
if !seen[s] {
seen[s] = true
out = append(out, s)
}
}
return out
}
+975
View File
@@ -0,0 +1,975 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"slices"
"strings"
"time"
)
// pick is the outcome of picking one original onto the batch branch.
type pick struct {
v *verdict
status string // picked, conflict, empty, held out by --defer, or waiting for a maintainer
commit string
files []string
// failed is the commit whose pick conflicted.
failed string
// needs are the originals still to come it waits on.
needs []int
// more counts the conflicting files a recorded conflict no longer names.
more int
reason string
}
// workflowsDir is where GitHub reads workflows from. A token without the
// workflows permission, a GitHub App's among them, cannot push a commit that
// changes anything under it.
const workflowsDir = ".github/workflows/"
// workflowFiles lists the workflow files the commits change, each against its
// parent: a push carries every pick, so a change one commit makes and a later
// one takes back is in it although the pull request's merge changes nothing
// there.
func workflowFiles(commits []string) ([]string, error) {
if len(commits) == 0 {
return nil, nil
}
out, err := gitInput(strings.Join(commits, "\n")+"\n", "diff-tree", "--stdin", "--no-commit-id", "--name-only", "-r", "-z", "--no-renames")
if err != nil {
return nil, err
}
var files []string
for _, f := range splitNUL(out) {
if strings.HasPrefix(f, workflowsDir) && !slices.Contains(files, f) {
files = append(files, f)
}
}
return files, nil
}
func batchBranch(l line, date time.Time) string {
return "backport-batch/" + l.branch() + "-" + date.Format("20060102")
}
func (a *app) run(args []string) int {
fs := a.policyFlags("run")
lineArg := fs.String("line", "", "release line, release-X.Y")
onto := fs.String("onto", "", "head of the pushed batch branch to continue")
cont := fs.Bool("continue", false, "continue the open batch pull request, or start a batch when none is open")
dateArg := fs.String("date", "", "batch date, YYYY-MM-DD (default: today, UTC)")
deferArg := fs.String("defer", "", "originals to take out of the open batch, N[,N]; rebuilds it without them")
dropArg := fs.String("drop", "", "originals to drop from the line, N[,N]; recorded on the open batch, and taken off it")
undropArg := fs.String("undrop", "", "originals whose drop to withdraw, N[,N]")
noWorkflows := fs.Bool("no-workflow-files", false, "leave an original that changes "+workflowsDir+" to a maintainer, for a token that cannot push one")
reasonArg := fs.String("reason", "", "why --defer takes them out or --drop drops them, recorded in the batch body")
push := fs.Bool("push", false, "push the branch and open or update the draft batch PR")
if err := parse(fs, args); err != nil {
return 2
}
l, err := parseLine(*lineArg)
if err != nil {
return a.fail(err)
}
date := a.now().UTC()
if *dateArg != "" {
if date, err = time.Parse(time.DateOnly, *dateArg); err != nil {
return a.fail(fmt.Errorf("--date %q is not YYYY-MM-DD", *dateArg))
}
}
if *onto != "" {
if err := checkSHA(*onto); err != nil {
return a.fail(fmt.Errorf("--onto: %w", err))
}
}
var deferred, drops []int
reason, ops := "", 0
for _, op := range []string{*deferArg, *dropArg, *undropArg} {
if op != "" {
ops++
}
}
switch {
case ops > 1:
return a.fail(errors.New("--defer, --drop and --undrop each change the open batch pull request on their own; give one"))
case *dropArg != "" || *undropArg != "":
if *onto != "" || *cont || *dateArg != "" {
return a.fail(errors.New("--drop and --undrop change the open batch pull request; each takes no --onto, --continue or --date"))
}
if *undropArg != "" && *reasonArg != "" {
return a.fail(errors.New("--undrop takes no --reason"))
}
list := *dropArg + *undropArg
if drops, err = parseNumbers(map[bool]string{true: "--drop", false: "--undrop"}[*dropArg != ""], list); err != nil {
return a.fail(err)
}
if reason = capped(oneLine(clean(*reasonArg, false)), titleLimit); *dropArg != "" && reason == "" {
return a.fail(errors.New("--drop needs a --reason, which the line's record keeps"))
}
case *deferArg != "" || *reasonArg != "":
if deferred, reason, err = parseDefer(*deferArg, *reasonArg); err != nil {
return a.fail(err)
}
if *onto != "" || *cont || *dateArg != "" {
return a.fail(errors.New("--defer rebuilds the open batch pull request; it takes no --onto, --continue or --date"))
}
case *cont && (*onto != "" || *dateArg != ""):
return a.fail(errors.New("--continue reads the batch to continue from the open batch pull request; it takes no --onto or --date"))
}
if err := cleanTree(); err != nil {
return a.fail(err)
}
if err := fetch(); err != nil {
return a.fail(err)
}
started := time.Now()
b, err := a.prepare(l)
if err != nil {
return a.fail(err)
}
if goOn, code := a.resumeClose(b, *push, deferred != nil || drops != nil); !goOn {
return code
}
if err := b.analyze(); err != nil {
return a.fail(err)
}
// The scheduled job has half an hour for every line; this says where it
// goes.
_, _ = fmt.Fprintf(a.stderr, "prepared %s in %s, %s of it judging partial units and what originals build on\n", l.branch(), time.Since(started).Round(time.Millisecond), b.detecting.Round(time.Millisecond))
if drops != nil {
return a.redrop(b, drops, reason, *undropArg != "", *push)
}
if deferred != nil {
return a.rebuild(b, deferred, reason, *push)
}
if *cont && b.pr != nil {
*onto = b.prHead
}
branch, start := batchBranch(l, date), b.lineHead
var carried *history
if *onto != "" {
if branch, err = ontoBranch(l, *onto, *dateArg != "", branch); err != nil {
return a.fail(err)
}
if carried, err = readPicks(*onto, b.lineHead, b.textUntil); err != nil {
return a.fail(err)
}
start = *onto
// The batch keeps the date it was opened under, in its title too.
if date, err = branchDate(branch); err != nil {
return a.fail(err)
}
}
if err := checkBranch(branch); err != nil {
return a.fail(err)
}
// Continuing the open batch keeps out what was deferred from it by hand;
// a new batch tries those again.
held := map[int]deferredEntry{}
if carried != nil && b.src.open && b.pr.Head.Ref == branch {
held = b.src.rec.deferred()
}
if err := a.warnReset(branch, start); err != nil {
return a.fail(err)
}
if _, err := git("switch", "--quiet", "--no-track", "-C", branch, "--end-of-options", start); err != nil {
return a.fail(err)
}
r := newReport(b)
r.opts = runOptions{NoWorkflowFiles: *noWorkflows, Mode: modeNew}
// A drop is a decision about the line, not about one batch: a new batch
// keeps the ones the closed batch recorded.
r.dropped = b.src.rec.dropped()
if carried != nil {
r.opts.Mode = modeContinue
if r.stale, err = keptOut(b, carried); err != nil {
return a.fail(err)
}
}
// cls is what each original became in this run, to an original that
// builds on it; the rules decide the rest.
mode := analysisMode{noWorkflowFiles: *noWorkflows}
cls := map[*verdict]edgeClass{}
classOf := func(f *verdict) edgeClass {
if c, ok := b.ruleClass(f); ok {
return c
}
if c, ok := cls[f]; ok {
return c
}
return edgeMissing
}
for _, v := range b.qualifying() {
on := false
if carried != nil {
if on, err = b.onBranch(carried, v); err != nil {
return a.fail(err)
}
}
switch {
case on:
r.onBranch = append(r.onBranch, v)
cls[v] = edgeSatisfied
continue
case held[v.o.number].Reason != "":
r.held = append(r.held, pick{v: v, status: "held", reason: held[v.o.number].Reason})
cls[v] = edgeDeferred
continue
case v.onBatch != "":
v.rule, v.reason = ruleOnLine, fmt.Sprintf("on the open batch pull request #%d", b.pr.Number)
r.skipped[ruleOnLine] = append(r.skipped[ruleOnLine], v)
continue
}
if v.partial != nil {
// Judged at the line's head; what the batch picked since may
// supply what it lacked there.
if err := a.partialAtHead(b, v); err != nil {
return a.fail(err)
}
if v.partial != nil {
r.parked = append(r.parked, v)
cls[v] = edgeNever
continue
}
}
if len(v.deps) > 0 {
kind, err := a.dependsAt(b, r, v, mode, classOf)
if err != nil {
return a.fail(err)
}
if kind != edgeSatisfied {
cls[v] = kind
continue
}
}
if *noWorkflows {
files, err := workflowFiles(v.commits)
if err != nil {
return a.fail(err)
}
if len(files) > 0 {
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: files})
cls[v] = edgeWorkflow
continue
}
}
p, err := a.pickUnit(v, func(base, head string) ([]string, error) {
omitted, diag, err := b.omittedEffects(v, base, head)
v.addDiag(diag...)
return omitted, err
})
if err != nil {
return a.fail(err)
}
switch p.status {
case "partial":
v.partial = p.files
v.park = &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkPartial, Files: sortedClean(p.files)}
r.parked = append(r.parked, v)
cls[v] = edgeNever
case "picked":
r.onBranch = append(r.onBranch, v)
cls[v] = edgeSatisfied
case "conflict":
r.deferred = append(r.deferred, p)
cls[v] = edgeDeferred
case "empty":
v.rule, v.reason = ruleOnLine, "the earlier picks already carry it"
r.skipped[ruleOnLine] = append(r.skipped[ruleOnLine], v)
}
}
if *onto != "" {
head, err := resolve("HEAD")
if err != nil {
return a.fail(err)
}
descends, err := isAncestor(*onto, head)
if err != nil {
return a.fail(err)
}
if !descends {
return a.fail(fmt.Errorf("%.12s does not descend from --onto %.12s", head, *onto))
}
if err := b.markPartial(r, carried.head); err != nil {
return a.fail(err)
}
}
return a.finish(b, r, branch, date, *push, false)
}
// dependsAt decides, from what v builds on that the branch lacks, whether
// the run leaves it out: waiting for a maintainer in the scheduled mode
// when only tests or docs build on what never comes, parked when anything
// else never comes, or waiting on prerequisites still to come. What is
// there, the line's, the batch's, or what this run picked or found empty,
// counts for nothing; once earlier picks moved the branch, v is judged at
// its head. It returns what v became to an original that builds on it,
// edgeSatisfied when the run goes on to pick it.
func (a *app) dependsAt(b *batch, r *report, v *verdict, mode analysisMode, classOf func(*verdict) edgeClass) (edgeClass, error) {
deps := v.deps
head, err := resolve("HEAD")
if err != nil {
return 0, err
}
if head != b.lineHead {
if deps, err = b.depsAt(v, head); err != nil {
return 0, err
}
}
never, pending := edges(deps, classOf)
primary, err := workflowPrimary(mode, v, never, pending)
switch {
case err != nil:
return 0, err
case primary:
files, err := workflowFiles(v.commits)
if err != nil {
return 0, err
}
v.addDiag(testsOnly(v, never, b.line))
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: files})
return edgeWorkflow, nil
case len(never) > 0:
v.park = dependencyPark(v, never)
r.parked = append(r.parked, v)
return edgeNever, nil
case len(pending) > 0:
class, needs := strongest(pending)
r.needs = append(r.needs, pick{v: v, status: "waiting", needs: numbers(needs)})
return class, nil
}
return edgeSatisfied, nil
}
// partialAtHead judges v again where the run has got to, when earlier picks
// moved the branch past the line's head.
func (a *app) partialAtHead(b *batch, v *verdict) error {
head, err := resolve("HEAD")
if err != nil || head == b.lineHead {
return err
}
return b.partialAt(v, head)
}
// keptOut lists the originals with backport/skip, or dropped, that the batch
// still carries: a run never takes a commit off the batch, --defer does.
func keptOut(b *batch, carried *history) ([]*verdict, error) {
var out []*verdict
for _, v := range b.verdicts {
if v.rule != ruleSkipLabel && v.rule != ruleDropped {
continue
}
if err := b.load(v); err != nil {
return nil, err
}
on, err := b.onBranch(carried, v)
if err != nil {
return nil, err
}
if on {
out = append(out, v)
}
}
return out, nil
}
func found(h *history, v *verdict) bool {
c, _ := h.found(v.o, v.commits)
return c != ""
}
// newReport starts the report of a run with what the rules left out and what
// the labels overrode.
func newReport(b *batch) *report {
r := &report{line: b.line, newer: b.newer, skipped: map[int][]*verdict{}, other: len(b.other), start: b.start, carried: b.src.rec}
for _, v := range b.verdicts {
if v.rule != 0 {
r.skipped[v.rule] = append(r.skipped[v.rule], v)
}
if v.label != "" {
r.overrides = append(r.overrides, v)
}
}
return r
}
// finish prints the body and what the run did, and publishes the branch when
// asked to.
func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, rebuilt bool) int {
head, err := resolve("HEAD")
if err != nil {
return a.fail(err)
}
if r.shapes, err = summarize(b, head, r.onBranch); err != nil {
return a.fail(err)
}
if rebuilt && head == b.lineHead && b.pr != nil {
// Deferring everything the batch carries leaves nothing to push: the
// batch is closed, and its record says so first, with the head the
// close is leased on.
r.closing = &closing{PR: b.pr.Number, Branch: branch, Head: b.prHead}
r.opts.Mode = modeClose
}
r.built = builtFrom{Main: b.mainHead, Line: b.lineHead, Newer: b.newerHead, Policy: b.policyAt, Batch: head, Tool: a.tool,
Run: runURL(os.LookupEnv), At: a.now().UTC().Format(time.RFC3339), Options: r.opts}
doc, err := buildReport(b, r)
if err != nil {
return a.fail(err)
}
p := publication{branch: branch, head: head, title: prTitle(b.line, date.Format(time.DateOnly)), report: storedReport{doc: doc, raw: reportJSON(doc)}}
p.record = r.lineRecord(doc)
// A record the next command would refuse is never written.
if err := p.record.validate(b.line); err != nil {
return a.fail(fmt.Errorf("the record this run made %w", err))
}
r.blocks = recordBlock(p.record) + "\n\n" + reportBlock(p.report.raw)
template, err := lineTemplate(b.lineHead)
if err != nil {
return a.fail(err)
}
body, left := r.body(template)
p.body = body
_, _ = fmt.Fprint(a.stdout, body)
skipped := 0
for _, items := range r.skipped {
skipped += len(items)
}
for _, rule := range left {
for _, v := range r.skipped[rule] {
_, _ = fmt.Fprintf(a.stderr, "skipped by %s: %s %s (%s)\n", ruleName(rule), v.o.url(), terminalSafe(v.o.title), terminalSafe(v.reason))
}
}
for _, p := range r.waiting {
_, _ = fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
}
_, _ = fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
branch, head, len(r.onBranch), len(r.deferred)+len(r.held), len(r.waiting), skipped, p.title, doc.ID)
if !push {
return 0
}
if head == b.lineHead && b.pr == nil {
// Picking nothing is not a failure of the run; check still reports
// whatever the line lacks. Merged and closed batches are never
// written, so there is nowhere to keep what the run found.
_, _ = fmt.Fprintf(a.stderr, "%s carries nothing beyond %s; nothing to push\n", branch, b.line.branch())
_, _ = fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
return 0
}
if err := a.publish(b, p, rebuilt); err != nil {
return a.fail(err)
}
return 0
}
// lineRecord is the record a run leaves for the line: what later commands
// act on, with the full classification left to the report it names.
func (r *report) lineRecord(doc reportDoc) lineRecord {
built := doc.Built
dropped := slices.Clone(r.dropped)
if dropped == nil {
dropped = []droppedEntry{}
}
slices.SortFunc(dropped, func(a, b droppedEntry) int { return a.Original - b.Original })
return lineRecord{Version: recordVersion, Line: doc.Line, Built: &built, Report: doc.ID, Closing: r.closing, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: dropped, Stale: slices.Clone(doc.Overlays.Stale),
Parked: carryAnnounced(doc.Primary.Parked, r.carried), Partial: slices.Clone(doc.Overlays.Partial), Changed: slices.Clone(doc.Overlays.Changed)}
}
// branchDate is the date a batch branch was opened under.
func branchDate(branch string) (time.Time, error) {
m := batchBranchRE.FindStringSubmatch(branch)
if m == nil {
return time.Time{}, fmt.Errorf("%q is not a batch branch", branch)
}
return time.Parse("20060102", m[2])
}
// cleanTree refuses a working tree with changes or an operation in progress:
// run switches it to the batch branch.
func cleanTree() error {
out, err := git("status", "--porcelain", "--untracked-files=no")
if err != nil {
return err
}
if strings.TrimSpace(out) != "" {
return errors.New("the working tree has changes; run in a clean worktree")
}
for _, name := range []string{"CHERRY_PICK_HEAD", "MERGE_HEAD", "REVERT_HEAD", "REBASE_HEAD", "rebase-merge", "rebase-apply", "sequencer"} {
p, err := git("rev-parse", "--git-path", name)
if err != nil {
return err
}
if _, err := os.Stat(strings.TrimSpace(p)); err == nil {
return fmt.Errorf("an operation is in progress (%s); finish or abort it first", name)
}
}
return nil
}
// warnReset says, before switch -C moves the local branch to start, which tip
// the move drops commits of and how to get them back: a clean tree says
// nothing about commits only a local branch has.
func (a *app) warnReset(branch, start string) error {
if err := a.mayAct(); err != nil {
return err
}
old, err := lookup("refs/heads/" + branch)
if err != nil || old == "" || old == start {
return err
}
kept, err := isAncestor(old, start)
if err != nil || kept {
return err
}
// The hint is the only way back to the dropped commits, so the reset does
// not run when it could not be written.
if _, err := fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old); err != nil {
return fmt.Errorf("not resetting the local %s: %w", branch, err)
}
return nil
}
// ontoBranch is the pushed batch branch whose head --onto names, read from the
// remote itself: a head that moved since, or that no batch branch has, is
// refused rather than continued.
func ontoBranch(l line, onto string, dated bool, dateBranch string) (string, error) {
heads, err := remoteBatchHeads(l)
if err != nil {
return "", err
}
var found []string
for branch, sha := range heads {
if sha == onto && (!dated || branch == dateBranch) {
found = append(found, branch)
}
}
switch {
case len(found) == 0 && dated:
return "", fmt.Errorf("--onto %.12s is not the head of %s on %s (%.12s)", onto, dateBranch, remote, heads[dateBranch])
case len(found) == 0:
return "", fmt.Errorf("--onto %.12s is not the head of any batch branch for %s on %s", onto, l.branch(), remote)
case len(found) > 1:
return "", fmt.Errorf("--onto %.12s heads more than one batch branch; name one with --date", onto)
}
have, err := lookup(onto)
if err != nil {
return "", err
}
if have == "" {
return "", fmt.Errorf("--onto %.12s is not in this repository; fetch it first", onto)
}
return found[0], nil
}
// remoteBatchHeads lists the batch branches for l on the remote, read with
// ls-remote rather than from the possibly stale remote-tracking refs.
func remoteBatchHeads(l line) (map[string]string, error) {
out, err := git("ls-remote", "--refs", "--heads", "--end-of-options", remote)
if err != nil {
return nil, err
}
heads := map[string]string{}
for ln := range strings.Lines(out) {
sha, ref, ok := strings.Cut(strings.TrimSpace(ln), "\t")
branch, isHead := strings.CutPrefix(ref, "refs/heads/")
if !ok || !isHead || checkSHA(sha) != nil {
continue
}
if m := batchBranchRE.FindStringSubmatch(branch); m != nil && m[1] == l.branch() {
heads[branch] = sha
}
}
return heads, nil
}
// applyPick runs one cherry-pick onto HEAD and leaves no operation in progress
// behind it: the outcome is picked, conflict, with the conflicting files, or
// empty.
func applyPick(args ...string) (string, []string, error) {
stdout, stderr, code, err := gitStatus(args...)
if err != nil {
return "", nil, err
}
if code == 0 {
return "picked", nil, nil
}
out, err := git("diff", "--name-only", "-z", "--diff-filter=U")
if err != nil {
return "", nil, err
}
if files := splitNUL(out); len(files) > 0 {
_, err := git("cherry-pick", "--abort")
return "conflict", files, err
}
if msg := stdout + stderr; strings.Contains(msg, "is now empty") || strings.Contains(msg, "nothing to commit") {
_, err := git("cherry-pick", "--skip")
return "empty", nil, err
}
failed := fmt.Errorf("failed: %s", strings.TrimSpace(stderr))
if _, err := git("cherry-pick", "--abort"); err != nil {
return "", nil, errors.Join(failed, err)
}
return "", nil, failed
}
// lineTemplate is the line's own pull request template, so the batch body has
// the sections reviewers of that line expect.
func lineTemplate(lineHead string) (string, error) {
const template = ".github/PULL_REQUEST_TEMPLATE.md"
out, err := git("--literal-pathspecs", "ls-tree", "--name-only", "--end-of-options", lineHead, "--", template)
if err != nil {
return "", err
}
if strings.TrimSpace(out) == "" {
return "## What this PR does\n\n### Release note\n\n```release-note\n\n```\n", nil
}
return git("show", "--end-of-options", lineHead+":"+template)
}
// publication is what a run publishes: the branch at head, and the body that
// carries the record and the report.
type publication struct {
branch, head, title, body string
report storedReport
record lineRecord
}
// publish pushes the batch branch, then opens the draft batch pull request or
// updates the open one. A run pushes only a fast-forward of what the remote
// has. A rebuild replaces the branch, and only the head it was rebuilt from:
// the lease is that head, as ls-remote reads it now, so a commit pushed since
// is never dropped.
//
// The report goes up as a comment before the push and the body that names
// it, so no write leaves a body naming a report nobody can read; a new batch
// carries the report in the body it is opened with, and the comment follows.
// Everything is rendered and measured before the first write.
func (a *app) publish(b *batch, p publication, rebuilt bool) error {
if b.pr != nil && b.pr.Head.Ref != p.branch {
return fmt.Errorf("the open batch pull request #%d is on %s; continue it with --onto %s", b.pr.Number, b.pr.Head.Ref, b.prHead)
}
// A body without the block was written by hand, and the tool's would
// replace it whole, title included; such a batch is the maintainer's.
if b.pr != nil && !b.src.open {
return fmt.Errorf("batch pull request #%d has no %s block, so backport-batch did not write it; merge or close it, and the next run opens a batch of its own", b.pr.Number, deferredFence)
}
if p.head == b.lineHead {
if rebuilt && p.record.Closing != nil {
return a.closeBatch(b, p)
}
return fmt.Errorf("%s carries nothing beyond %s; nothing to push", p.branch, b.line.branch())
}
heads, err := remoteBatchHeads(b.line)
if err != nil {
return err
}
pushed, onRemote := heads[p.branch]
lease := ""
switch {
case rebuilt:
if !onRemote || pushed != b.prHead {
return fmt.Errorf("%s on %s is at %.12s, not at %.12s, which the rebuild started from; run again", p.branch, remote, pushed, b.prHead)
}
lease = pushed
case onRemote && pushed != p.head:
have, err := lookup(pushed)
if err != nil {
return err
}
descends := false
if have != "" {
if descends, err = isAncestor(pushed, p.head); err != nil {
return err
}
}
if !descends {
refusal := fmt.Sprintf("%s on %s is at %.12s, which %.12s does not descend from; refusing a non-fast-forward push", p.branch, remote, pushed, p.head)
// A run that stopped between its push and opening the pull request
// leaves the branch, and the next run that day builds it anew.
switch c := b.src.carrier; {
case b.pr != nil:
case c != nil && !b.src.open && c.Head.Ref == p.branch:
// Continuing a batch that merged or closed would publish
// what nobody can merge any more.
refusal += fmt.Sprintf("; it is the branch of batch pull request #%d, which is %s; the next run picks again what the tool put on it, but not a commit someone pushed there, such as a resolved conflict: check what %.12s carries, then delete it with git push origin --delete %s and run again", c.Number, prState(c), pushed, p.branch)
default:
refusal += fmt.Sprintf("; no batch pull request is open on it, and backport-batch run --line %s --onto %s --push continues it and opens one", b.line.branch(), pushed)
}
return errors.New(refusal)
}
}
if err := p.fits(); err != nil {
return err
}
if b.pr != nil {
if err := a.keepReports(b, &p); err != nil {
return err
}
}
if pushed != p.head {
if err := a.mayAct(); err != nil {
return err
}
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
return err
}
}
name, err := writeTemp("backport-batch-body-*.md", p.body)
if err != nil {
return err
}
defer func() { _ = os.Remove(name) }()
if b.pr == nil {
n, url, err := a.gh.createPull(b.line.branch(), p.branch, p.title, name)
if err != nil {
return err
}
_, _ = fmt.Fprintf(a.stderr, "opened draft %s\n", url)
if err := a.postReport(n, p.report); err != nil {
return fmt.Errorf("posting report %.12s on #%d: %w; the body keeps it, and the next run --push posts it", p.report.doc.ID, n, err)
}
return nil
}
if err := a.stillOpen(b.pr.Number); err != nil {
return err
}
if err := a.gh.editPull(b.pr.Number, p.title, name); err != nil {
return err
}
_, _ = fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
return nil
}
// fits says why GitHub would not take the comment or the body.
func (p *publication) fits() error {
if _, err := reportComment(p.report); err != nil {
return err
}
if len(p.body) > bodyLimit {
return fmt.Errorf("the body is %d bytes with only what it cannot leave out, over GitHub's %d; nothing is published", len(p.body), bodyLimit)
}
return nil
}
// keepReports makes sure the open batch's comments keep both the report its
// body carries now and the one this run made, in that order, each once.
// When this run's report is one already kept, the body carries the kept
// bytes, which differ only in when and in which job it was made.
func (a *app) keepReports(b *batch, p *publication) error {
pr := b.pr
posted, bad, err := findReports(a.gh, pr.Number, b.line)
if err != nil {
return err
}
for _, why := range bad {
a.warn("batch pull request #%d: %s; it does not count as a report", pr.Number, why)
}
earlier, err := bodyReport(pr.Body, b.line)
if err != nil {
return fmt.Errorf("batch pull request #%d: %w", pr.Number, err)
}
kept, ok := posted[p.report.doc.ID]
if !ok && earlier != nil && earlier.doc.ID == p.report.doc.ID {
kept, ok = *earlier, true
}
if ok {
p.body = strings.Replace(p.body, reportBlock(p.report.raw), reportBlock(kept.raw), 1)
p.report = kept
if err := p.fits(); err != nil {
return err
}
}
if earlier != nil {
if _, ok := posted[earlier.doc.ID]; !ok {
if err := a.postReport(pr.Number, *earlier); err != nil {
return fmt.Errorf("posting the earlier report %.12s on #%d: %w", earlier.doc.ID, pr.Number, err)
}
posted[earlier.doc.ID] = *earlier
}
}
if _, ok := posted[p.report.doc.ID]; !ok {
if err := a.postReport(pr.Number, p.report); err != nil {
return fmt.Errorf("posting report %.12s on #%d: %w", p.report.doc.ID, pr.Number, err)
}
}
return nil
}
// pullURL is where a pull request is on GitHub.
func pullURL(pr *pullRequest) string {
if pullURLRE.MatchString(pr.HTMLURL) {
return pr.HTMLURL
}
return fmt.Sprintf("%spull/%d", ourRepoURL, pr.Number)
}
// closeBatch closes the open batch that --defer left carrying nothing. Every
// write happens while it is open: the report, then the body whose record
// says it is closing and at which head. The close and the deletion of its
// branch then go only while both are still at that head.
func (a *app) closeBatch(b *batch, p publication) error {
c := *p.record.Closing
if err := p.fits(); err != nil {
return err
}
if err := a.keepReports(b, &p); err != nil {
return err
}
name, err := writeTemp("backport-batch-body-*.md", p.body)
if err != nil {
return err
}
defer func() { _ = os.Remove(name) }()
if err := a.stillOpen(c.PR); err != nil {
return err
}
if err := a.gh.editPull(c.PR, p.title, name); err != nil {
return fmt.Errorf("recording the close of #%d: %w", c.PR, err)
}
return a.finishClose(b.line, pullURL(b.pr), c)
}
// finishClose closes the batch pull request a record says is closing, and
// deletes its branch, each only while it is at the head the record froze.
func (a *app) finishClose(l line, url string, c closing) error {
pr, err := a.gh.pull(c.PR)
if err != nil {
return fmt.Errorf("closing #%d, reading it again: %w", c.PR, err)
}
if st := prState(&pr); st != "open" || pr.Head.SHA != c.Head {
return fmt.Errorf("closing #%d: it is %s at %.12s, not open at %.12s, which its record froze", c.PR, st, terminalSafe(pr.Head.SHA), c.Head)
}
heads, err := remoteBatchHeads(l)
if err != nil {
return err
}
if h := heads[c.Branch]; h != c.Head {
return fmt.Errorf("closing #%d: %s on %s is at %.12s, not at %.12s, which its record froze", c.PR, c.Branch, remote, h, c.Head)
}
if err := a.gh.closePull(c.PR); err != nil {
return fmt.Errorf("closing #%d: %w; backport-batch run --line %s --push finishes it", c.PR, err, l.branch())
}
if err := a.deleteBranch(l, c); err != nil {
return err
}
_, _ = fmt.Fprintf(a.stderr, "closed %s: without what is deferred it carries nothing beyond %s; the next batch tries them again\n", url, l.branch())
return nil
}
// deleteBranch deletes the branch of a closed batch, leased on the head it
// was closed at: one someone pushed since is a person's to delete or keep.
func (a *app) deleteBranch(l line, c closing) error {
heads, err := remoteBatchHeads(l)
if err != nil {
return err
}
switch h, ok := heads[c.Branch]; {
case !ok:
return nil
case h != c.Head:
return movedBranch(c, h)
}
if err := a.mayAct(); err != nil {
return err
}
if _, err := git(deleteArgs(c.Branch, c.Head)...); err != nil {
return fmt.Errorf("deleting %s, left by the close of #%d: %w; backport-batch run --line %s --push deletes it", c.Branch, c.PR, err, l.branch())
}
return nil
}
// takenBranch is the refusal for a branch a close left that an open batch
// pull request now uses, whatever it holds, or nil.
func takenBranch(b *batch, c closing) error {
if b.pr == nil || b.pr.Head.Ref != c.Branch {
return nil
}
return fmt.Errorf("batch pull request #%d is open on %s, which the close of #%d left; the branch is that pull request's now, and the close leaves it alone", b.pr.Number, c.Branch, c.PR)
}
func movedBranch(c closing, h string) error {
return fmt.Errorf("batch pull request #%d was closed at %.12s, but %s is at %.12s now; the next run picks again what the tool put on it, but not a commit someone pushed there, such as a resolved conflict: check what %.12s carries, then delete it with git push origin --delete %s, and run again", c.PR, c.Head, c.Branch, h, h, c.Branch)
}
// resumeClose finishes a close the record says is under way, before a run
// does anything else, and only under --push. It says whether the run may go
// on: only once the batch is closed and its branch gone. A run that finishes
// one stops there, and fails when it was asked to change the batch.
func (a *app) resumeClose(b *batch, push, changing bool) (bool, int) {
if b.src.rec == nil || b.src.rec.Closing == nil {
return true, 0
}
c := *b.src.rec.Closing
heads, err := remoteBatchHeads(b.line)
if err != nil {
return false, a.fail(err)
}
h, onRemote := heads[c.Branch]
switch {
case b.src.open && h != c.Head:
return false, a.fail(fmt.Errorf("batch pull request #%d is being closed, but %s is at %.12s, not at %.12s, which its record froze; a person closes it, or pushes the branch back, by hand", c.PR, c.Branch, h, c.Head))
case b.src.open && !push:
return false, a.fail(fmt.Errorf("batch pull request #%d is being closed: closing it and deleting %s are left; backport-batch run --line %s --push finishes it", c.PR, c.Branch, b.line.branch()))
case b.src.open:
if err := a.finishClose(b.line, pullURL(b.src.carrier), c); err != nil {
return false, a.fail(err)
}
return false, a.finished(c, changing)
case !onRemote:
return true, 0
case takenBranch(b, c) != nil:
return false, a.fail(takenBranch(b, c))
case h != c.Head:
return false, a.fail(movedBranch(c, h))
case !push:
return false, a.fail(fmt.Errorf("batch pull request #%d was closed, but %s is still at %.12s; backport-batch run --line %s --push deletes it", c.PR, c.Branch, h, b.line.branch()))
}
if err := a.deleteBranch(b.line, c); err != nil {
return false, a.fail(err)
}
_, _ = fmt.Fprintf(a.stderr, "deleted %s, which the close of #%d left\n", c.Branch, c.PR)
return false, a.finished(c, changing)
}
// finished is the exit of a run that finished a close: a change it was asked
// for needs an open batch pull request, and none is left to make it on.
func (a *app) finished(c closing, changing bool) int {
if !changing {
return 0
}
return a.fail(fmt.Errorf("finished the close of #%d and made no other change: what this run was asked to change needs an open batch pull request, which the next run opens", c.PR))
}
// pushArgs pushes head to branch: as a fast-forward, which the remote checks,
// or, given a lease, over exactly that commit and no other.
func pushArgs(branch, head, lease string) []string {
args := []string{"push", "--quiet"}
if lease != "" {
args = append(args, "--force-with-lease=refs/heads/"+branch+":"+lease)
}
return append(args, "--end-of-options", remote, head+":refs/heads/"+branch)
}
// deleteArgs deletes branch, only while the remote has it at lease.
func deleteArgs(branch, lease string) []string {
return []string{"push", "--quiet", "--force-with-lease=refs/heads/" + branch + ":" + lease, "--end-of-options", remote, ":refs/heads/" + branch}
}
+318
View File
@@ -0,0 +1,318 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// A merged pull request lands as its own commits, each picked with -x and
// keeping its author and sign-offs, under a merge commit that carries the
// merge message from main and names it; a squash lands as one pick; a merge
// of main into the pull request's branch is left out; all in main's merge
// order.
func TestRunPicksInMergeOrder(t *testing.T) {
f := newFixture(t)
f.lines()
signoff := "Signed-off" + "-by: " + contributor
mc5 := f.prCommits(5, "fix(app): five", "Body.\n\n```release-note\nfix(app): five is fixed\n```",
change{files: map[string]string{"app/five.txt": "5\n"}, author: contributor, message: "fix(app): start five\n\n" + signoff},
change{files: map[string]string{"app/five.txt": "5, done\n", "app/five-more.txt": "more\n"}, author: contributor, message: "fix(app): finish five\n\n" + signoff})
sq6 := f.squash(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "feat(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
mc8 := f.prCommits(8, "fix(app): eight", "",
change{files: map[string]string{"app/eight.txt": "8\n"}},
change{mergeMain: true},
change{files: map[string]string{"app/eight.txt": "8, after main\n"}})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.git("branch", "--show-current"); got != batchBranch1 {
t.Fatalf("on %q, want %s", got, batchBranch1)
}
want := []string{"fix(app): five (#5)", "fix(app): six (#6)", "fix(app): eight (#8)"}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, want) {
t.Errorf("the batch carries %q, want %q", got, want)
}
line := f.git("rev-parse", "origin/release-1.1")
u5 := f.git("rev-parse", "HEAD~2")
if got := f.git("rev-parse", u5+"^1"); got != line {
t.Errorf("#5's merge sits on %.12s, want the line's head %.12s", got, line)
}
if msg := f.git("log", "-1", "--format=%B", u5); !strings.HasSuffix(msg, "\n\n"+batchTrailer+": "+mc5) || !strings.HasPrefix(msg, "fix(app): five (#5)") {
t.Errorf("#5's merge does not carry the merge message and name %s:\n%s", mc5, msg)
}
own := strings.Fields(f.git("rev-list", "--reverse", mc5+"^1.."+mc5+"^2"))
picks := strings.Fields(f.git("rev-list", "--reverse", u5+"^2", "^"+u5+"^1"))
if len(picks) != 2 || len(own) != 2 {
t.Fatalf("#5 brought %d picks of its %d commits, want 2", len(picks), len(own))
}
for i, c := range picks {
msg := f.git("log", "-1", "--format=%B", c)
if got := f.git("log", "-1", "--format=%an <%ae>", c); got != contributor {
t.Errorf("pick %d is authored by %q, want %q", i, got, contributor)
}
if !strings.Contains(msg, signoff+"\n(cherry picked from commit "+own[i]+")\nSigned-off-by: Batch Test <batch@example.invalid>") {
t.Errorf("pick %d lacks the contributor's sign-off, its -x line for %s or the batch's sign-off:\n%s", i, own[i], msg)
}
}
u6 := f.git("rev-parse", "HEAD~1")
if parents := strings.Fields(f.git("rev-list", "--parents", "-n", "1", u6)); len(parents) != 2 || parents[1] != u5 {
t.Errorf("#6 is %v, want one commit on #5's merge", parents)
}
if msg := f.git("log", "-1", "--format=%B", u6); !strings.Contains(msg, "(cherry picked from commit "+sq6+")") {
t.Errorf("#6's pick lacks its -x line:\n%s", msg)
}
picks8 := strings.Fields(f.git("rev-list", "--reverse", "HEAD^2", "^HEAD^1"))
if got := len(picks8); got != 2 {
t.Errorf("#8 brought %d commits, want its own 2 without the merge of main", got)
}
if !strings.HasSuffix(f.git("log", "-1", "--format=%B", "HEAD"), batchTrailer+": "+mc8) {
t.Error("#8's merge does not name its merge commit")
}
if f.git("rev-parse", "HEAD:app/eight.txt") != f.git("rev-parse", mc8+":app/eight.txt") {
t.Error("app/eight.txt is not what main has after #8")
}
for _, s := range []string{"[#5]", "[#6]", "[#8]", "fix(app): five is fixed", "Rule 2", "`fix(app): five`, 2 commits", "`fix(app): six`, 1 commit"} {
if !strings.Contains(stdout, s) {
t.Errorf("the body lacks %q:\n%s", s, stdout)
}
}
if code, out, stderr := f.exec(nil, "verify", "--line", "release-1.1", "--head", f.git("rev-parse", "HEAD")); code != 0 {
t.Errorf("verify of the batch = %d\n%s\n%s", code, out, stderr)
}
}
// A conflict is aborted, recorded with its files, and the next original is
// still picked.
func TestRunRecordsConflictsAndGoesOn(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
f.pr(5, "fix(chart): five", "", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("picked %q, want only #6", got)
}
if out := f.git("status", "--porcelain"); out != "" {
t.Errorf("the tree is left dirty:\n%s", out)
}
if _, err := os.Stat(filepath.Join(f.dir, ".git", "CHERRY_PICK_HEAD")); err == nil {
t.Error("a cherry-pick is left in progress")
}
l, _ := parseLine("release-1.1")
deferred, err := parseDeferred(stdout, l)
if err != nil || len(deferred[5].Files) == 0 || len(deferred) != 1 {
t.Errorf("deferred = %v, %v; want #5", deferred, err)
}
if !strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, "`chart/values.yaml`") {
t.Errorf("the body does not name the conflict and its file:\n%s", stdout)
}
}
// An original the earlier picks already carry is skipped as an empty pick.
func TestRunSkipsAnEmptyPick(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): add x", "", map[string]string{"app/x.txt": "x\n"})
f.pr(6, "revert(app): drop x", "", map[string]string{"app/x.txt": ""})
f.pr(7, "fix(app): add x again", "", map[string]string{"app/x.txt": "x\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): add x (#5)"}) {
t.Errorf("picked %q, want only #5", got)
}
if !strings.Contains(stdout, "the earlier picks already carry it") {
t.Errorf("#7 is not reported as carried by the earlier picks:\n%s", stdout)
}
}
// A hook in the repository or the user's configuration never rewrites what a
// pick carries.
func TestRunIgnoresHooks(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
hooks := filepath.Join(f.dir, ".git", "hooks")
for _, hook := range []string{"prepare-commit-msg", "commit-msg"} {
if err := os.WriteFile(filepath.Join(hooks, hook), []byte("#!/bin/sh\necho rewritten by a hook > \"$1\"\n"), 0o755); err != nil {
t.Fatal(err)
}
}
if code, _, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): five (#5)"}) {
t.Errorf("picked %q, want #5 as it was merged", got)
}
}
func TestRunRefusesADirtyTree(t *testing.T) {
f := newFixture(t)
f.lines()
f.write("README.md", "changed\n")
code, _, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 2 || !strings.Contains(stderr, "has changes") {
t.Errorf("run on a dirty tree = %d (%s), want 2", code, stderr)
}
if f.git("branch", "--show-current") != "main" {
t.Error("the tree was switched anyway")
}
// A pick that stopped empty leaves the tree clean and the pick going.
f.git("checkout", "-q", "--", "README.md")
if err := os.WriteFile(filepath.Join(f.dir, ".git", "CHERRY_PICK_HEAD"), []byte(f.git("rev-parse", "HEAD")+"\n"), 0o644); err != nil {
t.Fatal(err)
}
code, _, stderr = f.exec(nil, "run", "--line", "release-1.1")
if code != 2 || !strings.Contains(stderr, "operation is in progress (CHERRY_PICK_HEAD)") {
t.Errorf("run during a cherry-pick = %d (%s), want 2", code, stderr)
}
}
// A clean tree says nothing about commits only the local batch branch has, so
// a run or a rebuild that resets that branch over them names its tip and the
// command that gets them back; one that drops nothing says nothing.
func TestResetNamesWhatItDrops(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
unpushed := func(base string) string {
t.Helper()
f.git("switch", "-q", "-C", batchBranch1, base)
f.write("app/mine.txt", "unpushed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "an unpushed resolution")
tip := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
return tip
}
notice := func(stderr, tip string) bool {
t.Helper()
restore := "git branch " + batchBranch1 + "-" + tip[:12] + " " + tip
if !strings.Contains(stderr, "resetting the local "+batchBranch1+" from "+tip+",") || !strings.Contains(stderr, restore) {
return false
}
f.git(strings.Fields(restore)[1:]...)
if got := f.git("rev-parse", batchBranch1+"-"+tip[:12]); got != tip {
t.Errorf("%s restored %.12s, want %.12s", restore, got, tip)
}
return true
}
tip := unpushed("origin/release-1.1")
if code, _, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !notice(stderr, tip) {
t.Errorf("run over an unpushed commit = %d, want it named with its restore:\n%s", code, stderr)
}
f.git("switch", "-q", "main")
f.git("branch", "-f", batchBranch1, "origin/release-1.1~1")
if code, _, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stderr, "resetting") {
t.Errorf("run over a branch it fast-forwards = %d, want no notice:\n%s", code, stderr)
}
f.git("switch", "-q", "main")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
tip = unpushed(head)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the app e2e"); code != 0 || !notice(stderr, tip) {
t.Errorf("a rebuild over an unpushed commit = %d, want it named with its restore:\n%s", code, stderr)
}
}
// --push pushes a fresh branch and opens the draft, and a second push of the
// same branch updates the open pull request instead.
func TestRunPush(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
if !strings.Contains(stderr, "opened draft https://github.com/cozystack/cozystack/pull/90\n") {
t.Errorf("the run does not print the pull request it opened:\n%s", stderr)
}
l, _ := parseLine("release-1.1")
if _, err := parseDeferred(stdout, l); err != nil || !strings.HasPrefix(stdout, "## What this PR does") || strings.Contains(stdout, "https://github.com/cozystack/cozystack/pull/90") {
t.Errorf("stdout is no longer the body alone (%v):\n%s", err, stdout)
}
head := f.git("rev-parse", "HEAD")
if got := f.remoteHead(batchBranch1); got != head {
t.Errorf("remote %s = %q, want %s", batchBranch1, got, head)
}
if want := []string{"release-1.1 " + batchBranch1 + " fix(backport): batch for release-1.1, 2026-10-26"}; !slices.Equal(gh.created, want) {
t.Errorf("created %q, want %q", gh.created, want)
}
f.git("switch", "-q", "main")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.bodies[0])}
code, _, stderr = f.exec(gh, "run", "--line", "release-1.1", "--onto", head, "--push")
if code != 0 {
t.Fatalf("run --onto --push = %d\n%s", code, stderr)
}
if !strings.Contains(stderr, "updated https://github.com/cozystack/cozystack/pull/90\n") {
t.Errorf("the run does not print the pull request it updated:\n%s", stderr)
}
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): five (#5)", "fix(app): six (#6)"}) {
t.Errorf("the pushed batch carries %q, want #5 then #6", got)
}
if len(gh.edited) != 1 || gh.edited[0] != "#90 fix(backport): batch for release-1.1, 2026-10-26" {
t.Errorf("edited %q, want #90", gh.edited)
}
if body := gh.bodies[len(gh.bodies)-1]; !strings.Contains(body, "[#5]") || !strings.Contains(body, "[#6]") {
t.Errorf("the updated body does not list both originals:\n%s", body)
}
}
// With a batch pull request open on another branch, a fresh push would open
// a second one; it is refused before anything is pushed.
func TestRunPushRefusesASecondBatch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
old := "backport-batch/release-1.1-20261022"
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+old)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", old, "")}}
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 2 || !strings.Contains(stderr, "continue it with --onto") {
t.Errorf("run --push = %d (%s), want a refusal", code, stderr)
}
if f.remoteHead(batchBranch1) != "" || len(gh.created)+len(gh.edited) != 0 {
t.Error("something was pushed or opened")
}
}
+86
View File
@@ -0,0 +1,86 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"slices"
"strings"
"testing"
)
// A maintainer's run by hand continues the batch pull request the scheduled
// run opened as its bot, with nothing set: it edits that pull request rather
// than opening a second one for the line. A record another bot wrote is no
// record the batch trusts, and the run stops rather than reach past it.
func TestHandRunContinuesTheBotsBatch(t *testing.T) {
for _, tc := range []struct {
name, author string
continued bool
}{
{"the scheduled run's bot", "cozystack-ci[bot]", true},
{"another bot", "renovate[bot]", false},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{bot: batchBotLogin(func(string) (string, bool) { return "", false })}
openBatch(t, f, gh)
gh.prs[0].User.Login, gh.prs[0].User.Type = tc.author, "Bot"
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.now = f.now.AddDate(0, 0, 3)
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if tc.continued && code != 0 || !tc.continued && (code != 2 || !strings.Contains(stderr, "batch pull request #90 (open) carries a ```backport-batch block but was opened by \"renovate[bot]\"")) {
t.Fatalf("run --continue = %d\n%s", code, stderr)
}
continued := slices.Equal(gh.edited, []string{"#90 fix(backport): batch for release-1.1, 2026-10-26"}) && len(gh.created) == 1
if continued != tc.continued || len(gh.created) != 1 {
t.Errorf("edited %q, created %q; want the batch of %s continued=%v and no second one\n%s", gh.edited, gh.created, tc.author, tc.continued, stderr)
}
if !tc.continued && !strings.Contains(stderr, "ignoring #90") {
t.Errorf("the run does not say it ignores #90:\n%s", stderr)
}
})
}
}
// The bot comes from BACKPORT_BATCH_BOT when it is set, empty included, and
// is the scheduled run's own bot when it is not.
func TestBatchBotLogin(t *testing.T) {
env := func(v string, ok bool) func(string) (string, bool) {
return func(key string) (string, bool) {
if key != batchBotEnv {
t.Errorf("looked up %s", key)
}
return v, ok
}
}
for _, tc := range []struct {
name, v string
ok bool
want string
}{
{"unset", "", false, "cozystack-ci[bot]"},
{"set empty", "", true, ""},
{"set", "other-app[bot]", true, "other-app[bot]"},
} {
if got := batchBotLogin(env(tc.v, tc.ok)); got != tc.want {
t.Errorf("%s: batchBotLogin = %q, want %q", tc.name, got, tc.want)
}
}
}
+632
View File
@@ -0,0 +1,632 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"regexp"
"slices"
"strconv"
"strings"
"time"
"unicode"
)
// Titles, release notes and file names in the body were written by whoever
// opened the originals, and the body is published under the name of whoever
// runs --push. None of it is searched for what might be dangerous and then
// rendered by other rules: a title, a reason or a file name goes whole into a
// code span, and the release notes into the fenced release-note block, both
// of which GitHub shows as written, with no mention, reference, link or HTML
// in them acted on. neutralNote is for a note copied out of that block.
const (
ourRepoURL = "https://github.com/" + upstreamRepo + "/"
// tasksURL is where the instructions for the work a batch leaves live.
tasksURL = ourRepoURL + "blob/main/docs/release/backport-tasks/"
)
const (
titleLimit = 200
// GitHub refuses a pull request body longer than this.
bodyLimit = 65536
)
var (
backtickRunRE = regexp.MustCompile("`+")
fenceRunRE = regexp.MustCompile("`{3,}|~{3,}")
mdLinkRE = regexp.MustCompile(`!?\[([^\]]*)\]\(([^)]*)\)`)
urlRE = regexp.MustCompile("(?i)\\b(?:https?|ftp)://[^\\s<>()\\[\\]`]+|\\bwww\\.[^\\s<>()\\[\\]`]+")
// A closing keyword with its target, a cross-repository reference and a
// mention, wherever they stand: no exception for what precedes them, so
// nothing depends on agreeing with GitHub about word boundaries.
actingRE = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\b\s*:?\s*(?:(?:[\w.-]+/[\w.-]+)?#\d+|https?://github\.com/[\w.-]+/[\w.-]+/(?:issues|pull)/\d+)` +
`|[\w.-]+/[\w.-]+#\d+|@[A-Za-z0-9][A-Za-z0-9-]*(?:/[A-Za-z0-9_.-]+)?`)
// A link into this repository survives, unless a dot segment or an escape
// could take it somewhere else.
safeURLRE = regexp.MustCompile(`^` + regexp.QuoteMeta(ourRepoURL) + `[\w/?=&#+.-]*$`)
releaseNotRE = regexp.MustCompile("(?ms)^[ \t]*```+[ \t]*release-note[ \t]*\n(.*?)^[ \t]*```")
headingRE = regexp.MustCompile(`^#{1,6} `)
placeholders = []string{"", "none", "n/a", "na", "no", "-", "nothing"}
)
// clean drops what is not text: control characters other than tab and, when
// asked, newline, which a terminal acts on, and format characters such as the
// zero-width and bidi ones, which make text read as something it is not.
// Invalid UTF-8, a raw 8-bit escape among it, becomes U+FFFD.
func clean(s string, newlines bool) string {
var b strings.Builder
for _, r := range strings.ToValidUTF8(s, "\uFFFD") {
switch {
case r == '\t', r == '\n' && newlines:
b.WriteRune(r)
case r == '\r' || unicode.Is(unicode.Cf, r):
case r == '\n', r == '\u2028', r == '\u2029':
b.WriteRune(' ')
case unicode.IsControl(r):
b.WriteRune('\uFFFD')
default:
b.WriteRune(r)
}
}
return b.String()
}
func oneLine(s string) string {
return strings.Join(strings.FieldsFunc(s, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }), " ")
}
func capped(s string, limit int) string {
if r := []rune(s); len(r) > limit {
return strings.TrimRightFunc(string(r[:limit-1]), unicode.IsSpace) + "…"
}
return s
}
// codeSpan puts s on one line in a code span: its fence is longer than any
// backtick run inside, so nothing in s can close it.
func codeSpan(s string) string {
s = oneLine(clean(s, false))
longest := 0
for _, run := range backtickRunRE.FindAllString(s, -1) {
longest = max(longest, len(run))
}
fence := strings.Repeat("`", longest+1)
pad := ""
if s == "" || strings.HasPrefix(s, "`") || strings.HasSuffix(s, "`") {
pad = " "
}
return fence + pad + s + pad + fence
}
// titleSpan is a carried title as the body shows it.
func titleSpan(s string) string { return codeSpan(capped(oneLine(clean(s, false)), titleLimit)) }
func prLink(n int) string {
return fmt.Sprintf("[#%d](%spull/%d)", n, ourRepoURL, n)
}
func ours(url string) bool {
return safeURLRE.MatchString(url) && !strings.Contains(url, "/.")
}
// inertSigns swaps the characters GitHub acts on for their fullwidth forms,
// which read the same and act on nothing. Unlike a code span, the swap does
// not depend on how the backticks around it pair up.
var inertSigns = strings.NewReplacer("@", "@", "#", "#", "://", "://")
// neutralNote makes a release note safe inside the fenced block and inert
// outside it: no run of backticks or tildes is left to close the fence, links
// lose their target unless it is this repository, and closing keywords,
// cross-repository references and mentions lose the signs that make them
// act. Each step runs on the output of the one before, so a mention a link's
// text carried is still found.
func neutralNote(s string) string {
s = clean(s, true)
s = mdLinkRE.ReplaceAllString(s, "$1 ($2)")
s = urlRE.ReplaceAllStringFunc(s, func(u string) string {
if ours(u) {
return u
}
return "(link removed)"
})
s = actingRE.ReplaceAllStringFunc(s, inertSigns.Replace)
return fenceRunRE.ReplaceAllStringFunc(s, func(f string) string { return strings.Repeat("'", len(f)) })
}
// releaseNote is the release-note block of a body, or "" when it has none or
// only a placeholder.
func releaseNote(body string) string {
m := releaseNotRE.FindStringSubmatch(htmlCommentRE.ReplaceAllString(body, ""))
if m == nil {
return ""
}
note := strings.TrimSpace(m[1])
if slices.Contains(placeholders, strings.Trim(strings.ToLower(note), " .")) {
return ""
}
return note
}
// deferredFence opens the block check reads the deferred conflicts from.
const deferredFence = "```backport-batch"
// deferredEntry is a conflict, with the commit that failed and its files, an
// original --defer took out of the batch, with the reason given, or one that
// waits for a maintainer because it changes workflows, with those files.
type deferredEntry struct {
Original int `json:"original"`
Commit string `json:"commit,omitempty"`
Files []string `json:"files"`
More int `json:"more,omitempty"`
Reason string `json:"reason,omitempty"`
Workflows bool `json:"workflows,omitempty"`
// Needs, from version 3, are the originals still to come that it waits
// on, those of the kind that decides the wait.
Needs []int `json:"needs,omitempty"`
}
// How many conflicting files of one original the body names, in the list and
// in the block: a vendored chart can conflict in hundreds.
const (
filesListed = 5
filesKept = 50
)
type deferredRecord struct {
Line string `json:"line"`
Deferred []deferredEntry `json:"deferred"`
}
// deferredBlock is a version 1 record, which batches carried before records
// had a version. encoding/json leaves bidi and zero-width characters as they
// are, so file names go through clean first, as the reason does.
func deferredBlock(l line, conflicts, held, waiting []pick) string {
data, _ := json.Marshal(deferredRecord{Line: l.branch(), Deferred: deferredEntries(conflicts, held, waiting, nil)})
return deferredFence + "\n" + string(data) + "\n```"
}
func cleanFiles(files []string) []string {
out := make([]string, len(files))
for i, f := range files {
out[i] = clean(f, false)
}
return out
}
// parseDeferred reads what a batch body's record defers.
func parseDeferred(body string, l line) (map[int]deferredEntry, error) {
rec, err := parseRecord(body, l)
if err != nil {
return nil, err
}
return rec.deferred(), nil
}
// prTitle is built from the line and the date alone, never from carried text.
func prTitle(l line, date string) string {
return fmt.Sprintf("fix(backport): batch for %s, %s", l.branch(), date)
}
// skipRules are the rules a skipped original is reported under, in order.
var skipRules = []int{ruleBeforeStart, ruleOnLine, ruleType, ruleBreaking, ruleTouches, ruleNewer, ruleDropped}
// report is what one run put on the batch branch and what it left out.
type report struct {
line line
newer *line
onBranch []*verdict // carried from --onto and picked now, in merge order
deferred []pick // conflicts
held []pick // what --defer took out, with the reason
waiting []pick // what changes workflows, left to a maintainer
needs []pick // what waits on other originals still to come
skipped map[int][]*verdict
other int
// overrides are the originals a backport/ label kept out or let in, and
// stale those with backport/skip that are still on the batch.
overrides []*verdict
stale []*verdict
start time.Time
// shapes says, for an original on the branch, what it brought, and
// partialFiles what a partial unit leaves without effect.
shapes map[*verdict]unitShape
partialFiles map[*verdict][]string
// missing are originals the policy takes that the run neither tried nor
// found anywhere.
missing []*verdict
// opts is how the run was asked to classify, and built what it read.
opts runOptions
built builtFrom
// blocks are the record and the report the body ends with.
blocks string
// closing is the close of the open batch this run leaves carrying
// nothing.
closing *closing
// dropped are the maintainers' drops the record keeps.
dropped []droppedEntry
// parked are what the batch leaves out because it cannot land whole,
// each with its park, and partial the originals on the branch whose unit
// does only part of what they do on main.
parked []*verdict
partial []*verdict
// carried is the line's record this run found, whose announcements the
// parks that did not change keep.
carried *lineRecord
}
// ruleName is how output names what kept an original out: the policy's
// number for its own rules, and words for what the policy does not number.
func ruleName(rule int) string {
switch rule {
case ruleSkipLabel:
return labelSkip
case ruleBeforeStart:
return "rule 1, before the line's start"
case ruleDropped:
return "a maintainer's drop"
}
return "rule " + strconv.Itoa(rule)
}
// listTitle titles the list of what a rule skipped, a drop among them.
func (r *report) listTitle(rule int) string {
if rule == ruleDropped {
return "Dropped from " + codeSpan(r.line.branch()) + " by a maintainer"
}
return r.ruleTitle(rule)
}
func (r *report) ruleTitle(rule int) string {
switch rule {
case ruleBeforeStart:
return "Rule 1, merged before " + codeSpan(r.line.branch()) + " started under the policy on " + r.start.Format(time.DateOnly)
case ruleOnLine:
return "Rule 1, already on " + codeSpan(r.line.branch())
case ruleType:
return "Rule 2, the title type is not `fix` or `security`, or the scope is `deps`"
case ruleBreaking:
return "Rule 3, a breaking change"
case ruleTouches:
return "Rule 4, touches platform migrations, `targetVersion` or a CRD, or makes a sizeable API change"
case ruleNewer:
return "Rule 6, not on " + codeSpan(r.newer.branch()) + " yet"
}
return fmt.Sprintf("Rule %d", rule)
}
// what is the What this PR does section, with the skipped lists of the rules
// in details written out.
func (r *report) what(details map[int]bool) string {
var b strings.Builder
fmt.Fprintf(&b, "Backports to %s from `main`, picked by the rules of the [backport policy](%s) in merge order. A merged pull request comes as its own commits, picked with `git cherry-pick -x` so each keeps its author, message and sign-offs, and a merge commit that carries the original's merge message with a `%s:` trailer naming it; a squash-merged one comes as one pick. `backport-batch verify` checks that shape. A pick listed as changed differs from the commit it came from, and its release note waits under Check these notes. A conflict or a failing check here is a [backport task](%sREADME.md).\n\n", codeSpan(r.line.branch()), policyURL, batchTrailer, tasksURL)
if len(r.onBranch) == 0 {
b.WriteString("Nothing is picked yet.\n")
}
for _, v := range r.onBranch {
shape := ""
if s, ok := r.shapes[v]; ok {
shape = ", " + s.String()
}
fmt.Fprintf(&b, "- %s %s%s\n%s", prLink(v.o.number), titleSpan(v.o.title), shape, diagItems(v))
}
if len(r.deferred) > 0 {
fmt.Fprintf(&b, "\n### Deferred conflicts\n\nThese do not pick cleanly onto this branch. Each names the first of its commits that conflicted and the files it conflicted in, and nothing of it is on the branch. Each moves to the next batch, unless a resolution checked with `backport-batch fold` is pushed here first, as [resolving a conflict](%sresolve-conflict.md) describes.\n\n", tasksURL)
for _, p := range r.deferred {
var files []string
for _, f := range p.files[:min(len(p.files), filesListed)] {
files = append(files, codeSpan(f))
}
if more := len(p.files) - len(files) + p.more; more > 0 {
files = append(files, fmt.Sprintf("%d more", more))
}
at := ""
if p.failed != "" {
at = " at " + codeSpan(p.failed[:12])
}
fmt.Fprintf(&b, "- %s %s%s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), at, strings.Join(files, ", "), diagItems(p.v))
}
}
if len(r.held) > 0 {
b.WriteString("\n### Deferred to the next batch\n\nTaken out of this batch with `backport-batch run --defer`, each for the reason given. This pull request keeps them out; the next batch picks them again.\n\n")
for _, p := range r.held {
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), titleSpan(p.reason), diagItems(p.v))
}
}
if len(r.waiting) > 0 {
fmt.Fprintf(&b, "\n### Waiting for a maintainer\n\nThese change `%s`, which the token this batch runs with may not push, so it leaves them out. The policy still takes each, and `backport-batch check` lists it until it is on the line. A maintainer brings one over as [changes to workflows](%s#changes-to-workflows) describes.\n\n", workflowsDir, policyURL)
for _, p := range r.waiting {
var files []string
for _, f := range p.files[:min(len(p.files), filesListed)] {
files = append(files, codeSpan(f))
}
if more := len(p.files) - len(files) + p.more; more > 0 {
files = append(files, fmt.Sprintf("%d more", more))
}
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), strings.Join(files, ", "), diagItems(p.v))
}
}
if changed := r.changed(); len(changed) > 0 {
b.WriteString("\n### Check these notes\n\nThese picks differ from their originals, so their release notes are left out of the release-note block until someone checks that each still holds for this line. Each comes with what to compare: every pick that differs against the commit it came from, and the original's change against the unit's.\n")
for _, v := range changed {
s := r.shapes[v]
fmt.Fprintf(&b, "\n%s %s, carried by %s:\n\n", prLink(v.o.number), titleSpan(v.o.title), codeSpan(s.unit[:12]))
if n := releaseNote(v.o.body); n != "" {
fmt.Fprintf(&b, "```text\n%s\n```\n\n", neutralNote(n))
} else {
b.WriteString("It has no release note.\n\n")
}
for _, p := range s.pairs {
fmt.Fprintf(&b, "- %s\n", codeSpan(fmt.Sprintf("git range-diff --creation-factor=999 %s^! %s^!", p.source[:12], p.pick[:12])))
}
fmt.Fprintf(&b, "- %s\n- %s\n", codeSpan(fmt.Sprintf("git diff %s^1 %s", v.o.merge[:12], v.o.merge[:12])), codeSpan(fmt.Sprintf("git diff %s^1 %s", s.unit[:12], s.unit[:12])))
}
}
if len(r.partial) > 0 {
fmt.Fprintf(&b, "\n### Still on this branch: only part of it applies\n\nEach of these is on this branch but leaves files of its original as they were on %s, so it carries only part of what it does on `main`. Take each out with `backport-batch run --defer` before the cut; what applies of it goes as a pull request of its own.\n\n", codeSpan(r.line.branch()))
for _, v := range r.partial {
fmt.Fprintf(&b, "- %s %s: leaves %s without effect\n", prLink(v.o.number), titleSpan(v.o.title), fileSpans(r.partialFiles[v]))
}
}
if len(r.parked) > 0 {
fmt.Fprintf(&b, "\n### Parked\n\nThese cannot land on %s whole, so the batch does not pick them: each would leave part of it without effect there, or builds on a change that never reaches the line. A maintainer ports what applies as a pull request of its own, or drops the original with `backport-batch run --drop`; `backport/include` does not lift a park.\n\n", codeSpan(r.line.branch()))
for _, v := range r.parked {
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(v.o.number), titleSpan(v.o.title), parkReason(*v.park), diagItems(v))
}
}
if len(r.needs) > 0 {
fmt.Fprintf(&b, "\n### Waiting on another original\n\nThese build on another original that has not reached %s yet, so the batch does not try them; each names the one it waits on, and the batch tries it again once that one is there.\n\n", codeSpan(r.line.branch()))
for _, p := range r.needs {
fmt.Fprintf(&b, "- %s %s: after %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), prLinks(p.needs), diagItems(p.v))
}
}
if len(r.overrides) > 0 {
b.WriteString("\n### Maintainer overrides\n\n")
for _, v := range r.overrides {
fmt.Fprintf(&b, "- %s %s: %s, %s\n", prLink(v.o.number), titleSpan(v.o.title), codeSpan(v.label), r.outcome(v))
}
}
b.WriteString("\n### Skipped\n\n")
for _, rule := range skipRules {
items := r.skipped[rule]
if len(items) == 0 {
continue
}
left := ""
if !details[rule] {
left = ", too many to list here; the run's output lists them"
}
fmt.Fprintf(&b, "- %s: %d%s\n", r.listTitle(rule), len(items), left)
}
if r.other > 0 {
fmt.Fprintf(&b, "- Commits on `main` that merged no pull request: %d\n", r.other)
}
for _, rule := range skipRules {
items := r.skipped[rule]
if len(items) > 0 && details[rule] {
fmt.Fprintf(&b, "\n<details><summary>%s</summary>\n\n", r.listTitle(rule))
for _, v := range items {
fmt.Fprintf(&b, "- %s %s, %s\n", prLink(v.o.number), titleSpan(v.o.title), titleSpan(v.reason))
}
b.WriteString("\n</details>\n")
}
}
b.WriteString("\n" + r.blocks + "\n")
return b.String()
}
// outcome is what became of an original a label overrode.
func (r *report) outcome(v *verdict) string {
switch {
case slices.Contains(r.partial, v):
return "still on this branch, but only part of it applies; take it out with `backport-batch run --defer`"
case slices.Contains(r.stale, v) && v.rule == ruleDropped:
return "dropped, but still on this branch; take it out with `backport-batch run --defer`"
case slices.Contains(r.stale, v):
return "still on this branch, picked before the label; take it out with `backport-batch run --defer`"
case v.label == labelSkip:
return "left out"
case slices.Contains(r.onBranch, v):
return "on this branch"
case slices.ContainsFunc(r.deferred, func(p pick) bool { return p.v == v }):
return "a deferred conflict"
case slices.ContainsFunc(r.held, func(p pick) bool { return p.v == v }):
return "deferred to the next batch"
case slices.ContainsFunc(r.waiting, func(p pick) bool { return p.v == v }):
return "waiting for a maintainer: it changes " + codeSpan(workflowsDir)
case slices.Contains(r.parked, v):
return "parked: " + parkReason(*v.park)
case slices.ContainsFunc(r.needs, func(p pick) bool { return p.v == v }):
i := slices.IndexFunc(r.needs, func(p pick) bool { return p.v == v })
return "waiting on " + prLinks(r.needs[i].needs)
case v.rule != 0:
return "skipped by " + ruleName(v.rule) + ", " + titleSpan(v.reason)
}
return "not picked"
}
// diagItems lists, under an original's line, what could not be judged about
// it or what it was seen to build on, each as a code span.
func diagItems(v *verdict) string {
var b strings.Builder
for _, d := range v.diag {
fmt.Fprintf(&b, " - %s\n", titleSpan(d))
}
return b.String()
}
// fileSpans lists files as code spans, the first few of them.
func fileSpans(files []string) string {
var out []string
for _, f := range files[:min(len(files), filesListed)] {
out = append(out, codeSpan(f))
}
if more := len(files) - len(out); more > 0 {
out = append(out, fmt.Sprintf("%d more", more))
}
return strings.Join(out, ", ")
}
// prLinks links to pull requests by number.
func prLinks(numbers []int) string {
var out []string
for _, n := range numbers {
out = append(out, prLink(n))
}
return strings.Join(out, ", ")
}
// parkReason says why a park leaves its original out, for the body.
func parkReason(p parkedEntry) string {
if p.Kind == parkPartial {
return "only part of it applies; it would leave " + fileSpans(p.Files) + " without effect"
}
var needs []string
for _, n := range p.Needs {
needs = append(needs, prLink(n))
}
for _, c := range p.Commits {
needs = append(needs, codeSpan(c[:12]))
}
return "builds on " + strings.Join(needs, ", ") + ", which never reaches this line"
}
// changed lists the originals on the branch whose unit differs from them, in
// merge order.
func (r *report) changed() []*verdict {
var out []*verdict
for _, v := range r.onBranch {
if s, ok := r.shapes[v]; ok && s.changed() {
out = append(out, v)
}
}
return out
}
// notes are the release notes of what the branch carries as its originals
// made it; a changed pick's note waits under Check these notes.
func (r *report) notes() string {
var notes []string
for _, v := range r.onBranch {
if s, ok := r.shapes[v]; ok && s.changed() {
continue
}
if n := releaseNote(v.o.body); n != "" {
notes = append(notes, neutralNote(n))
}
}
return strings.Join(notes, "\n\n")
}
// section is one heading of the pull request template and what follows it.
type section struct {
heading string
text string
}
func sections(template string) (string, []section) {
var lead strings.Builder
var out []section
inFence := false
for _, ln := range strings.Split(htmlCommentRE.ReplaceAllString(template, ""), "\n") {
if strings.HasPrefix(strings.TrimSpace(ln), "```") {
inFence = !inFence
}
if !inFence && headingRE.MatchString(ln) {
out = append(out, section{heading: ln})
continue
}
if len(out) == 0 {
lead.WriteString(ln + "\n")
} else {
out[len(out)-1].text += ln + "\n"
}
}
return strings.TrimSpace(lead.String()), out
}
func headingIs(heading, name string) bool {
return strings.EqualFold(strings.TrimSpace(strings.TrimLeft(heading, "#")), name)
}
// body fills the line's own pull request template: the What, Screenshots and
// Release note sections are the batch's, every other section stays as the
// template has it. A body over GitHub's limit loses its longest skipped lists
// first, and its release notes' tail last; left out are the rules whose lists
// it lost.
func (r *report) body(template string) (body string, left []int) {
details := map[int]bool{}
for rule, items := range r.skipped {
details[rule] = len(items) > 0
}
notes := r.notes()
for {
full := r.render(template, r.what(details), notes)
if len(full) <= bodyLimit {
return full, left
}
longest := 0
for _, rule := range skipRules {
if details[rule] && (longest == 0 || len(r.skipped[rule]) > len(r.skipped[longest])) {
longest = rule
}
}
if longest == 0 {
const marker = "\n\n(release notes truncated; see the original pull requests)"
cut := max(0, len(notes)-(len(full)-bodyLimit)-len(marker))
for cut > 0 && !utf8Start(notes[cut]) {
cut--
}
return r.render(template, r.what(details), notes[:cut]+marker), left
}
details[longest] = false
left = append(left, longest)
}
}
func utf8Start(c byte) bool { return c&0xC0 != 0x80 }
func (r *report) render(template, what, notes string) string {
lead, secs := sections(template)
var b strings.Builder
if lead != "" {
b.WriteString(lead + "\n\n")
}
seen := map[string]bool{}
for _, s := range secs {
switch {
case headingIs(s.heading, "What this PR does"):
seen["what"] = true
b.WriteString(s.heading + "\n\n" + what + "\n")
case headingIs(s.heading, "Screenshots"):
b.WriteString(s.heading + "\n\nSee the original pull requests.\n\n")
case headingIs(s.heading, "Release note"):
seen["note"] = true
b.WriteString(s.heading + "\n\n```release-note\n" + notes + "\n```\n\n")
default:
b.WriteString(s.heading + "\n\n" + strings.TrimSpace(s.text) + "\n\n")
}
}
out := b.String()
if !seen["what"] {
out = "## What this PR does\n\n" + what + "\n" + out
}
if !seen["note"] {
out += "### Release note\n\n```release-note\n" + notes + "\n```\n"
}
return strings.TrimSpace(regexp.MustCompile(`\n{3,}`).ReplaceAllString(out, "\n\n")) + "\n"
}
+382
View File
@@ -0,0 +1,382 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"unicode"
)
// visible is what of a body GitHub renders as markdown rather than shows as
// code: fenced blocks and code spans taken out, delimited as CommonMark
// delimits them. Everything carried has to be gone from it.
func visible(body string) string {
fenceOpen := regexp.MustCompile("^(`{3,}|~{3,})")
var out []string
fence := ""
for _, ln := range strings.Split(body, "\n") {
t := strings.TrimLeft(ln, " ")
indented := len(ln)-len(t) > 3
if fence != "" {
run := strings.TrimLeft(t, fence[:1])
if !indented && len(t)-len(run) >= len(fence) && strings.TrimSpace(run) == "" {
fence = ""
}
continue
}
if m := fenceOpen.FindString(t); m != "" && !indented && (m[0] == '~' || !strings.Contains(t[len(m):], "`")) {
fence = m
continue
}
out = append(out, stripSpans(ln))
}
return strings.Join(out, "\n")
}
func stripSpans(s string) string {
var b strings.Builder
for i := 0; i < len(s); {
switch {
case s[i] == '\\' && i+1 < len(s):
b.WriteString(s[i : i+2])
i += 2
continue
case s[i] != '`':
b.WriteByte(s[i])
i++
continue
}
j := i
for j < len(s) && s[j] == '`' {
j++
}
n, closing := j-i, -1
for k := j; k < len(s); {
if s[k] != '`' {
k++
continue
}
e := k
for e < len(s) && s[e] == '`' {
e++
}
if e-k == n {
closing = k
break
}
k = e
}
if closing < 0 {
b.WriteString(s[i:j])
i = j
continue
}
i = closing + n
}
return b.String()
}
// hostile is carried text built to get out of its span or block: each carries
// a marker that must never be visible.
var hostile = []string{
"zqx01 thanks @alice",
"zqx02 (@org/team) please look",
"zqx03 [@bob](https://evil.example/x) [docs](https://github.com/cozystack/cozystack/../../evil/repo)",
"zqx04 zero\u200b@carol bidi \u202e@dave \ufeff@erin",
"zqx05 ``` `@frank` ``` still inside",
"zqx06 ` closes a single span, then @grace",
"zqx07 `` `` ``` ```` four",
"zqx08 Fixes #1, closes other/repo#2, resolves https://github.com/other/repo/issues/3",
"zqx09 </details><img src=x onerror=alert(1)><a href=\"https://evil.example\">x</a>",
"zqx10 <https://evil.example> www.evil.example <mail@evil.example>",
"zqx11 [ref]: https://evil.example",
"zqx12 &#64;heidi &amp; &lt;b&gt;",
"zqx13 \x1b[31mred\x1b[0m \u009b2J \x07 bell",
"zqx14 <!-- comment --> <details><summary>x</summary>",
"`zqx15` starts and ends with a backtick`",
}
// hostileNote is a release note whose own block a bare fence line would end,
// so it hides one behind a zero-width space, which clean then removes.
func hostileNote(i int, s string) string {
return fmt.Sprintf("fix(x): zqn%02d %s\n\u200b```\n@ivan zqn%02d after a fence\n~~~\n[@judy](https://evil.example) Fixes #9 <img src=x> \x1b[2J", i, s, i)
}
// Every carried title, reason and release note stays inside a code span or the
// release-note block, however it is written.
func TestBodyContainsCarriedText(t *testing.T) {
f := newFixture(t)
f.lines()
for i, s := range hostile {
typ := "fix"
if i%2 == 1 {
typ = "feat" // skipped, so it lands in a <details> list
}
body := "## What this PR does\n\n```release-note\n" + hostileNote(i, s) + "\n```\n"
f.pr(100+i, typ+"(x): "+s, body, map[string]string{fmt.Sprintf("app/%d.txt", i): "x\n"})
}
gh := &fakeGH{}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if len(gh.bodies) != 1 {
t.Fatalf("gh got %d bodies, want 1", len(gh.bodies))
}
for _, body := range []string{gh.bodies[0], stdout} {
for i := range hostile {
title, note := fmt.Sprintf("zqx%02d", i+1), fmt.Sprintf("zqn%02d", i)
if i == len(hostile)-1 {
title = "zqx15"
}
if !strings.Contains(body, title) {
t.Errorf("%s is missing from the body", title)
}
if i%2 == 0 && !strings.Contains(body, note) {
t.Errorf("%s is missing from the release notes", note)
}
}
if v := visible(body); strings.Contains(v, "zqx") || strings.Contains(v, "zqn") {
t.Errorf("carried text is visible outside code:\n%s", v)
}
for _, r := range body {
if r != '\n' && r != '\t' && unicode.IsControl(r) || unicode.Is(unicode.Cf, r) {
t.Errorf("the body carries %U", r)
break
}
}
}
}
// Copied out of the fenced block, a note must still close, ping and link
// nothing.
func TestNeutralNote(t *testing.T) {
cases := []struct{ in, gone string }{
{"thanks @alice", "@alice"},
{"(@org/team) look", "@org/team"},
{"[@bob](https://evil.example)", "@bob"},
{"x\u200b@carol", "@carol"},
{"a@dave.example", "@dave"},
{"Fixes #1", "Fixes #1"},
{"closes other/repo#2", "other/repo#2"},
{"see other/repo#3", "other/repo#3"},
{"resolves https://github.com/other/repo/issues/3", "github.com/other"},
{"Fixes https://github.com/cozystack/cozystack/issues/3", "Fixes https://"},
{"x``@erin", "@erin"},
{"`code @frank here`", "@frank"},
{"```@grace```", "@grace"},
{"go to https://evil.example/x", "evil.example"},
{"go to www.evil.example", "evil.example"},
{"[docs](https://evil.example)", "evil.example"},
{"[docs](https://github.com/cozystack/cozystack/../../evil/repo)", "evil/repo"},
{"line\n```\nescaped", "```"},
{"line\n~~~\nescaped", "~~~"},
{"zero\u200b``\u200b`", "```"},
{"\x1b[2J", "\x1b"},
}
for _, tc := range cases {
got := neutralNote(tc.in)
if strings.Contains(visible(got), tc.gone) {
t.Errorf("neutralNote(%q) = %q, still shows %q", tc.in, got, tc.gone)
}
}
keep := "see https://github.com/cozystack/cozystack/blob/main/docs/release/backports.md"
if got := neutralNote(keep); got != keep {
t.Errorf("neutralNote(%q) = %q, want a link into this repository kept", keep, got)
}
}
func TestCodeSpan(t *testing.T) {
cases := []string{"plain", "a `b` c", "``x``", "`lead", "trail`", "", "a\nb", "````", "\x1b[31m`\u202e`"}
for _, s := range cases {
got := codeSpan(s)
if v := visible(got); v != "" {
t.Errorf("codeSpan(%q) = %q leaves %q outside the span", s, got, v)
}
if strings.ContainsAny(got, "\n\x1b\u202e") {
t.Errorf("codeSpan(%q) = %q carries a newline or a control", s, got)
}
}
}
func TestDeferredRoundTrip(t *testing.T) {
l, _ := parseLine("release-1.6")
v := &verdict{o: &original{number: 3735}}
held := &verdict{o: &original{number: 3736}}
block := deferredBlock(l, []pick{{v: v, files: []string{"a\n```\nb", "<c>&d", "e`f"}}}, []pick{{v: held, reason: "breaks\n```\n<b>e2e</b>"}}, nil)
if lines := strings.Split(block, "\n"); len(lines) != 3 {
t.Fatalf("the block is %d lines, want 3:\n%s", len(lines), block)
}
body := "## What this PR does\n\ntext\n\n" + block + "\n\n### Release note\n"
got, err := parseDeferred(body, l)
if err != nil || len(got) != 2 || len(got[3735].Files) != 3 || got[3735].Reason != "" {
t.Errorf("parseDeferred = %v, %v; want the conflict #3735 with its files", got, err)
}
if e := got[3736]; e.Reason != "breaks ``` <b>e2e</b>" || len(e.Files) != 0 {
t.Errorf("parseDeferred = %+v; want #3736 held, for its reason on one line", e)
}
if strings.ContainsAny(strings.Split(block, "\n")[1], "<>&") {
t.Errorf("the JSON line carries raw HTML characters: %s", block)
}
other, _ := parseLine("release-1.5")
for name, body := range map[string]string{
"another line": body,
"two blocks": body + "\n" + block,
"not JSON": deferredFence + "\nnot json\n```",
"an unknown key": deferredFence + "\n{\"line\":\"release-1.6\",\"deferred\":[],\"x\":1}\n```",
"a bad number": deferredFence + "\n{\"line\":\"release-1.6\",\"deferred\":[{\"original\":-1}]}\n```",
"a repeat": deferredFence + "\n{\"line\":\"release-1.6\",\"deferred\":[{\"original\":5},{\"original\":5,\"reason\":\"x\"}]}\n```",
"a bad commit": deferredFence + "\n{\"line\":\"release-1.6\",\"deferred\":[{\"original\":5,\"commit\":\"-x\"}]}\n```",
"a broken fence": deferredFence + "\n{}\nmore\n```",
"an empty fence": deferredFence + "\n```",
"a trailing line": deferredFence,
} {
want := l
if name == "another line" {
want = other
}
if _, err := parseDeferred(body, want); err == nil {
t.Errorf("%s: parseDeferred accepted it", name)
}
}
if got, err := parseDeferred("no block here", l); err != nil || len(got) != 0 {
t.Errorf("no block: %v, %v; want nothing deferred", got, err)
}
}
func TestReleaseNote(t *testing.T) {
cases := map[string]string{
"### Release note\n\n```release-note\nfix(x): y\n```\n": "fix(x): y",
"```release-note\nNONE\n```": "",
"```release-note\n\n```": "",
"<!--\n```release-note\nhidden\n```\n-->": "",
"no block": "",
}
for body, want := range cases {
if got := releaseNote(body); got != want {
t.Errorf("releaseNote(%q) = %q, want %q", body, got, want)
}
}
}
// The body is the line's template with the batch's sections filled in, and
// fits the size GitHub accepts.
func TestBodyFollowsTheTemplate(t *testing.T) {
l, _ := parseLine("release-1.6")
template := "<!-- tips -->\n\n## What this PR does\n\n\n### Screenshots\n\n<!-- shots -->\n\n### Downstream repositories\n\n- [ ] No downstream repository is affected by this change\n\n### Release note\n\n```release-note\n\n```\n"
o := &original{number: 7, title: "fix(x): y", body: "```release-note\nfix(x): note\n```"}
r := &report{line: l, onBranch: []*verdict{{o: o}}, skipped: map[int][]*verdict{}, blocks: deferredBlock(l, nil, nil, nil)}
body, _ := r.body(template)
for _, want := range []string{"## What this PR does\n\nBackports to", "- [#7](https://github.com/cozystack/cozystack/pull/7) `fix(x): y`", "### Downstream repositories\n\n- [ ] No downstream", "```release-note\nfix(x): note\n```", deferredFence} {
if !strings.Contains(body, want) {
t.Errorf("the body lacks %q:\n%s", want, body)
}
}
if strings.Contains(body, "<!--") {
t.Errorf("the body keeps template comments:\n%s", body)
}
if body, _ := r.body("no headings at all"); !strings.Contains(body, "## What this PR does") || !strings.Contains(body, "```release-note") {
t.Errorf("a template without the sections gets them added:\n%s", body)
}
long := strings.Repeat("y", 4000)
var many []*verdict
for i := range 40 {
many = append(many, &verdict{o: &original{number: i + 1, title: "fix(x): y", body: "```release-note\n" + long + "\n```"}})
}
r = &report{line: l, onBranch: many, skipped: map[int][]*verdict{ruleType: many}, blocks: deferredBlock(l, nil, nil, nil)}
if body, _ := r.body(template); len(body) > bodyLimit || !strings.Contains(body, "truncated") || !strings.Contains(body, deferredFence) {
t.Errorf("a body over the limit is %d bytes (limit %d), truncated=%v", len(body), bodyLimit, strings.Contains(body, "truncated"))
}
// The longest list goes first; a short one and the notes stay whole.
var lots, few []*verdict
for i := range 600 {
lots = append(lots, &verdict{o: &original{number: 1000 + i, title: "feat(x): " + strings.Repeat("z", 150)}, reason: "type feat"})
}
for i := range 3 {
few = append(few, &verdict{o: &original{number: 10 + i, title: "fix(x): carried"}, reason: "picked in 0123456789ab"})
}
conflict := pick{v: &verdict{o: &original{number: 9, title: "fix(x): conflict"}}}
for i := range 80 {
conflict.files = append(conflict.files, fmt.Sprintf("chart/f%02d.yaml", i))
}
r = &report{line: l, onBranch: many[:1], deferred: []pick{conflict}, skipped: map[int][]*verdict{ruleType: lots, ruleOnLine: few}, blocks: deferredBlock(l, []pick{conflict}, nil, nil)}
body, left := r.body(template)
if !slices.Equal(left, []int{ruleType}) {
t.Errorf("left out the lists of rules %v, want only rule 2", left)
}
for _, want := range []string{"<details><summary>Rule 1", "[#12]", ": 600, too many to list here", "`chart/f04.yaml`, 75 more", `"more":30`} {
if !strings.Contains(body, want) {
t.Errorf("the fitted body lacks %q", want)
}
}
for _, unwanted := range []string{"<details><summary>Rule 2", "[#1000]", "`chart/f05.yaml`", "truncated"} {
if strings.Contains(body, unwanted) {
t.Errorf("the fitted body keeps %q", unwanted)
}
}
if len(body) > bodyLimit {
t.Errorf("the fitted body is %d bytes", len(body))
}
}
func TestPRTitle(t *testing.T) {
l, _ := parseLine("release-1.6")
if got := prTitle(l, "2026-10-23"); got != "fix(backport): batch for release-1.6, 2026-10-23" {
t.Errorf("prTitle = %q", got)
}
}
// Every task page the body links to is in the tree, under the path the link
// names on main.
func TestBodyLinksTaskPagesThatExist(t *testing.T) {
l, _ := parseLine("release-1.6")
v := &verdict{o: &original{number: 9, title: "fix(x): conflict"}}
r := &report{line: l, deferred: []pick{{v: v, files: []string{"a"}}}, held: []pick{{v: &verdict{o: &original{number: 10}}, reason: "r"}}, skipped: map[int][]*verdict{}}
body, _ := r.body("")
links := regexp.MustCompile(regexp.QuoteMeta(tasksURL)+`([\w.-]+)`).FindAllStringSubmatch(body, -1)
if len(links) < 2 {
t.Fatalf("the body links %d task pages, want the index and the conflict task:\n%s", len(links), body)
}
for _, m := range links {
if _, err := os.Stat(filepath.Join("..", "..", "docs", "release", "backport-tasks", m[1])); err != nil {
t.Errorf("the body links %s, which is not in the tree: %v", m[0], err)
}
}
}
// A body that what it has to carry pushes over GitHub's limit is refused
// before anything is written, the replay of an earlier report included,
// although the report alone would fit a comment.
func TestBodyRefusesMandatoryContentOverTheLimit(t *testing.T) {
f, gh, head := bigWaits(t, 4)
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
if code != 2 || !strings.Contains(stderr, "with only what it cannot leave out, over GitHub's 65536") {
t.Errorf("run --push = %d, want a refusal of the body:\n%s", code, stderr)
}
if f.remoteHead(batchBranch1) != head || len(gh.issueComments) != 0 || len(gh.edited) != 0 {
t.Errorf("the refused run moved the batch to %.12s, wrote %d comments and edited %q", f.remoteHead(batchBranch1), len(gh.issueComments), gh.edited)
}
}
+391
View File
@@ -0,0 +1,391 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strconv"
"strings"
)
// check says whether every original the policy takes is on the line, on the
// open batch pull request, or listed there as deferred, a conflict or one
// taken out with --defer, or as waiting for a maintainer because it changes
// workflows, each of which warns without failing. With --cut it is the gate
// before a cut, which tags the line: an original only on the open batch pull
// request fails it too, since the cut would not carry it, and so does one that
// waits, which nobody decided to leave out.
func (a *app) check(args []string) int {
fs := a.policyFlags("check")
lineArg := fs.String("line", "", "release line, release-X.Y")
cut := fs.Bool("cut", false, "fail too while a change is only on the open batch PR, as before a cut")
if err := parse(fs, args); err != nil {
return 2
}
l, err := parseLine(*lineArg)
if err != nil {
return a.fail(err)
}
if err := fetch(); err != nil {
return a.fail(err)
}
b, err := a.prepare(l)
if err != nil {
return a.fail(err)
}
if err := a.warnLeftBranch(b); err != nil {
return a.fail(err)
}
// The scheduled run's mode, as the open batch saved it; with none open,
// the scheduled run's, which is what the next batch is built in.
mode := analysisMode{noWorkflowFiles: true}
if b.src.open {
mode.noWorkflowFiles = b.src.savedNoWorkflowFiles()
}
disp, err := b.dispositions(mode, *cut)
if err != nil {
return a.fail(err)
}
var missing, failing []string
for _, v := range b.verdicts {
d, ok := disp[v]
switch {
case !ok:
case d.kind == outcomeMissing:
missing = append(missing, d.text)
case d.kind == outcomeUnmerged || d.kind == outcomeWaiting:
failing = append(failing, d.text)
case d.kind == outcomeWarning:
a.warn("%s", d.text)
case d.kind == outcomeNote:
_, _ = fmt.Fprintf(a.stderr, "note: %s\n", d.text)
}
}
if len(missing)+len(failing) == 0 {
if *cut {
_, _ = fmt.Fprintf(a.stdout, "%s: no change the policy takes is missing before the cut\n", l.branch())
} else {
_, _ = fmt.Fprintf(a.stdout, "%s: every change the policy takes is on the line or on the batch\n", l.branch())
}
return 0
}
where := "and no batch pull request is open"
if b.pr != nil {
where = "nor on batch pull request #" + strconv.Itoa(b.pr.Number)
}
if len(missing) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %d changes the policy takes are not on the line, %s:\n", l.branch(), len(missing), where)
for _, text := range missing {
_, _ = fmt.Fprintf(a.stdout, " %s\n", text)
}
}
for _, text := range failing {
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", l.branch(), text)
}
return 1
}
// warnLeftBranch names the branch a closed batch's close left behind.
func (a *app) warnLeftBranch(b *batch) error {
if b.src.open || b.src.rec == nil || b.src.rec.Closing == nil {
return nil
}
c := b.src.rec.Closing
heads, err := remoteBatchHeads(b.line)
if err != nil {
return err
}
switch h, ok := heads[c.Branch]; {
case !ok:
case takenBranch(b, *c) != nil:
a.warn("%v", takenBranch(b, *c))
case h == c.Head:
a.warn("batch pull request #%d is closed, but its branch %s is still at %.12s; backport-batch run --line %s --push deletes it", c.PR, c.Branch, h, b.line.branch())
default:
a.warn("%v", movedBranch(*c, h))
}
return nil
}
// outcome is what check makes of one original.
type outcome int
const (
// outcomeMissing fails check and the cut: nothing accounts for an
// original the line lacks.
outcomeMissing outcome = iota + 1
// outcomeUnmerged and outcomeWaiting fail the cut only: the cut would not
// carry what is only on the open batch, nor what waits there.
outcomeUnmerged
outcomeWaiting
outcomeWarning
outcomeNote
)
type disposition struct {
kind outcome
text string
}
// dispositions says what check reports about each original that needs a line
// of its own; the others pass in silence. The text is what follows the line's
// name, or the original alone for one that is missing. Originals are taken in
// merge order, each by the first of: a unit on the batch that does only part
// of it, a reason a person deferred it for, a park, a wait for a maintainer
// or on other originals, and what the record says of it.
func (b *batch) dispositions(mode analysisMode, cut bool) (map[*verdict]disposition, error) {
if c := b.src.closingOpen(); c != nil {
return nil, fmt.Errorf("batch pull request #%d is being closed; backport-batch run --line %s --push finishes it", c.PR, b.line.branch())
}
var stale []*verdict
if b.pr != nil {
var err error
if stale, err = keptOut(b, b.picks); err != nil {
return nil, err
}
}
// A record a closed batch left still says what a maintainer decided, but
// nothing waits on a batch that is gone.
deferred, on := b.src.rec.deferred(), ""
if c := b.src.carrier; c != nil {
on = fmt.Sprintf("#%d", c.Number)
if !b.src.open {
on += " (" + prState(c) + ")"
}
}
if err := b.analyze(); err != nil {
return nil, err
}
if b.pr != nil {
if err := b.keepRecordedPartials(b.prHead); err != nil {
return nil, err
}
}
held := map[int]string{}
for n, e := range deferred {
if e.Reason != "" {
held[n] = e.Reason
}
}
states, err := b.classify(mode, held, deferred, b.src.open)
if err != nil {
return nil, err
}
// waitsOn names what an original waits on, as check says it.
waitsOn := func(s *state) string {
var names []string
for _, f := range s.needs {
names = append(names, fmt.Sprintf("#%d", f.o.number))
}
return strings.Join(names, " and ")
}
out := map[*verdict]disposition{}
for _, v := range b.verdicts {
named := v.o.url() + " " + terminalSafe(v.o.title)
e, isDeferred := deferred[v.o.number]
s := states[v]
if s == nil {
s = &state{kind: stOwn}
}
switch {
case b.partialUnits[v] != nil && cut:
out[v] = disposition{outcomeUnmerged, fmt.Sprintf("#%d is on batch #%d, but only part of it applies to %s: it leaves %s without effect; take it out with backport-batch run --defer before the cut: %s", v.o.number, b.pr.Number, b.line.branch(), quotedFiles(b.partialUnits[v]), named)}
case b.partialUnits[v] != nil:
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d is on batch #%d, but only part of it applies to %s: it leaves %s without effect; take it out with backport-batch run --defer: %s", v.o.number, b.pr.Number, b.line.branch(), quotedFiles(b.partialUnits[v]), named)}
case slices.Contains(stale, v):
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d is still on batch #%d; take it out with backport-batch run --defer: %s", v.o.number, b.pr.Number, named)}
case v.rule == ruleDropped:
d := b.drops[v.o.number]
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is dropped from %s on #%d (%s): %s", v.o.number, b.line.branch(), d.On, terminalSafe(d.Reason), named)}
case v.rule != 0:
case v.onBatch != "":
if cut {
out[v] = disposition{outcomeUnmerged, fmt.Sprintf("#%d is only on open batch PR #%d, merge it before the cut: %s", v.o.number, b.pr.Number, named)}
}
case s.kind == stHeld:
out[v] = disposition{outcomeWarning, fmt.Sprintf("deferred on %s to the next batch: %s (%s)", on, named, terminalSafe(e.Reason))}
case s.kind == stPartial:
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is parked: only part of it applies to %s, which would leave %s without effect; port what applies as a pull request of its own: %s", v.o.number, b.line.branch(), quotedFiles(v.park.Files), named)}
case s.kind == stWorkflowPrimary && s.class == edgeMissing:
out[v] = disposition{outcomeMissing, named}
case s.kind == stParked:
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is parked: it builds on %s, which never reaches %s; port what applies as a pull request of its own, or drop it: %s", v.o.number, parkNames(*v.park), b.line.branch(), named)}
case s.kind == stNeeds && s.wait == edgeMissing:
out[v] = disposition{outcomeMissing, fmt.Sprintf("%s (it waits on %s, which is not on the line either)", named, waitsOn(s))}
case s.kind == stNeeds && s.wait == edgeWorkflow && cut:
out[v] = disposition{outcomeWaiting, fmt.Sprintf("#%d waits on %s, which changes %s and waits on batch PR %s for a maintainer; bring it over, or defer both, before the cut: %s", v.o.number, waitsOn(s), workflowsDir, on, named)}
case s.kind == stNeeds && s.wait == edgeWorkflow:
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d waits on %s, which waits on %s for a maintainer: %s", v.o.number, waitsOn(s), on, named)}
case s.kind == stNeeds && s.wait == edgeDeferred:
what := "a deferred conflict on " + on
if f := s.needs[0]; deferred[f.o.number].Reason != "" {
what = "deferred on " + on + " to the next batch"
} else if states[f] != nil && states[f].kind == stNeeds {
what = "which waits itself"
}
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d waits on %s, %s: %s", v.o.number, waitsOn(s), what, named)}
case s.kind == stNeeds:
newer := "the newer line"
if b.newer != nil {
newer = b.newer.branch()
}
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d waits on %s, which is not on %s yet: %s", v.o.number, waitsOn(s), newer, named)}
case isDeferred && e.Workflows && !b.src.open:
out[v] = disposition{outcomeMissing, named}
case isDeferred && e.Workflows && cut:
out[v] = disposition{outcomeWaiting, fmt.Sprintf("#%d changes %s and waits on batch PR %s for a maintainer, bring it over or defer it before the cut: %s", v.o.number, workflowsDir, on, named)}
case isDeferred && e.Workflows:
out[v] = disposition{outcomeWarning, fmt.Sprintf("waiting on %s for a maintainer, since it changes %s: %s", on, workflowsDir, named)}
case isDeferred && len(e.Needs) == 0:
out[v] = disposition{outcomeWarning, fmt.Sprintf("deferred conflict on %s: %s", on, named)}
default:
out[v] = disposition{outcomeMissing, named}
}
}
return out, nil
}
// parkNames names what a park waits on for a terminal.
func parkNames(p parkedEntry) string {
var out []string
for _, n := range p.Needs {
out = append(out, fmt.Sprintf("#%d", n))
}
for _, c := range p.Commits {
out = append(out, c[:12])
}
return strings.Join(out, ", ")
}
// quotedFiles names files for a terminal, each quoted, the first few of them.
func quotedFiles(files []string) string {
var out []string
for _, f := range files[:min(len(files), filesListed)] {
out = append(out, strconv.Quote(f))
}
if more := len(files) - len(out); more > 0 {
out = append(out, fmt.Sprintf("%d more", more))
}
return strings.Join(out, ", ")
}
// terminalSafe keeps a title from carrying escape sequences into a terminal.
func terminalSafe(s string) string { return capped(oneLine(clean(s, false)), titleLimit) }
// fold checks a hand-made resolution of a deferred conflict before anyone
// pushes it onto the batch: one original on the batch head, in the shape the
// batch carries it, inside the files that original touches.
func (a *app) fold(args []string) int {
fs := a.policyFlags("fold")
lineArg := fs.String("line", "", "release line, release-X.Y")
number := fs.Int("original", 0, "number of the original pull request")
commit := fs.String("commit", "", "the resolution: the merge commit, or for a squash-merged original its one commit")
if err := parse(fs, args); err != nil {
return 2
}
l, err := parseLine(*lineArg)
if err != nil {
return a.fail(err)
}
if *number <= 0 {
return a.fail(fmt.Errorf("--original %d is not a pull request number", *number))
}
if err := checkSHA(*commit); err != nil {
return a.fail(fmt.Errorf("--commit: %w", err))
}
if err := fetch(); err != nil {
return a.fail(err)
}
b, err := a.prepare(l)
if err != nil {
return a.fail(err)
}
if c := b.src.closingOpen(); c != nil {
return a.fail(fmt.Errorf("batch pull request #%d is being closed; backport-batch run --line %s --push finishes it", c.PR, l.branch()))
}
if err := a.warnLeftBranch(b); err != nil {
return a.fail(err)
}
why, err := refuseFold(b, *number, *commit)
if err != nil {
return a.fail(err)
}
if why != "" {
_, _ = fmt.Fprintf(a.stdout, "refused: %s\n", why)
return 1
}
_, _ = fmt.Fprintln(a.stdout, "ok")
return 0
}
// refuseFold says why commit is not a fold of original n onto the batch, or ""
// when it is one: for a merged pull request a merge whose first parent is the
// batch head and whose second parent ends a straight line of picks of the pull
// request's own commits, in order, each with its -x line, naming the original
// in its Backported-from trailer; for a squash-merged one a single pick of its
// commit on main. Pushed as it is, either is a fast-forward of the batch.
func refuseFold(b *batch, n int, commit string) (string, error) {
v := b.find(n)
switch {
case v == nil:
return fmt.Sprintf("#%d did not merge into main after %s branched", n, b.line.branch()), nil
case v.rule == ruleSkipLabel:
return fmt.Sprintf("#%d carries %s", n, labelSkip), nil
case v.rule != 0:
return fmt.Sprintf("#%d is not one the batch takes: %s, %s", n, ruleName(v.rule), terminalSafe(v.reason)), nil
case b.pr == nil:
return fmt.Sprintf("no batch pull request is open on %s", b.line.branch()), nil
case v.onBatch != "":
return fmt.Sprintf("batch pull request #%d already carries #%d in %.12s", b.pr.Number, n, v.onBatch), nil
}
have, err := lookup(commit)
if err != nil {
return "", err
}
if have == "" {
return fmt.Sprintf("%.12s is not a commit in this repository", commit), nil
}
u, err := readUnit(commit)
if err != nil {
return "", err
}
want := 1
if v.o.parents > 1 {
want = 2
}
switch {
case len(u.parents) != want && want == 2:
return fmt.Sprintf("%.12s has %d parents; #%d is a merged pull request, so a fold is a merge of its picked commits onto the batch head", commit, len(u.parents), n), nil
case len(u.parents) != want:
return fmt.Sprintf("%.12s has %d parents; #%d merged as one commit, so a fold is one commit with one parent", commit, len(u.parents), n), nil
case u.parents[0] != b.prHead:
return fmt.Sprintf("%.12s is on %.12s, not on the head of batch pull request #%d (%.12s)", commit, u.parents[0], b.pr.Number, b.prHead), nil
}
why, carried, err := checkUnit(b, u)
switch {
case err != nil || why != "":
return why, err
case carried != v:
return fmt.Sprintf("%.12s carries #%d, not #%d", commit, carried.o.number, n), nil
}
omitted, _, err := b.omittedEffects(v, u.parents[0], u.commit)
if err != nil || len(omitted) == 0 {
return "", err
}
return leftOut(v, b.line, omitted), nil
}
+457
View File
@@ -0,0 +1,457 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"regexp"
"strings"
"testing"
"unicode"
)
func TestCheck(t *testing.T) {
type state struct {
f *fixture
gh *fakeGH
mc5 string
mc6 string
}
setup := func(t *testing.T) *state {
f := newFixture(t)
f.lines()
s := &state{f: f, gh: &fakeGH{}}
s.mc5 = f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
s.mc6 = f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "feat(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
return s
}
// batch pushes a batch branch carrying the given merges and opens its
// pull request with body.
batch := func(s *state, body string, merges ...string) {
s.f.git("switch", "-q", "-c", "batch", "origin/release-1.1")
for _, mc := range merges {
s.f.git("cherry-pick", "-x", "-m", "1", mc)
}
s.f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
s.f.git("switch", "-q", "main")
s.gh.prs = append(s.gh.prs, s.f.batchPR(90, "release-1.1", batchBranch1, body))
}
l, _ := parseLine("release-1.1")
deferred6 := deferredBlock(l, []pick{{v: &verdict{o: &original{number: 6}}, files: []string{"app/six.txt"}}}, nil, nil)
held6 := deferredBlock(l, nil, []pick{{v: &verdict{o: &original{number: 6}}, reason: "fails the app e2e"}}, nil)
cases := []struct {
name string
arrange func(s *state)
cut bool
code int
listed []string
notListed []string
warnedAbout string
}{
{
name: "nothing picked, no batch",
arrange: func(*state) {},
code: 1,
listed: []string{"/pull/5 ", "/pull/6 "},
notListed: []string{"/pull/7"},
},
{
name: "both on the line",
arrange: func(s *state) {
s.f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+s.mc5+")", map[string]string{"app/five.txt": "5\n"})
s.f.onLine("release-1.1", "[Backport release-1.1] fix(app): six (#80)\n\nBackport of #6.", map[string]string{"app/six.txt": "6\n"})
},
code: 0,
},
{
name: "both on the open batch",
arrange: func(s *state) { batch(s, "", s.mc5, s.mc6) },
code: 0,
},
{
name: "--cut: both on the open batch, which the cut would not carry",
arrange: func(s *state) { batch(s, "", s.mc5, s.mc6) },
cut: true,
code: 1,
listed: []string{"#5 is only on open batch PR #90, merge it before the cut: https://github.com/cozystack/cozystack/pull/5 ", "#6 is only on open batch PR #90, merge it before the cut: https://github.com/cozystack/cozystack/pull/6 "},
},
{
name: "--cut: both on the line",
arrange: func(s *state) {
s.f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+s.mc5+")", map[string]string{"app/five.txt": "5\n"})
s.f.onLine("release-1.1", "fix(app): six (#6)\n\n(cherry picked from commit "+s.mc6+")", map[string]string{"app/six.txt": "6\n"})
batch(s, "")
},
cut: true,
code: 0,
},
{
name: "--cut: one on the line, the other deferred to the next batch, which stays a warning",
arrange: func(s *state) {
s.f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+s.mc5+")", map[string]string{"app/five.txt": "5\n"})
batch(s, "body\n\n"+held6+"\n")
},
cut: true,
code: 0,
warnedAbout: "/pull/6 ",
},
{
name: "one on the batch, one missing",
arrange: func(s *state) { batch(s, "", s.mc5) },
code: 1,
listed: []string{"/pull/6 "},
notListed: []string{"/pull/5 "},
},
{
name: "the missing one is a deferred conflict on the batch",
arrange: func(s *state) { batch(s, "body\n\n"+deferred6+"\n", s.mc5) },
code: 0,
warnedAbout: "/pull/6 ",
},
{
name: "the missing one is deferred to the next batch",
arrange: func(s *state) { batch(s, "body\n\n"+held6+"\n", s.mc5) },
code: 0,
warnedAbout: "/pull/6 ",
},
{
name: "a fork's pull request named like a batch claims nothing",
arrange: func(s *state) {
batch(s, "body\n\n"+deferred6+"\n", s.mc5)
s.gh.prs[0].Head.Repo.FullName = "someone/cozystack"
},
code: 1,
listed: []string{"/pull/5 ", "/pull/6 "},
},
{
name: "a batch opened by a member with read access claims nothing",
arrange: func(s *state) {
batch(s, "body\n\n", s.mc5)
s.gh.perms = map[string]string{"maintainer": "read"}
},
code: 1,
listed: []string{"/pull/5 ", "/pull/6 "},
},
{
// An older record is never taken in place of a newer one nobody
// can vouch for.
name: "a record by a member with read access stops it",
arrange: func(s *state) {
batch(s, "body\n\n"+deferred6+"\n", s.mc5)
s.gh.perms = map[string]string{"maintainer": "read"}
},
code: 2,
},
{
name: "a deferred list for another line",
arrange: func(s *state) { batch(s, strings.Replace(deferred6, "release-1.1", "release-1.0", 1), s.mc5) },
code: 2,
},
{
name: "GitHub cannot be read",
arrange: func(s *state) { s.gh.err = errString("gh: HTTP 502") },
code: 2,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s := setup(t)
tc.arrange(s)
args := []string{"check", "--line", "release-1.1"}
if tc.cut {
args = append(args, "--cut")
}
code, stdout, stderr := s.f.exec(s.gh, args...)
if code != tc.code {
t.Fatalf("check = %d, want %d\nstdout:\n%s\nstderr:\n%s", code, tc.code, stdout, stderr)
}
for _, want := range tc.listed {
if !strings.Contains(stdout, want) {
t.Errorf("stdout does not list %q:\n%s", want, stdout)
}
}
for _, unwanted := range tc.notListed {
if strings.Contains(stdout, unwanted) {
t.Errorf("stdout lists %q:\n%s", unwanted, stdout)
}
}
if tc.warnedAbout != "" && (!strings.Contains(stderr, "warning: deferred") || !strings.Contains(stderr, tc.warnedAbout)) {
t.Errorf("stderr does not warn about %q:\n%s", tc.warnedAbout, stderr)
}
})
}
}
// Every category an original can be in, through the commands that put it
// there, with what check and check --cut make of it: the exit code, and the
// warning or note on stderr that leaves it 0.
func TestCheckDispositions(t *testing.T) {
plain := func(t *testing.T) *fixture {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f
}
// conflicting has #5 conflict on the line, beside #6, which picks.
conflicting := func(t *testing.T) *fixture {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f
}
must := func(t *testing.T, f *fixture, gh *fakeGH, args ...string) {
t.Helper()
if code, _, stderr := f.exec(gh, args...); code != 0 {
t.Fatalf("%q = %d\n%s", args, code, stderr)
}
f.git("switch", "-q", "main")
if len(gh.bodies) > 0 {
reopen(f, gh)
}
}
sixOnLine := func(f *fixture) { pickOnLine(f, 6, map[string]string{"app/six.txt": "6\n"}) }
cases := []struct {
name string
arrange func(t *testing.T) (*fixture, *fakeGH)
line string
check int
cut int
checkErr string // on stderr from check
cutOut string // on stdout from check --cut
cutErr string // on stderr from check --cut
notCutOut string
}{
{
name: "missing",
arrange: func(t *testing.T) (*fixture, *fakeGH) { return plain(t), &fakeGH{} },
check: 1, cut: 1, cutOut: "/pull/5 ",
},
{
name: "carried",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f := plain(t)
pickOnLine(f, 5, map[string]string{"app/five.txt": "5\n"})
sixOnLine(f)
return f, &fakeGH{}
},
check: 0, cut: 0, notCutOut: "/pull/5",
},
{
name: "picked, on the open batch",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := plain(t), &fakeGH{}
openBatch(t, f, gh)
return f, gh
},
check: 0, cut: 1, cutOut: "#5 is only on open batch PR #90",
},
{
name: "deferred conflict, open record",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := conflicting(t), &fakeGH{}
openBatch(t, f, gh)
sixOnLine(f)
return f, gh
},
check: 0, cut: 0, checkErr: "warning: deferred conflict on #90: https://github.com/cozystack/cozystack/pull/5 ", cutErr: "warning: deferred conflict on #90",
},
{
name: "deferred with a reason, open record",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := plain(t), &fakeGH{}
openBatch(t, f, gh)
must(t, f, gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the app e2e", "--push")
sixOnLine(f)
return f, gh
},
check: 0, cut: 0, checkErr: "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ", cutErr: "(fails the app e2e)",
},
{
name: "workflow wait, open record, beside a pick",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := workflowWorld(t), &fakeGH{}
must(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
return f, gh
},
check: 0, cut: 1, checkErr: "warning: waiting on #90 for a maintainer", cutOut: "#5 changes .github/workflows/ and waits on batch PR #90",
},
{
name: "workflow wait, open record, alone",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := workflowWorld(t), &fakeGH{}
must(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
pickOnLine(f, 6, map[string]string{"app/six.txt": "6\n", ".github/renovate.json": "{}\n"})
return f, gh
},
check: 0, cut: 1, checkErr: "warning: waiting on #90 for a maintainer", cutOut: "#5 changes .github/workflows/ and waits on batch PR #90",
},
{
name: "backport/skip, not picked",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f := plain(t)
sixOnLine(f)
return f, &fakeGH{labels: map[string][]int{labelSkip: {5}}}
},
check: 0, cut: 0, notCutOut: "/pull/5",
},
{
name: "backport/skip after the pick, still on the open batch",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f, gh := plain(t), &fakeGH{}
openBatch(t, f, gh)
sixOnLine(f)
gh.labels = map[string][]int{labelSkip: {5}}
return f, gh
},
check: 0, cut: 0, checkErr: "warning: #5 is still on batch #90; take it out with backport-batch run --defer", cutErr: "still on batch #90",
},
{
name: "rule 6",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
return f, &fakeGH{}
},
line: "release-1.0",
check: 0, cut: 0, notCutOut: "/pull/5",
},
{
name: "rules 2 to 4",
arrange: func(t *testing.T) (*fixture, *fakeGH) {
f := newFixture(t)
f.lines()
f.pr(5, "feat(app): five", "", map[string]string{"app/five.txt": "5\n"})
return f, &fakeGH{}
},
check: 0, cut: 0, notCutOut: "/pull/5",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, gh := tc.arrange(t)
line := tc.line
if line == "" {
line = "release-1.1"
}
code, stdout, stderr := f.exec(gh, "check", "--line", line)
if code != tc.check || !strings.Contains(stderr, tc.checkErr) {
t.Errorf("check = %d, want %d with %q\nstdout:\n%s\nstderr:\n%s", code, tc.check, tc.checkErr, stdout, stderr)
}
code, stdout, stderr = f.exec(gh, "check", "--line", line, "--cut")
if code != tc.cut || !strings.Contains(stdout, tc.cutOut) || !strings.Contains(stderr, tc.cutErr) {
t.Errorf("check --cut = %d, want %d with %q, %q\nstdout:\n%s\nstderr:\n%s", code, tc.cut, tc.cutOut, tc.cutErr, stdout, stderr)
}
if tc.notCutOut != "" && strings.Contains(stdout, tc.notCutOut) {
t.Errorf("check --cut lists %q:\n%s", tc.notCutOut, stdout)
}
if tc.cut == 0 && !strings.Contains(stdout, "no change the policy takes is missing before the cut") {
t.Errorf("check --cut passes without saying so:\n%s", stdout)
}
})
}
}
// Output names a rule as the policy does: the numbers the tool keeps for
// what is no rule of the policy never reach a reader.
func TestOutputNamesNoInternalRule(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.setPolicy("release-1.1: 2099-01-01\n")
gh := &fakeGH{labels: map[string][]int{labelInclude: {6}}}
head := openBatch(t, f, gh)
internal := regexp.MustCompile(`rule 1[0-9][0-9]`)
for _, args := range [][]string{
{"fold", "--line", "release-1.1", "--original", "5", "--commit", head},
{"run", "--line", "release-1.1", "--defer", "5", "--reason", "x"},
} {
code, stdout, stderr := f.exec(gh, args...)
f.git("switch", "-q", "main")
if code == 0 || !strings.Contains(stdout+stderr, "#5 is not one the batch takes: rule 1, before the line's start, merged ") {
t.Errorf("%s = %d, want a refusal naming rule 1:\n%s%s", args[0], code, stdout, stderr)
}
if m := internal.FindString(stdout + stderr); m != "" {
t.Errorf("%s prints %q:\n%s%s", args[0], m, stdout, stderr)
}
}
}
// A refusal fold prints is one line with no control sequence, whatever the
// reason it repeats says: a drop's reason comes from a pull request body, a
// rule 4 reason from file names an original's author chose.
func TestFoldRefusalPrintsNothingThatDrivesATerminal(t *testing.T) {
l, _ := parseLine("release-1.1")
for _, reason := range []string{"dropped on #90: x\n\x1b[2Jrefused: forged", "touches \"app/a\x1b]0;x\x07\nok\""} {
b := &batch{line: l, verdicts: []*verdict{{o: &original{number: 5}, rule: ruleDropped, reason: reason}}}
why, err := refuseFold(b, 5, strings.Repeat("a", 40))
if err != nil || why == "" || strings.ContainsFunc(why, func(r rune) bool { return unicode.IsControl(r) || unicode.Is(unicode.Cf, r) }) {
t.Errorf("refuseFold = %q, %v; want one clean line", why, err)
}
}
}
type errString string
func (e errString) Error() string { return string(e) }
// Nothing the tool prints may carry a control sequence, whichever command
// prints it and wherever the text came from.
func TestTerminal(t *testing.T) {
f := newFixture(t)
f.lines()
const evil = "\x1b]0;owned\x07\x1b[2J\u009b31m\u202e"
f.pr(5, "fix(app): five "+evil, "", map[string]string{"app/five.txt": "5\n"})
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "")}}
f.git("switch", "-q", "--detach", "origin/"+batchBranch1)
f.write("app/"+evil+".txt", "x\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "port "+evil)
commit := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
// gh relays what the server said when it fails.
broken := &fakeGH{err: errString("HTTP 502: " + evil)}
for _, tc := range []struct {
gh *fakeGH
args []string
}{
{gh, []string{"check", "--line", "release-1.1"}},
{broken, []string{"check", "--line", "release-1.1"}},
{gh, []string{"fold", "--line", "release-1.1", "--original", "5", "--commit", commit}},
{gh, []string{"run", "--line", "release-1.1", "--date", "2026-10-26"}},
{gh, []string{"run", "--line", "release-1.1\x1b[2J"}},
} {
args := tc.args
_, stdout, stderr := f.exec(tc.gh, args...)
for name, out := range map[string]string{"stdout": stdout, "stderr": stderr} {
for _, r := range out {
if r != '\n' && r != '\t' && unicode.IsControl(r) || unicode.Is(unicode.Cf, r) {
t.Errorf("%s of %v carries %U:\n%q", name, args[0], r, out)
break
}
}
}
}
}
+118
View File
@@ -0,0 +1,118 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"strings"
"testing"
)
// pickOnLine lands original n on release-1.1 as a maintainer's pick of its
// merge commit, with the -x line git appends, so the line carries it.
func pickOnLine(f *fixture, n int, files map[string]string) {
f.t.Helper()
mc, _, _ := strings.Cut(f.git("log", "--first-parent", "--format=%H", "--grep", fmt.Sprintf("(#%d)$", n), "main"), "\n")
f.onLine("release-1.1", fmt.Sprintf("fix: original %d\n\n(cherry picked from commit %s)", n, mc), files)
}
// A workflow original that waits on the open batch fails the cut, also when
// it is all the batch has left: the pick beside it was labelled
// backport/skip after the pick, or reached the line by hand.
func TestCutFailsWhenOnlyAWorkflowWaitIsLeft(t *testing.T) {
for _, tc := range []struct {
name string
leave func(f *fixture, gh *fakeGH)
}{
{"skip after the pick", func(_ *fixture, gh *fakeGH) { gh.labels = map[string][]int{labelSkip: {6}} }},
{"picked onto the line by hand", func(f *fixture, _ *fakeGH) {
pickOnLine(f, 6, map[string]string{"app/six.txt": "6\n", ".github/renovate.json": "{}\n"})
}},
} {
t.Run(tc.name, func(t *testing.T) {
f := workflowWorld(t)
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
tc.leave(f, gh)
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
t.Errorf("check --cut = %d, want 1 naming #5:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
if strings.Contains(stdout, "on the line or on the batch") {
t.Errorf("check --cut passes the line while #5 waits:\n%s", stdout)
}
})
}
}
// A conflict the batch deferred is still deferred once that batch merged:
// the next run picks nothing, opens nothing and writes nothing to the merged
// pull request, and check reads the merged batch's record.
func TestCheckAfterTheBatchMerged(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
mergeBatch(f, gh)
written := len(gh.created) + len(gh.edited) + len(gh.issueComments)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 {
t.Fatalf("run --continue --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if got := len(gh.created) + len(gh.edited) + len(gh.issueComments); got != written || f.remoteHead(batchBranch1) != "" {
t.Errorf("the run picked nothing and still wrote: created %q, edited %q", gh.created, gh.edited)
}
for _, cut := range []bool{false, true} {
args := []string{"check", "--line", "release-1.1"}
if cut {
args = append(args, "--cut")
}
code, stdout, stderr := f.exec(gh, args...)
if code != 0 || !strings.Contains(stderr, "warning: deferred conflict on #90 (merged): https://github.com/cozystack/cozystack/pull/5 ") {
t.Errorf("%q = %d, want 0 with #5 a deferred conflict:\nstdout:\n%s\nstderr:\n%s", args, code, stdout, stderr)
}
}
}
// A workflow original waits only while a batch is open to list it: once the
// batch that listed it merged, it is missing.
func TestWorkflowWaitAfterTheBatchMergedIsMissing(t *testing.T) {
f := workflowWorld(t)
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
mergeBatch(f, gh)
for _, cut := range []bool{false, true} {
args := []string{"check", "--line", "release-1.1"}
if cut {
args = append(args, "--cut")
}
if code, stdout, stderr := f.exec(gh, args...); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("%q = %d, want 1 with #5 missing:\nstdout:\n%s\nstderr:\n%s", args, code, stdout, stderr)
}
}
}
+265
View File
@@ -0,0 +1,265 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"slices"
"strconv"
"strings"
)
// parseDefer reads --defer N[,N] and the --reason that has to come with it.
func parseDefer(list, reason string) ([]int, string, error) {
if list == "" {
return nil, "", errors.New("--reason goes with --defer or --drop")
}
reason = capped(oneLine(clean(reason, false)), titleLimit)
if reason == "" {
return nil, "", errors.New("--defer needs a --reason, which the batch body records")
}
numbers, err := parseNumbers("--defer", list)
return numbers, reason, err
}
// parseNumbers reads the N[,N] list a flag takes, each once.
func parseNumbers(flag, list string) ([]int, error) {
var numbers []int
for _, s := range strings.Split(list, ",") {
n, err := strconv.Atoi(strings.TrimPrefix(strings.TrimSpace(s), "#"))
if err != nil || n <= 0 {
return nil, fmt.Errorf("%s %q is not a list of pull request numbers", flag, list)
}
if !slices.Contains(numbers, n) {
numbers = append(numbers, n)
}
}
return numbers, nil
}
// rebuild takes the originals in numbers out of the open batch: it builds the
// batch branch again off the line's head without their commits, and records
// them in the body as deferred, for reason, next to what earlier deferrals
// recorded. It adds no new original; the next run does.
func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
if b.pr == nil {
return a.fail(fmt.Errorf("no batch pull request is open on %s; --defer rebuilds the open one", b.line.branch()))
}
hold := map[int]string{}
for n, e := range b.recorded() {
if e.Reason != "" {
hold[n] = e.Reason
}
}
carried, err := readPicks(b.prHead, b.lineHead, b.textUntil)
if err != nil {
return a.fail(err)
}
stale, err := keptOut(b, carried)
if err != nil {
return a.fail(err)
}
for _, n := range numbers {
switch v := b.find(n); {
case v == nil:
return a.fail(fmt.Errorf("#%d did not merge into main after %s branched", n, b.line.branch()))
case v.rule != 0 && !slices.Contains(stale, v) && b.partialUnits[v] == nil:
return a.fail(fmt.Errorf("#%d is not one the batch takes: %s, %s", n, ruleName(v.rule), terminalSafe(v.reason)))
}
hold[n] = reason
}
return a.reassemble(b, hold, hold, b.src.rec.dropped(), modeDefer, push)
}
// reassemble builds the open batch again off the line's head without the
// units of the originals in out, and reports it with hold deferred for their
// reasons and drops kept.
func (a *app) reassemble(b *batch, out, hold map[int]string, drops []droppedEntry, mode string, push bool) int {
branch := b.pr.Head.Ref
start, replay, err := rebuildPlan(b, out)
if err != nil {
return a.fail(err)
}
if err := a.warnReset(branch, start); err != nil {
return a.fail(err)
}
if _, err := git("switch", "--quiet", "--no-track", "-C", branch, "--end-of-options", start); err != nil {
return a.fail(err)
}
for _, u := range replay {
failed, files, err := a.replayNext(u)
switch {
case err != nil:
return a.fail(err)
case failed != "":
return a.fail(fmt.Errorf("without what is deferred, %.12s%s no longer applies (%s); defer it as well", failed, originName(b, u), terminalSafe(strings.Join(files, ", "))))
}
}
head, err := resolve("HEAD")
if err != nil {
return a.fail(err)
}
picks, err := readPicks(head, b.lineHead, b.textUntil)
if err != nil {
return a.fail(err)
}
return a.reclassify(b, picks, hold, drops, mode, push)
}
// reclassify reports the open batch as picks shows it, after an operation
// that adds no original: what the record held stays held, and an original
// the batch neither carries nor records is missing until a run tries it.
func (a *app) reclassify(b *batch, picks *history, hold map[int]string, drops []droppedEntry, mode string, push bool) int {
date, err := branchDate(b.pr.Head.Ref)
if err != nil {
return a.fail(err)
}
recorded := b.recorded()
r := newReport(b)
// None of these changes how the batch is assembled, so the record keeps
// the flag the batch was assembled with.
r.opts = runOptions{NoWorkflowFiles: b.src.savedNoWorkflowFiles(), Mode: mode}
r.dropped = drops
if r.stale, err = keptOut(b, picks); err != nil {
return a.fail(err)
}
// What the rebuild took off is no longer there for what builds on it.
units, err := b.unitsOn(picks.head)
if err != nil {
return a.fail(err)
}
for _, v := range b.verdicts {
v.batched = units[v]
}
on := map[*verdict]bool{}
for _, v := range b.qualifying() {
if on[v], err = b.onBranch(picks, v); err != nil {
return a.fail(err)
}
if !on[v] {
v.onBatch = ""
}
}
if err := b.keepRecordedPartials(picks.head); err != nil {
return a.fail(err)
}
states, err := b.classify(analysisMode{noWorkflowFiles: r.opts.NoWorkflowFiles}, hold, recorded, true)
if err != nil {
return a.fail(err)
}
for _, v := range b.qualifying() {
e := recorded[v.o.number]
s := states[v]
if s == nil {
s = &state{kind: stOwn}
}
switch {
case on[v]:
r.onBranch = append(r.onBranch, v)
case hold[v.o.number] != "":
r.held = append(r.held, pick{v: v, status: "held", reason: hold[v.o.number]})
case s.kind == stPartial || s.kind == stParked:
r.parked = append(r.parked, v)
case s.kind == stNeeds:
r.needs = append(r.needs, pick{v: v, status: "waiting", needs: numbers(s.needs)})
case s.kind == stWorkflowPrimary && !e.Workflows:
r.missing = append(r.missing, v)
case e.Workflows:
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: e.Files, more: e.More})
case len(e.Files) > 0 && len(e.Needs) == 0:
r.deferred = append(r.deferred, pick{v: v, status: "conflict", files: e.Files, more: e.More, failed: e.Commit})
default:
// Merged into main since the batch was assembled, or recorded
// nowhere: a rebuild adds no original, the next run tries it.
r.missing = append(r.missing, v)
}
}
if err := b.markPartial(r, picks.head); err != nil {
return a.fail(err)
}
return a.finish(b, r, b.pr.Head.Ref, date, push, true)
}
// recorded is what the open batch's record defers; a closed batch's record
// holds nothing back on this one.
func (b *batch) recorded() map[int]deferredEntry {
if !b.src.open {
return map[int]deferredEntry{}
}
return b.src.rec.deferred()
}
// rebuildPlan is where the rebuild of the open batch without the originals in
// drop starts, and the units of the batch it picks again on top, oldest first,
// each in its own shape. While the batch still sits on the line's head,
// everything before the first unit it drops stays as it is.
func rebuildPlan(b *batch, drop map[int]string) (string, []unit, error) {
units, err := batchUnits(b.prHead, b.lineHead)
if err != nil {
return "", nil, err
}
for _, u := range units {
if len(u.parents) > 2 || u.merge() && len(batchedFrom(u.message)) != 1 {
return "", nil, fmt.Errorf("batch pull request #%d carries merge commit %.12s, which is no batch merge of an original and which a rebuild cannot pick again; take the original out by hand", b.pr.Number, u.commit)
}
}
dropped := func(u unit) bool {
v := b.unitOriginal(u)
return v != nil && drop[v.o.number] != ""
}
var keep []unit
for _, u := range units {
if !dropped(u) {
keep = append(keep, u)
}
}
first := slices.IndexFunc(units, dropped)
onLineHead, err := isAncestor(b.lineHead, b.prHead)
if err != nil {
return "", nil, err
}
switch {
case !onLineHead:
return b.lineHead, keep, nil
case first < 0:
return b.prHead, nil, nil
case first == 0:
return b.lineHead, keep, nil
}
return units[first-1].commit, keep[first:], nil
}
// pickedBy is the original whose commits the -x sources name, or nil.
func (b *batch) pickedBy(sources []string) *verdict {
for _, v := range b.verdicts {
for _, s := range sources {
if slices.Contains(v.commits, s) {
return v
}
}
}
return nil
}
// originName names the original a unit carries, for an error message.
func originName(b *batch, u unit) string {
if v := b.unitOriginal(u); v != nil {
return fmt.Sprintf(", of #%d,", v.o.number)
}
return ""
}
+709
View File
@@ -0,0 +1,709 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"slices"
"strings"
"testing"
)
// openBatch pushes a batch for the fixes merged so far and opens its pull
// request on the fake, with the body the run printed.
func openBatch(t *testing.T, f *fixture, gh *fakeGH) string {
t.Helper()
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, stdout)}
return f.remoteHead(batchBranch1)
}
// reopen points the fake's batch pull request at what the remote now has and
// at the body the tool last wrote.
func reopen(f *fixture, gh *fakeGH) {
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.bodies[len(gh.bodies)-1])}
}
// --defer takes an original out of the open batch and nothing else: what came
// before it keeps its commits, what came after is picked again in its own
// shape, a resolution pushed by hand survives as the merge it was, and the
// body records the reason.
func TestDeferRebuildsWithoutTheOriginal(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only seven", map[string]string{"app/seven.txt": "line\n"})
f.prCommits(5, "fix(app): five", "",
change{files: map[string]string{"app/five.txt": "5\n"}},
change{files: map[string]string{"app/five.txt": "5, done\n"}})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
l, _ := parseLine("release-1.1")
if d, err := parseDeferred(gh.prs[0].Body, l); err != nil || len(d[7].Files) == 0 {
t.Fatalf("#7 is not a deferred conflict: %+v, %v", d, err)
}
// A resolution of #7, the way fold accepts one: its commit picked onto the
// batch head with the conflict resolved, under a merge that names it.
f.git("switch", "-q", "--detach", head)
f.write("app/seven.txt", "7, resolved\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "fix(app): seven\n\n(cherry picked from commit "+f.git("rev-parse", mc7+"^2")+")")
pick := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "--detach", head)
f.git("merge", "-q", "--no-ff", "-m", "fix(app): seven (#7)\n\n"+batchTrailer+": "+mc7, pick)
resolution := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.prs[0].Body)}
if code, out, stderr := f.exec(gh, "fold", "--line", "release-1.1", "--original", "7", "--commit", resolution); code != 1 || !strings.Contains(out, "already carries #7") {
t.Fatalf("fold of the pushed resolution = %d, %q, %s; want it seen as carried", code, out, stderr)
}
first := f.git("rev-parse", head+"~1")
// A commit picked again gets a new committer date, so a new name.
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the app e2e", "--push")
if code != 0 {
t.Fatalf("run --defer = %d\n%s", code, stderr)
}
pushed := f.remoteHead(batchBranch1)
want := []string{"fix(app): five (#5)", "fix(app): seven (#7)"}
if got := f.subjects("origin/release-1.1", pushed); !slices.Equal(got, want) {
t.Errorf("the rebuilt batch carries %q, want %q", got, want)
}
if got := f.git("rev-parse", pushed+"~1"); got != first {
t.Errorf("the merge of #5 became %.12s, want it kept as %.12s", got, first)
}
if parents := strings.Fields(f.git("rev-list", "--parents", "-n", "1", pushed)); len(parents) != 3 {
t.Errorf("#7's resolution came back as %v, want a merge", parents)
}
if !strings.HasSuffix(f.git("log", "-1", "--format=%B", pushed), batchTrailer+": "+mc7) {
t.Error("#7's merge lost its trailer")
}
if got := f.git("show", pushed+":app/seven.txt"); got != "7, resolved" {
t.Errorf("the resolution was lost: app/seven.txt is %q", got)
}
if want := "`fix(app): seven`, 1 commit, changed in the pick: `" + f.git("rev-parse", pushed+"^2")[:12] + "`"; !strings.Contains(stdout, want) {
t.Errorf("the body does not say the resolution differs from #7's commit (%s):\n%s", want, stdout)
}
if strings.Contains(stdout, "`fix(app): five`, 2 commits, changed") {
t.Errorf("the body calls #5's clean picks changed:\n%s", stdout)
}
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", pushed); code != 0 {
t.Errorf("verify of the rebuilt batch = %d\n%s\n%s", code, out, stderr)
}
deferred, err := parseDeferred(stdout, l)
if err != nil || len(deferred) != 1 || deferred[6].Reason != "fails the app e2e" {
t.Errorf("deferred = %+v, %v; want #6 for its reason", deferred, err)
}
if !strings.Contains(stdout, "### Deferred to the next batch") || !strings.Contains(stdout, "`fails the app e2e`") {
t.Errorf("the body does not show what is deferred and why:\n%s", stdout)
}
if !slices.Equal(gh.edited, []string{"#90 fix(backport): batch for release-1.1, 2026-10-26"}) || len(gh.created) != 1 {
t.Errorf("edited %q and created %q, want the open pull request edited", gh.edited, gh.created)
}
// check warns about it instead of failing.
reopen(f, gh)
code, _, stderr = f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/6 fix(app): six (fails the app e2e)") {
t.Errorf("check = %d, want 0 with a warning about #6:\n%s", code, stderr)
}
}
// The batch that held an original out keeps it out when a run continues it,
// and the next batch picks it again.
func TestDeferHoldsUntilTheNextBatch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "#6", "--reason", "fails the app e2e", "--push"); code != 0 {
t.Fatalf("run --defer = %d\n%s", code, stderr)
}
reopen(f, gh)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
f.now = f.now.AddDate(0, 0, 3)
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 0 {
t.Fatalf("run --continue = %d\n%s", code, stderr)
}
want := []string{"fix(app): five (#5)", "fix(app): eight (#8)"}
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, want) {
t.Errorf("the continued batch carries %q, want %q", got, want)
}
l, _ := parseLine("release-1.1")
if deferred, err := parseDeferred(stdout, l); err != nil || deferred[6].Reason != "fails the app e2e" {
t.Errorf("the continued body defers %+v, %v; want #6 still held", deferred, err)
}
// The batch merges; the next one starts from the line and tries #6 again.
f.git("push", "-q", "origin", f.remoteHead(batchBranch1)+":refs/heads/release-1.1")
gh.prs = nil
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
if code != 0 {
t.Fatalf("the next run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("the next batch carries %q, want #6 tried again", got)
}
if strings.Contains(stdout, "Deferred to the next batch") {
t.Errorf("the next batch still holds something out:\n%s", stdout)
}
}
// A rebuild picks the rest of the batch again onto a line that moved since,
// and refuses when dropping an original leaves a later commit that no longer
// applies, without pushing anything.
func TestDeferOnAMovedLine(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/x.txt": "five\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/x.txt": "five and six\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
moved := f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push")
if code != 2 || !strings.Contains(stderr, ", of #6, no longer applies (app/x.txt); defer it as well") {
t.Errorf("run --defer 5 = %d, want a refusal naming #6:\n%s", code, stderr)
}
if got := f.remoteHead(batchBranch1); got != head {
t.Errorf("the refused rebuild moved the batch to %.12s", got)
}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5,6", "--reason", "x", "--push"); code != 0 {
t.Fatalf("run --defer 5,6 = %d\n%s", code, stderr)
}
pushed := f.remoteHead(batchBranch1)
if got := f.git("rev-parse", pushed+"^"); got != moved {
t.Errorf("the rebuild sits on %.12s, want the line's head %.12s", got, moved)
}
if got := f.subjects("origin/release-1.1", pushed); !slices.Equal(got, []string{"fix(app): seven (#7)"}) {
t.Errorf("the rebuilt batch carries %q, want #7", got)
}
}
// What --defer cannot do is refused before anything is touched.
func TestDeferRefuses(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "feat(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x"); code != 2 || !strings.Contains(stderr, "no batch pull request is open") {
t.Errorf("--defer with no batch open = %d (%s), want a refusal", code, stderr)
}
head := openBatch(t, f, gh)
for _, args := range [][]string{
{"--defer", "5"},
{"--defer", "5", "--reason", " \n "},
{"--reason", "x"},
{"--defer", "five", "--reason", "x"},
{"--defer", "5,-1", "--reason", "x"},
{"--defer", "5", "--reason", "x", "--onto", head},
{"--defer", "5", "--reason", "x", "--continue"},
{"--defer", "5", "--reason", "x", "--date", "2026-10-26"},
{"--continue", "--onto", head},
{"--defer", "6", "--reason", "x"},
{"--defer", "4", "--reason", "x"},
} {
code, _, stderr := f.exec(gh, append([]string{"run", "--line", "release-1.1"}, args...)...)
if code != 2 {
t.Errorf("run %q = %d (%s), want 2", args, code, stderr)
}
}
if got := f.remoteHead(batchBranch1); got != head {
t.Errorf("a refused run moved the batch to %.12s", got)
}
}
// A rebuild replaces the head it started from and nothing else: one pushed
// since is refused by the tool, and by the remote through the lease.
func TestDeferLeasesTheHeadItRebuilt(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
l, _ := parseLine("release-1.1")
lineHead := f.git("rev-parse", "origin/release-1.1")
rebuilt := f.git("rev-parse", head+"~1")
// Someone pushes on top of the batch after the rebuild read its head.
f.git("switch", "-q", "--detach", head)
f.git("commit", "-q", "--allow-empty", "-m", "pushed since")
since := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
pr := f.batchPR(90, "release-1.1", batchBranch1, gh.prs[0].Body)
a := &app{gh: gh}
b := &batch{line: l, lineHead: lineHead, pr: &pr, prHead: head, src: recordSource{carrier: &pr, open: true, code: &pr}}
err := a.publish(b, publication{branch: batchBranch1, head: rebuilt, title: "title", body: "body"}, true)
if err == nil || !strings.Contains(err.Error(), "which the rebuild started from") {
t.Errorf("publish over a head pushed since = %v, want a refusal", err)
}
if _, err := git(pushArgs(batchBranch1, rebuilt, head)...); err == nil {
t.Error("the remote took a push whose lease names a head it no longer has")
}
if got := f.remoteHead(batchBranch1); got != since {
t.Errorf("the batch moved to %.12s, want it left at %.12s", got, since)
}
// The lease that names the head the remote has is taken, over a
// non-fast-forward; a plain push of the same is not.
if _, err := git(pushArgs(batchBranch1, rebuilt, "")...); err == nil {
t.Error("a push without a lease replaced the batch")
}
if _, err := git(pushArgs(batchBranch1, rebuilt, since)...); err != nil {
t.Errorf("a push leased on the remote's head = %v", err)
}
if got := f.remoteHead(batchBranch1); got != rebuilt {
t.Errorf("the batch is at %.12s, want %.12s", got, rebuilt)
}
}
// A plain run never replaces the batch, deferred or not: it stays a
// fast-forward.
func TestContinueIsAFastForward(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 {
t.Fatalf("run --continue = %d\n%s", code, stderr)
}
if pushed := f.remoteHead(batchBranch1); !descends(t, head, pushed) || pushed == head {
t.Errorf("the batch moved from %.12s to %.12s, want a fast-forward", head, pushed)
}
if !slices.Equal(gh.edited, []string{"#90 fix(backport): batch for release-1.1, 2026-10-26"}) {
t.Errorf("edited %q, want #90", gh.edited)
}
}
// A batch that carries a merge commit is not rebuilt: picking it again would
// lose whatever the merge resolved.
func TestDeferRefusesAMerge(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
f.git("switch", "-q", "--detach", head)
f.git("merge", "-q", "--no-ff", "-m", "Merge release-1.1 into the batch", "origin/release-1.1")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
reopen(f, gh)
gh.prs[0].Body = gh.bodies[0]
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push")
if code != 2 || !strings.Contains(stderr, "carries merge commit") {
t.Errorf("run --defer over a merge = %d, want a refusal:\n%s", code, stderr)
}
}
// A batch pull request whose body backport-batch did not write is never
// rewritten, nor its branch pushed: it is a maintainer's.
func TestPublishLeavesAHandWrittenBatch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
gh.prs[0].Body = "Picked by hand: #5."
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
for _, args := range [][]string{
{"--continue", "--push"},
{"--defer", "5", "--reason", "x", "--push"},
} {
code, _, stderr := f.exec(gh, append([]string{"run", "--line", "release-1.1"}, args...)...)
if code != 2 || !strings.Contains(stderr, "backport-batch did not write it") {
t.Errorf("run %q on a hand-written batch = %d, want a refusal:\n%s", args, code, stderr)
}
}
if got := f.remoteHead(batchBranch1); got != head || len(gh.edited) != 0 {
t.Errorf("the batch moved to %.12s and %q were edited, want it left alone", got, gh.edited)
}
}
// backport/skip added after the batch picked an original keeps the commit
// on the branch until --defer takes it out; the body says so meanwhile.
func TestSkipAfterThePick(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
gh.labels = map[string][]int{labelSkip: {6}}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue")
if code != 0 || !strings.Contains(stdout, "`backport/skip`, still on this branch") {
t.Errorf("run --continue = %d, want the body to say #6 is still on the branch:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "labelled backport/skip", "--push"); code != 0 {
t.Fatalf("run --defer of a skipped original = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): five (#5)"}) {
t.Errorf("the batch carries %q, want #6 taken out", got)
}
reopen(f, gh)
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 {
t.Errorf("check = %d\n%s", code, stderr)
}
}
// closeWorld is a batch carrying #5 alone, opened and pushed.
func closeWorld(t *testing.T) (*fixture, *fakeGH, string) {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
h1 := openBatch(t, f, gh)
return f, gh, h1
}
// Deferring everything a batch carries closes it: the record says why and
// what it froze while the pull request is still open, then the pull request
// is closed and its branch deleted, leased on that head. The next batch
// tries what was deferred again.
func TestDeferEverythingClosesTheBatch(t *testing.T) {
f, gh, h1 := closeWorld(t)
var log []string
gh.after = map[string]func(){
"createComment": func() { log = append(log, "comment") },
"editPull": func() { log = append(log, "edit") },
"closePull": func() { log = append(log, "close, branch at "+f.remoteHead(batchBranch1)) },
}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the app e2e", "--push")
if code != 0 || !strings.Contains(stderr, "closed https://github.com/cozystack/cozystack/pull/90: without what is deferred it carries nothing beyond release-1.1; the next batch tries them again") {
t.Fatalf("run --defer of the last pick = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if want := []string{"comment", "edit", "close, branch at " + h1}; !slices.Equal(log, want) || f.remoteHead(batchBranch1) != "" || gh.find(90).State != "closed" {
t.Errorf("writes %q, branch at %q, #90 %s; want %q, the branch deleted and #90 closed", log, f.remoteHead(batchBranch1), gh.find(90).State, want)
}
rec, err := parseRecord(gh.find(90).Body, notifyLine)
if err != nil || rec.Closing == nil || *rec.Closing != (closing{PR: 90, Branch: batchBranch1, Head: h1}) || rec.Built.Options.Mode != modeClose || rec.deferred()[5].Reason != "fails the app e2e" {
t.Errorf("the closed batch's record is %+v, %v; want #5 deferred and the close it froze", rec, err)
}
if !strings.Contains(stdout, `"closing":{"pr":90,"branch":"`+batchBranch1+`","head":"`+h1+`"}`) {
t.Errorf("the body printed does not say what the close froze:\n%s", stdout)
}
code, _, stderr = f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "warning: deferred on #90 (closed) to the next batch: https://github.com/cozystack/cozystack/pull/5 ") {
t.Errorf("check = %d, want 0 with #5 deferred:\n%s", code, stderr)
}
gh.nextPR = 91
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): five (#5)"}) || gh.find(91) == nil {
t.Errorf("the next batch carries %q, want #5 tried again on a new pull request", got)
}
}
// A close that stopped part way is finished by run --push, and by nothing
// else, and never past a head it did not freeze; nothing is ever written to
// the closed pull request.
func TestCloseRecovery(t *testing.T) {
deferAll := []string{"run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the app e2e", "--push"}
push := []string{"run", "--line", "release-1.1", "--continue", "--push"}
// moveBranch pushes a commit onto the batch branch, or recreates it, as
// someone else would.
moveBranch := func(f *fixture, base string) string {
f.git("switch", "-q", "--detach", base)
f.git("commit", "-q", "--allow-empty", "-m", "pushed since")
h2 := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
return h2
}
writes := func(gh *fakeGH) int { return len(gh.issueComments) + len(gh.edited) }
t.Run("stopped after the record, then the branch moved", func(t *testing.T) {
f, gh, h1 := closeWorld(t)
gh.after = map[string]func(){"editPull": func() { gh.fail = map[string]error{"pull": errString("HTTP 502")} }}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 || !strings.Contains(stderr, "HTTP 502") {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
h2 := moveBranch(f, h1)
before := writes(gh)
if code, _, stderr := f.exec(gh, push...); code != 2 || !strings.Contains(stderr, h2[:12]) {
t.Errorf("run --push over a moved branch = %d, want 2 naming it:\n%s", code, stderr)
}
if gh.find(90).State != "open" || f.remoteHead(batchBranch1) != h2 || writes(gh) != before {
t.Error("the run acted on a branch that moved since the close froze it")
}
})
// GitHub and the remote are read apart, and either may be the first to
// show that someone pushed while the record was written.
for _, seen := range []string{"by the remote", "by GitHub"} {
t.Run("the branch moved while the record was written, seen "+seen, func(t *testing.T) {
f, gh, h1 := closeWorld(t)
gh.after = map[string]func(){"editPull": func() {
gh.remoteHead = func(string) string { return "" }
if seen == "by GitHub" {
gh.find(90).Head.SHA = strings.Repeat("b", 40)
} else {
moveBranch(f, h1)
}
}}
code, _, stderr := f.exec(gh, deferAll...)
f.git("switch", "-q", "main")
if code != 2 || !strings.Contains(stderr, "which its record froze") || gh.find(90).State != "open" || f.remoteHead(batchBranch1) == "" {
t.Errorf("run = %d, #90 %s, branch %q; want 2, #90 open and the branch kept:\n%s", code, gh.find(90).State, f.remoteHead(batchBranch1), stderr)
}
})
}
t.Run("the close failed", func(t *testing.T) {
f, gh, _ := closeWorld(t)
gh.fail = map[string]error{"closePull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
before := writes(gh)
// Anything but run --push leaves it as it is.
for _, args := range [][]string{
{"run", "--line", "release-1.1", "--continue"},
{"check", "--line", "release-1.1"},
{"fold", "--line", "release-1.1", "--original", "5", "--commit", strings.Repeat("a", 40)},
{"notify", "--line", "release-1.1", "--push"},
} {
if code, _, stderr := f.exec(gh, args...); code != 2 || !strings.Contains(stderr, "being closed") {
t.Errorf("%q = %d, want 2:\n%s", args, code, stderr)
}
f.git("switch", "-q", "main")
}
if writes(gh) != before || gh.find(90).State != "open" {
t.Error("a command other than run --push wrote to the batch")
}
code, _, stderr := f.exec(gh, push...)
if code != 0 || gh.find(90).State != "closed" || f.remoteHead(batchBranch1) != "" || writes(gh) != before {
t.Errorf("run --push = %d, #90 %s, branch %q, %d writes; want the close finished and nothing else\n%s", code, gh.find(90).State, f.remoteHead(batchBranch1), writes(gh)-before, stderr)
}
})
// A run that finishes a close stops there: the change it was asked for
// needs an open batch pull request, and none is left to make it on.
t.Run("a change asked for while the close was left", func(t *testing.T) {
f, gh, _ := closeWorld(t)
gh.fail = map[string]error{"closePull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
code, _, stderr := f.exec(gh, deferAll...)
f.git("switch", "-q", "main")
if code != 2 || !strings.Contains(stderr, "finished the close of #90 and made no other change") || gh.find(90).State != "closed" || f.remoteHead(batchBranch1) != "" {
t.Errorf("run --defer = %d, #90 %s, branch %q; want 2, the close finished and nothing deferred:\n%s", code, gh.find(90).State, f.remoteHead(batchBranch1), stderr)
}
})
t.Run("closed by hand while the close writes", func(t *testing.T) {
f, gh, h1 := closeWorld(t)
gh.after = map[string]func(){"createComment": func() { gh.find(90).State = "closed" }}
code, _, stderr := f.exec(gh, deferAll...)
f.git("switch", "-q", "main")
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || len(gh.edited) != 0 || f.remoteHead(batchBranch1) != h1 {
t.Errorf("run = %d, edited %q, branch %q; want 2, no edit and the branch kept:\n%s", code, gh.edited, f.remoteHead(batchBranch1), stderr)
}
})
t.Run("the delete failed", func(t *testing.T) {
f, gh, h1 := closeWorld(t)
gh.after = map[string]func(){"closePull": func() { f.refusePushes(true) }}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
f.refusePushes(false)
code, _, stderr := f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "warning: batch pull request #90 is closed, but its branch "+batchBranch1+" is still at "+h1[:12]) {
t.Errorf("check = %d, want 0 naming the branch:\n%s", code, stderr)
}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue"); code != 2 {
t.Errorf("run without --push = %d, want 2:\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if code, _, stderr := f.exec(gh, push...); code != 0 || f.remoteHead(batchBranch1) != "" {
t.Errorf("run --push = %d, branch %q; want it deleted\n%s", code, f.remoteHead(batchBranch1), stderr)
}
mustRun(t, f, gh, "check", "--line", "release-1.1")
})
// A batch pull request a person opens on the branch a close left owns
// it: neither recovery nor check offers to delete it, moved or not. One
// on another branch does not stop the delete.
for _, tc := range []struct {
name string
branch string
moved bool
}{
{"a batch opened on the branch the close left", batchBranch1, false},
{"a batch opened on the branch the close left, moved since", batchBranch1, true},
{"a batch opened on another branch", "backport-batch/release-1.1-20261027", false},
} {
t.Run(tc.name, func(t *testing.T) {
f, gh, h1 := closeWorld(t)
gh.after = map[string]func(){"closePull": func() { f.refusePushes(true) }}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
f.refusePushes(false)
gh.after = nil
if tc.branch != batchBranch1 {
f.git("push", "-q", "origin", h1+":refs/heads/"+tc.branch)
}
left := h1
if tc.moved {
left = moveBranch(f, h1)
}
gh.prs = append(gh.prs, f.batchPR(91, "release-1.1", tc.branch, "a maintainer's batch"))
taken := tc.branch == batchBranch1
_, _, stderr := f.exec(gh, "check", "--line", "release-1.1")
f.git("switch", "-q", "main")
if named := strings.Contains(stderr, "batch pull request #91 is open on "+batchBranch1); named != taken || taken == strings.Contains(stderr, "--push deletes it") || taken && strings.Contains(stderr, "git push origin --delete") {
t.Errorf("check names the branch as #91's: %v, want %v, and offers no delete of it:\n%s", named, taken, stderr)
}
code, _, stderr := f.exec(gh, push...)
f.git("switch", "-q", "main")
if taken && (code != 2 || !strings.Contains(stderr, "batch pull request #91 is open on "+batchBranch1) || strings.Contains(stderr, "git push origin --delete") || f.remoteHead(batchBranch1) != left || gh.find(91).State != "open") {
t.Errorf("run --push = %d, branch %q; want 2, no delete offered and #91's branch kept:\n%s", code, f.remoteHead(batchBranch1), stderr)
}
if !taken && (code != 0 || f.remoteHead(batchBranch1) != "") {
t.Errorf("run --push = %d, branch %q; want the left branch deleted:\n%s", code, f.remoteHead(batchBranch1), stderr)
}
})
}
t.Run("the branch moved after the close", func(t *testing.T) {
f, gh, h1 := closeWorld(t)
var h2 string
gh.after = map[string]func(){"closePull": func() { h2 = moveBranch(f, h1) }}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 || !strings.Contains(stderr, "is at "+h2[:12]+" now") {
t.Fatalf("run = %d, want 2 naming where the branch moved\n%s", code, stderr)
}
f.git("switch", "-q", "main")
// What was pushed onto the branch may be work the next run cannot
// pick again, so the refusal promises nothing about deleting it.
if code, _, stderr := f.exec(gh, push...); code != 2 || !strings.Contains(stderr, h1[:12]) || !strings.Contains(stderr, h2[:12]) || !strings.Contains(stderr, "#90") || strings.Contains(stderr, "nothing on it is lost") || !strings.Contains(stderr, "check what "+h2[:12]+" carries") {
t.Errorf("run --push = %d, want 2 naming both heads and #90, and what to check before deleting:\n%s", code, stderr)
}
if f.remoteHead(batchBranch1) != h2 {
t.Error("the run moved a branch it did not freeze")
}
if _, err := git(deleteArgs(batchBranch1, h1)...); err == nil || f.remoteHead(batchBranch1) != h2 {
t.Error("the remote took a delete whose lease names a head it no longer has")
}
})
t.Run("a batch of the same day that opened no pull request", func(t *testing.T) {
f, gh, _ := closeWorld(t)
if code, _, stderr := f.exec(gh, deferAll...); code != 0 || f.remoteHead(batchBranch1) != "" {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
// The same day, a new batch reuses the branch name, pushes, and
// fails to open its pull request.
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh.fail = map[string]error{"createPull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, push...); code != 2 {
t.Fatalf("the same-day run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
h2 := f.remoteHead(batchBranch1)
for _, args := range [][]string{push, {"run", "--line", "release-1.1", "--onto", h2, "--push"}} {
if code, _, stderr := f.exec(gh, args...); code != 2 || !strings.Contains(stderr, "git push origin --delete "+batchBranch1) {
t.Errorf("%q = %d, want 2 naming the branch to delete:\n%s", args, code, stderr)
}
f.git("switch", "-q", "main")
}
if f.remoteHead(batchBranch1) != h2 || len(gh.created) != 1 {
t.Errorf("the branch moved to %.12s or a pull request was opened: %q", f.remoteHead(batchBranch1), gh.created)
}
f.git("push", "-q", "origin", "--delete", batchBranch1)
gh.nextPR = 91
mustRun(t, f, gh, push...)
if gh.find(91) == nil {
t.Error("after the branch was deleted the next run opened no batch")
}
})
t.Run("a preview", func(t *testing.T) {
f, gh, _ := closeWorld(t)
gh.fail = map[string]error{"closePull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
before := writes(gh)
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x")
if code != 2 || !strings.Contains(stderr, "run --line release-1.1 --push finishes") || writes(gh) != before || gh.find(90).State != "open" {
t.Errorf("a preview = %d, want 2 naming the step left and nothing written:\n%s", code, stderr)
}
})
}
// A batch that merged on GitHub and left its branch behind is named when a
// run of the same day refuses to push over that branch.
func TestRefusalNamesTheMergedBatchsBranch(t *testing.T) {
f, gh, _ := closeWorld(t)
head := f.remoteHead(batchBranch1)
mergeBatch(f, gh)
// A commit pushed onto the batch after it merged is left on its branch.
f.git("switch", "-q", "--detach", head)
f.git("commit", "-q", "--allow-empty", "-m", "pushed after the merge")
pushed := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 2 || !strings.Contains(stderr, "it is the branch of batch pull request #90, which is merged") || strings.Contains(stderr, "--onto") || !strings.Contains(stderr, "check what "+pushed[:12]+" carries") {
t.Errorf("run --push = %d, want a refusal naming #90:\n%s", code, stderr)
}
}
// A rebuild that stops names the files that no longer apply on the line of
// its refusal, whatever the files are called.
func TestDeferRefusalNamesFilesOnOneLine(t *testing.T) {
f := newFixture(t)
f.lines()
name := "app/x\nwarning: forged.txt"
f.pr(5, "fix(app): five", "", map[string]string{name: "five\n"})
f.pr(6, "fix(app): six", "", map[string]string{name: "five and six\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push")
f.git("switch", "-q", "main")
if code != 2 || !strings.Contains(stderr, "no longer applies") {
t.Fatalf("run --defer 5 = %d, want the refusal:\n%s", code, stderr)
}
for _, ln := range strings.Split(stderr, "\n") {
if strings.HasPrefix(ln, "warning: forged") {
t.Errorf("the refusal split onto a line of its own: %q", ln)
}
}
}
+912
View File
@@ -0,0 +1,912 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"path"
"regexp"
"slices"
"strconv"
"strings"
"time"
"unicode/utf8"
)
// What an original builds on that its line lacks is read from git alone, in
// two ways: a file it modifies or deletes that the line does not have, named
// by the first-parent commit on main that added it (V1), and a line it
// removes or changes that git blame gives to a commit the line lacks, kept
// only while that line's text is absent from the line's file (V2c). Either
// names the first-parent commit that brought the evidence to main: the merge
// of another original, or a commit that merged no pull request.
// Bounds per original, past which the evidence is incomplete rather than
// read at any cost.
const (
maxDepFiles = 256
maxBlamedLines = 20000
maxDepBlob = 8 << 20
gitDeadline = 2 * time.Minute
)
// dependency is a first-parent commit on main an original builds on, with
// the original's paths the evidence is in.
type dependency struct {
merge string
// v is the original merge merged, nil for a commit that merged none.
v *verdict
paths []string
// added says some of the evidence is a file the line lacks (V1).
added bool
}
// errDeadline is a git call that ran out of time: incomplete evidence, not
// a failure.
var errDeadline = errors.New("git took longer than " + gitDeadline.String())
// gitTimed runs git under the deadline the detector gives each call.
func gitTimed(args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), gitDeadline)
defer cancel()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Env = append(cmd.Environ(), commandEnv...)
var out, errOut bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &errOut
err := cmd.Run()
switch {
case ctx.Err() != nil:
return "", errDeadline
case err != nil:
return "", fmt.Errorf("git %s: %w: %s", quotedArgs(args), err, terminalSafe(errOut.String()))
}
return out.String(), nil
}
// quotedArgs names a git command's arguments for an error, each quoted: a
// file name can hold a newline, and the error reaches a terminal and a CI
// log, which would take the rest for a line of their own.
func quotedArgs(args []string) string {
out := make([]string, len(args))
for i, a := range args {
out[i] = strconv.Quote(a)
}
return strings.Join(out, " ")
}
// gitStatusTimed is gitStatus under the detector's deadline: a non-zero
// exit is an answer, and running out of time is errDeadline.
func gitStatusTimed(args ...string) (stdout, stderr string, code int, err error) {
ctx, cancel := context.WithTimeout(context.Background(), gitDeadline)
defer cancel()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Env = append(cmd.Environ(), commandEnv...)
var out, errOut bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &errOut
err = cmd.Run()
var ee *exec.ExitError
switch {
case ctx.Err() != nil:
return "", "", -1, errDeadline
case errors.As(err, &ee):
return out.String(), errOut.String(), ee.ExitCode(), nil
case err != nil:
return "", "", -1, fmt.Errorf("git %s: %w", quotedArgs(args), err)
}
return out.String(), errOut.String(), 0, nil
}
// broughtBy maps every commit in base..mainHead to the first-parent commit
// that brought it to main: each first-parent commit claims itself, then
// every commit of the range reachable from its other parents that no
// earlier one claimed. A commit outside the map is an ancestor of base.
func broughtBy(base, mainHead string) (map[string]string, error) {
first, err := revList("--first-parent", "--reverse", "--end-of-options", base+".."+mainHead)
if err != nil {
return nil, err
}
out, err := git("rev-list", "--parents", "--topo-order", "--end-of-options", base+".."+mainHead)
if err != nil {
return nil, err
}
parents := map[string][]string{}
for ln := range strings.Lines(out) {
f := strings.Fields(ln)
if len(f) == 0 {
continue
}
for _, sha := range f {
if checkSHA(sha) != nil {
return nil, fmt.Errorf("rev-list: %q is not a commit", sha)
}
}
parents[f[0]] = f[1:]
}
by := map[string]string{}
for _, fp := range first {
if _, ok := parents[fp]; !ok {
return nil, fmt.Errorf("rev-list: %s is on main's first-parent line but not in its range", fp)
}
by[fp] = fp
stack := slices.Clone(parents[fp][min(1, len(parents[fp])):])
for len(stack) > 0 {
c := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if _, inRange := parents[c]; !inRange || by[c] != "" {
continue
}
by[c] = fp
stack = append(stack, parents[c]...)
}
}
return by, nil
}
// hunk is an old range of a diff, a,+b in git blame's terms.
type hunk struct{ start, count int }
var hunkRE = regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@`)
// oldRanges reads the removed or changed line ranges of a -U0 diff of one
// file, and whether git called it binary. A header it cannot read is an
// error, never evidence of absence.
func oldRanges(diff string) ([]hunk, bool, error) {
var out []hunk
for ln := range strings.Lines(diff) {
ln = strings.TrimSuffix(ln, "\n")
switch {
case strings.HasPrefix(ln, "Binary files ") && strings.HasSuffix(ln, " differ"):
return nil, true, nil
case strings.HasPrefix(ln, "@@"):
m := hunkRE.FindStringSubmatch(ln)
if m == nil {
return nil, false, fmt.Errorf("unreadable hunk header %q", terminalSafe(ln))
}
start, _ := strconv.Atoi(m[1])
count := 1
if m[2] != "" {
count, _ = strconv.Atoi(m[2])
}
if count > 0 {
out = append(out, hunk{start, count})
}
}
}
return out, false, nil
}
// blameArgs blames ranges of file at rev, a full commit, which git takes for
// a revision whatever follows: blame refuses --end-of-options.
func blameArgs(rev, file string, ranges []hunk) ([]string, error) {
if err := checkSHA(rev); err != nil {
return nil, err
}
args := []string{"blame", "--porcelain", "-w"}
for _, r := range ranges {
args = append(args, "-L", fmt.Sprintf("%d,+%d", r.start, r.count))
}
return append(args, rev, "--", file), nil
}
// blamedLine is one line git blame gives to a commit.
type blamedLine struct{ commit, text string }
// parseBlame reads git blame --porcelain: a header per line, the commit's
// fields the first time it appears, and the line itself after a tab.
// Anything else is an error, never evidence of absence.
func parseBlame(out string) ([]blamedLine, error) {
var lines []blamedLine
commit := ""
for ln := range strings.Lines(out) {
ln = strings.TrimSuffix(ln, "\n")
switch {
case strings.HasPrefix(ln, "\t"):
if commit == "" {
return nil, errors.New("git blame: a line with no header")
}
lines = append(lines, blamedLine{commit: commit, text: ln[1:]})
commit = ""
case commit == "":
f := strings.Fields(ln)
if len(f) < 3 || len(f) > 4 || checkSHA(f[0]) != nil {
return nil, fmt.Errorf("git blame: unreadable header %q", terminalSafe(ln))
}
for _, n := range f[1:] {
if v, err := strconv.Atoi(n); err != nil || v <= 0 {
return nil, fmt.Errorf("git blame: unreadable header %q", terminalSafe(ln))
}
}
commit = f[0]
}
}
if commit != "" {
return nil, errors.New("git blame: a header with no line")
}
return lines, nil
}
var generatedMarkerRE = regexp.MustCompile(`Code generated .* DO NOT EDIT`)
// generatedFiles are written whole by a tool, whatever they hold.
var generatedFiles = []string{"go.sum", "package-lock.json", "yarn.lock", "pnpm-lock.yaml"}
// cozyRDGenerated are the keys hack/update-crd.sh writes into an
// ApplicationDefinition, the rest of which people write.
var cozyRDGenerated = []string{
"apiVersion", "kind", "metadata.name", "spec.application.openAPISchema", "spec.release.prefix",
"spec.release.chartRef.kind", "spec.release.chartRef.name", "spec.release.chartRef.namespace",
"spec.dashboard.description", "spec.dashboard.icon", "spec.dashboard.keysOrder",
}
var yamlKeyRE = regexp.MustCompile(`^( *)([A-Za-z0-9_.-]+):(?:\s|$)`)
// generatedLines says which lines of a file at a commit a tool wrote: all of
// them for a generated file, the keys hack/update-crd.sh sets for an
// ApplicationDefinition, found by indentation, and none otherwise. ok is
// false for an ApplicationDefinition the scan cannot read, whose lines are
// then not blamed at all.
func generatedLines(file string, blob []byte) (skip map[int]bool, whole, ok bool) {
base := path.Base(file)
if strings.HasPrefix(base, "zz_generated") || slices.Contains(generatedFiles, base) {
return nil, true, true
}
lines := strings.Split(string(blob), "\n")
for _, ln := range lines[:min(len(lines), 100)] {
if generatedMarkerRE.MatchString(ln) {
return nil, true, true
}
}
if rd, _ := path.Match(cozyRDGlob, file); !rd {
return nil, false, true
}
skip = map[int]bool{}
type key struct {
indent int
name string
}
var stack []key
genIndent := -1
for i, ln := range lines {
n := i + 1
trimmed := strings.TrimLeft(ln, " ")
indent := len(ln) - len(trimmed)
switch {
case strings.HasPrefix(trimmed, "\t") || strings.HasPrefix(ln, "---") && i > 0:
return nil, false, false
case trimmed == "" || strings.HasPrefix(trimmed, "#"):
if genIndent >= 0 {
skip[n] = true
}
continue
case genIndent >= 0 && indent > genIndent:
skip[n] = true
continue
}
genIndent = -1
m := yamlKeyRE.FindStringSubmatch(ln)
if m == nil {
continue
}
for len(stack) > 0 && stack[len(stack)-1].indent >= indent {
stack = stack[:len(stack)-1]
}
stack = append(stack, key{indent, m[2]})
var names []string
for _, k := range stack {
names = append(names, k.name)
}
if slices.Contains(cozyRDGenerated, strings.Join(names, ".")) {
skip[n] = true
genIndent = indent
}
}
return skip, false, true
}
// trimmedLines is the set of a blob's lines with their edges trimmed, which
// is all a reformat of indentation or trailing space changes.
func trimmedLines(blob []byte) map[string]bool {
set := map[string]bool{}
for ln := range strings.Lines(string(blob)) {
set[strings.TrimSpace(ln)] = true
}
return set
}
// shown is a path as a diagnostic names it.
func shown(p string) string { return strconv.Quote(p) }
// incomplete is a diagnostic about evidence the detector could not read.
func incomplete(format string, args ...any) string {
return oneLineText("incomplete evidence: " + fmt.Sprintf(format, args...))
}
// dependencies reads what v builds on that target lacks, with what it could
// not read. V1 looks at the paths whose counterpart target lacks, V2c at
// the ones it has.
func (b *batch) dependencies(v *verdict, target string) ([]dependency, []string, error) {
if err := b.load(v); err != nil {
return nil, nil, err
}
if b.brought == nil {
var err error
if b.brought, err = broughtBy(b.base, b.mainHead); err != nil {
return nil, nil, err
}
}
corr, unknown, err := b.correspondence(v, target)
if err != nil {
return nil, nil, err
}
var diag []string
for _, p := range unknown {
diag = append(diag, incomplete("git skipped rename detection for %s", shown(p)))
}
var at []string
for _, c := range corr {
at = append(at, c)
}
have, err := treeEntries(target, at)
if err != nil {
return nil, nil, err
}
parent, err := resolve(v.o.merge + "^1")
if err != nil {
return nil, nil, err
}
byMerge := map[string]*dependency{}
add := func(merge, p string, added bool) {
d := byMerge[merge]
if d == nil {
d = &dependency{merge: merge, v: b.findMerge(merge)}
byMerge[merge] = d
}
if !slices.Contains(d.paths, p) {
d.paths = append(d.paths, p)
}
d.added = d.added || added
}
files, blamed := 0, 0
for _, c := range v.changes {
cp, ok := corr[c.path]
if !ok || c.status != 'M' && c.status != 'D' {
continue
}
if !utf8.ValidString(c.path) {
diag = append(diag, incomplete("a path that is not UTF-8, %s", shown(c.path)))
continue
}
if files++; files > maxDepFiles {
diag = append(diag, incomplete("more than %d files to read", maxDepFiles))
break
}
cur, present := have[cp]
if !present {
out, err := gitTimed("--literal-pathspecs", "log", "--first-parent", "--diff-merges=first-parent", "--no-renames", "--diff-filter=A", "--format=%H", "--no-patch", "-1", "--end-of-options", b.base+".."+parent, "--", c.path)
switch {
case errors.Is(err, errDeadline):
diag = append(diag, incomplete("%s, finding what added %s", err, shown(c.path)))
continue
case err != nil:
return nil, nil, err
}
adder := strings.TrimSpace(out)
if adder == "" {
diag = append(diag, oneLineText(fmt.Sprintf("%s is not on the line, and nothing merged into main since it branched added it", shown(c.path))))
continue
}
if checkSHA(adder) != nil {
return nil, nil, fmt.Errorf("git log for %s: %q is not a commit", shown(c.path), terminalSafe(adder))
}
add(adder, c.path, true)
continue
}
found, more, err := b.blamed(v, parent, c.path, cur, target, maxBlamedLines-blamed)
if err != nil {
return nil, nil, err
}
blamed += more.lines
diag = append(diag, more.diag...)
for merge := range found {
add(merge, c.path, false)
}
if more.capped {
break
}
}
var deps []dependency
for _, d := range byMerge {
slices.Sort(d.paths)
deps = append(deps, *d)
// The rules decide before the line is looked at, so whether the
// line carries a prerequisite they keep out is asked here.
if f := d.v; f != nil && f.rule != 0 && f.rule != ruleOnLine && f.rule != ruleNewer && b.lineHist != nil {
if _, asked := b.lineCarries[f]; !asked {
if err := b.load(f); err != nil {
return nil, nil, err
}
how, err := b.lineHist.carries(f.o, f.commits)
if err != nil {
return nil, nil, err
}
if b.lineCarries == nil {
b.lineCarries = map[*verdict]bool{}
}
b.lineCarries[f] = how != ""
}
}
}
slices.SortFunc(deps, func(x, y dependency) int { return strings.Compare(x.merge, y.merge) })
return deps, diag, nil
}
// blameResult is what blaming one file read: how many lines, what it could
// not read, and whether the original's line budget ran out.
type blameResult struct {
lines int
diag []string
capped bool
}
// blamed gives each line of file that v removes or changes, at parent, to
// the first-parent commit that brought it, and keeps the commits one of
// whose lines target's counterpart of the file, cur, lacks.
func (b *batch) blamed(v *verdict, parent, file string, cur treeEntry, target string, budget int) (map[string]bool, blameResult, error) {
var res blameResult
entries, err := treeEntries(parent, []string{file})
if err != nil {
return nil, res, err
}
pre := entries[file]
switch {
case pre.mode == "120000" || cur.mode == "120000":
res.diag = append(res.diag, incomplete("%s is a symlink", shown(file)))
return nil, res, nil
case pre.kind == "commit" || cur.kind == "commit":
res.diag = append(res.diag, incomplete("%s is a submodule", shown(file)))
return nil, res, nil
case !pre.regular() || !cur.regular():
res.diag = append(res.diag, incomplete("%s is no file git can blame", shown(file)))
return nil, res, nil
}
// A file the line already has as the original makes it builds on
// nothing the line lacks, whoever wrote the lines it replaced.
if v.o.merge != "" && cur.oid != pre.oid {
post, err := treeEntries(v.o.merge, []string{file})
if err != nil {
return nil, res, err
}
if p1, ok := post[file]; ok && p1.regular() {
if cur.oid == p1.oid {
return nil, res, nil
}
applied, _, err := b.alreadyApplied(pre.oid, cur.oid, p1.oid)
if err != nil {
return nil, res, err
}
if applied {
return nil, res, nil
}
}
}
for _, oid := range []string{pre.oid, cur.oid} {
out, err := git("cat-file", "-s", oid)
if err != nil {
return nil, res, err
}
size, err := strconv.Atoi(strings.TrimSpace(out))
if err != nil {
return nil, res, fmt.Errorf("git cat-file -s %s: %q is no size", oid, terminalSafe(out))
}
if size > maxDepBlob {
res.diag = append(res.diag, incomplete("%s is larger than %d bytes", shown(file), maxDepBlob))
return nil, res, nil
}
}
diff, err := gitTimed("--literal-pathspecs", "diff", "--no-ext-diff", "--no-textconv", "--no-renames", "-U0", "--end-of-options", parent, v.o.merge, "--", file)
if errors.Is(err, errDeadline) {
res.diag = append(res.diag, incomplete("%s, diffing %s", err, shown(file)))
return nil, res, nil
} else if err != nil {
return nil, res, err
}
ranges, binary, err := oldRanges(diff)
switch {
case err != nil:
return nil, res, fmt.Errorf("the diff of %s in #%d: %w", shown(file), v.o.number, err)
case binary:
res.diag = append(res.diag, incomplete("%s is binary", shown(file)))
return nil, res, nil
case len(ranges) == 0:
return nil, res, nil
}
blobs, err := readObjects([]string{pre.oid, cur.oid}, "blob")
if err != nil {
return nil, res, err
}
skip, whole, ok := generatedLines(file, blobs[pre.oid])
switch {
case whole:
return nil, res, nil
case !ok:
res.diag = append(res.diag, incomplete("%s, whose generated keys could not be told apart", shown(file)))
return nil, res, nil
}
var kept []hunk
for _, r := range ranges {
for n := r.start; n < r.start+r.count; n++ {
if skip[n] {
continue
}
if l := len(kept) - 1; l >= 0 && kept[l].start+kept[l].count == n {
kept[l].count++
} else {
kept = append(kept, hunk{n, 1})
}
res.lines++
}
}
if res.lines > budget {
res.diag = append(res.diag, incomplete("more than %d lines to blame", maxBlamedLines))
res.capped = true
return nil, res, nil
}
if len(kept) == 0 {
return nil, res, nil
}
args, err := blameArgs(parent, file, kept)
if err != nil {
return nil, res, err
}
out, err := gitTimed(args...)
if errors.Is(err, errDeadline) {
res.diag = append(res.diag, incomplete("%s, blaming %s", err, shown(file)))
return nil, res, nil
} else if err != nil {
return nil, res, err
}
lines, err := parseBlame(out)
if err != nil {
return nil, res, fmt.Errorf("blaming %s for #%d: %w", shown(file), v.o.number, err)
}
onTarget := trimmedLines(blobs[cur.oid])
found := map[string]bool{}
unmapped := false
for _, ln := range lines {
merge, inRange := b.brought[ln.commit]
switch {
case !inRange:
// An ancestor of the line's fork point, which the line has, or
// a commit main does not have at all.
before, err := b.beforeBase(ln.commit)
if err != nil {
return nil, res, err
}
unmapped = unmapped || !before
case merge == v.o.merge:
case !onTarget[strings.TrimSpace(ln.text)]:
found[merge] = true
}
}
if unmapped {
res.diag = append(res.diag, incomplete("git blame gives lines of %s to a commit that did not come through main", shown(file)))
}
return found, res, nil
}
// timed adds what has passed since start to the time the detector took.
func (b *batch) timed(start time.Time) { b.detecting += time.Since(start) }
// beforeBase says whether commit is an ancestor of the line's fork point,
// once per commit.
func (b *batch) beforeBase(commit string) (bool, error) {
if ok, seen := b.ancestors[commit]; seen {
return ok, nil
}
ok, err := isAncestor(commit, b.base)
if err == nil {
if b.ancestors == nil {
b.ancestors = map[string]bool{}
}
b.ancestors[commit] = ok
}
return ok, err
}
// analyze reads what every original the policy takes and nothing carries
// builds on that the line lacks, and keeps it with the original, for the
// parks and waits that follow from it.
func (b *batch) analyze() error {
defer b.timed(time.Now())
for _, v := range b.qualifying() {
if v.onBatch != "" {
continue
}
deps, diag, err := b.dependencies(v, b.lineHead)
if err != nil {
return err
}
v.deps = deps
v.addDiag(diag...)
}
return nil
}
// analysisMode is how a classification treats an original that changes a
// workflow: as the scheduled run does, whose token cannot push one, or as a
// maintainer's run.
type analysisMode struct{ noWorkflowFiles bool }
// edgeClass is what a prerequisite is to an original that builds on it,
// from what never matters to what decides first.
type edgeClass int
const (
edgeSatisfied edgeClass = iota
// edgeNewer is held by rule 6 on this line.
edgeNewer
// edgeDeferred is a deferred conflict, or deferred for a reason.
edgeDeferred
// edgeWorkflow waits on the open batch for a maintainer.
edgeWorkflow
// edgeMissing is on neither the line, the batch nor the record.
edgeMissing
// edgeNever never reaches the line.
edgeNever
)
// edge is a dependency whose prerequisite has not reached the line, with
// what that prerequisite is.
type edge struct {
dep dependency
class edgeClass
}
// ruleClass is what the rules alone make of a prerequisite, or ok false when
// it depends on what this classification makes of it: one the line or the
// open batch carries is there, whatever its label, drop or rule says, and a
// direct push and an original the rules keep out never come.
func (b *batch) ruleClass(f *verdict) (edgeClass, bool) {
switch {
case f == nil:
return edgeNever, true
case f.rule == ruleOnLine || f.onBatch != "" || f.batched != "" || b.lineCarries[f]:
return edgeSatisfied, true
case f.rule == ruleNewer:
return edgeNewer, true
case f.rule != 0:
return edgeNever, true
}
return 0, false
}
// edges sorts dependencies by what classOf says of each prerequisite into
// those that never come and those still to come, dropping the ones there.
func edges(deps []dependency, classOf func(*verdict) edgeClass) (never, pending []edge) {
for _, d := range deps {
switch c := classOf(d.v); c {
case edgeSatisfied:
case edgeNever:
never = append(never, edge{d, c})
default:
pending = append(pending, edge{d, c})
}
}
return never, pending
}
// strongest is the class of prerequisite that decides a wait, and the
// prerequisites of that class, in merge order.
func strongest(pending []edge) (edgeClass, []*verdict) {
top := edgeSatisfied
for _, e := range pending {
top = max(top, e.class)
}
var out []*verdict
for _, e := range pending {
if e.class == top && !slices.Contains(out, e.dep.v) {
out = append(out, e.dep.v)
}
}
return top, out
}
// auxiliary says whether a path holds tests or documentation, which an
// original can change without building on what it needs to work.
func auxiliary(p string) bool {
parts := strings.Split(p, "/")
base := parts[len(parts)-1]
for _, d := range parts[:len(parts)-1] {
if d == "test" || d == "tests" || d == "testdata" {
return true
}
}
switch {
case strings.HasSuffix(base, "_test.go"), strings.Contains(base, ".test."), strings.Contains(base, ".spec."), strings.HasSuffix(base, ".bats"):
return true
case len(parts) > 1 && parts[0] == "docs":
return true
}
return len(parts) == 1 && strings.HasSuffix(base, ".md")
}
// workflowPrimary says whether v waits for a maintainer rather than parks:
// in the scheduled mode, an original that changes a workflow, all of whose
// missing dependencies that never come are tests or docs, and whose
// workflow lines build on nothing still missing. The workflow is what it is
// for, and a maintainer brings over what applies. Only what is still
// missing counts: never and pending are what is left once what is there was
// taken out.
func workflowPrimary(mode analysisMode, v *verdict, never, pending []edge) (bool, error) {
if !mode.noWorkflowFiles || len(never) == 0 {
return false, nil
}
for _, e := range never {
if slices.ContainsFunc(e.dep.paths, func(p string) bool { return !auxiliary(p) }) {
return false, nil
}
}
for _, e := range append(slices.Clone(never), pending...) {
if slices.ContainsFunc(e.dep.paths, func(p string) bool { return strings.HasPrefix(p, workflowsDir) }) {
return false, nil
}
}
files, err := workflowFiles(v.commits)
return len(files) > 0, err
}
// testsOnly is the note a workflow original gets that waits for a
// maintainer although its tests build on what never comes.
func testsOnly(v *verdict, never []edge, l line) string {
var who []string
for _, e := range never {
if e.dep.v != nil {
who = append(who, fmt.Sprintf("#%d", e.dep.v.o.number))
} else {
who = append(who, e.dep.merge[:12])
}
}
slices.Sort(who)
return oneLineText(fmt.Sprintf("only its tests or docs build on %s, which never reaches %s", strings.Join(slices.Compact(who), ", "), l.branch()))
}
// dependencyPark is the park of v for prerequisites that never come.
func dependencyPark(v *verdict, never []edge) *parkedEntry {
p := &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkDependency}
for _, e := range never {
if e.dep.v != nil {
p.Needs = append(p.Needs, e.dep.v.o.number)
} else {
p.Commits = append(p.Commits, e.dep.merge)
}
}
slices.Sort(p.Needs)
p.Needs = slices.Compact(p.Needs)
slices.Sort(p.Commits)
p.Commits = slices.Compact(p.Commits)
return p
}
// depsAt is what v builds on that target lacks, read once per target: the
// run asks again at its head once earlier picks moved the branch.
func (b *batch) depsAt(v *verdict, target string) ([]dependency, error) {
key := target + " " + v.o.merge
if d, ok := b.depCache[key]; ok {
return d, nil
}
deps, diag, err := b.dependencies(v, target)
if err != nil {
return nil, err
}
v.addDiag(diag...)
if b.depCache == nil {
b.depCache = map[string][]dependency{}
}
b.depCache[key] = deps
return deps, nil
}
// stateKind is what a classification makes of an original the policy takes
// that nothing carries, in the order that decides it.
type stateKind int
const (
// stHeld is deferred for a reason a person gave.
stHeld stateKind = iota + 1
// stPartial would leave part of it without effect on the line.
stPartial
// stWorkflowPrimary waits for a maintainer, though its tests build on
// what never comes.
stWorkflowPrimary
// stParked builds on what never comes.
stParked
// stNeeds waits on prerequisites still to come.
stNeeds
// stOwn is what the record says of it, or missing.
stOwn
)
// state is a classification of one original: what it is, what it is to an
// original that builds on it, and for a wait the class that decides it and
// the prerequisites of that class.
type state struct {
kind stateKind
class edgeClass
wait edgeClass
needs []*verdict
}
// classify decides, in merge order, what becomes of every original the
// policy takes that neither the line nor the open batch carries, as an
// operation that picks nothing sees it: check, and the ones that only
// change the record. held are the reasons people deferred originals for,
// recorded what the record defers, and listed says the record's workflow
// waits still wait on an open batch. Parks it finds go into each verdict.
func (b *batch) classify(mode analysisMode, held map[int]string, recorded map[int]deferredEntry, listed bool) (map[*verdict]*state, error) {
states := map[*verdict]*state{}
classOf := func(f *verdict) edgeClass {
if c, ok := b.ruleClass(f); ok {
return c
}
if s := states[f]; s != nil {
return s.class
}
return edgeMissing
}
for _, v := range b.qualifying() {
if v.onBatch != "" {
continue
}
s := &state{}
states[v] = s
e := recorded[v.o.number]
never, pending := edges(v.deps, classOf)
primary, err := workflowPrimary(mode, v, never, pending)
if err != nil {
return nil, err
}
switch {
case held[v.o.number] != "":
s.kind, s.class = stHeld, edgeDeferred
case v.park != nil && v.park.Kind == parkPartial:
s.kind, s.class = stPartial, edgeNever
case primary:
s.kind, s.class = stWorkflowPrimary, edgeMissing
if listed && e.Workflows {
s.class = edgeWorkflow
}
case len(never) > 0:
v.park = dependencyPark(v, never)
s.kind, s.class = stParked, edgeNever
case len(pending) > 0:
s.kind = stNeeds
s.wait, s.needs = strongest(pending)
s.class = s.wait
case listed && e.Workflows:
s.kind, s.class = stOwn, edgeWorkflow
case !e.Workflows && len(e.Needs) == 0 && (e.Commit != "" || len(e.Files) > 0):
s.kind, s.class = stOwn, edgeDeferred
default:
s.kind, s.class = stOwn, edgeMissing
}
}
return states, nil
}
+329
View File
@@ -0,0 +1,329 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strings"
"testing"
)
// depsOf reads what original n builds on that the line lacks.
func depsOf(t testing.TB, f *fixture, n int) ([]dependency, []string) {
t.Helper()
f.git("fetch", "-q", "origin")
b, err := shapeOf(line11)
if err != nil {
t.Fatal(err)
}
deps, diag, err := b.dependencies(b.find(n), b.lineHead)
if err != nil {
t.Fatal(err)
}
return deps, diag
}
// depNames lists dependencies as #N or a short commit, with their paths.
func depNames(deps []dependency) []string {
var out []string
for _, d := range deps {
who := d.merge[:12]
if d.v != nil {
who = fmt.Sprintf("#%d", d.v.o.number)
}
out = append(out, fmt.Sprintf("%s %v %v", who, d.paths, d.added))
}
return out
}
// Every commit main gained since the line branched is brought by one commit
// of its first-parent line: a pull request's own commits by its merge, a
// direct push by itself, and a commit from before the fork point by none.
func TestBroughtBy(t *testing.T) {
f := newFixture(t)
f.lines()
base := f.git("rev-parse", "origin/release-1.1")
mc3 := f.prCommits(3, "feat(app): three", "",
change{files: map[string]string{"app/3.txt": "3\n"}},
change{mergeMain: true},
change{files: map[string]string{"app/3.txt": "3b\n"}})
f.write("app/push.txt", "pushed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "chore: a direct push")
f.git("push", "-q", "origin", "main")
push := f.git("rev-parse", "HEAD")
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
by, err := broughtBy(base, mc5)
if err != nil {
t.Fatal(err)
}
moved := f.git("rev-parse", mc3+"^1")
for _, c := range strings.Fields(f.git("rev-list", mc3+"^1.."+mc3+"^2")) {
if c == moved {
continue
}
if by[c] != mc3 {
t.Errorf("%.12s, a commit of #3's branch, is brought by %.12s, want #3's merge", c, by[c])
}
}
for c, want := range map[string]string{mc3: mc3, moved: moved, push: push, mc5: mc5, f.git("rev-parse", mc5+"^2"): mc5} {
if by[c] != want {
t.Errorf("%.12s is brought by %.12s, want %.12s", c, by[c], want)
}
}
if _, ok := by[base]; ok {
t.Errorf("the fork point %.12s is in the map", base)
}
}
// V1: a file the original changes that the line does not have names the
// first-parent commit on main that added it, a merge or a direct push.
func TestDependenciesV1(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
f.write("app/y.txt", appFile)
f.git("add", "-A")
f.git("commit", "-q", "-m", "chore: y, pushed")
f.git("push", "-q", "origin", "main")
push := f.git("rev-parse", "HEAD")
f.pr(5, "fix(app): fix x and y", "", map[string]string{"app/x.txt": "fixed\n", "app/y.txt": "fixed\n"})
deps, diag := depsOf(t, f, 5)
want := []string{push[:12] + " [app/y.txt] true", "#3 [app/x.txt] true"}
slices.Sort(want)
got := depNames(deps)
slices.Sort(got)
if !slices.Equal(got, want) || len(diag) != 0 {
t.Errorf("dependencies = %q, %q; want %q", got, diag, want)
}
}
// v2World gives the line app/a.txt as the base had it, and main a feat that
// changes its second line and a fix of that same line.
func v2World(t testing.TB, onLine string) *fixture {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
f.lines()
if onLine != "" {
f.onLine("release-1.1", "fix(app): a, by hand", map[string]string{"app/a.txt": onLine})
}
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
return f
}
// V2c: a line the original changes that blame gives to another original is
// a dependency only while the line's file lacks that line's text.
func TestDependenciesV2c(t *testing.T) {
if deps, diag := depsOf(t, v2World(t, ""), 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) || len(diag) != 0 {
t.Errorf("dependencies = %q, %q; want #3 through app/a.txt", depNames(deps), diag)
}
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", "TWO", 1)), 5); len(deps) != 0 {
t.Errorf("dependencies with #3's line on the line by hand = %q; want none", depNames(deps))
}
}
// The content check trims a line's edges and nothing else: a reindented line
// is there, a line with a space inside it is not.
func TestDependenciesTrimOnlyEdges(t *testing.T) {
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", " TWO\t", 1)), 5); len(deps) != 0 {
t.Errorf("dependencies with #3's line reindented = %q; want none", depNames(deps))
}
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", "T WO", 1)), 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) {
t.Errorf("dependencies with a space inside #3's line = %q; want #3", depNames(deps))
}
}
// Lines a tool wrote are not blamed: a generated file whole, and the keys
// hack/update-crd.sh writes into an ApplicationDefinition, while the keys a
// person writes there are.
func TestDependenciesSkipGeneratedRanges(t *testing.T) {
const rd = "packages/system/app-rd/cozyrds/app.yaml"
rdFile := func(schema, category string) string {
return "apiVersion: cozystack.io/v1alpha1\nkind: ApplicationDefinition\nmetadata:\n name: app\nspec:\n application:\n kind: App\n openAPISchema: |-\n {\"v\":" + schema + "}\n dashboard:\n category: " + category + "\n keysOrder: [[\"spec\", \"" + schema + "\"]]\n"
}
world := func(t *testing.T, before, feat, fix map[string]string) *fixture {
f := newFixture(t)
f.squash(2, "chore: generated files", "", before)
f.lines()
f.pr(3, "feat(api): regenerate", "", feat)
f.pr(5, "fix(api): regenerate again", "", fix)
return f
}
gen := "// Code generated by controller-gen. DO NOT EDIT.\n\npackage v1\n\nvar a = 1\n"
f := world(t,
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 1\n", "api/types_gen.go": gen, rd: rdFile("1", "A")},
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 2\n", "api/types_gen.go": strings.Replace(gen, "= 1", "= 2", 1), rd: rdFile("2", "A")},
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 3\n", "api/types_gen.go": strings.Replace(gen, "= 1", "= 3", 1), rd: rdFile("3", "A")})
if deps, diag := depsOf(t, f, 5); len(deps) != 0 || len(diag) != 0 {
t.Errorf("dependencies through generated lines = %q, %q; want none", depNames(deps), diag)
}
f = world(t, map[string]string{rd: rdFile("1", "A")}, map[string]string{rd: rdFile("2", "B")}, map[string]string{rd: rdFile("3", "C")})
if deps, _ := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [" + rd + "] false"}) {
t.Errorf("dependencies through a key a person writes = %q; want #3", depNames(deps))
}
}
// Evidence the detector cannot read, a binary file or a symlink, is a
// diagnostic and never a dependency.
func TestDependenciesIncompleteNeverParks(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: an image", "", map[string]string{"app/a.bin": "a\x00one"})
f.lines()
f.pr(3, "feat(app): a new image", "", map[string]string{"app/a.bin": "a\x00two"})
f.pr(5, "fix(app): a fixed image", "", map[string]string{"app/a.bin": "a\x00three"})
if deps, diag := depsOf(t, f, 5); len(deps) != 0 || !slices.Equal(diag, []string{`incomplete evidence: "app/a.bin" is binary`}) {
t.Errorf("dependencies = %q, %q; want none and a diagnostic", depNames(deps), diag)
}
}
// Output of git the detector cannot read whole is an error, never taken
// for an absent dependency.
func TestDependenciesMalformedOutputIsAnError(t *testing.T) {
sha := strings.Repeat("a", 40)
for name, out := range map[string]string{
"a short header": sha + " 1\n\tline\n",
"a header of no commit": "aaaa 1 1 1\n\tline\n",
"a header of no number": sha + " x 1 1\n\tline\n",
"a line with no header": "\tline\n",
"a header with no line": sha + " 1 1 1\nauthor x\n",
} {
if _, err := parseBlame(out); err == nil {
t.Errorf("%s: parseBlame accepted %q", name, out)
}
}
if lines, err := parseBlame(sha + " 2 2 1\nauthor x\nfilename a\n\ttwo\n" + sha + " 4 4\n\tfour\n"); err != nil || len(lines) != 2 || lines[1].text != "four" {
t.Errorf("parseBlame of sound output = %+v, %v", lines, err)
}
if _, _, err := oldRanges("@@ -x +1 @@\n"); err == nil {
t.Error("oldRanges accepted an unreadable hunk header")
}
}
// Paths go to git as they are: a name a pathspec would read as a pattern
// means only itself.
func TestDependenciesLiteralPaths(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): a bracketed file", "", map[string]string{"app/[x].txt": appFile})
f.pr(4, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
f.pr(5, "fix(app): fix the bracketed file", "", map[string]string{"app/[x].txt": "fixed\n"})
if deps, _ := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [app/[x].txt] true"}) {
t.Errorf("dependencies = %q; want #3 alone", depNames(deps))
}
}
// A file with no final newline blames its last line like any other.
func TestDependenciesNoFinalNewline(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": "one\ntwo"})
f.lines()
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": "one\nTWO"})
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": "one\nTWO!"})
if deps, diag := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) || len(diag) != 0 {
t.Errorf("dependencies = %q, %q; want #3", depNames(deps), diag)
}
}
// Blame takes the revision as a full commit name, which git cannot read as
// an option, and refuses --end-of-options, so the tool passes none.
func TestBlameInvocation(t *testing.T) {
sha := strings.Repeat("a", 40)
args, err := blameArgs(sha, "-x", []hunk{{2, 3}, {9, 1}})
if want := []string{"blame", "--porcelain", "-w", "-L", "2,+3", "-L", "9,+1", sha, "--", "-x"}; err != nil || !slices.Equal(args, want) {
t.Errorf("blameArgs = %q, %v; want %q", args, err, want)
}
for _, rev := range []string{"HEAD", "--output=x", strings.Repeat("a", 39)} {
if _, err := blameArgs(rev, "a", nil); err == nil {
t.Errorf("blameArgs took %q for a revision", rev)
}
}
}
// What the detector could not read shows with the original whatever became
// of it: picked, waiting for a maintainer, or a deferred conflict, in the
// body and in the report.
func TestDiagnosticsShowOnEveryOutcome(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: images", "", map[string]string{"app/p.bin": "p\x00one", "app/w.bin": "w\x00one", "app/c.bin": "c\x00one", "app/c.txt": "old\n"})
f.lines()
f.onLine("release-1.1", "fix(app): c, on the line", map[string]string{"app/c.txt": "line\n"})
f.pr(5, "fix(app): picked", "", map[string]string{"app/p.bin": "p\x00two"})
f.pr(6, "fix(ci): waiting", "", map[string]string{"app/w.bin": "w\x00two", ".github/workflows/six.yaml": "on: push\n"})
f.pr(7, "fix(app): conflicting", "", map[string]string{"app/c.bin": "c\x00two", "app/c.txt": "seven\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
for _, s := range []struct{ section, file string }{{"[#5]", "p.bin"}, {"### Deferred conflicts", "c.bin"}, {"### Waiting for a maintainer", "w.bin"}} {
_, after, ok := strings.Cut(stdout, s.section)
if want := " - `incomplete evidence: \"app/" + s.file + "\" is binary`"; !ok || !strings.Contains(after, want) {
t.Errorf("the body does not show %q after %s:\n%s", want, s.section, stdout)
}
}
for n, file := range map[int]string{5: "p.bin", 6: "w.bin", 7: "c.bin"} {
if want := fmt.Sprintf(`"%d":["incomplete evidence: \"app/%s\" is binary"]`, n, file); !strings.Contains(stdout, want) {
t.Errorf("the report lacks %s:\n%s", want, stdout)
}
}
}
// BenchmarkAnalyze reads what forty originals build on, over a history
// where half of them build on a feat the line lacks.
func BenchmarkAnalyze(b *testing.B) {
f := newFixture(b)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
for n := 10; n < 50; n++ {
file := fmt.Sprintf("app/f%d.txt", n)
if n%2 == 0 {
f.pr(n, "feat(app): "+file, "", map[string]string{file: appFile})
continue
}
prev := fmt.Sprintf("app/f%d.txt", n-1)
f.pr(n, "fix(app): "+prev, "", map[string]string{prev: strings.Replace(appFile, "two", fmt.Sprint(n), 1), "app/a.txt": strings.Replace(appFile, "three", fmt.Sprint(n), 1)})
}
f.git("fetch", "-q", "origin")
b.ResetTimer()
for range b.N {
bt, err := shapeOf(line11)
if err != nil {
b.Fatal(err)
}
if err := bt.analyze(); err != nil {
b.Fatal(err)
}
}
}
// Files the line lacks count toward the files an original may have read
// for it, like the ones blamed.
func TestDependenciesCapMissingFiles(t *testing.T) {
f := newFixture(t)
f.lines()
added, fixed := map[string]string{}, map[string]string{}
for i := range maxDepFiles + 1 {
added[fmt.Sprintf("app/f%03d.txt", i)] = "v1\n"
fixed[fmt.Sprintf("app/f%03d.txt", i)] = "v2\n"
}
f.pr(3, "feat(app): many files", "", added)
f.pr(5, "fix(app): all of them", "", fixed)
if _, diag := depsOf(t, f, 5); !slices.ContainsFunc(diag, func(d string) bool { return strings.Contains(d, fmt.Sprintf("more than %d files", maxDepFiles)) }) {
t.Errorf("diag = %q, want the file cap", diag)
}
}
+304
View File
@@ -0,0 +1,304 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"cmp"
"fmt"
"maps"
"slices"
"strings"
"time"
)
// A drop is a maintainer's decision that an original stays off one line,
// for a reason, where the rules would take it. It is kept in the line's
// record and carried from batch to batch, bound to the original's merge
// commit on main, so a record cannot point it at another original. It is
// written only to the open batch's record, like everything else there.
// onLine says whether the line carries v, whatever rule decided it.
func (b *batch) onLine(v *verdict) (bool, error) {
if v.rule == ruleOnLine {
return true, nil
}
if err := b.load(v); err != nil {
return false, err
}
how, err := b.lineHist.carries(v.o, v.commits)
return how != "", err
}
// carried says whether the open batch has a unit of v, by the mapping the
// rebuild takes units off with: found needs every commit of a merged
// original, and one whose pick came out empty is missing from it.
func (b *batch) carried(v *verdict) (bool, error) {
units, err := batchUnits(b.prHead, b.lineHead)
if err != nil {
return false, err
}
for _, u := range units {
if o := b.unitOriginal(u); o != nil && o.o.number == v.o.number {
return true, nil
}
}
return false, nil
}
// redrop drops the originals in numbers from the line, for reason, or with
// undrop withdraws their drops. What the open batch carries of a dropped
// original comes off it, as --defer takes it off; otherwise only the record
// changes, and the batch stays as it is even when the line moved since.
func (a *app) redrop(b *batch, numbers []int, reason string, undrop bool, push bool) int {
if !b.src.open {
return a.fail(fmt.Errorf("no open batch pull request on %s carries a record; a drop is recorded on one", b.line.branch()))
}
drops := map[int]droppedEntry{}
for _, d := range b.src.rec.Dropped {
drops[d.Original] = d
}
mode, today := modeDrop, a.now().UTC().Format(time.DateOnly)
if undrop {
mode = modeUndrop
}
for _, n := range numbers {
v := b.find(n)
_, isDropped := drops[n]
switch {
case undrop && !isDropped:
return a.fail(fmt.Errorf("#%d is not dropped from %s", n, b.line.branch()))
case undrop:
delete(drops, n)
case v == nil:
return a.fail(fmt.Errorf("#%d did not merge into main after %s branched", n, b.line.branch()))
default:
// backport/skip and rules 1 to 4 decide before the line is
// looked at, so whether it carries the original is asked here.
on, err := b.onLine(v)
if err != nil {
return a.fail(err)
}
if on {
return a.fail(fmt.Errorf("#%d is already on %s; a drop cannot remove code the line has", n, b.line.branch()))
}
drops[n] = droppedEntry{Original: n, Merge: v.o.merge, Reason: reason, On: b.pr.Number, Date: today}
}
}
active := map[int]droppedEntry{}
for n, d := range drops {
if b.find(n) != nil {
active[n] = d
}
}
b.drops = active
out := map[int]string{}
for _, n := range numbers {
v := b.find(n)
if v == nil {
continue
}
if err := b.judge(v, active); err != nil {
return a.fail(err)
}
if err := b.load(v); err != nil {
return a.fail(err)
}
if err := b.rejudged(v); err != nil {
return a.fail(err)
}
if undrop {
continue
}
on, err := b.carried(v)
if err != nil {
return a.fail(err)
}
if on {
out[n] = "dropped"
}
}
hold := map[int]string{}
for n, e := range b.recorded() {
if e.Reason != "" {
hold[n] = e.Reason
}
}
list := slices.SortedFunc(maps.Values(drops), func(x, y droppedEntry) int { return cmp.Compare(x.Original, y.Original) })
if len(out) > 0 {
maps.Copy(out, hold)
return a.reassemble(b, out, hold, list, mode, push)
}
if err := a.warnReset(b.pr.Head.Ref, b.prHead); err != nil {
return a.fail(err)
}
if _, err := git("switch", "--quiet", "--no-track", "-C", b.pr.Head.Ref, "--end-of-options", b.prHead); err != nil {
return a.fail(err)
}
return a.reclassify(b, b.picks, hold, list, mode, push)
}
// bindDrops holds each drop the record keeps to the original it names: a
// drop of an original merged since the line branched has to name that
// original's merge commit, and one of an earlier original a merge of it on
// main's first-parent line. The first apply; the others are kept and carried
// on, and decide nothing.
func (b *batch) bindDrops(merges []*original) (map[int]droppedEntry, error) {
return bindEntries(b.src.rec.dropped(), merges, b.mainHead, func(d droppedEntry) string {
return fmt.Sprintf("batch pull request #%d drops #%d by %.12s", b.src.carrier.Number, d.Original, d.Merge)
})
}
// waitOrder refuses a record whose waits or parks need an original merged
// after the one that needs it: a dependency is always on main first.
func (b *batch) waitOrder(merges []*original) error {
if b.src.rec == nil {
return nil
}
at := map[int]int{}
for i, o := range merges {
at[o.number] = i
}
check := func(n int, needs []int) error {
i, ok := at[n]
for _, f := range needs {
if j, known := at[f]; ok && known && j >= i {
return fmt.Errorf("batch pull request #%d has #%d wait on #%d, which merged after it", b.src.carrier.Number, n, f)
}
}
return nil
}
for _, d := range b.src.rec.Deferred {
if err := check(d.Original, d.Needs); err != nil {
return err
}
}
for _, p := range b.src.rec.Parked {
if err := check(p.Original, p.Needs); err != nil {
return err
}
}
return nil
}
// bindEntries binds drops to main: one whose original is among merges, the
// originals main merged since the line branched, has to name that
// original's merge exactly; any other has to be a merge of its number on
// main's first-parent line up to mainHead, as an inherited drop is. It
// returns the drops whose originals are among merges; about says what an
// error is about.
func bindEntries(drops []droppedEntry, merges []*original, mainHead string, about func(droppedEntry) string) (map[int]droppedEntry, error) {
byMerge, byNumber := map[string]*original{}, map[int]*original{}
for _, o := range merges {
byMerge[o.merge], byNumber[o.number] = o, o
}
active := map[int]droppedEntry{}
for _, d := range drops {
at := about(d)
if o := byMerge[d.Merge]; o != nil {
if o.number != d.Original {
return nil, fmt.Errorf("%s, which merged #%d", at, o.number)
}
active[d.Original] = d
continue
}
if o := byNumber[d.Original]; o != nil {
return nil, fmt.Errorf("%s, but #%d merged as %.12s", at, d.Original, o.merge)
}
ok, err := mergedOnMain(d.Merge, mainHead, d.Original)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("%s, which is no merge of it on main", at)
}
}
return active, nil
}
// bindDrops binds each drop a report keeps to main exactly as the record's
// drops are bound, over the window the report classified: main and the
// line at the heads it was made from. Its errors read after "report <id>".
func (d *reportDoc) bindDrops() error {
if len(d.Overlays.Dropped) == 0 {
return nil
}
for _, h := range []struct{ name, head string }{{"main", d.Built.Main}, {d.Line, d.Built.Line}} {
have, err := lookup(h.head)
if err != nil {
return err
}
if have == "" {
return fmt.Errorf("was made on %s at %.12s, which this clone does not have; fetch %s and run again", h.name, h.head, h.name)
}
}
out, err := git("merge-base", "--end-of-options", d.Built.Main, d.Built.Line)
if err != nil {
return fmt.Errorf("was made from a line that shares no history with main: %w", err)
}
merges, _, err := mainMerges(strings.TrimSpace(out), d.Built.Main)
if err != nil {
return err
}
active, err := bindEntries(d.Overlays.Dropped, merges, d.Built.Main, func(e droppedEntry) string {
return fmt.Sprintf("drops #%d by %.12s", e.Original, e.Merge)
})
if err != nil {
return err
}
// A drop decides only for an original the report classified, and only
// once every earlier rule has passed it by; an inherited one decides
// nothing.
where, err := d.categories()
if err != nil {
return err
}
for _, n := range d.Primary.Skipped.Dropped {
if _, ok := active[n]; !ok {
return fmt.Errorf("has #%d dropped, but no drop of it binds to an original it classified", n)
}
}
for n := range active {
switch c := where[n]; c {
case "skipped.dropped", "skipped.skip-label", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.on-line":
default:
return fmt.Errorf("drops #%d, which it has %s, though a drop decides before that", n, or(c, "nowhere"))
}
}
return nil
}
// mergedOnMain says whether merge is on main's first-parent line up to
// mainHead and its message names pull request n, as mainMerges reads one.
func mergedOnMain(merge, mainHead string, n int) (bool, error) {
have, err := lookup(merge)
if err != nil || have == "" {
return false, err
}
if ok, err := isAncestor(merge, mainHead); err != nil || !ok {
return false, err
}
line, err := revList("--first-parent", "--end-of-options", mainHead)
if err != nil || !slices.Contains(line, merge) {
return false, err
}
msgs, err := commitMessages([]string{merge})
if err != nil {
return false, err
}
number, _, _ := mergedPR(msgs[merge].message)
return number == n, nil
}
+576
View File
@@ -0,0 +1,576 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strings"
"testing"
)
// recordOf is the record the open batch #90 carries now.
func recordOf(t *testing.T, gh *fakeGH) *lineRecord {
t.Helper()
rec, err := parseRecord(gh.find(90).Body, notifyLine)
if err != nil || rec == nil {
t.Fatalf("#90 carries no readable record: %v", err)
}
return rec
}
func dropped(rec *lineRecord) []int {
var out []int
for _, d := range rec.Dropped {
out = append(out, d.Original)
}
return out
}
// A drop takes a picked original off the batch and keeps it off the line:
// a run that continues the batch does not pick it, and nor does the next
// batch, which carries the drop over from the merged one.
func TestDropTakesAnOriginalOffTheLine(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
mc6 := f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
stdout := mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "6", "--reason", "needs the rework of #4", "--push")
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): five (#5)"}) {
t.Errorf("after the drop the batch carries %q, want #5 alone", got)
}
rec := recordOf(t, gh)
if want := (droppedEntry{Original: 6, Merge: mc6, Reason: "needs the rework of #4", On: 90, Date: "2026-10-26"}); len(rec.Dropped) != 1 || rec.Dropped[0] != want || rec.Built.Options.Mode != modeDrop {
t.Errorf("the record drops %+v (%s), want %+v", rec.Dropped, rec.Built.Options.Mode, want)
}
if s := bodyReportOf(t, stdout); !slices.Equal(s.doc.Primary.Skipped.Dropped, []int{6}) || !slices.Equal(droppedNumbers(s.doc.Overlays.Dropped), []int{6}) {
t.Errorf("the report has %v dropped and %v as the decision", s.doc.Primary.Skipped.Dropped, s.doc.Overlays.Dropped)
}
if !strings.Contains(stdout, "<details><summary>Dropped from `release-1.1` by a maintainer</summary>") {
t.Errorf("the body does not list #6 as dropped:\n%s", stdout)
}
code, _, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stderr, "note: #6 is dropped from release-1.1 on #90 (needs the rework of #4): https://github.com/cozystack/cozystack/pull/6 ") {
t.Errorf("check --cut = %d, want #5 to fail it and a note on #6:\n%s", code, stderr)
}
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): five (#5)", "fix(app): seven (#7)"}) || !slices.Equal(dropped(recordOf(t, gh)), []int{6}) {
t.Errorf("the continued batch carries %q and drops %v", got, dropped(recordOf(t, gh)))
}
mergeBatch(f, gh)
f.now = f.now.AddDate(0, 0, 3)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh.nextPR = 91
stdout = mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
rec, err := parseRecord(stdout, notifyLine)
if got := f.subjects("origin/release-1.1", f.remoteHead("backport-batch/release-1.1-20261029")); err != nil || !slices.Equal(got, []string{"fix(app): eight (#8)"}) || !slices.Equal(dropped(rec), []int{6}) {
t.Errorf("the next batch carries %q and drops %v (%v); want #8 alone, #6 still dropped", got, rec, err)
}
}
// Dropping an original the batch does not carry changes only the record:
// the batch is not rebuilt, so a line that moved since does not move it.
func TestDropIsRecordOnlyWhenTheLineMoved(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only seven", map[string]string{"app/seven.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
if recordOf(t, gh).deferred()[7].Commit == "" {
t.Fatal("#7 is not a recorded conflict")
}
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "7", "--reason", "needs #4", "--push")
rec := recordOf(t, gh)
if f.remoteHead(batchBranch1) != head || !slices.Equal(dropped(rec), []int{7}) || len(rec.deferred()) != 0 || len(gh.edited) != 1 {
t.Errorf("the batch moved to %.12s (from %.12s), the record drops %v and defers %v; want only the record changed", f.remoteHead(batchBranch1), head, dropped(rec), rec.deferred())
}
}
// A drop is written to the open batch's record, and to no other: with no
// batch open, or none ever, it is refused and nothing is written.
func TestDropNeedsAnOpenBatch(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only seven", map[string]string{"app/seven.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--drop", "5", "--reason", "r", "--push"); code != 2 || !strings.Contains(stderr, "no open batch pull request on release-1.1 carries a record; a drop is recorded on one") {
t.Errorf("a drop on a line that never had a batch = %d:\n%s", code, stderr)
}
openBatch(t, f, gh)
mergeBatch(f, gh)
written := len(gh.edited) + len(gh.issueComments)
for _, args := range [][]string{{"--drop", "7", "--reason", "r", "--push"}, {"--undrop", "7", "--push"}} {
if code, _, stderr := f.exec(gh, append([]string{"run", "--line", "release-1.1"}, args...)...); code != 2 || !strings.Contains(stderr, "carries a record; a drop is recorded on one") {
t.Errorf("%q after the batch merged = %d:\n%s", args, code, stderr)
}
}
if len(gh.edited)+len(gh.issueComments) != written {
t.Error("a refused drop wrote to GitHub")
}
}
// A drop is a decision kept beside whatever else decides an original: the
// rule, label or carry that applies first is its category, and the drop
// stays on record. backport/include does not lift it.
func TestDropOverlay(t *testing.T) {
setup := func(t *testing.T) (*fixture, *fakeGH) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "feat(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{labels: map[string][]int{labelInclude: {7}}}
openBatch(t, f, gh)
return f, gh
}
category := func(t *testing.T, gh *fakeGH, n int) string {
t.Helper()
s := bodyReportOf(t, gh.find(90).Body)
where, err := s.doc.categories()
if err != nil {
t.Fatal(err)
}
return where[n]
}
merge := func(f *fixture, n int) string {
mc, _, _ := strings.Cut(f.git("log", "--first-parent", "--format=%H", "--grep", fmt.Sprintf("(#%d)$", n), "main"), "\n")
return mc
}
drop := func(t *testing.T, f *fixture, gh *fakeGH, n string) {
t.Helper()
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", n, "--reason", "not for 1.1", "--push")
}
t.Run("then backport/skip", func(t *testing.T) {
f, gh := setup(t)
drop(t, f, gh, "6")
gh.labels[labelSkip] = []int{6}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if got := category(t, gh, 6); got != "skipped.skip-label" || !slices.Equal(dropped(recordOf(t, gh)), []int{6}) {
t.Errorf("#6 is %q, dropped %v", got, dropped(recordOf(t, gh)))
}
})
t.Run("then picked onto the line by hand", func(t *testing.T) {
f, gh := setup(t)
drop(t, f, gh, "6")
pickOnLine(f, 6, map[string]string{"app/six.txt": "6\n"})
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if got := category(t, gh, 6); got != "skipped.on-line" || !slices.Equal(dropped(recordOf(t, gh)), []int{6}) {
t.Errorf("#6 is %q, dropped %v", got, dropped(recordOf(t, gh)))
}
})
t.Run("a feat backport/include let in", func(t *testing.T) {
f, gh := setup(t)
drop(t, f, gh, "7")
if got := category(t, gh, 7); got != "skipped.dropped" || slices.Contains(f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)), "feat(app): seven (#7)") {
t.Errorf("#7 is %q; want it dropped and off the batch", got)
}
if !strings.Contains(gh.find(90).Body, "`backport/include`, skipped by a maintainer's drop") {
t.Errorf("the overrides do not say what became of #7:\n%s", gh.find(90).Body)
}
})
t.Run("undropped", func(t *testing.T) {
f, gh := setup(t)
drop(t, f, gh, "6")
head := f.remoteHead(batchBranch1)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "6", "--push")
if got := category(t, gh, 6); got != "missing" || len(recordOf(t, gh).Dropped) != 0 || f.remoteHead(batchBranch1) != head || recordOf(t, gh).Built.Options.Mode != modeUndrop {
t.Errorf("#6 is %q, dropped %v; want it missing until the next run, the batch where it was", got, dropped(recordOf(t, gh)))
}
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/6 ") {
t.Errorf("check = %d, want #6 missing:\n%s", code, stdout)
}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if got := category(t, gh, 6); got != "picked" {
t.Errorf("after the next run #6 is %q, want it picked again", got)
}
})
t.Run("a new original during a change to the record alone", func(t *testing.T) {
f, gh := setup(t)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
drop(t, f, gh, "7")
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "7", "--push")
if got := category(t, gh, 8); got != "missing" {
t.Errorf("#8 is %q, want missing until a run tries it", got)
}
})
// A stored drop is bound to the merge it names: one that names another
// original's, or one of no merge of it, is refused; one of an original
// merged before the line branched is kept and decides nothing.
t.Run("a stored drop of an earlier commit that names the original", func(t *testing.T) {
// Before release-1.1 branched, a commit on main already ended in
// (#6); the drop has to name the merge of #6 the line can take.
f := newFixture(t)
f.release("release-1.0")
f.tag("v1.0.0")
early := f.squash(6, "docs: what six will change", "", map[string]string{"docs/six.md": "6\n"})
f.release("release-1.1")
f.tag("v1.1.0")
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
drop(t, f, gh, "6")
gh.find(90).Body = strings.Replace(gh.find(90).Body, `{"original":6,"merge":"`+merge(f, 6)+`"`, `{"original":6,"merge":"`+early+`"`, 1)
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "but #6 merged as") {
t.Errorf("check = %d, want 2 naming the merge of #6:\n%s", code, stderr)
}
})
t.Run("a stored drop of a commit inside a pull request", func(t *testing.T) {
// A commit of a pull request's own branch can end in its number too,
// but only the merge on main's first-parent line is the original.
f := newFixture(t)
f.release("release-1.0")
f.tag("v1.0.0")
mc3 := f.prCommits(3, "feat(app): three", "", change{files: map[string]string{"app/three.txt": "3\n"}, message: "feat(app): the part of three (#3)"})
inner := f.git("rev-parse", mc3+"^2")
f.release("release-1.1")
f.tag("v1.1.0")
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
drop(t, f, gh, "6")
gh.find(90).Body = strings.Replace(gh.find(90).Body, `{"original":6,"merge":"`+merge(f, 6)+`"`, `{"original":3,"merge":"`+inner+`"`, 1)
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "which is no merge of it on main") {
t.Errorf("check = %d, want 2:\n%s", code, stderr)
}
})
for _, tc := range []struct {
name string
original func(f *fixture) (int, string)
refused bool
}{
{"another original's merge", func(f *fixture) (int, string) { return 6, merge(f, 5) }, true},
{"a commit that merged no pull request", func(f *fixture) (int, string) { return 6, f.git("rev-list", "--max-parents=0", "main") }, true},
{"a merge from before the line branched, of another pull request", func(f *fixture) (int, string) { return 2, merge(f, 1) }, true},
{"a merge from before the line branched", func(f *fixture) (int, string) { return 1, merge(f, 1) }, false},
} {
t.Run("a stored drop of "+tc.name, func(t *testing.T) {
f, gh := setup(t)
drop(t, f, gh, "6")
n, mc := tc.original(f)
gh.find(90).Body = strings.Replace(gh.find(90).Body, `{"original":6,"merge":"`+merge(f, 6)+`"`, fmt.Sprintf(`{"original":%d,"merge":"%s"`, n, mc), 1)
code, _, stderr := f.exec(gh, "check", "--line", "release-1.1")
switch {
case tc.refused && (code != 2 || !strings.Contains(stderr, fmt.Sprintf("drops #%d by %.12s", n, mc))):
t.Errorf("check = %d, want 2 naming the drop:\n%s", code, stderr)
case !tc.refused && code == 2:
t.Errorf("check = %d, want the drop kept:\n%s", code, stderr)
case !tc.refused:
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
if !slices.Equal(dropped(recordOf(t, gh)), []int{1}) || category(t, gh, 6) != "picked" {
t.Errorf("the next run drops %v and puts #6 in %q; want #1's drop carried on and #6 picked", dropped(recordOf(t, gh)), category(t, gh, 6))
}
}
})
}
}
// What a drop cannot do is refused before anything is touched.
func TestDropRefuses(t *testing.T) {
f := newFixture(t)
f.lines()
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{labels: map[string][]int{labelSkip: {7, 9}}}
head := openBatch(t, f, gh)
f.onLine("release-1.1", "fix(app): five\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "5\n"})
// The line carries these too, by hand, while backport/skip and rule 2
// decide them before the line is looked at.
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
mc8 := f.pr(8, "feat(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
f.onLine("release-1.1", "fix(app): seven\n\n(cherry picked from commit "+mc7+")", map[string]string{"app/seven.txt": "7\n"})
f.onLine("release-1.1", "feat(app): eight\n\n(cherry picked from commit "+mc8+")", map[string]string{"app/eight.txt": "8\n"})
// #9 is on the line only through a pick of its own commit, which a later
// line change has since moved away from what main has.
mc9 := f.pr(9, "fix(app): nine", "", map[string]string{"app/nine.txt": "9\n"})
f.onLine("release-1.1", "fix(app): nine\n\n(cherry picked from commit "+f.git("rev-parse", mc9+"^2")+")", map[string]string{"app/nine.txt": "9\n"})
f.onLine("release-1.1", "fix(app): nine, as the line needs it", map[string]string{"app/nine.txt": "nine on release-1.1\n"})
for _, tc := range []struct {
args []string
why string
}{
{[]string{"--drop", "4242", "--reason", "r"}, "#4242 did not merge into main"},
{[]string{"--drop", "5", "--reason", "r"}, "already on release-1.1; a drop cannot remove code the line has"},
{[]string{"--drop", "7", "--reason", "r"}, "#7 is already on release-1.1; a drop cannot remove code the line has"},
{[]string{"--drop", "8", "--reason", "r"}, "#8 is already on release-1.1; a drop cannot remove code the line has"},
{[]string{"--drop", "9", "--reason", "r"}, "#9 is already on release-1.1; a drop cannot remove code the line has"},
{[]string{"--drop", "6"}, "--drop needs a --reason"},
{[]string{"--drop", "6", "--reason", "r", "--defer", "6"}, "give one"},
{[]string{"--drop", "6", "--reason", "r", "--continue"}, "takes no --onto, --continue or --date"},
{[]string{"--drop", "6", "--reason", "r", "--onto", head}, "takes no --onto, --continue or --date"},
{[]string{"--drop", "6", "--reason", "r", "--date", "2026-10-26"}, "takes no --onto, --continue or --date"},
{[]string{"--undrop", "6", "--reason", "r"}, "--undrop takes no --reason"},
{[]string{"--undrop", "6"}, "#6 is not dropped from release-1.1"},
{[]string{"--drop", "6", "--undrop", "6", "--reason", "r"}, "give one"},
{[]string{"--drop", "six", "--reason", "r"}, "not a list of pull request numbers"},
} {
code, _, stderr := f.exec(gh, append(append([]string{"run", "--line", "release-1.1"}, tc.args...), "--push")...)
f.git("switch", "-q", "main")
if code != 2 || !strings.Contains(stderr, tc.why) {
t.Errorf("run %q = %d, want 2 with %q:\n%s", tc.args, code, tc.why, stderr)
}
}
if f.remoteHead(batchBranch1) != head || len(gh.edited) != 0 || len(reportsPosted(gh)) != 1 {
t.Errorf("a refused drop moved the batch to %.12s or wrote %q", f.remoteHead(batchBranch1), gh.edited)
}
}
// A drop or an undrop asked for while a close was left changes nothing: the
// run finishes the close, and no open batch pull request is left to record
// it on.
func TestDropDuringAnUnfinishedClose(t *testing.T) {
deferAll := []string{"run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the app e2e", "--push"}
for _, tc := range []struct {
name string
stop func(f *fixture, gh *fakeGH)
refused bool
args []string
}{
{"a drop after the close failed", func(f *fixture, gh *fakeGH) { gh.fail = map[string]error{"closePull": errString("HTTP 502")} }, false, []string{"--drop", "6", "--reason", "not for 1.1"}},
{"an undrop after the delete failed", func(f *fixture, gh *fakeGH) {
gh.after = map[string]func(){"closePull": func() { f.refusePushes(true) }}
}, true, []string{"--undrop", "6"}},
} {
t.Run(tc.name, func(t *testing.T) {
f, gh, _ := closeWorld(t)
tc.stop(f, gh)
if code, _, stderr := f.exec(gh, deferAll...); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if tc.refused {
f.refusePushes(false)
}
gh.after = nil
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, _, stderr := f.exec(gh, append([]string{"run", "--line", "release-1.1", "--push"}, tc.args...)...)
f.git("switch", "-q", "main")
rec, err := parseRecord(gh.find(90).Body, notifyLine)
if code != 2 || !strings.Contains(stderr, "finished the close of #90 and made no other change") || gh.find(90).State != "closed" || f.remoteHead(batchBranch1) != "" || err != nil || len(rec.Dropped) != 0 {
t.Errorf("run %q = %d, #90 %s, branch %q, record %+v, %v; want 2, the close finished and nothing dropped:\n%s", tc.args, code, gh.find(90).State, f.remoteHead(batchBranch1), rec, err, stderr)
}
})
}
}
// The batch carries an original whose first commit the line already had, so
// its pick came out empty and its unit holds the rest: a drop still takes
// that unit off.
func TestDropTakesOffAUnitWithAnEmptyPick(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): the first part, on its own", map[string]string{"app/already.txt": "first\n"})
f.prCommits(5, "fix(app): a fix in two parts", "",
change{files: map[string]string{"app/already.txt": "first\n"}},
change{files: map[string]string{"app/new.txt": "second\n"}},
)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "5", "--reason", "not for 1.1", "--push")
head := f.remoteHead(batchBranch1)
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): six (#6)"}) || f.git("ls-tree", "--name-only", head, "--", "app/new.txt") != "" {
t.Errorf("after the drop the batch carries %q, want #6 alone and nothing of #5", got)
}
}
// A report keeps each drop the record keeps, with the merge commit it is
// bound to, its reason and the batch pull request that recorded it, so an
// earlier report still says why after the drop is withdrawn.
func TestReportKeepsEachDrop(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
mc6 := f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
stdout := mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "6", "--reason", "needs the rework of #4", "--push")
dropped := bodyReportOf(t, stdout)
want := `"overlays":{"dropped":[{"original":6,"merge":"` + mc6 + `","reason":"needs the rework of #4","on":90,"date":"2026-10-26"}]`
if !strings.Contains(dropped.raw, want) {
t.Errorf("the report does not keep the drop with its merge and reason:\n%s", dropped.raw)
}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "6", "--push")
if raw := bodyReportOf(t, gh.find(90).Body).raw; !strings.Contains(raw, `"overlays":{"dropped":[],`) {
t.Errorf("the report after the undrop still keeps a drop:\n%s", raw)
}
code, out, stderr := f.exec(gh, "report", "--pr", "90", "--id", dropped.doc.ID)
if code != 0 || !strings.Contains(out, "dropped: #6, merged as "+mc6[:12]+", on #90: needs the rework of #4\n") {
t.Errorf("report --id of the earlier report = %d, want it to show the drop:\n%s%s", code, out, stderr)
}
}
// A report's drop is held to the record's rules and bound to main as the
// record's drops are: a reason a run does not write, another original's
// merge, or an earlier commit on main that names the same number is
// refused, however the ID was made to match, in the body and in a trusted
// report comment alike.
func TestReportRefusesAForgedDrop(t *testing.T) {
f := newFixture(t)
f.release("release-1.0")
f.tag("v1.0.0")
early := f.squash(6, "docs: what six will change", "", map[string]string{"docs/six.md": "6\n"})
f.release("release-1.1")
f.tag("v1.1.0")
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
mc6 := f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "6", "--reason", "not for 1.1", "--push")
body, comments := gh.find(90).Body, slices.Clone(gh.issueComments)
s := bodyReportOf(t, body)
for _, tc := range []struct{ name, old, new, why string }{
{"a reason of two lines", `"reason":"not for 1.1"`, `"reason":"not for 1.1\nwarning: forged"`, "which is no reason a run writes"},
{"another original's merge", `"merge":"` + mc6 + `"`, `"merge":"` + mc5 + `"`, "which merged #5"},
{"an earlier commit that names the same number", `"merge":"` + mc6 + `"`, `"merge":"` + early + `"`, "but #6 merged as " + mc6[:12]},
} {
raw := strings.Replace(s.raw, tc.old, tc.new, 1)
if raw == s.raw {
t.Fatalf("the report holds no %s to change:\n%s", tc.old, s.raw)
}
var d reportDoc
if err := decodeStrict(raw, &d); err != nil {
t.Fatalf("the changed report does not decode: %v", err)
}
d.ID = reportID(d)
forged := storedReport{doc: d, raw: reportJSON(d)}
refused := func(t *testing.T, args ...string) {
t.Helper()
code, out, stderr := f.exec(gh, append([]string{"report", "--pr", "90"}, args...)...)
if code != 2 || !strings.Contains(stderr, tc.why) || strings.Contains(out+stderr, "\nwarning: forged") {
t.Errorf("report of a forged drop = %d, want 2 with %q:\n%s%s", code, tc.why, out, stderr)
}
}
t.Run(tc.name+", in the body", func(t *testing.T) {
gh.find(90).Body = strings.Replace(body, reportBlock(s.raw), reportBlock(forged.raw), 1)
defer func() { gh.find(90).Body = body }()
refused(t)
})
t.Run(tc.name+", in a comment", func(t *testing.T) {
text, err := reportComment(forged)
if err != nil {
t.Fatal(err)
}
c := issueComment{ID: 9000, Body: text}
c.User.Login, c.User.Type = "maintainer", "User"
gh.issueComments = append(slices.Clone(comments), c)
defer func() { gh.issueComments = comments }()
refused(t, "--id", d.ID)
})
}
}
// droppedNumbers are the originals a report's drops name.
func droppedNumbers(es []droppedEntry) []int {
var out []int
for _, e := range es {
out = append(out, e.Original)
}
return out
}
// dropWorld has #3 merged before release-1.1 branched, an earlier commit on
// main that names #6, and a batch with #5 and #6 from which #6 is dropped.
func dropWorld(t *testing.T) (f *fixture, gh *fakeGH, inherited, mc5, mc6 string) {
t.Helper()
f = newFixture(t)
f.release("release-1.0")
f.tag("v1.0.0")
inherited = f.prCommits(3, "feat(app): three", "", change{files: map[string]string{"app/three.txt": "3\n"}, message: "feat(app): part of three (#3)"})
f.squash(6, "docs: what six will change", "", map[string]string{"docs/six.md": "6\n"})
f.release("release-1.1")
f.tag("v1.1.0")
mc5 = f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
mc6 = f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh = &fakeGH{}
openBatch(t, f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "6", "--reason", "not for 1.1", "--push")
return f, gh, inherited, mc5, mc6
}
// A report's drops and its categories hold each other up: an original it
// has dropped needs a drop that binds to it, and a drop of an original it
// classified needs that original dropped or decided by an earlier rule. A
// drop inherited from before the line branched decides nothing and stays
// readable. Each holds in the body and in a trusted report comment.
func TestReportTiesDropsToTheirCategory(t *testing.T) {
f, gh, inherited, mc5, _ := dropWorld(t)
body, comments := gh.find(90).Body, slices.Clone(gh.issueComments)
s := bodyReportOf(t, body)
kept := func(n int, merge string) droppedEntry {
return droppedEntry{Original: n, Merge: merge, Reason: "carried from an earlier batch", On: 89, Date: "2026-10-25"}
}
for _, tc := range []struct {
name string
change func(d *reportDoc)
why string
}{
{"an inherited drop in place of the classified one", func(d *reportDoc) { d.Overlays.Dropped[0] = kept(3, inherited) }, "has #6 dropped, but keeps no drop of it"},
{"an inherited drop classified as dropped", func(d *reportDoc) {
d.Primary.Skipped.Dropped = append(d.Primary.Skipped.Dropped, 3)
d.Overlays.Dropped = append(d.Overlays.Dropped, kept(3, inherited))
}, "has #3 dropped, but no drop of it binds to an original it classified"},
{"a drop of an original it picked", func(d *reportDoc) { d.Overlays.Dropped = append(d.Overlays.Dropped, kept(5, mc5)) }, "drops #5, which it has picked"},
{"an inherited drop beside the classified one", func(d *reportDoc) { d.Overlays.Dropped = append(d.Overlays.Dropped, kept(3, inherited)) }, ""},
} {
var d reportDoc
if err := decodeStrict(s.raw, &d); err != nil {
t.Fatal(err)
}
tc.change(&d)
d.canonical()
d.ID = reportID(d)
forged := storedReport{doc: d, raw: reportJSON(d)}
check := func(t *testing.T, args ...string) {
t.Helper()
code, out, stderr := f.exec(gh, append([]string{"report", "--pr", "90"}, args...)...)
switch {
case tc.why == "" && (code != 0 || !strings.Contains(out, "#6 skipped.dropped") || !strings.Contains(out, "dropped: #3, merged as "+inherited[:12]) || strings.Contains(out, "#3 skipped.dropped")):
t.Errorf("report of a sound report = %d, want it read with #3's drop beside #6's:\n%s%s", code, out, stderr)
case tc.why != "" && (code != 2 || out != "" || !strings.Contains(stderr, tc.why)):
t.Errorf("report = %d, want 2 with %q:\n%s%s", code, tc.why, out, stderr)
}
}
t.Run(tc.name+", in the body", func(t *testing.T) {
gh.find(90).Body = strings.Replace(body, reportBlock(s.raw), reportBlock(forged.raw), 1)
defer func() { gh.find(90).Body = body }()
check(t)
})
t.Run(tc.name+", in a comment", func(t *testing.T) {
text, err := reportComment(forged)
if err != nil {
t.Fatal(err)
}
c := issueComment{ID: 9001, Body: text}
c.User.Login, c.User.Type = "maintainer", "User"
gh.issueComments = append(slices.Clone(comments), c)
defer func() { gh.issueComments = comments }()
check(t, "--id", d.ID)
})
}
}
+625
View File
@@ -0,0 +1,625 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
)
// A clean pick can still carry only part of an original: git deletes a file
// the line never had without a word, and leaves alone a file the line lacks.
// What follows tells such a unit from a whole one, by what it leaves without
// effect of the files the original modifies or deletes.
// treeEntry is one record of git ls-tree.
type treeEntry struct{ mode, kind, oid string }
// regular says whether git can merge the entry as text: a file, not a
// symlink, a submodule or a tree.
func (e treeEntry) regular() bool {
return e.kind == "blob" && (e.mode == "100644" || e.mode == "100755")
}
// treeEntries reads the entries paths have at treeish, from full ls-tree
// records: a path that is a tree there, or absent, has none.
func treeEntries(treeish string, paths []string) (map[string]treeEntry, error) {
out := map[string]treeEntry{}
want := map[string]bool{}
for _, p := range paths {
want[p] = true
}
const chunk = 500
for len(paths) > 0 {
n := min(chunk, len(paths))
args := append([]string{"--literal-pathspecs", "ls-tree", "-r", "-z", "--full-tree", "--end-of-options", treeish, "--"}, paths[:n]...)
raw, stderr, code, err := gitStatus(args...)
switch {
case err != nil:
return nil, fmt.Errorf("git ls-tree %s did not run", terminalSafe(treeish))
case code != 0:
// The paths stay out of the error: they are file names someone
// chose, and the error reaches a terminal.
return nil, fmt.Errorf("git ls-tree %s: exit %d: %s", terminalSafe(treeish), code, terminalSafe(stderr))
}
for _, rec := range splitNUL(raw) {
meta, path, ok := strings.Cut(rec, "\t")
f := strings.Fields(meta)
if !ok || len(f) != 3 || checkSHA(f[2]) != nil {
return nil, fmt.Errorf("git ls-tree %s: unreadable record %q", treeish, terminalSafe(rec))
}
if want[path] {
out[path] = treeEntry{mode: f[0], kind: f[1], oid: f[2]}
}
}
paths = paths[n:]
}
return out, nil
}
// renameSet is what git diff -M found renamed between two commits, old path
// to new, and whether it looked at every candidate.
type renameSet struct {
moved map[string]string
complete bool
}
// renames reads the renames between from and to, once per pair. git says on
// stderr when it skipped inexact detection for too many files; complete is
// then false, and no path may be called absent for want of a rename.
func (b *batch) renames(from, to string) (renameSet, error) {
key := from + ".." + to
if s, ok := b.renameCache[key]; ok {
return s, nil
}
// -l0 lifts git's own limit, which the picker's merge does not share:
// a rename the pick follows has to be one the checks of its shape see.
stdout, stderr, code, err := gitStatusTimed("diff", "--no-ext-diff", "--no-textconv", "-M", "-l"+strconv.Itoa(b.renameLimit), "--name-status", "-z", "--diff-filter=R", "--end-of-options", from, to)
switch {
case errors.Is(err, errDeadline):
// Out of time is rename detection skipped: no path is called
// absent for want of it.
s := renameSet{moved: map[string]string{}}
if b.renameCache == nil {
b.renameCache = map[string]renameSet{}
}
b.renameCache[key] = s
return s, nil
case err != nil:
return renameSet{}, err
case code != 0:
return renameSet{}, fmt.Errorf("git diff -M %s %s: exit %d: %s", from, to, code, strings.TrimSpace(stderr))
}
f := splitNUL(stdout)
if len(f)%3 != 0 {
return renameSet{}, fmt.Errorf("unreadable renames between %s and %s", from, to)
}
s := renameSet{moved: map[string]string{}, complete: !strings.Contains(stderr, "detection was skipped")}
for i := 0; i < len(f); i += 3 {
if !strings.HasPrefix(f[i], "R") {
return renameSet{}, fmt.Errorf("unreadable renames between %s and %s: %q", from, to, terminalSafe(f[i]))
}
s.moved[f[i+1]] = f[i+2]
}
if b.renameCache == nil {
b.renameCache = map[string]renameSet{}
}
b.renameCache[key] = s
return s, nil
}
// effectPaths are the paths v modifies or deletes; what it adds has nothing
// to correspond to.
func effectPaths(v *verdict) []string {
var out []string
for _, c := range v.changes {
if c.status == 'M' || c.status == 'D' {
out = append(out, c.path)
}
}
return out
}
// correspondence maps a path of the original (at M^1) to the path holding the
// same file on target, through a main-side rename base..M^1, a line-side
// rename base..target, or both composed through the base path. A path with
// no counterpart maps to itself, and one git skipped rename detection for is
// left out and named in unknown: it is not known to be absent.
func (b *batch) correspondence(v *verdict, target string) (map[string]string, []string, error) {
paths := effectPaths(v)
have, err := treeEntries(target, paths)
if err != nil {
return nil, nil, err
}
corr := map[string]string{}
var missing []string
for _, p := range paths {
if _, ok := have[p]; ok {
corr[p] = p
} else {
missing = append(missing, p)
}
}
if len(missing) == 0 {
return corr, nil, nil
}
onMain, err := b.renames(b.base, v.o.merge+"^1")
if err != nil {
return nil, nil, err
}
onLine, err := b.renames(b.base, target)
if err != nil {
return nil, nil, err
}
fromBase := map[string]string{}
for old, now := range onMain.moved {
fromBase[now] = old
}
candidates := map[string]string{}
var names []string
for _, p := range missing {
q := p
if old, ok := fromBase[p]; ok {
q = old
}
r := q
if now, ok := onLine.moved[q]; ok {
r = now
}
if r != p {
candidates[p] = r
names = append(names, r)
}
}
found, err := treeEntries(target, names)
if err != nil {
return nil, nil, err
}
var unknown []string
for _, p := range missing {
if r, ok := candidates[p]; ok {
if _, ok := found[r]; ok {
corr[p] = r
continue
}
}
if !onMain.complete || !onLine.complete {
unknown = append(unknown, p)
continue
}
corr[p] = p
}
return corr, unknown, nil
}
// omittedEffects lists the paths v modifies or deletes that the change from
// before to after leaves without effect, each judged at its corresponding
// path c(p) on before. A modified path is omitted when c(p) exists on neither
// side, applied when the change touches it, and when the change leaves an
// existing file alone, satisfied only if the line already has what the
// original makes of it. A deleted path is omitted when c(p) is absent before:
// a deletion the line already did is no change this unit makes. diag says
// what could not be judged, which is never counted as omitted.
func (b *batch) omittedEffects(v *verdict, before, after string) ([]string, []string, error) {
if err := b.load(v); err != nil {
return nil, nil, err
}
corr, unknown, err := b.correspondence(v, before)
if err != nil {
return nil, nil, err
}
var diag []string
for _, p := range unknown {
diag = append(diag, "git skipped rename detection, so where "+strconv.Quote(p)+" is on the line is not known")
}
added := map[string]bool{}
for _, c := range v.changes {
if c.status == 'A' {
added[c.path] = true
}
}
// A deleted path whose counterpart the original adds itself is a rename
// the line already made the same way: it is judged as a modified file,
// against what the original makes of that path.
var at, own, moved []string
movedTo := map[string]string{}
for _, c := range v.changes {
cp, ok := corr[c.path]
if !ok {
continue
}
at = append(at, cp)
switch {
case c.status == 'M':
own = append(own, c.path)
case c.status == 'D' && cp != c.path && added[cp]:
own, moved = append(own, c.path), append(moved, cp)
movedTo[c.path] = cp
}
}
bef, err := treeEntries(before, at)
if err != nil {
return nil, nil, err
}
aft, err := treeEntries(after, at)
if err != nil {
return nil, nil, err
}
pre, err := treeEntries(v.o.merge+"^1", own)
if err != nil {
return nil, nil, err
}
post, err := treeEntries(v.o.merge, append(slices.Clone(own), moved...))
if err != nil {
return nil, nil, err
}
// A change that makes none is judged by its trees, whatever names them:
// a commit and the tree a net merge wrote can be the same change.
same := before == after
if !same {
bt, err := resolveTree(before)
if err != nil {
return nil, nil, err
}
at, err := resolveTree(after)
if err != nil {
return nil, nil, err
}
same = bt == at
}
var omitted []string
// modified judges a path whose counterpart cp should end up as the
// original's postimage p1 of it.
modified := func(path, cp string, p0, p1 treeEntry) error {
be, inBefore := bef[cp]
ae, inAfter := aft[cp]
switch {
case !inBefore && !inAfter:
omitted = append(omitted, path)
return nil
case inBefore != inAfter || be != ae:
return nil
case be.oid == p1.oid && (p0.mode == p1.mode || be.mode == p1.mode):
return nil
case !be.regular() || !p0.regular() || !p1.regular() || p0.mode != p1.mode:
diag = append(diag, "the mode or type of "+strconv.Quote(path)+" changes, which only a person can judge")
return nil
}
applied, unsupported, err := b.alreadyApplied(p0.oid, be.oid, p1.oid)
switch {
case err != nil:
return fmt.Errorf("judging %s of #%d: %w", strconv.Quote(path), v.o.number, err)
case unsupported:
diag = append(diag, "git cannot merge "+strconv.Quote(path)+" as text, so whether the line has its change is not known")
case !applied:
omitted = append(omitted, path)
}
return nil
}
for _, c := range v.changes {
cp, ok := corr[c.path]
if !ok {
continue
}
switch {
case c.status == 'D' && movedTo[c.path] != "":
if err := modified(c.path, cp, pre[c.path], post[cp]); err != nil {
return nil, nil, err
}
case c.status == 'D':
// Absent before: the line already made the deletion, which is
// no change of a unit that makes others, and is all of one
// that makes none. Still there after: the deletion did not
// happen, whatever the unit changed in the file; keeping what
// the line made of it is a port of its own.
_, inBefore := bef[cp]
_, inAfter := aft[cp]
if !inBefore && !same || inAfter {
omitted = append(omitted, c.path)
}
case c.status == 'M':
if err := modified(c.path, cp, pre[c.path], post[c.path]); err != nil {
return nil, nil, err
}
}
}
for i := range diag {
diag[i] = oneLineText(diag[i])
}
return omitted, diag, nil
}
// alreadyApplied says whether cur, a file the unit leaves alone, already
// holds the original's change from pre to post: it is post itself, or git
// merge-file of the three merges cleanly back into cur. unsupported is
// content merge-file refuses, such as differing binary files, which says
// nothing either way.
func (b *batch) alreadyApplied(pre, cur, post string) (applied, unsupported bool, err error) {
if cur == post {
return true, false, nil
}
b.mergeFiles++
blobs, err := readObjects([]string{pre, cur, post}, "blob")
if err != nil {
return false, false, err
}
dir, err := os.MkdirTemp("", "backport-batch-merge-file-*")
if err != nil {
return false, false, err
}
defer func() { _ = os.RemoveAll(dir) }()
names := map[string]string{}
for _, f := range []struct{ name, oid string }{{"cur", cur}, {"pre", pre}, {"post", post}} {
names[f.name] = filepath.Join(dir, f.name)
if err := os.WriteFile(names[f.name], blobs[f.oid], 0o600); err != nil {
return false, false, err
}
}
stdout, stderr, code, err := gitStatus("merge-file", "-p", names["cur"], names["pre"], names["post"])
switch {
case err != nil:
return false, false, err
case code == 0:
return stdout == string(blobs[cur]), false, nil
case code > 0 && code < 128:
// The number of conflicts.
return false, false, nil
case strings.Contains(stderr, "Cannot merge binary files"):
return false, true, nil
}
return false, false, fmt.Errorf("git merge-file: exit %d: %s", code, strings.TrimSpace(stderr))
}
// unitsOn maps each original the batch head carries a unit of to that unit,
// by its Backported-from trailer or appended -x line: found needs every
// commit of a merged original, and misses one whose first pick came out
// empty because the line already had that change.
func (b *batch) unitsOn(head string) (map[*verdict]string, error) {
if m, ok := b.unitCache[head]; ok {
return m, nil
}
units, err := batchUnits(head, b.lineHead)
if err != nil {
return nil, err
}
m := map[*verdict]string{}
for _, u := range units {
if v := b.unitOriginal(u); v != nil && m[v] == "" {
m[v] = u.commit
}
}
if b.unitCache == nil {
b.unitCache = map[string]map[*verdict]string{}
}
b.unitCache[head] = m
return m, nil
}
// onBranch says whether the batch head h carries v.
func (b *batch) onBranch(h *history, v *verdict) (bool, error) {
if found(h, v) {
return true, nil
}
m, err := b.unitsOn(h.head)
return m[v] != "", err
}
// partialStates finds what applies only in part on the line. Every unit on
// the open batch is checked against its own first parent, whatever the rules
// now say of its original: one that leaves an effect out fails the cut until
// it is taken out. Every original the policy takes and nothing carries is
// checked against the tree its clean net merge onto the line writes, so a
// partial one is found with no record and no adder; a conflict is never
// called partial.
func (b *batch) partialStates() error {
defer b.timed(time.Now())
b.partialUnits = map[*verdict][]string{}
if b.pr != nil {
var err error
if b.partialUnits, err = b.unitsPartial(b.prHead); err != nil {
return err
}
on, err := b.unitsOn(b.prHead)
if err != nil {
return err
}
for v, commit := range on {
v.batched = commit
if v.rule == 0 && v.onBatch == "" {
v.onBatch = commit
}
}
}
for _, v := range b.qualifying() {
if v.onBatch != "" {
continue
}
if err := b.partialAt(v, b.lineHead); err != nil {
return err
}
}
return nil
}
// unitsPartial maps each original whose unit between the line's head and
// head leaves part of it without effect to what it leaves.
func (b *batch) unitsPartial(head string) (map[*verdict][]string, error) {
units, err := batchUnits(head, b.lineHead)
if err != nil {
return nil, err
}
out := map[*verdict][]string{}
for _, u := range units {
v := b.unitOriginal(u)
if v == nil || len(u.parents) == 0 {
continue
}
omitted, diag, err := b.omittedEffects(v, u.parents[0], u.commit)
if err != nil {
return nil, err
}
v.addDiag(diag...)
if len(omitted) > 0 {
out[v] = append(out[v], omitted...)
}
}
return out, nil
}
// markPartial puts every original on the branch at head whose unit there
// does only part of it among the partial units, and so among the stale
// ones: still on the batch, and to be taken out before the cut.
func (b *batch) markPartial(r *report, head string) error {
units := b.partialUnits
if b.pr == nil || head != b.prHead {
var err error
if units, err = b.unitsPartial(head); err != nil {
return err
}
}
r.partialFiles = map[*verdict][]string{}
for _, v := range b.verdicts {
files := units[v]
if len(files) == 0 {
continue
}
r.partial = append(r.partial, v)
r.partialFiles[v] = files
if !slices.Contains(r.stale, v) {
r.stale = append(r.stale, v)
}
}
return nil
}
// keepRecordedPartials judges again, at head, what the batch carries now,
// each original the open record parks as partial that the line's head
// judges whole: the run's picker found it partial after the picks before
// it, which check and an operation that picks nothing do not replay. It
// stays parked while its net merge onto head still leaves something
// without effect, and is the line's again once that went, as when the pick
// that made it partial was taken off.
func (b *batch) keepRecordedPartials(head string) error {
if !b.src.open || b.src.rec == nil {
return nil
}
for _, p := range b.src.rec.Parked {
v := b.find(p.Original)
if p.Kind != parkPartial || v == nil || v.rule != 0 || v.onBatch != "" || v.park != nil || v.o.merge != p.Merge {
continue
}
if err := b.partialAt(v, head); err != nil {
return err
}
}
return nil
}
// rejudged reads again what partialStates and analyze read for v once its
// rules changed, as a withdrawn drop changes them: it is back where the
// line puts it.
func (b *batch) rejudged(v *verdict) error {
if b.pr != nil {
on, err := b.unitsOn(b.prHead)
if err != nil {
return err
}
v.batched = on[v]
if v.rule == 0 && v.onBatch == "" {
v.onBatch = on[v]
}
}
if v.rule != 0 || v.onBatch != "" {
return nil
}
if err := b.partialAt(v, b.lineHead); err != nil {
return err
}
deps, diag, err := b.dependencies(v, b.lineHead)
if err != nil {
return err
}
v.deps = deps
v.addDiag(diag...)
return nil
}
// partialAt judges v's clean net merge onto head, and parks it, or clears
// the park, by what that merge leaves without effect.
func (b *batch) partialAt(v *verdict, head string) error {
tree, err := b.netTree(head, v.o)
if err != nil {
return err
}
v.partial = nil
if v.park != nil && v.park.Kind == parkPartial {
v.park = nil
}
if tree == "" {
return nil
}
omitted, diag, err := b.omittedEffects(v, head, tree)
if err != nil {
return err
}
v.addDiag(diag...)
if len(omitted) > 0 {
v.partial = omitted
v.park = &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkPartial, Files: sortedClean(omitted)}
}
return nil
}
// netTree is the tree a clean net merge of o onto head writes, "" for a
// conflict; the line's head reuses what judging the originals merged.
func (b *batch) netTree(head string, o *original) (string, error) {
if head == b.lineHead && b.lineHist != nil {
return b.lineHist.netTree(o)
}
tree, _, err := noopPick(head, o)
return tree, err
}
// addDiag keeps what could not be judged of v, each line once.
func (v *verdict) addDiag(lines ...string) {
for _, ln := range lines {
if ln != "" && !slices.Contains(v.diag, ln) {
v.diag = append(v.diag, ln)
}
}
}
func sortedClean(files []string) []string {
out := cleanFiles(files)
slices.Sort(out)
return slices.Compact(out)
}
// leftOut is the refusal for a unit that leaves paths of v without effect.
func leftOut(v *verdict, l line, omitted []string) string {
quoted := make([]string, len(omitted))
for i, p := range omitted {
quoted[i] = strconv.Quote(p)
}
return fmt.Sprintf("#%d leaves %s without effect on %s; port only what the line has as a pull request of its own", v.o.number, strings.Join(quoted, ", "), l.branch())
}
+363
View File
@@ -0,0 +1,363 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
const appFile = "one\ntwo\nthree\nfour\nfive\n"
// renameWorld is the sign-off's rename history: the line keeps app/old.txt,
// main renames it to app/new.txt with #3, a feat, and #5 fixes app/new.txt.
func renameWorld(t *testing.T) (*fixture, string) {
t.Helper()
f := newFixture(t)
f.squash(2, "chore: an app file", "", map[string]string{"app/old.txt": appFile})
f.lines()
f.pr(3, "feat(app): rename the app file", "", map[string]string{"app/old.txt": "", "app/new.txt": appFile})
mc5 := f.pr(5, "fix(app): fix the app file", "", map[string]string{"app/new.txt": strings.Replace(appFile, "three", "THREE", 1)})
return f, mc5
}
// emptyBatch pushes the line's head as the batch branch and opens a batch
// pull request on it with body, for a fold to land on.
func emptyBatch(f *fixture, gh *fakeGH, body string) *foldWorld {
f.t.Helper()
f.git("fetch", "-q", "origin")
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
f.git("fetch", "-q", "origin")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, body)}
return &foldWorld{f: f, gh: gh, head: f.git("rev-parse", "origin/"+batchBranch1)}
}
// A fix of a file main renamed picks onto the file's old name on the line,
// and every check of the batch's shape takes the old name as the same file:
// verify, a hand fold, and a rebuild by --defer.
func TestRenamePickThroughFoldAndVerify(t *testing.T) {
f, _ := renameWorld(t)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): fix the app file (#5)", "fix(app): six (#6)"}) {
t.Fatalf("the batch carries %q, want #5 and #6", got)
}
if got := f.git("show", head+":app/old.txt"); !strings.Contains(got, "THREE") {
t.Fatalf("#5 did not land on app/old.txt: %q", got)
}
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
}
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e", "--push"); code != 0 {
t.Fatalf("run --defer 6 --push = %d\n%s", code, stderr)
}
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", f.remoteHead(batchBranch1)); code != 0 {
t.Errorf("verify after the rebuild = %d\n%s\n%s", code, out, stderr)
}
f, mc5 := renameWorld(t)
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/old.txt": strings.Replace(appFile, "three", "THREE", 1)})
fold := w.merge(w.head, pick, "fix(app): fix the app file (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 || out != "ok\n" {
t.Errorf("fold of #5 onto app/old.txt = %d, %q\n%s", code, out, stderr)
}
}
// shaped reads the line from the fixture's remote as verify does.
func shaped(t *testing.T, f *fixture) *batch {
t.Helper()
f.git("fetch", "-q", "origin")
b, err := shapeOf(line11)
if err != nil {
t.Fatal(err)
}
return b
}
// commitOn writes files onto parent and returns the commit, leaving every
// branch where it was.
func commitOn(f *fixture, parent string, files map[string]string) string {
f.t.Helper()
f.git("switch", "-q", "--detach", parent)
f.apply(files)
f.git("commit", "-q", "--allow-empty", "-m", "a unit")
sha := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
return sha
}
// A path of the original is followed to where the line keeps the same file:
// through a rename on main, on the line, or both, composed through the path
// at the base. Where git skipped rename detection the path is unknown, never
// absent.
func TestCorrespondence(t *testing.T) {
corr := func(t *testing.T, f *fixture, n int, limit ...int) (map[string]string, []string) {
t.Helper()
b := shaped(t, f)
if len(limit) > 0 {
b.renameLimit = limit[0]
}
v := b.find(n)
if err := b.load(v); err != nil {
t.Fatal(err)
}
c, unknown, err := b.correspondence(v, b.lineHead)
if err != nil {
t.Fatal(err)
}
return c, unknown
}
t.Run("renamed on main", func(t *testing.T) {
f, _ := renameWorld(t)
if c, unknown := corr(t, f, 5); c["app/new.txt"] != "app/old.txt" || len(unknown) != 0 {
t.Errorf("correspondence = %v, %v; want app/new.txt on app/old.txt", c, unknown)
}
})
t.Run("renamed on the line", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: an app file", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.onLine("release-1.1", "fix(app): the line renames it", map[string]string{"app/a.txt": "", "app/line.txt": appFile})
f.pr(5, "fix(app): fix it", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
if c, unknown := corr(t, f, 5); c["app/a.txt"] != "app/line.txt" || len(unknown) != 0 {
t.Errorf("correspondence = %v, %v; want app/a.txt on app/line.txt", c, unknown)
}
})
t.Run("renamed on both", func(t *testing.T) {
f, _ := renameWorld(t)
f.onLine("release-1.1", "fix(app): the line renames it its own way", map[string]string{"app/old.txt": "", "app/release.txt": appFile})
if c, unknown := corr(t, f, 5); c["app/new.txt"] != "app/release.txt" || len(unknown) != 0 {
t.Errorf("correspondence = %v, %v; want app/new.txt on app/release.txt", c, unknown)
}
})
t.Run("not renamed, absent", func(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
f.pr(5, "fix(app): fix x", "", map[string]string{"app/x.txt": strings.Replace(appFile, "two", "TWO", 1)})
if c, unknown := corr(t, f, 5); c["app/x.txt"] != "app/x.txt" || len(unknown) != 0 {
t.Errorf("correspondence = %v, %v; want app/x.txt on itself", c, unknown)
}
})
t.Run("rename detection skipped", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: two app files", "", map[string]string{"app/a.txt": appFile, "app/b.txt": appFile + "six\n"})
f.lines()
f.onLine("release-1.1", "fix(app): the line renames both, changed", map[string]string{"app/a.txt": "", "app/b.txt": "", "app/x.txt": appFile + "x\n", "app/y.txt": appFile + "six\ny\n"})
f.pr(5, "fix(app): fix a", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
// Past the limit, git says it skipped looking; a configured limit
// of its own does not decide for the tool.
f.git("config", "diff.renameLimit", "1")
if c, unknown := corr(t, f, 5, 0); c["app/a.txt"] != "app/x.txt" || len(unknown) != 0 {
t.Errorf("correspondence under diff.renameLimit=1 = %v, %v; want app/a.txt on app/x.txt", c, unknown)
}
if c, unknown := corr(t, f, 5, 1); !slices.Equal(unknown, []string{"app/a.txt"}) || c["app/a.txt"] != "" {
t.Errorf("correspondence past the limit = %v, %v; want app/a.txt unknown", c, unknown)
}
})
}
// What a change leaves without effect: a modified file the line lacks on both
// sides of it, a deleted file the line never had. A mode change that only a
// person can judge is a diagnostic, not an omission.
func TestOmittedEffects(t *testing.T) {
omitted := func(t *testing.T, f *fixture, n int, files map[string]string) ([]string, []string) {
t.Helper()
b := shaped(t, f)
after := commitOn(f, b.lineHead, files)
om, diag, err := b.omittedEffects(b.find(n), b.lineHead, after)
if err != nil {
t.Fatal(err)
}
return om, diag
}
t.Run("a modified file missing before and after", func(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
f.pr(5, "fix(app): fix x and five", "", map[string]string{"app/x.txt": "fixed\n", "app/five.txt": "5\n"})
if om, diag := omitted(t, f, 5, map[string]string{"app/five.txt": "5\n"}); !slices.Equal(om, []string{"app/x.txt"}) || len(diag) != 0 {
t.Errorf("omitted = %q, %q; want app/x.txt", om, diag)
}
})
t.Run("a deleted file absent before", func(t *testing.T) {
f, _ := policyWorld(t)
if om, _ := omitted(t, f, 5, map[string]string{"chart/tests/values_test.yaml": "suite: values\n"}); !slices.Equal(om, []string{"chart/templates/policy.yaml", "chart/tests/policy_test.yaml"}) {
t.Errorf("omitted = %q; want both policy files", om)
}
})
t.Run("a mode change", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a script", "", map[string]string{"app/run.sh": "#!/bin/sh\n", "app/b.txt": "old\n"})
f.lines()
f.git("switch", "-q", "-c", "pr-5")
if err := os.Chmod(filepath.Join(f.dir, "app/run.sh"), 0o755); err != nil {
t.Fatal(err)
}
f.write("app/b.txt", "fixed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "run it")
f.git("switch", "-q", "main")
f.git("merge", "-q", "--no-ff", "-m", "fix(app): make the script runnable (#5)", "pr-5")
f.git("push", "-q", "origin", "main")
om, diag := omitted(t, f, 5, map[string]string{"app/b.txt": "fixed\n"})
if len(om) != 0 || len(diag) != 1 || !strings.Contains(diag[0], `the mode or type of "app/run.sh" changes`) {
t.Errorf("omitted = %q, %q; want a diagnostic for app/run.sh and nothing omitted", om, diag)
}
})
}
// What git says when it fails names file names as text, never as lines a
// terminal or a CI log would take for its own.
func TestGitErrorsQuoteFileNames(t *testing.T) {
newFixture(t)
const evil = "x\n::warning::forged"
if _, err := gitTimed("log", "--no-such-option", "--", evil); err == nil || strings.Contains(err.Error(), "\n::warning") {
t.Errorf("gitTimed error = %v, want the path quoted", err)
}
if _, err := treeEntries("no-such-revision", []string{evil}); err == nil || strings.Contains(err.Error(), "\n::warning") {
t.Errorf("treeEntries error = %v, want the path quoted", err)
}
}
// fixedWorld is the sign-off's two-file case: the base has a and b as old,
// #5 fixes both, and the line already has a as onLine says, if anything.
func fixedWorld(t *testing.T, a, old, onLine, fixed string) (*fixture, string) {
t.Helper()
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{a: old, "app/b.txt": "old\n"})
f.lines()
if onLine != "" {
f.onLine("release-1.1", "fix(app): a, on the line", map[string]string{a: onLine})
}
mc5 := f.pr(5, "fix(app): a and b", "", map[string]string{a: fixed, "app/b.txt": "fixed\n"})
return f, mc5
}
// The line can already have what an original does to a file: then a pick
// that leaves the file alone carries the original whole. The check-time
// pass, the picker, fold and verify all take it, binary files included,
// without asking git merge-file what blob equality settles. A different fix
// on the line, or a deletion of what the line never had, is still left out.
func TestAlreadyAppliedFileIsNotOmitted(t *testing.T) {
const textPost = "one\nfixed\nthree\nfour\nfive\n"
accepted := func(t *testing.T, a, old, onLine, fixed string) {
t.Helper()
f, _ := fixedWorld(t, a, old, onLine, fixed)
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
t.Errorf("check = %d, want #5 missing and not parked:\n%s\n%s", code, stdout, stderr)
}
gh := &fakeGH{}
head := openBatch(t, f, gh)
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a and b (#5)"}) {
t.Errorf("picked %q, want #5", got)
}
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || strings.Contains(stdout, "only part of it applies") {
t.Errorf("check --cut = %d, want #5 only on the batch, whole:\n%s\n%s", code, stdout, stderr)
}
f, mc5 := fixedWorld(t, a, old, onLine, fixed)
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/b.txt": "fixed\n"})
fold := w.merge(w.head, pick, "fix(app): a and b (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 {
t.Errorf("fold = %d, %q\n%s", code, out, stderr)
}
}
t.Run("the line has the original's file", func(t *testing.T) {
accepted(t, "app/a.txt", "old\n", "fixed\n", "fixed\n")
})
t.Run("the line has the fix beside lines of its own", func(t *testing.T) {
accepted(t, "app/a.txt", strings.Replace(textPost, "fixed", "old", 1), textPost+"line only\n", textPost)
})
t.Run("the line has the original's binary file", func(t *testing.T) {
accepted(t, "app/a.bin", "old\x00payload", "fixed\x00payload", "fixed\x00payload")
f, _ := fixedWorld(t, "app/a.bin", "old\x00payload", "fixed\x00payload", "fixed\x00payload")
b := shaped(t, f)
om, diag, err := b.omittedEffects(b.find(5), b.lineHead, commitOn(f, b.lineHead, map[string]string{"app/b.txt": "fixed\n"}))
if err != nil || len(om)+len(diag) != 0 || b.mergeFiles != 0 {
t.Errorf("omitted = %q, %q, %v, merge-file ran %d times; want nothing, and merge-file never asked", om, diag, err, b.mergeFiles)
}
})
t.Run("an earlier unit of the batch brought the file", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old\n", "app/b.txt": "old\n"})
f.lines()
f.pr(4, "fix(app): a", "", map[string]string{"app/a.txt": "fixed\n"})
f.squash(40, "chore: put a back for now", "", map[string]string{"app/a.txt": "old\n"})
f.pr(5, "fix(app): a and b", "", map[string]string{"app/a.txt": "fixed\n", "app/b.txt": "fixed\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a (#4)", "fix(app): a and b (#5)"}) {
t.Errorf("picked %q, want #4 and #5", got)
}
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
}
})
t.Run("the line has a different fix", func(t *testing.T) {
f, mc5 := fixedWorld(t, "app/a.txt", "old\n", "other\n", "fixed\n")
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/b.txt": "fixed\n"})
fold := w.merge(w.head, pick, "fix(app): a and b (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/a.txt" without effect`) {
t.Errorf("fold = %d, %q, want it refused for app/a.txt\n%s", code, out, stderr)
}
})
t.Run("the line has a different binary file", func(t *testing.T) {
f, _ := fixedWorld(t, "app/a.bin", "old\x00payload", "other\x00payload", "fixed\x00payload")
b := shaped(t, f)
om, diag, err := b.omittedEffects(b.find(5), b.lineHead, commitOn(f, b.lineHead, map[string]string{"app/b.txt": "fixed\n"}))
if err != nil || len(om) != 0 || len(diag) != 1 || !strings.Contains(diag[0], `cannot merge "app/a.bin" as text`) {
t.Errorf("omitted = %q, %q, %v; want a diagnostic for app/a.bin, nothing omitted", om, diag, err)
}
})
t.Run("a deletion of what the line never had", func(t *testing.T) {
f, mc5 := policyWorld(t)
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"chart/tests/values_test.yaml": "suite: values\n"})
fold := w.merge(w.head, pick, "fix(chart): remove the policy a rebase brought back (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, "without effect") {
t.Errorf("fold = %d, %q, want it refused\n%s", code, out, stderr)
}
})
}
// A change that makes none, whatever names its two sides, leaves a deletion
// the line already made as it is: the original is there, not partial.
func TestDeletionAlreadyMadeByNoChange(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
f.pr(5, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": ""})
b := shaped(t, f)
tree := f.git("rev-parse", b.lineHead+"^{tree}")
for _, after := range []string{b.lineHead, tree} {
if om, _, err := b.omittedEffects(b.find(5), b.lineHead, after); err != nil || len(om) != 0 {
t.Errorf("omitted against %.12s = %q, %v; want nothing", after, om, err)
}
}
}
+613
View File
@@ -0,0 +1,613 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"cmp"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
// fixture is a repository whose origin is a bare repository next to it, with
// main and release lines shaped the way GitHub leaves them: every pull request
// lands as a merge commit whose message is its title, its number and its body.
type fixture struct {
t testing.TB
dir string
remote string
now time.Time
// tool is the commit the tool says it was built from.
tool string
}
func newFixture(t testing.TB) *fixture {
t.Helper()
// A hook or an outer repository can export these into the test process.
for _, key := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES"} {
t.Setenv(key, "")
if err := os.Unsetenv(key); err != nil {
t.Fatal(err)
}
}
// The user's global config may sign every commit or run hooks.
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
for _, who := range []string{"AUTHOR", "COMMITTER"} {
t.Setenv("GIT_"+who+"_NAME", "Batch Test")
t.Setenv("GIT_"+who+"_EMAIL", "batch@example.invalid")
}
root := t.TempDir()
f := &fixture{t: t, dir: filepath.Join(root, "work"), remote: filepath.Join(root, "remote.git"),
now: time.Date(2026, 10, 26, 6, 0, 0, 0, time.UTC)}
for _, dir := range []string{f.remote, f.dir} {
args := []string{"init", "-q", "-b", "main", dir}
if dir == f.remote {
args = []string{"init", "-q", "--bare", "-b", "main", dir}
}
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
t.Chdir(f.dir)
f.git("remote", "add", "origin", f.remote)
f.write("README.md", "seed\n")
f.write("chart/values.yaml", "replicas: 1\nimage: app:1\n")
f.write(policyPage, policyWith(""))
f.git("add", "-A")
f.git("commit", "-q", "-m", "chore: seed")
f.git("push", "-q", "origin", "main")
return f
}
func (f *fixture) git(args ...string) string {
f.t.Helper()
out, err := exec.Command("git", args...).CombinedOutput()
if err != nil {
f.t.Fatalf("git %v: %v\n%s", args, err, out)
}
return strings.TrimSpace(string(out))
}
func (f *fixture) write(path, content string) {
f.t.Helper()
full := filepath.Join(f.dir, path)
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
f.t.Fatal(err)
}
if err := os.WriteFile(full, []byte(content), 0o644); err != nil {
f.t.Fatal(err)
}
}
// apply writes files in a stable order; an empty content removes the file.
func (f *fixture) apply(files map[string]string) {
f.t.Helper()
keys := make([]string, 0, len(files))
for k := range files {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
if files[k] == "" {
f.git("rm", "-q", "--", k)
continue
}
f.write(k, files[k])
}
f.git("add", "-A")
}
// pr merges pull request n into main as a merge commit and returns it.
func (f *fixture) pr(n int, title, body string, files map[string]string) string {
f.t.Helper()
branch := fmt.Sprintf("pr-%d", n)
f.git("switch", "-q", "main")
f.git("switch", "-q", "-c", branch)
f.apply(files)
f.git("commit", "-q", "-m", fmt.Sprintf("change for #%d", n))
f.git("switch", "-q", "main")
f.git("merge", "-q", "--no-ff", "-m", fmt.Sprintf("%s (#%d)\n\n%s", title, n, body), branch)
f.git("push", "-q", "origin", "main")
return f.git("rev-parse", "HEAD")
}
// joined makes a merge of tip onto base with tip's tree, whatever a merge of
// the two would make.
func (f *fixture) joined(base, tip, message string) string {
f.t.Helper()
return f.git("commit-tree", "-p", base, "-p", tip, "-m", message, tip+"^{tree}")
}
// contributor writes the commits of the pull requests that say so.
const contributor = "Contributor One <one@example.invalid>"
// change is one step of a pull request's branch: a commit of files, by author
// with message when given, or with mergeMain a merge of main into the branch
// after main moved on.
type change struct {
files map[string]string
author string
message string
mergeMain bool
}
// prCommits merges pull request n into main as a merge commit of a branch
// made of changes, and returns the merge commit.
func (f *fixture) prCommits(n int, title, body string, changes ...change) string {
f.t.Helper()
branch := fmt.Sprintf("pr-%d", n)
f.git("switch", "-q", "main")
f.git("switch", "-q", "-c", branch)
for i, c := range changes {
if c.mergeMain {
f.git("switch", "-q", "main")
f.apply(map[string]string{fmt.Sprintf("main/%d-%d.txt", n, i): "main moved\n"})
f.git("commit", "-q", "-m", "chore: move main along")
f.git("switch", "-q", branch)
f.git("merge", "-q", "--no-ff", "-m", "Merge branch 'main' into "+branch, "main")
continue
}
f.apply(c.files)
msg := c.message
if msg == "" {
msg = fmt.Sprintf("change %d for #%d", i+1, n)
}
args := []string{"commit", "-q", "--allow-empty", "-m", msg}
if c.author != "" {
args = append(args, "--author", c.author)
}
f.git(args...)
}
f.git("switch", "-q", "main")
f.git("merge", "-q", "--no-ff", "-m", fmt.Sprintf("%s (#%d)\n\n%s", title, n, body), branch)
f.git("push", "-q", "origin", "main")
return f.git("rev-parse", "HEAD")
}
// squash merges pull request n into main as a single commit.
func (f *fixture) squash(n int, title, body string, files map[string]string) string {
f.t.Helper()
f.git("switch", "-q", "main")
f.apply(files)
f.git("commit", "-q", "-m", fmt.Sprintf("%s (#%d)\n\n%s", title, n, body))
f.git("push", "-q", "origin", "main")
return f.git("rev-parse", "HEAD")
}
// release cuts a release line at main's head.
func (f *fixture) release(name string) {
f.t.Helper()
f.git("push", "-q", "origin", "main:refs/heads/"+name)
}
// onLine commits onto a release line, as a maintainer's merge would land
// there, and returns the commit.
func (f *fixture) onLine(name, message string, files map[string]string) string {
f.t.Helper()
f.git("fetch", "-q", "origin")
f.git("switch", "-q", "--detach", "origin/"+name)
f.apply(files)
f.git("commit", "-q", "--allow-empty", "-m", message)
sha := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", sha+":refs/heads/"+name)
f.git("switch", "-q", "main")
return sha
}
// descends says whether tip descends from base, failing the test when git
// cannot tell.
func descends(t *testing.T, base, tip string) bool {
t.Helper()
ok, err := isAncestor(base, tip)
if err != nil {
t.Fatal(err)
}
return ok
}
// remoteHead is what the bare remote has for a branch, or "".
func (f *fixture) remoteHead(branch string) string {
f.t.Helper()
out, err := exec.Command("git", "--git-dir="+f.remote, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch).Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
// fakeGH stands in for gh: the open pull requests it reports, and what the
// tool asked it to create or edit.
type fakeGH struct {
prs []pullRequest
err error
perms map[string]string
permErr error
labels map[string][]int
listErr error
asked []string
created []string
edited []string
bodies []string
// issueComments are the comments on the batch pull request, which
// createComment and editComment change.
issueComments []issueComment
// bot is what BACKPORT_BATCH_BOT would say.
bot string
// listed counts the pages of recentPulls read.
listed int
// closedWrites names every write the tool attempted on a pull request
// that is no longer open; exec fails the test on one.
closedWrites []string
// fail makes the next call of an operation, by its method name, fail.
fail map[string]error
// nextPR is the number createPull gives, 90 when unset.
nextPR int
// remoteHead reads a branch's head off the fixture's remote: GitHub
// reports an open pull request at what its branch has.
remoteHead func(string) string
// commented lists the pull request each comment went to, in order.
commented []int
// after runs once an operation, by its method name, has succeeded: what
// someone else does on GitHub meanwhile.
after map[string]func()
}
// live is pr as GitHub reports it now: an open one at the head its branch
// has on the remote.
func (g *fakeGH) live(pr pullRequest) pullRequest {
if g.remoteHead != nil && (pr.State == "" || pr.State == "open") {
if head := g.remoteHead(pr.Head.Ref); head != "" {
pr.Head.SHA = head
}
}
return pr
}
// inject is the failure a test set for the next call of op, once.
func (g *fakeGH) inject(op string) error {
err := g.fail[op]
delete(g.fail, op)
return err
}
func (g *fakeGH) pull(n int) (pullRequest, error) {
if err := g.inject("pull"); err != nil {
return pullRequest{}, err
}
for _, pr := range g.prs {
if pr.Number == n {
return g.live(pr), nil
}
}
return pullRequest{}, fmt.Errorf("HTTP 404: no pull request #%d", n)
}
// isOpen says whether the fake has pull request n open, or does not know it.
func (g *fakeGH) isOpen(n int) bool {
for _, pr := range g.prs {
if pr.Number == n {
return pr.State == "" || pr.State == "open"
}
}
return true
}
// write refuses a write to a pull request that is no longer open, which the
// tool must never attempt.
func (g *fakeGH) write(n int, what string) error {
if g.isOpen(n) {
return nil
}
g.closedWrites = append(g.closedWrites, fmt.Sprintf("%s on #%d", what, n))
return fmt.Errorf("#%d is closed", n)
}
// recentPulls pages every pull request against base, newest first, as the
// REST listing sorted by creation does; a higher number is a newer one.
func (g *fakeGH) recentPulls(base string, page int) ([]pullRequest, error) {
g.listed++
var all []pullRequest
for _, pr := range g.prs {
if pr.Base.Ref == base {
all = append(all, pr)
}
}
slices.SortFunc(all, func(a, b pullRequest) int { return b.Number - a.Number })
from := min(len(all), (page-1)*recentPullsPage)
return slices.Clone(all[from:min(len(all), from+recentPullsPage)]), g.err
}
func (g *fakeGH) batchBot() string { return g.bot }
func (g *fakeGH) comments(int) ([]issueComment, error) {
defer func() {
if f := g.after["comments"]; f != nil {
f()
}
}()
return slices.Clone(g.issueComments), nil
}
func (g *fakeGH) createComment(number int, bodyFile string) error {
if err := g.write(number, "a comment"); err != nil {
return err
}
if err := g.inject("createComment"); err != nil {
return err
}
g.commented = append(g.commented, number)
defer func() {
if f := g.after["createComment"]; f != nil {
f()
}
}()
data, err := os.ReadFile(bodyFile)
var c issueComment
c.ID, c.Body = int64(1000+len(g.issueComments)), string(data)
c.User.Login, c.User.Type = "maintainer", "User"
g.issueComments = append(g.issueComments, c)
return err
}
func (g *fakeGH) editComment(id int64, bodyFile string) error {
data, err := os.ReadFile(bodyFile)
for i := range g.issueComments {
if g.issueComments[i].ID == id {
g.issueComments[i].Body = string(data)
return err
}
}
return fmt.Errorf("no comment %d", id)
}
func (g *fakeGH) labelled(label string) ([]int, error) {
return g.labels[label], g.listErr
}
// permission answers write for the fixture's maintainer and read for anyone
// else, unless the test says otherwise.
func (g *fakeGH) permission(login string) (string, error) {
g.asked = append(g.asked, login)
if g.permErr != nil {
return "", g.permErr
}
if p, ok := g.perms[login]; ok {
return p, nil
}
if login == "maintainer" {
return "write", nil
}
return "read", nil
}
func (g *fakeGH) openPulls(base string) ([]pullRequest, error) {
var out []pullRequest
for _, pr := range g.prs {
if pr.Base.Ref == base && (pr.State == "" || pr.State == "open") {
out = append(out, g.live(pr))
}
}
return out, g.err
}
// createPull opens the pull request on the fake, as the maintainer, at the
// head the remote has, numbered nextPR.
func (g *fakeGH) createPull(base, head, title, bodyFile string) (int, string, error) {
if err := g.inject("createPull"); err != nil {
return 0, "", err
}
g.created = append(g.created, base+" "+head+" "+title)
if err := g.readBody(bodyFile); err != nil {
return 0, "", err
}
n := cmp.Or(g.nextPR, 90)
var pr pullRequest
pr.Number, pr.Body, pr.State = n, g.bodies[len(g.bodies)-1], "open"
pr.HTMLURL = fmt.Sprintf("https://github.com/%s/pull/%d", upstreamRepo, n)
pr.User.Login, pr.User.Type = "maintainer", "User"
pr.Head.Ref = head
if g.remoteHead != nil {
pr.Head.SHA = g.remoteHead(head)
}
pr.Head.Repo = &struct {
FullName string `json:"full_name"`
}{upstreamRepo}
pr.Base.Ref, pr.Base.Repo.FullName = base, upstreamRepo
g.prs = append(slices.DeleteFunc(g.prs, func(p pullRequest) bool { return p.Number == n }), pr)
return n, pr.HTMLURL, nil
}
func (g *fakeGH) editPull(number int, title, bodyFile string) error {
if err := g.write(number, "an edit"); err != nil {
return err
}
if err := g.inject("editPull"); err != nil {
return err
}
g.edited = append(g.edited, fmt.Sprintf("#%d %s", number, title))
if err := g.readBody(bodyFile); err != nil {
return err
}
if pr := g.find(number); pr != nil {
pr.Body = g.bodies[len(g.bodies)-1]
}
if f := g.after["editPull"]; f != nil {
f()
}
return nil
}
func (g *fakeGH) editBody(number int, bodyFile string) error {
if err := g.write(number, "a body edit"); err != nil {
return err
}
if err := g.inject("editBody"); err != nil {
return err
}
if err := g.readBody(bodyFile); err != nil {
return err
}
if pr := g.find(number); pr != nil {
pr.Body = g.bodies[len(g.bodies)-1]
}
return nil
}
func (g *fakeGH) closePull(number int) error {
if err := g.write(number, "a close"); err != nil {
return err
}
if err := g.inject("closePull"); err != nil {
return err
}
if pr := g.find(number); pr != nil {
pr.State = "closed"
}
if f := g.after["closePull"]; f != nil {
f()
}
return nil
}
func (g *fakeGH) find(n int) *pullRequest {
for i := range g.prs {
if g.prs[i].Number == n {
return &g.prs[i]
}
}
return nil
}
func (g *fakeGH) readBody(path string) error {
data, err := os.ReadFile(path)
g.bodies = append(g.bodies, string(data))
return err
}
// batchPR is an open batch pull request opened by a maintainer from a branch
// of this repository, at whatever head the remote has for the branch.
func (f *fixture) batchPR(n int, base, branch, body string) pullRequest {
f.t.Helper()
var pr pullRequest
pr.Number, pr.Body = n, body
pr.HTMLURL = fmt.Sprintf("https://github.com/%s/pull/%d", upstreamRepo, n)
pr.User.Login, pr.User.Type, pr.AuthorAssociation = "maintainer", "User", "MEMBER"
pr.Head.Ref, pr.Head.SHA = branch, f.remoteHead(branch)
pr.Head.Repo = &struct {
FullName string `json:"full_name"`
}{upstreamRepo}
pr.Base.Ref, pr.Base.Repo.FullName = base, upstreamRepo
pr.State = "open"
return pr
}
// mergeBatch merges the open batch into release-1.1 with a merge commit, as
// GitHub does, and deletes its branch, as the repository's
// delete_branch_on_merge does. The pull request stays, merged, with its body
// and comments.
func mergeBatch(f *fixture, gh *fakeGH) {
f.t.Helper()
f.git("fetch", "-q", "origin")
f.git("switch", "-q", "--detach", "origin/release-1.1")
f.git("merge", "-q", "--no-ff", "-m", "Merge pull request #90 from cozystack/"+batchBranch1, "origin/"+batchBranch1)
f.git("push", "-q", "origin", "HEAD:refs/heads/release-1.1", ":refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
for i := range gh.prs {
if gh.prs[i].Number == 90 {
at := "2026-10-26T12:00:00Z"
gh.prs[i].State, gh.prs[i].MergedAt = "closed", &at
}
}
}
// exec runs the tool in the fixture with gh faked, on the fixture's date, and
// fails the test when the tool wrote to a pull request that is not open.
func (f *fixture) exec(gh *fakeGH, args ...string) (int, string, string) {
f.t.Helper()
if gh == nil {
gh = &fakeGH{}
}
if gh.remoteHead == nil {
gh.remoteHead = f.remoteHead
}
var stdout, stderr bytes.Buffer
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return f.now }, tool: f.tool}
written := len(gh.closedWrites)
code := a.main(args)
if len(gh.closedWrites) > written {
f.t.Errorf("%v wrote to a pull request that is not open: %q", args, gh.closedWrites[written:])
}
return code, stdout.String(), stderr.String()
}
const batchBranch1 = "backport-batch/release-1.1-20261026"
// lines cuts and releases release-1.0 and then release-1.1 with one change
// between them, leaving release-1.1 the newer of the two lines the batch runs
// for.
func (f *fixture) lines() {
f.t.Helper()
f.release("release-1.0")
f.tag("v1.0.0")
f.pr(1, "feat(app): something for 1.1", "", map[string]string{"app/feature.txt": "1.1\n"})
f.release("release-1.1")
f.tag("v1.1.0")
}
// tag tags main's head on the remote: a stable release when the name has no
// pre-release suffix.
func (f *fixture) tag(name string) {
f.t.Helper()
f.git("push", "-q", "origin", "main:refs/tags/"+name)
}
// subjects lists the subjects of the units on branch beyond base, oldest
// first: the first-parent line, where a batch has one commit per original.
func (f *fixture) subjects(base, branch string) []string {
f.t.Helper()
out := f.git("log", "--reverse", "--first-parent", "--format=%s", base+".."+branch)
if out == "" {
return nil
}
return strings.Split(out, "\n")
}
// batchOnlyDay is the fixture's batch-only day: commits a test makes carry
// the clock's date, later than it, unless the test dates them.
const batchOnlyDay = "2026-10-26"
// policyWith is a policy page whose block lists entries, and the fixture's
// batch-only day unless entries give one.
func policyWith(entries string) string {
if !strings.Contains(entries, batchOnlyKey+":") {
entries += batchOnlyKey + ": " + batchOnlyDay + "\n"
}
return "## Backports\n\n" + policyFence + "\n# release-X.Y: YYYY-MM-DD\n" + entries + "```\n"
}
// setPolicy commits a policy page with entries onto main.
func (f *fixture) setPolicy(entries string) {
f.t.Helper()
f.squash(9000, "docs(release): set the policy", "", map[string]string{policyPage: policyWith(entries)})
}
+422
View File
@@ -0,0 +1,422 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strconv"
"strings"
"testing"
)
// foldWorld is a line whose own change makes the first of #5's two commits
// conflict, an empty batch pushed for it, and the originals around #5.
type foldWorld struct {
f *fixture
gh *fakeGH
head string
mc5, mc6 string
c1, c2 string // #5's own commits
c6 string // #6's one commit
sq8 string
}
func newFoldWorld(t *testing.T) *foldWorld {
t.Helper()
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
w := &foldWorld{f: f}
w.mc5 = f.prCommits(5, "fix(chart): five", "",
change{files: map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"}},
change{files: map[string]string{"app/five.txt": "5\n"}})
own := strings.Fields(f.git("rev-list", "--reverse", w.mc5+"^1.."+w.mc5+"^2"))
w.c1, w.c2 = own[0], own[1]
w.mc6 = f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
w.c6 = f.git("rev-parse", w.mc6+"^2")
f.pr(7, "feat(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
w.sq8 = f.squash(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
f.git("fetch", "-q", "origin")
w.head = f.git("rev-parse", "origin/"+batchBranch1)
w.gh = &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "")}}
return w
}
// commit writes files on parent with message and returns the commit.
func (w *foldWorld) commit(parent, message string, files map[string]string) string {
w.f.git("switch", "-q", "--detach", parent)
w.f.apply(files)
w.f.git("commit", "-q", "--allow-empty", "-m", message)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
}
// pick is a pick of source on parent, written as files, with its -x line.
func (w *foldWorld) pick(parent, source string, files map[string]string) string {
return w.commit(parent, "port\n\n(cherry picked from commit "+source+")", files)
}
func (w *foldWorld) merge(base, tip, message string) string {
w.f.git("switch", "-q", "--detach", base)
w.f.git("merge", "-q", "--no-ff", "-m", message, tip)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
}
// joined makes a merge of tip onto base with tip's tree, whatever a merge of
// the two would make.
func (w *foldWorld) joined(base, tip, message string) string {
return w.f.git("commit-tree", "-p", base, "-p", tip, "-m", message, tip+"^{tree}")
}
func batchMessage(merges ...string) string {
msg := "fix(chart): five (#5)\n"
for _, m := range merges {
msg += "\n" + batchTrailer + ": " + m
}
return msg
}
var (
resolved1 = map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"}
resolved2 = map[string]string{"app/five.txt": "5\n"}
)
// resolution is the right fold of #5: both its commits picked in order on the
// batch head, the conflict resolved, under a merge that names it.
func (w *foldWorld) resolution() string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(w.head, r2, batchMessage(w.mc5))
}
// fold accepts one original on the batch head in the batch's shape, inside the
// files it touches, and nothing else.
func TestFold(t *testing.T) {
cases := []struct {
name string
original int
make func(w *foldWorld) string
refusal string
}{
{
name: "a resolution of #5", original: 5,
make: (*foldWorld).resolution,
},
{
name: "one commit, the way -m 1 picked it", original: 5,
make: func(w *foldWorld) string {
return w.commit(w.head, "fix(chart): five (#5)\n\n(cherry picked from commit "+w.mc5+")", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n", "app/five.txt": "5\n"})
},
refusal: "has 1 parents; #5 is a merged pull request",
},
{
name: "a merge not on the batch head", original: 5,
make: func(w *foldWorld) string {
base := w.commit(w.head, "something else", map[string]string{"app/five.txt": "x\n"})
r1 := w.pick(base, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(base, r2, batchMessage(w.mc5))
},
refusal: "not on the head of batch pull request #90",
},
{
name: "a merge that does not name #5", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(w.head, r2, batchMessage())
},
refusal: "with 0 " + batchTrailer + " trailers",
},
{
name: "two originals in one resolution", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
r3 := w.pick(r2, w.c6, map[string]string{"app/six.txt": "6\n"})
return w.merge(w.head, r3, batchMessage(w.mc5, w.mc6))
},
refusal: "with 2 " + batchTrailer + " trailers",
},
{
name: "a commit of another pull request", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
r3 := w.pick(r2, w.c6, map[string]string{"app/six.txt": "6\n"})
return w.merge(w.head, r3, batchMessage(w.mc5))
},
refusal: "picks a commit of #6 into the merge of #5",
},
{
name: "an extra commit", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
r3 := w.commit(r2, "fix up the port", map[string]string{"app/five.txt": "5, tidied\n"})
return w.merge(w.head, r3, batchMessage(w.mc5))
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "a file #5 does not touch", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
r2 := w.pick(r1, w.c2, map[string]string{"app/five.txt": "5\n", "app/smuggled.txt": "x\n"})
return w.merge(w.head, r2, batchMessage(w.mc5))
},
refusal: `touches "app/smuggled.txt"`,
},
{
name: "a pick without its -x line", original: 5,
make: func(w *foldWorld) string {
r1 := w.commit(w.head, "port", resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(w.head, r2, batchMessage(w.mc5))
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "an abbreviated -x line", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1[:12], resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(w.head, r2, batchMessage(w.mc5))
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "an -x line in the body, not in the trailers", original: 5,
make: func(w *foldWorld) string {
r1 := w.commit(w.head, "port\n\n(cherry picked from commit "+w.c1+")\n\nMore to say.", resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.merge(w.head, r2, batchMessage(w.mc5))
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "both commits in one pick", original: 5,
make: func(w *foldWorld) string {
both := w.commit(w.head, "port\n\n(cherry picked from commit "+w.c1+")\n(cherry picked from commit "+w.c2+")", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n", "app/five.txt": "5\n"})
return w.merge(w.head, both, batchMessage(w.mc5))
},
refusal: "claims more than one commit of #5",
},
{
name: "the commits in the wrong order", original: 5,
make: func(w *foldWorld) string {
r2 := w.pick(w.head, w.c2, resolved2)
r1 := w.pick(r2, w.c1, resolved1)
return w.merge(w.head, r1, batchMessage(w.mc5))
},
refusal: "out of the order of #5's commits",
},
{
name: "one commit picked twice", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, resolved1)
again := w.pick(r1, w.c1, map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:2\n"})
return w.merge(w.head, again, batchMessage(w.mc5))
},
refusal: "out of the order of #5's commits",
},
{
name: "picks that do not start on the batch head", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head+"~1", w.c1, resolved1)
r2 := w.pick(r1, w.c2, resolved2)
return w.joined(w.head, r2, batchMessage(w.mc5))
},
refusal: "is not on a straight line of picks",
},
{
name: "picks that change nothing", original: 5,
make: func(w *foldWorld) string {
r1 := w.pick(w.head, w.c1, nil)
r2 := w.pick(r1, w.c2, nil)
return w.merge(w.head, r2, batchMessage(w.mc5))
},
refusal: "changes nothing",
},
{
name: "a sound fold of another original", original: 5,
make: func(w *foldWorld) string {
p := w.pick(w.head, w.c6, map[string]string{"app/six.txt": "6\n"})
return w.merge(w.head, p, "fix(app): six (#6)\n\n"+batchTrailer+": "+w.mc6)
},
refusal: "carries #6, not #5",
},
{
name: "one pick of a squash-merged original", original: 8,
make: func(w *foldWorld) string {
return w.pick(w.head, w.sq8, map[string]string{"app/eight.txt": "8\n"})
},
},
{
name: "a squash-merged original as a merge", original: 8,
make: func(w *foldWorld) string {
p := w.pick(w.head, w.sq8, map[string]string{"app/eight.txt": "8\n"})
return w.merge(w.head, p, "fix(app): eight (#8)\n\n"+batchTrailer+": "+w.sq8)
},
refusal: "has 2 parents; #8 merged as one commit",
},
{
name: "an original the batch does not take", original: 7,
make: func(w *foldWorld) string { return w.commit(w.head, "port", map[string]string{"app/seven.txt": "7\n"}) },
refusal: "rule 2",
},
{
name: "an original that never merged here", original: 4242,
make: func(w *foldWorld) string { return w.commit(w.head, "port", map[string]string{"app/x.txt": "x\n"}) },
refusal: "did not merge into main",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
w := newFoldWorld(t)
sha := tc.make(w)
code, stdout, stderr := w.f.exec(w.gh, "fold", "--line", "release-1.1", "--original", strconv.Itoa(tc.original), "--commit", sha)
switch {
case tc.refusal == "" && (code != 0 || strings.TrimSpace(stdout) != "ok"):
t.Errorf("fold = %d, %q, %s; want ok", code, stdout, stderr)
case tc.refusal != "" && (code != 1 || !strings.Contains(stdout, tc.refusal)):
t.Errorf("fold = %d, %q, %s; want a refusal with %q", code, stdout, stderr, tc.refusal)
}
})
}
t.Run("no open batch pull request", func(t *testing.T) {
w := newFoldWorld(t)
code, stdout, _ := w.f.exec(&fakeGH{}, "fold", "--line", "release-1.1", "--original", "5", "--commit", w.resolution())
if code != 1 || !strings.Contains(stdout, "no batch pull request is open") {
t.Errorf("fold = %d, %q; want a refusal", code, stdout)
}
})
t.Run("already carried by the batch", func(t *testing.T) {
w := newFoldWorld(t)
folded := w.resolution()
w.f.git("push", "-q", "origin", folded+":refs/heads/"+batchBranch1)
w.gh.prs[0].Head.SHA = folded
w.head = folded
code, stdout, _ := w.f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", w.resolution())
if code != 1 || !strings.Contains(stdout, "already carries #5") {
t.Errorf("fold = %d, %q; want a refusal", code, stdout)
}
})
t.Run("never commits", func(t *testing.T) {
w := newFoldWorld(t)
sha := w.resolution()
before := w.f.git("for-each-ref")
w.f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", sha)
if after := w.f.git("for-each-ref"); after != before || w.f.remoteHead(batchBranch1) != w.head {
t.Errorf("fold moved a ref:\nbefore:\n%s\nafter:\n%s", before, after)
}
})
}
// A resolution of an original a maintainer dropped from the line is refused:
// the drop is the decision that it does not go there.
func TestFoldRefusesADroppedOriginal(t *testing.T) {
w := newFoldWorld(t)
sha := strings.Repeat("a", 40)
rec := strings.Replace(v2(""), `"dropped":[{"original":8,"merge":"`+sha+`"`, `"dropped":[{"original":5,"merge":"`+w.mc5+`"`, 1)
rec = strings.Replace(rec, `"deferred":[{"original":5,"commit":"`+sha+`","files":["app/five.txt"]},`, `"deferred":[`, 1)
rec = strings.Replace(rec, `"stale":[9]`, `"stale":[]`, 1)
w.gh.prs[0].Body = "text\n\n" + deferredFence + "\n" + rec + "\n```\n"
if _, err := parseRecord(w.gh.prs[0].Body, notifyLine); err != nil {
t.Fatal(err)
}
code, stdout, stderr := w.f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", w.resolution())
if code != 1 || !strings.Contains(stdout, "#5 is not one the batch takes: a maintainer's drop, dropped on #90: needs #4") {
t.Errorf("fold = %d, %q, %s; want a refusal naming the drop", code, stdout, stderr)
}
}
// A resolution that ports what the line has of #5 and leaves alone a file
// the line lacks is no fold: what #5 does to that file would be lost, and a
// line-only port is a pull request of its own.
func TestFoldRefusesAnOmittedEffect(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): a feature the line never gets", "", map[string]string{"app/feature2.txt": "v1\n"})
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/feature2.txt": "v2\n", "app/five.txt": "5\n"})
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
fold := w.merge(w.head, pick, "fix(app): five (#5)\n\n"+batchTrailer+": "+mc5)
code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold)
if code != 1 || out != "refused: #5 leaves \"app/feature2.txt\" without effect on release-1.1; port only what the line has as a pull request of its own\n" {
t.Errorf("fold = %d, %q, want it refused for app/feature2.txt\n%s", code, out, stderr)
}
}
// A resolution that keeps a file the original deletes leaves the deletion
// without effect.
func TestFoldRefusesAKeptDeletion(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: x", "", map[string]string{"app/x.txt": "x\n"})
f.lines()
mc5 := f.pr(5, "fix(app): five, and x goes", "", map[string]string{"app/x.txt": "", "app/five.txt": "5\n"})
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
fold := w.merge(w.head, pick, "fix(app): five, and x goes (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/x.txt" without effect`) {
t.Errorf("fold that keeps app/x.txt = %d, %q\n%s", code, out, stderr)
}
}
// An original parked for what it builds on can still land as a complete
// hand adaptation: every file it changes, changed, in the batch's shape.
func TestFoldAcceptsACompleteAdaptationOfAParkedOriginal(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
mc5 := f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/a.txt": strings.Replace(appFile, "two", "two!", 1)})
fold := w.merge(w.head, pick, "fix(app): an excited two (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 || out != "ok\n" {
t.Errorf("fold of an adaptation of #5 = %d, %q\n%s", code, out, stderr)
}
}
// A fold that keeps a file the original deletes leaves the deletion without
// effect, whatever it changes in the file: a port that keeps what the line
// made of it, or the file with a line added, is a pull request of its own.
func TestFoldRefusesAChangedKeptDeletion(t *testing.T) {
for name, kept := range map[string]string{
"the line's own content": "line only\n",
"the file, one line more": "feature\nline only\n# reviewed\n",
} {
t.Run(name, func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a feature", "", map[string]string{"app/x.txt": "feature\n"})
f.lines()
f.onLine("release-1.1", "fix(app): a line-only setting beside the feature", map[string]string{"app/x.txt": "feature\nline only\n"})
mc5 := f.pr(5, "fix(app): drop the feature, and five", "", map[string]string{"app/x.txt": "", "app/five.txt": "5\n"})
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/x.txt": kept, "app/five.txt": "5\n"})
fold := w.merge(w.head, pick, "fix(app): drop the feature, and five (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/x.txt" without effect`) {
t.Errorf("fold = %d, %q, want it refused\n%s", code, out, stderr)
}
})
}
}
+276
View File
@@ -0,0 +1,276 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
var shaRE = regexp.MustCompile(`^[0-9a-f]{40}$`)
// commandEnv pins the locale, because telling a conflict from an empty pick
// reads git's messages, and keeps git from opening an editor.
var commandEnv = []string{"LC_ALL=C", "GIT_EDITOR=true"}
func command(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
cmd.Env = append(os.Environ(), commandEnv...)
return cmd
}
func capture(name string, args ...string) (string, error) {
out, err := command(name, args...).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
return "", fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(string(ee.Stderr)))
}
return "", fmt.Errorf("%s %s: %w", name, strings.Join(args, " "), err)
}
return string(out), nil
}
// git runs a git command whose failure means the batch cannot be trusted.
func git(args ...string) (string, error) { return capture("git", args...) }
func gitInput(input string, args ...string) (string, error) {
cmd := command("git", args...)
cmd.Stdin = strings.NewReader(input)
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
return string(out), nil
}
// gitOK runs git for a command whose exit 1 is the answer no; any other
// failure is an error.
func gitOK(args ...string) (bool, error) {
_, stderr, code, err := gitStatus(args...)
switch {
case err != nil:
return false, err
case code != 0 && code != 1:
return false, fmt.Errorf("git %s: exit %d: %s", strings.Join(args, " "), code, strings.TrimSpace(stderr))
}
return code == 0, nil
}
// gitStatus runs git for a command whose non-zero exit is an answer rather
// than a failure: a conflicting pick, a grep that matched nothing.
func gitStatus(args ...string) (stdout, stderr string, code int, err error) {
var out, errOut bytes.Buffer
cmd := command("git", args...)
cmd.Stdout, cmd.Stderr = &out, &errOut
err = cmd.Run()
var ee *exec.ExitError
if errors.As(err, &ee) {
return out.String(), errOut.String(), ee.ExitCode(), nil
}
if err != nil {
return "", "", -1, fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
return out.String(), errOut.String(), 0, nil
}
func checkSHA(s string) error {
if !shaRE.MatchString(s) {
return fmt.Errorf("%q is not a full commit SHA", s)
}
return nil
}
// checkBranch refuses a name git could read as an option or would rewrite.
// check-ref-format --branch expands @{-N} into whatever branch that was, so
// its answer has to be the name itself.
func checkBranch(name string) error {
if name == "" || strings.HasPrefix(name, "-") || strings.ContainsFunc(name, unicode.IsControl) {
return fmt.Errorf("%q is not a branch name", name)
}
out, _, code, err := gitStatus("check-ref-format", "--branch", name)
switch {
case err != nil:
return err
case code != 0 || strings.TrimSuffix(out, "\n") != name:
return fmt.Errorf("%q is not a branch name", name)
}
return nil
}
// resolve pins a ref to the commit it names now, so every later read sees the
// same commit however the ref moves.
func resolve(ref string) (string, error) {
out, err := git("rev-parse", "--verify", "--quiet", "--end-of-options", ref+"^{commit}")
if err != nil {
return "", fmt.Errorf("cannot resolve %s: %w", ref, err)
}
return strings.TrimSpace(out), nil
}
// lookup is the commit rev names, or "" when this repository has none: a ref
// that does not exist, or an object it does not have. cat-file -e exits 128
// for a missing object, as it does when git fails, so it cannot tell them.
func lookup(rev string) (string, error) {
out, stderr, code, err := gitStatus("rev-parse", "--verify", "--quiet", "--end-of-options", rev+"^{commit}")
switch {
case err != nil:
return "", err
case code == 1:
return "", nil
case code != 0:
return "", fmt.Errorf("git rev-parse %s: exit %d: %s", rev, code, strings.TrimSpace(stderr))
}
return strings.TrimSpace(out), nil
}
func isAncestor(a, b string) (bool, error) {
return gitOK("merge-base", "--is-ancestor", "--end-of-options", a, b)
}
// splitNUL splits -z output, dropping the empty field after the last NUL.
func splitNUL(out string) []string {
var fields []string
for f := range strings.SplitSeq(out, "\x00") {
if f != "" {
fields = append(fields, f)
}
}
return fields
}
// revList lists commits with rev-list, whose output is object names and
// nothing a commit message could add to; each one is checked to be one.
func revList(args ...string) ([]string, error) {
out, err := git(append([]string{"rev-list"}, args...)...)
if err != nil {
return nil, err
}
shas := strings.Fields(out)
for _, sha := range shas {
if err := checkSHA(sha); err != nil {
return nil, fmt.Errorf("rev-list: %w", err)
}
}
return shas, nil
}
// commitObject is what the batch reads of a commit: its message, its parents,
// and when it was committed, which for a merge GitHub made is when it merged.
type commitObject struct {
message string
parents []string
committed time.Time
}
// readObjects reads objects of one type through one cat-file --batch, whose
// output gives each object's length before its content: nothing inside one, a
// NUL or a line shaped like a header included, can pass for the start of the
// next.
func readObjects(ids []string, kind string) (map[string][]byte, error) {
objects := map[string][]byte{}
if len(ids) == 0 {
return objects, nil
}
cmd := command("git", "cat-file", "--batch")
cmd.Stdin = strings.NewReader(strings.Join(ids, "\n") + "\n")
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git cat-file --batch: %w", err)
}
r := bufio.NewReader(bytes.NewReader(out))
for _, want := range ids {
header, err := r.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("git cat-file --batch: no answer for %s", want)
}
f := strings.Fields(header)
if len(f) != 3 || f[0] != want || f[1] != kind {
return nil, fmt.Errorf("git cat-file --batch: %q is not %s %s", strings.TrimSpace(header), kind, want)
}
size, err := strconv.Atoi(f[2])
if err != nil || size < 0 {
return nil, fmt.Errorf("git cat-file --batch: bad size in %q", strings.TrimSpace(header))
}
body := make([]byte, size+1)
if _, err := io.ReadFull(r, body); err != nil || body[size] != '\n' {
return nil, fmt.Errorf("git cat-file --batch: short object %s", want)
}
objects[want] = body[:size]
}
return objects, nil
}
// commitMessages reads commits through readObjects. A message ends at its
// first NUL, where git log ends it, so nothing hidden from a reader of the log
// counts here.
func commitMessages(shas []string) (map[string]commitObject, error) {
raw, err := readObjects(shas, "commit")
if err != nil {
return nil, err
}
msgs := map[string]commitObject{}
for sha, data := range raw {
headers, msg, _ := strings.Cut(string(data), "\n\n")
msg, _, _ = strings.Cut(msg, "\x00")
committed, err := committerTime(headers)
if err != nil {
return nil, fmt.Errorf("commit %s: %w", sha, err)
}
var parents []string
for ln := range strings.Lines(headers) {
if p, ok := strings.CutPrefix(strings.TrimRight(ln, "\n"), "parent "); ok {
if err := checkSHA(p); err != nil {
return nil, fmt.Errorf("commit %s: %w", sha, err)
}
parents = append(parents, p)
}
}
msgs[sha] = commitObject{message: msg, parents: parents, committed: committed}
}
return msgs, nil
}
// committerTime reads the committer line of a commit's headers:
// "committer Name <email> <unix seconds> <zone>".
func committerTime(headers string) (time.Time, error) {
for ln := range strings.Lines(headers) {
rest, ok := strings.CutPrefix(strings.TrimRight(ln, "\n"), "committer ")
if !ok {
continue
}
f := strings.Fields(rest[strings.LastIndexByte(rest, '>')+1:])
if len(f) != 2 {
break
}
secs, err := strconv.ParseInt(f[0], 10, 64)
if err != nil {
break
}
return time.Unix(secs, 0).UTC(), nil
}
return time.Time{}, fmt.Errorf("no readable committer line")
}
+448
View File
@@ -0,0 +1,448 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
)
// openPullsCap is the page size of the open pull request listing. The REST
// listing stops at it in silence, so a full page is an error, not an answer.
const openPullsCap = 100
// recentPullsPage is the page size of the listing of every pull request
// against a line, newest first, and recentPullsPages how many pages of it a
// search for the line's record reads.
const (
recentPullsPage = 100
recentPullsPages = 10
)
// searchResultCap is the most results GitHub search returns for one query,
// however far it is paginated. gh pr list answers a --label filter through
// search, so a listing that long has to be taken as cut short.
const searchResultCap = 1000
// The labels count on a pull request merged into main, the original, and on
// no batch pull request.
const (
labelInclude = "backport/include"
labelSkip = "backport/skip"
)
var (
batchBranchRE = regexp.MustCompile(`^backport-batch/(release-\d+\.\d+)-(\d{8})$`)
loginRE = regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$`)
botLoginRE = regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\[bot\]$`)
pullURLRE = regexp.MustCompile(`^https://[^/\s]+/[^/\s]+/[^/\s]+/pull/([1-9][0-9]{0,8})$`)
)
// batchBotEnv names the GitHub App bot the scheduled batch opens its pull
// requests and comments as, <app slug>[bot]. The workflow sets it from the
// token it mints. Unset, it is defaultBatchBot, so that a maintainer's run by
// hand continues the scheduled batch instead of opening a second one; set and
// empty, no bot is trusted.
const (
batchBotEnv = "BACKPORT_BATCH_BOT"
defaultBatchBot = "cozystack-ci[bot]"
)
// batchBotLogin is the bot to trust given lookup, os.LookupEnv outside tests.
func batchBotLogin(lookup func(string) (string, bool)) string {
if v, ok := lookup(batchBotEnv); ok {
return v
}
return defaultBatchBot
}
// pullRequest is the REST view of a pull request: unlike gh pr list, it
// carries the author's association with the repository.
type pullRequest struct {
Number int `json:"number"`
HTMLURL string `json:"html_url"`
Body string `json:"body"`
State string `json:"state"`
MergedAt *string `json:"merged_at"`
AuthorAssociation string `json:"author_association"`
User struct {
Login string `json:"login"`
Type string `json:"type"`
} `json:"user"`
Head struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
Repo *struct {
FullName string `json:"full_name"`
} `json:"repo"`
} `json:"head"`
Base struct {
Ref string `json:"ref"`
Repo struct {
FullName string `json:"full_name"`
} `json:"repo"`
} `json:"base"`
}
// issueComment is the REST view of a comment on a pull request.
type issueComment struct {
ID int64 `json:"id"`
Body string `json:"body"`
User struct {
Login string `json:"login"`
Type string `json:"type"`
} `json:"user"`
}
// github is the part of GitHub the tool talks to: gh outside tests.
type github interface {
openPulls(base string) ([]pullRequest, error)
// recentPulls is one page of every pull request against base, open,
// merged or closed, newest first.
recentPulls(base string, page int) ([]pullRequest, error)
// permission is login's permission on the repository as GitHub's
// legacy field gives it: admin, write, read or none.
permission(login string) (string, error)
// labelled lists the pull requests merged into main that carry label.
labelled(label string) ([]int, error)
// pull reads one pull request as it is now.
pull(number int) (pullRequest, error)
// createPull opens the draft and returns its number and URL.
createPull(base, head, title, bodyFile string) (int, string, error)
editPull(number int, title, bodyFile string) error
// editBody replaces the body of pull request number and nothing else.
editBody(number int, bodyFile string) error
closePull(number int) error
comments(number int) ([]issueComment, error)
createComment(number int, bodyFile string) error
editComment(id int64, bodyFile string) error
// batchBot is the bot the batch writes as, from batchBotEnv, or "".
batchBot() string
}
// guardedGitHub refuses every GitHub write once the run's output was lost.
type guardedGitHub struct {
github
a *app
}
func (g guardedGitHub) createPull(base, head, title, bodyFile string) (int, string, error) {
if err := g.a.mayAct(); err != nil {
return 0, "", err
}
return g.github.createPull(base, head, title, bodyFile)
}
func (g guardedGitHub) editPull(number int, title, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editPull(number, title, bodyFile)
}
func (g guardedGitHub) editBody(number int, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editBody(number, bodyFile)
}
func (g guardedGitHub) closePull(number int) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.closePull(number)
}
func (g guardedGitHub) createComment(number int, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.createComment(number, bodyFile)
}
func (g guardedGitHub) editComment(id int64, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editComment(id, bodyFile)
}
type ghCLI struct{ bot string }
func (g ghCLI) batchBot() string { return g.bot }
func (ghCLI) openPulls(base string) ([]pullRequest, error) {
out, err := capture("gh", "api", "-X", "GET", "repos/{owner}/{repo}/pulls",
"-f", "state=open", "-f", "base="+base, "-f", "per_page="+strconv.Itoa(openPullsCap))
if err != nil {
return nil, err
}
var prs []pullRequest
if err := json.Unmarshal([]byte(out), &prs); err != nil {
return nil, fmt.Errorf("reading the open pull requests on %s: %w", base, err)
}
if len(prs) >= openPullsCap {
return nil, fmt.Errorf("the open pull requests on %s fill a page of %d, so the list may be truncated", base, openPullsCap)
}
return prs, nil
}
func (ghCLI) recentPulls(base string, page int) ([]pullRequest, error) {
out, err := capture("gh", "api", "-X", "GET", "repos/{owner}/{repo}/pulls",
"-f", "state=all", "-f", "base="+base, "-f", "sort=created", "-f", "direction=desc",
"-f", "per_page="+strconv.Itoa(recentPullsPage), "-f", "page="+strconv.Itoa(page))
if err != nil {
return nil, err
}
var prs []pullRequest
if err := json.Unmarshal([]byte(out), &prs); err != nil {
return nil, fmt.Errorf("reading the pull requests against %s: %w", base, err)
}
return prs, nil
}
// permCache asks GitHub for a login's permission once per command: the open
// batch's author is asked when it is chosen and again when its record is.
type permCache struct {
github
perms map[string]string
}
func (c *permCache) permission(login string) (string, error) {
if p, ok := c.perms[login]; ok {
return p, nil
}
p, err := c.github.permission(login)
if err == nil {
c.perms[login] = p
}
return p, err
}
func (ghCLI) permission(login string) (string, error) {
if !loginRE.MatchString(login) {
return "", fmt.Errorf("%q is not a GitHub login", login)
}
out, err := capture("gh", "api", "repos/{owner}/{repo}/collaborators/"+login+"/permission")
if err != nil {
return "", err
}
var answer struct {
Permission string `json:"permission"`
}
if err := json.Unmarshal([]byte(out), &answer); err != nil {
return "", fmt.Errorf("reading %s's permission: %w", login, err)
}
return answer.Permission, nil
}
func (ghCLI) labelled(label string) ([]int, error) {
out, err := capture("gh", "pr", "list", "--base", "main", "--state", "merged", "--label", label,
"--limit", strconv.Itoa(searchResultCap), "--json", "number")
if err != nil {
return nil, err
}
var prs []struct {
Number int `json:"number"`
}
if err := json.Unmarshal([]byte(out), &prs); err != nil {
return nil, fmt.Errorf("reading the pull requests labelled %s: %w", label, err)
}
if len(prs) >= searchResultCap {
return nil, fmt.Errorf("%d pull requests carry %s, as many as GitHub search returns, so the list may be cut short", len(prs), label)
}
numbers := make([]int, 0, len(prs))
for _, pr := range prs {
numbers = append(numbers, pr.Number)
}
return numbers, nil
}
func (ghCLI) pull(number int) (pullRequest, error) {
var pr pullRequest
out, err := capture("gh", "api", "repos/{owner}/{repo}/pulls/"+strconv.Itoa(number))
if err != nil {
return pr, err
}
if err := json.Unmarshal([]byte(out), &pr); err != nil {
return pr, fmt.Errorf("reading pull request #%d: %w", number, err)
}
if pr.Number != number {
return pr, fmt.Errorf("asked for pull request #%d, GitHub answered with #%d", number, pr.Number)
}
return pr, nil
}
func (ghCLI) createPull(base, head, title, bodyFile string) (int, string, error) {
out, err := capture("gh", "pr", "create", "--draft", "--base", base, "--head", head, "--title", title, "--body-file", bodyFile)
if err != nil {
return 0, "", err
}
url := strings.TrimSpace(out)
m := pullURLRE.FindStringSubmatch(url)
if m == nil {
return 0, "", fmt.Errorf("gh pr create printed %q, not the URL of a pull request", url)
}
n, err := strconv.Atoi(m[1])
return n, url, err
}
func (ghCLI) editPull(number int, title, bodyFile string) error {
_, err := capture("gh", "pr", "edit", strconv.Itoa(number), "--title", title, "--body-file", bodyFile)
return err
}
func (ghCLI) editBody(number int, bodyFile string) error {
_, err := capture("gh", "pr", "edit", strconv.Itoa(number), "--body-file", bodyFile)
return err
}
func (ghCLI) closePull(number int) error {
_, err := capture("gh", "pr", "close", strconv.Itoa(number))
return err
}
func (ghCLI) comments(number int) ([]issueComment, error) {
out, err := capture("gh", "api", "--paginate", "--slurp", "repos/{owner}/{repo}/issues/"+strconv.Itoa(number)+"/comments")
if err != nil {
return nil, err
}
var pages [][]issueComment
if err := json.Unmarshal([]byte(out), &pages); err != nil {
return nil, fmt.Errorf("reading the comments on #%d: %w", number, err)
}
var all []issueComment
for _, page := range pages {
all = append(all, page...)
}
return all, nil
}
func (ghCLI) createComment(number int, bodyFile string) error {
_, err := capture("gh", "api", "--method", "POST", "repos/{owner}/{repo}/issues/"+strconv.Itoa(number)+"/comments", "-F", "body=@"+bodyFile)
return err
}
func (ghCLI) editComment(id int64, bodyFile string) error {
_, err := capture("gh", "api", "--method", "PATCH", "repos/{owner}/{repo}/issues/comments/"+strconv.FormatInt(id, 10), "-F", "body=@"+bodyFile)
return err
}
// trusted says why a pull request's author may not speak for the batch, or ""
// when they may.
func trusted(gh github, pr pullRequest) (string, error) {
why, err := trustedAuthor(gh, pr.User.Login, pr.User.Type)
if why != "" {
why = "opened by " + why
}
return why, err
}
// trustedAuthor says why an author may not speak for the batch, or "" when
// they may: the bot the batch runs as, by its exact login, or a person whose
// permission on the repository is write or admin. Any other bot is refused,
// GitHub Actions' own included: anything with a workflow's token can write as
// it. The author association is not asked: an organization member or an
// invited collaborator can hold read access only, and the legacy permission
// folds maintain into write and triage into read. A permission GitHub does not
// answer is an error, not a refusal: taken for one, it would hide the open
// batch and its comment, and the run would open a second of each.
func trustedAuthor(gh github, login, kind string) (string, error) {
switch {
case kind == "Bot" && gh.batchBot() != "" && login == gh.batchBot():
return "", nil
case kind != "User" || !loginRE.MatchString(login):
return fmt.Sprintf("%q, which is neither a person nor the batch's bot (%s)", login, batchBotEnv), nil
}
perm, err := gh.permission(login)
switch {
case err != nil:
return "", err
case perm != "write" && perm != "admin":
return fmt.Sprintf("%s, whose permission is %q", login, perm), nil
}
return "", nil
}
// batchPull picks the open batch pull request for l out of prs, or nil.
//
// A pull request counts only when its head is a batch branch for l in this
// repository, not a fork, and a trusted author opened it: anyone can open a
// pull request from a branch named like one, and its -x lines and its list of
// deferred conflicts are what check believes. Look-alikes come back as
// warnings.
func batchPull(gh github, prs []pullRequest, l line) (*pullRequest, []string, error) {
var found []*pullRequest
var warnings []string
for i := range prs {
pr := &prs[i]
m := batchBranchRE.FindStringSubmatch(pr.Head.Ref)
if m == nil || m[1] != l.branch() || pr.Base.Ref != l.branch() {
continue
}
switch {
case pr.Head.Repo == nil || pr.Head.Repo.FullName != pr.Base.Repo.FullName:
warnings = append(warnings, fmt.Sprintf("ignoring #%d: its batch branch is in a fork", pr.Number))
case checkSHA(pr.Head.SHA) != nil:
warnings = append(warnings, fmt.Sprintf("ignoring #%d: GitHub reports no head commit for it", pr.Number))
default:
why, err := trusted(gh, *pr)
if err != nil {
return nil, warnings, fmt.Errorf("batch pull request #%d: %w", pr.Number, err)
}
if why != "" {
warnings = append(warnings, fmt.Sprintf("ignoring #%d: %s", pr.Number, why))
continue
}
found = append(found, pr)
}
}
switch len(found) {
case 0:
return nil, warnings, nil
case 1:
return found[0], warnings, nil
}
return nil, warnings, fmt.Errorf("more than one open batch pull request on %s (#%d, #%d); close all but one", l.branch(), found[0].Number, found[1].Number)
}
// batchHead is the head of the batch pull request as fetched here, refused
// unless it is the commit GitHub reports for the pull request.
func batchHead(pr *pullRequest) (string, error) {
if err := checkBranch(pr.Head.Ref); err != nil {
return "", err
}
ref := "refs/remotes/" + remote + "/" + pr.Head.Ref
got, err := lookup(ref)
if err != nil || got == pr.Head.SHA {
return got, err
}
if _, err := git("fetch", "--quiet", "--end-of-options", remote, "+refs/heads/"+pr.Head.Ref+":"+ref); err != nil {
return "", err
}
if got, err = resolve(ref); err != nil {
return "", err
}
if got != pr.Head.SHA {
return "", fmt.Errorf("batch pull request #%d: %s is at %.12s, not at %.12s as GitHub reports; run again", pr.Number, pr.Head.Ref, got, pr.Head.SHA)
}
return got, nil
}
+394
View File
@@ -0,0 +1,394 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"regexp"
"slices"
"strconv"
"strings"
"time"
)
// A reference to a PR, optionally qualified by its repository, and a list of
// them as prose writes one: "#1 and #2", "#1, #2 and #3", "#1, #2, and #3".
const (
prRef = `(?:[\w.-]+/[\w.-]+)?#\d+`
prRefList = prRef + `(?:(?:\s*,\s*(?:and\s+)?|\s+and\s+)` + prRef + `)*`
)
// A release line named in prose, bare or in backticks, as a whole word.
const releaseLineName = `(?:\x60release-\d+\.\d+\x60|release-\d+\.\d+)(?:$|\s|\)|[.,;:](?:\s|$))`
var (
// A hand backport that carries more than one change names all of them in
// its "Backport of" phrase, either as a list or as the "to release-X.Y,
// together with #N" this repository writes for a dependency pulled along.
backportOfRE = regexp.MustCompile(`[Bb]ackport of\s+(` + prRefList + `)` +
`(?:\s+to\s+\x60?release-\d+\.\d+\x60?,?\s+together with\s+(` + prRefList + `))?`)
// What a list of references may be followed by for every item in it to be
// an original. Anything else may be the list going on to say something
// about its later items -- "#10, #20 is not included" -- so an
// unterminated list keeps only its first item.
prRefListEndRE = regexp.MustCompile(`^(?:[ \t]*(?:\r?\n|$)|\.(?:\s|$)|\s+to\s+` + releaseLineName + `)`)
prRefRE = regexp.MustCompile(`(?:([\w.-]+/[\w.-]+))?#(\d+)`)
xLineRE = regexp.MustCompile(`^\(cherry picked from commit ([0-9a-f]{40})\)$`)
batchLineRE = regexp.MustCompile(`^` + batchTrailer + `: ([0-9a-f]{40})$`)
trailerRE = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-]*[ \t]*:[ \t]`)
paragraphRE = regexp.MustCompile(`\n[ \t]*\n`)
)
// batchTrailer names, in the merge commit a batch makes for an original, the
// merge commit on main it carries.
const batchTrailer = "Backported-from"
// trailerBlock is the last paragraph of a message when every line of it is a
// trailer, a trailer's continuation or an -x line, which is where git writes
// both; nil otherwise. The same lines anywhere else are prose, which anyone
// can write. interpret-trailers does not report -x lines, so the block is read
// here the way git finds it.
func trailerBlock(message string) []string {
var paras []string
for _, p := range paragraphRE.Split(strings.ReplaceAll(message, "\r\n", "\n"), -1) {
if strings.TrimSpace(p) != "" {
paras = append(paras, p)
}
}
if len(paras) < 2 {
return nil
}
var block []string
for _, ln := range strings.Split(paras[len(paras)-1], "\n") {
switch t := strings.TrimSpace(ln); {
case t == "":
case xLineRE.MatchString(t):
block = append(block, t)
case strings.HasPrefix(ln, " ") || strings.HasPrefix(ln, "\t"):
// Left out: only the trailer a continuation belongs to is read.
case !trailerRE.MatchString(ln):
return nil
default:
block = append(block, t)
}
}
return block
}
// trailerValues lists what the lines of a message's trailer block that re
// matches name.
func trailerValues(message string, re *regexp.Regexp) []string {
var values []string
for _, ln := range trailerBlock(message) {
if m := re.FindStringSubmatch(ln); m != nil {
values = append(values, m[1])
}
}
return values
}
// xSources reads every -x line of the trailer block, whoever wrote it; only
// appendedSource reads the one git appended.
func xSources(message string) []string { return trailerValues(message, xLineRE) }
// lastParagraph is the last paragraph of a message, line by line, or nil for
// a message of one paragraph.
func lastParagraph(message string) []string {
var paras []string
for _, p := range paragraphRE.Split(strings.ReplaceAll(message, "\r\n", "\n"), -1) {
if strings.TrimSpace(p) != "" {
paras = append(paras, p)
}
}
if len(paras) < 2 {
return nil
}
return strings.Split(paras[len(paras)-1], "\n")
}
// appendedSource is the commit the -x line git appends when it picks names:
// the last -x line of the message's last paragraph, where git writes it, with
// nothing after it but trailers, such as the sign-off --signoff adds. The
// picked commit's own message comes before it, so nothing a pull request's
// author wrote can stand in for it. "" when there is none.
func appendedSource(message string) string {
lines := lastParagraph(message)
for i := len(lines) - 1; i >= 0; i-- {
t := strings.TrimSpace(lines[i])
if m := xLineRE.FindStringSubmatch(t); m != nil {
return m[1]
}
if t != "" && !strings.HasPrefix(lines[i], " ") && !strings.HasPrefix(lines[i], "\t") && !trailerRE.MatchString(lines[i]) {
return ""
}
}
return ""
}
// claimedSources lists what every -x line of a message's last paragraph
// names, the appended one and any the picked commit's message already had.
func claimedSources(message string) []string {
var sources []string
for _, ln := range lastParagraph(message) {
if m := xLineRE.FindStringSubmatch(strings.TrimSpace(ln)); m != nil {
sources = append(sources, m[1])
}
}
return sources
}
// batchedFrom lists the merge commits on main a batch merge says it carries.
func batchedFrom(message string) []string { return trailerValues(message, batchLineRE) }
// bodyOrigins returns the PR numbers named in a message's "Backport of"
// phrases, in order of appearance and without repeats. A reference qualified
// with another repository is not a local number.
func bodyOrigins(body, repo string) []int {
var origins []int
for _, m := range backportOfRE.FindAllStringSubmatchIndex(body, -1) {
for g := 1; 2*g+1 < len(m); g++ {
start, end := m[2*g], m[2*g+1]
if start < 0 {
continue
}
refs := prRefRE.FindAllStringSubmatch(body[start:end], -1)
if !prRefListEndRE.MatchString(body[end:]) {
refs = refs[:1]
}
for _, ref := range refs {
if ref[1] != "" && !strings.EqualFold(ref[1], repo) {
continue
}
n, _ := strconv.Atoi(ref[2])
if n > 0 && !slices.Contains(origins, n) {
origins = append(origins, n)
}
}
}
}
return origins
}
// pickRef is an -x reference: the commit it names, and the commit carrying it.
type pickRef struct{ source, commit string }
// history is what a branch carries beyond a base it shares with main.
//
// Only the commits outside the base supply evidence: shared history can name
// an original without carrying it on this branch. What a commit's text says
// counts only for commits from before the batch-only day: a picked commit
// keeps its message and a batch merge carries the original's, so after that
// day a pull request's author could write any of it. From then on only the
// -x line git appends to a pick counts, which names the commit picked.
type history struct {
head string
reachable map[string]bool
// appended maps a commit to a pick on the branch whose appended -x line
// names it.
appended map[string]string
// What text says, from commits before the batch-only day: every -x line
// of a trailer block, the Backported-from trailers and "Backport of".
picks []pickRef
batches map[string]string
backports map[int]string
// trees caches what a net merge of an original onto head writes, by its
// merge commit: "" for a conflict.
trees map[string]string
}
func readHistory(head, base string, textUntil time.Time) (*history, error) {
h, err := readPicks(head, base, textUntil)
if err != nil {
return nil, err
}
all, err := revList("--end-of-options", head)
if err != nil {
return nil, err
}
for _, oid := range all {
h.reachable[oid] = true
}
return h, nil
}
// readPicks reads only the messages of head's commits outside base, which is
// all a batch branch is asked about; textUntil is the batch-only day.
func readPicks(head, base string, textUntil time.Time) (*history, error) {
h := &history{head: head, reachable: map[string]bool{}, appended: map[string]string{}, batches: map[string]string{}, backports: map[int]string{}, trees: map[string]string{}}
oids, err := revList("--end-of-options", head, "^"+base)
if err != nil {
return nil, err
}
msgs, err := commitMessages(oids)
if err != nil {
return nil, err
}
for _, oid := range oids {
msg := msgs[oid].message
if len(msgs[oid].parents) == 1 {
if source := appendedSource(msg); source != "" {
if _, ok := h.appended[source]; !ok {
h.appended[source] = oid
}
}
}
if !msgs[oid].committed.Before(textUntil) {
continue
}
for _, source := range xSources(msg) {
h.picks = append(h.picks, pickRef{source: source, commit: oid})
}
for _, merge := range batchedFrom(msg) {
if _, ok := h.batches[merge]; !ok {
h.batches[merge] = oid
}
}
for _, n := range bodyOrigins(msg, upstreamRepo) {
if _, ok := h.backports[n]; !ok {
h.backports[n] = oid
}
}
}
return h, nil
}
// pickOf returns the commit whose -x line names source, or "".
func (h *history) pickOf(source string) string {
for _, r := range h.picks {
if r.source == source {
return r.commit
}
}
return ""
}
// found returns the commit that carries an original on the branch and how,
// or "" when none does. A pick counts when the -x line git appended to it
// names the original's merge commit, as for a squash or a pick with -m 1, or
// when such lines name every commit of its own, as a batch merge's picks do.
// From before the batch-only day, text counts too: a Backported-from trailer
// naming the merge commit, or -x lines anywhere in a trailer block naming it
// or every commit of its own.
func (h *history) found(o *original, commits []string) (string, string) {
if c := h.appended[o.merge]; c != "" {
return c, "picked in " + c[:12]
}
if c := coveredBy(h.appended, o, commits); c != "" {
return c, fmt.Sprintf("its %d %s picked, the last in %s", len(commits), plural(len(commits), "commit"), c[:12])
}
if c, ok := h.batches[o.merge]; ok {
return c, "named by a " + batchTrailer + " trailer in " + c[:12]
}
if c := h.pickOf(o.merge); c != "" {
return c, "named by an -x line in " + c[:12]
}
named := map[string]string{}
for _, r := range h.picks {
if _, ok := named[r.source]; !ok {
named[r.source] = r.commit
}
}
if c := coveredBy(named, o, commits); c != "" {
return c, fmt.Sprintf("its %d %s named by -x lines, the last in %s", len(commits), plural(len(commits), "commit"), c[:12])
}
return "", ""
}
// coveredBy returns the commit in by that names the last of a merged pull
// request's own commits when by names every one of them, or "".
func coveredBy(by map[string]string, o *original, commits []string) string {
if o.parents < 2 || len(commits) == 0 {
return ""
}
last := ""
for _, source := range commits {
if last = by[source]; last == "" {
return ""
}
}
return last
}
// carries says how the branch carries an original, or "" when it does not.
// The no-op pick goes last: it is the one check that asks git to merge.
func (h *history) carries(o *original, commits []string) (string, error) {
if h.reachable[o.merge] {
return "merged before the line forked", nil
}
if c, how := h.found(o, commits); c != "" {
return how, nil
}
if c, ok := h.backports[o.number]; ok {
return `named by "Backport of" in ` + c[:12], nil
}
tree, err := h.netTree(o)
if err != nil {
return "", err
}
headTree, err := resolveTree(h.head)
if err != nil {
return "", err
}
if tree == headTree {
return "a pick would change nothing", nil
}
return "", nil
}
// netTree is noopPick's tree for o onto the branch's head, read once.
func (h *history) netTree(o *original) (string, error) {
if tree, ok := h.trees[o.merge]; ok {
return tree, nil
}
tree, _, err := noopPick(h.head, o)
if err == nil {
h.trees[o.merge] = tree
}
return tree, err
}
// noopPick merges the original onto head as one change and returns the tree
// that writes for a clean merge, "" for a conflict, and whether the tree is
// head's own: the content is there, whatever names it.
func noopPick(head string, o *original) (string, bool, error) {
stdout, stderr, code, err := gitStatus("merge-tree", "--write-tree", "--no-messages", "--merge-base="+o.merge+"^1", "--end-of-options", head, o.merge)
switch {
case err != nil:
return "", false, err
case code == 1:
return "", false, nil
case code != 0:
return "", false, fmt.Errorf("git merge-tree for #%d: exit %d: %s", o.number, code, strings.TrimSpace(stderr))
}
tree, _, _ := strings.Cut(stdout, "\n")
if err := checkSHA(tree); err != nil {
return "", false, fmt.Errorf("git merge-tree for #%d: %w", o.number, err)
}
headTree, err := resolveTree(head)
if err != nil {
return "", false, err
}
return tree, tree == headTree, nil
}
func resolveTree(commit string) (string, error) {
out, err := git("rev-parse", "--verify", "--quiet", "--end-of-options", commit+"^{tree}")
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
+333
View File
@@ -0,0 +1,333 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"slices"
"strings"
"testing"
)
// verdictOn prepares the line and returns the verdict on one original.
func verdictOn(t *testing.T, f *fixture, gh *fakeGH, lineName string, n int) *verdict {
t.Helper()
f.git("fetch", "-q", "origin")
l, _ := parseLine(lineName)
if gh == nil {
gh = &fakeGH{}
}
b, err := (&app{gh: gh, stderr: &strings.Builder{}}).prepare(l)
if err != nil {
t.Fatal(err)
}
v := b.find(n)
if v == nil {
t.Fatalf("#%d is not an original for %s", n, lineName)
}
return v
}
func TestPresenceOnTheLine(t *testing.T) {
const fix = "fix(app): the change"
files := map[string]string{"app/x.txt": "fixed\n", "app/y.txt": "also\n"}
cases := []struct {
name string
// land puts something on release-1.1 for the original merged as mc,
// whose own commit is pr.
land func(f *fixture, mc, pr string)
carried string
}{
{
name: "nothing on the line",
land: func(*fixture, string, string) {},
},
{
name: "an -x line naming the merge commit",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", fix+"\n\n(cherry picked from commit "+mc+")", map[string]string{"app/x.txt": "fixed\n"})
},
carried: "picked in",
},
{
name: "an -x line naming the pull request's only commit",
land: func(f *fixture, _, pr string) {
f.onLine("release-1.1", "change\n\n(cherry picked from commit "+pr+")", map[string]string{"app/x.txt": "fixed\n"})
},
carried: "its 1 commit picked",
},
{
name: "an -x line among other trailers",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\nSigned-off-by: A <a@example.invalid>\n(cherry picked from commit "+mc+")\nSigned-off-by: B <b@example.invalid>", map[string]string{"app/x.txt": "fixed\n"})
},
carried: "picked in",
},
{
name: "an abbreviated -x line",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc[:12]+")", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "an -x line in the body, not in the trailers",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc+")\n\nThe rest of it is still to come.", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "an -x line inside a prose paragraph at the end",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\nThis is not (cherry picked from commit "+mc+")\nbut a sentence.", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "a squash-merged hand port that says Backport of, from before the batch-only day",
land: func(f *fixture, _, _ string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
f.onLine("release-1.1", "[Backport release-1.1] fix(app): the change (#77)\n\n# Description\nBackport of #5 to `release-1.1`.", map[string]string{"app/x.txt": "fixed\n"})
},
carried: `named by "Backport of"`,
},
{
name: "the same after the batch-only day",
land: func(f *fixture, _, _ string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T00:00:00Z")
f.onLine("release-1.1", "[Backport release-1.1] fix(app): the change (#77)\n\n# Description\nBackport of #5 to `release-1.1`.", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "an -x line followed by prose in its paragraph",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc+")\nand more words", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "an -x line in a paragraph git reads as trailers that holds prose too",
land: func(f *fixture, mc, _ string) {
f.onLine("release-1.1", "port\n\nFixes the thing.\nSigned-off-by: A <a@example.invalid>\n(cherry picked from commit "+mc+")\nSigned-off-by: B <b@example.invalid>", map[string]string{"app/x.txt": "fixed\n"})
},
carried: "picked in",
},
{
name: "Backport of another repository's number",
land: func(f *fixture, _, _ string) {
f.onLine("release-1.1", "port\n\nBackport of other/repo#5.", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "the same change under another name",
land: func(f *fixture, _, _ string) {
f.onLine("release-1.1", "fix(app): unrelated title", files)
},
carried: "a pick would change nothing",
},
{
name: "half of the change, named nowhere",
land: func(f *fixture, _, _ string) {
f.onLine("release-1.1", "fix(app): half", map[string]string{"app/x.txt": "fixed\n"})
},
},
{
name: "an -x line naming some other commit",
land: func(f *fixture, _, _ string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit 0123456789abcdef0123456789abcdef01234567)", map[string]string{"app/z.txt": "z\n"})
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.pr(5, fix, "", files)
pr := f.git("rev-parse", mc+"^2")
tc.land(f, mc, pr)
v := verdictOn(t, f, nil, "release-1.1", 5)
switch {
case tc.carried == "" && v.rule != 0:
t.Errorf("verdict = rule %d (%s), want it to qualify", v.rule, v.reason)
case tc.carried != "" && (v.rule != ruleOnLine || !strings.HasPrefix(v.reason, tc.carried)):
t.Errorf("verdict = rule %d (%s), want rule 1 with %q", v.rule, v.reason, tc.carried)
}
})
}
}
// History the line shares with main is not evidence: a commit from before
// the line branched can say "Backport of #5" without the line carrying #5.
func TestSharedHistoryIsNotEvidence(t *testing.T) {
f := newFixture(t)
f.release("release-1.0")
f.pr(1, "docs(app): plan", "Backport of #5 is planned.", map[string]string{"app/plan.txt": "p\n"})
f.release("release-1.1")
f.pr(5, "fix(app): the change", "", map[string]string{"app/x.txt": "fixed\n"})
if v := verdictOn(t, f, nil, "release-1.1", 5); v.rule != 0 {
t.Errorf("verdict = rule %d (%s), want it to qualify", v.rule, v.reason)
}
}
// Only a batch pull request opened from this repository by someone with write
// access, or by the bot the batch runs as, speaks for the batch.
func TestCarriedByTheBatch(t *testing.T) {
cases := []struct {
name string
edit func(pr *pullRequest, gh *fakeGH)
carried bool
notAsked bool // the author's permission must not be looked up
}{
{name: "a maintainer with write access", carried: true},
{name: "an admin", edit: func(_ *pullRequest, gh *fakeGH) { gh.perms = map[string]string{"maintainer": "admin"} }, carried: true},
{name: "a private member reported as a contributor, with write access",
edit: func(pr *pullRequest, _ *fakeGH) { pr.AuthorAssociation = "CONTRIBUTOR" }, carried: true},
{name: "an organization member with read access",
edit: func(pr *pullRequest, gh *fakeGH) { gh.perms = map[string]string{"maintainer": "read"} }},
{name: "an invited collaborator with read access", edit: func(pr *pullRequest, gh *fakeGH) {
pr.AuthorAssociation = "COLLABORATOR"
gh.perms = map[string]string{"maintainer": "read"}
}},
{name: "no access", edit: func(_ *pullRequest, gh *fakeGH) { gh.perms = map[string]string{"maintainer": "none"} }},
{name: "the batch's bot", edit: func(pr *pullRequest, gh *fakeGH) {
gh.bot = "cozystack-ci[bot]"
pr.User.Login, pr.User.Type, pr.AuthorAssociation = "cozystack-ci[bot]", "Bot", "CONTRIBUTOR"
// The bot writes the record into every body it opens.
l, _ := parseLine("release-1.1")
pr.Body = deferredBlock(l, nil, nil, nil)
}, carried: true, notAsked: true},
{name: "the batch's bot when no bot is configured", edit: func(pr *pullRequest, _ *fakeGH) {
pr.User.Login, pr.User.Type = "cozystack-ci[bot]", "Bot"
}, notAsked: true},
{name: "GitHub Actions' bot", edit: func(pr *pullRequest, gh *fakeGH) {
gh.bot = "cozystack-ci[bot]"
pr.User.Login, pr.User.Type, pr.AuthorAssociation = "github-actions[bot]", "Bot", "NONE"
}, notAsked: true},
{name: "another bot", edit: func(pr *pullRequest, gh *fakeGH) {
gh.bot = "cozystack-ci[bot]"
pr.User.Login, pr.User.Type = "renovate[bot]", "Bot"
}, notAsked: true},
{name: "a user named like the bot", edit: func(pr *pullRequest, gh *fakeGH) {
gh.bot = "cozystack-ci[bot]"
pr.User.Login, pr.User.Type = "cozystack-ci[bot]", "User"
}, notAsked: true},
{name: "a login shaped like a path", edit: func(pr *pullRequest, _ *fakeGH) { pr.User.Login = "maintainer/../../orgs/x" }, notAsked: true},
{name: "a fork whose branch is named like a batch", edit: func(pr *pullRequest, _ *fakeGH) { pr.Head.Repo.FullName = "someone/cozystack" }, notAsked: true},
{name: "a deleted fork", edit: func(pr *pullRequest, _ *fakeGH) { pr.Head.Repo = nil }, notAsked: true},
{name: "a branch for another line", edit: func(pr *pullRequest, _ *fakeGH) { pr.Head.Ref = "backport-batch/release-1.0-20261026" }, notAsked: true},
{name: "a branch not named for a batch", edit: func(pr *pullRequest, _ *fakeGH) { pr.Head.Ref = "my-backports" }, notAsked: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.pr(5, "fix(app): the change", "", map[string]string{"app/x.txt": "fixed\n"})
f.git("switch", "-q", "-c", "batch", "origin/release-1.1")
f.git("cherry-pick", "-x", "-m", "1", mc)
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
pr := f.batchPR(90, "release-1.1", batchBranch1, "")
gh := &fakeGH{}
if tc.edit != nil {
tc.edit(&pr, gh)
}
gh.prs = []pullRequest{pr}
v := verdictOn(t, f, gh, "release-1.1", 5)
if (v.onBatch != "") != tc.carried {
t.Errorf("onBatch = %q, want carried=%v", v.onBatch, tc.carried)
}
if tc.notAsked && len(gh.asked) > 0 {
t.Errorf("asked GitHub for the permission of %q", gh.asked)
}
})
}
}
// The batch head is the one GitHub reports: a branch that moved since is read
// again, and one that does not match even then is refused.
func TestBatchHeadMustBeGitHubs(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): the change", "", map[string]string{"app/x.txt": "fixed\n"})
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
pr := f.batchPR(90, "release-1.1", batchBranch1, "")
pr.Head.SHA = strings.Repeat("a", 40)
f.git("fetch", "-q", "origin")
l, _ := parseLine("release-1.1")
_, err := (&app{gh: &fakeGH{prs: []pullRequest{pr}}, stderr: &strings.Builder{}}).prepare(l)
if err == nil || !strings.Contains(err.Error(), "as GitHub reports") {
t.Errorf("prepare = %v, want a refusal of the moved head", err)
}
}
func TestTwoBatchPullRequestsAreRefused(t *testing.T) {
f := newFixture(t)
f.lines()
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/backport-batch/release-1.1-20261022")
gh := &fakeGH{prs: []pullRequest{
f.batchPR(90, "release-1.1", batchBranch1, ""),
f.batchPR(91, "release-1.1", "backport-batch/release-1.1-20261022", ""),
}}
code, _, stderr := f.exec(gh, "check", "--line", "release-1.1")
if code != 2 || !strings.Contains(stderr, "more than one open batch") {
t.Errorf("check = %d (%s), want 2", code, stderr)
}
}
// A permission GitHub does not answer fails the run: read as a refusal, it
// would pass over the open batch, and the run would open a second one.
func TestUnreadablePermissionFailsTheRun(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.pr(5, "fix(app): the change", "", map[string]string{"app/x.txt": "fixed\n"})
const opened = "backport-batch/release-1.1-20261022"
f.git("switch", "-q", "-c", "batch", "origin/release-1.1")
f.git("cherry-pick", "-x", "-m", "1", mc)
f.git("push", "-q", "origin", "HEAD:refs/heads/"+opened)
f.git("switch", "-q", "main")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", opened, "")}, permErr: errString("HTTP 502")}
for _, args := range [][]string{
{"run", "--line", "release-1.1", "--continue", "--push"},
{"check", "--line", "release-1.1"},
{"verify", "--line", "release-1.1"},
} {
if code, _, stderr := f.exec(gh, args...); code != 2 || !strings.Contains(stderr, "batch pull request #90: HTTP 502") {
t.Errorf("%q = %d (%s), want 2 naming the read that failed", args, code, stderr)
}
}
if len(gh.created) != 0 || len(gh.edited) != 0 || f.remoteHead(batchBranch1) != "" {
t.Errorf("opened %q, edited %q and pushed %q; want nothing written", gh.created, gh.edited, f.remoteHead(batchBranch1))
}
}
func TestBodyOrigins(t *testing.T) {
cases := []struct {
body string
want []int
}{
{"Backport of #4253 to `release-1.6`.", []int{4253}},
{"Backport of #1, #2, and #3", []int{1, 2, 3}},
{"Backport of #4253 to release-1.6, together with #3460", []int{4253, 3460}},
{"Backport of #10, #20 is not included", []int{10}},
{"Backport of cozystack/cozystack#7.", []int{7}},
{"Backport of other/repo#7.", nil},
{"Fixes #12; see #13", nil},
}
for _, tc := range cases {
if got := bodyOrigins(tc.body, upstreamRepo); !slices.Equal(got, tc.want) {
t.Errorf("bodyOrigins(%q) = %v, want %v", tc.body, got, tc.want)
}
}
}
+239
View File
@@ -0,0 +1,239 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestCheckBranch(t *testing.T) {
f := newFixture(t)
f.git("branch", "elsewhere")
f.git("switch", "-q", "elsewhere")
f.git("switch", "-q", "main")
for _, name := range []string{"backport-batch/release-1.6-20261026", "release-1.6"} {
if err := checkBranch(name); err != nil {
t.Errorf("checkBranch(%q) = %v, want nil", name, err)
}
}
// @{-1} is a valid argument to check-ref-format --branch, which answers
// with the branch it stands for.
for _, name := range []string{"", "-x", "--upload-pack=evil", "@{-1}", "a..b", "a b", "line\nbreak", "esc\x1b", "a~1", "a:b", "refs/heads/../x"} {
if err := checkBranch(name); err == nil {
t.Errorf("checkBranch(%q) accepted it", name)
}
}
}
// A no from git is an answer; git failing is an error, never a no.
func TestGitTellsAnAnswerFromAFailure(t *testing.T) {
f := newFixture(t)
head := f.git("rev-parse", "HEAD")
missing := strings.Repeat("1", 40)
if got, err := lookup(head); got != head || err != nil {
t.Errorf("lookup(HEAD) = %q, %v", got, err)
}
for _, rev := range []string{missing, "refs/heads/nowhere", head + "^{tree}"} {
if got, err := lookup(rev); got != "" || err != nil {
t.Errorf("lookup(%q) = %q, %v; want no commit and no error", rev, got, err)
}
}
if ok, err := isAncestor(head, head); !ok || err != nil {
t.Errorf("isAncestor(HEAD, HEAD) = %v, %v", ok, err)
}
if _, err := isAncestor(missing, head); err == nil {
t.Error("isAncestor of a commit this repository does not have answered instead of failing")
}
if got, err := lineTemplate(head); err != nil || !strings.HasPrefix(got, "## What this PR does") {
t.Errorf("lineTemplate without a template = %q, %v; want the default", got, err)
}
f.write(".github/PULL_REQUEST_TEMPLATE.md", "## The line's own\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "chore: template")
if got, err := lineTemplate(f.git("rev-parse", "HEAD")); err != nil || got != "## The line's own\n" {
t.Errorf("lineTemplate = %q, %v; want the line's own", got, err)
}
t.Chdir(t.TempDir())
t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(t.TempDir()))
if _, err := lookup(head); err == nil {
t.Error("lookup outside a repository answered instead of failing")
}
if _, err := lineTemplate(head); err == nil {
t.Error("lineTemplate outside a repository gave the default instead of failing")
}
}
func TestParseLine(t *testing.T) {
for _, ok := range []string{"release-1.6", "release-10.0"} {
if _, err := parseLine(ok); err != nil {
t.Errorf("parseLine(%q) = %v", ok, err)
}
}
for _, bad := range []string{"", "-x", "--line", "release-1.6\n", "release-01.6", "release-1.6.1", "release-1.6-rc", " release-1.6", "origin/release-1.6", "release-1.6;rm"} {
if _, err := parseLine(bad); err == nil {
t.Errorf("parseLine(%q) accepted it", bad)
}
}
}
// What the command line, GitHub or a pull request supplies never reaches git
// as an option.
func TestOptionInjection(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
canary := filepath.Join(t.TempDir(), "canary")
for _, args := range [][]string{
{"run", "--line", "-x"},
{"run", "--line", "--upload-pack=touch " + canary},
{"run", "--line", "release-1.1", "--onto", "--upload-pack=touch " + canary},
{"run", "--line", "release-1.1", "--date", "-1"},
{"run", "--line", "release-1.1", "--date", "2026-10-26\n"},
{"run", "--line", "release-1.1", "stray"},
{"check", "--line", "release-1.1;touch " + canary},
{"fold", "--line", "release-1.1", "--original", "-5", "--commit", strings.Repeat("a", 40)},
{"fold", "--line", "release-1.1", "--original", "5", "--commit", "--output=" + canary},
} {
if code, _, _ := f.exec(nil, args...); code != 2 {
t.Errorf("%q = %d, want 2", args, code)
}
}
if _, err := os.Stat(canary); err == nil {
t.Error("an argument ran as a command")
}
// A batch pull request whose head GitHub reports as an option is ignored.
for _, ref := range []string{"--upload-pack=touch " + canary, "-backport-batch/release-1.1-20261026", "backport-batch/release-1.1-20261026\n"} {
pr := f.batchPR(90, "release-1.1", batchBranch1, "")
pr.Head.Ref, pr.Head.SHA = ref, strings.Repeat("a", 40)
code, _, stderr := f.exec(&fakeGH{prs: []pullRequest{pr}}, "check", "--line", "release-1.1")
if code != 1 {
t.Errorf("head %q: check = %d (%s), want the pull request ignored and #5 missing", ref, code, stderr)
}
}
if _, err := os.Stat(canary); err == nil {
t.Error("a head ref ran as a command")
}
}
// A file name reads the same in the deferred block as in the list above it:
// no bidi override reorders it and no zero-width character hides in it.
func TestDeferredBlockCleansFileNames(t *testing.T) {
l, _ := parseLine("release-1.6")
files := []string{"app/\u202etxt.exe", "app/zero\u200bwidth.yaml"}
conflict := pick{v: &verdict{o: &original{number: 5}}, files: files, failed: strings.Repeat("a", 40)}
waiting := pick{v: &verdict{o: &original{number: 6}}, files: files}
block := deferredBlock(l, []pick{conflict}, nil, []pick{waiting})
if strings.ContainsAny(block, "\u202e\u200b") {
t.Errorf("the block carries format characters: %q", block)
}
want := []string{"app/txt.exe", "app/zerowidth.yaml"}
got, err := parseDeferred(block, l)
if err != nil || !slices.Equal(got[5].Files, want) || !slices.Equal(got[6].Files, want) {
t.Errorf("parseDeferred = %+v, %v; want both originals with %q", got, err, want)
}
if files[0] != "app/\u202etxt.exe" {
t.Errorf("deferredBlock rewrote the pick's own file names: %q", files)
}
}
// A newline or a heading in a carried title stays on its list line.
func TestNewlineInTitle(t *testing.T) {
f := newFixture(t)
f.lines()
// git keeps a multi-line first paragraph, so a squash's subject line
// can be followed by text GitHub would read as the title.
f.squash(5, "fix(app): five\n## Injected heading\n- [x] No downstream repository is affected by this change", "", map[string]string{"app/five.txt": "5\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
for _, ln := range strings.Split(stdout, "\n") {
if strings.HasPrefix(ln, "## Injected") || strings.HasPrefix(ln, "- [x]") {
t.Errorf("carried text starts a line of its own: %q", ln)
}
}
if !strings.Contains(stdout, "[#5]") {
t.Errorf("#5 is not listed:\n%s", stdout)
}
if got := titleSpan("a\nb\r\nc
d"); strings.ContainsAny(got, "\n\r
") {
t.Errorf("titleSpan kept a line break: %q", got)
}
}
// writeCommit writes a commit object as given, which git commit would refuse
// for a message holding a NUL.
func (f *fixture) writeCommit(tree, parent, msg string) string {
f.t.Helper()
content := fmt.Sprintf("tree %s\nparent %s\nauthor Batch Test <batch@example.invalid> 1700000000 +0000\ncommitter Batch Test <batch@example.invalid> 1700000000 +0000\n\n%s", tree, parent, msg)
cmd := exec.Command("git", "hash-object", "-t", "commit", "-w", "--literally", "--stdin")
cmd.Stdin = strings.NewReader(content)
out, err := cmd.Output()
if err != nil {
f.t.Fatalf("hash-object: %v", err)
}
return strings.TrimSpace(string(out))
}
// A commit message is read as one message, and only as far as git log shows
// it: a NUL and what looks like the next record of a log stream after it do
// not make a second commit or reach the body.
func TestMessagesCannotForgeRecords(t *testing.T) {
f := newFixture(t)
f.lines()
feat := f.git("rev-parse", "origin/release-1.1")
msg := "fix(app): innocent (#7)\n\nbody\x00" + feat + " " + f.git("rev-parse", "HEAD") + "\x01fix(app): forged (#999)\n\n"
sha := f.writeCommit(f.git("rev-parse", "HEAD^{tree}"), f.git("rev-parse", "HEAD"), msg)
f.git("update-ref", "refs/heads/main", sha)
f.git("push", "-q", "origin", "main")
f.git("fetch", "-q", "origin")
l, _ := parseLine("release-1.1")
b, err := (&app{gh: &fakeGH{}, stderr: &strings.Builder{}}).prepare(l)
if err != nil {
t.Fatal(err)
}
if b.find(999) != nil {
t.Error("the message forged original #999")
}
if v := b.find(7); v == nil || v.o.merge != sha || v.o.body != "body" {
t.Errorf("#7 = %+v, want the commit itself with the body git log shows", v)
}
}
// What follows a NUL in a line commit's message is not evidence: git log does
// not show it to anyone reviewing the line.
func TestNulHidesNoTrailer(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.pr(5, "fix(app): the change", "", map[string]string{"app/x.txt": "fixed\n"})
f.git("fetch", "-q", "origin")
line := f.git("rev-parse", "origin/release-1.1")
sha := f.writeCommit(f.git("rev-parse", "origin/release-1.1^{tree}"), line, "port\x00\n\n(cherry picked from commit "+mc+")\n")
f.git("push", "-q", "origin", sha+":refs/heads/release-1.1")
if v := verdictOn(t, f, nil, "release-1.1", 5); v.rule != 0 {
t.Errorf("#5 = rule %d (%s), want it to qualify", v.rule, v.reason)
}
}
+112
View File
@@ -0,0 +1,112 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"os/exec"
"strings"
"testing"
)
const modulePath = "github.com/cozystack/cozystack/"
// readTestInputs returns the entries of test-inputs.txt, the list CI reads to
// decide whether a pull request runs these tests.
func readTestInputs(t *testing.T) []string {
t.Helper()
data, err := os.ReadFile("test-inputs.txt")
if err != nil {
t.Fatal(err)
}
var entries []string
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
entries = append(entries, line)
}
if len(entries) == 0 {
t.Fatal("test-inputs.txt lists no path")
}
return entries
}
// covered reports whether a change to path matches an entry the way the plan
// job matches it: a trailing slash is a directory prefix, anything else is
// one file.
func covered(entries []string, path string) bool {
for _, e := range entries {
if strings.HasSuffix(e, "/") && strings.HasPrefix(path, e) || path == e {
return true
}
}
return false
}
// A pull request that changes an input of these tests has to run them, and CI
// knows the inputs only from test-inputs.txt. An import added here, or added to
// a package this tool imports, lands in a pull request that touches a listed
// path, so it runs this test, which fails until the new package is listed.
func TestInputsListed(t *testing.T) {
entries := readTestInputs(t)
out, err := exec.Command("go", "list", "-deps", "-test", "-f", "{{.ImportPath}}", ".").Output()
if err != nil {
t.Fatalf("go list: %v", err)
}
var deps int
for _, pkg := range strings.Split(strings.TrimSpace(string(out)), "\n") {
// A test variant prints as "pkg [pkg.test]"; the synthesized main
// package as "pkg.test". Both are this directory.
pkg, _, _ = strings.Cut(pkg, " ")
pkg = strings.TrimSuffix(pkg, ".test")
rel, ok := strings.CutPrefix(pkg, modulePath)
if !ok {
continue
}
deps++
if !covered(entries, rel+"/x.go") {
t.Errorf("these tests import %s, which test-inputs.txt does not list: add %s/", pkg, rel)
}
}
if deps == 0 {
t.Fatalf("go list named no package of %s, so nothing was checked:\n%s", modulePath, out)
}
for _, page := range []string{policyPage, "docs/release/backport-tasks/x.md"} {
if !covered(entries, page) {
t.Errorf("the tests read %s, which test-inputs.txt does not cover", page)
}
}
for _, f := range []string{"go.mod", "go.sum"} {
if !covered(entries, f) {
t.Errorf("test-inputs.txt does not list %s", f)
}
}
}
// A listed path that no longer exists makes the list read as broader than it
// is, and usually means a dependency moved and its new home is missing.
func TestInputsExist(t *testing.T) {
for _, e := range readTestInputs(t) {
if _, err := os.Stat("../../" + e); err != nil {
t.Errorf("test-inputs.txt lists %s: %v", e, err)
}
}
}
+252
View File
@@ -0,0 +1,252 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"regexp"
"slices"
"sort"
"strconv"
"strings"
"time"
)
var (
releaseLineRE = regexp.MustCompile(`^release-(\d+)\.(\d+)$`)
stableTagRE = regexp.MustCompile(`^refs/tags/v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`)
prNumberRE = regexp.MustCompile(`\(#(\d+)\)\s*$`)
mergePRRE = regexp.MustCompile(`^Merge pull request #(\d+) from \S+$`)
)
// line is a release line, i.e. a major.minor pair with a release-X.Y branch.
type line struct{ major, minor int }
func (l line) branch() string { return fmt.Sprintf("release-%d.%d", l.major, l.minor) }
func (l line) less(o line) bool {
if l.major != o.major {
return l.major < o.major
}
return l.minor < o.minor
}
// parseLine accepts exactly the branch name of a line: release-01.6 parses to
// the same numbers as release-1.6 without being that branch.
func parseLine(s string) (line, error) {
m := releaseLineRE.FindStringSubmatch(s)
if m == nil {
return line{}, fmt.Errorf("not a release line: %q (expected release-<major>.<minor>)", s)
}
major, err1 := strconv.Atoi(m[1])
minor, err2 := strconv.Atoi(m[2])
l := line{major, minor}
if err1 != nil || err2 != nil || l.branch() != s {
return line{}, fmt.Errorf("not a release line: %q (expected release-<major>.<minor>)", s)
}
return l, nil
}
// releaseLines returns the two-component release branches on the remote,
// oldest first. release-1.6.0-rc.1 and the like are staging branches, not
// maintenance lines, so they are excluded.
func releaseLines() ([]line, error) {
out, err := git("for-each-ref", "--format=%(refname:strip=3)", "refs/remotes/"+remote+"/release-*")
if err != nil {
return nil, err
}
var lines []line
for name := range strings.FieldsSeq(out) {
if l, err := parseLine(name); err == nil {
lines = append(lines, l)
}
}
sort.Slice(lines, func(i, j int) bool { return lines[i].less(lines[j]) })
return lines, nil
}
// shippedLines reads which lines have a stable release on the remote: a
// vX.Y.Z tag with no pre-release suffix, which only the finalize job of
// pull-requests-release.yaml creates, when the promoted release merges.
func shippedLines() (map[line]bool, error) {
out, err := git("ls-remote", "--tags", "--refs", "--end-of-options", remote, "refs/tags/v*")
if err != nil {
return nil, err
}
shipped := map[line]bool{}
for ln := range strings.Lines(out) {
_, ref, _ := strings.Cut(strings.TrimSpace(ln), "\t")
m := stableTagRE.FindStringSubmatch(ref)
if m == nil {
continue
}
major, err1 := strconv.Atoi(m[1])
minor, err2 := strconv.Atoi(m[2])
if err1 == nil && err2 == nil {
shipped[line{major, minor}] = true
}
}
return shipped, nil
}
// supportedLines picks from all, oldest first, the lines the batch runs for:
// the newest released lines that have shipped a stable release, and every
// line branched after the newest of them, whose rc window is open.
func supportedLines(all []line, shipped map[line]bool, released int) []line {
var out []line
kept := 0
for i := len(all) - 1; i >= 0 && kept < released; i-- {
switch {
case shipped[all[i]]:
kept++
case kept > 0:
// Older than a release and never released itself: abandoned.
continue
}
out = append(out, all[i])
}
slices.Reverse(out)
return out
}
// batchLines are the lines the batch runs for, oldest first.
func batchLines(released int) ([]line, error) {
all, err := releaseLines()
if err != nil {
return nil, err
}
if len(all) == 0 {
return nil, fmt.Errorf("no release-X.Y branches on %s", remote)
}
shipped, err := shippedLines()
if err != nil {
return nil, err
}
return supportedLines(all, shipped, released), nil
}
// newerLine returns the line after want among lines, oldest first, and nil
// for the newest. Rule 6 of the policy holds a line to what the next newer one
// carries, and the batch runs for no line outside them.
func newerLine(want line, lines []line) (*line, error) {
i := slices.Index(lines, want)
switch {
case i < 0:
names := make([]string, len(lines))
for j, l := range lines {
names[j] = l.branch()
}
return nil, fmt.Errorf("%s is not a line the batch runs for; it runs for %s", want.branch(), strings.Join(names, ", "))
case i == len(lines)-1:
return nil, nil
}
return &lines[i+1], nil
}
// original is a pull request merged into main, read from its merge commit.
//
// The title and body come from the commit message, not from GitHub: the
// commit holds them as they were when a maintainer merged the pull request,
// while the live pull request stays editable by its author.
type original struct {
number int
title string
body string
merge string
parents int
merged time.Time
}
func (o *original) url() string {
return fmt.Sprintf("https://github.com/%s/pull/%d", upstreamRepo, o.number)
}
// mainMerges reads main's first-parent history after base, oldest first: one
// original per pull request merge, and the commits that name no pull request.
func mainMerges(base, mainHead string) ([]*original, []string, error) {
out, err := git("rev-list", "--first-parent", "--reverse", "--parents", "--end-of-options", base+".."+mainHead)
if err != nil {
return nil, nil, err
}
type merge struct {
oid string
parents int
}
var list []merge
var oids []string
for ln := range strings.Lines(out) {
f := strings.Fields(ln)
for _, sha := range f {
if checkSHA(sha) != nil {
return nil, nil, fmt.Errorf("rev-list: %q is not a commit", sha)
}
}
if len(f) < 2 {
return nil, nil, fmt.Errorf("rev-list: %s has no parent", f[0])
}
list = append(list, merge{f[0], len(f) - 1})
oids = append(oids, f[0])
}
msgs, err := commitMessages(oids)
if err != nil {
return nil, nil, err
}
var merges []*original
var other []string
for _, m := range list {
o := &original{merge: m.oid, parents: m.parents, merged: msgs[m.oid].committed}
o.number, o.title, o.body = mergedPR(msgs[m.oid].message)
if o.number <= 0 || o.parents > 2 {
other = append(other, m.oid)
continue
}
merges = append(merges, o)
}
return merges, other, nil
}
// mergedPR reads the pull request a commit on main's first-parent line
// merged from its message, with its title and body; 0 when it names none.
func mergedPR(message string) (int, string, string) {
// The subject is the first paragraph, its lines joined, as git reads it.
subject, body, _ := strings.Cut(strings.TrimRight(message, "\n"), "\n\n")
subject = strings.ReplaceAll(subject, "\n", " ")
number, title := 0, ""
if pr := mergePRRE.FindStringSubmatch(subject); pr != nil {
number, _ = strconv.Atoi(pr[1])
body = strings.TrimLeft(body, "\n")
title, body, _ = strings.Cut(body, "\n")
} else if pr := prNumberRE.FindStringSubmatchIndex(subject); pr != nil {
number, _ = strconv.Atoi(subject[pr[2]:pr[3]])
title = strings.TrimSpace(subject[:pr[0]])
}
return number, title, strings.TrimLeft(body, "\n")
}
// prCommits lists what an original brings to a line, one pick each: for a
// merged pull request every commit of its own, oldest first, leaving out the
// merges that brought main into its branch, whose content main already has;
// for a squash, its one commit on main.
func prCommits(o *original) ([]string, error) {
if o.parents < 2 {
return []string{o.merge}, nil
}
oids, err := revList("--reverse", "--topo-order", "--no-merges", "--end-of-options", o.merge+"^1.."+o.merge+"^2")
if err != nil {
return nil, fmt.Errorf("reading the commits of #%d: %w", o.number, err)
}
return oids, nil
}
+581
View File
@@ -0,0 +1,581 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Command backport-batch assembles the automatic backport batch for a release
// line and gates a patch cut on it. docs/release/backports.md is the policy it
// implements; the README describes the commands.
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
const usageText = `Assemble and check the automatic backport batch for a release line.
Usage:
backport-batch run --line release-X.Y [--onto SHA] [--date YYYY-MM-DD] [--no-workflow-files] [--push] [--policy REF]
backport-batch run --line release-X.Y --continue [--no-workflow-files] [--push] [--policy REF]
backport-batch run --line release-X.Y --defer N[,N] --reason TEXT [--push] [--policy REF]
backport-batch run --line release-X.Y --drop N[,N] --reason TEXT [--push] [--policy REF]
backport-batch run --line release-X.Y --undrop N[,N] [--push] [--policy REF]
backport-batch check --line release-X.Y [--cut] [--policy REF]
backport-batch fold --line release-X.Y --original N --commit SHA [--policy REF]
backport-batch verify --line release-X.Y [--head SHA]
backport-batch notify --line release-X.Y [--mention org/team|login] [--push]
backport-batch report --pr N [--id ID] [--json]
backport-batch lines [--policy REF]
run Builds backport-batch/<line>-<yyyymmdd> in this working tree, off
origin/<line>, or on top of the pushed batch branch whose head is
--onto. Each original goes in as its own commits, picked with -x, and
a merge commit naming it; a squash-merged one as one pick. --continue
takes the head from the open batch PR, and starts a batch when none is
open. Prints the pull request body on stdout.
--no-workflow-files leaves an original that changes .github/workflows/
to a maintainer, listed on the batch, for a token that cannot push it.
--push pushes the branch, fast-forward only, and opens or updates the
draft batch PR.
--defer rebuilds the open batch off origin/<line> without those
originals and records them as deferred, for --reason: the batch keeps
them out, the next one tries them again. --push replaces the branch,
leased on the head the rebuild started from; when nothing is left it
closes the batch PR instead, after recording why in it.
--drop drops originals from the line for --reason: the line's record
keeps them out of this batch and every later one. One the open batch
carries is taken off it as --defer would; otherwise only the record
changes. --undrop withdraws a drop. Both need an open batch PR whose
record they change.
check Exits 1 while a change the policy takes is neither on the line nor on
the open batch PR, 0 when nothing is, 2 when it cannot tell. With --cut,
the gate before an rc or a patch is cut, it also exits 1 while a change
is only on the open batch PR, which the cut would not carry, or waits
there for a maintainer because it changes workflows.
fold Checks a hand-made conflict resolution for the open batch PR: for a
merged pull request a merge of its picked commits, for a squash one
commit. Prints ok or why not, exiting 0 or 1. It never commits.
verify Checks every original on the batch, from the line's head to --head or
to the open batch PR's head, against that same shape. Exits 0 when all
pass, 1 with the reasons when one does not.
notify Prints what waits on the open batch PR for a maintainer: a batch
open for 3 days or more, its deferred conflicts, and the originals
that change workflows. --push keeps one comment on the PR that says
what waits, mentioning --mention.
report Prints a report batch PR N keeps: the one its body carries, or the
one --id names, read from its comments or its body. --json prints the
report as it is stored. It writes nothing, so it reads merged and
closed batches too.
lines Prints the release lines the batch runs for, newest first.
The policy is docs/release/backports.md. run, check, fold and lines read its
policy block from that page on origin/main, or on --policy REF. Every command
but notify and report fetches origin first. notify reads only GitHub; report
reads GitHub, and for a report that keeps drops also this clone's history,
which has to hold the main and line heads the report was made from: report
never fetches on its own.
BACKPORT_BATCH_BOT names the bot the scheduled batch writes as,
<app>[bot], cozystack-ci[bot] when unset: a batch pull request or a notify
comment it wrote is trusted like a maintainer's. Set and empty, no bot is.
`
const (
remote = "origin"
upstreamRepo = "cozystack/cozystack"
policyURL = "https://github.com/" + upstreamRepo + "/blob/main/docs/release/backports.md"
)
type app struct {
gh github
stdout io.Writer
stderr io.Writer
now func() time.Time
// policyRef is where the policy page is read from; origin/main when empty.
policyRef string
// tool is the commit backport-batch was built from, for the record.
tool string
// unwritten is the first error writing to stdout or stderr. Output goes
// out before a push or a publish, so a run that lost it acts on nothing,
// and one that lost a confirmation afterwards fails.
unwritten error
}
func main() {
catchSIGPIPE()
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now, tool: toolVersion()}
os.Exit(a.main(os.Args[1:]))
}
// catchSIGPIPE makes a write to a closed stdout or stderr fail with EPIPE like
// any other write, so the run stops before it pushes or publishes and says
// why, instead of dying silently. signal.Ignore would do the same here but
// would also leave SIGPIPE ignored in the git and gh children, across exec.
func catchSIGPIPE() {
signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE)
}
func (a *app) main(args []string) int {
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr, failed: &a.unwritten}
code := a.command(args)
if a.unwritten != nil {
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v\n", a.unwritten)
if code == 0 {
code = 2
}
}
return code
}
func (a *app) command(args []string) int {
a.gh = guardedGitHub{github: &permCache{github: a.gh, perms: map[string]string{}}, a: a}
if len(args) == 0 {
_, _ = fmt.Fprint(a.stderr, usageText)
return 2
}
if bot := a.gh.batchBot(); bot != "" && !botLoginRE.MatchString(bot) {
return a.fail(fmt.Errorf("%s %q is not a bot login, <app>[bot]", batchBotEnv, bot))
}
switch args[0] {
case "run":
return a.run(args[1:])
case "check":
return a.check(args[1:])
case "fold":
return a.fold(args[1:])
case "verify":
return a.verify(args[1:])
case "notify":
return a.notify(args[1:])
case "report":
return a.report(args[1:])
case "lines":
return a.lines(args[1:])
case "-h", "--help", "help":
_, _ = fmt.Fprint(a.stdout, usageText)
return 0
}
_, _ = fmt.Fprintf(a.stderr, "unknown command %q\n\n%s", args[0], usageText)
return 2
}
// textWriter keeps output fit for a terminal. Titles, file names and the
// messages git relays come from other people, and a control sequence among
// them would move the cursor or rewrite what was printed before.
type textWriter struct {
w io.Writer
failed *error
}
func (t textWriter) Write(p []byte) (int, error) {
if _, err := io.WriteString(t.w, clean(string(p), true)); err != nil {
if t.failed != nil && *t.failed == nil {
*t.failed = err
}
return 0, err
}
return len(p), nil
}
func (a *app) flags(name string) *flag.FlagSet {
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.SetOutput(a.stderr)
fs.Usage = func() { _, _ = fmt.Fprint(a.stderr, usageText) }
return fs
}
func (a *app) policyFlags(name string) *flag.FlagSet {
fs := a.flags(name)
fs.StringVar(&a.policyRef, "policy", "", "ref to read "+policyPage+" from (default: origin/main)")
return fs
}
// parse parses a subcommand's flags; positional arguments are refused, since
// none is defined and a stray one is a typo for a flag.
func parse(fs *flag.FlagSet, args []string) error {
if err := fs.Parse(args); err != nil {
return err
}
if fs.NArg() > 0 {
return fmt.Errorf("unexpected argument %q", fs.Arg(0))
}
return nil
}
// mayAct refuses any further change once output was lost: the run would act
// on what it printed while nobody read it. Every push and every move of a
// local batch branch go through it, and every GitHub write through
// guardedGitHub.
func (a *app) mayAct() error {
if a.unwritten != nil {
return fmt.Errorf("output was lost: %w; nothing more is pushed or written to GitHub", a.unwritten)
}
return nil
}
func (a *app) fail(err error) int {
_, _ = fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
return 2
}
func (a *app) warn(format string, args ...any) {
_, _ = fmt.Fprintf(a.stderr, "warning: "+format+"\n", args...)
}
// verdict is what the policy says about one original for the line.
type verdict struct {
o *original
rule int // the rule that keeps it out, 0 when it qualifies
reason string
// commits are the commits the original brings, one pick each, and changes
// the files it changes; both are read only for what passes rules 2 and 3,
// and by load for anything else.
commits []string
changes []fileChange
// onBatch is the commit of the open batch pull request that picks it,
// and batched the unit of the open batch that carries it whatever the
// rules now say of it.
onBatch string
batched string
// label is the maintainer override the original carries, if any.
label string
// partial lists what its clean net merge onto the line would leave
// without effect, and park why the batch leaves it out, if it does.
partial []string
park *parkedEntry
// deps is what it builds on that the line lacks.
deps []dependency
// diag is what could not be judged about it, shown whatever became of
// it.
diag []string
}
// batch is everything read about a line before anything is written.
type batch struct {
line line
newer *line
lineHead string
mainHead string
// newerHead is the newer line's head, and policyAt the commit the
// policy was read from.
newerHead string
policyAt string
base string
other []string
verdicts []*verdict
pr *pullRequest
prHead string
// picks is what the open batch pull request carries beyond the line.
picks *history
// src is the line's record and where it was found, and drops its drops
// that name an original of this batch.
src recordSource
drops map[int]droppedEntry
// lineHist and newerHist are what the line and the newer line carry,
// read once for every original judged.
lineHist, newerHist *history
// start is when the line started under the policy, zero for its fork point.
start time.Time
// textUntil is the batch-only day: what a commit's text says counts as
// evidence only on commits from before it.
textUntil time.Time
// partialUnits are the originals whose unit on the open batch leaves
// what it lists without effect.
partialUnits map[*verdict][]string
// What one command read once and asks again: renames by commit pair,
// units by batch head, and how often git merge-file ran.
renameCache map[string]renameSet
unitCache map[string]map[*verdict]string
mergeFiles int
// renameLimit is the -l rename detection runs with; 0, git's widest.
renameLimit int
// brought maps main's commits since the fork point to the first-parent
// commit that brought each, and ancestors says which commits blame
// named are older than the fork point.
brought map[string]string
ancestors map[string]bool
// detecting is how long judging partial units and dependencies took.
detecting time.Duration
depCache map[string][]dependency
// lineCarries says, for a prerequisite the rules keep out, whether the
// line carries it all the same.
lineCarries map[*verdict]bool
}
// fetch moves to the top of the working tree first: every path the batch
// hands git is relative to it.
func fetch() error {
top, err := git("rev-parse", "--show-toplevel")
if err != nil {
return err
}
if err := os.Chdir(strings.TrimSpace(top)); err != nil {
return err
}
_, err = git("fetch", "--quiet", "--end-of-options", remote)
return err
}
// policy reads the policy and says from which commit.
func (a *app) policy(mainHead string) (*policy, string, error) {
at := mainHead
if a.policyRef != "" {
if strings.HasPrefix(a.policyRef, "-") {
return nil, "", fmt.Errorf("--policy %q is not a ref", a.policyRef)
}
var err error
if at, err = resolve(a.policyRef); err != nil {
return nil, "", err
}
}
pol, err := readPolicy(at)
return pol, at, err
}
// prepare reads the line, main and the open batch pull request, each pinned to
// one commit, and judges every original by the policy.
func (a *app) prepare(l line) (*batch, error) {
b := &batch{line: l}
var err error
if b.mainHead, err = resolve("refs/remotes/" + remote + "/main"); err != nil {
return nil, err
}
pol, policyAt, err := a.policy(b.mainHead)
if err != nil {
return nil, err
}
lines, err := batchLines(pol.released)
if err != nil {
return nil, err
}
if b.newer, err = newerLine(l, lines); err != nil {
return nil, err
}
if b.lineHead, err = resolve("refs/remotes/" + remote + "/" + l.branch()); err != nil {
return nil, err
}
b.start, b.textUntil, b.policyAt = pol.starts[l.branch()], pol.batchOnly, policyAt
out, err := git("merge-base", "--end-of-options", b.mainHead, b.lineHead)
if err != nil {
return nil, fmt.Errorf("%s shares no history with main: %w", l.branch(), err)
}
b.base = strings.TrimSpace(out)
merges, other, err := mainMerges(b.base, b.mainHead)
if err != nil {
return nil, err
}
b.other = other
if b.lineHist, err = readHistory(b.lineHead, b.mainHead, b.textUntil); err != nil {
return nil, err
}
if b.newer != nil {
if b.newerHead, err = resolve("refs/remotes/" + remote + "/" + b.newer.branch()); err != nil {
return nil, err
}
if b.newerHist, err = readHistory(b.newerHead, b.mainHead, b.textUntil); err != nil {
return nil, err
}
}
labels, err := overrides(a.gh)
if err != nil {
return nil, err
}
prs, err := a.gh.openPulls(l.branch())
if err != nil {
return nil, err
}
pr, warnings, err := batchPull(a.gh, prs, l)
for _, w := range warnings {
a.warn("%s", w)
}
if err != nil {
return nil, err
}
if pr != nil {
b.pr = pr
if b.prHead, err = batchHead(pr); err != nil {
return nil, err
}
if b.picks, err = readPicks(b.prHead, b.lineHead, b.textUntil); err != nil {
return nil, err
}
}
if b.src, err = findLineRecord(a.gh, l, pr); err != nil {
return nil, err
}
if b.drops, err = b.bindDrops(merges); err != nil {
return nil, err
}
if err := b.waitOrder(merges); err != nil {
return nil, err
}
for _, o := range merges {
v := &verdict{o: o, label: labels[o.number]}
if err := b.judge(v, b.drops); err != nil {
return nil, err
}
b.verdicts = append(b.verdicts, v)
}
if err := b.partialStates(); err != nil {
return nil, err
}
return b, nil
}
// judge judges v again under drops, and finds it on the open batch when the
// policy takes it.
func (b *batch) judge(v *verdict, drops map[int]droppedEntry) error {
j, err := judge(v.o, v.label, b.start, b.lineHist, b.newer, b.newerHist, drops)
if err != nil {
return err
}
*v = *j
if v.rule == 0 && b.picks != nil {
v.onBatch, _ = b.picks.found(v.o, v.commits)
}
return nil
}
// overrides maps each pull request merged into main that carries a backport/
// label to the one that decides; backport/skip wins over backport/include.
func overrides(gh github) (map[int]string, error) {
labels := map[int]string{}
for _, label := range []string{labelInclude, labelSkip} {
numbers, err := gh.labelled(label)
if err != nil {
return nil, err
}
for _, n := range numbers {
labels[n] = label
}
}
return labels, nil
}
// judge applies rules 2, 3, 4, 1 and 6, in the order of what each costs to
// read; the first that keeps the original out is the one reported. A line that
// started under the policy after it branched takes nothing merged before that
// start. backport/skip keeps an original out whatever the rules say, and
// backport/include lifts the start and rules 2 to 4, but not what the line or
// the newer line carries. A maintainer's drop keeps out what the line does
// not carry, include or not.
func judge(o *original, label string, start time.Time, lineHist *history, newer *line, newerHist *history, drops map[int]droppedEntry) (*verdict, error) {
v := &verdict{o: o, label: label}
if label == labelSkip {
v.rule, v.reason = ruleSkipLabel, labelSkip
return v, nil
}
if o.merged.Before(start) && label != labelInclude {
v.rule, v.reason = ruleBeforeStart, "merged "+o.merged.Format(time.DateOnly)
return v, nil
}
if r := typeRule(o.title); r != "" && label != labelInclude {
v.rule, v.reason = ruleType, r
return v, nil
}
if r := breakingRule(o.title, o.body); r != "" && label != labelInclude {
v.rule, v.reason = ruleBreaking, r
return v, nil
}
var err error
if v.changes, err = changedFiles(o.merge+"^1", o.merge); err != nil {
return nil, err
}
if label != labelInclude {
r, err := touchesRule(o, v.changes)
if err != nil {
return nil, err
}
if r != "" {
v.rule, v.reason = ruleTouches, r
return v, nil
}
}
if v.commits, err = prCommits(o); err != nil {
return nil, err
}
how, err := lineHist.carries(o, v.commits)
if err != nil {
return nil, err
}
if how != "" {
v.rule, v.reason = ruleOnLine, how
return v, nil
}
if d, ok := drops[o.number]; ok {
v.rule, v.reason = ruleDropped, fmt.Sprintf("dropped on #%d: %s", d.On, d.Reason)
return v, nil
}
if newer != nil {
how, err := newerHist.carries(o, v.commits)
if err != nil {
return nil, err
}
if how == "" {
v.rule, v.reason = ruleNewer, "not on "+newer.branch()+" yet"
}
}
return v, nil
}
func (b *batch) qualifying() []*verdict {
var out []*verdict
for _, v := range b.verdicts {
if v.rule == 0 {
out = append(out, v)
}
}
return out
}
// load reads what the rules did not need to read for an original: its
// commits and the files it changes.
func (b *batch) load(v *verdict) error {
var err error
if v.changes == nil {
if v.changes, err = changedFiles(v.o.merge+"^1", v.o.merge); err != nil {
return err
}
}
if v.commits == nil {
v.commits, err = prCommits(v.o)
}
return err
}
// findMerge is the original merged as merge commit sha, or nil.
func (b *batch) findMerge(sha string) *verdict {
for _, v := range b.verdicts {
if v.o.merge == sha {
return v
}
}
return nil
}
func (b *batch) find(number int) *verdict {
for _, v := range b.verdicts {
if v.o.number == number {
return v
}
}
return nil
}
+99
View File
@@ -0,0 +1,99 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os/exec"
"regexp"
"slices"
"strings"
"testing"
)
// releaseNoteBlock is the release-note block of a body.
func releaseNoteBlock(t *testing.T, body string) string {
t.Helper()
m := regexp.MustCompile("(?s)```release-note\n(.*?)\n```").FindStringSubmatch(body)
if m == nil {
t.Fatalf("no release-note block in:\n%s", body)
}
return m[1]
}
// A pick that differs from its original keeps its release note out of the
// release-note block, under Check these notes with what to compare: one git
// had to fit to a line that already had part of the change, and one whose
// first commit came out empty because the line had resized the value itself.
func TestChangedOriginalNoteIsSetApart(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.onLine("release-1.1", "fix(app): two, on the line", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.onLine("release-1.1", "fix(chart): two replicas, on the line", map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"})
f.pr(5, "fix(app): two and four", "```release-note\nfix(app): two and four are loud.\n```\n", map[string]string{"app/a.txt": strings.NewReplacer("two", "TWO", "four", "FOUR").Replace(appFile)})
f.prCommits(6, "fix(chart): two replicas and six", "```release-note\nfix(chart): two replicas by default.\n```\n",
change{files: map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"}},
change{files: map[string]string{"app/six.txt": "6\n"}})
f.pr(7, "fix(app): seven", "```release-note\nfix(app): seven is calm.\n```\n", map[string]string{"app/seven.txt": "7\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): two and four (#5)", "fix(chart): two replicas and six (#6)", "fix(app): seven (#7)"}) {
t.Fatalf("picked %q, want #5, #6 and #7", got)
}
notes := releaseNoteBlock(t, stdout)
if !strings.Contains(notes, "seven is calm") || strings.Contains(notes, "two and four are loud") || strings.Contains(notes, "two replicas by default") {
t.Errorf("the release-note block keeps a changed pick's note, or loses #7's:\n%s", notes)
}
_, check, ok := strings.Cut(stdout, "### Check these notes")
check, _, _ = strings.Cut(check, "\n#")
if !ok || !strings.Contains(check, "two and four are loud") || !strings.Contains(check, "two replicas by default") || strings.Contains(check, "seven is calm") {
t.Errorf("Check these notes does not list #5 and #6 alone:\n%s", stdout)
}
if !strings.Contains(stdout, `"changed":[5,6]`) {
t.Errorf("the record or the report does not mark #5 and #6 changed:\n%s", stdout)
}
}
// The command printed for a changed pick pairs the pick with the commit it
// came from, however much the pick lost: the default creation factor leaves
// a pick that carries only #5088's test unpaired.
func TestRangeDiffCommandPairsThePick(t *testing.T) {
f, mc5 := policyWorld(t)
gh := &fakeGH{}
head := handBatch(f, gh, mc5)
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue")
if code != 0 {
t.Fatalf("run --continue = %d\n%s", code, stderr)
}
m := regexp.MustCompile("`git range-diff --creation-factor=999 ([0-9a-f]{12})\\^! ([0-9a-f]{12})\\^!`").FindStringSubmatch(stdout)
if m == nil {
t.Fatalf("the body prints no range-diff for #5's pick:\n%s", stdout)
}
src, pick := f.git("rev-parse", mc5+"^2"), f.git("rev-parse", head+"^2")
if !strings.HasPrefix(src, m[1]) || !strings.HasPrefix(pick, m[2]) {
t.Errorf("the range-diff names %s and %s, want #5's commit %.12s and its pick %.12s", m[1], m[2], src, pick)
}
out, err := exec.Command("git", "range-diff", "--no-color", "--creation-factor=999", m[1]+"^!", m[2]+"^!").Output()
if want := "1: " + f.git("rev-parse", "--short", src) + " ! 1: " + f.git("rev-parse", "--short", pick); err != nil || !strings.HasPrefix(string(out), want) {
t.Errorf("the printed range-diff gives %q, %v; want it to start %q", out, err, want)
}
if out := f.git("range-diff", "--no-color", mc5+"^1.."+mc5+"^2", head+"^1.."+head+"^2"); !strings.Contains(out, " < ") {
t.Errorf("the default creation factor pairs #5's pick after all, so the test shows nothing:\n%s", out)
}
}
+360
View File
@@ -0,0 +1,360 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"regexp"
"slices"
"strings"
"time"
)
// notifyMarker starts the one comment notify keeps on a batch pull request.
const notifyMarker = "<!-- backport-batch:notify -->"
// stalledAfter is how many days a batch stays open before notify calls it
// stalled: the shorter gap between two scheduled runs, Monday to Thursday, so
// a batch the previous run opened or continued is stalled at the next one.
const stalledAfter = 3
var mentionRE = regexp.MustCompile(`^@?([A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:/[A-Za-z0-9_.-]+)?)$`)
// notify reports what waits on the open batch pull request of a line for a
// maintainer: a batch open since before the previous run, the conflicts it
// deferred, and the originals that change workflows. One line per finding goes
// to stdout, for the run's summary. --push writes them into the comment, one
// per pull request, updated in place on every run.
func (a *app) notify(args []string) int {
fs := a.flags("notify")
lineArg := fs.String("line", "", "release line, release-X.Y")
mention := fs.String("mention", "", "team or person the comment mentions, org/team or login, with or without @")
push := fs.Bool("push", false, "write the comment")
if err := parse(fs, args); err != nil {
return 2
}
l, err := parseLine(*lineArg)
if err != nil {
return a.fail(err)
}
if *mention != "" {
m := mentionRE.FindStringSubmatch(*mention)
if m == nil {
return a.fail(fmt.Errorf("--mention %q is not @login or @org/team", *mention))
}
*mention = "@" + m[1]
}
prs, err := a.gh.openPulls(l.branch())
if err != nil {
return a.fail(err)
}
pr, warnings, err := batchPull(a.gh, prs, l)
for _, w := range warnings {
a.warn("%s", w)
}
if err != nil {
return a.fail(err)
}
// With no batch open there is nothing to report on and nowhere to write,
// but a record nobody can read is said all the same.
src, err := findLineRecord(a.gh, l, pr)
if err != nil {
return a.fail(err)
}
if c := src.closingOpen(); c != nil {
return a.fail(fmt.Errorf("batch pull request #%d is being closed; backport-batch run --line %s --push finishes it", c.PR, l.branch()))
}
if pr == nil {
return 0
}
opened, err := branchDate(pr.Head.Ref)
if err != nil {
return a.fail(err)
}
deferred := map[int]deferredEntry{}
var partial []int
var parks []parkedEntry
if src.open {
deferred, partial = src.rec.deferred(), src.rec.Partial
for _, p := range src.rec.Parked {
if !p.Announced {
parks = append(parks, p)
}
}
}
// An original that waits on others is listed once, after what decides
// its wait, where that one is listed.
var conflicts, waiting []int
after := map[int][]int{}
for n, e := range deferred {
switch placement(deferred, n, map[int]bool{}) {
case "workflow":
waiting = append(waiting, n)
case "conflict":
conflicts = append(conflicts, n)
}
if len(e.Needs) > 0 {
after[n] = e.Needs
}
}
slices.Sort(conflicts)
slices.Sort(waiting)
item := func(n int) string {
if needs := after[n]; len(needs) > 0 {
return fmt.Sprintf("\n- %s, after %s", prLink(n), prLinks(needs))
}
return "\n- " + prLink(n)
}
count := func(ns []int) int {
c := 0
for _, n := range ns {
if len(after[n]) == 0 {
c++
}
}
return c
}
today := a.now().UTC().Truncate(24 * time.Hour)
days := int(today.Sub(opened).Hours() / 24)
stalled := days >= stalledAfter
var text []string
if stalled {
_, _ = fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
text = append(text, fmt.Sprintf("This batch has been open since %s, %d days, through at least one scheduled run. Merge it, or close it and the next run starts a new one; until then each run adds what the rules take to it.", opened.Format(time.DateOnly), days))
}
if len(partial) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %d units on #%d carry only part of their originals\n", l.branch(), len(partial), pr.Number)
var b strings.Builder
b.WriteString("These are on the batch but carry only part of their originals on this line; take each out with `backport-batch run --defer` before the cut, and port what applies as a pull request of its own:\n")
for _, n := range partial {
b.WriteString(item(n))
}
text = append(text, b.String())
}
if len(conflicts) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), count(conflicts), pr.Number)
var b strings.Builder
fmt.Fprintf(&b, "These conflicts wait for a maintainer, as [resolving a conflict](%sresolve-conflict.md) describes; unresolved, each goes to the next batch, and so does what waits on one:\n", tasksURL)
for _, n := range conflicts {
b.WriteString(item(n))
}
text = append(text, b.String())
}
if len(waiting) > 0 {
if count(waiting) == len(waiting) {
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
} else {
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s or wait on one that does\n", l.branch(), len(waiting), pr.Number, workflowsDir)
}
var b strings.Builder
fmt.Fprintf(&b, "These change `%s`, which the batch's token may not push, and wait for a maintainer to bring them over, as [changes to workflows](%s#changes-to-workflows) describes, or wait on one that does:\n", workflowsDir, policyURL)
for _, n := range waiting {
b.WriteString(item(n))
}
text = append(text, b.String())
}
if len(parks) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %d parked originals on #%d, named once\n", l.branch(), len(parks), pr.Number)
var b strings.Builder
fmt.Fprintf(&b, "These cannot land on this line whole, so the batch parked them; each is named here once. A maintainer ports what applies as a pull request of its own, as [fixes that exist only on a release line](%s#fixes-that-exist-only-on-a-release-line) describes, or drops the original with `backport-batch run --drop`:\n", policyURL)
for _, p := range parks {
fmt.Fprintf(&b, "\n- %s: %s", prLink(p.Original), parkReason(p))
}
text = append(text, b.String())
}
if !*push {
return 0
}
if code := a.comment(pr, *mention, today, text); code != 0 || len(parks) == 0 {
return code
}
if err := a.announced(pr, src.rec, parks); err != nil {
return a.fail(fmt.Errorf("%w; the next notify names them again", err))
}
return 0
}
// announced marks parks as named in the record of the open batch pull
// request, which the comment just named: only while it is open and its body
// still carries the record notify read, so a run that rewrote the body
// meanwhile loses nothing; the next notify names those parks again.
func (a *app) announced(pr *pullRequest, rec *lineRecord, parks []parkedEntry) error {
marked := *rec
marked.Parked = slices.Clone(rec.Parked)
for i, p := range marked.Parked {
if slices.ContainsFunc(parks, func(q parkedEntry) bool { return q.Original == p.Original && parkEvent(q) == parkEvent(p) }) {
marked.Parked[i].Announced = true
}
}
now, err := a.gh.pull(pr.Number)
if err != nil {
return err
}
if st := prState(&now); st != "open" {
return fmt.Errorf("batch pull request #%d is %s now; nothing is written to it", pr.Number, st)
}
lines, err := fencedLines(now.Body, deferredFence)
if err != nil || len(lines) != 1 {
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
}
read, err := parseRecord(now.Body, notifyLineOf(rec))
if err != nil || recordBlock(*read) != recordBlock(*rec) {
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
}
// GitHub keeps the CRLF line ends a body edited on the web has; the
// record is found, and the body written back, with LF ones.
body := strings.ReplaceAll(now.Body, "\r\n", "\n")
block := deferredFence + "\n" + lines[0] + "\n```"
if strings.Count(body, block) != 1 {
return fmt.Errorf("batch pull request #%d carries the record notify read, but not as one block it can mark", pr.Number)
}
body = strings.Replace(body, block, recordBlock(marked), 1)
name, err := writeTemp("backport-batch-body-*.md", body)
if err != nil {
return err
}
defer func() { _ = os.Remove(name) }()
return a.gh.editBody(pr.Number, name)
}
// notifyLineOf is the line a record is for, which it was read against.
func notifyLineOf(rec *lineRecord) line {
l, _ := parseLine(rec.Line)
return l
}
// placement says under which list notify names original n of the record:
// workflow for a wait for a maintainer, conflict for a deferred conflict, ""
// for neither, a deferral for a reason among them. One that waits on others
// goes where what decides its wait goes, followed down the record; one that
// is not in the record is held by rule 6 or missing, and waits on nobody.
func placement(deferred map[int]deferredEntry, n int, seen map[int]bool) string {
e, ok := deferred[n]
switch {
case !ok || seen[n]:
return ""
case e.Workflows:
return "workflow"
case e.Reason != "":
return ""
case len(e.Needs) == 0:
return "conflict"
}
seen[n] = true
place := ""
for _, f := range e.Needs {
switch p := placement(deferred, f, seen); p {
case "workflow":
return p
case "conflict":
place = p
}
}
return place
}
// comment writes what waits into the one notify comment on the batch pull
// request: it edits the comment if there is one, and opens it only when
// something waits. Only a comment by an author the batch trusts is taken for
// it, so a look-alike someone else wrote is neither edited nor believed.
func (a *app) comment(pr *pullRequest, mention string, today time.Time, text []string) int {
comments, err := a.gh.comments(pr.Number)
if err != nil {
return a.fail(err)
}
var ours *issueComment
for i := range comments {
c := &comments[i]
if !strings.HasPrefix(c.Body, notifyMarker) {
continue
}
why, err := trustedAuthor(a.gh, c.User.Login, c.User.Type)
if err != nil {
return a.fail(err)
}
if why == "" {
ours = c
}
}
if ours == nil && len(text) == 0 {
return 0
}
var b strings.Builder
b.WriteString(notifyMarker + "\n")
if mention != "" && len(text) > 0 {
b.WriteString(mention + "\n\n")
}
if len(text) == 0 {
text = []string{"Nothing waits on a maintainer."}
}
b.WriteString(strings.Join(text, "\n\n"))
fmt.Fprintf(&b, "\n\n<sub>Updated by the backport batch on %s.</sub>\n", today.Format(time.DateOnly))
f, err := os.CreateTemp("", "backport-batch-comment-*.md")
if err != nil {
return a.fail(err)
}
defer func() { _ = os.Remove(f.Name()) }()
if _, err := f.WriteString(b.String()); err != nil {
_ = f.Close()
return a.fail(err)
}
if err := f.Close(); err != nil {
return a.fail(err)
}
if err := a.stillOpen(pr.Number); err != nil {
return a.fail(err)
}
if ours != nil {
err = a.gh.editComment(ours.ID, f.Name())
} else {
err = a.gh.createComment(pr.Number, f.Name())
}
if err != nil {
return a.fail(err)
}
return 0
}
// lines prints the lines the batch runs for, newest first, in the order the
// policy has them processed.
func (a *app) lines(args []string) int {
fs := a.policyFlags("lines")
if err := parse(fs, args); err != nil {
return 2
}
if err := fetch(); err != nil {
return a.fail(err)
}
mainHead, err := resolve("refs/remotes/" + remote + "/main")
if err != nil {
return a.fail(err)
}
pol, _, err := a.policy(mainHead)
if err != nil {
return a.fail(err)
}
lines, err := batchLines(pol.released)
if err != nil {
return a.fail(err)
}
for _, l := range slices.Backward(lines) {
_, _ = fmt.Fprintln(a.stdout, l.branch())
}
return 0
}
+496
View File
@@ -0,0 +1,496 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"errors"
"slices"
"strings"
"testing"
"time"
)
// notifyPR is an open batch pull request for release-1.1 on the branch opened
// on date, YYYYMMDD, with body.
func notifyPR(date, body string) pullRequest {
var pr pullRequest
pr.Number, pr.Body = 90, body
pr.User.Login, pr.User.Type = "maintainer", "User"
pr.Head.Ref, pr.Head.SHA = "backport-batch/release-1.1-"+date, strings.Repeat("a", 40)
pr.Head.Repo = &struct {
FullName string `json:"full_name"`
}{upstreamRepo}
pr.Base.Ref, pr.Base.Repo.FullName = "release-1.1", upstreamRepo
return pr
}
// notifyRun runs notify on 2026-10-26 with gh faked.
func notifyRun(gh *fakeGH, args ...string) (int, string, string) {
var stdout, stderr bytes.Buffer
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
code := a.main(append([]string{"notify", "--line", "release-1.1"}, args...))
return code, stdout.String(), stderr.String()
}
var notifyLine, _ = parseLine("release-1.1")
// A batch open since before the previous run gets one comment, mentioning
// the team, and keeps that one comment however many runs see it.
func TestNotifyStalled(t *testing.T) {
for _, tc := range []struct {
date string
stalled bool
}{
{"20261026", false},
{"20261024", false},
{"20261023", true},
{"20261019", true},
} {
gh := &fakeGH{prs: []pullRequest{notifyPR(tc.date, "")}}
code, stdout, stderr := notifyRun(gh, "--mention", "cozystack/maintainers", "--push")
if code != 0 {
t.Fatalf("%s: notify = %d\n%s", tc.date, code, stderr)
}
if got := strings.Contains(stdout, "release-1.1: batch pull request #90 has been open since"); got != tc.stalled {
t.Errorf("%s: stalled = %v, want %v:\n%s", tc.date, got, tc.stalled, stdout)
}
if got := len(gh.issueComments); got != map[bool]int{true: 1, false: 0}[tc.stalled] {
t.Errorf("%s: %d comments, want one only when stalled", tc.date, got)
}
if !tc.stalled {
continue
}
c := gh.issueComments[0].Body
if !strings.HasPrefix(c, notifyMarker+"\n@cozystack/maintainers\n") || !strings.Contains(c, "open since") {
t.Errorf("%s: the comment does not mention the team and say why:\n%s", tc.date, c)
}
for range 2 {
if code, _, stderr := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push"); code != 0 {
t.Fatalf("notify again = %d\n%s", code, stderr)
}
}
if len(gh.issueComments) != 1 {
t.Errorf("%s: %d comments after three runs, want the one updated", tc.date, len(gh.issueComments))
}
}
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
if code, _, _ := notifyRun(gh, "--push"); code != 0 || strings.Contains(gh.issueComments[0].Body, "@") {
t.Errorf("notify with no team = %d and wrote %q; want no mention", code, gh.issueComments[0].Body)
}
gh = &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
if code, stdout, _ := notifyRun(gh); code != 0 || stdout == "" || len(gh.issueComments) != 0 {
t.Errorf("notify without --push = %d, %q and %d comments; want the line and no comment", code, stdout, len(gh.issueComments))
}
}
// The comment lists the deferred conflicts for a maintainer, and nothing
// --defer held out.
func TestNotifyConflicts(t *testing.T) {
conflict := pick{v: &verdict{o: &original{number: 6}}, files: []string{"app/six.txt"}}
held := pick{v: &verdict{o: &original{number: 7}}, reason: "fails the e2e"}
body := "text\n\n" + deferredBlock(notifyLine, []pick{conflict}, []pick{held}, nil) + "\n"
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", body)}}
code, stdout, stderr := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push")
if code != 0 || !strings.Contains(stdout, "release-1.1: 1 deferred conflicts on #90 wait for a maintainer") {
t.Fatalf("notify = %d, %q\n%s", code, stdout, stderr)
}
if len(gh.issueComments) != 1 {
t.Fatalf("%d comments, want one", len(gh.issueComments))
}
c := gh.issueComments[0].Body
if !strings.Contains(c, "@cozystack/maintainers") || !strings.Contains(c, "[#6](") || !strings.Contains(c, "resolve-conflict.md") || strings.Contains(c, "#7") {
t.Errorf("the comment does not list #6 alone for a maintainer:\n%s", c)
}
}
// Only a comment by an author the batch trusts is taken for the notify
// comment: a look-alike is neither edited nor counted.
func TestNotifyKeepsToItsOwnComment(t *testing.T) {
look := issueComment{ID: 7, Body: notifyMarker + "\nplanted"}
look.User.Login, look.User.Type = "someone", "User"
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}, issueComments: []issueComment{look}}
if code, _, stderr := notifyRun(gh, "--push"); code != 0 {
t.Fatalf("notify = %d\n%s", code, stderr)
}
if len(gh.issueComments) != 2 || gh.issueComments[0].Body != look.Body {
t.Errorf("comments = %+v; want the look-alike untouched and one of ours", gh.issueComments)
}
// Ours says so once nothing waits any more, and calls nobody for it.
gh.prs = []pullRequest{notifyPR("20261026", "")}
if code, _, _ := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push"); code != 0 || len(gh.issueComments) != 2 ||
!strings.Contains(gh.issueComments[1].Body, "Nothing waits") || strings.Contains(gh.issueComments[1].Body, "@cozystack") {
t.Errorf("comments = %+v; want ours updated to say nothing waits, mentioning nobody", gh.issueComments)
}
}
// The scheduled batch writes as its bot: notify keeps to the comment that bot
// wrote, on a pull request it opened, and to no other bot's.
func TestNotifyFindsTheBotsComment(t *testing.T) {
const bot = "cozystack-ci[bot]"
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
ours.User.Login, ours.User.Type = bot, "Bot"
other := issueComment{ID: 8, Body: notifyMarker + "\nplanted"}
other.User.Login, other.User.Type = "renovate[bot]", "Bot"
// The bot writes the record into every body it opens.
pr := notifyPR("20261019", deferredBlock(notifyLine, nil, nil, nil))
pr.User.Login, pr.User.Type = bot, "Bot"
gh := &fakeGH{prs: []pullRequest{pr}, issueComments: []issueComment{ours, other}, bot: bot}
if code, stdout, stderr := notifyRun(gh, "--push"); code != 0 || !strings.Contains(stdout, "has been open since") {
t.Fatalf("notify = %d, %q\n%s", code, stdout, stderr)
}
if len(gh.issueComments) != 2 || !strings.Contains(gh.issueComments[0].Body, "has been open since") || gh.issueComments[1].Body != other.Body {
t.Errorf("comments = %+v; want the bot's own comment edited and the other bot's untouched", gh.issueComments)
}
if code, _, stderr := notifyRun(&fakeGH{prs: []pullRequest{pr}, bot: "cozystack-ci"}, "--push"); code != 2 || !strings.Contains(stderr, batchBotEnv) {
t.Errorf("a %s that is no bot login = %d (%s), want a refusal", batchBotEnv, code, stderr)
}
}
// A permission GitHub does not answer fails notify before it writes: read as
// a refusal, it would pass over a person's batch, or over the comment notify
// keeps and post a second one.
func TestNotifyFailsOnAnUnreadablePermission(t *testing.T) {
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}, permErr: errString("HTTP 502")}
if code, stdout, stderr := notifyRun(gh, "--push"); code != 2 || stdout != "" || len(gh.issueComments) != 0 || !strings.Contains(stderr, "HTTP 502") {
t.Errorf("notify on a person's batch = %d, %q, %d comments (%s); want 2 and nothing written", code, stdout, len(gh.issueComments), stderr)
}
const bot = "cozystack-ci[bot]"
pr := notifyPR("20261019", deferredBlock(notifyLine, nil, nil, nil))
pr.User.Login, pr.User.Type = bot, "Bot"
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
ours.User.Login, ours.User.Type = "maintainer", "User"
gh = &fakeGH{prs: []pullRequest{pr}, issueComments: []issueComment{ours}, bot: bot, permErr: errString("HTTP 502")}
if code, _, stderr := notifyRun(gh, "--push"); code != 2 || len(gh.issueComments) != 1 || gh.issueComments[0].Body != ours.Body || !strings.Contains(stderr, "HTTP 502") {
t.Errorf("notify with a person's comment = %d, comments %+v (%s); want 2 and the comment untouched", code, gh.issueComments, stderr)
}
}
func TestNotifyRefuses(t *testing.T) {
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
for _, args := range [][]string{
{"--mention", "@a b"},
{"--mention", "@org/team/x"},
{"--mention", "@-x"},
} {
if code, _, _ := notifyRun(gh, args...); code != 2 {
t.Errorf("notify %q = %d, want 2", args, code)
}
}
if code, stdout, _ := notifyRun(&fakeGH{}, "--push"); code != 0 || stdout != "" {
t.Errorf("notify with no batch open = %d, %q; want nothing", code, stdout)
}
pr := notifyPR("20261019", "")
pr.User.Login = "someone"
if code, stdout, _ := notifyRun(&fakeGH{prs: []pullRequest{pr}}, "--push"); code != 0 || stdout != "" {
t.Errorf("notify on a batch someone untrusted opened = %d, %q; want nothing", code, stdout)
}
}
// notify reads the line's record the way the other commands do, and stays
// silent with no batch open, but a record it cannot read, or one that says
// its open batch is being closed, stops it.
func TestNotifyValidatesTheRecord(t *testing.T) {
sha := strings.Repeat("a", 40)
closingRecord := strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":91,"branch":"backport-batch/release-1.1-20261002","head":"`+sha+`"}`, 1)
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
for _, tc := range []struct {
name string
pr pullRequest
code int
quiet bool
}{
{"a clean closed record", recordPR(91, "closed", "maintainer", block(v2(""))), 0, true},
{"a malformed record", recordPR(91, "closed", "maintainer", block(`{"line":`)), 2, false},
{"a closed record that was closing", recordPR(91, "closed", "maintainer", block(closingRecord)), 0, true},
{"an open record that is closing", recordPR(91, "open", "maintainer", block(closingRecord)), 2, false},
} {
gh := &fakeGH{prs: []pullRequest{tc.pr}}
code, stdout, stderr := notifyRun(gh, "--push")
if code != tc.code || tc.quiet && (stdout != "" || stderr != "") || len(gh.issueComments) != 0 {
t.Errorf("%s: notify = %d, %q, %q, %d comments; want %d and nothing written", tc.name, code, stdout, stderr, len(gh.issueComments), tc.code)
}
}
}
// The maintainer's example: with 1.6 and 1.7 released, release-1.8 joins them
// when it branches, and 1.8.0 drops 1.6.
func TestSupportedLines(t *testing.T) {
l := func(minor int) line { return line{1, minor} }
cases := []struct {
name string
all []line
shipped []line
released int
want []line
}{
{"two released", []line{l(5), l(6), l(7)}, []line{l(5), l(6), l(7)}, 2, []line{l(6), l(7)}},
{"release-1.8 branched", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7)}, 2, []line{l(6), l(7), l(8)}},
{"1.8.0 shipped", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7), l(8)}, 2, []line{l(7), l(8)}},
{"three released", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7)}, 3, []line{l(5), l(6), l(7), l(8)}},
{"one released", []line{l(6), l(7), l(8)}, []line{l(6), l(7)}, 1, []line{l(7), l(8)}},
{"a line between releases that never shipped", []line{l(5), l(6), l(7)}, []line{l(5), l(7)}, 2, []line{l(5), l(7)}},
{"nothing shipped yet", []line{l(0), l(1)}, nil, 2, []line{l(0), l(1)}},
}
for _, tc := range cases {
shipped := map[line]bool{}
for _, s := range tc.shipped {
shipped[s] = true
}
if got := supportedLines(tc.all, shipped, tc.released); !slices.Equal(got, tc.want) {
t.Errorf("%s: supportedLines = %v, want %v", tc.name, got, tc.want)
}
}
}
// lines prints the lines the batch runs for, newest first, which is the order
// the scheduled run takes them in: the newest released lines, as many as the
// policy says, and a line branched since, whose stable release drops the
// oldest.
func TestLines(t *testing.T) {
f := newFixture(t)
f.release("release-0.9")
f.tag("v0.9.0")
f.lines()
f.release("release-1.2")
f.release("release-1.2.0-rc.1")
f.tag("v1.2.0-rc.1")
lines := func() string {
t.Helper()
code, stdout, stderr := f.exec(nil, "lines")
if code != 0 {
t.Fatalf("lines = %d (%s)", code, stderr)
}
return stdout
}
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\n" {
t.Errorf("with release-1.2 at its first rc, lines = %q; want it before the two released lines", got)
}
f.setPolicy(releasedKey + ": 3\n")
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\nrelease-0.9\n" {
t.Errorf("with three released lines, lines = %q; want release-0.9 too", got)
}
f.setPolicy("")
f.tag("v1.2.0")
if got := lines(); got != "release-1.2\nrelease-1.1\n" {
t.Errorf("once v1.2.0 ships, lines = %q; want release-1.0 dropped", got)
}
f.setPolicy(releasedKey + ": 3\n")
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\n" {
t.Errorf("once v1.2.0 ships, with three released lines, lines = %q", got)
}
}
// A scheduled run with nothing to pick for a line succeeds without pushing or
// opening anything.
func TestRunWithNothingToPick(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "feat(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 0 || !strings.Contains(stderr, "nothing to push") {
t.Errorf("run with nothing to pick = %d, want 0:\n%s", code, stderr)
}
if f.remoteHead(batchBranch1) != "" || len(gh.created) != 0 {
t.Errorf("pushed %q and opened %q with nothing picked", f.remoteHead(batchBranch1), gh.created)
}
}
// notify writes its comment only after GitHub says the batch is still open,
// so a batch closed while notify reads it is not written to, whether the
// comment would be new or an update of its own.
func TestNotifyWritesNothingOnABatchClosedMeanwhile(t *testing.T) {
for _, update := range []bool{false, true} {
t.Run(map[bool]string{false: "a new comment", true: "an update"}[update], func(t *testing.T) {
gh := &fakeGH{prs: []pullRequest{notifyPR("20261023", recordBody(5))}}
if update {
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
ours.User.Login, ours.User.Type = "maintainer", "User"
gh.issueComments = []issueComment{ours}
}
gh.after = map[string]func(){"comments": func() { gh.find(90).State = "closed" }}
code, _, stderr := notifyRun(gh, "--push")
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || len(gh.closedWrites) != 0 || update && gh.issueComments[0].Body != notifyMarker+"\nearlier" {
t.Errorf("notify = %d, closed writes %q, comments %+v; want 2 and nothing written:\n%s", code, gh.closedWrites, gh.issueComments, stderr)
}
})
}
}
// parkedBody is an open batch body whose version 3 record parks p.
func parkedBody(p ...parkedEntry) string {
sha := strings.Repeat("a", 40)
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
Deferred: []deferredEntry{}, Dropped: []droppedEntry{}, Stale: []int{}, Parked: p}
return "batch\n\n" + recordBlock(rec) + "\n"
}
// notifyAt runs notify at a time of day, with gh faked.
func notifyAt(gh *fakeGH, at time.Time, args ...string) (int, string, string) {
var stdout, stderr bytes.Buffer
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return at }}
code := a.main(append([]string{"notify", "--line", "release-1.1"}, args...))
return code, stdout.String(), stderr.String()
}
// A park is announced once on the open batch: notify names it in its
// comment and marks it announced in the record; a later notify is silent
// about it. A mark that fails to land repeats the announcement once, the
// next day. With no batch open nothing is announced, and the first notify of
// the next batch does it.
func TestNotifyAnnouncesAParkOnce(t *testing.T) {
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
parked := func(gh *fakeGH) []parkedEntry {
t.Helper()
rec, err := parseRecord(gh.prs[0].Body, notifyLine)
if err != nil {
t.Fatal(err)
}
return rec.Parked
}
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
code, stdout, stderr := notifyRun(gh, "--push")
if code != 0 || !strings.Contains(stdout, "release-1.1: 1 parked originals on #90, named once") || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "- [#5](https://github.com/cozystack/cozystack/pull/5): builds on [#3](") {
t.Fatalf("notify = %d, want #5 announced:\n%s\n%s\n%v", code, stdout, stderr, gh.issueComments)
}
if p := parked(gh); len(p) != 1 || !p[0].Announced {
t.Errorf("the record parks %+v, want #5 marked announced", p)
}
if code, stdout, _ = notifyRun(gh, "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(gh.issueComments[0].Body, "[#5]") {
t.Errorf("notify again = %d, want nothing about #5:\n%s\n%s", code, stdout, gh.issueComments[0].Body)
}
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}, fail: map[string]error{"editBody": errors.New("HTTP 502")}}
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.issueComments) != 1 || parked(gh)[0].Announced {
t.Fatalf("notify with a failing mark = %d, want 2 after the comment, the record unmarked", code)
}
code, _, stderr = notifyAt(gh, time.Date(2026, 10, 27, 0, 5, 0, 0, time.UTC), "--push")
if code != 0 || !strings.Contains(gh.issueComments[0].Body, "[#5](") || !parked(gh)[0].Announced {
t.Errorf("notify the next day = %d, want #5 named once more and marked:\n%s\n%s", code, stderr, gh.issueComments[0].Body)
}
// Closed, or rewritten by a run, between the comment and the mark: the
// mark is not written, and the next notify names #5 again.
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
gh.after = map[string]func(){"createComment": func() { gh.prs[0].State = "closed" }}
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.closedWrites) != 0 || parked(gh)[0].Announced {
t.Errorf("notify on a batch closed meanwhile = %d, writes %q; want 2 and no mark", code, gh.closedWrites)
}
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
rewritten := parkedBody(park, parkedEntry{Original: 6, Merge: strings.Repeat("d", 40), Kind: parkPartial, Files: []string{"x"}})
gh.after = map[string]func(){"createComment": func() { gh.prs[0].Body = rewritten }}
if code, _, _ := notifyRun(gh, "--push"); code != 2 || gh.prs[0].Body != rewritten || len(gh.bodies) != 0 {
t.Errorf("notify on a body rewritten meanwhile = %d; want 2 and the new body left as it is", code)
}
// A body someone edited on the web comes back with CRLF line ends; the
// mark lands all the same.
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", strings.ReplaceAll(parkedBody(park), "\n", "\r\n"))}}
if code, _, stderr := notifyRun(gh, "--push"); code != 0 || !parked(gh)[0].Announced {
t.Errorf("notify on a CRLF body = %d, want #5 marked:\n%s", code, stderr)
}
closed := notifyPR("20261026", parkedBody(park))
at := "2026-10-26T12:00:00Z"
closed.State, closed.MergedAt = "closed", &at
gh = &fakeGH{prs: []pullRequest{closed}}
if code, stdout, _ := notifyRun(gh, "--push"); code != 0 || stdout != "" || len(gh.issueComments) != 0 || len(gh.bodies) != 0 {
t.Errorf("notify with no batch open = %d, %q, %d comments, %d edits; want nothing", code, stdout, len(gh.issueComments), len(gh.bodies))
}
next := notifyPR("20261027", parkedBody(park))
next.Number = 91
gh.prs = append(gh.prs, next)
if code, _, _ := notifyRun(gh, "--push"); code != 0 || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "[#5](") {
t.Errorf("the next batch's notify = %d, want #5 announced on it: %v", code, gh.issueComments)
}
}
// A park carries its announcement into the next record only while it is
// the same park: the same merge, kind, prerequisites, commits and files.
func TestParkEventIdentity(t *testing.T) {
a, b := strings.Repeat("a", 40), strings.Repeat("b", 40)
was := parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Announced: true}
carrier := &lineRecord{Parked: []parkedEntry{was}}
for name, tc := range map[string]struct {
now parkedEntry
announced bool
}{
"the same park": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}}, true},
"another unnumbered commit": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{b}}, false},
"another kind": {parkedEntry{Original: 5, Merge: a, Kind: parkPartial, Files: []string{"x"}}, false},
"another prerequisite": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Needs: []int{3}}, false},
"another merge": {parkedEntry{Original: 5, Merge: b, Kind: parkDependency, Commits: []string{a}}, false},
"another original": {parkedEntry{Original: 6, Merge: a, Kind: parkDependency, Commits: []string{a}}, false},
} {
got := carryAnnounced([]parkedEntry{tc.now}, carrier)
if got[0].Announced != tc.announced {
t.Errorf("%s: announced = %v, want %v", name, got[0].Announced, tc.announced)
}
}
if got := carryAnnounced([]parkedEntry{was}, nil); got[0].Announced {
t.Error("a park with no carrier came out announced")
}
unannounced := was
unannounced.Announced = false
if got := carryAnnounced([]parkedEntry{was}, &lineRecord{Parked: []parkedEntry{unannounced}}); got[0].Announced {
t.Error("a park the carrier holds unannounced came out announced")
}
}
// A park keeps its announcement through a continuation and into the next
// batch, so notify does not name it again.
func TestRolloverKeepsTheAnnouncement(t *testing.T) {
f := featWorld(t)
gh := &fakeGH{}
openBatch(t, f, gh)
if code, _, stderr := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(notifyComment(gh), "[#5](") {
t.Fatalf("notify = %d, want #5 announced:\n%s", code, stderr)
}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
t.Fatalf("run --continue = %d, want #5's park still announced:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
mergeBatch(f, gh)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh.nextPR = 91
f.now = f.now.Add(24 * time.Hour)
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
t.Fatalf("the next batch's run = %d, want #5's park announced from the merged batch:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
gh.issueComments = nil
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(notifyComment(gh), "[#5](") {
t.Errorf("notify on the next batch = %d, want nothing about #5:\n%s\n%s", code, stdout, notifyComment(gh))
}
}
// notifyComment is the notify comment on the fake, or "".
func notifyComment(gh *fakeGH) string {
for _, c := range slices.Backward(gh.issueComments) {
if strings.HasPrefix(c.Body, notifyMarker) {
return c.Body
}
}
return ""
}
+140
View File
@@ -0,0 +1,140 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"slices"
"strings"
"testing"
)
// pushed builds and pushes a batch for #5 and returns its head.
func pushed(t *testing.T, f *fixture) string {
t.Helper()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
if code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
head := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
return head
}
// --onto continues the pushed head and nothing else: it keeps what the batch
// carries and picks only what is new.
func TestOntoContinues(t *testing.T) {
f := newFixture(t)
f.lines()
head := pushed(t, f)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
if code != 2 || !strings.Contains(stderr, "not the head of backport-batch/release-1.1-20261029") {
t.Errorf("--onto with another date = %d (%s), want a refusal", code, stderr)
}
f.now = f.now.AddDate(0, 0, 3)
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
if code != 0 {
t.Fatalf("run --onto = %d\n%s", code, stderr)
}
if !strings.Contains(stderr, "title: fix(backport): batch for release-1.1, 2026-10-26\n") {
t.Errorf("the continued batch is not titled for the date it opened on:\n%s", stderr)
}
if got := f.subjects(head, "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("picked %q on top of the batch, want only #6", got)
}
if !descends(t, head, f.git("rev-parse", "HEAD")) {
t.Error("the new head does not descend from --onto")
}
if listed, _, _ := strings.Cut(stdout, "### Skipped"); !strings.Contains(listed, "[#5]") || !strings.Contains(listed, "[#6]") {
t.Errorf("the body does not list what the batch carries and what it picked:\n%s", stdout)
}
}
// A head that is not the one the remote has now is refused, so work pushed
// since cannot be dropped.
func TestOntoRefusesAStaleHead(t *testing.T) {
f := newFixture(t)
f.lines()
head := pushed(t, f)
f.git("switch", "-q", "--detach", head)
f.write("app/fold.txt", "folded\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "fold")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
if code != 2 || !strings.Contains(stderr, "is not the head of any batch branch") {
t.Errorf("run --onto a stale head = %d (%s), want a refusal", code, stderr)
}
for _, bad := range []string{head[:12], "-" + head[1:], strings.ToUpper(head), "HEAD", "--help"} {
if code, _, _ := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", bad); code != 2 {
t.Errorf("--onto %q = %d, want 2", bad, code)
}
}
}
// A rebuilt branch that does not descend from what the remote has is never
// pushed over it.
func TestPushRefusesANonFastForward(t *testing.T) {
f := newFixture(t)
f.lines()
head := pushed(t, f)
f.git("switch", "-q", "--detach", head)
f.write("app/fold.txt", "folded\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "fold")
folded := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--push")
// git would refuse too, but only after trying; the tool refuses first.
if code != 2 || !strings.Contains(stderr, "refusing a non-fast-forward push") {
t.Errorf("a fresh rebuild pushed over the batch = %d (%s), want a refusal", code, stderr)
}
if got := f.remoteHead(batchBranch1); got != folded {
t.Errorf("the remote batch moved to %.12s, want it left at %.12s", got, folded)
}
}
// A run that pushed its branch and stopped before opening the pull request
// leaves the branch behind. A run the same day builds the batch anew and
// refuses to push over it, naming the run that continues it, which then
// opens the pull request.
func TestOrphanBranchNamesItsRecovery(t *testing.T) {
f := newFixture(t)
f.lines()
head := pushed(t, f)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
// A commit picked again gets a new committer date, so a new name.
t.Setenv("GIT_COMMITTER_DATE", "2026-10-26T07:00:00Z")
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--continue", "--push")
recovery := "backport-batch run --line release-1.1 --onto " + head + " --push"
if code != 2 || !strings.Contains(stderr, recovery) {
t.Fatalf("a same-day run over the left branch = %d (%s), want a refusal naming %q", code, stderr, recovery)
}
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, strings.Fields(recovery)[1:]...); code != 0 {
t.Fatalf("%s = %d\n%s", recovery, code, stderr)
}
now := f.remoteHead(batchBranch1)
if !descends(t, head, now) || now == head || len(gh.created) != 1 {
t.Errorf("the recovery pushed %.12s over %.12s and opened %q; want a fast-forward with #6 and one pull request", now, head, gh.created)
}
}
+385
View File
@@ -0,0 +1,385 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"io"
"os"
"os/exec"
"reflect"
"strings"
"testing"
"time"
)
type failingWriter struct{}
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
// Output that never reached its reader fails the command, and stderr says why.
func TestUnwritableOutputFailsTheCommand(t *testing.T) {
var stderr strings.Builder
a := &app{gh: &fakeGH{}, stdout: failingWriter{}, stderr: &stderr}
if code := a.main([]string{"help"}); code != 2 {
t.Fatalf("help with an unwritable stdout = %d, want 2", code)
}
if !strings.Contains(stderr.String(), "broken pipe") {
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
}
}
// A command that already failed keeps its own exit code when its output is
// lost as well: check still says something is missing.
func TestUnwritableOutputKeepsTheCommandsFailure(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
var stderr strings.Builder
a := &app{gh: &fakeGH{remoteHead: f.remoteHead}, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return f.now }, tool: f.tool}
if code := a.main([]string{"check", "--line", "release-1.1"}); code != 1 {
t.Fatalf("check with #6 missing and an unwritable stdout = %d, want 1\n%s", code, stderr.String())
}
if !strings.Contains(stderr.String(), "broken pipe") {
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
}
}
// A closed pipe on stdout does not kill the run before it can say what
// already happened: the write fails like any other, and the run exits 2.
func TestClosedStdoutPipeIsReported(t *testing.T) {
if os.Getenv("BACKPORT_BATCH_CLOSED_STDOUT_HELPER") == "1" {
os.Args = []string{"backport-batch", "help"}
main()
return
}
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_ = r.Close()
t.Cleanup(func() { _ = w.Close() })
cmd := exec.Command(os.Args[0], "-test.run=^TestClosedStdoutPipeIsReported$")
cmd.Env = append(os.Environ(), "BACKPORT_BATCH_CLOSED_STDOUT_HELPER=1")
var stderr strings.Builder
cmd.Stdout, cmd.Stderr = w, &stderr
err = cmd.Run()
var exit *exec.ExitError
if !errors.As(err, &exit) || exit.ExitCode() != 2 {
t.Fatalf("help into a closed pipe = %v, want exit 2\nstderr:\n%s", err, stderr.String())
}
if !strings.Contains(stderr.String(), "output was lost") {
t.Errorf("stderr does not say the output was lost:\n%s", stderr.String())
}
}
// failOn fails every write that contains want, and takes every other one.
type failOn struct {
want string
buf strings.Builder
}
func (w *failOn) Write(p []byte) (int, error) {
if strings.Contains(string(p), w.want) {
return 0, errors.New("broken pipe")
}
return w.buf.Write(p)
}
func runWith(f *fixture, gh *fakeGH, stdout, stderr io.Writer, args ...string) int {
f.t.Helper()
if gh.remoteHead == nil {
gh.remoteHead = f.remoteHead
}
a := &app{gh: gh, stdout: stdout, stderr: stderr, now: func() time.Time { return f.now }, tool: f.tool}
return a.main(args)
}
// Output is written before the push; when it is lost, on either stream,
// nothing is pushed and no pull request is opened.
func TestLostOutputPushesNothing(t *testing.T) {
for _, stream := range []string{"stdout", "stderr"} {
t.Run(stream, func(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
var stdout, stderr io.Writer = &strings.Builder{}, &strings.Builder{}
if stream == "stdout" {
stdout = failingWriter{}
} else {
stderr = failingWriter{}
}
if code := runWith(f, gh, stdout, stderr, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run --push with a failing %s = %d, want 2", stream, code)
}
if head := f.remoteHead(batchBranch1); head != "" {
t.Errorf("the batch was pushed at %.12s", head)
}
if len(gh.prs) != 0 {
t.Errorf("a pull request was opened: %+v", gh.prs)
}
})
}
}
// A confirmation lost after the push fails the run: the pull request was
// opened, and the run does not exit 0 without having said so.
func TestLostConfirmationFailsTheRun(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "opened draft"}, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run --push losing its confirmation = %d, want 2", code)
}
if len(gh.prs) != 1 {
t.Errorf("%d pull requests, want the one the run opened", len(gh.prs))
}
if len(gh.issueComments) != 0 {
t.Errorf("the report was posted after the confirmation was lost: %+v", gh.issueComments)
}
}
// notify writes what it found before it comments; when that is lost, it
// does not comment.
func TestLostOutputCommentsNothing(t *testing.T) {
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
var stderr strings.Builder
a := &app{gh: gh, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
t.Fatalf("notify --push with a failing stdout = %d, want 2\n%s", code, stderr.String())
}
if len(gh.issueComments) != 0 {
t.Errorf("notify commented: %+v", gh.issueComments)
}
}
// writesGitHub records the GitHub writes that reach it.
type writesGitHub struct {
github
writes []string
}
func (g *writesGitHub) createPull(string, string, string, string) (int, string, error) {
g.writes = append(g.writes, "createPull")
return 1, "", nil
}
func (g *writesGitHub) editPull(int, string, string) error {
g.writes = append(g.writes, "editPull")
return nil
}
func (g *writesGitHub) editBody(int, string) error {
g.writes = append(g.writes, "editBody")
return nil
}
func (g *writesGitHub) closePull(int) error {
g.writes = append(g.writes, "closePull")
return nil
}
func (g *writesGitHub) createComment(int, string) error {
g.writes = append(g.writes, "createComment")
return nil
}
func (g *writesGitHub) editComment(int64, string) error {
g.writes = append(g.writes, "editComment")
return nil
}
// Every GitHub write is refused once output was lost. Every method of
// github is either a read or in this table, so a write added later without
// a guard fails here.
func TestEveryGitHubWriteIsGuarded(t *testing.T) {
reads := map[string]bool{"openPulls": true, "recentPulls": true, "permission": true, "labelled": true, "pull": true, "comments": true, "batchBot": true}
inner := &writesGitHub{}
g := guardedGitHub{github: inner, a: &app{unwritten: errors.New("broken pipe")}}
writes := map[string]func() error{
"createPull": func() error { _, _, err := g.createPull("b", "h", "t", "f"); return err },
"editPull": func() error { return g.editPull(1, "t", "f") },
"editBody": func() error { return g.editBody(1, "f") },
"closePull": func() error { return g.closePull(1) },
"createComment": func() error { return g.createComment(1, "f") },
"editComment": func() error { return g.editComment(1, "f") },
}
iface := reflect.TypeFor[github]()
for i := range iface.NumMethod() {
name := iface.Method(i).Name
if _, ok := writes[name]; !ok && !reads[name] {
t.Errorf("github.%s is neither a known read nor a guarded write", name)
}
}
for name, write := range writes {
if err := write(); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("%s with output lost = %v, want a refusal", name, err)
}
}
if len(inner.writes) != 0 {
t.Errorf("writes reached GitHub after output was lost: %q", inner.writes)
}
}
// A warning lost while the open batch is being updated stops the update:
// nothing is pushed, posted or edited.
func TestLostWarningLeavesTheOpenBatchAlone(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
bad.User.Login, bad.User.Type = "maintainer", "User"
gh.issueComments = append(gh.issueComments, bad)
head, comments, body := f.remoteHead(batchBranch1), len(gh.issueComments), gh.find(90).Body
f.now = f.now.AddDate(0, 0, 1)
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run --continue --push losing a warning = %d, want 2", code)
}
if got := f.remoteHead(batchBranch1); got != head {
t.Errorf("the batch moved to %.12s", got)
}
if len(gh.issueComments) != comments {
t.Errorf("a comment was posted: %d comments, want %d", len(gh.issueComments), comments)
}
if gh.find(90).Body != body {
t.Error("the batch pull request was edited")
}
}
// A warning lost while a batch is being closed leaves it open, with no
// closing record half written.
func TestLostWarningLeavesTheBatchUnclosed(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
bad.User.Login, bad.User.Type = "maintainer", "User"
gh.issueComments = append(gh.issueComments, bad)
body := gh.find(90).Body
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push"); code != 2 {
t.Fatalf("run --defer losing a warning = %d, want 2", code)
}
if gh.find(90).Body != body {
t.Error("the batch pull request was edited")
}
if !gh.isOpen(90) {
t.Error("the batch pull request was closed")
}
}
// The git and gh children keep the default SIGPIPE: catching it here must not
// leave it ignored in them, as signal.Ignore would through exec.
func TestChildrenKeepTheDefaultSIGPIPE(t *testing.T) {
catchSIGPIPE()
out, err := exec.Command("sh", "-c", "kill -PIPE $$; echo survived").Output()
if err == nil || strings.Contains(string(out), "survived") {
t.Fatalf("a child survived SIGPIPE: %q, %v", out, err)
}
}
// The branch a close left is not deleted once output was lost: a deletion
// is a push like any other.
func TestLostOutputDeletesNoBranch(t *testing.T) {
f, gh, h1 := closeWorld(t)
f.git("switch", "-q", "main")
l, err := parseLine("release-1.1")
if err != nil {
t.Fatal(err)
}
a := &app{gh: gh, unwritten: errors.New("broken pipe")}
if err := a.deleteBranch(l, closing{PR: 90, Branch: batchBranch1, Head: h1}); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Fatalf("deleteBranch with output lost = %v, want a refusal", err)
}
if got := f.remoteHead(batchBranch1); got != h1 {
t.Errorf("the branch is at %q, want it left at %.12s", got, h1)
}
}
// A park line lost on stdout stops notify before it writes: no comment, and
// the record is not marked as announced.
func TestLostParkLineWritesNothing(t *testing.T) {
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
body := gh.prs[0].Body
var stderr strings.Builder
a := &app{gh: gh, stdout: &failOn{want: "parked originals"}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
t.Fatalf("notify --push losing the park line = %d, want 2\n%s", code, stderr.String())
}
if len(gh.issueComments) != 0 {
t.Errorf("notify commented: %+v", gh.issueComments)
}
if gh.prs[0].Body != body {
t.Error("notify marked the park as announced")
}
}
// The hint naming the commits a reset drops is the only way back to them, so
// the local branch is not reset when the hint is lost.
func TestLostResetHintKeepsTheBranch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.git("switch", "-q", "-C", batchBranch1, "origin/release-1.1")
f.write("app/mine.txt", "unpushed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "an unpushed resolution")
tip := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "resetting the local"}, "run", "--line", "release-1.1"); code != 2 {
t.Fatalf("run losing the reset hint = %d, want 2", code)
}
if got := f.git("rev-parse", batchBranch1); got != tip {
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, tip)
}
}
// Once output is lost the run moves no local branch, even one a reset would
// only fast-forward.
func TestLostOutputMovesNoLocalBranch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.git("branch", "-f", batchBranch1, "origin/release-1.1^")
before := f.git("rev-parse", batchBranch1)
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "prepared"}, "run", "--line", "release-1.1"); code != 2 {
t.Fatalf("run losing an early line = %d, want 2", code)
}
if got := f.git("rev-parse", batchBranch1); got != before {
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, before)
}
}
// No new unit starts once output is lost: neither a pick nor a replay touches
// the branch.
func TestLostOutputStartsNoUnit(t *testing.T) {
f := newFixture(t)
f.lines()
before := f.git("rev-parse", "HEAD")
a := &app{unwritten: errors.New("broken pipe")}
if _, err := a.pickUnit(&verdict{}, nil); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("pickUnit with output lost = %v, want a refusal", err)
}
if _, _, err := a.replayNext(unit{}); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("replayNext with output lost = %v, want a refusal", err)
}
if got := f.git("rev-parse", "HEAD"); got != before {
t.Errorf("HEAD moved to %.12s", got)
}
}
+275
View File
@@ -0,0 +1,275 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// overridden builds a line where backport/ labels decide: #5 a feature let in,
// #6 a fix kept out, #7 a breaking fix and #8 a migration let in, #9 carrying
// both labels, #10 a feature let in that the line already carries, adapted, so
// picking it again would conflict rather than come out empty.
func overridden(t *testing.T) (*fixture, *fakeGH) {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(5, "feat(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app)!: seven", "", map[string]string{"app/seven.txt": "7\n"})
f.pr(8, "fix(platform): eight", "", map[string]string{migrationsDir + "61": "#!/bin/sh\n"})
f.pr(9, "fix(app): nine", "", map[string]string{"app/nine.txt": "9\n"})
mc10 := f.pr(10, "feat(app): ten", "", map[string]string{"app/ten.txt": "10\n"})
f.onLine("release-1.1", "feat(app): ten (#10)\n\n(cherry picked from commit "+mc10+")", map[string]string{"app/ten.txt": "10, adapted\n"})
gh := &fakeGH{labels: map[string][]int{
labelInclude: {5, 7, 8, 9, 10},
labelSkip: {6, 9},
}}
return f, gh
}
func TestOverrides(t *testing.T) {
f, gh := overridden(t)
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
want := []string{"feat(app): five (#5)", "fix(app)!: seven (#7)", "fix(platform): eight (#8)"}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, want) {
t.Errorf("picked %q, want %q", got, want)
}
section, _, _ := strings.Cut(stdout[strings.Index(stdout, "### Maintainer overrides"):], "### Skipped")
for n, outcome := range map[int]string{
5: "`backport/include`, on this branch",
6: "`backport/skip`, left out",
7: "`backport/include`, on this branch",
8: "`backport/include`, on this branch",
9: "`backport/skip`, left out",
10: "`backport/include`, skipped by rule 1",
} {
if !strings.Contains(section, fmt.Sprintf("/pull/%d)", n)) || !strings.Contains(section, outcome) {
t.Errorf("the overrides section does not show #%d as %q:\n%s", n, outcome, section)
}
}
if skipped := stdout[strings.Index(stdout, "### Skipped"):]; strings.Contains(skipped, "/pull/6)") || strings.Contains(skipped, "/pull/9)") {
t.Errorf("a skipped-by-label original is listed under a rule:\n%s", skipped)
}
}
// check holds the line to what the labels decide, and fold refuses an
// original kept out.
func TestOverridesInCheckAndFold(t *testing.T) {
f, gh := overridden(t)
code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1")
if code != 1 {
t.Fatalf("check = %d, want 1:\n%s", code, stdout)
}
for _, n := range []int{5, 7, 8} {
if !strings.Contains(stdout, fmt.Sprintf("/pull/%d ", n)) {
t.Errorf("check does not list #%d:\n%s", n, stdout)
}
}
for _, n := range []int{6, 9, 10} {
if strings.Contains(stdout, fmt.Sprintf("/pull/%d ", n)) {
t.Errorf("check lists #%d:\n%s", n, stdout)
}
}
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
f.git("fetch", "-q", "origin")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "")}
f.git("switch", "-q", "--detach", "origin/"+batchBranch1)
f.write("app/six.txt", "6\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "port\n\n(cherry picked from commit "+f.git("rev-parse", "origin/main~4")+")")
commit := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
code, stdout, _ = f.exec(gh, "fold", "--line", "release-1.1", "--original", "6", "--commit", commit)
if code != 1 || !strings.Contains(stdout, "#6 carries backport/skip") {
t.Errorf("fold of a skipped original = %d, %q; want a refusal", code, stdout)
}
}
// backport/include lifts rules 2 to 4, not rule 6: an older line still waits
// for the newer one.
func TestIncludeKeepsRuleSix(t *testing.T) {
f, gh := overridden(t)
f.git("fetch", "-q", "origin")
l, _ := parseLine("release-1.0")
b, err := (&app{gh: gh, stderr: &strings.Builder{}}).prepare(l)
if err != nil {
t.Fatal(err)
}
if v := b.find(5); v == nil || v.rule != ruleNewer {
t.Errorf("#5 on release-1.0 = %+v, want rule 6", v)
}
if v := b.find(10); v == nil || v.rule != 0 {
t.Errorf("#10 on release-1.0 = %+v, want it to qualify: release-1.1 carries it", v)
}
}
func TestOverridesUnreadable(t *testing.T) {
f, gh := overridden(t)
gh.listErr = errString("gh: HTTP 502")
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "HTTP 502") {
t.Errorf("check with labels unreadable = %d (%s), want 2", code, stderr)
}
}
// ghCLI against a stub gh on PATH: the exact argv, and a listing that fills
// GitHub search's cap is refused rather than read as complete.
func TestGHCLI(t *testing.T) {
bin := t.TempDir()
args, out := filepath.Join(bin, "args"), filepath.Join(bin, "out")
stub := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"" + args + "\"\ncat \"" + out + "\"\n"
if err := os.WriteFile(filepath.Join(bin, "gh"), []byte(stub), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
write := func(s string) {
t.Helper()
if err := os.WriteFile(out, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
_ = os.Remove(args)
}
called := func() string {
data, _ := os.ReadFile(args)
return strings.TrimSpace(string(data))
}
write(`[{"number":5},{"number":7}]`)
got, err := ghCLI{}.labelled(labelInclude)
if err != nil || !slices.Equal(got, []int{5, 7}) {
t.Errorf("labelled = %v, %v; want [5 7]", got, err)
}
if want := "pr list --base main --state merged --label backport/include --limit 1000 --json number"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
var full []string
for i := range searchResultCap {
full = append(full, fmt.Sprintf(`{"number":%d}`, i+1))
}
write("[" + strings.Join(full, ",") + "]")
if _, err := (ghCLI{}).labelled(labelSkip); err == nil {
t.Error("a listing as long as the search cap was read as complete")
}
write(`{"permission":"write"}`)
if p, err := (ghCLI{}).permission("maintainer"); err != nil || p != "write" {
t.Errorf("permission = %q, %v", p, err)
}
if want := "api repos/{owner}/{repo}/collaborators/maintainer/permission"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write(`{"permission":"admin"}`)
for _, login := range []string{"x/../../orgs/y", "-x", "a b", "github-actions[bot]", ""} {
if _, err := (ghCLI{}).permission(login); err == nil || called() != "" {
t.Errorf("permission(%q) = %v and called gh %q; want a refusal before gh", login, err, called())
}
}
write("[]")
if _, err := (ghCLI{}).openPulls("release-1.6"); err != nil {
t.Fatal(err)
}
if want := "api -X GET repos/{owner}/{repo}/pulls -f state=open -f base=release-1.6 -f per_page=100"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write("[]")
if _, err := (ghCLI{}).recentPulls("release-1.6", 2); err != nil {
t.Fatal(err)
}
if want := "api -X GET repos/{owner}/{repo}/pulls -f state=all -f base=release-1.6 -f sort=created -f direction=desc -f per_page=100 -f page=2"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
var page []string
for i := range openPullsCap {
page = append(page, fmt.Sprintf(`{"number":%d}`, i+1))
}
write("[" + strings.Join(page, ",") + "]")
if _, err := (ghCLI{}).openPulls("release-1.6"); err == nil {
t.Error("a full page of open pull requests was read as all of them")
}
write(`[[{"id":1,"body":"a","user":{"login":"x","type":"User"}}],[{"id":2,"body":"b","user":{"login":"y","type":"Bot"}}]]`)
comments, err := ghCLI{}.comments(90)
if err != nil || len(comments) != 2 || comments[1].ID != 2 || comments[1].User.Type != "Bot" {
t.Errorf("comments = %+v, %v; want both pages", comments, err)
}
if want := "api --paginate --slurp repos/{owner}/{repo}/issues/90/comments"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write("{}")
if err := (ghCLI{}).createComment(90, "/tmp/body.md"); err != nil {
t.Fatal(err)
}
if want := "api --method POST repos/{owner}/{repo}/issues/90/comments -F body=@/tmp/body.md"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write("{}")
if err := (ghCLI{}).editComment(12345678901, "/tmp/body.md"); err != nil {
t.Fatal(err)
}
if want := "api --method PATCH repos/{owner}/{repo}/issues/comments/12345678901 -F body=@/tmp/body.md"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
// A batch pull request carries no label: the release workflows act on a
// pull request cozystack-ci[bot] opened only when it has the release label.
write("https://github.com/cozystack/cozystack/pull/5300\n")
if n, url, err := (ghCLI{}).createPull("release-1.7", batchBranch1, "fix(backport): batch", "/tmp/body.md"); err != nil || n != 5300 || url != "https://github.com/cozystack/cozystack/pull/5300" {
t.Errorf("createPull = %d, %q, %v; want the pull request gh printed", n, url, err)
}
if want := "pr create --draft --base release-1.7 --head " + batchBranch1 + " --title fix(backport): batch --body-file /tmp/body.md"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write("Warning: 1 uncommitted change\n")
if _, _, err := (ghCLI{}).createPull("release-1.7", batchBranch1, "fix(backport): batch", "/tmp/body.md"); err == nil {
t.Error("createPull took output that is no pull request URL")
}
write(`{"number":90,"state":"closed","merged_at":"2026-10-26T12:00:00Z","head":{"ref":"x","sha":"y"}}`)
if pr, err := (ghCLI{}).pull(90); err != nil || pr.State != "closed" || pr.MergedAt == nil {
t.Errorf("pull = %+v, %v; want #90 merged", pr, err)
}
if want := "api repos/{owner}/{repo}/pulls/90"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
if _, err := (ghCLI{}).pull(91); err == nil {
t.Error("pull took GitHub's answer about another pull request")
}
write("")
if err := (ghCLI{}).closePull(90); err != nil {
t.Fatal(err)
}
if want := "pr close 90"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
write("")
if err := (ghCLI{}).editPull(90, "fix(backport): batch", "/tmp/body.md"); err != nil {
t.Fatal(err)
}
if want := "pr edit 90 --title fix(backport): batch --body-file /tmp/body.md"; called() != want {
t.Errorf("gh %q, want gh %q", called(), want)
}
}
+796
View File
@@ -0,0 +1,796 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strings"
"testing"
)
var line11, _ = parseLine("release-1.1")
// handBatch pushes a batch carrying merges in the batch's shape, as a run
// made one before the partial guard, and opens its pull request with a
// record that defers nothing.
func handBatch(f *fixture, gh *fakeGH, merges ...string) string {
f.t.Helper()
f.git("fetch", "-q", "origin")
f.git("switch", "-q", "--detach", "origin/release-1.1")
for _, mc := range merges {
if parents := strings.Fields(f.git("rev-list", "--parents", "-n", "1", mc)); len(parents) == 2 {
f.git("cherry-pick", "-x", mc)
continue
}
base := f.git("rev-parse", "HEAD")
for _, c := range strings.Fields(f.git("rev-list", "--reverse", "--no-merges", mc+"^1.."+mc+"^2")) {
f.git("cherry-pick", "-x", c)
}
merge := f.joined(base, "HEAD", f.git("log", "-1", "--format=%B", mc)+"\n\n"+batchTrailer+": "+mc)
f.git("update-ref", "HEAD", merge)
}
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")}
return f.remoteHead(batchBranch1)
}
// policyWorld is #5088's shape: #3, a feat the line never gets, adds a
// policy and its test; #5 deletes both again and adds a test of its own.
func policyWorld(t *testing.T) (*fixture, string) {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n", "chart/tests/policy_test.yaml": "suite: policy\n"})
mc5 := f.pr(5, "fix(chart): remove the policy a rebase brought back", "```release-note\nfix(chart): the stray network policy is gone.\n```\n",
map[string]string{"chart/templates/policy.yaml": "", "chart/tests/policy_test.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
return f, mc5
}
// A clean pick that would delete files the line never had, and so carry only
// #5's test, is withheld: #5 is parked as partial, with the paths it would
// leave without effect, and its release note stays out of the body.
func TestPartialPickIsWithheld(t *testing.T) {
f, mc5 := policyWorld(t)
f.pr(6, "fix(app): six", "```release-note\nfix(app): six works.\n```\n", map[string]string{"app/six.txt": "6\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("picked %q, want only #6", got)
}
want := `"parked":[{"original":5,"merge":"` + mc5 + `","kind":"partial","files":["chart/templates/policy.yaml","chart/tests/policy_test.yaml"]}]`
if !strings.Contains(stdout, want) {
t.Errorf("the record does not park #5 as partial (%s):\n%s", want, stdout)
}
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "`chart/templates/policy.yaml`") {
t.Errorf("the body does not say #5 is parked and why:\n%s", stdout)
}
if strings.Contains(stdout, "stray network policy") || !strings.Contains(stdout, "six works") {
t.Errorf("the release notes are not #6's alone:\n%s", stdout)
}
}
// A file the line removed itself, which no original added after the line
// branched, still leaves #5's deletion without effect: check finds it with no
// record and parks it, and run picks nothing.
func TestPartialWithoutAnAdder(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
f.pr(5, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
t.Errorf("picked %q, want nothing", got)
}
if !strings.Contains(stdout, "### Parked") {
t.Errorf("the body does not park #5:\n%s", stdout)
}
f.git("switch", "-q", "main")
code, stdout, stderr = f.exec(nil, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies to release-1.1") {
t.Errorf("check = %d, want 0 with a parked-partial note:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
}
// The picker judges what its picks did, not what the original's merge did:
// #5's first commit deletes a file the line no longer has and comes out
// empty, its second adds a test, and the unit left would be the test alone.
func TestPartialByAnEmptyOwnCommit(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
f.prCommits(5, "fix(app): drop the policy and test it", "```release-note\nfix(app): no more policy.\n```\n",
change{files: map[string]string{"app/policy.yaml": ""}},
change{files: map[string]string{"app/policy_test.txt": "gone\n"}})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
t.Errorf("picked %q, want #5 withheld", got)
}
if strings.Contains(stdout, "no more policy") || !strings.Contains(stdout, "### Parked") {
t.Errorf("#5's note is in the body, or #5 is not parked:\n%s", stdout)
}
}
// A batch built before the guard can already carry a partial unit. It fails
// the cut and verify whatever its label says, and --defer takes it off,
// closing a batch it was the last unit of.
func TestExistingPartialUnitFailsTheCut(t *testing.T) {
f, mc5 := policyWorld(t)
gh := &fakeGH{}
head := handBatch(f, gh, mc5)
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
t.Errorf("check --cut = %d, want 1 naming the partial unit:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
code, stdout, stderr = f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
if code != 1 || !strings.Contains(stdout, `#5 leaves "chart/templates/policy.yaml", "chart/tests/policy_test.yaml" without effect on release-1.1`) {
t.Errorf("verify = %d, want 1 naming what #5 leaves out:\n%s\n%s", code, stdout, stderr)
}
gh.labels = map[string][]int{labelSkip: {5}}
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
t.Errorf("check --cut with backport/skip = %d, want the partial unit to still fail it:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
code, _, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push")
if code != 0 || prState(gh.find(90)) != "closed" || f.remoteHead(batchBranch1) != "" {
t.Errorf("run --defer 5 --push = %d, #90 %s, branch at %q; want the batch closed and its branch gone:\n%s", code, prState(gh.find(90)), f.remoteHead(batchBranch1), stderr)
}
}
// An original whose first pick came out empty, because the line already had
// that change, is on the batch all the same: check does not call it missing,
// a continuation keeps it picked, and so does a rebuild without another
// original.
func TestOnBatchAfterAnEmptyFirstPick(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old\n", "app/b.txt": "old\n"})
f.lines()
f.onLine("release-1.1", "fix(app): a, on the line first", map[string]string{"app/a.txt": "fixed\n"})
f.prCommits(5, "fix(app): a and b", "",
change{files: map[string]string{"app/a.txt": "fixed\n"}},
change{files: map[string]string{"app/b.txt": "fixed\n"}})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
head := openBatch(t, f, gh)
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a and b (#5)", "fix(app): six (#6)"}) {
t.Fatalf("the batch carries %q, want #5 and #6", got)
}
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
if code != 0 {
t.Errorf("check = %d, want 0 with #5 on the batch:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
code, stdout, _ = f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") {
t.Errorf("check --cut = %d, want #5 only on the batch:\n%s", code, stdout)
}
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
if code != 0 || !strings.Contains(stdout, `"picked":[5,6]`) {
t.Errorf("run --continue = %d, want #5 and #6 picked:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e")
if code != 0 || !strings.Contains(stdout, `"picked":[5]`) || !strings.Contains(stdout, `"missing":[]`) {
t.Errorf("run --defer 6 = %d, want #5 still picked:\n%s\n%s", code, stdout, stderr)
}
}
// featWorld is #5071's shape: #3, a feat the line never gets, adds a file,
// and #5 fixes that file; #6 is a plain fix beside it.
func featWorld(t *testing.T) *fixture {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(3, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
f.pr(5, "fix(app): fix the tap", "```release-note\nfix(app): the tap works.\n```\n", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f
}
// A fix of a file a feat added on main, which the line never gets, is
// parked, not tried, and check takes the park as a note.
func TestParkAFixOfAFeatureTheLineLacks(t *testing.T) {
f := featWorld(t)
gh := &fakeGH{}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
if strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
t.Errorf("#5 is not parked on #3:\n%s", stdout)
}
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "builds on [#3]") {
t.Errorf("the body does not say #5 builds on #3:\n%s", stdout)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: it builds on #3, which never reaches release-1.1") {
t.Errorf("check = %d, want 0 with a note that #5 is parked:\n%s\n%s", code, stdout, stderr)
}
}
// backport/include lifts no park.
func TestIncludeDoesNotLiftThePark(t *testing.T) {
f := featWorld(t)
code, stdout, stderr := f.exec(&fakeGH{labels: map[string][]int{labelInclude: {5}}}, "run", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, "`backport/include`, parked: builds on") {
t.Errorf("run with backport/include on #5 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
}
}
// A prerequisite that is itself parked, but adapted onto the batch by hand,
// satisfies what builds on it: #3 is parked on #2, a feat, and #5 changes a
// line #3 wrote.
func TestAdaptedPrerequisiteSatisfiesItsDependent(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": appFile})
f.lines()
f.pr(20, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
mc3 := f.pr(3, "fix(app): an excited two, a loud three", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
f.pr(5, "fix(app): an excited three", "", map[string]string{"app/b.txt": strings.Replace(appFile, "three", "THREE!", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
t.Fatalf("run = %d, want #5 parked as long as #3 is:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
w := emptyBatch(f, &fakeGH{}, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")
// #3 adapted by hand: every file it changes, changed, in the batch's
// shape.
pick := w.pick(w.head, f.git("rev-parse", mc3+"^2"), map[string]string{"app/a.txt": strings.Replace(appFile, "two", "two!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
fold := w.merge(w.head, pick, "fix(app): an excited two, a loud three (#3)\n\n"+batchTrailer+": "+mc3)
f.git("push", "-q", "origin", fold+":refs/heads/"+batchBranch1)
w.gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, w.gh.prs[0].Body)}
code, stdout, stderr := f.exec(w.gh, "check", "--line", "release-1.1")
if code != 1 || strings.Contains(stderr, "#5 is parked") || strings.Contains(stdout+stderr, "waits on") || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
}
code, stdout, stderr = f.exec(w.gh, "run", "--line", "release-1.1", "--continue")
if code != 0 || !strings.Contains(stdout, `"picked":[3,5]`) {
t.Errorf("run --continue = %d, want #5 picked after #3's adaptation:\n%s\n%s", code, stdout, stderr)
}
}
// What an earlier unit of the batch brought counts: #3 writes the line #8
// changes, a feat takes it back and another feat, #6, writes it again, so
// blame names #6, which never comes; once #3 is picked the branch has the
// line all the same.
func TestContentFromAnotherUnitSatisfies(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(3, "fix(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(4, "feat(app): a quiet two", "", map[string]string{"app/a.txt": appFile})
f.pr(6, "feat(app): a loud two again", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(8, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): a loud two (#3)", "fix(app): an excited two (#8)"}) {
t.Errorf("picked %q, want #3 and #8:\n%s", got, stdout)
}
}
// auxWorld is #5142's shape: #3, a feat, adds a bats test; #5 fixes a
// workflow and that test, so all it builds on that never comes is a test;
// #6 is a plain fix.
func auxWorld(t *testing.T) *fixture {
t.Helper()
f := newFixture(t)
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
f.lines()
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f
}
// A workflow original whose only missing dependency is a test waits for a
// maintainer in the scheduled run, whose token cannot push workflows, and
// check holds the cut on it. A maintainer's own run parks it. With no batch
// open, or one whose body carries no record, it is missing; a record from
// before the mode was saved says the scheduled mode by listing a wait.
func TestWorkflowModeCarriesToCheck(t *testing.T) {
f := auxWorld(t)
gh := &fakeGH{}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
t.Fatalf("scheduled run = %d, want #5 waiting, not parked:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
t.Errorf("check --cut = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
}
v1 := "batch\n\n" + deferredBlock(line11, nil, nil, []pick{{v: &verdict{o: &original{number: 5}}, files: []string{".github/workflows/release.yaml"}}}) + "\n"
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, v1)}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
t.Errorf("check --cut with a version 1 record = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
}
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "a batch someone wrote")}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
t.Errorf("check under a batch with no record = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
}
f = auxWorld(t)
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
t.Errorf("check with no batch = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
}
gh = &fakeGH{}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Fatalf("a maintainer's run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
t.Errorf("check after a maintainer's run = %d, want 0 with #5 parked:\n%s\n%s", code, stdout, stderr)
}
}
// Evidence found only in a test file still parks an original that changes
// no workflow: #4973's shape.
func TestTestOnlyEvidenceParksOtherOriginals(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.go": "package app\n", "app/nodes_test.go": "package app\n\nfunc a() {}\n"})
f.lines()
f.pr(3, "feat(app): proxmox nodes", "", map[string]string{"app/nodes_test.go": "package app\n\nfunc a() { proxmox() }\n"})
f.pr(5, "fix(app): nodes", "", map[string]string{"app/nodes.go": "package app\n\nvar fixed = true\n", "app/nodes_test.go": "package app\n\nfunc a() { proxmox(); fixed() }\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 || strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("run = %d, want #5 parked on #3:\n%s\n%s", code, stdout, stderr)
}
}
// What an original builds on through lines it adds is not seen: #4973's
// $isProxmox. The original is picked; CI on the batch is what catches it.
func TestAddedLinesDependencyIsNotSeen(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.tpl": "a\nb\n"})
f.lines()
f.pr(3, "feat(app): define isProxmox", "", map[string]string{"app/proxmox.tpl": "isProxmox\n"})
f.pr(5, "fix(app): use isProxmox", "", map[string]string{"app/nodes.tpl": "a\nif isProxmox\nb\n"})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
t.Errorf("run = %d, want #5 picked:\n%s\n%s", code, stdout, stderr)
}
}
// With no batch open, check reads a closed record whatever its version and
// whatever mode it saved as the scheduled run's: the workflow original it
// left waiting is missing, not parked, while its drops and its other
// deferrals still count.
func TestClosedRecordModeFallback(t *testing.T) {
for _, version := range []int{2, 3} {
for _, saved := range []bool{false, true} {
t.Run(fmt.Sprintf("version %d, noWorkflowFiles %v", version, saved), func(t *testing.T) {
f := auxWorld(t)
mc6 := f.git("log", "--first-parent", "--format=%H", "-1", "--grep", "(#6)$", "main")
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
sha := f.git("rev-parse", "origin/main")
rec := lineRecord{Version: version, Line: "release-1.1", Report: strings.Repeat("b", 64),
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: saved, Mode: modeContinue}},
Deferred: []deferredEntry{{Original: 5, Files: []string{".github/workflows/release.yaml"}, Workflows: true}, {Original: 7, Commit: f.git("rev-parse", mc7+"^2"), Files: []string{"app/seven.txt"}}},
Dropped: []droppedEntry{{Original: 6, Merge: mc6, Reason: "not for this line", On: 90, Date: "2026-10-26"}}, Stale: []int{}}
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
at := "2026-10-26T12:00:00Z"
pr.State, pr.MergedAt = "closed", &at
code, stdout, stderr := f.exec(&fakeGH{prs: []pullRequest{pr}}, "check", "--line", "release-1.1")
if code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
t.Errorf("check = %d, want #5 missing and not parked:\n%s\n%s", code, stdout, stderr)
}
if !strings.Contains(stderr, "note: #6 is dropped") || !strings.Contains(stderr, "warning: deferred conflict on #90 (merged): https://github.com/cozystack/cozystack/pull/7 ") {
t.Errorf("check lost the closed record's drop or deferral:\n%s", stderr)
}
})
}
}
}
// Withdrawing a drop puts the original back where the rules and the line
// put it: a partial original is parked again, in the record as in check.
func TestUndropRestoresThePark(t *testing.T) {
for name, world := range map[string]func(*testing.T) *fixture{
"partial": func(t *testing.T) *fixture {
f, _ := policyWorld(t)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f
},
"dependency": featWorld,
} {
t.Run(name, func(t *testing.T) {
f := world(t)
gh := &fakeGH{}
openBatch(t, f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "5", "--reason", "not for 1.1", "--push")
reopen(f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "5", "--push")
reopen(f, gh)
if rec := recordOf(t, gh); len(rec.Parked) != 1 || rec.Parked[0].Original != 5 || rec.Parked[0].Kind != name {
t.Errorf("after the undrop the record parks %+v, want #5 parked %s", rec.Parked, name)
}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
t.Errorf("check = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
}
})
}
}
// A prerequisite the open batch carries is there for what builds on it,
// whatever its label now says: check and run agree that #7 is not parked.
func TestPrerequisiteStillOnTheBatchSatisfies(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
gh := &fakeGH{}
openBatch(t, f, gh)
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
gh.labels = map[string][]int{labelSkip: {5}}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#7 is parked") || !strings.Contains(stdout, "/pull/7 ") {
t.Errorf("check = %d, want #7 missing, not parked:\n%s\n%s", code, stdout, stderr)
}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue"); code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
t.Errorf("run --continue = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
}
}
// The picker judges what its picks did where the run has got to: #4
// deletes the file #5 also deletes, so after #4 #5 would bring only its
// test, which the net merge at the line's head cannot see.
func TestPartialByAnEarlierPick(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, `"picked":[4]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("run = %d, want #4 picked and #5 parked as partial:\n%s\n%s", code, stdout, stderr)
}
}
// What an earlier pick brought clears a partial park the line's head
// gave: the line removed the policy, #4 brings it back, and #5's deletion
// then has something to delete.
func TestPartialClearedByAnEarlierPick(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
f.pr(3, "feat(app): no policy", "", map[string]string{"app/policy.yaml": ""})
f.pr(4, "fix(app): the policy, back", "", map[string]string{"app/policy.yaml": "allow: some\n"})
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, `"picked":[4,5]`) {
t.Errorf("run = %d, want #4 and #5 picked:\n%s\n%s", code, stdout, stderr)
}
}
// A reason a person deferred an original for comes before its partial
// park.
func TestReasonComesBeforeThePartialPark(t *testing.T) {
f, _ := policyWorld(t)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "waits for the policy rework", "--push")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
t.Errorf("check = %d, want #5 deferred for its reason:\n%s\n%s", code, stdout, stderr)
}
}
// The workflow exception holds only while what never comes is tests or
// docs, and while the workflow lines build on nothing missing: a
// workflow original that also fixes a file a feat added, or whose workflow
// line another original still to come wrote, parks in the scheduled run too.
func TestWorkflowExceptionNeedsTestsOnly(t *testing.T) {
t.Run("a never dependency outside tests", func(t *testing.T) {
f := auxWorld(t)
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
}
})
t.Run("a workflow line still to come", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n", "app/four.txt": "old\n"})
f.lines()
f.onLine("release-1.1", "fix(app): four, the line's way", map[string]string{"app/four.txt": "line\n"})
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
f.pr(4, "fix(ci): four", "", map[string]string{"app/four.txt": "four\n", ".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("scheduled run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
}
})
}
// What a rebuild takes off the batch is no longer there for what builds on
// it: deferring #5 makes #7, which merged after the batch was built, wait
// on it.
func TestRebuildTakesThePrerequisiteAway(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) {
t.Errorf("run --defer 5 = %d, want #7 waiting on #5:\n%s\n%s", code, stdout, stderr)
}
}
// A file the line renamed the way the original does is the original's own
// rename, not a deletion left without effect.
func TestSameRenameOnBothSidesIsWhole(t *testing.T) {
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
f := newFixture(t)
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
f.lines()
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
f.pr(5, "fix(app): rename p to q, and fix l10", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[5]`) {
t.Errorf("run = %d, want #5 picked whole:\n%s\n%s", code, stdout, stderr)
}
}
// A partial park only the picker saw, where the net merge at the line's
// head looks whole, holds in check and in the operations that pick nothing
// as the open record keeps it: check does not call the original missing.
func TestPickerParkHoldsInCheck(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.lines()
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies") {
t.Errorf("check = %d, want #5 parked as the record says:\n%s\n%s", code, stdout, stderr)
}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("run --defer 6 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
}
// Deferring #4, which made #5 partial, ends the park: #5 applies whole
// on what the batch carries now.
f.git("switch", "-q", "main")
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "4", "--reason", "fails the e2e", "--push")
if code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"missing":[5]`) {
t.Errorf("run --defer 4 = %d, want #5 missing, no longer parked:\n%s\n%s", code, stdout, stderr)
}
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check after deferring #4 = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
}
}
// A workflow original that waits for a maintainer only as the open record
// lists it: one merged since the batch was built is missing, and so is
// what waits on it.
func TestUnlistedWorkflowWaitIsMissing(t *testing.T) {
f := auxWorld(t)
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
f.git("switch", "-q", "main")
reopen(f, gh)
f.pr(8, "fix(ci): run the new release on tags", "", map[string]string{".github/workflows/new.yaml": "on: tag\n", "hack/changelog.bats": "@test x {\n false\n}\n# eight\n", "app/eight.txt": appFile + "TWO\n"})
f.pr(9, "fix(app): nine", "", map[string]string{"app/eight.txt": appFile + "TWO!\n"})
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/9 fix(app): nine (it waits on #8, which is not on the line either)") {
t.Errorf("check = %d, want #9 missing, waiting on #8:\n%s\n%s", code, stdout, stderr)
}
}
// The line's own rename stands for the original's only with the original's
// change in it: a fold that leaves the renamed file as it was is refused.
func TestSameRenameStillNeedsItsFix(t *testing.T) {
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
f := newFixture(t)
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
f.lines()
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
mc5 := f.pr(5, "fix(app): rename p to q, fix l10, and five", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1), "app/five.txt": "5\n"})
w := emptyBatch(f, &fakeGH{}, "")
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
fold := w.merge(w.head, pick, "fix(app): rename p to q, fix l10, and five (#5)\n\n"+batchTrailer+": "+mc5)
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/p.txt" without effect`) {
t.Errorf("fold without the fix in q = %d, %q\n%s", code, out, stderr)
}
}
// A prerequisite the line carries is there, though the rules would keep it
// out: a feat carried by a hand adaptation does not park what builds on it.
func TestCarriedExcludedPrerequisiteSatisfies(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
f.lines()
mc3 := f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.onLine("release-1.1", "feat(app): a loud two, adapted\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "Two", 1)})
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) {
t.Errorf("run = %d, want #5 not parked on the carried #3:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") {
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
}
}
// With no batch open, a workflow wait a closed record keeps waits on
// nothing: the original is judged as the scheduled run judges it, so one
// that also builds on a feat the line lacks is parked, in check as in run.
func TestClosedWorkflowWaitIsJudgedAsRunJudgesIt(t *testing.T) {
f := auxWorld(t)
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
sha := f.git("rev-parse", "origin/main")
rec := lineRecord{Version: 2, Line: "release-1.1", Report: strings.Repeat("b", 64),
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeContinue}},
Deferred: []deferredEntry{{Original: 7, Files: []string{".github/workflows/tap.yaml"}, Workflows: true}}, Dropped: []droppedEntry{}, Stale: []int{}}
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
at := "2026-10-26T12:00:00Z"
pr.State, pr.MergedAt = "closed", &at
gh := &fakeGH{prs: []pullRequest{pr}}
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); strings.Contains(stdout, "/pull/7 ") || !strings.Contains(stderr, "note: #7 is parked: it builds on #20") {
t.Errorf("check --cut = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
}
}
// A pick that comes out empty is judged too: #5's own commit did half its
// fix, which the line has, and a merge of main into its branch did the
// rest, which no pick carries; #5 is not on the line.
func TestEmptyPicksStillJudged(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: b", "", map[string]string{"app/b.txt": "old\n"})
f.lines()
f.onLine("release-1.1", "fix(app): b, half of it", map[string]string{"app/b.txt": "middle\n"})
f.git("switch", "-q", "-c", "pr-5", "main")
f.apply(map[string]string{"app/b.txt": "middle\n"})
f.git("commit", "-q", "-m", "b, half of it")
f.git("switch", "-q", "main")
f.apply(map[string]string{"app/main.txt": "moved\n"})
f.git("commit", "-q", "-m", "chore: move main along")
f.git("switch", "-q", "pr-5")
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
f.apply(map[string]string{"app/b.txt": "fixed\n"})
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
f.git("switch", "-q", "main")
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b (#5)", "pr-5")
f.git("push", "-q", "origin", "main")
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
}
}
// --defer takes off a unit that does only part of its original whatever
// rule now keeps that original out, as check tells it to.
func TestDeferTakesOffAPartialExcludedUnit(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n"})
mc5 := f.pr(5, "feat(chart): remove the policy, and test values", "", map[string]string{"chart/templates/policy.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
gh := &fakeGH{}
handBatch(f, gh, mc5)
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "only part of it applies") {
t.Fatalf("check --cut = %d, want the partial unit to fail it:\n%s", code, stdout)
}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push"); code != 0 || prState(gh.find(90)) != "closed" {
t.Errorf("run --defer 5 --push = %d, #90 %s; want the unit taken off and the batch closed:\n%s", code, prState(gh.find(90)), stderr)
}
}
// A stored wait on an original merged after the one that waits cannot be
// what a run wrote.
func TestForwardWaitIsRefused(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
sha := f.git("rev-parse", "origin/main")
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
Deferred: []deferredEntry{{Original: 5, Files: []string{}, Needs: []int{7}}}, Dropped: []droppedEntry{}, Stale: []int{}}
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")}}
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "#5 wait on #7") {
t.Errorf("check = %d, want 2 for a wait on a later original:\n%s", code, stderr)
}
}
// The workflow exception looks only at what still keeps the original from
// the line: a workflow line its prerequisite wrote, which the open batch
// carries though that prerequisite is labelled backport/skip, does not
// stop it, in check as in run.
func TestWorkflowExceptionIgnoresWhatIsThere(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
f.lines()
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
f.pr(4, "fix(ci): run the release on pushes", "", map[string]string{".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(ci): run the release on tags too", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
f.git("switch", "-q", "main")
reopen(f, gh)
gh.labels = map[string][]int{labelSkip: {4}}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
t.Fatalf("scheduled run = %d, want #7 waiting for a maintainer:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 changes .github/workflows/ and waits on batch PR #90") {
t.Errorf("check --cut = %d, want #7 waiting, as run has it:\n%s\n%s", code, stdout, stderr)
}
}
// An empty pick leaves a deletion undone that only a merge of main into the
// original's branch made: the original is not on the line.
func TestEmptyPickLeavesADeletionUndone(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old feature\n", "app/b.txt": "old\n"})
f.lines()
f.onLine("release-1.1", "fix(app): b, already", map[string]string{"app/b.txt": "fixed\n"})
f.git("switch", "-q", "-c", "pr-5", "main")
f.apply(map[string]string{"app/b.txt": "fixed\n"})
f.git("commit", "-q", "-m", "b")
f.git("switch", "-q", "main")
f.apply(map[string]string{"app/main.txt": "moved\n"})
f.git("commit", "-q", "-m", "chore: move main along")
f.git("switch", "-q", "pr-5")
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
f.git("rm", "-q", "app/a.txt")
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
f.git("switch", "-q", "main")
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b, and a goes (#5)", "pr-5")
f.git("push", "-q", "origin", "main")
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
}
}
// A file the line already has as the original makes it builds on nothing
// the line lacks: #5 fixes a.txt, which the line fixed the same way, and
// b.txt, so it picks whole and is not parked on the feat that wrote a.txt.
func TestAlreadyAppliedFileBuildsOnNothing(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": "old\n"})
f.lines()
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.onLine("release-1.1", "fix(app): two, the line's way", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
f.pr(5, "fix(app): an excited two, and b", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": "fixed\n"})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
t.Errorf("run = %d, want #5 picked, not parked:\n%s\n%s", code, stdout, stderr)
}
}
+144
View File
@@ -0,0 +1,144 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// policyPage is the policy the batch applies. The block in it that names when
// each line started under the policy is read from the page itself, on the ref
// the run pins, so the page and the batch cannot drift apart.
const policyPage = "docs/release/backports.md"
const policyFence = "```backport-policy"
var (
policyLineRE = regexp.MustCompile(`^(release-\d+\.\d+|` + batchOnlyKey + `):[ \t]*(\d{4}-\d{2}-\d{2})[ \t]*$`)
policyReleasedRE = regexp.MustCompile(`^` + releasedKey + `:[ \t]*(\d+)[ \t]*$`)
)
const (
// batchOnlyKey names the day from which the batch is the only way onto a
// line, so that from then on only its picks show what a line carries.
batchOnlyKey = "batch-only"
// releasedKey names how many of the lines that have shipped a stable
// release the batch runs for, the newest of them; defaultReleased when
// the block does not say.
releasedKey = "released-lines"
defaultReleased = 2
)
// The rule 2 types and the scopes that never go into a patch release, as the
// policy page lists them; policy_test.go holds the page to these.
var (
takenTypes = []string{"fix", "security"}
excludedScopes = []string{"deps"}
)
// starts maps a release line to the day it started under the policy. A line
// that is not listed started at its fork point from main.
type starts map[string]time.Time
// policy is what the backport-policy block says: when each line started, the
// batch-only day, and how many released lines the batch runs for.
type policy struct {
starts starts
batchOnly time.Time
released int
}
// parsePolicy reads the one backport-policy block of the page: a line per
// release line, `release-X.Y: YYYY-MM-DD`, exactly one `batch-only:
// YYYY-MM-DD`, at most one `released-lines: N`, and comments starting with #.
func parsePolicy(page string) (*policy, error) {
var blocks [][]string
var cur []string
in := false
for _, ln := range strings.Split(strings.ReplaceAll(page, "\r\n", "\n"), "\n") {
t := strings.TrimSpace(ln)
switch {
case !in && t == policyFence:
in, cur = true, nil
case in && t == "```":
in = false
blocks = append(blocks, cur)
case in:
cur = append(cur, t)
}
}
if in {
return nil, fmt.Errorf("%s: the %s block is not closed", policyPage, policyFence)
}
if len(blocks) != 1 {
return nil, fmt.Errorf("%s holds %d %s blocks, not one", policyPage, len(blocks), policyFence)
}
out := &policy{starts: starts{}, released: defaultReleased}
seen := map[string]bool{}
for _, t := range blocks[0] {
if t == "" || strings.HasPrefix(t, "#") {
continue
}
if m := policyReleasedRE.FindStringSubmatch(t); m != nil {
n, err := strconv.Atoi(m[1])
switch {
case err != nil || n < 1:
return nil, fmt.Errorf("%s: %s is %q, not a number of lines", policyPage, releasedKey, m[1])
case seen[releasedKey]:
return nil, fmt.Errorf("%s: %s is listed twice", policyPage, releasedKey)
}
seen[releasedKey], out.released = true, n
continue
}
m := policyLineRE.FindStringSubmatch(t)
if m == nil {
return nil, fmt.Errorf("%s: %q in the %s block is not `release-X.Y: YYYY-MM-DD`, `%s: YYYY-MM-DD` or `%s: N`", policyPage, t, policyFence, batchOnlyKey, releasedKey)
}
day, err := time.Parse(time.DateOnly, m[2])
if err != nil {
return nil, fmt.Errorf("%s: %q is not a date: %w", policyPage, m[2], err)
}
if seen[m[1]] {
return nil, fmt.Errorf("%s: %s is listed twice", policyPage, m[1])
}
seen[m[1]] = true
if m[1] == batchOnlyKey {
out.batchOnly = day
continue
}
if _, err := parseLine(m[1]); err != nil {
return nil, fmt.Errorf("%s: %w", policyPage, err)
}
out.starts[m[1]] = day
}
if !seen[batchOnlyKey] {
return nil, fmt.Errorf("%s: the %s block gives no %s day", policyPage, policyFence, batchOnlyKey)
}
return out, nil
}
func readPolicy(commit string) (*policy, error) {
page, err := git("show", "--end-of-options", commit+":"+policyPage)
if err != nil {
return nil, fmt.Errorf("reading the policy at %.12s: %w", commit, err)
}
return parsePolicy(page)
}
+212
View File
@@ -0,0 +1,212 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"regexp"
"slices"
"strings"
"testing"
"time"
)
func TestParsePolicy(t *testing.T) {
got, err := parsePolicy("text\n\n" + policyFence + "\n# comment\n\nrelease-1.6: 2026-10-26\nrelease-1.5:2026-01-02\nbatch-only: 2026-10-20\n```\n")
if err != nil {
t.Fatal(err)
}
if len(got.starts) != 2 || !got.starts["release-1.6"].Equal(time.Date(2026, 10, 26, 0, 0, 0, 0, time.UTC)) {
t.Errorf("parsePolicy = %v", got)
}
if !got.batchOnly.Equal(time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC)) {
t.Errorf("the batch-only day = %v, want 2026-10-20", got.batchOnly)
}
if got.released != 2 {
t.Errorf("released lines with none given = %d, want 2", got.released)
}
if got, err := parsePolicy(policyFence + "\nbatch-only: 2026-10-20\n```\n"); err != nil || len(got.starts) != 0 {
t.Errorf("a block with no line = %v, %v; want no starts", got, err)
}
if got, err := parsePolicy(policyFence + "\nbatch-only: 2026-10-20\nreleased-lines: 3\n```\n"); err != nil || got.released != 3 {
t.Errorf("released-lines: 3 = %v, %v; want 3", got, err)
}
for name, page := range map[string]string{
"no batch-only day": policyFence + "\nrelease-1.6: 2026-10-26\n```\n",
"two batch-only days": policyFence + "\nbatch-only: 2026-10-26\nbatch-only: 2026-10-27\n```\n",
"a bad batch-only day": policyFence + "\nbatch-only: soon\n```\n",
"no block": "## Backports\n",
"two blocks": policyFence + "\n```\n" + policyFence + "\n```\n",
"unclosed": policyFence + "\nrelease-1.6: 2026-10-26\n",
"a bad line": policyFence + "\nrelease 1.6 on 2026-10-26\n```\n",
"a bad date": policyFence + "\nrelease-1.6: 2026-13-40\n```\n",
"a padded line": policyFence + "\nrelease-01.6: 2026-10-26\n```\n",
"listed twice": policyFence + "\nrelease-1.6: 2026-10-26\nrelease-1.6: 2026-10-27\n```\n",
"not a line": policyFence + "\nmain: 2026-10-26\n```\n",
"trailing words": policyFence + "\nrelease-1.6: 2026-10-26 or so\n```\n",
"no released lines": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 0\n```\n",
"released lines twice": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 2\nreleased-lines: 3\n```\n",
"released lines words": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: two\n```\n",
"released lines huge": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 99999999999999999999\n```\n",
} {
if _, err := parsePolicy(page); err == nil {
t.Errorf("%s: parsePolicy accepted it", name)
}
}
}
// A line listed in the policy takes nothing merged before its start, unless a
// maintainer lets an original in; any other line starts at its fork point.
func TestPolicyStart(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "feat(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{labels: map[string][]int{labelInclude: {6, 7}}}
verdicts := func() map[int]*verdict {
t.Helper()
f.git("fetch", "-q", "origin")
l, _ := parseLine("release-1.1")
b, err := (&app{gh: gh, stderr: &strings.Builder{}}).prepare(l)
if err != nil {
t.Fatal(err)
}
return map[int]*verdict{5: b.find(5), 6: b.find(6), 7: b.find(7)}
}
if v := verdicts(); v[5].rule != 0 || v[6].rule != 0 || v[7].rule != 0 {
t.Errorf("with no start every original qualifies: %d %d %d", v[5].rule, v[6].rule, v[7].rule)
}
f.setPolicy("release-1.1: 2099-01-01\n")
v := verdicts()
if v[5].rule != ruleBeforeStart {
t.Errorf("#5 = rule %d (%s), want merged before the start", v[5].rule, v[5].reason)
}
if v[6].rule != 0 || v[7].rule != 0 {
t.Errorf("backport/include lifts the start: #6 = rule %d, #7 = rule %d", v[6].rule, v[7].rule)
}
f.setPolicy("release-1.0: 2099-01-01\n")
if v := verdicts(); v[5].rule != 0 {
t.Errorf("a start for another line kept #5 out of release-1.1: rule %d", v[5].rule)
}
f.setPolicy("release-1.1: 2000-01-01\n")
if v := verdicts(); v[5].rule != 0 {
t.Errorf("a start before the fork point kept #5 out: rule %d", v[5].rule)
}
}
// The policy is read from main as the run pins it, or from the ref --policy
// names, and a run without a readable policy stops. Only the commands that
// read the policy take --policy.
func TestPolicyRef(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.git("switch", "-q", "-c", "proposal")
f.write(policyPage, policyWith("release-1.1: 2099-01-01\n"+releasedKey+": 1\n"))
f.git("commit", "-q", "-am", "docs: start release-1.1 later")
f.git("switch", "-q", "main")
code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1")
if code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check against main's policy = %d, %q, %s; want #5 missing", code, stdout, stderr)
}
code, stdout, stderr = f.exec(nil, "check", "--line", "release-1.1", "--policy", "proposal")
if code != 0 {
t.Errorf("check against the proposal's policy = %d, %q, %s; want #5 before the start", code, stdout, stderr)
}
if code, _, _ := f.exec(nil, "check", "--line", "release-1.1", "--policy", "--upload-pack=x"); code != 2 {
t.Errorf("--policy with an option = %d, want 2", code)
}
if code, stdout, stderr := f.exec(nil, "lines", "--policy", "proposal"); code != 0 || stdout != "release-1.1\n" {
t.Errorf("lines against the proposal's policy = %d, %q (%s); want release-1.1 alone", code, stdout, stderr)
}
for _, cmd := range []string{"verify", "notify"} {
if code, _, stderr := f.exec(&fakeGH{}, cmd, "--line", "release-1.1", "--policy", "proposal"); code != 2 || !strings.Contains(stderr, "flag provided but not defined: -policy") {
t.Errorf("%s --policy = %d (%s); want it refused, as %s reads no policy", cmd, code, stderr, cmd)
}
}
f.squash(9001, "docs: drop the page", "", map[string]string{policyPage: ""})
if code, _, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "reading the policy") {
t.Errorf("check without a policy page = %d (%s), want 2", code, stderr)
}
}
// The policy page and the code say the same thing: the start block parses as
// the batch reads it, rule 2 names the types the code takes, the page names
// the scopes the code keeps out, and both override labels.
func TestPolicyPageMatchesCode(t *testing.T) {
data, err := os.ReadFile("../../" + policyPage)
if err != nil {
t.Fatal(err)
}
page := string(data)
pol, err := parsePolicy(page)
if err != nil {
t.Fatalf("the page's policy block does not parse: %v", err)
}
if day, ok := pol.starts["release-1.6"]; !ok || day.Format(time.DateOnly) != "2026-10-26" {
t.Errorf("release-1.6 starts %v, want 2026-10-26", pol.starts["release-1.6"])
}
if day := pol.batchOnly.Format(time.DateOnly); day != "2026-10-26" {
t.Errorf("the batch-only day is %s, want 2026-10-26", day)
}
// The page says how many released lines the batch runs for, rather than
// leaving it to the default.
if !regexp.MustCompile("(?m)^"+releasedKey+":").MatchString(page) || pol.released != 2 {
t.Errorf("the page's block gives %d released lines, want 2 written out", pol.released)
}
rule2 := regexp.MustCompile(`(?m)^2\. Its title type is (.*)$`).FindStringSubmatch(page)
if rule2 == nil {
t.Fatal("the page has no rule 2")
}
var types []string
for _, m := range regexp.MustCompile("`([a-z]+)`").FindAllStringSubmatch(rule2[1], -1) {
types = append(types, m[1])
}
if !slices.Equal(types, takenTypes) {
t.Errorf("rule 2 on the page takes %v, the code %v", types, takenTypes)
}
never := regexp.MustCompile(`(?m)^### What waits for the next minor\n\n(.*)$`).FindStringSubmatch(page)
if never == nil {
t.Fatal("the page has no list of what never goes into a patch release")
}
for _, scope := range excludedScopes {
if !strings.Contains(never[1], "(scope `"+scope+"`)") {
t.Errorf("the page does not keep scope %s out", scope)
}
}
if scopes := regexp.MustCompile("scope `([a-z]+)`").FindAllStringSubmatch(never[1], -1); len(scopes) != len(excludedScopes) {
t.Errorf("the page keeps %d scopes out, the code %d", len(scopes), len(excludedScopes))
}
for i := 1; i <= 6; i++ {
if !regexp.MustCompile("(?m)^" + string(rune('0'+i)) + "\\. ").MatchString(page) {
t.Errorf("the page has no rule %d", i)
}
}
for _, label := range []string{labelInclude, labelSkip} {
if !strings.Contains(page, "`"+label+"`") {
t.Errorf("the page does not describe %s", label)
}
}
}
+682
View File
@@ -0,0 +1,682 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"regexp"
"slices"
"strings"
"time"
)
// A line's record is the backport-batch block of its newest batch pull
// request, read whatever that pull request's state and written only while it
// is open: a merged or closed pull request is never written again, so what it
// recorded stays what the line's later commands act on until a newer batch
// carries a record of its own.
// lineRecord is the one line of JSON in the block. Version 1, the block
// before it had a version, holds only line and deferred; version 3 adds what
// is parked and which units on the batch do only part of their original.
type lineRecord struct {
Version int `json:"version"`
Line string `json:"line"`
Built *builtFrom `json:"built,omitempty"`
Report string `json:"report,omitempty"`
Closing *closing `json:"closing"`
Deferred []deferredEntry `json:"deferred"`
Dropped []droppedEntry `json:"dropped"`
Stale []int `json:"stale"`
Parked []parkedEntry `json:"parked,omitempty"`
Partial []int `json:"partial,omitempty"`
// Changed are the picks that differ from their originals.
Changed []int `json:"changed,omitempty"`
}
// recordVersion is the version a run writes.
const recordVersion = 3
// lineRecordV2 is version 2, which has none of version 3's keys.
type lineRecordV2 struct {
Version int `json:"version"`
Line string `json:"line"`
Built *builtFrom `json:"built,omitempty"`
Report string `json:"report,omitempty"`
Closing *closing `json:"closing"`
Deferred []deferredEntry `json:"deferred"`
Dropped []droppedEntry `json:"dropped"`
Stale []int `json:"stale"`
}
// The kinds of park: an original that builds on what never reaches the
// line, and one whose clean pick would leave part of it without effect.
const (
parkDependency = "dependency"
parkPartial = "partial"
)
// parkedEntry is an original the batch leaves out because it cannot land on
// the line whole: what it needs that never comes, or the paths it would
// leave without effect. announced says notify has named it on the open
// batch, for exactly this entry.
type parkedEntry struct {
Original int `json:"original"`
Merge string `json:"merge"`
Kind string `json:"kind"`
Needs []int `json:"needs,omitempty"`
Commits []string `json:"commits,omitempty"`
Files []string `json:"files,omitempty"`
Announced bool `json:"announced,omitempty"`
}
// parkEvent is what makes a park the same park from one record to the
// next: its merge, kind, prerequisites, commits and files. A park whose
// event changed is news, and notify names it again.
func parkEvent(p parkedEntry) string {
data, _ := json.Marshal(struct {
Merge string `json:"merge"`
Kind string `json:"kind"`
Needs []int `json:"needs"`
Commits []string `json:"commits"`
Files []string `json:"files"`
}{p.Merge, p.Kind, p.Needs, p.Commits, p.Files})
return string(data)
}
// carryAnnounced marks each park announced that the carrier's record holds
// announced as the same event, a closed carrier's too, so a new batch does
// not name again what an earlier one named.
func carryAnnounced(parked []parkedEntry, carrier *lineRecord) []parkedEntry {
out := slices.Clone(parked)
for i := range out {
out[i].Announced = false
}
if carrier == nil {
return out
}
for i := range out {
out[i].Announced = slices.ContainsFunc(carrier.Parked, func(c parkedEntry) bool {
return c.Announced && c.Original == out[i].Original && parkEvent(c) == parkEvent(out[i])
})
}
return out
}
// validate holds a parked entry to what a run writes. Its errors read after
// "the block" or "report <id>".
func (p parkedEntry) validate() error {
sortedUnique := func(n int, less func(i int) bool) bool {
for i := 1; i < n; i++ {
if !less(i) {
return false
}
}
return true
}
switch {
case checkSHA(p.Merge) != nil:
return fmt.Errorf("parks #%d by merge %q", p.Original, p.Merge)
case !sortedUnique(len(p.Needs), func(i int) bool { return p.Needs[i-1] < p.Needs[i] }) || slices.ContainsFunc(p.Needs, func(n int) bool { return n <= 0 || n == p.Original }):
return fmt.Errorf("parks #%d for originals %v, which are not what a run writes", p.Original, p.Needs)
case !sortedUnique(len(p.Commits), func(i int) bool { return p.Commits[i-1] < p.Commits[i] }) || slices.ContainsFunc(p.Commits, func(c string) bool { return checkSHA(c) != nil }):
return fmt.Errorf("parks #%d for commits %q, which are not what a run writes", p.Original, p.Commits)
case !sortedUnique(len(p.Files), func(i int) bool { return p.Files[i-1] < p.Files[i] }) || !slices.Equal(p.Files, cleanFiles(p.Files)):
return fmt.Errorf("parks #%d for files %q, which are not what a run writes", p.Original, p.Files)
}
switch p.Kind {
case parkPartial:
if len(p.Files) == 0 || len(p.Needs)+len(p.Commits) > 0 {
return fmt.Errorf("parks #%d as partial with no files it leaves without effect, or with what it needs", p.Original)
}
case parkDependency:
if len(p.Needs)+len(p.Commits) == 0 || len(p.Files) > 0 {
return fmt.Errorf("parks #%d for a dependency it does not name, or with files", p.Original)
}
default:
return fmt.Errorf("parks #%d as %q, which is no kind of park", p.Original, p.Kind)
}
return nil
}
// builtFrom is what a classification was made of; batch is the head it
// describes.
type builtFrom struct {
Main string `json:"main"`
Line string `json:"line"`
Newer string `json:"newer"`
Policy string `json:"policy"`
Batch string `json:"batch"`
Tool string `json:"tool"`
Run string `json:"run"`
At string `json:"at"`
Options runOptions `json:"options"`
}
type runOptions struct {
NoWorkflowFiles bool `json:"noWorkflowFiles"`
Mode string `json:"mode"`
}
// The operations a record is written by: a run that assembles the batch, and
// the ones that only change what the record says about it.
const (
modeNew = "new"
modeContinue = "continue"
modeDefer = "defer"
modeDrop = "drop"
modeUndrop = "undrop"
modeClose = "close"
)
var modes = []string{modeNew, modeContinue, modeDefer, modeDrop, modeUndrop, modeClose}
// closing is written into the record of an open batch that the run is about
// to close: the head it froze, which the close and the branch's deletion are
// leased on.
type closing struct {
PR int `json:"pr"`
Branch string `json:"branch"`
Head string `json:"head"`
}
// droppedEntry is a maintainer's decision that an original stays off the
// line, bound to its merge commit on main.
type droppedEntry struct {
Original int `json:"original"`
Merge string `json:"merge"`
Reason string `json:"reason"`
On int `json:"on"`
Date string `json:"date"`
}
var (
toolRE = regexp.MustCompile(`^[0-9a-f]{40}(\+dirty)?$`)
reportIDRE = regexp.MustCompile(`^[0-9a-f]{64}$`)
)
// oneLineText is s as a run writes a reason: one line, cleaned and capped.
// A record holding anything else was not written by a run, and its text
// would reach a terminal, a body or a comment as someone else wrote it.
func oneLineText(s string) string { return capped(oneLine(clean(s, false)), titleLimit) }
// fencedLines lists the one line of each block that fence opens.
func fencedLines(body, fence string) ([]string, error) {
var blocks []string
lines := strings.Split(strings.ReplaceAll(body, "\r\n", "\n"), "\n")
for i := 0; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) != fence {
continue
}
if i+2 >= len(lines) || strings.TrimSpace(lines[i+2]) != "```" {
return nil, fmt.Errorf("the %s block is not one line of JSON", fence)
}
blocks = append(blocks, lines[i+1])
i += 2
}
return blocks, nil
}
// decodeStrict reads exactly one JSON value into v, refusing a key v does not
// have and anything after the value.
func decodeStrict(data string, v any) error {
dec := json.NewDecoder(strings.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(v); err != nil {
return err
}
var rest json.RawMessage
if dec.More() || dec.Decode(&rest) != io.EOF {
return errors.New("something follows the JSON value")
}
return nil
}
// parseRecord reads the line's record from a batch body: nil when the body
// has no block, an error when it has more than one or one it cannot vouch
// for whole.
func parseRecord(body string, l line) (*lineRecord, error) {
blocks, err := fencedLines(body, deferredFence)
switch {
case err != nil:
return nil, err
case len(blocks) == 0:
return nil, nil
case len(blocks) > 1:
return nil, fmt.Errorf("the body holds %d %s blocks", len(blocks), deferredFence)
}
var head struct {
Version *int `json:"version"`
}
if err := json.Unmarshal([]byte(blocks[0]), &head); err != nil {
return nil, fmt.Errorf("reading the %s block: %w", deferredFence, err)
}
version := 1
if head.Version != nil {
version = *head.Version
}
var rec lineRecord
switch version {
case 1:
var v1 struct {
Version int `json:"version"`
Line string `json:"line"`
Deferred []deferredEntry `json:"deferred"`
}
err = decodeStrict(blocks[0], &v1)
rec = lineRecord{Version: 1, Line: v1.Line, Deferred: v1.Deferred}
case 2:
var v2 lineRecordV2
err = decodeStrict(blocks[0], &v2)
rec = lineRecordWithout(v2)
case 3:
err = decodeStrict(blocks[0], &rec)
default:
return nil, fmt.Errorf("the %s block is version %d, which this backport-batch does not read; it reads 1 to %d", deferredFence, version, recordVersion)
}
if err == nil && version < 3 && slices.ContainsFunc(rec.Deferred, func(d deferredEntry) bool { return d.Needs != nil }) {
err = fmt.Errorf("version %d has no waits on other originals", version)
}
if err != nil {
return nil, fmt.Errorf("reading the %s block: %w", deferredFence, err)
}
if err := rec.validate(l); err != nil {
return nil, fmt.Errorf("the %s block %w", deferredFence, err)
}
return &rec, nil
}
// validate holds the record to what a run writes. Its errors read after
// "the block".
func (rec *lineRecord) validate(l line) error {
if rec.Line != l.branch() {
return fmt.Errorf("is for %q, not %s", rec.Line, l.branch())
}
seen := map[int]bool{}
once := func(n int) error {
if n <= 0 {
return fmt.Errorf("names original %d", n)
}
if seen[n] {
return fmt.Errorf("names #%d twice", n)
}
seen[n] = true
return nil
}
for _, d := range rec.Deferred {
if err := once(d.Original); err != nil {
return err
}
if d.Commit != "" && checkSHA(d.Commit) != nil {
return fmt.Errorf("names commit %q for #%d", d.Commit, d.Original)
}
// A conflict names the commit that failed and its files, a deferral
// its reason, and a wait its workflow files.
kinds := 0
for _, is := range []bool{d.Commit != "" || len(d.Files) > 0 && !d.Workflows && d.Reason == "" && len(d.Needs) == 0, d.Reason != "", d.Workflows, len(d.Needs) > 0} {
if is {
kinds++
}
}
switch {
case d.Workflows && (d.Commit != "" || d.Reason != ""):
return fmt.Errorf("has #%d wait for a maintainer and defers it as well", d.Original)
case kinds != 1:
return fmt.Errorf("does not say whether #%d is a conflict, deferred for a reason or waiting", d.Original)
case d.Reason != "" && strings.TrimSpace(d.Reason) == "":
return fmt.Errorf("defers #%d for an empty reason", d.Original)
case d.Reason != oneLineText(d.Reason):
return fmt.Errorf("defers #%d for %q, which is no reason a run writes", d.Original, d.Reason)
case !slices.Equal(d.Files, cleanFiles(d.Files)):
return fmt.Errorf("names files of #%d that are not as a run writes them: %q", d.Original, d.Files)
case d.More < 0:
return fmt.Errorf("counts %d more files for #%d", d.More, d.Original)
case len(d.Needs) > 0 && (len(d.Files) > 0 || d.More != 0):
return fmt.Errorf("has #%d wait on other originals with files of its own", d.Original)
}
for i, n := range d.Needs {
if n <= 0 || n == d.Original || i > 0 && d.Needs[i-1] >= n {
return fmt.Errorf("has #%d wait on %v, which is not what a run writes", d.Original, d.Needs)
}
}
}
for _, d := range rec.Dropped {
if err := once(d.Original); err != nil {
return err
}
if _, err := time.Parse(time.DateOnly, d.Date); err != nil || checkSHA(d.Merge) != nil || strings.TrimSpace(d.Reason) == "" || d.On <= 0 {
return fmt.Errorf("drops #%d with a merge, reason, pull request or date it cannot have", d.Original)
}
if d.Reason != oneLineText(d.Reason) {
return fmt.Errorf("drops #%d for %q, which is no reason a run writes", d.Original, d.Reason)
}
}
for _, p := range rec.Parked {
if err := once(p.Original); err != nil {
return err
}
if err := p.validate(); err != nil {
return err
}
}
staleSeen := map[int]bool{}
for _, n := range rec.Stale {
if n <= 0 || staleSeen[n] {
return fmt.Errorf("lists %d as still on the batch more than once or not at all", n)
}
staleSeen[n] = true
}
changedSeen := map[int]bool{}
for _, n := range rec.Changed {
if n <= 0 || changedSeen[n] {
return fmt.Errorf("lists %d as a changed pick more than once or not at all", n)
}
changedSeen[n] = true
}
if err := rec.waitsHold(); err != nil {
return err
}
// What is on the batch is neither deferred nor parked.
left := map[int]bool{}
for _, d := range rec.Deferred {
left[d.Original] = true
}
for _, p := range rec.Parked {
left[p.Original] = true
if slices.Contains(p.Commits, p.Merge) {
return fmt.Errorf("parks #%d for its own merge", p.Original)
}
}
for _, list := range [][]int{rec.Partial, rec.Changed} {
for _, n := range list {
if left[n] {
return fmt.Errorf("has #%d on the batch and left off it at once", n)
}
}
}
partialSeen := map[int]bool{}
for _, n := range rec.Partial {
// A unit that does only part of its original is still on the batch.
if !staleSeen[n] || partialSeen[n] {
return fmt.Errorf("lists #%d as a partial unit more than once, or not as still on the batch", n)
}
partialSeen[n] = true
}
if rec.Version < 2 {
return nil
}
if b := rec.Built; b == nil {
return errors.New("does not say what it was built from")
} else if err := b.validate(); err != nil {
return err
}
if !reportIDRE.MatchString(rec.Report) {
return fmt.Errorf("names report %q", rec.Report)
}
if c := rec.Closing; c != nil {
m := batchBranchRE.FindStringSubmatch(c.Branch)
if c.PR <= 0 || m == nil || m[1] != l.branch() || checkSHA(c.Head) != nil {
return fmt.Errorf("closes #%d on %q at %q, which is no batch of %s", c.PR, c.Branch, c.Head, l.branch())
}
}
return nil
}
func (b *builtFrom) validate() error {
for _, sha := range []string{b.Main, b.Line, b.Policy, b.Batch} {
if checkSHA(sha) != nil {
return fmt.Errorf("says it was built from %q", sha)
}
}
if b.Newer != "" && checkSHA(b.Newer) != nil {
return fmt.Errorf("says it was built from %q", b.Newer)
}
if b.Tool != "" && !toolRE.MatchString(b.Tool) {
return fmt.Errorf("names tool %q", b.Tool)
}
if b.Run != "" && !runURLRE.MatchString(b.Run) {
return fmt.Errorf("names run %q", b.Run)
}
if _, err := time.Parse(time.RFC3339, b.At); err != nil {
return fmt.Errorf("was built at %q", b.At)
}
if !slices.Contains(modes, b.Options.Mode) {
return fmt.Errorf("was written by %q, which is no operation of backport-batch", b.Options.Mode)
}
return nil
}
var runURLRE = regexp.MustCompile(`^https://[^/\s]+/[^/\s]+/[^/\s]+/actions/runs/[0-9]+(/attempts/[0-9]+)?$`)
// waitsHold holds what waits and parks need to what the record says of the
// originals they need: a wait needs ones still to come, so none parked or
// dropped, a park ones that never come, so none deferred, and no wait ends
// up waiting on itself. Its errors read after "the block".
func (rec *lineRecord) waitsHold() error {
deferred, never := map[int][]int{}, map[int]bool{}
for _, d := range rec.Deferred {
deferred[d.Original] = d.Needs
}
for _, p := range rec.Parked {
never[p.Original] = true
}
for _, d := range rec.Dropped {
never[d.Original] = true
}
for _, d := range rec.Deferred {
for _, n := range d.Needs {
if never[n] {
return fmt.Errorf("has #%d wait on #%d, which never comes", d.Original, n)
}
}
}
for _, p := range rec.Parked {
for _, n := range p.Needs {
if _, ok := deferred[n]; ok {
return fmt.Errorf("parks #%d for #%d, which is still to come", p.Original, n)
}
}
}
needs := map[int][]int{}
for _, d := range rec.Deferred {
needs[d.Original] = d.Needs
}
for _, p := range rec.Parked {
needs[p.Original] = p.Needs
}
return waitCycle(needs)
}
// waitCycle refuses waits that lead back to where they started, which no
// run writes: a prerequisite merged before what needs it.
func waitCycle(needs map[int][]int) error {
state := map[int]int{} // 1 on the path, 2 done
var visit func(n int) error
visit = func(n int) error {
switch state[n] {
case 1:
return fmt.Errorf("has #%d wait on itself through what it waits on", n)
case 2:
return nil
}
state[n] = 1
for _, f := range needs[n] {
if err := visit(f); err != nil {
return err
}
}
state[n] = 2
return nil
}
for _, n := range slices.Sorted(maps.Keys(needs)) {
if err := visit(n); err != nil {
return err
}
}
return nil
}
// lineRecordWithout is a version 2 record as the newest version holds it.
func lineRecordWithout(v2 lineRecordV2) lineRecord {
return lineRecord{Version: v2.Version, Line: v2.Line, Built: v2.Built, Report: v2.Report, Closing: v2.Closing, Deferred: v2.Deferred, Dropped: v2.Dropped, Stale: v2.Stale}
}
// recordBlock is the block that holds rec. One line of JSON: encoding/json
// escapes newlines and <, > and &, so nothing in a file name or a reason can
// end the fence or open HTML.
func recordBlock(rec lineRecord) string {
data, _ := json.Marshal(rec)
return deferredFence + "\n" + string(data) + "\n```"
}
// savedNoWorkflowFiles is the --no-workflow-files the open batch was last
// assembled with, which an operation that only changes the record keeps. A
// record from before it was saved says so by listing a wait.
func (s recordSource) savedNoWorkflowFiles() bool {
if !s.open || s.rec == nil {
return false
}
if s.rec.Built != nil {
return s.rec.Built.Options.NoWorkflowFiles
}
return slices.ContainsFunc(s.rec.Deferred, func(d deferredEntry) bool { return d.Workflows })
}
// dropped is what the record drops, which every later record keeps.
func (rec *lineRecord) dropped() []droppedEntry {
if rec == nil {
return nil
}
return slices.Clone(rec.Dropped)
}
// deferred maps the originals the record defers to their entries.
func (rec *lineRecord) deferred() map[int]deferredEntry {
out := map[int]deferredEntry{}
if rec != nil {
for _, d := range rec.Deferred {
out[d.Original] = d
}
}
return out
}
// recordSource is where a line's record was found.
type recordSource struct {
// carrier is the pull request whose block is the record, nil when the
// line has none.
carrier *pullRequest
// open says the carrier is the open batch, so the record may be written.
open bool
rec *lineRecord
// code is the open batch pull request, if any. One whose body someone
// wrote by hand is the batch the line builds on but carries no record.
code *pullRequest
}
// closingOpen is the close the record says is under way on a batch that is
// still open, or nil.
func (s recordSource) closingOpen() *closing {
if !s.open || s.rec == nil {
return nil
}
return s.rec.Closing
}
func prState(pr *pullRequest) string {
switch {
case pr.MergedAt != nil:
return "merged"
case pr.State == "" || pr.State == "open":
return "open"
case pr.State == "closed":
return "closed"
}
return fmt.Sprintf("in state %q", pr.State)
}
func hasRecord(body string) bool {
return slices.ContainsFunc(strings.Split(body, "\n"), func(ln string) bool { return strings.TrimSpace(ln) == deferredFence })
}
// findLineRecord finds the line's record: the block of the newest pull
// request opened from a batch branch of this repository for the line by an
// author the batch trusts, open, merged or closed. open is the open batch
// pull request batchPull chose, or nil. Anything that leaves in doubt which
// record is the newest is an error, never a fall back to an older one: a
// newer block by an author the batch does not trust, a block the bot wrote
// and someone removed, a record it cannot read, a closed record newer than
// the open batch, or more pull requests than it reads.
func findLineRecord(gh github, l line, open *pullRequest) (recordSource, error) {
src := recordSource{code: open}
for page := 1; src.carrier == nil; page++ {
if page > recentPullsPages {
return src, fmt.Errorf("none of the %d newest pull requests against %s is a batch pull request with a record, so the line's record cannot be told apart from none", recentPullsPages*recentPullsPage, l.branch())
}
prs, err := gh.recentPulls(l.branch(), page)
if err != nil {
return src, err
}
for i := range prs {
pr := &prs[i]
m := batchBranchRE.FindStringSubmatch(pr.Head.Ref)
if m == nil || m[1] != l.branch() || pr.Base.Ref != l.branch() || pr.Head.Repo == nil || pr.Head.Repo.FullName != pr.Base.Repo.FullName {
continue
}
if open != nil && pr.Number == open.Number {
pr = open
}
if !hasRecord(pr.Body) {
if pr.User.Type == "Bot" && gh.batchBot() != "" && pr.User.Login == gh.batchBot() {
return src, fmt.Errorf("batch pull request #%d (%s) was opened by %s but carries no %s block; someone removed it, so the line's record is unknown", pr.Number, prState(pr), pr.User.Login, deferredFence)
}
continue
}
why, err := trusted(gh, *pr)
if err != nil {
return src, fmt.Errorf("batch pull request #%d: %w", pr.Number, err)
}
if why != "" {
return src, fmt.Errorf("batch pull request #%d (%s) carries a %s block but was %s; an older record is not taken in its place", pr.Number, prState(pr), deferredFence, why)
}
rec, err := parseRecord(pr.Body, l)
if err != nil {
return src, fmt.Errorf("batch pull request #%d (%s): %w", pr.Number, prState(pr), err)
}
src.carrier, src.rec = pr, rec
break
}
if len(prs) < recentPullsPage {
break
}
}
switch c := src.carrier; {
case open != nil && hasRecord(open.Body) && (c == nil || c.Number < open.Number):
return src, fmt.Errorf("GitHub does not list the open batch pull request #%d among the pull requests against %s; run again", open.Number, l.branch())
case open != nil && hasRecord(open.Body) && c.Number > open.Number:
return src, fmt.Errorf("batch pull request #%d (%s) carries a record newer than the open batch pull request #%d; close one of them by hand", c.Number, prState(c), open.Number)
case open != nil && c == open:
src.open = true
case c != nil && prState(c) == "open":
return src, fmt.Errorf("batch pull request #%d carries the line's record and is open, but is not the batch this run builds on", c.Number)
}
return src, nil
}
+433
View File
@@ -0,0 +1,433 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strings"
"testing"
)
// The records the live batches carried before records had a version, as
// GitHub returned them on 2026-11-09, read as version 1.
func TestRecordReadsVersion1(t *testing.T) {
for line, block := range map[string]string{
"release-1.7": `{"line":"release-1.7","deferred":[{"original":4756,"commit":"647407545d630291cfe2a20880cce45af22edf0c","files":["packages/system/backupstrategy-controller/values.yaml"]},{"original":4867,"commit":"813c6180861efe1896c26d62a7afcbc1ff590ebc","files":[".github/renovate.json"]},{"original":5082,"commit":"e93db8848027450c2e23d21348409efa641cb6f1","files":["pkg/registry/core/tap/rest.go"]},{"original":4888,"files":[".github/workflows/cut-prerelease.yaml"],"workflows":true},{"original":4971,"files":[".github/workflows/e2e-proxmox.yaml"],"workflows":true}]}`,
"release-1.6": `{"line":"release-1.6","deferred":[{"original":5071,"commit":"3fa534f3cf6cb5774a183f0202f7f5c13e9ae211","files":["internal/operator/tapmaterializer_reconciler.go","internal/operator/tapmaterializer_reconciler_test.go"]},{"original":5086,"commit":"5265ee662c591db409c8ef1ed8bb457cf29f5c22","files":["packages/apps/tenant/templates/gateway.yaml"]},{"original":5087,"commit":"8d8179f20a2c10de03face1472435d5083cde38d","files":["internal/controller/applicationdefinition_helmreconciler.go","internal/controller/applicationdefinition_helmreconciler_test.go"]},{"original":5142,"files":[".github/workflows/pull-requests-release.yaml",".github/workflows/update-releasenotes.yaml"],"workflows":true}]}`,
} {
l, _ := parseLine(line)
rec, err := parseRecord("## What this PR does\n\ntext\n\n"+deferredFence+"\n"+block+"\n```\n\n### Release note\n", l)
if err != nil {
t.Fatalf("%s: parseRecord = %v", line, err)
}
waits, conflicts := 0, 0
for _, e := range rec.Deferred {
switch {
case e.Workflows:
waits++
case e.Commit != "":
conflicts++
}
}
if rec.Version != 1 || rec.Line != line || rec.Closing != nil || len(rec.Dropped) != 0 || waits+conflicts != len(rec.Deferred) || waits == 0 || conflicts == 0 {
t.Errorf("%s: record = %+v; want version 1 with its conflicts and waits", line, rec)
}
}
}
// v2 is a version 2 record with each value it may hold.
func v2(extra string) string {
sha := strings.Repeat("a", 40)
return `{"version":2,"line":"release-1.1","built":{"main":"` + sha + `","line":"` + sha + `","newer":"","policy":"` + sha + `","batch":"` + sha + `","tool":"` + sha + `+dirty","run":"","at":"2026-10-26T06:00:00Z","options":{"noWorkflowFiles":true,"mode":"continue"}},` +
`"report":"` + strings.Repeat("b", 64) + `","closing":null,"deferred":[{"original":5,"commit":"` + sha + `","files":["app/five.txt"]},{"original":6,"files":[],"reason":"fails the e2e"},{"original":7,"files":[".github/workflows/x.yaml"],"workflows":true}],` +
`"dropped":[{"original":8,"merge":"` + sha + `","reason":"needs #4","on":90,"date":"2026-10-26"}],"stale":[9]` + extra + `}`
}
// A record is read whole or not at all: anything it cannot vouch for stops
// the command rather than being half believed.
func TestRecordRefuses(t *testing.T) {
l, _ := parseLine("release-1.1")
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
if rec, err := parseRecord(block(v2("")), l); err != nil || rec.Version != 2 || len(rec.Dropped) != 1 || rec.Built.Options.Mode != "continue" {
t.Fatalf("parseRecord of a whole v2 record = %+v, %v", rec, err)
}
sha := strings.Repeat("a", 40)
for name, json := range map[string]string{
"version 4": strings.Replace(v2(""), `"version":2`, `"version":4`, 1),
"trailing data": v2("") + `{}`,
"trailing text": v2("") + ` x`,
"an original listed twice": strings.Replace(v2(""), `"original":8`, `"original":5`, 1),
"a needs entry with a commit": strings.Replace(v2(""), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"commit":"`+sha+`","files":[],"needs":[4]}`, 1),
"an empty reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":""`, 1),
"an empty drop reason": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":" "`, 1),
"a bad tool": strings.Replace(v2(""), `+dirty"`, `-dirty"`, 1),
"an unknown mode": strings.Replace(v2(""), `"mode":"continue"`, `"mode":"rebase"`, 1),
"a reason with a commit": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"x","commit":"`+sha+`"`, 1),
"a short merge": strings.Replace(v2(""), `"merge":"`+sha+`"`, `"merge":"abc"`, 1),
"a bad date": strings.Replace(v2(""), `"date":"2026-10-26"`, `"date":"26.10.2026"`, 1),
"a bad time": strings.Replace(v2(""), `"at":"2026-10-26T06:00:00Z"`, `"at":"yesterday"`, 1),
"a short report": strings.Replace(v2(""), strings.Repeat("b", 64), "bbbb", 1),
"a closing on another line": strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"backport-batch/release-1.0-20261026","head":"`+sha+`"}`, 1),
"a closing without a head": strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"`+batchBranch1+`","head":""}`, 1),
"a negative stale": strings.Replace(v2(""), `"stale":[9]`, `"stale":[-9]`, 1),
"an unknown key": v2(`,"parked":[]`),
"an entry of no kind": strings.Replace(v2(""), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"files":[]}`, 1),
"another line": strings.Replace(v2(""), `"line":"release-1.1"`, `"line":"release-1.0"`, 1),
"an escape in a reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"fails\u001b[2J the e2e"`, 1),
"a newline in a reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"fails\nwarning: forged"`, 1),
"an escape in a drop": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"needs\u001b]0;x\u0007 #4"`, 1),
"a newline in a drop": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"needs #4\nnote: forged"`, 1),
"a reason past its length": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"`+strings.Repeat("x", titleLimit+1)+`"`, 1),
"a bidi file name": strings.Replace(v2(""), `"files":["app/five.txt"]`, `"files":["app/\u202efive.txt"]`, 1),
} {
if _, err := parseRecord(block(json), l); err == nil {
t.Errorf("%s: parseRecord accepted it", name)
}
}
closing := strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"`+batchBranch1+`","head":"`+sha+`"}`, 1)
if rec, err := parseRecord(block(closing), l); err != nil || rec.Closing == nil || rec.Closing.PR != 90 {
t.Errorf("a record that is closing = %+v, %v", rec, err)
}
}
// recordPR is a batch pull request on release-1.1 carrying body, numbered n,
// in state, opened by login.
func recordPR(n int, state, login, body string) pullRequest {
pr := notifyPR("20261026", body)
pr.Number, pr.State = n, state
pr.Head.Ref = fmt.Sprintf("backport-batch/release-1.1-202610%02d", n%30+1)
pr.User.Login = login
if strings.HasSuffix(login, "[bot]") {
pr.User.Type = "Bot"
}
return pr
}
func recordBody(conflict int) string {
return "text\n\n" + deferredFence + "\n" + fmt.Sprintf(`{"line":"release-1.1","deferred":[{"original":%d,"commit":"%s","files":["a"]}]}`, conflict, strings.Repeat("a", 40)) + "\n```\n"
}
// The record is the block of the newest batch pull request that carries one,
// whatever its state, among those a trusted author opened from a batch branch
// of this repository; anything that leaves the newest record in doubt stops
// the command rather than falling back to an older one.
func TestFindLineRecord(t *testing.T) {
const bot = defaultBatchBot
other := func(n int) pullRequest {
pr := recordPR(n, "closed", "maintainer", "")
pr.Head.Ref = fmt.Sprintf("fix-%d", n)
return pr
}
fork := recordPR(92, "closed", "maintainer", recordBody(92))
fork.Head.Repo = &struct {
FullName string `json:"full_name"`
}{"someone/cozystack"}
cases := []struct {
name string
prs []pullRequest
open int // the open batch the caller found, or 0
bot string
carrier int // 0 for none
isOpen bool
refusal string
pages int
}{
{name: "the open batch first", prs: []pullRequest{recordPR(92, "open", "maintainer", recordBody(92)), recordPR(91, "closed", "maintainer", recordBody(91))}, open: 92, carrier: 92, isOpen: true},
{name: "the newest closed record", prs: []pullRequest{recordPR(91, "closed", bot, recordBody(91)), recordPR(90, "closed", bot, recordBody(90))}, bot: bot, carrier: 91},
{name: "a person's batch without a block is passed over", prs: []pullRequest{recordPR(92, "closed", "maintainer", "picked by hand"), recordPR(91, "closed", "maintainer", recordBody(91))}, carrier: 91},
{name: "the bot's batch without a block", prs: []pullRequest{recordPR(92, "closed", bot, "edited away"), recordPR(91, "closed", bot, recordBody(91))}, bot: bot, refusal: "carries no ```backport-batch block"},
{name: "a fork is no batch", prs: []pullRequest{fork, recordPR(91, "closed", "maintainer", recordBody(91))}, carrier: 91},
{name: "a newer record by someone untrusted", prs: []pullRequest{recordPR(92, "closed", "someone", recordBody(92)), recordPR(91, "closed", "maintainer", recordBody(91))}, refusal: "#92"},
{name: "a closed record newer than the open batch", prs: []pullRequest{recordPR(92, "closed", "maintainer", recordBody(92)), recordPR(91, "open", "maintainer", recordBody(91))}, open: 91, refusal: "newer than the open batch pull request #91"},
{name: "a record on the second page", prs: append(func() (out []pullRequest) {
for n := 1000; n < 1000+recentPullsPage; n++ {
out = append(out, other(n))
}
return out
}(), recordPR(91, "closed", "maintainer", recordBody(91))), carrier: 91, pages: 2},
{name: "ten full pages without a record", prs: func() (out []pullRequest) {
for n := 1000; n < 1000+10*recentPullsPage+1; n++ {
out = append(out, other(n))
}
return out
}(), refusal: "pull requests", pages: 10},
{name: "the bot's record when no bot is trusted", prs: []pullRequest{recordPR(91, "closed", bot, recordBody(91))}, refusal: "#91"},
{name: "a hand-written open batch beside a closed record", prs: []pullRequest{recordPR(92, "open", "maintainer", "picked by hand"), recordPR(91, "closed", "maintainer", recordBody(91))}, open: 92, carrier: 91},
{name: "no batch ever", prs: []pullRequest{other(5)}},
{name: "an unreadable record", prs: []pullRequest{recordPR(91, "closed", "maintainer", "text\n\n"+deferredFence+"\n{\n```\n")}, refusal: "#91 (closed)"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
l, _ := parseLine("release-1.1")
gh := &fakeGH{prs: tc.prs, bot: tc.bot}
var open *pullRequest
for i := range tc.prs {
if tc.prs[i].Number == tc.open {
open = &tc.prs[i]
}
}
src, err := findLineRecord(gh, l, open)
switch {
case tc.refusal != "":
if err == nil || !strings.Contains(err.Error(), tc.refusal) {
t.Errorf("findLineRecord = %+v, %v; want a refusal with %q", src, err, tc.refusal)
}
case err != nil:
t.Errorf("findLineRecord: %v", err)
case tc.carrier == 0 && (src.carrier != nil || src.rec != nil):
t.Errorf("found #%d, want no record", src.carrier.Number)
case tc.carrier != 0 && (src.carrier == nil || src.carrier.Number != tc.carrier || src.rec == nil || src.open != tc.isOpen || src.rec.Deferred[0].Original != tc.carrier):
t.Errorf("found %+v, want #%d open=%v", src, tc.carrier, tc.isOpen)
}
if tc.pages != 0 && gh.listed != tc.pages {
t.Errorf("read %d pages, want %d", gh.listed, tc.pages)
}
if tc.open != 0 && src.code != open {
t.Errorf("the batch to build on is %+v, want #%d", src.code, tc.open)
}
})
}
}
// A run that picks nothing while no batch is open writes nothing, since
// merged and closed batches are never written: it says its classification is
// not kept, and check still reads the merged batch's record.
func TestZeroPickRunLeavesNoRecord(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n", "app/seven.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
mergeBatch(f, gh)
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
edits, comments := len(gh.edited), len(gh.issueComments)
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 0 || !strings.Contains(stderr, "no batch pull request is open on release-1.1; this run's classification is not retained") {
t.Errorf("run = %d, want 0 saying nothing is retained:\n%s", code, stderr)
}
if len(gh.created) != 1 || len(gh.edited) != edits || len(gh.issueComments) != comments || f.remoteHead(batchBranch1) != "" {
t.Errorf("created %q, edited %q, %d comments, branch at %q; want nothing written", gh.created, gh.edited, len(gh.issueComments), f.remoteHead(batchBranch1))
}
f.git("switch", "-q", "main")
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
if code != 1 || !strings.Contains(stdout, "/pull/7 ") || strings.Contains(stdout, "/pull/5 ") || !strings.Contains(stderr, "deferred conflict on #90 (merged)") {
t.Errorf("check = %d, want 1 for #7 alone and #5 still a deferred conflict:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
}
// The open batch's author is asked for once although both the open batch and
// its record are read.
func TestRecordAsksForAPermissionOnce(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
gh.asked = nil
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 {
t.Fatalf("check = %d\n%s", code, stderr)
}
if len(gh.asked) != 1 {
t.Errorf("asked GitHub for %q, want the author once", gh.asked)
}
}
// The record and the report name what the classification was made of: the
// heads it read, the policy, the tool, the job, the time and the flags.
func TestRecordNamesItsInputs(t *testing.T) {
f := newFixture(t)
f.lines()
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.onLine("release-1.1", "fix(app): five\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "5\n"})
f.tool = strings.Repeat("c", 40) + "+dirty"
t.Setenv("GITHUB_SERVER_URL", "https://github.com")
t.Setenv("GITHUB_REPOSITORY", "cozystack/cozystack")
t.Setenv("GITHUB_RUN_ID", "123")
t.Setenv("GITHUB_RUN_ATTEMPT", "2")
gh := &fakeGH{}
stdout := mustRun(t, f, gh, "run", "--line", "release-1.0", "--no-workflow-files", "--push")
l, _ := parseLine("release-1.0")
const branch = "backport-batch/release-1.0-20261026"
rec, err := parseRecord(stdout, l)
if err != nil {
t.Fatal(err)
}
want := builtFrom{Main: f.git("rev-parse", "origin/main"), Line: f.git("rev-parse", "origin/release-1.0"), Newer: f.git("rev-parse", "origin/release-1.1"),
Policy: f.git("rev-parse", "origin/main"), Batch: f.remoteHead(branch), Tool: f.tool, Run: "https://github.com/cozystack/cozystack/actions/runs/123/attempts/2",
At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeNew}}
if rec.Version != recordVersion || *rec.Built != want || bodyReportOf(t, stdout, l).doc.Built != want || rec.Report != bodyReportOf(t, stdout, l).doc.ID {
t.Errorf("record = %+v, built %+v; want built %+v and the report it names", rec, rec.Built, want)
}
// A policy from another ref, a run by hand that continues the batch.
f.git("push", "-q", "origin", "origin/main:refs/heads/policy-draft")
f.git("fetch", "-q", "origin")
t.Setenv("GITHUB_RUN_ID", "")
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
mc6 := f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.onLine("release-1.1", "fix(app): six\n\n(cherry picked from commit "+mc6+")", map[string]string{"app/six.txt": "6\n"})
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--continue", "--policy", "origin/policy-draft", "--push")
if rec, err = parseRecord(stdout, l); err != nil {
t.Fatal(err)
}
if b := rec.Built; b.Policy != f.git("rev-parse", "origin/policy-draft") || b.Main != f.git("rev-parse", "origin/main") || b.Run != "" || b.Options != (runOptions{Mode: modeContinue}) || b.Batch != f.remoteHead(branch) {
t.Errorf("built %+v; want the draft's policy, no job and a continuation without the flag", b)
}
}
// What a run assembled with --no-workflow-files stays saved through the
// operations that only change the record, which take no such flag: check
// keeps failing the cut on the wait they leave in place.
func TestMetadataOperationsKeepTheMode(t *testing.T) {
for _, v1 := range []bool{false, true} {
t.Run(fmt.Sprintf("an open record of version 1: %v", v1), func(t *testing.T) {
f := workflowWorld(t)
f.onLine("release-1.1", "fix(app): a line-only nine", map[string]string{"app/nine.txt": "line\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
f.pr(9, "fix(app): nine", "", map[string]string{"app/nine.txt": "9\n"})
gh := &fakeGH{}
stdout := mustRun(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
reopen(f, gh)
if v1 {
waits := []pick{{v: &verdict{o: &original{number: 5}}, files: []string{".github/workflows/five.yaml"}}}
gh.prs[0].Body = strings.Replace(stdout, strings.Split(strings.Split(stdout, deferredFence+"\n")[1], "\n")[0], strings.Split(deferredBlock(notifyLine, nil, nil, waits), "\n")[1], 1)
} else if rec, err := parseRecord(stdout, notifyLine); err != nil || !rec.Built.Options.NoWorkflowFiles {
t.Fatalf("the scheduled run saved %+v, %v", rec, err)
}
for _, op := range []struct {
args []string
mode string
}{
{[]string{"--drop", "9", "--reason", "needs #4"}, modeDrop},
{[]string{"--undrop", "9"}, modeUndrop},
{[]string{"--defer", "7", "--reason", "fails the e2e"}, modeDefer},
} {
stdout = mustRun(t, f, gh, append(append([]string{"run", "--line", "release-1.1"}, op.args...), "--push")...)
rec, err := parseRecord(stdout, notifyLine)
if err != nil || rec.Built.Options != (runOptions{NoWorkflowFiles: true, Mode: op.mode}) {
t.Errorf("after %q the record is %+v, %v; want the flag kept", op.args, rec.Built, err)
}
reopen(f, gh)
}
var rec *lineRecord
var err error
reopen(f, gh)
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/") {
t.Errorf("check --cut = %d, want #5 still waiting:\n%s", code, stdout)
}
// Deferring the last picks closes the batch, and its record
// still says how the batch was assembled.
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "6,8", "--reason", "fails the e2e", "--push")
rec, err = parseRecord(gh.find(90).Body, notifyLine)
if err != nil || gh.find(90).State != "closed" || rec.Closing == nil || rec.Built.Options != (runOptions{NoWorkflowFiles: true, Mode: modeClose}) {
t.Errorf("after the close the record is %+v, %v; want the flag kept", rec.Built, err)
}
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check --cut = %d, want #5 missing once no batch lists it:\n%s", code, stdout)
}
})
}
}
// A state is named only when it is one GitHub uses; anything else is quoted.
func TestPRStateNamesOnlyKnownStates(t *testing.T) {
at := "2026-10-26T12:00:00Z"
for _, tc := range []struct {
pr pullRequest
want string
}{
{pullRequest{State: "open"}, "open"},
{pullRequest{State: ""}, "open"},
{pullRequest{State: "closed"}, "closed"},
{pullRequest{State: "closed", MergedAt: &at}, "merged"},
{pullRequest{State: "open\n\x1b[2Jforged"}, `in state "open\n\x1b[2Jforged"`},
} {
if got := prState(&tc.pr); got != tc.want {
t.Errorf("prState(%q) = %q, want %q", tc.pr.State, got, tc.want)
}
}
}
// v3 is v2 as version 3, with extra after the stale list.
func v3(extra string) string {
return strings.Replace(v2(extra), `"version":2`, `"version":3`, 1)
}
// Version 3 adds what is parked and which units do only part of their
// original, each held to what a run writes, and version 2 has neither.
func TestRecordVersion3(t *testing.T) {
l, _ := parseLine("release-1.1")
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
sha, other := strings.Repeat("a", 40), strings.Repeat("c", 40)
parked := `,"parked":[{"original":10,"merge":"` + sha + `","kind":"partial","files":["a","b"]},{"original":11,"merge":"` + sha + `","kind":"dependency","needs":[3,4],"commits":["` + other + `"],"announced":true}],"partial":[9]`
waits := strings.Replace(v3(parked), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"files":[],"needs":[3,5]}`, 1)
rec, err := parseRecord(block(waits), l)
if err != nil || rec.Version != 3 || len(rec.Parked) != 2 || !rec.Parked[1].Announced || !slices.Equal(rec.Partial, []int{9}) || !slices.Equal(rec.deferred()[6].Needs, []int{3, 5}) {
t.Fatalf("parseRecord of a whole v3 record = %+v, %v", rec, err)
}
for name, json := range map[string]string{
"version 1 with a wait": `{"line":"release-1.1","deferred":[{"original":6,"files":[],"needs":[3,5]}]}`,
"a wait with files": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"files":["a"],"needs":[3,5]`, 1),
"a wait that is a conflict": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"commit":"`+sha+`","files":[],"needs":[3,5]`, 1),
"a wait on itself": strings.Replace(waits, `"needs":[3,5]`, `"needs":[3,6]`, 1),
"an unsorted wait": strings.Replace(waits, `"needs":[3,5]`, `"needs":[5,3]`, 1),
"a wait on nothing": strings.Replace(waits, `"needs":[3,5]`, `"needs":[0]`, 1),
"a wait cycle": strings.Replace(strings.Replace(waits, `"needs":[3,5]`, `"needs":[12]`, 1), `"deferred":[`, `"deferred":[{"original":12,"files":[],"needs":[6]},`, 1),
"a wait on a parked one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[10]`, 1),
"a wait on a dropped one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[8]`, 1),
"a park on a deferred one": strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,5]`, 1),
"a partial unit also deferred": strings.Replace(strings.Replace(waits, `"stale":[9]`, `"stale":[9,5]`, 1), `"partial":[9]`, `"partial":[9,5]`, 1),
"a changed pick also parked": v3(parked + `,"changed":[10]`),
"a changed pick also deferred": v3(parked + `,"changed":[5]`),
"a park on its own merge": strings.Replace(waits, `"commits":["`+other+`"]`, `"commits":["`+sha+`"]`, 1),
"a park cycle": strings.Replace(strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,13]`, 1), `"parked":[`, `"parked":[{"original":13,"merge":"`+sha+`","kind":"dependency","needs":[11]},`, 1),
} {
if _, err := parseRecord(block(json), l); err == nil {
t.Errorf("%s: parseRecord accepted it", name)
}
}
for name, json := range map[string]string{
"version 2 with a park": v2(parked),
"version 4": strings.Replace(v3(""), `"version":3`, `"version":4`, 1),
"a park of a deferred original": v3(strings.Replace(parked, `"original":10`, `"original":5`, 1)),
"a park of a dropped original": v3(strings.Replace(parked, `"original":10`, `"original":8`, 1)),
"a partial park with no files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":[]`, 1)),
"a partial park that needs": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a"],"needs":[3]`, 1)),
"unsorted files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["b","a"]`, 1)),
"a file that drives a terminal": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a\u001b[2J"]`, 1)),
"a dependency park with no need": v3(strings.Replace(strings.Replace(parked, `"needs":[3,4],`, ``, 1), `"commits":["`+other+`"],`, ``, 1)),
"unsorted needs": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[4,3]`, 1)),
"a park that needs itself": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[3,11]`, 1)),
"a short commit": v3(strings.Replace(parked, `"commits":["`+other+`"]`, `"commits":["abc"]`, 1)),
"a short merge": v3(strings.Replace(parked, `"merge":"`+sha+`","kind":"partial"`, `"merge":"abc","kind":"partial"`, 1)),
"an unknown kind": v3(strings.Replace(parked, `"kind":"partial"`, `"kind":"later"`, 1)),
"a partial unit not on the batch": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[10]`, 1)),
"a partial unit twice": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[9,9]`, 1)),
"a changed pick twice": v3(parked + `,"changed":[4,4]`),
"a changed pick that is none": v3(parked + `,"changed":[0]`),
} {
if _, err := parseRecord(block(json), l); err == nil {
t.Errorf("%s: parseRecord accepted it", name)
}
}
}
+885
View File
@@ -0,0 +1,885 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"cmp"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"maps"
"os"
"regexp"
"runtime/debug"
"slices"
"strconv"
"strings"
"time"
)
// A report is one classification of a line: where every original merged into
// main since the line branched stands, and what it was made of. The batch
// pull request's body carries the latest one beside the record, and an issue
// comment the tool never edits keeps each one, so a later run that replaces
// the body loses none of them. Its ID hashes the classification, not the pull
// request it lands on, which a new batch does not have yet when the report is
// made.
const (
reportFence = "```backport-batch-report"
reportMarker = "<!-- backport-batch:report "
// reportFormat is the format a run writes. Format 2 adds parked
// originals and partial units; format 1, which has neither, is read too.
reportFormat = 2
// reportLimit keeps a report comment well inside GitHub's 65536.
reportLimit = 60000
)
type reportDoc struct {
Format int `json:"format"`
ID string `json:"id"`
Line string `json:"line"`
Built builtFrom `json:"built"`
Overrides reportOverrides `json:"overrides"`
Primary reportPrimary `json:"primary"`
Overlays reportOverlays `json:"overlays"`
Diag map[string][]string `json:"diag"`
Other []string `json:"other"`
}
type reportOverrides struct {
Include []int `json:"backport/include"`
Skip []int `json:"backport/skip"`
}
// reportPrimary puts every original in exactly one place.
type reportPrimary struct {
Picked []int `json:"picked"`
Missing []int `json:"missing"`
Deferred []deferredEntry `json:"deferred"`
Parked []parkedEntry `json:"parked"`
Skipped reportSkipped `json:"skipped"`
}
type reportSkipped struct {
OnLine []int `json:"on-line"`
BeforeStart []int `json:"before-start"`
Type []int `json:"type"`
Breaking []int `json:"breaking"`
Touches []int `json:"touches"`
Newer []int `json:"newer"`
SkipLabel []int `json:"skip-label"`
Dropped []int `json:"dropped"`
}
// reportOverlays mark originals that are also something else: stale ones
// are excluded, or applied only in part, but still on the batch, partial
// ones those whose unit there leaves part of them without effect, and each
// drop is the record's entry, with the merge commit it is bound to and its
// reason.
type reportOverlays struct {
Dropped []droppedEntry `json:"dropped"`
Stale []int `json:"stale"`
Changed []int `json:"changed"`
Partial []int `json:"partial,omitempty"`
}
func (s *reportSkipped) list(rule int) *[]int {
switch rule {
case ruleOnLine:
return &s.OnLine
case ruleBeforeStart:
return &s.BeforeStart
case ruleType:
return &s.Type
case ruleBreaking:
return &s.Breaking
case ruleTouches:
return &s.Touches
case ruleNewer:
return &s.Newer
case ruleSkipLabel:
return &s.SkipLabel
case ruleDropped:
return &s.Dropped
}
return nil
}
type namedList struct {
name string
list *[]int
}
// lists names each skipped list as the report does.
func (s *reportSkipped) lists() []namedList {
return []namedList{
{"on-line", &s.OnLine}, {"before-start", &s.BeforeStart}, {"type", &s.Type}, {"breaking", &s.Breaking},
{"touches", &s.Touches}, {"newer", &s.Newer}, {"skip-label", &s.SkipLabel}, {"dropped", &s.Dropped},
}
}
func numbers(vs []*verdict) []int {
out := []int{}
for _, v := range vs {
out = append(out, v.o.number)
}
return out
}
// deferredEntries is what the record and the report say about the conflicts,
// the deferrals and the waits of a run.
func deferredEntries(conflicts, held, waiting, needs []pick) []deferredEntry {
out := []deferredEntry{}
for _, p := range conflicts {
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
out = append(out, deferredEntry{Original: p.v.o.number, Commit: p.failed, Files: files, More: len(p.files) - len(files) + p.more})
}
for _, p := range held {
out = append(out, deferredEntry{Original: p.v.o.number, Files: []string{}, Reason: capped(oneLine(clean(p.reason, false)), titleLimit)})
}
for _, p := range waiting {
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
out = append(out, deferredEntry{Original: p.v.o.number, Files: files, More: len(p.files) - len(files) + p.more, Workflows: true})
}
for _, p := range needs {
out = append(out, deferredEntry{Original: p.v.o.number, Files: []string{}, Needs: slices.Sorted(slices.Values(p.needs))})
}
return out
}
// parkedEntries is what the record and the report say about what a run
// parked; whether notify named it is the record's alone.
func parkedEntries(vs []*verdict) []parkedEntry {
out := []parkedEntry{}
for _, v := range vs {
p := *v.park
p.Announced = false
out = append(out, p)
}
return out
}
// buildReport makes the report of a run whose built is set. Every original
// main merged since the line branched has to land in exactly one primary
// category; one that does not is an error rather than a report that
// silently drops it.
func buildReport(b *batch, r *report) (reportDoc, error) {
doc := reportDoc{Format: reportFormat, Line: b.line.branch(), Built: r.built, Diag: map[string][]string{}, Other: append([]string{}, b.other...)}
doc.Overrides = reportOverrides{Include: []int{}, Skip: []int{}}
for _, v := range r.overrides {
switch v.label {
case labelInclude:
doc.Overrides.Include = append(doc.Overrides.Include, v.o.number)
case labelSkip:
doc.Overrides.Skip = append(doc.Overrides.Skip, v.o.number)
}
}
doc.Primary = reportPrimary{Picked: numbers(r.onBranch), Missing: numbers(r.missing), Deferred: deferredEntries(r.deferred, r.held, r.waiting, r.needs), Parked: parkedEntries(r.parked)}
for _, l := range doc.Primary.Skipped.lists() {
*l.list = []int{}
}
for rule, vs := range r.skipped {
list := doc.Primary.Skipped.list(rule)
if list == nil {
return doc, fmt.Errorf("the report has no place for what rule %d skipped", rule)
}
*list = append(*list, numbers(vs)...)
}
doc.Overlays = reportOverlays{Dropped: []droppedEntry{}, Stale: numbers(r.stale), Changed: numbers(r.changed())}
if len(r.partial) > 0 {
doc.Overlays.Partial = numbers(r.partial)
}
for _, v := range b.verdicts {
if len(v.diag) > 0 {
doc.Diag[strconv.Itoa(v.o.number)] = slices.Clone(v.diag)
}
}
doc.Overlays.Dropped = append(doc.Overlays.Dropped, r.dropped...)
doc.canonical()
where, err := doc.categories()
if err != nil {
return doc, err
}
for _, v := range b.verdicts {
if where[v.o.number] == "" {
return doc, fmt.Errorf("the report of %s puts #%d nowhere", b.line.branch(), v.o.number)
}
delete(where, v.o.number)
}
for n := range where {
return doc, fmt.Errorf("the report of %s names #%d, which main did not merge after the line branched", b.line.branch(), n)
}
doc.ID = reportID(doc)
return doc, nil
}
// canonical sorts every list, so equal classifications are equal bytes.
func (d *reportDoc) canonical() {
lists := []*[]int{&d.Primary.Picked, &d.Primary.Missing, &d.Overrides.Include, &d.Overrides.Skip, &d.Overlays.Stale, &d.Overlays.Changed, &d.Overlays.Partial}
for _, l := range d.Primary.Skipped.lists() {
lists = append(lists, l.list)
}
for _, l := range lists {
slices.Sort(*l)
}
slices.SortFunc(d.Primary.Deferred, func(a, b deferredEntry) int { return cmp.Compare(a.Original, b.Original) })
slices.SortFunc(d.Primary.Parked, func(a, b parkedEntry) int { return cmp.Compare(a.Original, b.Original) })
slices.SortFunc(d.Overlays.Dropped, func(a, b droppedEntry) int { return cmp.Compare(a.Original, b.Original) })
slices.Sort(d.Other)
}
// categories maps each original the report names to its primary category,
// refusing one named twice.
func (d *reportDoc) categories() (map[int]string, error) {
where := map[int]string{}
put := func(n int, category string) error {
if n <= 0 {
return fmt.Errorf("the report puts original %d in %s", n, category)
}
if prev := where[n]; prev != "" {
return fmt.Errorf("the report puts #%d in both %s and %s", n, prev, category)
}
where[n] = category
return nil
}
for _, n := range d.Primary.Picked {
if err := put(n, "picked"); err != nil {
return nil, err
}
}
for _, n := range d.Primary.Missing {
if err := put(n, "missing"); err != nil {
return nil, err
}
}
for _, e := range d.Primary.Deferred {
if err := put(e.Original, "deferred"); err != nil {
return nil, err
}
}
for _, e := range d.Primary.Parked {
if err := put(e.Original, "parked"); err != nil {
return nil, err
}
}
for _, l := range d.Primary.Skipped.lists() {
for _, n := range *l.list {
if err := put(n, "skipped."+l.name); err != nil {
return nil, err
}
}
}
return where, nil
}
// reportID hashes everything a report says but when and in which job it was
// made, so a run that finds what the last one found makes the same report.
func reportID(doc reportDoc) string {
doc.ID, doc.Built.At, doc.Built.Run = "", "", ""
data, _ := json.Marshal(doc)
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}
// storedReport is a report as it was written, whose bytes a replay posts
// unchanged.
type storedReport struct {
doc reportDoc
raw string
}
func reportJSON(doc reportDoc) string {
data, _ := json.Marshal(doc)
return string(data)
}
func reportBlock(raw string) string { return reportFence + "\n" + raw + "\n```" }
// parseReport reads one report line of a batch pull request against l. An
// ID that matches the content shows only that nobody changed it since it
// was hashed, so everything it says is also held to what a run writes.
func parseReport(raw string, l line) (reportDoc, error) {
var doc reportDoc
if err := decodeStrict(raw, &doc); err != nil {
return doc, fmt.Errorf("reading the report: %w", err)
}
if doc.Format != 1 && doc.Format != reportFormat {
return doc, fmt.Errorf("the report is format %d, which this backport-batch does not read", doc.Format)
}
if !reportIDRE.MatchString(doc.ID) || reportID(doc) != doc.ID {
return doc, fmt.Errorf("report %.12s does not match what it says", terminalSafe(doc.ID))
}
if err := doc.validate(l); err != nil {
return doc, fmt.Errorf("report %.12s %w", doc.ID, err)
}
if err := doc.bindDrops(); err != nil {
return doc, fmt.Errorf("report %.12s %w", doc.ID, err)
}
return doc, nil
}
// validate holds a report to what a run writes. Its errors read after
// "report <id>".
func (d *reportDoc) validate(l line) error {
if d.Line != l.branch() {
return fmt.Errorf("is for %q, not %s", d.Line, l.branch())
}
if err := d.Built.validate(); err != nil {
return err
}
for _, sha := range d.Other {
if checkSHA(sha) != nil {
return fmt.Errorf("counts %q as a commit on main", sha)
}
}
if _, err := d.categories(); err != nil {
return err
}
// The deferred and dropped entries are the record's, held to the
// record's rules.
kept := lineRecord{Version: 1, Line: d.Line, Deferred: d.Primary.Deferred, Dropped: d.Overlays.Dropped}
if err := kept.validate(l); err != nil {
return err
}
for n, lines := range d.Diag {
if k, err := strconv.Atoi(n); err != nil || k <= 0 || strconv.Itoa(k) != n {
return fmt.Errorf("has diagnostics for %q, which is no original", n)
}
for _, ln := range lines {
if ln != oneLineText(ln) {
return fmt.Errorf("has a diagnostic for #%s that is not one clean line: %q", n, ln)
}
}
}
for _, p := range d.Primary.Parked {
if p.Original <= 0 {
return fmt.Errorf("names original %d", p.Original)
}
if err := p.validate(); err != nil {
return err
}
// Whether notify named it is the record's to say, not the
// classification's.
if p.Announced {
return fmt.Errorf("has #%d announced", p.Original)
}
}
for _, list := range [][]int{d.Overrides.Include, d.Overrides.Skip, d.Overlays.Stale, d.Overlays.Changed, d.Overlays.Partial} {
for _, n := range list {
if n <= 0 {
return fmt.Errorf("names original %d", n)
}
}
}
return d.links()
}
// links holds the fields of a report that speak of one another to agree as
// a run writes them, so no field says something another contradicts. Its
// errors read after "report <id>".
func (d *reportDoc) links() error {
where, err := d.categories()
if err != nil {
return err
}
// Format 1 was written before anything was parked.
if d.Format == 1 && len(d.Primary.Parked)+len(d.Overlays.Partial) > 0 {
return errors.New("parks originals, which format 1 does not")
}
// What a dependency park needs never reaches the line: it is excluded
// by a rule, a label, a drop or a park of its own, or a commit on main
// that merged no pull request.
other := map[string]bool{}
for _, sha := range d.Other {
other[sha] = true
}
for _, p := range d.Primary.Parked {
for _, n := range p.Needs {
switch c := where[n]; c {
case "parked", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.skip-label", "skipped.dropped":
default:
return fmt.Errorf("parks #%d for #%d, which it has %s", p.Original, n, or(c, "nowhere"))
}
}
if other[p.Merge] || slices.Contains(p.Commits, p.Merge) {
return fmt.Errorf("parks #%d by a merge it counts as no pull request's, or for its own merge", p.Original)
}
for _, c := range p.Commits {
if !other[c] {
return fmt.Errorf("parks #%d for %.12s, which it does not count among main's commits that merged no pull request", p.Original, c)
}
}
}
// A partial unit is still on the batch.
stale := map[int]bool{}
for _, n := range d.Overlays.Stale {
stale[n] = true
}
for _, n := range d.Overlays.Partial {
if !stale[n] {
return fmt.Errorf("marks #%d partial on the batch, but not still on it", n)
}
}
drops := map[int]bool{}
for _, e := range d.Overlays.Dropped {
drops[e.Original] = true
}
for _, n := range d.Primary.Skipped.Dropped {
if !drops[n] {
return fmt.Errorf("has #%d dropped, but keeps no drop of it", n)
}
}
// Stale marks something a run took or excluded, and changed a pick.
for _, n := range d.Overlays.Stale {
if c := where[n]; c != "picked" && !strings.HasPrefix(c, "skipped.") {
return fmt.Errorf("marks #%d stale, which it has %s", n, or(c, "nowhere"))
}
}
for _, n := range d.Overlays.Changed {
if c := where[n]; c != "picked" {
return fmt.Errorf("marks #%d changed, which it has %s", n, or(c, "nowhere"))
}
}
// backport/skip decides before any rule, and backport/include lifts
// rules 1 to 4.
skip := map[int]bool{}
for _, n := range d.Overrides.Skip {
skip[n] = true
if where[n] != "skipped.skip-label" {
return fmt.Errorf("has #%d labelled %s, which it has %s", n, labelSkip, or(where[n], "nowhere"))
}
}
for _, n := range d.Primary.Skipped.SkipLabel {
if !skip[n] {
return fmt.Errorf("has #%d skipped for a %s label it does not list", n, labelSkip)
}
}
for _, n := range d.Overrides.Include {
switch c := where[n]; c {
case "", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.skip-label":
return fmt.Errorf("has #%d labelled %s, which it has %s", n, labelInclude, or(c, "nowhere"))
}
}
for n := range d.Diag {
if k, err := strconv.Atoi(n); err != nil || where[k] == "" {
return fmt.Errorf("has diagnostics for %q, which it puts nowhere", n)
}
}
for _, e := range d.Primary.Deferred {
if e.Workflows && !d.Built.Options.NoWorkflowFiles {
return fmt.Errorf("has #%d waiting for its workflow files, but was made bringing them over", e.Original)
}
// What a wait waits on is still to come: not on the line, deferred,
// or held by rule 6.
for _, n := range e.Needs {
switch c := where[n]; c {
case "missing", "deferred", "skipped.newer":
default:
return fmt.Errorf("has #%d wait on #%d, which it has %s", e.Original, n, or(c, "nowhere"))
}
}
if d.Format == 1 && len(e.Needs) > 0 {
return fmt.Errorf("has #%d wait on other originals, which format 1 does not", e.Original)
}
}
needs := map[int][]int{}
for _, e := range d.Primary.Deferred {
needs[e.Original] = e.Needs
}
for _, p := range d.Primary.Parked {
needs[p.Original] = p.Needs
}
if err := waitCycle(needs); err != nil {
return err
}
if d.Built.Options.Mode == modeClose && len(d.Primary.Picked)+len(d.Overlays.Stale) > 0 {
return errors.New("closes the batch, but still has originals on it")
}
return nil
}
// bodyReport is the report a batch body against l carries, or nil.
func bodyReport(body string, l line) (*storedReport, error) {
blocks, err := fencedLines(body, reportFence)
switch {
case err != nil:
return nil, err
case len(blocks) == 0:
return nil, nil
case len(blocks) > 1:
return nil, fmt.Errorf("the body holds %d %s blocks", len(blocks), reportFence)
}
doc, err := parseReport(blocks[0], l)
if err != nil {
return nil, err
}
return &storedReport{doc: doc, raw: blocks[0]}, nil
}
// reportComment is the comment that keeps a report: a marker naming it, and
// the report folded away in the same block the body carries. Nothing in it
// mentions anyone: numbers are bare, and text sits inside the fence.
//
// Only checked values reach the summary GitHub renders: an ID of hex digits,
// a release line and the date of a time that parses.
func reportComment(s storedReport) (string, error) {
if !reportIDRE.MatchString(s.doc.ID) {
return "", fmt.Errorf("report %q has no ID", s.doc.ID)
}
if _, err := parseLine(s.doc.Line); err != nil {
return "", fmt.Errorf("report %.12s: %w", s.doc.ID, err)
}
at, err := time.Parse(time.RFC3339, s.doc.Built.At)
if err != nil {
return "", fmt.Errorf("report %.12s was made at %q", s.doc.ID, s.doc.Built.At)
}
date := at.UTC().Format(time.DateOnly)
c := reportMarker + s.doc.ID + " -->\n" +
"<details><summary>Backport batch report for " + s.doc.Line + ", " + date + " (" + s.doc.ID[:12] + ")</summary>\n\n" +
reportBlock(s.raw) + "\n</details>\n"
if len(c) > reportLimit {
return "", fmt.Errorf("the report comment is %d bytes, over the %d it may have", len(c), reportLimit)
}
return c, nil
}
var reportMarkerRE = regexp.MustCompile(`^` + regexp.QuoteMeta(reportMarker) + `([0-9a-f]{64}) -->\n`)
// findReports reads the report comments on batch pull request n, against l,
// by authors the batch trusts, as it trusts a record's author, by ID. One
// whose content does not match the ID its marker gives, or is not what a run
// writes, is returned apart, with why; a look-alike by anyone else is not
// read, whatever it says.
func findReports(gh github, n int, l line) (map[string]storedReport, map[string]string, error) {
comments, err := gh.comments(n)
if err != nil {
return nil, nil, err
}
found, bad := map[string]storedReport{}, map[string]string{}
for _, c := range comments {
if !strings.HasPrefix(c.Body, reportMarker) {
continue
}
why, err := trustedAuthor(gh, c.User.Login, c.User.Type)
if err != nil {
return nil, nil, err
}
if why != "" {
continue
}
m := reportMarkerRE.FindStringSubmatch(c.Body)
if m == nil {
bad[strconv.FormatInt(c.ID, 10)] = fmt.Sprintf("comment %d starts like a report but names none", c.ID)
continue
}
blocks, err := fencedLines(c.Body, reportFence)
if err == nil && len(blocks) != 1 {
err = fmt.Errorf("it holds %d reports", len(blocks))
}
var doc reportDoc
if err == nil {
doc, err = parseReport(blocks[0], l)
}
if err == nil && doc.ID != m[1] {
err = fmt.Errorf("its marker names %.12s", m[1])
}
if err != nil {
bad[m[1]] = fmt.Sprintf("comment %d: %v", c.ID, err)
continue
}
found[m[1]] = storedReport{doc: doc, raw: blocks[0]}
}
return found, bad, nil
}
// toolVersion is the commit this binary was built from, with +dirty when its
// tree had changes, or "" when the build did not record it, as go run does
// not.
func toolVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
rev, dirty := "", false
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
rev = s.Value
case "vcs.modified":
dirty = s.Value == "true"
}
}
if checkSHA(rev) != nil {
return ""
}
if dirty {
rev += "+dirty"
}
return rev
}
// runURL is the Actions run the command runs in, from the variables GitHub
// sets for it, or "" outside one.
func runURL(lookup func(string) (string, bool)) string {
get := func(key string) string { v, _ := lookup(key); return v }
url := get("GITHUB_SERVER_URL") + "/" + get("GITHUB_REPOSITORY") + "/actions/runs/" + get("GITHUB_RUN_ID")
if attempt := get("GITHUB_RUN_ATTEMPT"); attempt != "" && attempt != "1" {
url += "/attempts/" + attempt
}
if !runURLRE.MatchString(url) {
return ""
}
return url
}
// report prints a report a batch pull request keeps, writing nothing, so it
// works on merged and closed batches too. It reads GitHub, and for a report
// that keeps drops also this clone's history at the heads the report was
// made from, which it never fetches.
func (a *app) report(args []string) int {
fs := a.flags("report")
number := fs.Int("pr", 0, "the batch pull request")
id := fs.String("id", "", "the report, by its ID (default: the one the body carries)")
asJSON := fs.Bool("json", false, "print the report's JSON")
if err := parse(fs, args); err != nil {
return 2
}
if *number <= 0 {
return a.fail(fmt.Errorf("--pr %d is not a pull request number", *number))
}
if *id != "" && !reportIDRE.MatchString(*id) {
return a.fail(fmt.Errorf("--id %q is not a report ID", *id))
}
pr, err := a.gh.pull(*number)
if err != nil {
return a.fail(err)
}
l, err := parseLine(pr.Base.Ref)
if m := batchBranchRE.FindStringSubmatch(pr.Head.Ref); err != nil || m == nil || m[1] != l.branch() || pr.Head.Repo == nil || pr.Head.Repo.FullName != pr.Base.Repo.FullName {
return a.fail(fmt.Errorf("#%d is not a batch pull request of this repository", pr.Number))
}
why, err := trusted(a.gh, pr)
if err != nil {
return a.fail(err)
}
if why != "" {
return a.fail(fmt.Errorf("batch pull request #%d was %s, so what it says is not the batch's", pr.Number, why))
}
inBody, err := bodyReport(pr.Body, l)
if err != nil {
return a.fail(fmt.Errorf("batch pull request #%d: %w", pr.Number, err))
}
if *id == "" {
if inBody == nil {
return a.fail(fmt.Errorf("batch pull request #%d carries no report in its body; name one with --id", pr.Number))
}
*id = inBody.doc.ID
}
posted, bad, err := findReports(a.gh, pr.Number, l)
if err != nil {
return a.fail(err)
}
if why := bad[*id]; why != "" {
return a.fail(fmt.Errorf("batch pull request #%d: the report %.12s is not what its comment says: %s", pr.Number, *id, why))
}
s, inComment := posted[*id]
switch {
case inComment:
case inBody != nil && inBody.doc.ID == *id:
s = *inBody
default:
return a.fail(fmt.Errorf("batch pull request #%d keeps no report %.12s", pr.Number, *id))
}
if *asJSON {
_, _ = fmt.Fprintln(a.stdout, s.raw)
return 0
}
a.printReport(&pr, s, inBody, inComment)
return 0
}
// printReport prints a report. It was checked when it was read, and every
// field still goes through terminalSafe, so nothing it carries spans lines
// or drives a terminal.
func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport, inComment bool) {
d, b := s.doc, s.doc.Built
safe := terminalSafe
_, _ = fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
if b.Options.NoWorkflowFiles {
_, _ = fmt.Fprint(a.stdout, " with --no-workflow-files")
}
_, _ = fmt.Fprintln(a.stdout)
_, _ = fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
current := "none"
if inBody != nil {
current = safe(inBody.doc.ID)
}
switch {
case inBody != nil && inBody.doc.ID == d.ID:
_, _ = fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
case pr.Head.SHA == b.Batch:
_, _ = fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
default:
_, _ = fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
}
if !inComment {
if prState(pr) == "open" {
_, _ = fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
} else {
_, _ = fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
}
}
type line struct {
n int
text string
}
var lines []line
for _, n := range d.Primary.Picked {
lines = append(lines, line{n, "picked"})
}
for _, n := range d.Primary.Missing {
lines = append(lines, line{n, "missing"})
}
for _, e := range d.Primary.Deferred {
text := "deferred: "
switch {
case e.Workflows:
text += "waits for a maintainer, changes " + strings.Join(e.Files, ", ")
case len(e.Needs) > 0:
var needs []string
for _, n := range e.Needs {
needs = append(needs, "#"+strconv.Itoa(n))
}
text += "waits on " + strings.Join(needs, ", ")
case e.Reason != "":
text += e.Reason
default:
text += fmt.Sprintf("conflict at %.12s in %s", e.Commit, strings.Join(e.Files, ", "))
}
if e.More > 0 {
text += fmt.Sprintf(" and %d more", e.More)
}
lines = append(lines, line{e.Original, text})
}
for _, e := range d.Primary.Parked {
text := "parked: "
switch e.Kind {
case parkPartial:
text += "only part of it applies, it would leave " + strings.Join(e.Files, ", ") + " without effect"
default:
var needs []string
for _, n := range e.Needs {
needs = append(needs, "#"+strconv.Itoa(n))
}
for _, c := range e.Commits {
needs = append(needs, c[:12])
}
text += "builds on " + strings.Join(needs, ", ") + ", which never reaches the line"
}
lines = append(lines, line{e.Original, text})
}
for _, l := range d.Primary.Skipped.lists() {
for _, n := range *l.list {
lines = append(lines, line{n, "skipped." + l.name})
}
}
slices.SortFunc(lines, func(x, y line) int { return cmp.Compare(x.n, y.n) })
for _, l := range lines {
_, _ = fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
}
overlay := func(name string, list []int) {
if len(list) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
}
}
overlay("stale, still on the batch", d.Overlays.Stale)
overlay("only part of it applies, still on the batch", d.Overlays.Partial)
for _, e := range d.Overlays.Dropped {
_, _ = fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
}
overlay("changed in the pick", d.Overlays.Changed)
for _, n := range slices.Sorted(maps.Keys(d.Diag)) {
for _, ln := range d.Diag[n] {
_, _ = fmt.Fprintf(a.stdout, "#%s: %s\n", terminalSafe(n), terminalSafe(ln))
}
}
if len(d.Other) > 0 {
other := make([]string, len(d.Other))
for i, sha := range d.Other {
other[i] = safe(sha)
}
_, _ = fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
}
}
func or(s, otherwise string) string {
if s == "" {
return otherwise
}
return s
}
// postReport writes a report comment on pull request n, after GitHub says
// it is still open: nothing is written to a merged or closed one.
func (a *app) postReport(n int, s storedReport) error {
if err := a.stillOpen(n); err != nil {
return err
}
c, err := reportComment(s)
if err != nil {
return err
}
name, err := writeTemp("backport-batch-report-*.md", c)
if err != nil {
return err
}
defer func() { _ = os.Remove(name) }()
return a.gh.createComment(n, name)
}
// stillOpen reads pull request n again and refuses to go on unless it is
// open.
func (a *app) stillOpen(n int) error {
pr, err := a.gh.pull(n)
if err != nil {
return err
}
if st := prState(&pr); st != "open" {
return fmt.Errorf("batch pull request #%d is %s now; nothing is written to it", n, st)
}
return nil
}
func writeTemp(pattern, content string) (string, error) {
f, err := os.CreateTemp("", pattern)
if err != nil {
return "", err
}
if _, err := f.WriteString(content); err != nil {
_ = f.Close()
_ = os.Remove(f.Name())
return "", err
}
if err := f.Close(); err != nil {
_ = os.Remove(f.Name())
return "", err
}
return f.Name(), nil
}
+780
View File
@@ -0,0 +1,780 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"unicode"
)
// reportsPosted lists the report IDs the comments carry, in the order they
// were posted, whoever wrote them.
func reportsPosted(gh *fakeGH) []string {
var ids []string
for _, c := range gh.issueComments {
if m := reportMarkerRE.FindStringSubmatch(c.Body); m != nil {
ids = append(ids, m[1])
}
}
return ids
}
// bodyReportOf is the report a body on release-1.1, or on the line given,
// carries, failing the test without one.
func bodyReportOf(t *testing.T, body string, on ...line) storedReport {
t.Helper()
l := notifyLine
if len(on) > 0 {
l = on[0]
}
s, err := bodyReport(body, l)
if err != nil || s == nil {
t.Fatalf("the body carries no readable report: %v\n%s", err, body)
}
return *s
}
// refusePushes makes the remote refuse every push, as a lost connection or
// a protected branch would, until it is called with false.
func (f *fixture) refusePushes(on bool) {
f.t.Helper()
hook := filepath.Join(f.remote, "hooks", "pre-receive")
if !on {
if err := os.Remove(hook); err != nil {
f.t.Fatal(err)
}
return
}
if err := os.WriteFile(hook, []byte("#!/bin/sh\necho refused >&2\nexit 1\n"), 0o755); err != nil {
f.t.Fatal(err)
}
}
func mustRun(t *testing.T, f *fixture, gh *fakeGH, args ...string) string {
t.Helper()
code, stdout, stderr := f.exec(gh, args...)
if code != 0 {
t.Fatalf("%q = %d\n%s", args, code, stderr)
}
f.git("switch", "-q", "main")
return stdout
}
// A report comment keeps each classification: one the next run replaced in
// the body is still read back by its ID, also once the batch merged, which
// nothing writes to.
func TestReportSurvivesContinuation(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n"})
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
r1 := bodyReportOf(t, gh.prs[0].Body).doc.ID
f.onLine("release-1.1", "fix(app): five, by hand\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "5\n"})
f.now = f.now.AddDate(0, 0, 1)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
r2 := bodyReportOf(t, gh.find(90).Body).doc.ID
if r1 == r2 || !slices.Equal(reportsPosted(gh), []string{r1, r2}) {
t.Fatalf("reports %q; want R1 %.12s, then R2 %.12s", reportsPosted(gh), r1, r2)
}
read := func(id, want string) {
t.Helper()
code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", id)
if code != 0 || !strings.Contains(stdout, want) {
t.Errorf("report --id %.12s = %d, want %q:\n%s%s", id, code, want, stdout, stderr)
}
}
for _, merged := range []bool{false, true} {
if merged {
mergeBatch(f, gh)
}
read(r1, "#5 deferred: conflict at ")
read(r2, "#5 skipped.on-line")
read(r2, "#6 picked")
}
}
// Two runs that find the same post one report, and the body carries the one
// kept, whatever time and job the second ran in. A look-alike someone else
// wrote is no report, and a kept report edited since stops the reader.
func TestReportDedupAndTrust(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
t.Setenv("GITHUB_SERVER_URL", "https://github.com")
t.Setenv("GITHUB_REPOSITORY", "cozystack/cozystack")
t.Setenv("GITHUB_RUN_ID", "100")
t.Setenv("GITHUB_RUN_ATTEMPT", "1")
// The report the next run makes, read off a run that publishes nothing,
// planted by someone the batch does not trust.
preview := bodyReportOf(t, mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue"))
planted, err := reportComment(preview)
if err != nil {
t.Fatal(err)
}
look := issueComment{ID: 7, Body: planted}
look.User.Login, look.User.Type = "someone", "User"
gh.issueComments = append(gh.issueComments, look)
f.now = f.now.AddDate(0, 0, 1)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
kept := bodyReportOf(t, gh.find(90).Body)
if kept.doc.ID != preview.doc.ID {
t.Fatalf("the run made %.12s, the preview %.12s", kept.doc.ID, preview.doc.ID)
}
f.now = f.now.AddDate(0, 0, 1)
t.Setenv("GITHUB_RUN_ID", "101")
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
posted := reportsPosted(gh)
if n := len(slices.DeleteFunc(slices.Clone(posted), func(id string) bool { return id != kept.doc.ID })); n != 2 || len(posted) != 3 {
t.Errorf("reports %q; want the first, the look-alike and the run's own once", posted)
}
body := gh.find(90).Body
again := bodyReportOf(t, body)
rec, err := parseRecord(body, notifyLine)
if err != nil || again.raw != kept.raw || rec.Report != again.doc.ID || !strings.Contains(again.raw, `"run":"https://github.com/cozystack/cozystack/actions/runs/100"`) {
t.Errorf("the second run's body carries %s with record report %v (%v); want the kept bytes of run 100", again.raw, rec, err)
}
// The reader takes the run's own comment, not the look-alike.
if code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", kept.doc.ID); code != 0 || !strings.Contains(stdout, "#6 picked") {
t.Errorf("report = %d:\n%s%s", code, stdout, stderr)
}
for i := range gh.issueComments {
c := &gh.issueComments[i]
if c.User.Login == "maintainer" && strings.Contains(c.Body, kept.doc.ID) {
c.Body = strings.Replace(c.Body, `"picked":[5,6]`, `"picked":[5]`, 1)
}
}
if code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", kept.doc.ID); code != 2 || !strings.Contains(stderr, "does not match") {
t.Errorf("report of an edited comment = %d, want 2:\n%s%s", code, stdout, stderr)
}
}
// The report holds no pull request number, so a new batch's report is whole
// before GitHub numbers the pull request, and the reader finds it on
// whatever number it got.
func TestFirstReportIDIsKnownBeforeTheNumber(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{nextPR: 4242}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
inBody := bodyReportOf(t, gh.bodies[0])
// Every list is one, empty or not, so equal reports are equal bytes:
// main here has no commit that merged no pull request.
if !strings.Contains(inBody.raw, `"other":[]`) {
t.Errorf("the report writes its empty list of other commits as something else:\n%s", inBody.raw)
}
if !slices.Equal(gh.commented, []int{4242}) || !slices.Equal(reportsPosted(gh), []string{inBody.doc.ID}) {
t.Fatalf("commented on %v with %q, want #4242 with %.12s", gh.commented, reportsPosted(gh), inBody.doc.ID)
}
found, bad, err := findReports(gh, 4242, notifyLine)
if err != nil || len(bad) != 0 || found[inBody.doc.ID].raw != inBody.raw {
t.Errorf("findReports = %v, %v, %v; want the body's report", found, bad, err)
}
if code, stdout, stderr := f.exec(gh, "report", "--pr", "4242"); code != 0 || !strings.Contains(stdout, "report "+inBody.doc.ID) || !strings.Contains(stdout, "#4242's body carries this report") {
t.Errorf("report --pr 4242 = %d:\n%s%s", code, stdout, stderr)
}
}
// Every original main merged since the line branched is in exactly one
// category of the report, a commit that merged no pull request is counted
// apart, and the report says so whatever the run had to do.
func TestReportPartition(t *testing.T) {
f := newFixture(t)
f.lines()
gh := &fakeGH{labels: map[string][]int{labelSkip: {9}}}
// onNewer puts an original on release-1.1, as the newer line's batch
// would, so rule 6 lets it onto release-1.0.
onNewer := func(mc string, files map[string]string) {
f.onLine("release-1.1", "fix: by the newer batch\n\n(cherry picked from commit "+mc+")", files)
}
pr := func(n int, title string, files map[string]string, newer bool) {
mc := f.pr(n, title, "", files)
if newer {
onNewer(mc, files)
}
}
t.Setenv("GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z")
f.pr(4, "fix(app): four, before the start", "", map[string]string{"app/four.txt": "4\n"})
if err := os.Unsetenv("GIT_COMMITTER_DATE"); err != nil {
t.Fatal(err)
}
f.setPolicy("release-1.0: 2026-06-01\n")
f.onLine("release-1.0", "fix(app): a line-only eleven", map[string]string{"app/eleven.txt": "line\n"})
pr(5, "fix(app): five", map[string]string{"app/five.txt": "5\n"}, true)
pr(6, "fix(app): six, not on the newer line", map[string]string{"app/six.txt": "6\n"}, false)
pr(7, "fix(app)!: seven", map[string]string{"app/seven.txt": "7\n"}, true)
pr(8, "fix(platform): eight", map[string]string{migrationsDir + "42": "#!/bin/sh\n"}, true)
pr(9, "fix(app): nine", map[string]string{"app/nine.txt": "9\n"}, true)
mc10 := f.pr(10, "fix(app): ten", "", map[string]string{"app/ten.txt": "10\n"})
onNewer(mc10, map[string]string{"app/ten.txt": "10\n"})
f.onLine("release-1.0", "fix(app): ten\n\n(cherry picked from commit "+mc10+")", map[string]string{"app/ten.txt": "10\n"})
pr(11, "fix(app): eleven", map[string]string{"app/eleven.txt": "11\n"}, true)
pr(12, "fix(ci): twelve", map[string]string{".github/workflows/twelve.yaml": "on: push\n"}, true)
pr(13, "fix(app): thirteen", map[string]string{"app/thirteen.txt": "13\n"}, true)
f.git("switch", "-q", "main")
f.write("app/direct.txt", "pushed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "chore: a direct push")
direct := f.git("rev-parse", "HEAD")
f.git("push", "-q", "origin", "main")
stdout := mustRun(t, f, gh, "run", "--line", "release-1.0", "--no-workflow-files", "--push")
const branch = "backport-batch/release-1.0-20261026"
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
pr(14, "fix(app): fourteen, after the batch", map[string]string{"app/fourteen.txt": "14\n"}, true)
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--defer", "13", "--reason", "fails the e2e", "--push")
// An active drop, of #5, which the batch carries, and one of a feat,
// which rule 2 keeps out first.
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--drop", "5,1", "--reason", "not for 1.0", "--push")
l, _ := parseLine("release-1.0")
s := bodyReportOf(t, stdout, l)
got, err := s.doc.categories()
if err != nil {
t.Fatal(err)
}
want := map[int]string{1: "skipped.type", 4: "skipped.before-start", 5: "skipped.dropped", 6: "skipped.newer", 7: "skipped.breaking",
8: "skipped.touches", 9: "skipped.skip-label", 10: "skipped.on-line", 11: "deferred", 12: "deferred", 13: "deferred",
14: "missing", 9000: "skipped.type"}
for n, category := range want {
if got[n] != category {
t.Errorf("#%d is %q, want %q", n, got[n], category)
}
delete(got, n)
}
if len(got) != 0 {
t.Errorf("the report also names %v", got)
}
if gh.find(90).State != "closed" || !strings.Contains(stdout, `"mode":"close"`) {
t.Errorf("dropping the batch's last pick left #90 %s; want it closed", gh.find(90).State)
}
if !slices.Equal(droppedNumbers(s.doc.Overlays.Dropped), []int{1, 5}) || len(s.doc.Primary.Picked) != 0 {
t.Errorf("the report drops %v and picks %v; want #1 and #5 dropped, nothing picked", s.doc.Overlays.Dropped, s.doc.Primary.Picked)
}
if !slices.Contains(s.doc.Other, direct) {
t.Errorf("the report does not count the direct push %.12s: %v", direct, s.doc.Other)
}
kinds := map[int]string{}
for _, e := range s.doc.Primary.Deferred {
kinds[e.Original] = fmt.Sprintf("commit=%v reason=%q workflows=%v", e.Commit != "", e.Reason, e.Workflows)
}
if kinds[11] != `commit=true reason="" workflows=false` || kinds[12] != `commit=false reason="" workflows=true` || kinds[13] != `commit=false reason="fails the e2e" workflows=false` {
t.Errorf("the deferred entries are %v", kinds)
}
}
// A report too big for a comment is refused before anything is written: no
// push, no comment, no edit, and no replay of the earlier report either.
func TestReportTooLargeIsRefusedBeforePush(t *testing.T) {
f, gh, head := bigWaits(t, 7)
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
if code != 2 || !strings.Contains(stderr, "the report comment is") {
t.Errorf("run --push = %d, want a refusal of the report:\n%s", code, stderr)
}
if f.remoteHead(batchBranch1) != head || len(gh.issueComments) != 0 || len(gh.edited) != 0 {
t.Errorf("the refused run moved the batch to %.12s, wrote %d comments and edited %q", f.remoteHead(batchBranch1), len(gh.issueComments), gh.edited)
}
}
// bigWaits opens a batch whose report comment failed, so its body keeps a
// report no comment does, and then merges n originals that each wait on 50
// workflow files with long names, which both the record and the report list
// in full.
func bigWaits(t *testing.T, n int) (*fixture, *fakeGH, string) {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{fail: map[string]error{"createComment": errString("HTTP 502")}}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 2 || len(gh.created) != 1 {
t.Fatalf("the first run = %d, want its comment to fail after the pull request opened\n%s", code, stderr)
}
f.git("switch", "-q", "main")
for i := range n {
files := map[string]string{}
for j := range filesKept {
files[fmt.Sprintf(".github/workflows/%s-%02d-%02d.yaml", strings.Repeat("w", 170), i, j)] = "on: push\n"
}
f.pr(10+i, fmt.Sprintf("fix(ci): wait %d", i), "", files)
}
f.now = f.now.AddDate(0, 0, 1)
return f, gh, f.remoteHead(batchBranch1)
}
// A failure between any two writes leaves what was written readable, and the
// next run, whatever changed meanwhile, replays a report the body kept but no
// comment did, byte for byte, and posts nothing twice. A batch closed in
// between is never written again.
func TestReportReplayAfterEachFailure(t *testing.T) {
setup := func(t *testing.T) (*fixture, *fakeGH) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
return f, &fakeGH{}
}
// change moves a label, main, the clock and the run's flags between
// the failure and the retry, so the retry's report is another one.
change := func(f *fixture, gh *fakeGH) []string {
gh.labels = map[string][]int{labelSkip: {6}}
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
f.now = f.now.AddDate(0, 0, 1)
return []string{"run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push"}
}
once := func(t *testing.T, gh *fakeGH, ids ...string) {
t.Helper()
posted := reportsPosted(gh)
for _, id := range ids {
if n := len(slices.DeleteFunc(slices.Clone(posted), func(p string) bool { return p != id })); n != 1 {
t.Errorf("report %.12s is posted %d times: %q", id, n, posted)
}
}
}
t.Run("a new batch whose pull request was not opened", func(t *testing.T) {
f, gh := setup(t)
gh.fail = map[string]error{"createPull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if len(gh.prs) != 0 || len(gh.issueComments) != 0 || f.remoteHead(batchBranch1) == "" {
t.Errorf("after the failure: %d pull requests, %d comments, branch %q; want the branch alone", len(gh.prs), len(gh.issueComments), f.remoteHead(batchBranch1))
}
mustRun(t, f, gh, change(f, gh)...)
if len(gh.prs) != 1 || len(reportsPosted(gh)) != 1 || bodyReportOf(t, gh.prs[0].Body).doc.ID != reportsPosted(gh)[0] {
t.Errorf("the retry opened %d pull requests with reports %q", len(gh.prs), reportsPosted(gh))
}
})
t.Run("a new batch whose report comment failed", func(t *testing.T) {
f, gh := setup(t)
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 || !strings.Contains(stderr, "the body keeps it") {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
first := bodyReportOf(t, gh.find(90).Body)
mustRun(t, f, gh, change(f, gh)...)
second := bodyReportOf(t, gh.find(90).Body)
want, err := reportComment(first)
if err != nil || len(gh.issueComments) != 2 || gh.issueComments[0].Body != want || reportsPosted(gh)[1] != second.doc.ID || first.doc.ID == second.doc.ID {
t.Errorf("comments %q; want the first report replayed as it was, then the retry's", reportsPosted(gh))
}
})
t.Run("a continuation whose push failed", func(t *testing.T) {
f, gh := setup(t)
openBatch(t, f, gh)
first := reportsPosted(gh)[0]
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
f.refusePushes(true)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
f.refusePushes(false)
attempted := reportsPosted(gh)[1]
if bodyReportOf(t, gh.find(90).Body).doc.ID != first {
t.Error("the body moved on although the push failed")
}
mustRun(t, f, gh, change(f, gh)...)
once(t, gh, first, attempted)
if len(reportsPosted(gh)) != 3 {
t.Errorf("reports %q; want the first, the attempted and the retry's", reportsPosted(gh))
}
})
t.Run("a continuation whose body edit failed", func(t *testing.T) {
f, gh := setup(t)
openBatch(t, f, gh)
first := reportsPosted(gh)[0]
before := f.remoteHead(batchBranch1)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh.fail = map[string]error{"editPull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
attempted := reportsPosted(gh)[1]
if f.remoteHead(batchBranch1) == before || bodyReportOf(t, gh.find(90).Body).doc.ID != first {
t.Error("the push or the body is not where the failure left them")
}
mustRun(t, f, gh, change(f, gh)...)
once(t, gh, first, attempted)
if last := reportsPosted(gh)[len(reportsPosted(gh))-1]; bodyReportOf(t, gh.find(90).Body).doc.ID != last || len(reportsPosted(gh)) != 3 {
t.Errorf("reports %q; want the body to carry the retry's, posted last", reportsPosted(gh))
}
})
t.Run("a batch closed while the run writes to it", func(t *testing.T) {
f, gh := setup(t)
openBatch(t, f, gh)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh.after = map[string]func(){"createComment": func() { gh.find(90).State = "closed" }}
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || len(gh.edited) != 0 {
t.Errorf("run = %d, edited %q; want 2 and no edit of the closed pull request:\n%s", code, gh.edited, stderr)
}
})
t.Run("a batch closed before its report was posted", func(t *testing.T) {
f, gh := setup(t)
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
kept := bodyReportOf(t, gh.find(90).Body)
gh.find(90).State = "closed"
gh.nextPR = 91
mustRun(t, f, gh, change(f, gh)...)
if slices.Contains(gh.commented, 90) || slices.Contains(reportsPosted(gh), kept.doc.ID) {
t.Errorf("commented on %v, posting %q; want nothing on the closed #90", gh.commented, reportsPosted(gh))
}
code, stdout, stderr := f.exec(gh, "report", "--pr", "90")
if code != 0 || !strings.Contains(stdout, "report "+kept.doc.ID) || !strings.Contains(stdout, "kept in the body only") {
t.Errorf("report --pr 90 = %d:\n%s%s", code, stdout, stderr)
}
})
// Every comment is posted only after GitHub says the batch is still
// open, the replay's included, so a close between the two stops the
// second.
t.Run("a batch closed between the replay and the new report", func(t *testing.T) {
f, gh := setup(t)
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
kept := bodyReportOf(t, gh.find(90).Body)
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh.after = map[string]func(){"createComment": func() { gh.find(90).State = "closed" }}
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || !slices.Equal(reportsPosted(gh), []string{kept.doc.ID}) || len(gh.edited) != 0 {
t.Errorf("run = %d, posted %q, edited %q; want 2 after the replay alone:\n%s", code, reportsPosted(gh), gh.edited, stderr)
}
})
}
// The reader says how a report stands against the pull request as GitHub
// shows it now, and claims publication only when the head or the body shows
// it: a failed push, a push whose answer was lost, a body that was not
// updated and a rebuild since all read as they are.
func TestReportPublicationStatus(t *testing.T) {
setup := func(t *testing.T) (*fixture, *fakeGH, string) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
openBatch(t, f, gh)
return f, gh, reportsPosted(gh)[0]
}
status := func(t *testing.T, f *fixture, gh *fakeGH, id, want string) {
t.Helper()
code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", id)
if code != 0 || !strings.Contains(stdout, want) {
t.Errorf("report --id %.12s = %d, want %q:\n%s%s", id, code, want, stdout, stderr)
}
}
t.Run("a push the remote refused, and one it took whose answer was lost", func(t *testing.T) {
f, gh, first := setup(t)
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
f.refusePushes(true)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.refusePushes(false)
attempted := reportsPosted(gh)[1]
status(t, f, gh, attempted, "publication unconfirmed: #90's head is ")
status(t, f, gh, attempted, "its body carries report "+first[:12])
// The same head reaching the remote is what a push GitHub took looks
// like when its answer never came back.
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
f.git("switch", "-q", "main")
status(t, f, gh, attempted, "the batch this report describes, but its body carries report "+first[:12])
})
t.Run("a push followed by a failed body edit", func(t *testing.T) {
f, gh, first := setup(t)
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh.fail = map[string]error{"editPull": errString("HTTP 502")}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
status(t, f, gh, reportsPosted(gh)[1], "the batch this report describes, but its body carries report "+first[:12])
status(t, f, gh, first, "#90's body carries this report")
})
t.Run("a rebuild since", func(t *testing.T) {
f, gh, first := setup(t)
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "x", "--push")
second := reportsPosted(gh)[1]
status(t, f, gh, first, "publication unconfirmed: #90's head is ")
status(t, f, gh, second, "#90's body carries this report")
})
}
// forged is a report on release-1.1 with what change sets, under an ID that
// matches it: only checking what it says stands in its way.
func forged(change func(d *reportDoc)) storedReport {
sha := strings.Repeat("a", 40)
d := reportDoc{Format: reportFormat, Line: "release-1.1", Diag: map[string][]string{}, Other: []string{sha},
Built: builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeNew}}}
d.Primary.Picked = []int{5}
change(&d)
d.ID = reportID(d)
return storedReport{doc: d, raw: reportJSON(d)}
}
// reportRun runs report with gh faked; it reads nothing but GitHub.
func reportRun(gh *fakeGH, args ...string) (int, string, string) {
var stdout, stderr strings.Builder
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 0, 0, 0, time.UTC) }}
code := a.main(append([]string{"report"}, args...))
return code, stdout.String(), stderr.String()
}
// A report is read only when everything it says is what a run writes, and
// it is for the line of the pull request it sits on: an ID that matches its
// content proves only that nobody changed it since it was hashed.
func TestReportRefusesWhatItCannotVouchFor(t *testing.T) {
for name, change := range map[string]func(d *reportDoc){
"a line that is no release line": func(d *reportDoc) { d.Line = "release-1.1\n\x1b[2Jpicked" },
"another line": func(d *reportDoc) { d.Line = "release-1.0" },
"a main that is no commit": func(d *reportDoc) { d.Built.Main = "main\n#5 picked" },
"a time that is none": func(d *reportDoc) { d.Built.At = "2026-10-26T<b>" },
"an unknown operation": func(d *reportDoc) { d.Built.Options.Mode = "new\x1b[2J" },
"a run that is no Actions run": func(d *reportDoc) { d.Built.Run = "javascript:alert(1)" },
"a tool that is no commit": func(d *reportDoc) { d.Built.Tool = "v1\n" },
"a commit on main that is none": func(d *reportDoc) { d.Other = []string{"HEAD\x1b]0;x\x07"} },
"a deferred commit that is none": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 6, Commit: "-x", Files: []string{"a"}}}
},
"an original named twice": func(d *reportDoc) { d.Primary.Missing = []int{5} },
"an original that is none": func(d *reportDoc) { d.Primary.Skipped.Type = []int{-1} },
"a diagnostic of two lines": func(d *reportDoc) { d.Diag = map[string][]string{"5": {"incomplete\n\x1b[2Jforged"}} },
"a diagnostic of no original": func(d *reportDoc) { d.Diag = map[string][]string{"five": {"incomplete"}} },
// Fields that speak of one another have to agree.
"a drop category with no drop": func(d *reportDoc) { d.Primary.Skipped.Dropped = []int{6} },
"a stale original it has missing": func(d *reportDoc) {
d.Primary.Missing, d.Overlays.Stale = []int{7}, []int{7}
},
"a stale original it puts nowhere": func(d *reportDoc) { d.Overlays.Stale = []int{8} },
"a changed original it defers": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 9, Files: []string{}, Reason: "fails the e2e"}}
d.Overlays.Changed = []int{9}
},
"a skip label on a picked original": func(d *reportDoc) { d.Overrides.Skip = []int{5} },
"a skip-label category with no label": func(d *reportDoc) { d.Primary.Skipped.SkipLabel = []int{10} },
"an include label on a rule 2 skip": func(d *reportDoc) {
d.Primary.Skipped.Type, d.Overrides.Include = []int{11}, []int{11}
},
"both labels on one original": func(d *reportDoc) {
d.Primary.Skipped.SkipLabel, d.Overrides.Include, d.Overrides.Skip = []int{12}, []int{12}, []int{12}
},
"a label on no original": func(d *reportDoc) { d.Overrides.Include = []int{13} },
"a diagnostic of an original it puts nowhere": func(d *reportDoc) { d.Diag = map[string][]string{"14": {"incomplete"}} },
"a workflow wait without the flag": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 15, Files: []string{".github/workflows/ci.yaml"}, Workflows: true}}
},
"a close that still picks": func(d *reportDoc) { d.Built.Options.Mode = modeClose },
"a park with no merge": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Kind: parkPartial, Files: []string{"a"}}}
},
"a parked original it picks": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 5, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
},
"a park of an unknown kind": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: "later", Files: []string{"a"}}}
},
"a partial park that needs an original": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Needs: []int{5}}}
},
"a partial park of unsorted files": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"b", "a"}}}
},
"a partial park of a file that drives a terminal": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a\x1b[2J"}}}
},
"a park announced in the report": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Announced: true}}
},
"a park for an original it picks": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{5}}}
},
"a park that needs itself": func(d *reportDoc) {
d.Primary.Skipped.Type = []int{17}
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}}}
},
"a park for a commit that merged a pull request": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("c", 40)}}}
},
"a partial unit not on the batch": func(d *reportDoc) { d.Overlays.Partial = []int{5} },
"a wait on an original it picks": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{5}}}
},
"a wait on an original it puts nowhere": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
},
"a park on its own merge as a commit": func(d *reportDoc) {
d.Other = append(d.Other, strings.Repeat("b", 40))
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("b", 40)}}}
},
"a park cycle": func(d *reportDoc) {
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{18}}, {Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16}}}
},
"a wait cycle": func(d *reportDoc) {
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{7}}, {Original: 7, Files: []string{}, Needs: []int{6}}}
},
"format 1 with a wait": func(d *reportDoc) {
d.Format = 1
d.Primary.Missing = []int{19}
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
},
"format 1 with a park": func(d *reportDoc) {
d.Format = 1
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
},
} {
s := forged(change)
c, _ := reportComment(storedReport{doc: forged(func(*reportDoc) {}).doc, raw: s.raw})
comment := issueComment{ID: 1, Body: strings.Replace(c, forged(func(*reportDoc) {}).doc.ID, s.doc.ID, 1)}
comment.User.Login, comment.User.Type = "maintainer", "User"
for where, gh := range map[string]*fakeGH{
"in the body": {prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(s.raw)+"\n")}},
"in a comment": {prs: []pullRequest{notifyPR("20261026", "text")}, issueComments: []issueComment{comment}},
} {
if code, stdout, stderr := reportRun(gh, "--pr", "90", "--id", s.doc.ID); code != 2 || stdout != "" {
t.Errorf("%s, %s: report = %d, want 2 and nothing printed:\n%s%s", name, where, code, stdout, stderr)
}
}
}
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(forged(func(*reportDoc) {}).raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
t.Errorf("report of a sound one = %d:\n%s%s", code, stdout, stderr)
}
// What a run wrote before anything was parked reads as it was written.
format1 := forged(func(d *reportDoc) { d.Format, d.Primary.Parked = 1, []parkedEntry{} })
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(format1.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
t.Errorf("report of a sound format 1 one = %d:\n%s%s", code, stdout, stderr)
}
parked := forged(func(d *reportDoc) {
d.Primary.Skipped.Type = []int{17}
d.Other = append(d.Other, strings.Repeat("c", 40))
d.Primary.Parked = []parkedEntry{
{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"chart/templates/policy.yaml"}},
{Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}, Commits: []string{strings.Repeat("c", 40)}},
}
})
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(parked.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#16 parked: only part of it applies") || !strings.Contains(stdout, "#18 parked: builds on #16, #17, cccccccccccc") {
t.Errorf("report of sound parks = %d:\n%s%s", code, stdout, stderr)
}
}
// What report prints stays one line per field and carries nothing a
// terminal acts on, whatever reached it.
func TestReportPrintsNothingThatDrivesATerminal(t *testing.T) {
const evil = "\n\x1b[2Jfake: #9 picked\u202e"
s := forged(func(d *reportDoc) {
d.Line, d.Built.At, d.Built.Options.Mode = "release-1.1"+evil, "2026-10-26"+evil, "new"+evil
d.Built.Main, d.Built.Tool, d.Built.Run, d.Built.Newer = "main"+evil, "tool"+evil, "run"+evil, "newer"+evil
d.Other = []string{"other" + evil}
d.Primary.Deferred = []deferredEntry{{Original: 6, Reason: "why" + evil}}
})
var stdout strings.Builder
a := &app{stdout: &stdout}
pr := notifyPR("20261026", "")
a.printReport(&pr, s, nil, true)
for _, ln := range strings.Split(stdout.String(), "\n") {
if strings.HasPrefix(ln, "fake") || strings.ContainsFunc(ln, func(r rune) bool { return r != '\t' && unicode.IsControl(r) || unicode.Is(unicode.Cf, r) }) {
t.Errorf("report printed %q", ln)
}
}
}
// The comment is made of what was checked: a line, a time and an ID that
// carry no markup into the summary GitHub renders.
func TestReportCommentIsBuiltFromCheckedFields(t *testing.T) {
for name, change := range map[string]func(d *reportDoc){
"markup in the line": func(d *reportDoc) { d.Line = "release-1.1</summary><img src=x onerror=alert(1)>" },
"markup in the time": func(d *reportDoc) { d.Built.At = "<b>2026-10-26</b>T06:00:00Z" },
"a mention": func(d *reportDoc) { d.Line = "release-1.1 @cozystack/maintainers" },
} {
if c, err := reportComment(forged(change)); err == nil {
t.Errorf("%s: reportComment made\n%s", name, c)
}
}
if _, err := reportComment(forged(func(*reportDoc) {})); err != nil {
t.Errorf("reportComment of a sound report: %v", err)
}
}
// Anyone can comment on a public pull request: the reader takes a report
// from a comment only when the batch trusts its author, whatever ID it
// carries.
func TestReportReaderTakesOnlyTrustedComments(t *testing.T) {
s := forged(func(*reportDoc) {})
c, err := reportComment(s)
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
login, kind string
read bool
}{
{"someone", "User", false},
{"renovate[bot]", "Bot", false},
{"github-actions[bot]", "Bot", false},
{"maintainer", "User", true},
{defaultBatchBot, "Bot", true},
} {
comment := issueComment{ID: 1, Body: c}
comment.User.Login, comment.User.Type = tc.login, tc.kind
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", "text")}, issueComments: []issueComment{comment}, bot: defaultBatchBot}
code, stdout, stderr := reportRun(gh, "--pr", "90", "--id", s.doc.ID)
if read := code == 0 && strings.Contains(stdout, "#5 picked"); read != tc.read || !tc.read && (code != 2 || !strings.Contains(stderr, "keeps no report")) {
t.Errorf("a report by %s: report = %d, read %v, want %v:\n%s%s", tc.login, code, read, tc.read, stdout, stderr)
}
}
}
// A report whose ID is not one says so without repeating it as written.
func TestReportErrorsRepeatNothingRaw(t *testing.T) {
s := forged(func(*reportDoc) {})
// Short enough that a cut to 12 characters keeps the forged line.
raw := strings.Replace(s.raw, `"id":"`+s.doc.ID+`"`, `"id":"\nforged"`, 1)
code, _, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(raw)+"\n")}}, "--pr", "90")
for _, ln := range strings.Split(stderr, "\n") {
if strings.HasPrefix(ln, "forged") {
t.Errorf("report = %d, printed the forged line %q", code, ln)
}
}
if code != 2 {
t.Errorf("report = %d, want 2:\n%s", code, stderr)
}
}
+344
View File
@@ -0,0 +1,344 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"maps"
"path/filepath"
"regexp"
"slices"
"strings"
"sigs.k8s.io/yaml"
)
// Rule numbers as docs/release/backports.md numbers them. Rule 5, a clean
// pick, is decided while the batch is assembled.
const (
ruleOnLine = 1
ruleType = 2
ruleBreaking = 3
ruleTouches = 4
ruleNewer = 6
// Not a rule of the policy: the backport/skip override.
ruleSkipLabel = 100
// Rule 1 for a line that started under the policy after it branched:
// merged before that start.
ruleBeforeStart = 101
// Not a rule of the policy: a maintainer dropped the original from the
// line with run --drop.
ruleDropped = 102
)
const (
// The numbered scripts run-migrations.sh walks; hack/check-migrations-target.sh
// reads the same directory.
migrationsDir = "packages/core/platform/images/migrations/migrations/"
// Where migrations.targetVersion is set; hack/check-migrations-target.sh
// reads it from here.
platformValues = "packages/core/platform/values.yaml"
// Only a file holding this text at either end of the change can hold a
// CRD document; crdDocs decides document by document.
crdKind = "CustomResourceDefinition"
)
// The title grammar and the breaking footer are pr-labeler.yaml's, so that a
// pull request labelled kind/bug and kind/breaking-change reads the same here.
var (
backportPrefixRE = regexp.MustCompile(`^(?:\[Backport [^\]]+\]\s+)+`)
conventionalRE = regexp.MustCompile(`^([a-z]+)(?:\(([^)]+)\))?(!)?:\s*\S`)
breakingFooterRE = regexp.MustCompile(`(?m)^BREAKING[ -]CHANGE:`)
htmlCommentRE = regexp.MustCompile(`(?s)<!--.*?-->`)
targetVersionRE = regexp.MustCompile(`(?m)^[+-][ \t]*targetVersion[ \t]*:`)
)
// title is a parsed Conventional Commits title.
type title struct {
typ string
scopes []string
bang bool
}
func parseTitle(s string) (title, bool) {
m := conventionalRE.FindStringSubmatch(backportPrefixRE.ReplaceAllString(s, ""))
if m == nil {
return title{}, false
}
t := title{typ: m[1], bang: m[3] == "!"}
for scope := range strings.SplitSeq(m[2], ",") {
if scope = strings.TrimSpace(scope); scope != "" {
t.scopes = append(t.scopes, scope)
}
}
return t, true
}
// typeRule is rule 2 together with the policy's "never in a patch release":
// dependency bumps, scope deps, stay out whatever their type.
func typeRule(s string) string {
t, ok := parseTitle(s)
switch {
case !ok:
return "the title is not a Conventional Commits title"
case !slices.Contains(takenTypes, t.typ):
return "type " + t.typ
case slices.ContainsFunc(t.scopes, func(s string) bool { return slices.Contains(excludedScopes, s) }):
return "an excluded scope"
}
return ""
}
// breakingRule is rule 3. The PR template quotes the footer inside an HTML
// comment, which a body merged with the template left in carries.
func breakingRule(s, body string) string {
if t, ok := parseTitle(s); ok && t.bang {
return "! in the title"
}
if breakingFooterRE.MatchString(htmlCommentRE.ReplaceAllString(body, "")) {
return "BREAKING CHANGE footer"
}
return ""
}
// fileChange is one path a merge changes against its first parent.
type fileChange struct {
status byte
path string
}
func changedFiles(from, to string) ([]fileChange, error) {
out, err := git("diff", "--no-ext-diff", "--no-textconv", "--name-status", "-z", "--no-renames", "--end-of-options", from, to)
if err != nil {
return nil, err
}
f := splitNUL(out)
if len(f)%2 != 0 {
return nil, fmt.Errorf("unreadable diff of %s..%s", from, to)
}
var changes []fileChange
for i := 0; i < len(f); i += 2 {
changes = append(changes, fileChange{status: f[i][0], path: f[i+1]})
}
return changes, nil
}
func paths(changes []fileChange) []string {
out := make([]string, 0, len(changes))
for _, c := range changes {
out = append(out, c.path)
}
return out
}
const (
touchesMigrations = "platform migrations"
touchesTargetVersion = "targetVersion"
touchesAPI = "a sizeable API change"
touchesCRD = "a CRD"
)
// touchesRule is rule 4: what of migrations, targetVersion, the API and CRDs
// the merge touches, or "" when it touches none of them.
func touchesRule(o *original, changes []fileChange) (string, error) {
hit := map[string]bool{}
for _, c := range changes {
p := c.path
switch {
case strings.HasPrefix(p, migrationsDir):
hit[touchesMigrations] = true
case p == platformValues:
diff, err := git("--literal-pathspecs", "diff", "-U0", "--no-ext-diff", "--no-textconv", "--end-of-options", o.merge+"^1", o.merge, "--", platformValues)
if err != nil {
return "", err
}
hit[touchesTargetVersion] = targetVersionRE.MatchString(diff)
}
}
var err error
if hit[touchesCRD], err = changesCRD(o.merge+"^1", o.merge, changes); err != nil {
return "", err
}
asks, err := readsAPI(o.merge+"^1", o.merge, changes)
if err != nil {
return "", err
}
apiDetail := ""
if asks {
if apiDetail, err = sizeableAPIChange(o); err != nil {
return "", err
}
hit[touchesAPI] = apiDetail != ""
}
var why []string
for _, r := range []string{touchesMigrations, touchesTargetVersion, touchesAPI, touchesCRD} {
switch {
case !hit[r]:
case r == touchesAPI:
why = append(why, r+" ("+apiDetail+")")
default:
why = append(why, r)
}
}
return strings.Join(why, ", "), nil
}
// changesCRD reports whether the change adds, removes or modifies a
// CustomResourceDefinition document. It looks document by document: an edit to
// the DaemonSet beside a CRD in one manifest is not a CRD change, and a CRD in
// a test fixture changes nothing a cluster runs.
func changesCRD(parent, merge string, changes []fileChange) (bool, error) {
var files []string
for _, c := range changes {
if manifest(c.path) && !testFixture(c.path) {
files = append(files, c.path)
}
}
candidates := map[string]bool{}
for _, rev := range []string{parent, merge} {
found, err := grepFiles(rev, files, "-I", "-e", crdKind)
if err != nil {
return false, err
}
for _, f := range found {
candidates[f] = true
}
}
for _, c := range changes {
if !candidates[c.path] {
continue
}
var before, after map[string]string
if c.status != 'A' {
content, err := git("cat-file", "blob", parent+":"+c.path)
if err != nil {
return false, err
}
before = crdDocs(content)
}
if c.status != 'D' {
content, err := git("cat-file", "blob", merge+":"+c.path)
if err != nil {
return false, err
}
after = crdDocs(content)
}
if !maps.Equal(before, after) {
return true, nil
}
}
return false, nil
}
// manifest reports whether path is a file a chart or a cluster can apply: YAML,
// JSON or a Helm template. A CRD quoted in documentation or code applies
// nothing.
func manifest(path string) bool {
switch filepath.Ext(path) {
case ".yaml", ".yml", ".json", ".tpl":
return true
}
dirs := strings.Split(path, "/")
return slices.ContainsFunc(dirs[:len(dirs)-1], func(d string) bool { return d == "templates" || d == "crds" })
}
func testFixture(path string) bool {
dirs := strings.Split(path, "/")
return slices.ContainsFunc(dirs[:len(dirs)-1], func(d string) bool { return d == "tests" || d == "testdata" })
}
// grepFiles lists which of files at rev hold any of the fixed strings that
// patterns give. Paths are passed literally, so a name such as ":(exclude)x"
// means only that file.
func grepFiles(rev string, files []string, patterns ...string) ([]string, error) {
const chunk = 500
var found []string
for len(files) > 0 {
n := min(chunk, len(files))
args := append([]string{"--literal-pathspecs", "grep", "-l", "-z", "-F"}, patterns...)
args = append(append(args, "--end-of-options", rev, "--"), files[:n]...)
stdout, stderr, code, err := gitStatus(args...)
switch {
case err != nil:
return nil, err
case code != 0 && code != 1:
return nil, fmt.Errorf("git grep at %s: exit %d: %s", rev, code, strings.TrimSpace(stderr))
}
for _, hit := range splitNUL(stdout) {
if path, ok := strings.CutPrefix(hit, rev+":"); ok {
found = append(found, path)
}
}
files = files[n:]
}
return found, nil
}
var (
docSeparatorRE = regexp.MustCompile(`(?m)^---[ \t]*(?:#.*)?$`)
// A document that declares the kind on a line of its own, in YAML or in
// JSON, under a bare or quoted key, is a CRD even when it cannot be read;
// a sentence or a lookup call that names it declares nothing.
crdKindLineRE = regexp.MustCompile(`(?m)^[ \t]*(?:-[ \t]+)?(?:kind|"kind"|'kind')[ \t]*:[ \t]*["']?CustomResourceDefinition["']?[ \t]*,?[ \t]*(?:#.*)?$`)
)
// crdDocs maps the CRD documents of a file to their text, keyed by
// metadata.name, so a CRD added, removed or edited changes the map. Every
// document that names the kind is read, so a CRD in compact JSON or under a
// quoted key counts as well. A document that declares the kind on a line of
// its own but cannot be read as an object, a CRD behind a Helm template guard
// for one, is kept under its own text, so that any change to it counts: the
// check fails closed. So does a list holding a CRD.
func crdDocs(content string) map[string]string {
docs := map[string]string{}
for _, doc := range docSeparatorRE.Split(content, -1) {
declares := crdKindLineRE.MatchString(doc)
if !declares && !strings.Contains(doc, crdKind) {
continue
}
var head struct {
Kind string `json:"kind"`
Metadata struct {
Name string `json:"name"`
} `json:"metadata"`
Items []struct {
Kind string `json:"kind"`
} `json:"items"`
}
key := ""
switch err := yaml.Unmarshal([]byte(doc), &head); {
case err != nil && declares:
key = "unreadable:" + doc
case err != nil:
continue
case head.Kind == crdKind && head.Metadata.Name != "":
key = "crd:" + head.Metadata.Name
case head.Kind == crdKind:
key = "unnamed:" + doc
case strings.HasSuffix(head.Kind, "List") && slices.ContainsFunc(head.Items, func(i struct {
Kind string `json:"kind"`
}) bool {
return i.Kind == crdKind
}):
key = "list:" + doc
default:
continue
}
docs[key] += doc
}
return docs
}
+461
View File
@@ -0,0 +1,461 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"maps"
"slices"
"strings"
"testing"
)
func TestTypeRule(t *testing.T) {
cases := []struct {
title string
taken bool
}{
{"fix(postgres): keep the replica count on resize", true},
{"fix: typo in monitoring-agents values.yaml", true},
{"security(dashboard): escape the tenant name", true},
{"fix(ci): retry the flaky e2e step", true},
{"[Backport release-1.6] fix(linstor): x", true},
{"[Backport release-1.5] [Backport release-1.6] fix(linstor): x", true},
{"fix(platform, dashboard): two scopes", true},
{"feat(postgres): add a field", false},
{"chore(release): promote", false},
{"docs(release): split the page", false},
{"refactor(api): move code", false},
{"fix(deps): bump golang.org/x/net", false},
{"fix(platform, deps): bump and fix", false},
{"Fix(postgres): capitalised type", false},
{"fix postgres without a colon", false},
{"[postgres] bracket form", false},
{"fixes(postgres): not the fix type", false},
{"", false},
}
for _, tc := range cases {
reason := typeRule(tc.title)
if (reason == "") != tc.taken {
t.Errorf("typeRule(%q) = %q, want taken=%v", tc.title, reason, tc.taken)
}
}
}
func TestBreakingRule(t *testing.T) {
cases := []struct {
name, title, body string
breaking bool
}{
{"plain fix", "fix(x): y", "## What this PR does\n\nFixes it.", false},
{"bang", "fix(x)!: y", "", true},
{"bang without scope", "fix!: y", "", true},
{"bang behind a backport prefix", "[Backport release-1.6] fix(x)!: y", "", true},
{"footer", "fix(x): y", "Body.\n\nBREAKING CHANGE: the field is gone", true},
{"hyphenated footer", "fix(x): y", "BREAKING-CHANGE: the field is gone", true},
{"footer quoted mid-line", "fix(x): y", "This is not a BREAKING CHANGE: nothing changes.", false},
{"footer inside the template comment", "fix(x): y", "<!--\nBREAKING CHANGE: example\n-->\nBody.", false},
{"lower-case footer", "fix(x): y", "breaking change: no", false},
}
for _, tc := range cases {
reason := breakingRule(tc.title, tc.body)
if (reason != "") != tc.breaking {
t.Errorf("%s: breakingRule = %q, want breaking=%v", tc.name, reason, tc.breaking)
}
}
}
// Rule 4 reads the merge itself, so each case is a pull request merged into a
// real repository.
func TestTouchesRule(t *testing.T) {
cases := []struct {
name string
setup map[string]string // committed on main before the pull request
files map[string]string
want string
}{
{name: "a chart change", files: map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"}},
{name: "a migration script", files: map[string]string{migrationsDir + "61": "#!/bin/sh\n"}, want: "platform migrations"},
{name: "a migration library file", files: map[string]string{migrationsDir + "lib/common.sh": "x\n"}, want: "platform migrations"},
{name: "the migration runner is not a migration", files: map[string]string{"packages/core/platform/images/migrations/run-migrations.sh": "x\n"}},
{
name: "targetVersion moves",
setup: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 60\n"},
files: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 61\n"},
want: "targetVersion",
},
{
name: "another key of the platform values",
setup: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 60\n"},
files: map[string]string{platformValues: "migrations:\n enabled: true\n targetVersion: 60\n"},
},
{name: "api types the gate sees nothing in", setup: demoAPI, files: map[string]string{"api/apps/v1alpha1/types.go": "package v1alpha1\n"}},
{name: "pkg/apis is not api/", files: map[string]string{"pkg/apis/apps/types.go": "package apps\n"}},
{name: "a CRD added under crds/", files: map[string]string{"packages/system/x/charts/x/crds/a.yaml": crdDoc("as.x.io", "a") + "\n"}, want: "a CRD"},
{name: "a ConfigMap under crds/ is not a CRD", files: map[string]string{"packages/system/x/charts/x/crds/a.yaml": "kind: ConfigMap\n"}},
{
name: "a DaemonSet edit next to an unchanged CRD",
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("2"))},
},
{
name: "a real CRD edit in a manifest with other documents",
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "b"), daemonSet("1"))},
want: "a CRD",
},
{
name: "a CRD renamed",
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("bs.x.io", "a"), daemonSet("1"))},
want: "a CRD",
},
{name: "a CRD in a fixture under tests/", files: map[string]string{"packages/system/x/tests/render_test.yaml": crdDoc("as.x.io", "a")}},
{name: "a CRD in a fixture under testdata/", files: map[string]string{"hack/testdata/crd.yaml": crdDoc("as.x.io", "a")}},
{
name: "a templated CRD edited, which cannot be read",
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("1"))},
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("b"), daemonSet("1"))},
want: "a CRD",
},
{
name: "a DaemonSet edit next to an unchanged templated CRD",
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("1"))},
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("2"))},
},
{
name: "an unparseable file that names the kind, changed",
setup: map[string]string{"x/broken.yaml": "kind: CustomResourceDefinition\nspec: [unclosed\n"},
files: map[string]string{"x/broken.yaml": "kind: CustomResourceDefinition\nspec: [still unclosed\n"},
want: "a CRD",
},
{
name: "a lookup that names the kind in a Helm helper",
setup: map[string]string{"packages/system/x/templates/_helpers.tpl": "{{- $crd := lookup \"apiextensions.k8s.io/v1\" \"CustomResourceDefinition\" \"\" .crd -}}\n{{- if $crd }}a{{ end }}\n"},
files: map[string]string{"packages/system/x/templates/_helpers.tpl": "{{- $crd := lookup \"apiextensions.k8s.io/v1\" \"CustomResourceDefinition\" \"\" .crd -}}\n{{- if $crd }}b{{ end }}\n"},
},
{
name: "a CRD example in documentation",
setup: map[string]string{"docs/crds.md": "Example:\n\n```yaml\nkind: CustomResourceDefinition\nmetadata:\n name: as.x.io\n```\n"},
files: map[string]string{"docs/crds.md": "An example:\n\n```yaml\nkind: CustomResourceDefinition\nmetadata:\n name: bs.x.io\n```\n"},
},
{name: "a CRD in a template without a YAML extension", files: map[string]string{"packages/system/x/templates/crd.tmpl": crdDoc("as.x.io", "a")}, want: "a CRD"},
{name: "a CRD added as JSON", files: map[string]string{"x/crd.json": `{"apiVersion": "apiextensions.k8s.io/v1",` + "\n" + ` "kind": "CustomResourceDefinition",` + "\n" + ` "metadata": {"name": "as.x.io"}}` + "\n"}, want: "a CRD"},
{name: "a List of CRDs", files: map[string]string{"x/list.yaml": "kind: List\nitems:\n- kind: CustomResourceDefinition\n metadata:\n name: as.x.io\n"}, want: "a CRD"},
{name: "a template declaring a CRD", files: map[string]string{"packages/system/x/templates/crd.yaml": "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\n"}, want: "a CRD"},
{name: "a quoted declaration", files: map[string]string{"x/crd.yaml": " kind: \"CustomResourceDefinition\" # yes\n"}, want: "a CRD"},
{
name: "a CRD changed in place",
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\nspec: {}\n"},
files: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\nspec: {a: 1}\n"},
want: "a CRD",
},
{
name: "a CRD deleted",
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\n"},
files: map[string]string{"x/crd.yaml": ""},
want: "a CRD",
},
{
name: "a CRD turned into something else",
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\n"},
files: map[string]string{"x/crd.yaml": "kind: ConfigMap\n"},
want: "a CRD",
},
{name: "a CRD list is not a CRD", files: map[string]string{"x/list.yaml": "kind: CustomResourceDefinitionList\n"}},
{name: "a CRD as compact JSON", files: map[string]string{"x/crd.json": `{"apiVersion":"apiextensions.k8s.io/v1","kind":"CustomResourceDefinition","metadata":{"name":"as.x.io"}}` + "\n"}, want: "a CRD"},
{name: "a CRD under a quoted kind key", files: map[string]string{"x/crd.yaml": "'kind': CustomResourceDefinition\nmetadata:\n name: as.x.io\n"}, want: "a CRD"},
{
name: "a templated CRD under a quoted kind key, edited",
setup: map[string]string{"packages/system/x/templates/m.yaml": strings.Replace(templatedCRD("a"), "kind:", "'kind':", 1)},
files: map[string]string{"packages/system/x/templates/m.yaml": strings.Replace(templatedCRD("b"), "kind:", "'kind':", 1)},
want: "a CRD",
},
{name: "a ConfigMap as compact JSON naming the kind", files: map[string]string{"x/cm.json": `{"kind":"ConfigMap","data":{"note":"kind: CustomResourceDefinition"}}` + "\n"}},
{name: "a CRD mentioned in prose", files: map[string]string{"x/README.md": "Set kind: CustomResourceDefinition here.\n"}},
{name: "a file named like an option declaring a CRD", files: map[string]string{"-e.yaml": "kind: CustomResourceDefinition\n"}, want: "a CRD"},
// As pathspec magic, :(icase)... would match a file of that name in
// another case, and there is none.
{name: "a file named like pathspec magic declaring a CRD", files: map[string]string{":(icase)zz-crd.yaml": "kind: CustomResourceDefinition\n"}, want: "a CRD"},
{
name: "a migration and a sizeable API change together",
setup: demoAPI,
files: map[string]string{migrationsDir + "61": "x\n", "api/x.go": "package x\n", demoRD: cozyrd("demo", `{"type":"object","properties":{}}`)},
want: "platform migrations, a sizeable API change (breaking-change: [v1alpha1] spec: field \"size\" was removed)",
},
}
for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
setup := maps.Clone(apiBase)
maps.Copy(setup, tc.setup)
f.squash(1000, "chore: setup", "", setup)
mc := f.pr(10+i, "fix(x): y", "", tc.files)
o := &original{number: 10 + i, merge: mc, parents: 2}
changes, err := changedFiles(mc+"^1", mc)
if err != nil {
t.Fatal(err)
}
got, err := touchesRule(o, changes)
if err != nil {
t.Fatal(err)
}
if got != tc.want {
t.Errorf("touchesRule = %q, want %q", got, tc.want)
}
})
}
}
// Rule 6: an older line takes only what the newer line already carries, and
// rule 1: neither line takes what it carries already.
func TestNewerLineAndOnLine(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(2, "fix(app): before nothing", "", map[string]string{"app/a.txt": "a\n"})
f.pr(3, "fix(app): picked onto 1.1", "", map[string]string{"app/b.txt": "b\n"})
mc3 := f.git("rev-parse", "HEAD")
f.pr(4, "fix(app): only on main", "", map[string]string{"app/c.txt": "c\n"})
f.onLine("release-1.1", "fix(app): picked onto 1.1 (#3)\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/b.txt": "b\n"})
for _, tc := range []struct {
line string
qualified []int
skipped map[int]int
}{
{line: "release-1.1", qualified: []int{2, 4}, skipped: map[int]int{3: ruleOnLine}},
// #1 is a feature; #2 and #4 are not on release-1.1 yet; #3 is.
{line: "release-1.0", qualified: []int{3}, skipped: map[int]int{1: ruleType, 2: ruleNewer, 4: ruleNewer}},
} {
t.Run(tc.line, func(t *testing.T) {
f.git("fetch", "-q", "origin")
l, _ := parseLine(tc.line)
b, err := (&app{gh: &fakeGH{}, stderr: &strings.Builder{}}).prepare(l)
if err != nil {
t.Fatal(err)
}
var got []int
for _, v := range b.qualifying() {
got = append(got, v.o.number)
}
if !slices.Equal(got, tc.qualified) {
t.Errorf("qualifying = %v, want %v", got, tc.qualified)
}
for n, rule := range tc.skipped {
if v := b.find(n); v == nil || v.rule != rule {
t.Errorf("#%d: verdict %+v, want rule %d", n, v, rule)
}
}
})
}
}
// An original merged before the newer line branched is on it, even when the
// newer line has since changed the same lines and a pick would conflict.
func TestNewerLineCarriesWhatItBranchedWith(t *testing.T) {
f := newFixture(t)
f.release("release-1.0")
f.pr(2, "fix(app): two", "", map[string]string{"app/a.txt": "two\n"})
f.release("release-1.1")
mc3 := f.pr(3, "fix(app): three", "", map[string]string{"app/a.txt": "three\n"})
f.onLine("release-1.1", "fix(app): three (#3)\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/a.txt": "three\n"})
if v := verdictOn(t, f, nil, "release-1.0", 2); v.rule != 0 {
t.Errorf("#2 on release-1.0 = rule %d (%s), want it to qualify", v.rule, v.reason)
}
}
// A line branched and not yet released is one the batch runs for, and rule 6
// holds each older line to the next newer one.
func TestNewerLineInTheRCWindow(t *testing.T) {
f := newFixture(t)
f.lines()
f.release("release-1.2")
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
for _, tc := range []struct{ line, reason string }{
{"release-1.2", ""},
{"release-1.1", "not on release-1.2 yet"},
{"release-1.0", "not on release-1.1 yet"},
} {
v := verdictOn(t, f, nil, tc.line, 5)
if v.reason != tc.reason || (v.rule == 0) != (tc.reason == "") {
t.Errorf("#5 on %s = rule %d (%s), want %q", tc.line, v.rule, v.reason, tc.reason)
}
}
}
func TestOnlySupportedLines(t *testing.T) {
f := newFixture(t)
f.release("release-0.9")
f.lines()
code, _, stderr := f.exec(nil, "check", "--line", "release-0.9")
if code != 2 || !strings.Contains(stderr, "not a line the batch runs for") {
t.Errorf("check on release-0.9 = %d (%s), want 2 and a refusal", code, stderr)
}
}
// demoRD is an ApplicationDefinition where apigate looks for one, and demoAPI
// the tree the API cases start from.
const demoRD = "packages/system/demo-rd/cozyrds/demo.yaml"
// apiBase is the least API a tree holds: apigate refuses a tree with none.
var apiBase = map[string]string{"packages/system/keep-rd/cozyrds/keep.yaml": cozyrd("keep", `{"type":"object"}`)}
var demoAPI = map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string","description":"Size."}}}`),
"api/apps/v1alpha1/demo/types.go": "package demo\n\n// Size of it.\ntype Spec struct{ Size string }\n",
}
func cozyrd(name, schema string) string {
return "apiVersion: cozystack.io/v1alpha1\nkind: ApplicationDefinition\nmetadata:\n name: " + name +
"\nspec:\n application:\n kind: " + strings.ToUpper(name[:1]) + name[1:] + "\n plural: " + name + "s\n singular: " + name +
"\n openAPISchema: '" + schema + "'\n"
}
// Rule 4 holds back a change to the API only when the API review gate calls it
// sizeable: a new or removed resource or group, or a breaking schema change.
func TestAPIGateRule(t *testing.T) {
typesGo := "api/apps/v1alpha1/demo/types.go"
cases := []struct {
name string
files map[string]string
want string
}{
{name: "a description reworded", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string","description":"The size."}}}`),
typesGo: "package demo\n\n// The size.\ntype Spec struct{ Size string }\n",
}},
{name: "an optional field added", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string"},"zone":{"type":"string"}}}`),
typesGo: "package demo\n\ntype Spec struct{ Size, Zone string }\n",
}},
{name: "a field removed", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{}}`),
typesGo: "package demo\n\ntype Spec struct{}\n",
}, want: "breaking-change"},
{name: "a field's type changed", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"integer"}}}`),
typesGo: "package demo\n\ntype Spec struct{ Size int }\n",
}, want: "breaking-change"},
{name: "a field made required", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","required":["size"],"properties":{"size":{"type":"string"}}}`),
typesGo: "package demo\n\n// +required\ntype Spec struct{ Size string }\n",
}, want: "breaking-change"},
{name: "a resource added", files: map[string]string{
"packages/system/other-rd/cozyrds/other.yaml": cozyrd("other", `{"type":"object"}`),
"api/apps/v1alpha1/other/types.go": "package other\n",
}, want: "new-resource"},
{name: "a resource removed", files: map[string]string{
demoRD: "",
typesGo: "",
}, want: "removed"},
{name: "a breaking schema change that does not touch api/", files: map[string]string{
demoRD: cozyrd("demo", `{"type":"object","properties":{}}`),
}, want: "breaking-change"},
{name: "an aggregated group registered in a chart", files: map[string]string{
"packages/system/extra/templates/apiservice.yaml": apiService("extra.cozystack.io"),
}, want: "new-api-group"},
}
for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
setup := maps.Clone(apiBase)
maps.Copy(setup, demoAPI)
f.squash(1000, "chore: setup", "", setup)
mc := f.pr(10+i, "fix(x): y", "", tc.files)
o := &original{number: 10 + i, merge: mc, parents: 2}
changes, err := changedFiles(mc+"^1", mc)
if err != nil {
t.Fatal(err)
}
got, err := touchesRule(o, changes)
if err != nil {
t.Fatal(err)
}
if (got != "") != (tc.want != "") || !strings.Contains(got, tc.want) {
t.Errorf("touchesRule = %q, want %q", got, tc.want)
}
})
}
}
func apiService(group string) string {
return "apiVersion: apiregistration.k8s.io/v1\nkind: APIService\nmetadata:\n name: v1alpha1." + group +
"\nspec:\n group: " + group + "\n version: v1alpha1\n"
}
// The API review gate is asked about exactly the changes that reach what
// apigate parses at either end of the merge.
func TestReadsAPI(t *testing.T) {
crd := crdDoc("as.x.io", "a")
cases := []struct {
name string
setup map[string]string
files map[string]string
want bool
}{
{name: "the apiserver's registrations", files: map[string]string{apiserverGo: "package apiserver\n"}, want: true},
{name: "an ApplicationDefinition, which carries no marker", files: map[string]string{demoRD: "kind: ApplicationDefinition\n"}, want: true},
{name: "a CRD in a chart", files: map[string]string{"packages/system/x/crds/a.yaml": crd}, want: true},
{name: "an APIService under internal/", files: map[string]string{"internal/x/apiservice.yml": apiService("x.cozystack.io")}, want: true},
{
name: "a CRD turned into something else",
setup: map[string]string{"packages/system/x/crds/a.yaml": crd},
files: map[string]string{"packages/system/x/crds/a.yaml": "kind: ConfigMap\n"},
want: true,
},
{name: "a binary file with a marker", files: map[string]string{"packages/system/x/a.yaml": "\x00kind: APIService\n"}, want: true},
{name: "chart values", files: map[string]string{"packages/system/x/values.yaml": "replicas: 2\n"}},
{name: "Go types under api/", files: map[string]string{"api/apps/v1alpha1/demo/types.go": "package demo\n"}},
{name: "a CRD outside the roots apigate walks", files: map[string]string{"hack/crds/a.yaml": crd}},
{name: "a CRD in a template without a YAML extension", files: map[string]string{"packages/system/x/templates/crd.tpl": crd}},
{name: "a file beside the ApplicationDefinitions", files: map[string]string{"packages/system/demo-rd/cozyrds/sub/demo.yaml": "kind: ApplicationDefinition\n"}},
}
for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
setup := maps.Clone(apiBase)
maps.Copy(setup, tc.setup)
f.squash(1000, "chore: setup", "", setup)
mc := f.pr(10+i, "fix(x): y", "", tc.files)
changes, err := changedFiles(mc+"^1", mc)
if err != nil {
t.Fatal(err)
}
got, err := readsAPI(mc+"^1", mc, changes)
if err != nil {
t.Fatal(err)
}
if got != tc.want {
t.Errorf("readsAPI = %v, want %v", got, tc.want)
}
})
}
}
func crdDoc(name, version string) string {
return "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n name: " + name +
"\nspec:\n group: x.io\n versions:\n - name: v1" + version + "\n"
}
func daemonSet(replicas string) string {
return "apiVersion: apps/v1\nkind: DaemonSet\nmetadata:\n name: agent\nspec:\n template:\n spec:\n priority: {{ .Values.priority | default " + replicas + " }}\n"
}
func templatedCRD(version string) string {
return "{{- if .Values.crds }}\n" + crdDoc("ts.x.io", version) + "{{- end }}\n"
}
func multiDoc(docs ...string) string { return "---\n" + strings.Join(docs, "---\n") }
+530
View File
@@ -0,0 +1,530 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"slices"
"strconv"
"strings"
)
// A batch carries each original as one unit on its first-parent line. A
// merged pull request is a merge commit whose first parent is the batch before
// it and whose second parent ends a straight line of picks of the pull
// request's own commits, oldest first, each keeping its author and sign-offs;
// the merge says which merge commit on main it carries in a Backported-from
// trailer. A squash-merged original is one pick of its commit on main.
// pickOriginal picks one original onto HEAD in that shape. A conflict on any
// of its commits takes the branch back to where it was before the original,
// so nothing of it is left half applied, and so does a clean pick validate
// finds leaving something of the original without effect: partial, with
// those paths. Hooks are off so that each commit carries its message and -x
// line as git writes them; signing is left to the user's configuration.
func pickOriginal(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
p := pick{v: v}
base, err := resolve("HEAD")
if err != nil {
return p, err
}
for _, c := range v.commits {
status, files, err := applyPick("-c", "core.hooksPath=/dev/null", "cherry-pick", "-x", "--signoff", "--end-of-options", c)
if err != nil {
if rerr := resetTo(base); rerr != nil {
return p, rerr
}
return p, fmt.Errorf("cherry-pick of %.12s of #%d: %w", c, v.o.number, err)
}
if status == "conflict" {
p.status, p.files, p.failed = "conflict", files, c
return p, resetTo(base)
}
}
head, err := resolve("HEAD")
switch {
case err != nil:
return p, err
case head == base && validate != nil:
// Picks that change nothing carry the original only when the line
// already has its changes: a change made where its branch merged
// main is in no pick.
omitted, err := validate(base, head)
if err != nil {
return p, err
}
if len(omitted) > 0 {
p.status, p.files = "partial", omitted
return p, nil
}
p.status = "empty"
return p, nil
case head == base:
p.status = "empty"
return p, nil
}
if validate != nil {
omitted, err := validate(base, head)
if err == nil && len(omitted) > 0 {
p.status, p.files = "partial", omitted
}
if err != nil || len(omitted) > 0 {
return p, errors.Join(err, resetTo(base))
}
}
if v.o.parents < 2 {
p.status, p.commit = "picked", head
return p, nil
}
msgs, err := commitMessages([]string{v.o.merge})
if err != nil {
return p, err
}
if p.commit, err = commitMerge(base, head, mergeMessage(msgs[v.o.merge].message, v.o.merge)); err != nil {
if rerr := resetTo(base); rerr != nil {
return p, rerr
}
return p, err
}
p.status = "picked"
return p, nil
}
// mergeMessage is the message of a batch merge: the one of the merge commit
// on main, which says which pull request it was, and a paragraph of its own
// naming that merge commit, so nothing the original's text ends with can pass
// for the trailer.
func mergeMessage(message, merge string) string {
return strings.TrimRight(message, " \t\r\n") + "\n\n" + batchTrailer + ": " + merge + "\n"
}
// pickUnit and replayNext start the next unit only while the run's output is
// intact: once it was lost no new unit starts, and a unit already in flight
// still finishes its rollback.
func (a *app) pickUnit(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
if err := a.mayAct(); err != nil {
return pick{}, err
}
return pickOriginal(v, validate)
}
func (a *app) replayNext(u unit) (string, []string, error) {
if err := a.mayAct(); err != nil {
return "", nil, err
}
return replayUnit(u)
}
// commitMerge makes the merge of tip onto base, whose tree is tip's since tip
// descends from base, and moves the current branch from tip to it.
func commitMerge(base, tip, message string) (string, error) {
f, err := os.CreateTemp("", "backport-batch-merge-*.txt")
if err != nil {
return "", err
}
defer func() { _ = os.Remove(f.Name()) }()
if _, err := f.WriteString(message); err != nil {
_ = f.Close()
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
out, err := git("commit-tree", "-p", base, "-p", tip, "-F", f.Name(), tip+"^{tree}")
if err != nil {
return "", err
}
merge := strings.TrimSpace(out)
if err := checkSHA(merge); err != nil {
return "", fmt.Errorf("commit-tree: %w", err)
}
if _, err := git("update-ref", "-m", "backport-batch: merge", "HEAD", merge, tip); err != nil {
return "", err
}
return merge, nil
}
// resetTo takes the current branch, its index and its tree back to commit,
// which run owns: it starts only in a clean tree.
func resetTo(commit string) error {
_, err := git("reset", "--quiet", "--hard", commit)
return err
}
// unit is one original as a batch carries it: a commit on the batch's
// first-parent line, and for a merge the picks its second parent brings.
type unit struct {
commit string
parents []string
message string
// picks are the commits reachable from the second parent and not from the
// first, oldest first.
picks []string
}
func (u unit) merge() bool { return len(u.parents) == 2 }
func readUnit(commit string) (unit, error) {
msgs, err := commitMessages([]string{commit})
if err != nil {
return unit{}, err
}
u := unit{commit: commit, parents: msgs[commit].parents, message: msgs[commit].message}
if u.merge() {
if u.picks, err = revList("--reverse", "--topo-order", "--end-of-options", u.parents[1], "^"+u.parents[0]); err != nil {
return unit{}, err
}
}
return u, nil
}
// batchUnits lists the units of the batch from base to head, oldest first.
func batchUnits(head, base string) ([]unit, error) {
oids, err := revList("--reverse", "--first-parent", "--end-of-options", head, "^"+base)
if err != nil {
return nil, err
}
var units []unit
for _, oid := range oids {
u, err := readUnit(oid)
if err != nil {
return nil, err
}
units = append(units, u)
}
return units, nil
}
// unitOriginal is the original a unit says it carries, or nil: the one its
// Backported-from trailer names, or the one its appended -x line picks.
func (b *batch) unitOriginal(u unit) *verdict {
if u.merge() {
if merges := batchedFrom(u.message); len(merges) == 1 {
return b.findMerge(merges[0])
}
return nil
}
source := appendedSource(u.message)
if v := b.findMerge(source); v != nil {
return v
}
return b.pickedBy([]string{source})
}
// ownerOf is the original one of whose own commits source is, or nil. It reads
// every original's commits, so it is for explaining a refusal only.
func (b *batch) ownerOf(source string) (*verdict, error) {
for _, v := range b.verdicts {
if v.commits == nil {
commits, err := prCommits(v.o)
if err != nil {
return nil, err
}
v.commits = commits
}
if v.o.parents > 1 && slices.Contains(v.commits, source) {
return v, nil
}
}
return nil, nil
}
// checkUnit says why u is not one original carried in the batch's shape, or
// "" when it is, and which original it carries: a merge whose second parent
// is a straight line from its first, of picks of the pull request's own
// commits in their order, each with the -x line of the commit it came from,
// that names the original's merge commit in its trailer; or for a squash one
// pick of its commit on main. Either changes something, and only files the
// original changes. A pick is known by the -x line git appended, not by
// lines its message brought along, and one that also claims another commit
// of the same original is refused.
func checkUnit(b *batch, u unit) (string, *verdict, error) {
switch len(u.parents) {
case 1:
return checkPick(b, u)
case 2:
return checkMerge(b, u)
}
return fmt.Sprintf("%.12s has %d parents", u.commit, len(u.parents)), nil, nil
}
func checkPick(b *batch, u unit) (string, *verdict, error) {
source := appendedSource(u.message)
if source == "" {
return fmt.Sprintf("%.12s ends in no (cherry picked from commit …) line; a batch carries originals only", u.commit), nil, nil
}
v := b.findMerge(source)
if v == nil {
owner, err := b.ownerOf(source)
if err != nil {
return "", nil, err
}
if owner != nil {
return fmt.Sprintf("%.12s picks one commit of #%d on its own; a merged pull request goes in as a merge of all its commits", u.commit, owner.o.number), owner, nil
}
return fmt.Sprintf("%.12s picks %s, which merged no pull request into main after %s branched", u.commit, source, b.line.branch()), nil, nil
}
if v.o.parents > 1 {
return fmt.Sprintf("%.12s picks the merge commit of #%d as one commit; a merged pull request goes in as a merge of its own commits", u.commit, v.o.number), v, nil
}
why, err := contained(b, v, u.parents[0], u.commit)
return why, v, err
}
func checkMerge(b *batch, u unit) (string, *verdict, error) {
merges := batchedFrom(u.message)
if len(merges) != 1 {
return fmt.Sprintf("%.12s is a merge with %d %s trailers, not one", u.commit, len(merges), batchTrailer), nil, nil
}
v := b.findMerge(merges[0])
switch {
case v == nil:
return fmt.Sprintf("%.12s names %s, which merged no pull request into main after %s branched", u.commit, merges[0], b.line.branch()), nil, nil
case v.o.parents < 2:
return fmt.Sprintf("%.12s merges #%d, which main took as one commit; it goes in as one pick", u.commit, v.o.number), v, nil
case len(u.picks) == 0:
return fmt.Sprintf("%.12s merges nothing", u.commit), v, nil
}
if err := b.load(v); err != nil {
return "", nil, err
}
msgs, err := commitMessages(u.picks)
if err != nil {
return "", nil, err
}
prev, last := u.parents[0], -1
for _, c := range u.picks {
ps := msgs[c].parents
if len(ps) != 1 || ps[0] != prev {
return fmt.Sprintf("%.12s is not on a straight line of picks from %.12s", c, u.parents[0]), v, nil
}
source := appendedSource(msgs[c].message)
if source == "" {
return fmt.Sprintf("%.12s ends in no (cherry picked from commit …) line", c), v, nil
}
i := slices.Index(v.commits, source)
if i < 0 {
owner, err := b.ownerOf(source)
switch {
case err != nil:
return "", nil, err
case owner != nil:
return fmt.Sprintf("%.12s picks a commit of #%d into the merge of #%d", c, owner.o.number, v.o.number), v, nil
}
return fmt.Sprintf("%.12s picks %s, which is not a commit of #%d", c, source, v.o.number), v, nil
}
if i <= last {
return fmt.Sprintf("%.12s picks %.12s out of the order of #%d's commits", c, source, v.o.number), v, nil
}
for _, claimed := range claimedSources(msgs[c].message) {
if claimed != source && (claimed == v.o.merge || slices.Contains(v.commits, claimed)) {
return fmt.Sprintf("%.12s claims more than one commit of #%d", c, v.o.number), v, nil
}
}
prev, last = c, i
}
why, err := contained(b, v, u.parents[0], u.commit)
return why, v, err
}
// contained says why the change from base to tip is not inside the files v
// changes, or the paths a rename on main or on the line gave them at base,
// or "" when it is.
func contained(b *batch, v *verdict, base, tip string) (string, error) {
if err := b.load(v); err != nil {
return "", err
}
changes, err := changedFiles(base, tip)
if err != nil {
return "", err
}
if len(changes) == 0 {
return fmt.Sprintf("%.12s changes nothing", tip), nil
}
corr, _, err := b.correspondence(v, base)
if err != nil {
return "", err
}
allowed := paths(v.changes)
for _, c := range corr {
allowed = append(allowed, c)
}
var extra []string
for _, p := range paths(changes) {
if !slices.Contains(allowed, p) {
extra = append(extra, strconv.Quote(p))
}
}
if len(extra) > 0 {
return fmt.Sprintf("%.12s touches %s, which #%d does not touch", tip, strings.Join(extra, ", "), v.o.number), nil
}
return "", nil
}
// replayUnit picks a unit of the batch again onto HEAD in its own shape: a
// pushed resolution keeps what it resolved. On a conflict it takes the branch
// back to where it was before the unit and names the commit that failed.
func replayUnit(u unit) (string, []string, error) {
base, err := resolve("HEAD")
if err != nil {
return "", nil, err
}
picks := u.picks
if !u.merge() {
picks = []string{u.commit}
}
for _, c := range picks {
status, files, err := applyPick("-c", "core.hooksPath=/dev/null", "cherry-pick", "--end-of-options", c)
if err == nil && status == "conflict" {
return c, files, resetTo(base)
}
if err != nil {
if rerr := resetTo(base); rerr != nil {
return "", nil, rerr
}
return "", nil, fmt.Errorf("picking %.12s again: %w", c, err)
}
}
tip, err := resolve("HEAD")
if err != nil || !u.merge() || tip == base {
return "", nil, err
}
_, err = commitMerge(base, tip, u.message)
return "", nil, err
}
// pickPair is a pick on the batch and the commit it was picked from.
type pickPair struct{ source, pick string }
// unitShape is what an original's unit on the batch brought: how many
// commits, the picks that differ from the commit they came from, a
// resolution or a pick git had to fit to the line, and whether the unit as a
// whole makes another change than the original's merge.
type unitShape struct {
unit string
commits int
pairs []pickPair
net bool
}
// changed says whether the unit differs from its original, so its release
// note may no longer hold.
func (s unitShape) changed() bool { return len(s.pairs) > 0 || s.net }
// String is the shape as the body lists it.
func (s unitShape) String() string {
out := "1 commit"
if s.commits != 1 {
out = fmt.Sprintf("%d commits", s.commits)
}
if len(s.pairs) > 0 {
var picks []string
for _, p := range s.pairs {
picks = append(picks, codeSpan(p.pick[:12]))
}
out += ", changed in the pick: " + strings.Join(picks, ", ")
} else if s.net {
out += ", changed as a whole: some of its change is not in the picks"
}
return out
}
// summarize says, for each original the batch head carries in units, what
// its unit brought.
func summarize(b *batch, head string, on []*verdict) (map[*verdict]unitShape, error) {
units, err := batchUnits(head, b.lineHead)
if err != nil {
return nil, err
}
out := map[*verdict]unitShape{}
for _, u := range units {
v := b.unitOriginal(u)
if v == nil || !slices.Contains(on, v) {
continue
}
picks := u.picks
if !u.merge() {
picks = []string{u.commit}
}
msgs, err := commitMessages(picks)
if err != nil {
return nil, err
}
s := unitShape{unit: u.commit, commits: len(picks)}
for _, c := range picks {
source := appendedSource(msgs[c].message)
if source == "" {
continue
}
same, err := samePatch(source, c)
if err != nil {
return nil, err
}
if !same {
s.pairs = append(s.pairs, pickPair{source: source, pick: c})
}
}
// A pick that came out empty leaves no pick to differ, only a net
// change short of the original's.
mine, err := patchIDRange(v.o.merge+"^1", v.o.merge)
if err != nil {
return nil, err
}
theirs, err := patchIDRange(u.parents[0], u.commit)
if err != nil {
return nil, err
}
s.net = mine != theirs
out[v] = s
}
return out, nil
}
func plural(n int, word string) string {
if n == 1 {
return word
}
return word + "s"
}
// samePatch says whether two commits make the same change to their first
// parents, by git patch-id of their diffs without context, so that a pick
// that only lands among other lines on the line counts as the same.
func samePatch(a, b string) (bool, error) {
pa, err := patchIDRange(a+"^", a)
if err != nil {
return false, err
}
pb, err := patchIDRange(b+"^", b)
return pa == pb, err
}
// patchIDRange is the patch-id of the change from one revision to another.
func patchIDRange(from, to string) (string, error) {
diff, err := git("diff", "--no-ext-diff", "--no-textconv", "--unified=0", "--end-of-options", from, to)
if err != nil {
return "", err
}
out, err := gitInput(diff, "patch-id", "--stable")
if err != nil {
return "", err
}
id, _, _ := strings.Cut(strings.TrimSpace(out), " ")
return id, nil
}
+372
View File
@@ -0,0 +1,372 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// A conflict on a commit in the middle of a pull request takes the batch back
// to where it was before that original: none of its commits stays, the
// conflict is recorded with the commit that failed, and the next original is
// still picked.
func TestRunConflictInAMiddleCommit(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
mc5 := f.prCommits(5, "fix(chart): five", "",
change{files: map[string]string{"app/five.txt": "5\n"}},
change{files: map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"}},
change{files: map[string]string{"app/five.txt": "5, done\n"}})
own := strings.Fields(f.git("rev-list", "--reverse", mc5+"^1.."+mc5+"^2"))
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("the batch carries %q, want only #6", got)
}
if log := f.git("log", "--format=%B", "origin/release-1.1..HEAD"); strings.Contains(log, own[0]) || strings.Contains(log, own[1]) {
t.Errorf("a commit of #5 stayed on the batch:\n%s", log)
}
if out := f.git("status", "--porcelain"); out != "" {
t.Errorf("the tree is left dirty:\n%s", out)
}
if _, err := os.Stat(filepath.Join(f.dir, ".git", "CHERRY_PICK_HEAD")); err == nil {
t.Error("a cherry-pick is left in progress")
}
l, _ := parseLine("release-1.1")
deferred, err := parseDeferred(stdout, l)
if err != nil || deferred[5].Commit != own[1] || !slices.Equal(deferred[5].Files, []string{"chart/values.yaml"}) {
t.Errorf("deferred = %+v, %v; want #5 at %s in chart/values.yaml", deferred, err, own[1])
}
if !strings.Contains(stdout, "`fix(chart): five` at `"+own[1][:12]+"`: `chart/values.yaml`") {
t.Errorf("the body does not name the commit that failed:\n%s", stdout)
}
}
// What counts as carrying a merged pull request of several commits: picks
// whose appended -x lines name every commit of its own, as a batch merge's do,
// or a pick whose appended -x line names its merge commit; not some of its
// commits. A Backported-from trailer counts only on a commit from before the
// batch-only day, and only in its trailer block.
func TestPresenceOfAMultiCommitOriginal(t *testing.T) {
cases := []struct {
name string
land func(f *fixture, mc string, own []string)
carried string
}{
{
name: "a batch merge of its commits",
land: func(f *fixture, mc string, own []string) {
f.git("fetch", "-q", "origin")
base := f.git("rev-parse", "origin/release-1.1")
f.git("switch", "-q", "--detach", base)
for _, c := range own {
f.git("cherry-pick", "-x", c)
}
f.git("switch", "-q", "--detach", f.joined(base, f.git("rev-parse", "HEAD"), "fix(app): five (#5)\n\n"+batchTrailer+": "+mc))
f.git("push", "-q", "origin", "HEAD:refs/heads/release-1.1")
f.git("switch", "-q", "main")
},
carried: "its 2 commits picked",
},
{
name: "a pick whose -x line names the merge commit",
land: func(f *fixture, mc string, _ []string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc+")", map[string]string{"app/a.txt": "a\n"})
},
carried: "picked in",
},
{
name: "picks whose -x lines name every commit of its own",
land: func(f *fixture, _ string, own []string) {
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
f.onLine("release-1.1", "b\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/b.txt": "b, half\n"})
},
carried: "its 2 commits picked",
},
{
name: "a pick of one of its two commits",
land: func(f *fixture, _ string, own []string) {
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
},
},
{
name: "the trailer alone, from before the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
},
carried: "named by a " + batchTrailer + " trailer",
},
{
name: "the trailer alone, after the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T00:00:00Z")
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
},
},
{
name: "the trailer in the body, from before the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
f.onLine("release-1.1", "port\n\n"+batchTrailer+": "+mc+"\n\nMore to say.", map[string]string{"app/a.txt": "a\n"})
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.prCommits(5, "fix(app): five", "",
change{files: map[string]string{"app/a.txt": "a\n"}},
change{files: map[string]string{"app/b.txt": "b\n"}})
own := strings.Fields(f.git("rev-list", "--reverse", mc+"^1.."+mc+"^2"))
tc.land(f, mc, own)
v := verdictOn(t, f, nil, "release-1.1", 5)
switch {
case tc.carried == "" && v.rule != 0:
t.Errorf("verdict = rule %d (%s), want it to qualify", v.rule, v.reason)
case tc.carried != "" && (v.rule != ruleOnLine || !strings.HasPrefix(v.reason, tc.carried)):
t.Errorf("verdict = rule %d (%s), want rule 1 with %q", v.rule, v.reason, tc.carried)
}
})
}
}
// What a pull request's author writes reaches the line with every commit the
// batch picks and with the merge message it carries. None of it makes another
// original count as carried: not an -x line, a Backported-from trailer or a
// Backport of naming it, not at the end of the message where the line git
// appends lands in the same trailer block, and not an -x line naming one of
// the other original's own commits. The line is read as after the batch-only
// day, as a batch made after it leaves it.
func TestPresenceCannotBeClaimedByText(t *testing.T) {
type world struct {
f *fixture
mc5, c5 string
}
claims := []struct {
name string
claim func(w *world) string
}{
{"an -x line naming the merge commit", func(w *world) string { return "(cherry picked from commit " + w.mc5 + ")" }},
{"an -x line naming its own commit", func(w *world) string { return "(cherry picked from commit " + w.c5 + ")" }},
{"a Backported-from trailer", func(w *world) string { return batchTrailer + ": " + w.mc5 }},
{"a Backport of", func(w *world) string { return "Backport of #5" }},
{"all of them after a sign-off", func(w *world) string {
return "Signed-off" + "-by: " + contributor + "\n(cherry picked from commit " + w.mc5 + ")\n(cherry picked from commit " + w.c5 + ")\n" + batchTrailer + ": " + w.mc5
}},
}
for _, where := range []string{"commit message", "pull request body"} {
for _, tc := range claims {
t.Run(where+": "+tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
// #5 conflicts on the line, so the batch defers it.
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
w := &world{f: f}
w.mc5 = f.pr(5, "fix(chart): five", "", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"})
w.c5 = f.git("rev-parse", w.mc5+"^2")
claim := tc.claim(w)
msg, body := "fix(app): six\n\n"+claim, ""
if where == "pull request body" {
msg, body = "fix(app): six", "Six.\n\n"+claim
}
f.prCommits(6, "fix(app): six", body, change{files: map[string]string{"app/six.txt": "6\n"}, message: msg})
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
// The batch picks #6; on the open batch pull request the text
// claims nothing for #5.
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.bodies[0])}
if v := verdictOn(t, f, gh, "release-1.1", 6); v.onBatch == "" {
t.Fatal("#6 is not on the batch")
}
if v := verdictOn(t, f, gh, "release-1.1", 5); v.onBatch != "" {
t.Errorf("#5 is on the batch in %.12s, want nowhere", v.onBatch)
}
// Nor on the line, once the line takes the batch.
f.git("push", "-q", "origin", f.remoteHead(batchBranch1)+":refs/heads/release-1.1")
if v := verdictOn(t, f, nil, "release-1.1", 6); v.rule != ruleOnLine {
t.Fatalf("#6 = rule %d (%s), want it on the line", v.rule, v.reason)
}
if v := verdictOn(t, f, nil, "release-1.1", 5); v.rule != 0 {
t.Errorf("#5 = rule %d (%s), want it still to qualify", v.rule, v.reason)
}
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
}
})
}
}
}
// verify passes a batch the run built and fails one with an original that is
// not in the batch's shape, reading nothing from GitHub when given the head.
func TestVerify(t *testing.T) {
type world struct {
f *fixture
gh *fakeGH
head string
mc5 string
sq6 string
}
setup := func(t *testing.T) *world {
f := newFixture(t)
f.lines()
w := &world{f: f}
w.mc5 = f.prCommits(5, "fix(app): five", "",
change{files: map[string]string{"app/five.txt": "5\n"}},
change{files: map[string]string{"app/five.txt": "5, done\n"}})
w.sq6 = f.squash(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
w.gh = &fakeGH{}
w.head = openBatch(t, f, w.gh)
return w
}
// on adds a commit with message and files on top of the batch head.
on := func(w *world, message string, files map[string]string) string {
w.f.git("switch", "-q", "--detach", w.head)
w.f.apply(files)
w.f.git("commit", "-q", "--allow-empty", "-m", message)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
}
cases := []struct {
name string
head func(w *world) string
refusal string
}{
{name: "the batch as the run built it", head: func(w *world) string { return w.head }},
{
name: "an original picked as one commit with -m 1",
head: func(w *world) string {
return on(w, "fix(app): five (#5)\n\n(cherry picked from commit "+w.mc5+")", map[string]string{"app/five.txt": "5, again\n"})
},
refusal: "picks the merge commit of #5 as one commit",
},
{
name: "a commit that is no original",
head: func(w *world) string {
return on(w, "fix(ci): adjust for the line", map[string]string{"ci.txt": "x\n"})
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "an original carried twice",
head: func(w *world) string {
own := strings.Fields(w.f.git("rev-list", "--reverse", w.mc5+"^1.."+w.mc5+"^2"))
p := on(w, "again\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/five.txt": "5, done again\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): five (#5)\n\n"+batchTrailer+": "+w.mc5, p)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "carries #5 again",
},
{
name: "a squash-merged original carried as a merge",
head: func(w *world) string {
p := on(w, "six\n\n(cherry picked from commit "+w.sq6+")", map[string]string{"app/six.txt": "6, again\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): six (#6)\n\n"+batchTrailer+": "+w.sq6, p)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "which main took as one commit",
},
{
name: "a merge of the line into the batch",
head: func(w *world) string {
line := w.f.onLine("release-1.1", "fix(chart): on the line", map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "Merge branch 'release-1.1' into the batch", line)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "with 0 " + batchTrailer + " trailers",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
w := setup(t)
head := tc.head(w)
// GitHub is not asked: a failing fake would fail the run.
gh := &fakeGH{err: errString("gh: not to be asked")}
code, stdout, stderr := w.f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
switch {
case tc.refusal == "" && code != 0:
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
case tc.refusal != "" && (code != 1 || !strings.Contains(stdout, tc.refusal)):
t.Errorf("verify = %d, want 1 with %q:\n%s\n%s", code, tc.refusal, stdout, stderr)
}
})
}
t.Run("the open batch pull request", func(t *testing.T) {
w := setup(t)
code, stdout, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, "3 units at "+w.head[:12]) {
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
}
if code, stdout, _ := w.f.exec(&fakeGH{}, "verify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "nothing to verify") {
t.Errorf("verify with no batch open = %d, %q", code, stdout)
}
})
t.Run("a head that is not here", func(t *testing.T) {
w := setup(t)
if code, _, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1", "--head", strings.Repeat("ab", 20)); code != 2 || !strings.Contains(stderr, "fetch it first") {
t.Errorf("verify = %d (%s), want 2", code, stderr)
}
})
}
// A pick that lands among lines the line changed is the same change, and the
// body does not call it changed; one whose change differs is called so.
func TestSummaryCallsOnlyChangedPicksChanged(t *testing.T) {
f := newFixture(t)
f.release("release-1.0")
f.squash(2, "chore(app): a file to change", "", map[string]string{"app/ctx.txt": "a\nb\nc\nd\ne\n"})
f.pr(1, "feat(app): something for 1.1", "", map[string]string{"app/feature.txt": "1.1\n"})
f.release("release-1.1")
f.onLine("release-1.1", "fix(app): a line-only change", map[string]string{"app/ctx.txt": "A\nb\nc\nd\ne\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/ctx.txt": "a\nb\nC\nd\ne\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if !strings.Contains(stdout, "`fix(app): five`, 1 commit\n") {
t.Errorf("a pick git only fit to its place is called changed:\n%s", stdout)
}
}
+26
View File
@@ -0,0 +1,26 @@
# Every path whose change can change the result of
# `go test ./cmd/backport-batch/...`. The plan job in
# .github/workflows/pull-requests.yaml runs those tests when, and only when, the
# pull request touches one of these paths.
#
# One repository-relative path per line. A trailing slash names a directory and
# matches everything below it; anything else matches that file alone. Blank lines
# and lines starting with # are ignored.
#
# TestInputsListed in inputs_test.go keeps this list in step with the code: it
# fails when the tests' in-repo package dependencies (go list -deps -test) or the
# docs pages they read are missing from it.
cmd/backport-batch/
go.mod
go.sum
# In-repo packages the tool and its tests import.
internal/apigate/
pkg/apis/apps/v1alpha1/
pkg/apis/apps/validation/
pkg/config/
# Pages the tests read from the tree.
docs/release/backports.md
docs/release/backport-tasks/
+148
View File
@@ -0,0 +1,148 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"strings"
)
// verify checks every original a batch carries, from the line's head to the
// batch head, against the shape the batch carries it in: what fold checks for
// one resolution, for all of them and for the clean picks too. fold runs only
// where someone resolved a conflict and chose to run it; CI runs verify on
// every push to a batch pull request.
//
// With --head it reads that commit and nothing from GitHub, which is how CI
// runs it: the tool built from main, the batch head fetched as data.
func (a *app) verify(args []string) int {
fs := a.flags("verify")
lineArg := fs.String("line", "", "release line, release-X.Y")
headArg := fs.String("head", "", "the batch head to verify (default: the head of the open batch PR)")
if err := parse(fs, args); err != nil {
return 2
}
l, err := parseLine(*lineArg)
if err != nil {
return a.fail(err)
}
if *headArg != "" {
if err := checkSHA(*headArg); err != nil {
return a.fail(fmt.Errorf("--head: %w", err))
}
}
if err := fetch(); err != nil {
return a.fail(err)
}
b, err := shapeOf(l)
if err != nil {
return a.fail(err)
}
head := *headArg
if head == "" {
prs, err := a.gh.openPulls(l.branch())
if err != nil {
return a.fail(err)
}
pr, warnings, err := batchPull(a.gh, prs, l)
for _, w := range warnings {
a.warn("%s", w)
}
if err != nil {
return a.fail(err)
}
if pr == nil {
_, _ = fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
return 0
}
if head, err = batchHead(pr); err != nil {
return a.fail(err)
}
} else if have, err := lookup(head); err != nil {
return a.fail(err)
} else if have == "" {
return a.fail(fmt.Errorf("--head %.12s is not in this repository; fetch it first", head))
}
units, err := batchUnits(head, b.lineHead)
if err != nil {
return a.fail(err)
}
seen := map[*verdict]string{}
var refused []string
for _, u := range units {
why, v, err := checkUnit(b, u)
if err != nil {
return a.fail(err)
}
if why == "" && seen[v] != "" {
why = fmt.Sprintf("carries #%d again, which %.12s already carries", v.o.number, seen[v])
}
if why == "" {
omitted, _, err := b.omittedEffects(v, u.parents[0], u.commit)
if err != nil {
return a.fail(err)
}
if len(omitted) > 0 {
why = leftOut(v, l, omitted)
}
}
if why != "" {
refused = append(refused, why)
_, _ = fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
continue
}
seen[v] = u.commit
n := len(u.picks)
if !u.merge() {
n = 1
}
_, _ = fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
}
if len(refused) > 0 {
_, _ = fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
return 1
}
_, _ = fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
return 0
}
// shapeOf reads what verify needs of a line, from git alone: its head, and
// every pull request merged into main since it branched, unjudged.
func shapeOf(l line) (*batch, error) {
b := &batch{line: l}
var err error
if b.lineHead, err = resolve("refs/remotes/" + remote + "/" + l.branch()); err != nil {
return nil, err
}
if b.mainHead, err = resolve("refs/remotes/" + remote + "/main"); err != nil {
return nil, err
}
out, err := git("merge-base", "--end-of-options", b.mainHead, b.lineHead)
if err != nil {
return nil, fmt.Errorf("%s shares no history with main: %w", l.branch(), err)
}
b.base = strings.TrimSpace(out)
merges, other, err := mainMerges(b.base, b.mainHead)
if err != nil {
return nil, err
}
b.other = other
for _, o := range merges {
b.verdicts = append(b.verdicts, &verdict{o: o})
}
return b, nil
}
+236
View File
@@ -0,0 +1,236 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strings"
"testing"
)
// chainWorld has #5 conflict on the line and change a shared line, and #7
// change that line again: #7 builds on #5.
func chainWorld(t *testing.T) (*fixture, string) {
t.Helper()
f := newFixture(t)
f.squash(2, "chore: five and shared", "", map[string]string{"app/five.txt": "old\n", "app/shared.txt": appFile})
f.lines()
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!", 1)})
return f, mc5
}
// An original that builds on a deferred conflict waits for it: check warns,
// notify lists it after the conflict, and once the conflict lands, the next
// run picks it.
func TestWaitForADeferredDependency(t *testing.T) {
f, mc5 := chainWorld(t)
gh := &fakeGH{}
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
if code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
if !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) || !strings.Contains(stdout, "### Waiting on another original") {
t.Errorf("#7 does not wait on #5:\n%s", stdout)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "warning: #7 waits on #5, a deferred conflict on #90: https://github.com/cozystack/cozystack/pull/7 ") {
t.Errorf("check = %d, want a warning that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
}
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1", "--push")
if code != 0 || !strings.Contains(gh.issueComments[len(gh.issueComments)-1].Body, "- [#7](https://github.com/cozystack/cozystack/pull/7), after [#5](https://github.com/cozystack/cozystack/pull/5)") {
t.Errorf("notify = %d, want #7 listed after #5:\n%s\n%v", code, stdout, gh.issueComments)
}
// The batch merges, and #5 lands on the line by hand, resolved: the
// next batch picks #7.
mergeBatch(f, gh)
f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
if code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
t.Errorf("run --continue after #5 landed = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
}
}
// Of several prerequisites still to come, the strongest decides: one not on
// the line at all, then one waiting for a maintainer, then a conflict or a
// deferral, then one rule 6 holds; a chain inherits what its end decides.
func TestWaitReduction(t *testing.T) {
t.Run("a conflict and a workflow wait", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
f.lines()
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(6, "fix(ci): six", "", map[string]string{".github/workflows/six.yaml": "on: push\n", "app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
gh := &fakeGH{}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[6]}`) {
t.Fatalf("run = %d, want #7 waiting on #6 alone:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 waits on #6, which changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
t.Errorf("check --cut = %d, want #7 to hold the cut:\n%s\n%s", code, stdout, stderr)
}
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "2 originals on #90 change .github/workflows/ or wait on one that does") {
t.Errorf("notify = %d, want #7 among the waits:\n%s", code, stdout)
}
})
t.Run("a conflict and one not on the line", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
f.lines()
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
f.pr(9, "fix(app): nine", "", map[string]string{"app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(10, "fix(app): ten", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/10 fix(app): ten (it waits on #9, which is not on the line either)") {
t.Errorf("check = %d, want #10 to fail it, waiting on #9:\n%s\n%s", code, stdout, stderr)
}
})
t.Run("a prerequisite on the open batch", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
gh := &fakeGH{}
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"picked":[5,7]`) {
t.Fatalf("run = %d, want #5 and #7 picked:\n%s\n%s", code, stdout, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") || strings.Contains(stdout+stderr, "waits on") {
t.Errorf("check --cut = %d, want #5 and #7 only on the batch and no wait:\n%s\n%s", code, stdout, stderr)
}
})
t.Run("a direct reason", func(t *testing.T) {
f := featWorld(t)
gh := &fakeGH{}
openBatch(t, f, gh)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "needs the tap", "--push"); code != 0 {
t.Fatalf("run --defer = %d\n%s", code, stderr)
}
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "parked") {
t.Errorf("check = %d, want #5 deferred for its reason, not parked:\n%s\n%s", code, stdout, stderr)
}
})
t.Run("a prerequisite rule 6 holds", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
f.onLine("release-1.1", "fix(app): seven (#7)\n\n(cherry picked from commit "+mc7+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.0"); code != 0 || !strings.Contains(stderr, "note: #7 waits on #5, which is not on release-1.1 yet") {
t.Errorf("check of release-1.0 = %d, want a note that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
}
})
t.Run("a chain", func(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(4, "fix(app): four", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!!", 1)})
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/7 fix(app): seven (it waits on #5, which is not on the line either)") {
t.Errorf("check = %d, want #7 failing it through #5:\n%s\n%s", code, stdout, stderr)
}
})
}
// run's report, check and notify give an original one disposition: a
// workflow original whose only missing dependency is a test waits for a
// maintainer, and one deferred for a reason stays deferred, though it
// builds on what never comes; neither is parked.
func TestDispositionOrderIsSharedByRunCheckNotify(t *testing.T) {
f := auxWorld(t)
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
f.pr(7, "fix(app): fix the tap", "", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "waits for the tap", "--push")
if code != 0 {
t.Fatalf("run --defer = %d\n%s", code, stderr)
}
if strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) || !strings.Contains(stdout, `{"original":7,"files":[],"reason":"waits for the tap"}`) {
t.Errorf("the report does not have #5 waiting and #7 deferred, unparked:\n%s", stdout)
}
reopen(f, gh)
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || strings.Contains(stderr, "parked") || !strings.Contains(stderr, "warning: waiting on #90 for a maintainer, since it changes .github/workflows/: https://github.com/cozystack/cozystack/pull/5 ") || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/7 ") {
t.Errorf("check = %d, want #5 waiting and #7 deferred:\n%s\n%s", code, stdout, stderr)
}
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, "1 originals on #90 change .github/workflows/") || strings.Contains(stdout, "parked") || strings.Contains(stdout, "conflicts") {
t.Errorf("notify = %d, want #5 waiting and nothing for #7:\n%s", code, stdout)
}
}
// A never and a pending prerequisite together park.
func TestParkWinsOverWait(t *testing.T) {
f, _ := chainWorld(t)
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
f.pr(8, "fix(app): eight", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!!", 1), "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[20]`) || strings.Contains(stdout, `"original":8,"files":[],"needs"`) {
t.Errorf("run = %d, want #8 parked on #20, not waiting on #7:\n%s\n%s", code, stdout, stderr)
}
}
// A prerequisite picked earlier in the same batch satisfies what builds on
// it, and so does one whose pick came out empty because an earlier pick
// already carried it: #4280 and #3938.
func TestDependencySatisfiedEarlierInTheBatch(t *testing.T) {
f := newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(3, "fix(kafka): delete hooks", "", map[string]string{"app/hooks.yaml": appFile})
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[3,4]`) {
t.Errorf("run = %d, want #3 and #4 picked:\n%s\n%s", code, stdout, stderr)
}
// #30 adds back the file #2 brought and a feat took away, so its pick
// comes out empty after #2's, and #4 builds on #30.
f = newFixture(t)
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
f.lines()
f.pr(20, "fix(kafka): delete hooks, and two", "", map[string]string{"app/hooks.yaml": appFile, "app/two.txt": "2\n"})
f.pr(21, "feat(kafka): no delete hooks", "", map[string]string{"app/hooks.yaml": ""})
f.pr(30, "fix(kafka): delete hooks again", "", map[string]string{"app/hooks.yaml": appFile})
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[4,20]`) || strings.Contains(stdout, `"needs"`) {
t.Errorf("run with #30's pick empty = %d, want #4 picked:\n%s\n%s", code, stdout, stderr)
}
}
+254
View File
@@ -0,0 +1,254 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"slices"
"strings"
"testing"
)
// workflowWorld has #5 change a workflow and #6 change the app and a file
// under .github/ that is no workflow.
func workflowWorld(t *testing.T) *fixture {
t.Helper()
f := newFixture(t)
f.lines()
f.pr(5, "fix(ci): five", "", map[string]string{".github/workflows/five.yaml": "on: push\n", "app/five.txt": "5\n"})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n", ".github/renovate.json": "{}\n"})
return f
}
// With --no-workflow-files, as the scheduled batch runs, an original that
// changes a workflow is left to a maintainer, with its workflow files, and is
// neither a conflict nor deferred with a reason. Without the flag it is picked
// like any other.
func TestRunLeavesWorkflowChangesToAMaintainer(t *testing.T) {
l, _ := parseLine("release-1.1")
f := workflowWorld(t)
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
if code != 0 {
t.Fatalf("run --no-workflow-files = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("picked %q, want only #6", got)
}
deferred, err := parseDeferred(stdout, l)
if e := deferred[5]; err != nil || len(deferred) != 1 || !e.Workflows || e.Reason != "" || e.Commit != "" || !slices.Equal(e.Files, []string{".github/workflows/five.yaml"}) {
t.Errorf("deferred = %+v, %v; want #5 waiting with its workflow file", deferred, err)
}
for _, want := range []string{"### Waiting for a maintainer", "[#5](", "`.github/workflows/five.yaml`", "#changes-to-workflows"} {
if !strings.Contains(stdout, want) {
t.Errorf("the body lacks %q:\n%s", want, stdout)
}
}
if strings.Contains(stdout, "### Deferred") {
t.Errorf("the body lists #5 as deferred:\n%s", stdout)
}
if !strings.Contains(stderr, "waiting for a maintainer: https://github.com/cozystack/cozystack/pull/5 fix(ci): five changes .github/workflows/five.yaml") {
t.Errorf("the run does not say #5 waits:\n%s", stderr)
}
f = workflowWorld(t)
code, stdout, stderr = f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(ci): five (#5)", "fix(app): six (#6)"}) {
t.Errorf("without the flag picked %q, want #5 and #6", got)
}
if strings.Contains(stdout, "Waiting for a maintainer") {
t.Errorf("without the flag the body has something wait:\n%s", stdout)
}
}
// Waiting is no way off the line: check lists the original until it is on
// it, the scheduled run that continues the batch keeps it waiting rather than
// trying it as a conflict, notify lists it apart from the conflicts, and a
// maintainer's own run picks it onto the open batch.
func TestWorkflowChangesWaitUntilOnTheLine(t *testing.T) {
f := workflowWorld(t)
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check with no batch open = %d, %q; want #5 missing", code, stdout)
}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
if code != 0 || !strings.Contains(stderr, "warning: waiting on #90 for a maintainer, since it changes .github/workflows/: https://github.com/cozystack/cozystack/pull/5 ") {
t.Errorf("check = %d, %q; want 0 and a warning about #5:\n%s", code, stdout, stderr)
}
code, stdout, _ = f.exec(gh, "check", "--line", "release-1.1", "--cut")
if code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
t.Errorf("check --cut = %d, %q; want #5 to stop the cut", code, stdout)
}
code, stdout, stderr = f.exec(gh, "notify", "--line", "release-1.1", "--push")
if code != 0 || !strings.Contains(stdout, "1 originals on #90 change .github/workflows/ and wait for a maintainer") || strings.Contains(stdout, "deferred conflicts") {
t.Errorf("notify = %d, %q; want #5 for a maintainer and no conflict\n%s", code, stdout, stderr)
}
notes := slices.DeleteFunc(slices.Clone(gh.issueComments), func(c issueComment) bool { return !strings.HasPrefix(c.Body, notifyMarker) })
if len(notes) != 1 || !strings.Contains(notes[0].Body, "[#5](") || !strings.Contains(notes[0].Body, "#changes-to-workflows") || strings.Contains(notes[0].Body, "These conflicts") {
t.Errorf("the comment does not list #5 for a maintainer: %+v", gh.issueComments)
}
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
if code != 0 {
t.Fatalf("run --continue = %d\n%s", code, stderr)
}
l, _ := parseLine("release-1.1")
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || len(deferred) != 1 {
t.Errorf("the continued body has %+v, %v; want #5 still waiting", deferred, err)
}
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): six (#6)", "fix(app): seven (#7)"}) {
t.Errorf("the continued batch carries %q", got)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 {
t.Fatalf("a maintainer's run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): six (#6)", "fix(app): seven (#7)", "fix(ci): five (#5)"}) {
t.Errorf("after a maintainer's run the batch carries %q, want #5 on top", got)
}
f.git("switch", "-q", "main")
reopen(f, gh)
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || strings.Contains(stderr, "waiting") {
t.Errorf("check after the maintainer's run = %d, %q\n%s", code, stdout, stderr)
}
}
// --defer keeps what waits as it is, and takes one out like any other original
// when a maintainer gives the reason.
func TestDeferKeepsWorkflowChangesWaiting(t *testing.T) {
f := workflowWorld(t)
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
l, _ := parseLine("release-1.1")
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "fails the app e2e", "--push")
if code != 0 {
t.Fatalf("run --defer 7 = %d\n%s", code, stderr)
}
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || deferred[7].Reason != "fails the app e2e" || len(deferred) != 2 {
t.Errorf("after --defer 7 the body has %+v, %v; want #5 waiting and #7 deferred", deferred, err)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "waits for the 1.1.4 workflow rework", "--push")
if code != 0 {
t.Fatalf("run --defer 5 = %d\n%s", code, stderr)
}
if deferred, err := parseDeferred(stdout, l); err != nil || deferred[5].Workflows || deferred[5].Reason == "" {
t.Errorf("after --defer 5 the body has %+v, %v; want #5 deferred with the reason", deferred, err)
}
f.git("switch", "-q", "main")
reopen(f, gh)
// #6 and #7 are still only on the batch, which fails the cut; #5 does not.
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || strings.Contains(stdout, "#5 changes") || !strings.Contains(stdout, "#6 is only on open batch PR #90") {
t.Errorf("check --cut = %d, %q; want 1 for #6 and #7 alone, a deferred #5 no longer waiting", code, stdout)
}
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") {
t.Errorf("check = %d, want 0 with a warning about #5:\n%s", code, stderr)
}
}
// A record that has an original wait and also names a failed commit or a
// reason is refused: it is one or the other.
func TestParseDeferredRefusesAMixedEntry(t *testing.T) {
l, _ := parseLine("release-1.1")
for _, entry := range []string{
`{"original":5,"files":[],"workflows":true,"reason":"x"}`,
`{"original":5,"files":[],"workflows":true,"commit":"` + strings.Repeat("a", 40) + `"}`,
} {
body := deferredFence + "\n" + `{"line":"release-1.1","deferred":[` + entry + `]}` + "\n```\n"
if _, err := parseDeferred(body, l); err == nil {
t.Errorf("parseDeferred accepted %s", entry)
}
}
}
// A pull request that changes a workflow in one commit and takes it back in a
// later one changes no workflow in its merge, but its first pick does, and
// the push carries that pick: it waits as well.
func TestRunHoldsAWorkflowChangeTakenBack(t *testing.T) {
f := newFixture(t)
f.lines()
f.prCommits(5, "fix(ci): five", "",
change{files: map[string]string{".github/workflows/five.yaml": "on: push\n"}},
change{files: map[string]string{".github/workflows/five.yaml": "", "app/five.txt": "5\n"}})
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("picked %q, want only #6", got)
}
l, _ := parseLine("release-1.1")
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || !slices.Equal(deferred[5].Files, []string{".github/workflows/five.yaml"}) {
t.Errorf("deferred = %+v, %v; want #5 waiting on its workflow file", deferred, err)
}
}
// A rebuild keeps the count of workflow files a waiting original changes
// beyond those the block names.
func TestDeferKeepsTheWaitingFileCount(t *testing.T) {
f := newFixture(t)
f.lines()
files := map[string]string{"app/five.txt": "5\n"}
for i := range filesKept + 2 {
files[fmt.Sprintf(".github/workflows/w%02d.yaml", i)] = "on: push\n"
}
f.pr(5, "fix(ci): five", "", files)
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
reopen(f, gh)
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "fails the app e2e", "--push")
if code != 0 {
t.Fatalf("run --defer = %d\n%s", code, stderr)
}
l, _ := parseLine("release-1.1")
deferred, err := parseDeferred(stdout, l)
if e := deferred[5]; err != nil || !e.Workflows || len(e.Files) != filesKept || e.More != 2 {
t.Errorf("after the rebuild #5 is %d files and %d more, %v; want %d and 2", len(e.Files), e.More, err, filesKept)
}
if want := fmt.Sprintf("%d more", filesKept+2-filesListed); !strings.Contains(stdout, want) {
t.Errorf("the rebuilt body does not say %q:\n%s", want, stdout)
}
}
+1
View File
@@ -243,6 +243,7 @@ func main() {
Namespace: os.Getenv("BACKUP_CREDENTIALS_HELMRELEASE_NAMESPACE"),
Name: os.Getenv("BACKUP_CREDENTIALS_HELMRELEASE_NAME"),
},
CredentialsCOSISecretName: os.Getenv("BACKUP_CREDENTIALS_COSI_SECRET_NAME"),
}
if err := gate.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to add DefaultObjectsGate runnable")
+40 -15
View File
@@ -22,6 +22,7 @@ package main
import (
"fmt"
"io"
"os"
"os/exec"
"strconv"
@@ -58,6 +59,10 @@ Options:
--no-color Disable color output (auto-disabled on non-TTY)
-h, --help Show this help
Exit status: 0 when everything is ready, 1 when something is not ready or a
write to stdout fails, 2 on invalid arguments. A closed pipe ends the process
with SIGPIPE, like other Unix tools.
`
// entry is a resource catalog item.
@@ -181,10 +186,16 @@ func run(args []string) int {
cfg.coreOnly = true
i++
case "-h", "--help":
fmt.Fprint(os.Stdout, usageText)
if _, err := fmt.Fprint(os.Stdout, usageText); err != nil {
fmt.Fprintf(os.Stderr, "writing usage: %v\n", err)
return 1
}
return 0
case "--version":
fmt.Fprintln(os.Stdout, Version)
if _, err := fmt.Fprintln(os.Stdout, Version); err != nil {
fmt.Fprintf(os.Stderr, "writing version: %v\n", err)
return 1
}
return 0
default:
fmt.Fprintf(os.Stderr, "Unknown argument: %s\n", a)
@@ -213,14 +224,14 @@ func run(args []string) int {
cfg.timeoutS = sec
if cfg.waitMode {
return cfg.runWait()
return cfg.runWait(os.Stdout)
}
if cfg.watch {
cfg.runWatch()
return 0
return cfg.runWatch(os.Stdout)
}
if writeOutput(os.Stdout, cfg.runOnce()) != nil {
return 1
}
out := cfg.runOnce()
fmt.Print(out)
if cfg.foundIssues {
return 1
}
@@ -281,18 +292,31 @@ func parseTimeout(raw string) (int, bool) {
return 0, false
}
func (cfg *config) runWait() int {
// writeOutput prints a result. A result its reader never got fails the run,
// whatever the resources' state, since the exit code alone would vouch for it.
func writeOutput(w io.Writer, parts ...string) error {
for _, part := range parts {
if _, err := io.WriteString(w, part); err != nil {
fmt.Fprintf(os.Stderr, "writing output: %v\n", err)
return err
}
}
return nil
}
func (cfg *config) runWait(w io.Writer) int {
start := time.Now()
for {
output := cfg.runOnce()
elapsed := int(time.Since(start).Seconds())
if strings.Contains(output, "All resources are ready.") {
fmt.Print(output)
fmt.Printf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)
if writeOutput(w, output, fmt.Sprintf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)) != nil {
return 1
}
return 0
}
if elapsed >= cfg.timeoutS {
fmt.Print(output)
_ = writeOutput(w, output)
fmt.Fprintf(os.Stderr, "%s%sTimeout after %ds — resources still not ready.%s\n", cfg.red, cfg.bold, cfg.timeoutS, cfg.reset)
return 1
}
@@ -300,13 +324,14 @@ func (cfg *config) runWait() int {
}
}
func (cfg *config) runWatch() {
func (cfg *config) runWatch(w io.Writer) int {
for {
output := cfg.runOnce()
// Smooth clear is cosmetic and untested; just reprint.
fmt.Printf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
fmt.Println()
fmt.Print(output)
header := fmt.Sprintf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
if writeOutput(w, header, output) != nil {
return 1
}
time.Sleep(time.Duration(cfg.interval) * time.Second)
}
}
+68
View File
@@ -0,0 +1,68 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"io"
"os"
"strings"
"testing"
)
func TestHelpAndVersionSucceedOnWritableStdout(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = r.Close() })
stdout := os.Stdout
os.Stdout = w
t.Cleanup(func() { os.Stdout = stdout })
read := make(chan string, 1)
go func() {
b, _ := io.ReadAll(r)
read <- string(b)
}()
for _, arg := range []string{"--help", "--version"} {
if code := run([]string{arg}); code != 0 {
t.Errorf("run(%s) = %d, want 0", arg, code)
}
}
_ = w.Close()
if out := <-read; !strings.Contains(out, "Usage:") || !strings.Contains(out, Version) {
t.Errorf("stdout = %q, want the usage text and the version", out)
}
}
func TestHelpAndVersionFailOnUnwritableStdout(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_ = r.Close()
_ = w.Close()
stdout := os.Stdout
os.Stdout = w
t.Cleanup(func() { os.Stdout = stdout })
for _, arg := range []string{"--help", "--version"} {
if code := run([]string{arg}); code == 0 {
t.Errorf("run(%s) with an unwritable stdout = 0, want a failure", arg)
}
}
}
+1 -1
View File
@@ -390,7 +390,7 @@ func createPackageFromFile(ctx context.Context, k8sClient client.Client, filePat
}
}
return fmt.Errorf("Package %s not found in file", packageName)
return fmt.Errorf("no Package %s in file", packageName)
}
func installPackage(ctx context.Context, k8sClient client.Client, packageSourceName string) error {
+27 -21
View File
@@ -19,7 +19,7 @@ package cmd
import (
"context"
"fmt"
"os"
"io"
"strings"
"text/tabwriter"
@@ -78,24 +78,25 @@ Use --components flag to show components on separate lines.`,
}
if listCmdFlags.installed {
return listPackages(ctx, k8sClient, listCmdFlags.components)
return listPackages(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
}
return listPackageSources(ctx, k8sClient, listCmdFlags.components)
return listPackageSources(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
},
}
func listPackageSources(ctx context.Context, k8sClient client.Client, showComponents bool) error {
func listPackageSources(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
var psList cozyv1alpha1.PackageSourceList
if err := k8sClient.List(ctx, &psList); err != nil {
return fmt.Errorf("failed to list PackageSources: %w", err)
}
// Use tabwriter for better column alignment
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
// Print header
fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS")
if _, err := fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS"); err != nil {
return err
}
// Print rows
for _, ps := range psList.Items {
@@ -123,24 +124,26 @@ func listPackageSources(ctx context.Context, k8sClient client.Client, showCompon
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status)
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status); err != nil {
return err
}
// Show components if requested
if showComponents {
for _, variant := range ps.Spec.Variants {
for _, component := range variant.Components {
fmt.Fprintf(w, " %s\t%s\t\t\n",
fmt.Sprintf("%s.%s", ps.Name, component.Name),
variant.Name)
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", ps.Name, component.Name, variant.Name); err != nil {
return err
}
}
}
}
}
return nil
return w.Flush()
}
func listPackages(ctx context.Context, k8sClient client.Client, showComponents bool) error {
func listPackages(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
var pkgList cozyv1alpha1.PackageList
if err := k8sClient.List(ctx, &pkgList); err != nil {
return fmt.Errorf("failed to list Packages: %w", err)
@@ -160,11 +163,12 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
}
// Use tabwriter for better column alignment
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
// Print header
fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS")
if _, err := fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS"); err != nil {
return err
}
// Print rows
for _, pkg := range pkgList.Items {
@@ -187,7 +191,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status)
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status); err != nil {
return err
}
// Show components if requested
if showComponents {
@@ -197,9 +203,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
for _, v := range ps.Spec.Variants {
if v.Name == variant {
for _, component := range v.Components {
fmt.Fprintf(w, " %s\t%s\t\t\n",
fmt.Sprintf("%s.%s", pkg.Name, component.Name),
variant)
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", pkg.Name, component.Name, variant); err != nil {
return err
}
}
break
}
@@ -208,7 +214,7 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
}
}
return nil
return w.Flush()
}
func init() {
+185
View File
@@ -0,0 +1,185 @@
/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type failingWriter struct{}
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
func TestPrintReportReturnsWriteError(t *testing.T) {
r := &Report{Findings: []Finding{{Severity: SeverityError, Code: "X", Message: "m"}}}
if err := printReport(r, failingWriter{}); err == nil {
t.Fatal("printReport to an unwritable writer returned nil")
}
}
// The table is buffered until the tabwriter flushes, so a failing reader shows
// up only as a Flush error, which list has to return.
func TestListReturnsWriteError(t *testing.T) {
s := runtime.NewScheme()
utilruntime.Must(cozyv1alpha1.AddToScheme(s))
ps := &cozyv1alpha1.PackageSource{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
pkg := &cozyv1alpha1.Package{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(ps, pkg).Build()
for name, list := range map[string]func(io.Writer) error{
"sources": func(w io.Writer) error { return listPackageSources(context.Background(), cl, w, true) },
"packages": func(w io.Writer) error { return listPackages(context.Background(), cl, w, true) },
} {
t.Run(name, func(t *testing.T) {
if err := list(failingWriter{}); err == nil {
t.Fatal("list to an unwritable writer returned nil")
}
var out strings.Builder
if err := list(&out); err != nil || !strings.Contains(out.String(), "acme.app") {
t.Fatalf("list = %v, output %q; want the row", err, out.String())
}
})
}
}
// A command that cannot print its validation report still fails, and a
// refusal keeps validation as its reason with the write error joined to it.
func TestValidationCommandsReportUnwritableReport(t *testing.T) {
fakeFlux := t.TempDir()
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
emptyRepo := func(t *testing.T) string {
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "packages"), 0o755); err != nil {
t.Fatal(err)
}
return root
}
run := func(c *cobra.Command, args ...string) error {
c.SetOut(failingWriter{})
t.Cleanup(func() { c.SetOut(nil) })
return c.RunE(c, args)
}
push := func(root string) error {
pushCmdFlags.path = root
t.Cleanup(func() { pushCmdFlags.path = "." })
return run(pushCmd, "oci://registry.example/acme/apps:v1")
}
for _, tc := range []struct {
name string
run func(t *testing.T) error
refusal string
}{
{name: "validate, valid", run: func(t *testing.T) error { return run(validateCmd, validRepo(t)) }},
{name: "validate, invalid", run: func(t *testing.T) error { return run(validateCmd, emptyRepo(t)) }, refusal: "validation failed"},
{name: "push, valid", run: func(t *testing.T) error { return push(validRepo(t)) }, refusal: "nothing was pushed"},
{name: "push, invalid", run: func(t *testing.T) error { return push(emptyRepo(t)) }, refusal: "refusing to push"},
{name: "tap, invalid", run: func(t *testing.T) error { return run(tapCmd, "oci://registry.example/acme/apps:v1") }, refusal: "refusing to tap"},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.run(t)
if err == nil || !strings.Contains(err.Error(), "broken pipe") {
t.Fatalf("err = %v, want the write error", err)
}
if tc.refusal != "" && !strings.Contains(err.Error(), tc.refusal) {
t.Errorf("err = %v, want the refusal %q kept", err, tc.refusal)
}
})
}
}
// A tap that applied is reported as applied even when its summary is lost,
// and the lost summary still fails the command.
func TestTapSummaryReportsUnwritableOutput(t *testing.T) {
err := writeTapSummary(failingWriter{}, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"})
if err == nil || !strings.Contains(err.Error(), "tap applied") || !strings.Contains(err.Error(), "broken pipe") {
t.Fatalf("err = %v, want the write error with the tap reported as applied", err)
}
var out strings.Builder
if err := writeTapSummary(&out, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"}); err != nil || !strings.Contains(out.String(), "cozypkg add acme.app") {
t.Fatalf("writeTapSummary = %v, output %q", err, out.String())
}
}
// A clean report has no finding lines, so its summary is its only write and
// alone decides the result.
func TestPrintReportReturnsSummaryWriteError(t *testing.T) {
if err := printReport(&Report{}, failingWriter{}); err == nil {
t.Fatal("printReport of a clean report to an unwritable writer returned nil")
}
}
// push runs flux only after its status line was written, with validation
// skipped and after a report that was written.
func TestPushStopsBeforeFluxWhenItsStatusIsLost(t *testing.T) {
fakeFlux := t.TempDir()
ran := filepath.Join(t.TempDir(), "flux-ran")
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\ntouch "+ran+"\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
for _, skip := range []bool{true, false} {
t.Run(fmt.Sprintf("skip-validate=%v", skip), func(t *testing.T) {
pushCmdFlags.path, pushCmdFlags.skipValidate = validRepo(t), skip
pushCmdFlags.source, pushCmdFlags.revision = "https://example.com/acme.git", "v1"
t.Cleanup(func() {
pushCmdFlags.path, pushCmdFlags.skipValidate = ".", false
pushCmdFlags.source, pushCmdFlags.revision = "", ""
})
pushCmd.SetOut(&failOn{want: "Pushing"})
t.Cleanup(func() { pushCmd.SetOut(nil) })
err := pushCmd.RunE(pushCmd, []string{"oci://registry.example/acme/apps:v1"})
if err == nil || !strings.Contains(err.Error(), "nothing was pushed") {
t.Fatalf("push losing its status = %v, want nothing pushed", err)
}
if _, statErr := os.Stat(ran); statErr == nil {
t.Fatal("flux ran after the status line was lost")
}
})
}
}
// failOn fails every write that contains want, and takes every other one.
type failOn struct {
want string
buf strings.Builder
}
func (w *failOn) Write(p []byte) (int, error) {
if strings.Contains(string(p), w.want) {
return 0, errors.New("broken pipe")
}
return w.buf.Write(p)
}
+9 -3
View File
@@ -17,6 +17,7 @@ limitations under the License.
package cmd
import (
"errors"
"fmt"
"os"
"os/exec"
@@ -106,10 +107,13 @@ published. Source URL and revision are derived from git when not given.`,
if err != nil {
return err
}
printReport(rep, cmd.OutOrStdout())
printErr := printReport(rep, cmd.OutOrStdout())
if rep.HasErrors() {
errs, _, _ := rep.Counts()
return fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs)
return errors.Join(fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs), printErr)
}
if printErr != nil {
return fmt.Errorf("nothing was pushed: writing the validation report failed: %w", printErr)
}
}
@@ -129,7 +133,9 @@ published. Source URL and revision are derived from git when not given.`,
}
fluxArgs := buildFluxPushArgs(ref, packagesDir, source, revision, pushCmdFlags.reproducible)
fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision)
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision); err != nil {
return fmt.Errorf("nothing was pushed: writing the push status failed: %w", err)
}
flux := exec.Command("flux", fluxArgs...)
flux.Stdout = cmd.OutOrStdout()
flux.Stderr = cmd.ErrOrStderr()
+22 -12
View File
@@ -18,6 +18,7 @@ package cmd
import (
"context"
"errors"
"fmt"
"io"
"regexp"
@@ -402,9 +403,8 @@ runs its charts in your management cluster: tap only sources you trust.`,
return err
}
if rep.HasErrors() {
printReport(rep, cmd.OutOrStdout())
errs, _, _ := rep.Counts()
return fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs)
return errors.Join(fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs), printReport(rep, cmd.OutOrStdout()))
}
}
@@ -487,19 +487,29 @@ runs its charts in your management cluster: tap only sources you trust.`,
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not reset materialization state on OCIRepository/%s: %v\n", srcName, err)
}
fmt.Fprintf(cmd.OutOrStdout(), "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
for _, n := range names {
fmt.Fprintf(cmd.OutOrStdout(), " PackageSource/%s\n", n)
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Tapping installs nothing. Install an app with:\n")
for _, n := range names {
fmt.Fprintf(cmd.OutOrStdout(), " cozypkg add %s\n", n)
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
return nil
return writeTapSummary(cmd.OutOrStdout(), fullRef, srcName, names)
},
}
// writeTapSummary reports what a tap created. The tap is applied by then, so a
// summary that cannot be written fails the command while saying so.
func writeTapSummary(w io.Writer, fullRef, srcName string, names []string) error {
var b strings.Builder
fmt.Fprintf(&b, "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
for _, n := range names {
fmt.Fprintf(&b, " PackageSource/%s\n", n)
}
b.WriteString("Tapping installs nothing. Install an app with:\n")
for _, n := range names {
fmt.Fprintf(&b, " cozypkg add %s\n", n)
}
b.WriteString("A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
if _, err := io.WriteString(w, b.String()); err != nil {
return fmt.Errorf("tap applied, but writing its summary failed: %w", err)
}
return nil
}
var untapCmd = &cobra.Command{
Use: "untap <packagesource-name>",
Short: "Remove a tapped repository",
+12 -5
View File
@@ -21,6 +21,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
@@ -535,16 +536,19 @@ func pullOCIArtifact(ref string) (string, func(), error) {
return dir, cleanup, nil
}
func printReport(r *Report, w io.Writer) {
func printReport(r *Report, w io.Writer) error {
for _, f := range r.Findings {
loc := f.Location
if loc == "" {
loc = "-"
}
fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message)
if _, err := fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message); err != nil {
return err
}
}
errs, warns, infos := r.Counts()
fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
_, err := fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
return err
}
var validateCmdFlags struct {
@@ -663,10 +667,13 @@ runs "helm lint" on every component chart.`,
if err != nil {
return err
}
printReport(rep, cmd.OutOrStdout())
printErr := printReport(rep, cmd.OutOrStdout())
if rep.HasErrors() {
errs, _, _ := rep.Counts()
return fmt.Errorf("validation failed: %d error(s)", errs)
return errors.Join(fmt.Errorf("validation failed: %d error(s)", errs), printErr)
}
if printErr != nil {
return fmt.Errorf("writing the validation report failed: %w", printErr)
}
return nil
},
+37 -8
View File
@@ -99,6 +99,7 @@ func main() {
var platformSourceName string
var platformSourceRef string
var platformSourceSecret string
var platformSourceCertSecret string
var systemNamespaceMemoryLimit string
var systemNamespaceMemoryRequest string
@@ -145,6 +146,7 @@ func main() {
flag.StringVar(&platformSourceName, "platform-source-name", "cozystack-platform", "Name for the generated platform source resource and PackageSource")
flag.StringVar(&platformSourceRef, "platform-source-ref", "", "Reference specification as key=value pairs (e.g., 'branch=main' or 'digest=sha256:...,tag=v1.0'). For OCI: digest, semver, semverFilter, tag. For Git: branch, tag, semver, name, commit.")
flag.StringVar(&platformSourceSecret, "platform-source-secret", "", "Name of the Secret in cozy-system namespace containing credentials for the platform source. Sets spec.secretRef on the generated OCIRepository or GitRepository. Secret type depends on the source kind: kubernetes.io/dockerconfigjson for OCI (oci://...); Opaque with username+password (or bearerToken) for Git over HTTPS; Opaque with identity (PEM private key) and known_hosts for Git over SSH.")
flag.StringVar(&platformSourceCertSecret, "platform-source-cert-secret", "", "Name of the Secret in cozy-system namespace holding TLS material for an OCI platform source: ca.crt for a registry behind a private CA, and/or tls.crt+tls.key for client certificate auth. Sets spec.certSecretRef on the generated OCIRepository. Not supported for Git sources, where Flux reads ca.crt from the --platform-source-secret Secret.")
flag.StringVar(&cozyValuesSecretName, "cozy-values-secret-name", "cozystack-values", "The name of the secret containing cluster-wide configuration values.")
flag.StringVar(&cozyValuesSecretNamespace, "cozy-values-secret-namespace", "cozy-system", "The namespace of the secret containing cluster-wide configuration values.")
flag.StringVar(&cozyValuesNamespaceSelector, "cozy-values-namespace-selector", "cozystack.io/system=true", "The label selector for namespaces where the cluster-wide configuration values must be replicated.")
@@ -152,10 +154,10 @@ func main() {
"Default container memory limit in eligible system namespaces, including kube-system. "+
"Lowering or an oversized request creates a durable hold requiring an administrator's "+
"exact-value acknowledgement. Empty or 0 disables and removes managed LimitRanges; "+
"safety state is retained. See docs/operations/system-memory-limits.md.")
"safety state is retained. See https://cozystack.io/docs/v1/operations/configuration/system-memory-limits/.")
flag.StringVar(&systemNamespaceMemoryRequest, "system-namespace-memory-request", DefaultSystemNamespaceMemoryRequest,
"Default memory request paired with the system namespace limit. Keep small; 0 is supported. "+
"An empty installer value selects the operator default. See docs/operations/system-memory-limits.md.")
"An empty installer value selects the operator default. See https://cozystack.io/docs/v1/operations/configuration/system-memory-limits/.")
opts := zap.Options{
Development: true,
@@ -189,6 +191,11 @@ func main() {
os.Exit(1)
}
if err := validatePlatformSourceCertSecret(platformSourceURL, platformSourceCertSecret); err != nil {
setupLog.Error(err, "invalid platform source flags")
os.Exit(1)
}
config := ctrl.GetConfigOrDie()
// Create a direct client (without cache) for pre-start operations
@@ -278,12 +285,12 @@ func main() {
// Generate and install platform source resource if specified
if platformSourceURL != "" {
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef, "secret", platformSourceSecret)
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef, "secret", platformSourceSecret, "certSecret", platformSourceCertSecret)
installCtx, installCancel := context.WithTimeout(mgrCtx, 2*time.Minute)
defer installCancel()
// Use direct client for pre-start operations (cache is not ready yet)
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef, platformSourceSecret); err != nil {
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef, platformSourceSecret, platformSourceCertSecret); err != nil {
setupLog.Error(err, "failed to install platform source resource")
os.Exit(1)
} else {
@@ -414,7 +421,7 @@ func main() {
// installPlatformSourceResource generates and installs a Flux source resource (OCIRepository or GitRepository)
// based on the platform source URL
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec, secretName string) error {
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec, secretName, certSecretName string) error {
logger := log.FromContext(ctx)
// Parse the source URL to determine type
@@ -432,11 +439,14 @@ func installPlatformSourceResource(ctx context.Context, k8sClient client.Client,
var obj client.Object
switch sourceType {
case "oci":
obj, err = generateOCIRepository(resourceName, repoURL, refMap, secretName)
obj, err = generateOCIRepository(resourceName, repoURL, refMap, secretName, certSecretName)
if err != nil {
return fmt.Errorf("failed to generate OCIRepository: %w", err)
}
case "git":
if err := validatePlatformSourceCertSecret(sourceURL, certSecretName); err != nil {
return err
}
obj, err = generateGitRepository(resourceName, repoURL, refMap, secretName)
if err != nil {
return fmt.Errorf("failed to generate GitRepository: %w", err)
@@ -532,6 +542,19 @@ func parseSystemNamespaceMemory(rawLimit, rawRequest string) (limit, request res
return limit, request, nil
}
// validatePlatformSourceCertSecret rejects --platform-source-cert-secret for a
// Git source: GitRepository has no certSecretRef, and Flux reads a Git CA from
// the ca.crt key of the --platform-source-secret Secret instead.
func validatePlatformSourceCertSecret(sourceURL, certSecretName string) error {
if certSecretName == "" {
return nil
}
if sourceType, _, err := parsePlatformSourceURL(sourceURL); err == nil && sourceType == "git" {
return fmt.Errorf("--platform-source-cert-secret is only supported for OCI sources; for a Git source put ca.crt into the --platform-source-secret Secret")
}
return nil
}
// parsePlatformSourceURL parses the source URL and returns the source type and repository URL.
// Supports formats:
// - oci://registry.example.com/repo
@@ -640,7 +663,7 @@ func validateGitRef(refMap map[string]string) error {
return fmt.Errorf("commit SHA should be at least 7 characters, got: %s", commit)
}
for _, c := range commit {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
return fmt.Errorf("commit SHA should be a hexadecimal string, got: %s", commit)
}
}
@@ -650,7 +673,7 @@ func validateGitRef(refMap map[string]string) error {
}
// generateOCIRepository creates an OCIRepository resource
func generateOCIRepository(name, repoURL string, refMap map[string]string, secretName string) (*sourcev1.OCIRepository, error) {
func generateOCIRepository(name, repoURL string, refMap map[string]string, secretName, certSecretName string) (*sourcev1.OCIRepository, error) {
if err := validateOCIRef(refMap); err != nil {
return nil, err
}
@@ -687,6 +710,12 @@ func generateOCIRepository(name, repoURL string, refMap map[string]string, secre
}
}
if certSecretName != "" {
obj.Spec.CertSecretRef = &fluxmeta.LocalObjectReference{
Name: certSecretName,
}
}
return obj, nil
}
Loaded 100 of 757 files, more files were not shown because too many files have changed in this diff. Show more