mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 20:56:02 +09:00
Merge branch 'main' into mattia/pwnescape-kvm-cozystack-talos-dec626
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
1138 files changed
+94894
-25466
No files matched your search
@@ -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:
|
||||
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
packages/system/capi-providers-infraprovider/images/cluster-api-provider-kubevirt/patches/*.diff -whitespace
|
||||
packages/system/cilium/images/cilium-operator/patches/*.diff -whitespace
|
||||
packages/system/seaweedfs/images/seaweedfs/patches/*.diff -whitespace
|
||||
packages/system/kamaji/images/kamaji/patches/*.diff -whitespace
|
||||
@@ -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
@@ -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
|
||||
+101
-1
@@ -32,6 +32,13 @@
|
||||
"matchStrings": ["clastix/kubectl:(?<currentValue>v[0-9]+\\.[0-9]+(?:\\.[0-9]+)?)@(?<currentDigest>sha256:[a-f0-9]+)"],
|
||||
"datasourceTemplate": "docker",
|
||||
"depNameTemplate": "docker.io/clastix/kubectl"
|
||||
},
|
||||
{
|
||||
"customType": "regex",
|
||||
"description": "Track the BuildKit SBOM scanner pinned by tag and digest in hack/common-envs.mk. The dockerfile manager does not read Makefiles and the helm-values manager is disabled repo-wide (see enabledManagers above), so without this manager the pin would stay on the syft it was cut with: the floating stable-1 tag it replaced moved to a fixed syft on its own, a digest pin does not. Tag and digest move together here, as the comment above the pin asks.",
|
||||
"managerFilePatterns": ["/^hack/common-envs\\.mk$/"],
|
||||
"matchStrings": ["SBOM_SCANNER \\?= (?<depName>docker\\.io/docker/buildkit-syft-scanner):(?<currentValue>[0-9]+\\.[0-9]+\\.[0-9]+)@(?<currentDigest>sha256:[a-f0-9]+)"],
|
||||
"datasourceTemplate": "docker"
|
||||
}
|
||||
],
|
||||
"packageRules": [
|
||||
@@ -54,6 +61,61 @@
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["sigs.k8s.io/controller-runtime", "k8s.io/**"]
|
||||
},
|
||||
{
|
||||
"description": "cel-go moved to cel.dev/cel-go, and the github.com/google/cel-go repository has no tags past v0.30.0. proxy.golang.org still serves v0.31.0 and v0.32.0 from its cache, but a build that resolves modules from the repository (GOPROXY=direct, or an Athens or Artifactory proxy that fetches from VCS) fails on them, and v0.32.0 declares the module path cel.dev/cel-go, which k8s.io/apiserver does not import. The rule can go once nothing in the module graph requires github.com/google/cel-go.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["github.com/google/cel-go"],
|
||||
"allowedVersions": "<0.31.0"
|
||||
},
|
||||
{
|
||||
"description": "The newest kube-openapi commits require structured-merge-diff v7, and k8s.io/apimachinery v0.37 does not compile against it (pkg/util/managedfields/internal/typeconverter.go hands v7 schema types to v6). Renovate does not raise kube-openapi; each module keeps the commit its dependencies require, and it moves with the Kubernetes libraries.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["k8s.io/kube-openapi"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "Renovate offers a dependency's patch and minor update as one update to the newest minor unless they are split, so the Flux hold below would also drop the patch releases it means to let through.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["github.com/fluxcd/**"],
|
||||
"separateMinorPatch": true
|
||||
},
|
||||
{
|
||||
"description": "structured-merge-diff v7 is a new module path that k8s.io/apimachinery v0.37 does not compile against: its managedfields package hands v7 schema types to v6 (pkg/util/managedfields/internal/typeconverter.go). It moves when the Kubernetes libraries do.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["sigs.k8s.io/structured-merge-diff/**"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "The Flux API modules describe the CRDs of the Flux release the platform installs (internal/fluxinstall), so they stay on that release line; patches still arrive. A newer minor would let the operator and the apps API write fields the installed controllers do not know. They move together with the Flux release.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["github.com/fluxcd/**"],
|
||||
"matchUpdateTypes": ["major", "minor"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "controller-runtime's admission.Patched takes gomodules.xyz/jsonpatch/v2 operations, so the flux-shard-operator webhook cannot build its patch with v3 types. The major moves when controller-runtime does.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["gomodules.xyz/jsonpatch/**"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "jwx v4 removes jwk.Cache, on which the token proxy's JWKS refresh and readiness are built, so the major needs that code rewritten rather than an import path bump.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchFileNames": ["packages/system/dashboard/images/token-proxy/**"],
|
||||
"matchPackageNames": ["github.com/lestrrat-go/jwx/**"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "Upstream kubevirt csi-driver builds against kubevirt.io/api v1.2 and CDI API v1.59; with a newer CDI API its controller no longer compiles (it passes ResourceRequirements where the newer DataVolume type wants VolumeResourceRequirements). Both follow upstream csi-driver.",
|
||||
"matchManagers": ["gomod"],
|
||||
"matchFileNames": ["packages/apps/kubernetes/images/kubevirt-csi-driver/**"],
|
||||
"matchPackageNames": ["kubevirt.io/api", "kubevirt.io/containerized-data-importer-api"],
|
||||
"matchUpdateTypes": ["major", "minor"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "Never bump the Go toolchain directive. go.mod is generated and the toolchain is pinned deliberately; raising it needs a matching setup-go and a codegen run, which is out of scope for an automated dependency bump.",
|
||||
"matchManagers": ["gomod"],
|
||||
@@ -73,7 +135,7 @@
|
||||
"schedule": ["before 6am on monday"]
|
||||
},
|
||||
{
|
||||
"description": "The Harbor rebuilds reproduce one upstream release, so their build bases follow upstream's Makefile at that tag (GOBUILDIMAGE, NODEBUILDIMAGE) rather than the newest image. A golang tag bump builds Harbor with a toolchain upstream never shipped it with, and a node major breaks the Angular portal build. Tag bumps are disabled here; digest refreshes of the same tag still arrive through the digest group above. Move the tags by hand with HARBOR_VERSION.",
|
||||
"description": "The Harbor rebuilds reproduce one upstream release, and their Dockerfiles assert that the Go and node versions they build with equal GOBUILDIMAGE and NODEBUILDIMAGE in upstream's Makefile at that tag, so a tag that moves either version fails the build. Digest refreshes of the same tag still arrive through the digest group above; move the tags with HARBOR_VERSION.",
|
||||
"matchManagers": ["dockerfile"],
|
||||
"matchFileNames": ["packages/system/harbor/images/**"],
|
||||
"matchUpdateTypes": ["major", "minor", "patch"],
|
||||
@@ -111,6 +173,44 @@
|
||||
"matchManagers": ["gomod"],
|
||||
"matchPackageNames": ["kubevirt.io/csi-driver"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "The flux action's tag has to equal the Flux CLI version it installs, which follows the Flux release the platform runs: hack/verify-promoted-packages.sh restates that CLI's excludeOCI set, and hack/ci-tool-installs-contract.bats fails when the two differ. The action moves together with the Flux release.",
|
||||
"matchManagers": ["github-actions"],
|
||||
"matchPackageNames": ["fluxcd/flux2"],
|
||||
"matchUpdateTypes": ["major", "minor", "patch"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "AlmaLinux 10 builds its amd64 userland for x86-64-v3 (AVX2). The kubevirt CSI node service runs mkfs, blkid, fsck and mount from this image inside tenant VMs, and on a vCPU without x86-64-v3 each of them exits at startup while the pod looks healthy; the same CPU floor broke the driver's sidecars before. The image stays on AlmaLinux 9.",
|
||||
"matchManagers": ["dockerfile"],
|
||||
"matchFileNames": ["packages/apps/kubernetes/images/kubevirt-csi-driver/**"],
|
||||
"matchPackageNames": ["almalinux", "docker.io/library/almalinux"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "Ubuntu 26.04 ships uutils coreutils, whose date costs about four times as much per call as GNU date. hack/cozytest.sh forks date for every traced line, so on 26.04 the install phase of the e2e sandbox takes about ten minutes longer and the full suite no longer fits its job budget. The sandbox stays on Ubuntu 24.04 until the harness stops forking per line.",
|
||||
"matchManagers": ["dockerfile"],
|
||||
"matchFileNames": ["packages/core/testing/images/e2e-sandbox/**"],
|
||||
"matchPackageNames": ["ubuntu"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "outline-server declares engines node 18.x, and npm ci in the shadowbox build fails with EBADENGINE on any other node major. The node tag moves when outline-server raises its engine.",
|
||||
"matchManagers": ["dockerfile"],
|
||||
"matchFileNames": ["packages/apps/vpn/images/shadowbox/**"],
|
||||
"matchPackageNames": ["node"],
|
||||
"matchUpdateTypes": ["major"],
|
||||
"enabled": false
|
||||
},
|
||||
{
|
||||
"description": "The cleanup hooks (post-delete, and kafka's pre-delete) run clastix/kubectl against the management cluster, and kubectl supports one minor of skew with the API server. The tag stays on a minor the supported management cluster versions accept; digest refreshes of that tag still arrive.",
|
||||
"matchManagers": ["custom.regex"],
|
||||
"matchPackageNames": ["docker.io/clastix/kubectl"],
|
||||
"matchUpdateTypes": ["major", "minor"],
|
||||
"enabled": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
name: API Review Gate (fork)
|
||||
|
||||
# Privileged publisher for the "API Review Gate" merge gate on FORK pull
|
||||
# requests, after the split e2e-fork.yaml uses for "E2E Tests"
|
||||
# (docs/agents/e2e-testing.md §10).
|
||||
#
|
||||
# No run of api-review-gate.yaml can post a status for a fork: the GITHUB_TOKEN
|
||||
# is read-only for a fork PR on both the `pull_request` and the
|
||||
# `pull_request_review` trigger. Nor can that run's verdict be relayed, because a
|
||||
# fork's run executes the fork's own copy of the workflow, so its conclusion is
|
||||
# whatever the fork wants it to be. This workflow runs from the DEFAULT branch
|
||||
# and reaches the verdict itself, from inputs a fork cannot forge: the detector
|
||||
# built from the default branch, reading the PR head as data, plus the base repo's
|
||||
# PR and review APIs. The fork's run is used only as a trigger.
|
||||
#
|
||||
# Fork code never executes here. The head is fetched into a worktree that only
|
||||
# the default-branch detector reads, in a job whose token can write nothing; the jobs
|
||||
# that can write statuses never see the head at all. That is why this is a
|
||||
# workflow_run and not a pull_request_target run, which would offer fork code a
|
||||
# privileged token for no gain.
|
||||
#
|
||||
# Two ways in:
|
||||
# - workflow_run, after any gate run that evaluated: a push, or a review on a
|
||||
# PR whose runs need no maintainer approval. Full evaluation.
|
||||
# - schedule, for reviews on PRs from external contributors. The repository
|
||||
# makes every run from them wait for a maintainer's approval, and a run in
|
||||
# `action_required` never fires workflow_run, so an API owner's approval
|
||||
# would otherwise not reach the status until a maintainer approved the
|
||||
# review-triggered run. The sweep never runs the detector. It moves only a
|
||||
# status that the full evaluation set after finding a sizeable change,
|
||||
# between "needs an approving review" and "approved", as the reviews on the
|
||||
# head commit now dictate. A fork PR whose push run still awaits approval
|
||||
# has no status to move and stays unmergeable, as it does for "E2E Tests".
|
||||
#
|
||||
# Same-repo PRs are published by api-review-gate.yaml's `api-gate-report`. Every
|
||||
# path here acts only on PRs whose head lives in another repository, decided
|
||||
# from the base repo's PR list: for a `pull_request_review` run on a fork PR,
|
||||
# `workflow_run.head_repository` names the BASE repository and
|
||||
# `workflow_run.pull_requests` is empty, so neither can tell a fork apart.
|
||||
#
|
||||
# A status lives on the SHA, not on a PR. A head that an open same-repo PR
|
||||
# carries is therefore left to `api-gate-report` whatever else carries it. A head
|
||||
# carried only by fork PRs fails closed: `resolve` writes the failure, and the
|
||||
# sweep turns a success already standing there into the same failure. One commit
|
||||
# status cannot hold a verdict per base, so every PR on a head shared with a
|
||||
# same-repo PR shows whichever verdict stands there, as two same-repo PRs at
|
||||
# different bases already do.
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["API Review Gate"]
|
||||
types: [completed]
|
||||
schedule:
|
||||
- cron: "*/15 * * * *"
|
||||
workflow_dispatch: {}
|
||||
|
||||
# Keyed by head SHA: a newer evaluation of the same commit reads newer reviews,
|
||||
# so it may supersede an older one, while a push gets a new SHA and leaves the
|
||||
# old commit's run alone. A cancelled or skipped gate run evaluated nothing and
|
||||
# `resolve` ignores it; in the shared group it would still cancel the live run
|
||||
# that owns the SHA after that run had posted `pending`, leaving nothing to
|
||||
# conclude it. Keyed by its own run id, it can cancel only itself.
|
||||
concurrency:
|
||||
group: >-
|
||||
api-review-gate-fork-${{
|
||||
github.event_name != 'workflow_run' && 'sweep'
|
||||
|| (github.event.workflow_run.conclusion == 'cancelled'
|
||||
|| github.event.workflow_run.conclusion == 'skipped')
|
||||
&& format('discarded-{0}', github.event.workflow_run.id)
|
||||
|| github.event.workflow_run.head_sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
resolve:
|
||||
name: Resolve fork PR
|
||||
# A `commented` review skips every job of the gate run, so it concludes
|
||||
# `skipped`; a run superseded by a newer one concludes `cancelled`. Neither is
|
||||
# a reason to re-evaluate. A fork can force either outcome by editing its copy
|
||||
# of the gate, which can only withhold a re-evaluation: a push then leaves no
|
||||
# status at all, and a withdrawn approval is caught by the sweep.
|
||||
if: >-
|
||||
${{ github.event_name == 'workflow_run'
|
||||
&& github.event.workflow_run.conclusion != 'cancelled'
|
||||
&& github.event.workflow_run.conclusion != 'skipped' }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: read
|
||||
statuses: write
|
||||
outputs:
|
||||
should_run: ${{ steps.r.outputs.should_run }}
|
||||
pr_number: ${{ steps.r.outputs.pr_number }}
|
||||
base_ref: ${{ steps.r.outputs.base_ref }}
|
||||
steps:
|
||||
- name: Resolve fork PR and claim "API Review Gate"
|
||||
id: r
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
|
||||
with:
|
||||
script: |
|
||||
const { HEAD_SHA } = process.env;
|
||||
const { owner, repo } = context.repo;
|
||||
core.setOutput('should_run', 'false');
|
||||
if (!/^[0-9a-f]{40}$/.test(HEAD_SHA)) {
|
||||
core.setFailed(`unexpected head_sha '${HEAD_SHA}'`);
|
||||
return;
|
||||
}
|
||||
const setStatus = (state, description) => github.rest.repos.createCommitStatus({
|
||||
owner, repo, sha: HEAD_SHA, state, context: 'API Review Gate', description,
|
||||
target_url: `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`,
|
||||
});
|
||||
|
||||
// The association endpoint does not index fork heads, so match the
|
||||
// open PR list on the head SHA, as e2e-fork.yaml's `resolve` does.
|
||||
// A closed or merged PR never counts.
|
||||
const openPrs = await github.paginate(
|
||||
github.rest.pulls.list,
|
||||
{ owner, repo, state: 'open', per_page: 100 },
|
||||
);
|
||||
const atHead = openPrs.filter(p => p.head && p.head.sha === HEAD_SHA);
|
||||
const isFork = p => !p.head.repo || p.head.repo.full_name !== `${owner}/${repo}`;
|
||||
// The status lives on the SHA, so a head that an open same-repo PR
|
||||
// carries is that PR's: api-gate-report publishes it, and a failure
|
||||
// written here would overwrite its verdict. A fork sees every commit
|
||||
// of this repository, so a fork PR at that head can be opened on
|
||||
// purpose, not only by accident. It inherits the verdict whatever its
|
||||
// base: one commit status cannot hold two.
|
||||
if (atHead.length === 0 || atHead.some(p => !isFork(p))) {
|
||||
core.info(`no open PR, or a same-repo PR, carries head ${HEAD_SHA} — nothing to publish`);
|
||||
return;
|
||||
}
|
||||
// Only fork PRs are left. Their bases may differ, so one status cannot
|
||||
// speak for all of them while being computed from one.
|
||||
if (atHead.length !== 1) {
|
||||
await setStatus('failure', `expected exactly one open PR for ${HEAD_SHA}, found ${atHead.length}`);
|
||||
return;
|
||||
}
|
||||
const pr = atHead[0];
|
||||
if (!/^[A-Za-z0-9._\/-]+$/.test(pr.base.ref)) {
|
||||
core.setFailed(`unexpected base ref '${pr.base.ref}'`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Claimed before the minute-long evaluation, so a dismissal cannot
|
||||
// leave a stale `success` mergeable meanwhile.
|
||||
await setStatus('pending', 're-evaluating in the privileged fork workflow');
|
||||
core.setOutput('pr_number', String(pr.number));
|
||||
core.setOutput('base_ref', pr.base.ref);
|
||||
core.setOutput('should_run', 'true');
|
||||
|
||||
# The only job that touches fork bytes, so its token can write nothing and it
|
||||
# reaches no secret. The detector is built from the default branch, never
|
||||
# from the head or the merge base.
|
||||
detect:
|
||||
name: Detect sizeable API changes
|
||||
needs: [resolve]
|
||||
if: ${{ needs.resolve.outputs.should_run == 'true' }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
outputs:
|
||||
sizeable: ${{ steps.detect.outputs.sizeable }}
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
|
||||
PR_NUMBER: ${{ needs.resolve.outputs.pr_number }}
|
||||
BASE_REF: ${{ needs.resolve.outputs.base_ref }}
|
||||
GOPROXY: "https://proxy.golang.org|direct"
|
||||
steps:
|
||||
- name: Checkout default branch
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
|
||||
# From the default branch's go.mod, and with no cache: a cache saved here
|
||||
# would be keyed in the default branch's scope.
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: false
|
||||
|
||||
- name: Materialize head and merge-base worktrees
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git fetch --no-tags origin "refs/pull/${PR_NUMBER}/head" "${BASE_REF}"
|
||||
got="$(git rev-parse FETCH_HEAD)"
|
||||
if [ "${got}" != "${HEAD_SHA}" ]; then
|
||||
echo "::error::PR head moved: refs/pull/${PR_NUMBER}/head is ${got}, resolved ${HEAD_SHA}"
|
||||
exit 1
|
||||
fi
|
||||
merge_base="$(git merge-base "${HEAD_SHA}" "origin/${BASE_REF}")"
|
||||
echo "Merge base: ${merge_base}"
|
||||
# Links stay links: a checkout that flattened them would hide a CRD
|
||||
# behind a file link from the detector while Helm still ships it, and
|
||||
# the verdict would differ from the one the same-repo gate reaches.
|
||||
git worktree add --detach "${RUNNER_TEMP}/head" "${HEAD_SHA}"
|
||||
git worktree add --detach "${RUNNER_TEMP}/base" "${merge_base}"
|
||||
|
||||
# Always from the default branch, the commit this workflow and its approval
|
||||
# rule come from. Not from the merge base: a fork's author chooses that,
|
||||
# and an old one would select an old detector, so a fix to the detector
|
||||
# would not reach the PRs that most need it.
|
||||
- name: Build api-gate
|
||||
run: |
|
||||
set -euo pipefail
|
||||
( cd "${GITHUB_WORKSPACE}" && go build -o "${RUNNER_TEMP}/api-gate" ./cmd/api-gate )
|
||||
|
||||
- name: Detect sizeable API changes
|
||||
id: detect
|
||||
run: |
|
||||
set -uo pipefail
|
||||
# api-gate exits 2 for a sizeable change; the runner's default `bash -e`
|
||||
# would end the step there, before the code is read.
|
||||
set +e
|
||||
"${RUNNER_TEMP}/api-gate" \
|
||||
--base "${RUNNER_TEMP}/base" \
|
||||
--head "${RUNNER_TEMP}/head" \
|
||||
--format markdown > "${RUNNER_TEMP}/report.md"
|
||||
code=$?
|
||||
set -e
|
||||
cat "${RUNNER_TEMP}/report.md"
|
||||
{ echo "## API Review Gate (fork PR #${PR_NUMBER})"; echo; cat "${RUNNER_TEMP}/report.md"; } >> "$GITHUB_STEP_SUMMARY"
|
||||
case "${code}" in
|
||||
0) echo "sizeable=false" >> "$GITHUB_OUTPUT" ;;
|
||||
2) echo "sizeable=true" >> "$GITHUB_OUTPUT" ;;
|
||||
*) echo "::error::api-gate failed with exit code ${code}"; exit "${code}" ;;
|
||||
esac
|
||||
|
||||
# Publishes the verdict of a workflow_run evaluation, or sweeps on a schedule.
|
||||
# One script for both, so the approval rule exists once in this file.
|
||||
conclude:
|
||||
name: Report fork API review gate status
|
||||
needs: [resolve, detect]
|
||||
if: >-
|
||||
${{ !cancelled()
|
||||
&& (github.event_name != 'workflow_run'
|
||||
|| needs.resolve.outputs.should_run == 'true') }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
statuses: write
|
||||
steps:
|
||||
- name: Checkout the gate workflow
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
sparse-checkout: .github/workflows/api-review-gate.yaml
|
||||
sparse-checkout-cone-mode: false
|
||||
persist-credentials: false
|
||||
|
||||
# api-review-gate.yaml owns the list, so the two publishers cannot drift.
|
||||
# Read from the default branch, which is where this workflow runs from.
|
||||
- name: Read API owners
|
||||
id: owners
|
||||
run: |
|
||||
set -euo pipefail
|
||||
owners="$(yq -r '.env.API_OWNERS' .github/workflows/api-review-gate.yaml)"
|
||||
if ! [[ "${owners}" =~ ^[A-Za-z0-9-]+(\ [A-Za-z0-9-]+)*$ ]]; then
|
||||
echo "::error::unexpected API_OWNERS '${owners}' in api-review-gate.yaml"
|
||||
exit 1
|
||||
fi
|
||||
echo "owners=${owners}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Report "API Review Gate" commit status
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
API_OWNERS: ${{ steps.owners.outputs.owners }}
|
||||
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
|
||||
PR_NUMBER: ${{ needs.resolve.outputs.pr_number }}
|
||||
DETECT_RESULT: ${{ needs.detect.result }}
|
||||
SIZEABLE: ${{ needs.detect.outputs.sizeable }}
|
||||
with:
|
||||
script: |
|
||||
const { API_OWNERS, HEAD_SHA, PR_NUMBER, DETECT_RESULT, SIZEABLE } = process.env;
|
||||
const { owner, repo } = context.repo;
|
||||
const owners = new Set(API_OWNERS.toLowerCase().split(' '));
|
||||
// The same descriptions api-gate-report posts. The sweep reads the
|
||||
// first two back as state, so change them only together with it.
|
||||
const APPROVED = 'sizeable API change approved by an API owner';
|
||||
const NEEDS_PREFIX = 'sizeable API change needs an approving review from: ';
|
||||
const NEEDS = NEEDS_PREFIX + API_OWNERS;
|
||||
|
||||
const setStatus = (sha, state, description) => github.rest.repos.createCommitStatus({
|
||||
owner, repo, sha, state, context: 'API Review Gate', description,
|
||||
target_url: `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`,
|
||||
});
|
||||
|
||||
// The rule of api-review-gate.yaml's approval step: each reviewer's
|
||||
// latest non-comment review on this exact commit, so an approval of an
|
||||
// older commit, a dismissed one and one superseded by a request for
|
||||
// changes all fail to count.
|
||||
const ownerApproved = async (pull_number, sha) => {
|
||||
const reviews = await github.paginate(
|
||||
github.rest.pulls.listReviews,
|
||||
{ owner, repo, pull_number, per_page: 100 },
|
||||
);
|
||||
const latest = new Map();
|
||||
for (const r of reviews) {
|
||||
if (!r.user || r.commit_id !== sha || r.state === 'COMMENTED') continue;
|
||||
const login = r.user.login.toLowerCase();
|
||||
const prev = latest.get(login);
|
||||
if (!prev || (r.submitted_at || '') >= (prev.submitted_at || '')) latest.set(login, r);
|
||||
}
|
||||
return [...latest].some(([login, r]) => owners.has(login) && r.state === 'APPROVED');
|
||||
};
|
||||
|
||||
if (context.eventName === 'workflow_run') {
|
||||
const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: Number(PR_NUMBER) });
|
||||
// A push since `resolve` started a newer run, which owns the new head.
|
||||
if (pr.state !== 'open' || pr.head.sha !== HEAD_SHA) {
|
||||
core.info(`PR #${PR_NUMBER} is no longer open at ${HEAD_SHA} — posting nothing`);
|
||||
return;
|
||||
}
|
||||
if (DETECT_RESULT === 'success' && SIZEABLE === 'false') {
|
||||
await setStatus(HEAD_SHA, 'success', 'no sizeable API change');
|
||||
} else if (DETECT_RESULT === 'success' && SIZEABLE === 'true') {
|
||||
const ok = await ownerApproved(pr.number, HEAD_SHA);
|
||||
await setStatus(HEAD_SHA, ok ? 'success' : 'failure', ok ? APPROVED : NEEDS);
|
||||
} else {
|
||||
await setStatus(HEAD_SHA, 'failure', 'gate evaluation did not complete — see the run for details');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Sweep. One GraphQL page returns 100 open PRs with the gate's status
|
||||
// on each head, which keeps a 15-minute cadence well inside the
|
||||
// GITHUB_TOKEN rate limit; reviews are fetched only for the few PRs
|
||||
// whose status the reviews can move.
|
||||
const query = `query($owner: String!, $repo: String!, $cursor: String) {
|
||||
repository(owner: $owner, name: $repo) {
|
||||
pullRequests(states: OPEN, first: 100, after: $cursor) {
|
||||
pageInfo { hasNextPage endCursor }
|
||||
nodes {
|
||||
number
|
||||
isCrossRepository
|
||||
headRefOid
|
||||
commits(last: 1) { nodes { commit { oid status {
|
||||
context(name: "API Review Gate") { state description creator { login } }
|
||||
} } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
// Every open PR is read before any is swept: whether a head is shared
|
||||
// is a fact about all of them, not about the page a PR sits on.
|
||||
const nodes = [];
|
||||
let cursor = null;
|
||||
do {
|
||||
const page = (await github.graphql(query, { owner, repo, cursor })).repository.pullRequests;
|
||||
nodes.push(...page.nodes);
|
||||
cursor = page.pageInfo.hasNextPage ? page.pageInfo.endCursor : null;
|
||||
} while (cursor);
|
||||
const carriers = new Map();
|
||||
for (const pr of nodes) {
|
||||
const c = carriers.get(pr.headRefOid) || { count: 0, sameRepo: false };
|
||||
c.count++;
|
||||
c.sameRepo = c.sameRepo || !pr.isCrossRepository;
|
||||
carriers.set(pr.headRefOid, c);
|
||||
}
|
||||
|
||||
let errors = 0;
|
||||
const closed = new Set();
|
||||
for (const pr of nodes) {
|
||||
const commit = pr.commits.nodes[0]?.commit;
|
||||
const status = commit?.status?.context;
|
||||
const shared = carriers.get(pr.headRefOid);
|
||||
if (shared.count > 1) {
|
||||
// These PRs share one status. A same-repo carrier owns it through
|
||||
// api-gate-report. Among forks it is ambiguous: a success standing
|
||||
// there may belong to a PR on another base, or rest on an approval
|
||||
// withdrawn on a PR whose run awaits a maintainer and never reaches
|
||||
// `resolve`. Close it as `resolve` does, once per head, and never
|
||||
// create a status here.
|
||||
if (!shared.sameRepo && !closed.has(pr.headRefOid) && commit?.oid === pr.headRefOid
|
||||
&& status?.creator?.login === 'github-actions' && status.state === 'SUCCESS') {
|
||||
closed.add(pr.headRefOid);
|
||||
try {
|
||||
await setStatus(pr.headRefOid, 'failure', `expected exactly one open PR for ${pr.headRefOid}, found ${shared.count}`);
|
||||
core.info(`${pr.headRefOid}: head carried by ${shared.count} fork PRs → failure`);
|
||||
} catch (err) {
|
||||
errors++;
|
||||
core.warning(`${pr.headRefOid}: ${err}`);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Only a status a workflow posted: a fork's own runs cannot post
|
||||
// one, so on a fork head that means this workflow.
|
||||
if (!pr.isCrossRepository || !status || commit.oid !== pr.headRefOid
|
||||
|| status.creator?.login !== 'github-actions') continue;
|
||||
const description = status.description || '';
|
||||
const awaiting = status.state === 'FAILURE' && description.startsWith(NEEDS_PREFIX);
|
||||
const approved = status.state === 'SUCCESS' && description === APPROVED;
|
||||
if (!awaiting && !approved) continue;
|
||||
try {
|
||||
const ok = await ownerApproved(pr.number, pr.headRefOid);
|
||||
if (awaiting && ok) {
|
||||
await setStatus(pr.headRefOid, 'success', APPROVED);
|
||||
core.info(`#${pr.number}: approved by an API owner → success`);
|
||||
} else if (approved && !ok) {
|
||||
await setStatus(pr.headRefOid, 'failure', NEEDS);
|
||||
core.info(`#${pr.number}: API owner approval withdrawn → failure`);
|
||||
}
|
||||
} catch (err) {
|
||||
errors++;
|
||||
core.warning(`#${pr.number}: ${err}`);
|
||||
}
|
||||
}
|
||||
if (errors) core.setFailed(`${errors} PR(s) could not be swept`);
|
||||
@@ -5,35 +5,92 @@ name: API Review Gate
|
||||
# API group, a new resource, or a breaking change to an existing resource's
|
||||
# schema. Detection is deterministic (cmd/api-gate); no LLM or network calls.
|
||||
#
|
||||
# The job runs on every PR (no path filter) so it can always report a status
|
||||
# and therefore be marked as a required check in branch protection without
|
||||
# leaving PRs that touch no API stuck in "pending". When nothing sizeable
|
||||
# changed it passes immediately.
|
||||
# The job runs on every PR (no path filter) so it can always reach a verdict
|
||||
# without leaving PRs that touch no API stuck in "pending". When nothing
|
||||
# sizeable changed it passes immediately.
|
||||
#
|
||||
# The merge gate is the COMMIT STATUS named "API Review Gate", posted by the
|
||||
# `api-gate-report` job for same-repo PRs and by api-review-gate-fork.yaml for
|
||||
# fork PRs — NOT by the evaluating job below. Same reasoning as the "E2E Tests"
|
||||
# gate (docs/agents/e2e-testing.md §10), and it is what makes the
|
||||
# `commented`-review skip below safe:
|
||||
# - A required *job* that skips counts as PASSED by branch protection, so any
|
||||
# path that skips the evaluating job would hand out a free pass (#3257). A
|
||||
# status is "success" only when a report step posts it, and a never-posted
|
||||
# required status stays "Expected", which blocks the merge.
|
||||
# - Check runs are NOT deduplicated per name: every workflow run gets its own
|
||||
# check suite, so `GET /commits/{sha}/check-runs?filter=latest` returns the
|
||||
# same set as `filter=all` and the rollup is worst-of. Two runs on one head
|
||||
# SHA leave two verdicts standing for the same name, and a cancelled run
|
||||
# then poisons the context for good. Commit statuses ARE deduplicated per
|
||||
# context, newest wins.
|
||||
#
|
||||
# Consequence, and the invariant every change here must preserve: declining to
|
||||
# post can only preserve a verdict some real run reached, never invent one. So
|
||||
# skipped and cancelled runs post no verdict. The one thing a run posts before it
|
||||
# has one is `pending`, which is not a pass and blocks the merge until a real
|
||||
# verdict replaces it.
|
||||
#
|
||||
# For the gate to actually block a merge, main branch protection must require
|
||||
# the "API Review Gate" status CONTEXT; without that it is advisory. Never wire
|
||||
# branch protection to a job name.
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
# Re-evaluate when a review is submitted or dismissed so an approval by an
|
||||
# API owner flips the check from failing to passing without a new push.
|
||||
# API owner flips the status from failing to passing without a new push. The
|
||||
# job-level `if` below drops the review-bot noise (plain `commented` reviews);
|
||||
# only reviews that can move the gate — approved, changes_requested,
|
||||
# dismissed — actually re-run it.
|
||||
pull_request_review:
|
||||
types: [submitted, dismissed]
|
||||
|
||||
# A run that computes a verdict must never be cancelled by a run that does not.
|
||||
# A plain `commented` review skips the evaluation (see the job `if`), so it gets
|
||||
# its own group and can only cancel other no-op runs, while pushes and
|
||||
# actionable reviews share the `eval` group and legitimately supersede each
|
||||
# other. Under a single shared group a review bot commenting during the ~30s
|
||||
# evaluation cancelled it and then contributed nothing — observed on real PRs
|
||||
# (41d1153c, 4b1966c5), leaving a cancelled run as that SHA's only verdict.
|
||||
concurrency:
|
||||
group: api-review-gate-${{ github.event.pull_request.number }}
|
||||
group: >-
|
||||
api-review-gate-${{ github.event.pull_request.number }}-${{
|
||||
github.event_name == 'pull_request_review'
|
||||
&& github.event.review.state == 'commented' && 'noop' || 'eval' }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
|
||||
env:
|
||||
# Space-separated GitHub logins allowed to satisfy the gate. Edit here to
|
||||
# change who can approve sizeable API changes. Workflow-level so the
|
||||
# evaluating job and the published status description cannot drift apart.
|
||||
API_OWNERS: "lllamnyp kvaps"
|
||||
|
||||
jobs:
|
||||
api-review-gate:
|
||||
name: Require API owner review for sizeable API changes
|
||||
# Deliberately NOT named after the gate: the merge gate is the "API Review
|
||||
# Gate" commit status, and a required context that is also a job name
|
||||
# inherits the skip-to-green and no-dedup problems in the header.
|
||||
name: API review gate (evaluate)
|
||||
# A push always evaluates the gate. A review event re-runs it only when the
|
||||
# review can move the verdict: an approval can flip fail->pass, a dismissal
|
||||
# or changes_requested can flip pass->fail. Plain `commented` reviews — all
|
||||
# that the review bots (CodeRabbit, dosubot) ever post — cannot, so they skip
|
||||
# here instead of rebuilding the detector on every bot comment. Safe because
|
||||
# `api-gate-report` skips in lockstep and posts nothing, which leaves the
|
||||
# last real verdict standing.
|
||||
if: >-
|
||||
github.event_name != 'pull_request_review' ||
|
||||
github.event.review.state != 'commented'
|
||||
runs-on: ubuntu-22.04
|
||||
outputs:
|
||||
sizeable: ${{ steps.detect.outputs.sizeable }}
|
||||
verdict: ${{ steps.approve.outputs.verdict }}
|
||||
env:
|
||||
# Space-separated GitHub logins allowed to satisfy the gate. Edit here to
|
||||
# change who can approve sizeable API changes.
|
||||
API_OWNERS: "lllamnyp kvaps"
|
||||
BASE_REF: ${{ github.event.pull_request.base.ref }}
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
PR_NUMBER: ${{ github.event.pull_request.number }}
|
||||
@@ -41,7 +98,7 @@ jobs:
|
||||
GOPROXY: "https://proxy.golang.org|direct"
|
||||
steps:
|
||||
- name: Checkout head
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.head.sha }}
|
||||
fetch-depth: 0
|
||||
@@ -52,7 +109,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
@@ -121,6 +178,7 @@ jobs:
|
||||
esac
|
||||
|
||||
- name: Check for an API owner approval
|
||||
id: approve
|
||||
if: steps.detect.outputs.sizeable == 'true'
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
@@ -135,14 +193,18 @@ jobs:
|
||||
# An APPROVED review by any API owner satisfies the gate, but only
|
||||
# each reviewer's *latest* review *on the current head commit* counts:
|
||||
# this drops approvals that a later commit invalidated, and approvals
|
||||
# an owner subsequently superseded with CHANGES_REQUESTED. GitHub
|
||||
# forbids self-approval, so a sizeable PR authored by an owner still
|
||||
# needs the other owner.
|
||||
# an owner subsequently superseded with CHANGES_REQUESTED or that was
|
||||
# dismissed. A COMMENTED review is dropped before picking the latest:
|
||||
# GitHub does not let a comment withdraw an approval, and the review
|
||||
# trigger skips comments, so counting one here would make the verdict
|
||||
# depend on which event happened to re-run the gate. GitHub forbids
|
||||
# self-approval, so a sizeable PR authored by an owner still needs the
|
||||
# other owner. api-review-gate-fork.yaml applies the same rule.
|
||||
approvals="$(
|
||||
gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/reviews" --paginate |
|
||||
jq -rs --arg head "${HEAD_SHA}" '
|
||||
add
|
||||
| map(select(.commit_id == $head))
|
||||
| map(select(.commit_id == $head and .state != "COMMENTED"))
|
||||
| sort_by(.submitted_at // "")
|
||||
| group_by(.user.login)
|
||||
| map(last)
|
||||
@@ -152,12 +214,138 @@ jobs:
|
||||
'
|
||||
)"
|
||||
|
||||
# The verdict is recorded as a step output so `api-gate-report` can tell
|
||||
# "sizeable change, no owner approval" apart from "the evaluation itself
|
||||
# broke" — both of which fail this job. Written before the exit in each
|
||||
# branch, since a step output set after exiting is never emitted.
|
||||
for owner in ${API_OWNERS}; do
|
||||
if grep -qxi "${owner}" <<<"${approvals}"; then
|
||||
echo "Sizeable API change approved by @${owner}."
|
||||
echo "verdict=pass-approved" >> "$GITHUB_OUTPUT"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
echo "verdict=fail-needs-approval" >> "$GITHUB_OUTPUT"
|
||||
echo "::error::This PR makes a sizeable API change (new group, new resource, or breaking change) and needs an approving review from one of: ${API_OWNERS}. See the job summary for details."
|
||||
exit 1
|
||||
|
||||
# A dismissal or a request for changes can turn a green head red, and until
|
||||
# the evaluation reports, about a minute later, the old `success` would still
|
||||
# let the PR merge. Same opener as the "E2E Tests" lane. Pushes need none: a
|
||||
# new head SHA starts with no status. Losing it to a transient API error only
|
||||
# reopens that window, so it must not fail the run.
|
||||
api-gate-pending:
|
||||
name: Mark API review gate as re-evaluating
|
||||
if: >-
|
||||
${{ github.event_name == 'pull_request_review'
|
||||
&& github.event.review.state != 'commented'
|
||||
&& !github.event.pull_request.head.repo.fork }}
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
permissions:
|
||||
statuses: write
|
||||
steps:
|
||||
- name: Report pending "API Review Gate" commit status
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
with:
|
||||
script: |
|
||||
const { HEAD_SHA, RUN_URL } = process.env;
|
||||
const { owner, repo } = context.repo;
|
||||
await github.rest.repos.createCommitStatus({
|
||||
owner, repo, sha: HEAD_SHA, state: 'pending',
|
||||
context: 'API Review Gate', description: 're-evaluating after a review',
|
||||
target_url: RUN_URL,
|
||||
});
|
||||
|
||||
# Publish the required "API Review Gate" commit status. Separate from the
|
||||
# evaluating job for the reasons in the header: the gate must not be a job
|
||||
# name, and a run that reached no verdict must post none.
|
||||
#
|
||||
# The `if` guard stays in LOCKSTEP with the evaluating job's — this job runs if
|
||||
# and only if that one does. A `commented` review skips both, so the previous
|
||||
# real status stands untouched. `!cancelled()` rather than `always()`: a run
|
||||
# killed by the concurrency group has no verdict, and posting one anyway would
|
||||
# overwrite a green status with red — the exact failure #3262 hit twice with
|
||||
# "E2E Tests" (see its `e2e-report`).
|
||||
#
|
||||
# Fork PRs skip this job. Their GITHUB_TOKEN is read-only on BOTH the
|
||||
# `pull_request` and `pull_request_review` triggers, so no run of this workflow
|
||||
# can post a status for a fork, and its verdict could not be relayed anyway: a
|
||||
# fork's run executes the fork's copy of this file. api-review-gate-fork.yaml
|
||||
# evaluates fork PRs again from the default branch. Until it does, the required
|
||||
# status is unposted and the fork PR is not mergeable — fail-closed by
|
||||
# construction.
|
||||
api-gate-report:
|
||||
name: Report API review gate status
|
||||
needs: [api-gate-pending, api-review-gate]
|
||||
if: >-
|
||||
${{ !cancelled()
|
||||
&& !github.event.pull_request.head.repo.fork
|
||||
&& (github.event_name != 'pull_request_review'
|
||||
|| github.event.review.state != 'commented') }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
statuses: write
|
||||
steps:
|
||||
- name: Report "API Review Gate" commit status
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
GATE_RESULT: ${{ needs.api-review-gate.result }}
|
||||
SIZEABLE: ${{ needs.api-review-gate.outputs.sizeable }}
|
||||
VERDICT: ${{ needs.api-review-gate.outputs.verdict }}
|
||||
# Inherited from the workflow-level env; passed explicitly so the
|
||||
# script's use of it is visible at the call site.
|
||||
API_OWNERS: ${{ env.API_OWNERS }}
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
with:
|
||||
script: |
|
||||
const { GATE_RESULT, SIZEABLE, VERDICT, API_OWNERS, HEAD_SHA, RUN_URL } = process.env;
|
||||
const { owner, repo } = context.repo;
|
||||
|
||||
// No verdict to publish — say nothing and let the last real status
|
||||
// stand. Reachable when the evaluating job was skipped despite the
|
||||
// lockstep guard (a `needs` short-circuit), and defensive against a
|
||||
// future guard drift.
|
||||
if (GATE_RESULT !== 'success' && GATE_RESULT !== 'failure') {
|
||||
core.info(`evaluation result '${GATE_RESULT}' — posting nothing, previous status stands`);
|
||||
return;
|
||||
}
|
||||
|
||||
let state, description;
|
||||
if (GATE_RESULT === 'success') {
|
||||
state = 'success';
|
||||
description = SIZEABLE === 'true'
|
||||
? 'sizeable API change approved by an API owner'
|
||||
: 'no sizeable API change';
|
||||
} else if (VERDICT === 'fail-needs-approval') {
|
||||
state = 'failure';
|
||||
description = `sizeable API change needs an approving review from: ${API_OWNERS}`;
|
||||
} else {
|
||||
// Either the job failed before reaching a verdict (checkout, build or
|
||||
// the detector), or it reached `fail-needs-approval` but the output did
|
||||
// not survive the failing job — whether a failed job propagates its
|
||||
// outputs is not something the docs pin down. Only the description
|
||||
// degrades: the state is `failure` down both paths, so the gate itself
|
||||
// is unaffected. Fail-closed either way — an unverifiable gate is red.
|
||||
state = 'failure';
|
||||
description = 'gate evaluation did not complete — see the run for details';
|
||||
}
|
||||
|
||||
try {
|
||||
await github.rest.repos.createCommitStatus({
|
||||
owner, repo, sha: HEAD_SHA, state,
|
||||
context: 'API Review Gate', description, target_url: RUN_URL,
|
||||
});
|
||||
} catch (err) {
|
||||
// Fail loudly and fail-closed: setFailed keeps this job red and the
|
||||
// required status unposted ("Expected"), so the PR stays unmergeable.
|
||||
core.setFailed(`Failed to report API Review Gate status: ${err}`);
|
||||
return;
|
||||
}
|
||||
core.info(`API Review Gate → ${state}: ${description}`);
|
||||
@@ -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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
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@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
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
|
||||
@@ -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"
|
||||
}
|
||||
@@ -43,7 +43,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -67,7 +67,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Login to OCIR
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -80,7 +80,7 @@ jobs:
|
||||
# limited.
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
|
||||
@@ -54,7 +54,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -111,7 +111,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Login to OCIR
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -124,7 +124,7 @@ jobs:
|
||||
# isolated buildkit so it never contends with PR builds on the shared one.
|
||||
- name: Set up Buildx (docker-container driver)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
env:
|
||||
|
||||
@@ -97,7 +97,7 @@ jobs:
|
||||
echo "Building line '$REF_NAME'."
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -120,7 +120,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Login to OCIR
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -132,7 +132,7 @@ jobs:
|
||||
# with PR builds on the shared embedded builder.
|
||||
- name: Set up Buildx (docker-container driver)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
env:
|
||||
|
||||
@@ -79,7 +79,7 @@ jobs:
|
||||
# prompt's instructions. The caller owns every write.
|
||||
- name: Generate read-only GitHub App token
|
||||
id: app-token-read
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -131,7 +131,7 @@ jobs:
|
||||
# upper bound" a property of the checkout rather than an instruction the
|
||||
# prompt has to talk the agent into honouring.
|
||||
- name: Checkout rc tag
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ steps.parse.outputs.rc_tag }}
|
||||
fetch-depth: 0
|
||||
@@ -147,13 +147,14 @@ jobs:
|
||||
# actions/checkout rather than by shelling out to the repo's own tooling.
|
||||
# Gitignored, so the caller's `git add -A` never stages it.
|
||||
- name: Checkout release tooling from the dispatch ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.ref_name }}
|
||||
path: .release-tooling
|
||||
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
|
||||
@@ -216,7 +216,7 @@ jobs:
|
||||
|
||||
- name: Setup Node.js
|
||||
if: steps.check_changelog.outputs.exists == 'false'
|
||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: 22
|
||||
|
||||
@@ -273,7 +273,7 @@ jobs:
|
||||
# promote-rc's open-pr and changelog-rc's commit job download.
|
||||
- name: Upload changelog artifact
|
||||
if: steps.changelog_status.outputs.present == 'true'
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: changelog-v${{ steps.parse.outputs.stable_version }}
|
||||
path: docs/changelogs/v${{ steps.parse.outputs.stable_version }}.md
|
||||
|
||||
@@ -67,7 +67,7 @@ jobs:
|
||||
# parse-rc-tag.sh lives in the tree, so overlay the dispatch ref's hack/
|
||||
# before calling it. Gitignored; nothing in this job commits.
|
||||
- name: Checkout release tooling from the dispatch ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.ref_name }}
|
||||
path: .release-tooling
|
||||
@@ -165,7 +165,7 @@ jobs:
|
||||
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -177,7 +177,7 @@ jobs:
|
||||
# parse job (before generation), so it is not re-checked here — a checkout
|
||||
# failure for a branch deleted in the meantime is a rare, self-evident race.
|
||||
- name: Checkout rc staging branch
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.parse.outputs.rc_branch }}
|
||||
fetch-depth: 0
|
||||
@@ -188,7 +188,7 @@ jobs:
|
||||
# hack/validate-changelog.sh. Overlay the current tooling from the dispatch
|
||||
# ref, exactly as promote-rc and changelog-generate do. Gitignored.
|
||||
- name: Checkout release tooling from the dispatch ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.ref_name }}
|
||||
path: .release-tooling
|
||||
@@ -198,7 +198,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Download changelog artifact
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: changelog-v${{ needs.parse.outputs.stable_version }}
|
||||
path: docs/changelogs
|
||||
|
||||
@@ -39,10 +39,10 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
|
||||
@@ -56,18 +56,18 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: false
|
||||
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4.36.2
|
||||
uses: github/codeql-action/init@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
build-mode: ${{ matrix.build-mode }}
|
||||
@@ -93,6 +93,6 @@ jobs:
|
||||
done
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4.36.2
|
||||
uses: github/codeql-action/analyze@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||
with:
|
||||
category: "/language:${{ matrix.language }}"
|
||||
@@ -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).
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
id: parse
|
||||
env:
|
||||
TAG: ${{ inputs.tag }}
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const tag = process.env.TAG.trim();
|
||||
@@ -128,7 +128,7 @@ jobs:
|
||||
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -142,7 +142,7 @@ jobs:
|
||||
# changes or the job is reached by another path. Reads permission via the
|
||||
# app token (fails the step if the API errors — fail closed).
|
||||
- name: Require write access
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -168,7 +168,7 @@ jobs:
|
||||
# credential (persist-credentials, default on) so the bare push below is
|
||||
# app-authenticated, and to resolve HEAD. One ref, shallow, no tags.
|
||||
- name: Check out the dispatched branch
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 1
|
||||
token: ${{ steps.app-token.outputs.token }}
|
||||
|
||||
@@ -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
|
||||
@@ -94,7 +95,7 @@ jobs:
|
||||
# pull-requests.yaml's e2e-report and docs/agents/e2e-testing.md §10.
|
||||
- name: Resolve fork PR and post "E2E Tests" status
|
||||
id: r
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
|
||||
RUN_ID: ${{ github.event.workflow_run.id }}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -359,7 +366,7 @@ jobs:
|
||||
# The base repo's own merge ref — safe to fetch, not fork-controlled code
|
||||
# execution. pr.patch is applied as DATA below; nothing from the tree runs.
|
||||
- name: Checkout PR merge ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: refs/pull/${{ needs.resolve.outputs.pr_number }}/merge
|
||||
fetch-depth: 0
|
||||
@@ -380,7 +387,7 @@ jobs:
|
||||
got="$(git rev-parse HEAD^2)"
|
||||
[ "$got" = "$EXPECTED_SHA" ] || { echo "::error::merge ref moved: HEAD^2=$got != resolved $EXPECTED_SHA"; exit 1; }
|
||||
- name: Download pr.patch
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: pr-patch
|
||||
path: _out/assets
|
||||
@@ -389,7 +396,7 @@ jobs:
|
||||
- name: Apply pr.patch (reconstruct digest-pinned tree)
|
||||
run: git apply --3way _out/assets/pr.patch
|
||||
- name: Download OCI image archives
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
pattern: pr-oci-*
|
||||
merge-multiple: true
|
||||
@@ -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
|
||||
@@ -558,7 +565,7 @@ jobs:
|
||||
SANDBOX_NAME: cozy-e2e-sandbox-fork-${{ github.run_id }}
|
||||
steps:
|
||||
- name: Checkout PR merge ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: refs/pull/${{ needs.resolve.outputs.pr_number }}/merge
|
||||
fetch-depth: 0
|
||||
@@ -586,7 +593,7 @@ jobs:
|
||||
rm -f "$yq_bin"
|
||||
fi
|
||||
- name: Download pr.patch
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: pr-patch
|
||||
path: _out/assets
|
||||
@@ -755,7 +762,7 @@ jobs:
|
||||
_out/chainsaw-report.xml || true
|
||||
- name: Upload chainsaw report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: chainsaw-report
|
||||
path: _out/chainsaw-report.xml
|
||||
@@ -766,26 +773,11 @@ jobs:
|
||||
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME collect-report || true
|
||||
- name: Upload cozyreport.tgz
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
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
|
||||
@@ -809,7 +801,7 @@ jobs:
|
||||
statuses: write
|
||||
steps:
|
||||
- name: Report "E2E Tests" commit status
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
PUBLISH_RESULT: ${{ needs.publish.result }}
|
||||
E2E_RESULT: ${{ needs.e2e.result }}
|
||||
|
||||
@@ -81,7 +81,7 @@ jobs:
|
||||
echo "Installing nightly ${rev:-<no revision annotation>}, packages ${digest}"
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 1
|
||||
persist-credentials: false
|
||||
@@ -174,7 +174,7 @@ jobs:
|
||||
|
||||
- name: Upload cozyreport.tgz
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: cozyreport-isp-hosted
|
||||
path: _out/cozyreport.tgz
|
||||
|
||||
@@ -36,7 +36,8 @@ jobs:
|
||||
# The job needs a self-hosted runner carrying the `proxmox` label. On a
|
||||
# repository without one a scheduled run would sit in the queue until GitHub
|
||||
# cancels it, so the schedule fires only where the stand is declared;
|
||||
# manual dispatch always runs.
|
||||
# manual dispatch always runs. The runner must be v2.327.1 or newer, the
|
||||
# minimum the checkout README names for its node24 runtime.
|
||||
if: github.event_name == 'workflow_dispatch' || vars.PROXMOX_NIGHTLY == 'true'
|
||||
runs-on: [self-hosted, proxmox]
|
||||
timeout-minutes: 90
|
||||
@@ -51,7 +52,7 @@ jobs:
|
||||
COZY_KEEP_TENANT: ${{ inputs.keep_tenant == true }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -91,7 +92,10 @@ jobs:
|
||||
cat "$RUNNER_TEMP/kubeconfig-check.err" >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s' "$PVE_SSH_KEY" > "$COZY_PVE_SSH_KEY"
|
||||
# OpenSSH refuses a private key whose last line has no newline ("error
|
||||
# in libcrypto"), and `gh secret set` strips it from a key read from
|
||||
# stdin. A second newline, when the secret kept its own, is harmless.
|
||||
printf '%s\n' "$PVE_SSH_KEY" > "$COZY_PVE_SSH_KEY"
|
||||
|
||||
# The credentials the management-side CSI controller and CCM read. A
|
||||
# Secret rather than values so no token is ever written into a
|
||||
@@ -157,4 +161,8 @@ jobs:
|
||||
|
||||
- name: Remove staged credentials
|
||||
if: always()
|
||||
run: rm -f "$GITHUB_WORKSPACE/.kubeconfig" "$COZY_PVE_SSH_KEY"
|
||||
# The tenant kubeconfig is the suite's, fetched by run-proxmox.sh into
|
||||
# RUNNER_TEMP, which the runner exports into every step (the runner
|
||||
# context is not allowed in job-level env); its finally removes it too,
|
||||
# but a run killed mid-step never reaches that.
|
||||
run: rm -f "$GITHUB_WORKSPACE/.kubeconfig" "$COZY_PVE_SSH_KEY" "$RUNNER_TEMP/proxmox-e2e-tenant.kubeconfig"
|
||||
@@ -63,7 +63,7 @@ jobs:
|
||||
steps:
|
||||
- name: Validate tag and resolve release
|
||||
id: resolve
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
TAG: ${{ inputs.tag }}
|
||||
with:
|
||||
@@ -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.
|
||||
@@ -145,7 +145,7 @@ jobs:
|
||||
# The resolved ref is the digest-pinned staging branch for a prerelease,
|
||||
# or the stable tag's pinned merge tree.
|
||||
- name: Checkout resolved release tree
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.resolve.outputs.tree_ref }}
|
||||
fetch-depth: 1
|
||||
@@ -283,7 +283,7 @@ jobs:
|
||||
|
||||
- name: Upload chainsaw report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: chainsaw-report-${{ inputs.tag }}
|
||||
path: /tmp/${{ env.SANDBOX_NAME }}/_out/chainsaw-report.xml
|
||||
@@ -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
|
||||
@@ -328,7 +328,7 @@ jobs:
|
||||
|
||||
- name: Upload cozyreport.tgz
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: cozyreport-${{ inputs.tag }}
|
||||
path: /tmp/${{ env.SANDBOX_NAME }}/_out/cozyreport.tgz
|
||||
@@ -382,7 +382,7 @@ jobs:
|
||||
|
||||
- name: Upload image list
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: image-list-${{ inputs.tag }}
|
||||
path: |
|
||||
|
||||
@@ -65,7 +65,7 @@ jobs:
|
||||
issues: write
|
||||
steps:
|
||||
- name: Apply triage labels
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
// Labels applied with the workflow's GITHUB_TOKEN do not trigger
|
||||
|
||||
@@ -27,7 +27,7 @@ jobs:
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
|
||||
- name: Validate labels.yml schema
|
||||
run: |
|
||||
@@ -80,8 +80,8 @@ jobs:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: EndBug/label-sync@52074158190acb45f3077f9099fea818aa43f97a # v2
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: EndBug/label-sync@52074158190acb45f3077f9099fea818aa43f97a # v2.3.3
|
||||
with:
|
||||
config-file: .github/labels.yml
|
||||
delete-other-labels: false
|
||||
@@ -130,7 +130,7 @@ jobs:
|
||||
echo "Resolved main build: sha=$sha version=$version packages_ref=$packages_ref"
|
||||
|
||||
- name: Checkout source commit
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ steps.resolve.outputs.sha }}
|
||||
fetch-depth: 1
|
||||
@@ -197,7 +197,7 @@ jobs:
|
||||
packages: write
|
||||
steps:
|
||||
- name: Checkout source commit
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.mirror.outputs.sha }}
|
||||
fetch-depth: 1
|
||||
@@ -250,13 +250,13 @@ 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
|
||||
steps:
|
||||
- name: Checkout source commit
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.mirror.outputs.sha }}
|
||||
fetch-depth: 1
|
||||
@@ -379,13 +379,13 @@ jobs:
|
||||
|
||||
- name: Upload chainsaw report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: chainsaw-report
|
||||
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
|
||||
@@ -418,7 +418,7 @@ jobs:
|
||||
|
||||
- name: Upload cozyreport.tgz
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: cozyreport
|
||||
path: _out/cozyreport.tgz
|
||||
|
||||
@@ -20,11 +20,11 @@ jobs:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- name: Apply labels from PR title
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: main
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
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"'
|
||||
@@ -19,7 +19,7 @@ jobs:
|
||||
permissions:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const pr = context.payload.pull_request;
|
||||
|
||||
@@ -23,13 +23,13 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@a37ce9120846195fa4ece8f58b268e6043cb2f26 # v3
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4
|
||||
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
@@ -37,7 +37,7 @@ jobs:
|
||||
run: pip install pre-commit
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
@@ -89,19 +89,17 @@ 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
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.head.sha }}
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -90,7 +90,7 @@ jobs:
|
||||
id: parse
|
||||
env:
|
||||
RC_TAG: ${{ inputs.rc_tag }}
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const rc = process.env.RC_TAG;
|
||||
@@ -135,7 +135,7 @@ jobs:
|
||||
- name: Verify green RC E2E
|
||||
id: e2e-gate
|
||||
if: ${{ !inputs.skip_e2e_gate }}
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
RC_TAG: ${{ steps.parse.outputs.rc_tag }}
|
||||
with:
|
||||
@@ -274,7 +274,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -285,7 +285,7 @@ jobs:
|
||||
# Gate: the rc must exist as a (draft) release and the stable tag must NOT
|
||||
# exist yet. Refuse to promote twice or to an already-shipped version.
|
||||
- name: Validate rc is promotable
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -391,7 +394,7 @@ jobs:
|
||||
|
||||
# The rc's digest-vendored tree (the staging branch the rc build pushed).
|
||||
- name: Checkout rc staging branch
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.parse.outputs.rc_branch }}
|
||||
fetch-depth: 0
|
||||
@@ -445,7 +448,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Login to registry (GHCR)
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -461,7 +464,7 @@ jobs:
|
||||
# actions/checkout rather than by shelling out to the repo's own tooling.
|
||||
# Gitignored, so promote's `git add -A` never stages it.
|
||||
- name: Checkout release tooling from the dispatch ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.ref_name }}
|
||||
path: .release-tooling
|
||||
@@ -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:
|
||||
@@ -706,7 +710,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -715,7 +719,7 @@ jobs:
|
||||
owner: cozystack
|
||||
|
||||
- name: Checkout website repo
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: cozystack/website
|
||||
ref: main
|
||||
@@ -871,7 +875,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -883,7 +887,7 @@ jobs:
|
||||
# whether or not the changelog step ran, so it cannot rely on the
|
||||
# `git remote set-url` that only the conditional commit step performs.
|
||||
- name: Checkout stable staging branch
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ needs.promote.outputs.stable_branch }}
|
||||
fetch-depth: 0
|
||||
@@ -901,7 +905,7 @@ jobs:
|
||||
# actions/checkout rather than by shelling out to the repo's own tooling.
|
||||
# Gitignored, so promote's `git add -A` never stages it.
|
||||
- name: Checkout release tooling from the dispatch ref
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.ref_name }}
|
||||
path: .release-tooling
|
||||
@@ -941,7 +945,7 @@ jobs:
|
||||
id: download
|
||||
if: needs.changelog.outputs.present == 'true' && steps.existing.outputs.present == 'false'
|
||||
continue-on-error: true
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: changelog-v${{ needs.promote.outputs.stable_version }}
|
||||
path: docs/changelogs
|
||||
@@ -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."
|
||||
|
||||
@@ -55,7 +55,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -64,7 +64,7 @@ jobs:
|
||||
# Extract tag from branch name (branch = release-X.Y.Z*)
|
||||
- name: Extract tag from branch name
|
||||
id: get_tag
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const branch = context.payload.pull_request.head.ref;
|
||||
@@ -79,7 +79,7 @@ jobs:
|
||||
|
||||
# Checkout repo & create / push annotated tag
|
||||
- name: Checkout repo
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
# Do NOT persist GITHUB_TOKEN as http.extraheader. Every tag/branch push
|
||||
@@ -96,7 +96,7 @@ jobs:
|
||||
# argument remains the merged packages tree checked out above. This keeps
|
||||
# the final guard independent of the release PR's executable contents.
|
||||
- name: Checkout release tooling from base
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.base.sha }}
|
||||
path: .release-tooling
|
||||
@@ -236,7 +236,7 @@ jobs:
|
||||
|
||||
# Ensure maintenance branch release-X.Y
|
||||
- name: Ensure maintenance branch release-X.Y
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -308,7 +308,7 @@ jobs:
|
||||
# Publish draft release and ensure correct latest flag
|
||||
- name: Publish draft release
|
||||
id: publish
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -419,24 +419,51 @@ jobs:
|
||||
// Reading the file here is both race-free and branch-agnostic;
|
||||
// update-releasenotes.yaml remains for later maintainer edits and
|
||||
// manual re-syncs.
|
||||
//
|
||||
// docs/releasenotes/${tag}.md, a short hand-written summary for a
|
||||
// release whose changelog is too long to read on the release page,
|
||||
// is the body when it has content; the changelog otherwise.
|
||||
//
|
||||
// The tags above already exist, so a body GitHub refuses would
|
||||
// strand a half-promoted release. GitHub's web UI cut the
|
||||
// v1.7.0-rc.2 changelog at about 125000 characters. MAX_BODY_BYTES
|
||||
// counts bytes, which are never fewer than characters, and sits 25
|
||||
// percent under that figure, so neither the difference nor an
|
||||
// imprecise limit matters, and GitHub's answer to an oversized
|
||||
// body is never relied on. update-releasenotes.yaml uses the same
|
||||
// constant.
|
||||
const fs = require('fs');
|
||||
const MAX_BODY_BYTES = 100000;
|
||||
const changelogPath = `docs/changelogs/${tag}.md`;
|
||||
let body;
|
||||
if (fs.existsSync(changelogPath)) {
|
||||
const raw = fs.readFileSync(changelogPath, 'utf8');
|
||||
let sourcePath;
|
||||
let raw;
|
||||
for (const candidate of [`docs/releasenotes/${tag}.md`, changelogPath]) {
|
||||
if (!fs.existsSync(candidate)) continue;
|
||||
raw = fs.readFileSync(candidate, 'utf8');
|
||||
// Last line of defence before the body becomes public and
|
||||
// immutable-by-convention. promote-rc.yaml validates the changelog
|
||||
// structurally, but it can also arrive by hand-commit, and an
|
||||
// empty or whitespace-only file here would blank the release notes
|
||||
// more thoroughly than publishing the draft's body would.
|
||||
if (raw.trim().length === 0) {
|
||||
console.log(`::warning::${changelogPath} is empty or whitespace-only — refusing to use it as the release body for ${tag}. Publishing with the draft's existing body instead.`);
|
||||
console.log(`::warning::${candidate} is empty or whitespace-only — not using it as the release body for ${tag}.`);
|
||||
continue;
|
||||
}
|
||||
sourcePath = candidate;
|
||||
break;
|
||||
}
|
||||
let body;
|
||||
if (!sourcePath) {
|
||||
console.log(`::warning::No usable docs/releasenotes/${tag}.md or ${changelogPath} in the merge commit — publishing ${tag} with the draft's existing body. Add the changelog and re-run 'Update Release Notes' to backfill it.`);
|
||||
} else {
|
||||
const bytes = Buffer.byteLength(raw, 'utf8');
|
||||
if (bytes > MAX_BODY_BYTES) {
|
||||
body = `The release notes for ${tag} are too long for a GitHub release description; the full changelog is [${changelogPath}](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/${tag}/${changelogPath}).`;
|
||||
console.log(`::warning::${sourcePath} is ${bytes} bytes, over MAX_BODY_BYTES (${MAX_BODY_BYTES}) — publishing ${tag} with a link to ${changelogPath} instead.`);
|
||||
} else {
|
||||
body = raw;
|
||||
console.log(`📝 Release body from ${changelogPath} (${body.length} bytes)`);
|
||||
console.log(`📝 Release body from ${sourcePath} (${bytes} bytes)`);
|
||||
}
|
||||
} else {
|
||||
console.log(`::warning::${changelogPath} not found in the merge commit — publishing ${tag} with the draft's existing body. Add the changelog and re-run 'Update Release Notes' to backfill it.`);
|
||||
}
|
||||
|
||||
// Publish the release
|
||||
|
||||
@@ -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 }}
|
||||
@@ -111,7 +113,7 @@ jobs:
|
||||
# still gets posted by `e2e-report`, and a run that dies before that
|
||||
# leaves the merge blocked either way.
|
||||
continue-on-error: true
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
@@ -127,7 +129,7 @@ jobs:
|
||||
description: 'e2e in progress — this run has not reported yet',
|
||||
target_url: RUN_URL,
|
||||
});
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
# e2e-fork.yaml pushes a fork's images only under names the base tree
|
||||
@@ -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"
|
||||
@@ -222,14 +240,14 @@ jobs:
|
||||
# The default pull_request checkout is GitHub's prospective merge commit,
|
||||
# not just the head branch. That makes base-branch package drift visible.
|
||||
- name: Checkout prospective merge
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
# Execute the verifier from the trusted base SHA. An older rc-derived head
|
||||
# may not contain it, and a PR must not be able to weaken its own guard.
|
||||
- name: Checkout release tooling from base
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.base.sha }}
|
||||
path: .release-tooling
|
||||
@@ -263,7 +281,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Login to registry (GHCR)
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -293,33 +311,36 @@ 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:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
# 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,11 +396,23 @@ 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
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: bats-unit-report
|
||||
path: _out/test-reports/
|
||||
@@ -406,7 +440,7 @@ jobs:
|
||||
pkg: ${{ fromJSON(needs.plan.outputs.matrix) }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -423,7 +457,7 @@ jobs:
|
||||
fi
|
||||
- name: Login to GitHub Container Registry
|
||||
if: ${{ !github.event.pull_request.head.repo.fork }}
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -442,12 +476,14 @@ jobs:
|
||||
# OCIR auth) so buildx forwards push/cache auth to the container builder.
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
- name: Record image references before the build
|
||||
run: sh -c '. hack/lib/image-refs.sh; collect_image_refs packages' | sort -u > "$RUNNER_TEMP/image-refs-before"
|
||||
- name: Build ${{ matrix.pkg }}
|
||||
run: |
|
||||
mkdir -p _out/oci
|
||||
@@ -477,6 +513,18 @@ jobs:
|
||||
# against the package dir — the archive would land in <pkg>/_out/oci and
|
||||
# miss the repo-root _out/oci the upload reads.
|
||||
OCI_EXPORT_DIR: ${{ github.event.pull_request.head.repo.fork && format('{0}/_out/oci', github.workspace) || '' }}
|
||||
# Fork PR images exist only as OCI archives at this point, so there is
|
||||
# nothing to pull.
|
||||
- name: Smoke-test the images this job built
|
||||
if: ${{ !github.event.pull_request.head.repo.fork }}
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
COZYTEST_TRACE: '0'
|
||||
run: |
|
||||
sh -c '. hack/lib/image-refs.sh; collect_image_refs packages' | sort -u > "$RUNNER_TEMP/image-refs-after"
|
||||
comm -13 "$RUNNER_TEMP/image-refs-before" "$RUNNER_TEMP/image-refs-after" > "$RUNNER_TEMP/image-refs-built"
|
||||
cat "$RUNNER_TEMP/image-refs-built"
|
||||
IMAGE_SMOKE_REFS_FILE="$RUNNER_TEMP/image-refs-built" hack/cozytest.sh hack/image-smoke.bats
|
||||
- name: Capture digest patch fragment
|
||||
id: frag
|
||||
env:
|
||||
@@ -491,7 +539,7 @@ jobs:
|
||||
echo "safe=${safe}" >> "$GITHUB_OUTPUT"
|
||||
echo "Fragment for ${PKG}:"; git --no-pager diff --stat HEAD
|
||||
- name: Upload patch fragment
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-patch-fragment-${{ steps.frag.outputs.safe }}
|
||||
path: _out/fragments/${{ steps.frag.outputs.safe }}.patch
|
||||
@@ -500,7 +548,7 @@ jobs:
|
||||
# (OCI_EXPORT_DIR above). Upload them for e2e-fork.yaml to push by digest.
|
||||
- name: Upload OCI image archives (fork PR)
|
||||
if: ${{ github.event.pull_request.head.repo.fork }}
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-oci-${{ steps.frag.outputs.safe }}
|
||||
path: _out/oci/*.oci.tar
|
||||
@@ -539,7 +587,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -550,7 +598,7 @@ jobs:
|
||||
fi
|
||||
- name: Login to GitHub Container Registry
|
||||
if: ${{ !github.event.pull_request.head.repo.fork }}
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -562,7 +610,7 @@ jobs:
|
||||
# anonymous-pull rate limit; see the build job for the full rationale.
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
@@ -590,7 +638,7 @@ jobs:
|
||||
git diff --binary HEAD > _out/fragments/talos.patch
|
||||
echo "Talos fragment:"; git --no-pager diff --stat HEAD
|
||||
- name: Upload Talos patch fragment
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-patch-fragment-talos
|
||||
path: _out/fragments/talos.patch
|
||||
@@ -598,7 +646,7 @@ jobs:
|
||||
# Fork PR only: upload the Talos image OCI archive for e2e-fork.yaml.
|
||||
- name: Upload Talos OCI image archive (fork PR)
|
||||
if: ${{ github.event.pull_request.head.repo.fork }}
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-oci-talos
|
||||
path: _out/oci/*.oci.tar
|
||||
@@ -629,7 +677,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -638,7 +686,7 @@ jobs:
|
||||
with:
|
||||
version: "2.8.6"
|
||||
- name: Download patch fragments
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
pattern: pr-patch-fragment-*
|
||||
merge-multiple: true
|
||||
@@ -665,7 +713,7 @@ jobs:
|
||||
fi
|
||||
- name: Login to GitHub Container Registry
|
||||
if: ${{ !github.event.pull_request.head.repo.fork }}
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -785,7 +833,7 @@ jobs:
|
||||
# anonymous-pull rate limit; see the build job for the full rationale.
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
@@ -819,7 +867,7 @@ jobs:
|
||||
# Fork PR only: upload the operator image OCI archive for e2e-fork.yaml.
|
||||
- name: Upload installer OCI image archive (fork PR)
|
||||
if: ${{ github.event.pull_request.head.repo.fork }}
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-oci-installer
|
||||
path: _out/oci/*.oci.tar
|
||||
@@ -832,7 +880,7 @@ jobs:
|
||||
git diff --binary HEAD > _out/assets/pr.patch
|
||||
echo "Final pr.patch:"; git --no-pager diff --stat HEAD
|
||||
- name: Upload git diff patch
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: pr-patch
|
||||
path: _out/assets/pr.patch
|
||||
@@ -860,7 +908,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -870,7 +918,7 @@ jobs:
|
||||
|
||||
- name: Checkout code
|
||||
if: contains(github.event.pull_request.labels.*.name, 'release')
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -878,7 +926,7 @@ jobs:
|
||||
- name: Extract tag from PR branch (release PR)
|
||||
if: contains(github.event.pull_request.labels.*.name, 'release')
|
||||
id: get_tag
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const branch = context.payload.pull_request.head.ref;
|
||||
@@ -897,7 +945,7 @@ jobs:
|
||||
# asset's presence is still checked in e2e-tag.yaml and promote-rc.yaml.
|
||||
- name: Verify draft release exists (release PR)
|
||||
if: contains(github.event.pull_request.labels.*.name, 'release')
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -953,7 +1001,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
|
||||
@@ -986,7 +1034,7 @@ jobs:
|
||||
# fetch-depth: 0 is required by the "Select E2E tests" step so that
|
||||
# `git diff origin/${BASE_REF}...HEAD` can resolve the base ref.
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -995,7 +1043,7 @@ jobs:
|
||||
# lane consumes no nocloud asset and waits on no Talos build.
|
||||
- name: Download PR patch
|
||||
if: "!contains(github.event.pull_request.labels.*.name, 'release')"
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: pr-patch
|
||||
path: _out/assets
|
||||
@@ -1216,7 +1264,7 @@ jobs:
|
||||
|
||||
- name: Upload chainsaw report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: chainsaw-report
|
||||
path: _out/chainsaw-report.xml
|
||||
@@ -1224,7 +1272,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 +1283,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
|
||||
@@ -1269,7 +1317,7 @@ jobs:
|
||||
|
||||
- name: Upload cozyreport.tgz
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: cozyreport
|
||||
path: _out/cozyreport.tgz
|
||||
@@ -1281,7 +1329,7 @@ jobs:
|
||||
|
||||
- name: Upload image list
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: image-list
|
||||
path: _out/images.txt
|
||||
@@ -1348,28 +1396,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
|
||||
@@ -1427,7 +1453,7 @@ jobs:
|
||||
statuses: write
|
||||
steps:
|
||||
- name: Report "E2E Tests" commit status
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
env:
|
||||
PLAN_RESULT: ${{ needs.plan.result }}
|
||||
CODE: ${{ needs.plan.outputs.code }}
|
||||
@@ -1449,7 +1475,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
|
||||
|
||||
@@ -65,7 +65,7 @@ jobs:
|
||||
pull-requests: read
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -76,7 +76,7 @@ jobs:
|
||||
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
|
||||
@@ -25,12 +25,12 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Run analysis
|
||||
uses: ossf/scorecard-action@4eaacf0543bb3f2c246792bd56e8cdeffafb205a # v2.4.3
|
||||
uses: ossf/scorecard-action@2d1146689b8cda280b9bc96326124645441f03bc # v2.4.4
|
||||
with:
|
||||
results_file: results.sarif
|
||||
results_format: sarif
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
# first-party findings. The public scorecard.dev badge (publish_results
|
||||
# above) is the canonical view of these results.
|
||||
- name: Upload SARIF as artifact
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: scorecard-sarif
|
||||
path: results.sarif
|
||||
|
||||
@@ -21,7 +21,7 @@ jobs:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: actions/stale@1e223db275d687790206a7acac4d1a11bd6fe629 # v10
|
||||
- uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0
|
||||
with:
|
||||
stale-issue-label: lifecycle/stale
|
||||
stale-pr-label: lifecycle/stale
|
||||
@@ -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
|
||||
|
||||
+45
-42
@@ -44,7 +44,7 @@ jobs:
|
||||
# rebuilt, which is the safe direction.
|
||||
- name: Check if release already exists
|
||||
id: check_release
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const tag = context.ref.replace('refs/tags/', '');
|
||||
@@ -59,7 +59,7 @@ jobs:
|
||||
|
||||
- name: Checkout code
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -73,7 +73,7 @@ jobs:
|
||||
|
||||
- name: Login to GHCR
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -83,7 +83,7 @@ jobs:
|
||||
|
||||
- name: Login to OCIR (build cache)
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -97,7 +97,7 @@ jobs:
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
@@ -120,6 +120,7 @@ jobs:
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
BUILDER: ${{ steps.buildx.outputs.name }}
|
||||
SBOM: '1'
|
||||
PLATFORM: linux/arm64
|
||||
CACHE_REGISTRY: iad.ocir.io/idyksih5sir9/cozystack
|
||||
# Read the arm64 cache build-arm64-nightly.yaml keeps warm; a release
|
||||
@@ -174,7 +175,7 @@ jobs:
|
||||
# the draft and skips straight to changelog/website.
|
||||
- name: Check if release already exists
|
||||
id: check_release
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const tag = context.ref.replace('refs/tags/', '');
|
||||
@@ -199,7 +200,7 @@ jobs:
|
||||
- name: Parse tag
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
id: tag
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const ref = context.ref.replace('refs/tags/', ''); // e.g. v0.31.5-rc.1
|
||||
@@ -234,7 +235,7 @@ jobs:
|
||||
- name: Get base branch
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
id: get_base
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const baseRef = context.payload.base_ref;
|
||||
@@ -252,7 +253,7 @@ jobs:
|
||||
|
||||
- name: Checkout code
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
@@ -268,7 +269,7 @@ jobs:
|
||||
|
||||
- name: Login to GHCR
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -282,7 +283,7 @@ jobs:
|
||||
# cold (the persistent runner used to warm-start from local buildkit state).
|
||||
- name: Login to OCIR (build cache)
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ secrets.OCIR_USER }}
|
||||
password: ${{ secrets.OCIR_TOKEN }}
|
||||
@@ -290,12 +291,15 @@ jobs:
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
|
||||
# Only for the matchbox rebuild below: the default docker driver cannot
|
||||
# build an index, and `use: false` keeps make build on that driver.
|
||||
- name: Set up Buildx for matchbox (docker-container driver)
|
||||
# Every image carries an SBOM attestation, which the default docker driver
|
||||
# cannot attach, and the matchbox index below needs this driver too.
|
||||
# docker.io pulls go through the mirror in hack/buildkitd.toml, as the
|
||||
# arm64 leg's do. `use: false` keeps plain `docker build` on the default
|
||||
# driver; the builds name this one with BUILDER.
|
||||
- name: Set up Buildx (docker-container driver, docker.io mirror)
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
id: matchbox_buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
||||
with:
|
||||
driver: docker-container
|
||||
buildkitd-config: hack/buildkitd.toml
|
||||
@@ -315,17 +319,16 @@ jobs:
|
||||
# The arm64 leg skips the talos package, so matchbox gets no arm64
|
||||
# half to stitch. Its Dockerfile only copies files, so both arches
|
||||
# build here without QEMU and the stamped ref pins the index.
|
||||
make -C packages/core/talos image-matchbox PLATFORM=linux/amd64,linux/arm64 BUILDER="$MATCHBOX_BUILDER"
|
||||
make -C packages/core/talos image-matchbox PLATFORM=linux/amd64,linux/arm64
|
||||
env:
|
||||
MATCHBOX_BUILDER: ${{ steps.matchbox_buildx.outputs.name }}
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
BUILDER: ${{ steps.buildx.outputs.name }}
|
||||
SBOM: '1'
|
||||
# Cache manifests are registry-portable (oci-mediatypes=true) — read
|
||||
# them from OCIR while pushing images to GHCR. A missing :buildcache
|
||||
# ref 404s harmlessly into a cold build.
|
||||
CACHE_REGISTRY: iad.ocir.io/idyksih5sir9/cozystack
|
||||
# amd64 here; build-arm64 builds arm64 natively. This job also
|
||||
# builds on the default docker driver, which handles a
|
||||
# multi-platform image only on the containerd image store.
|
||||
# amd64 here; build-arm64 builds arm64 natively.
|
||||
PLATFORM: linux/amd64
|
||||
# Push the release tag as :IMAGE_TAG (vX.Y.Z-rc.N) and the component
|
||||
# versions as :<component-ver>. Deliberately NO PUBLISH_FLOATING: this
|
||||
@@ -356,7 +359,7 @@ jobs:
|
||||
|
||||
- name: Upload the stamped tree and the pushed tags
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-tree
|
||||
path: ${{ runner.temp }}/release-tree/
|
||||
@@ -369,7 +372,7 @@ jobs:
|
||||
# talos image and are not release assets.
|
||||
- name: Upload the assets
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-assets
|
||||
path: |
|
||||
@@ -404,7 +407,7 @@ jobs:
|
||||
- name: Generate GitHub App token
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -412,10 +415,11 @@ jobs:
|
||||
|
||||
- name: Checkout code
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
persist-credentials: false
|
||||
|
||||
# The stitch and the multi-arch gate read the registry with skopeo and
|
||||
# the tree with mikefarah yq; the republish needs flux and helm.
|
||||
@@ -434,7 +438,7 @@ jobs:
|
||||
|
||||
- name: Login to GHCR
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
with:
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -444,14 +448,14 @@ jobs:
|
||||
|
||||
- name: Download the amd64 build
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: release-tree
|
||||
path: ${{ runner.temp }}/release-tree
|
||||
|
||||
- name: Download the assets
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: release-assets
|
||||
path: _out/assets
|
||||
@@ -488,7 +492,6 @@ jobs:
|
||||
git config user.name "cozystack-ci[bot]"
|
||||
git config user.email "274107086+cozystack-ci[bot]@users.noreply.github.com"
|
||||
git remote set-url origin https://x-access-token:${APP_TOKEN}@github.com/${GITHUB_REPOSITORY}
|
||||
git config --unset-all http.https://github.com/.extraheader || true
|
||||
git add .
|
||||
git commit -m "Prepare release ${GITHUB_REF#refs/tags/}" -s || echo "No changes to commit"
|
||||
|
||||
@@ -503,7 +506,7 @@ jobs:
|
||||
- name: Create / reuse draft release
|
||||
if: needs.build-amd64.outputs.release_exists == 'false'
|
||||
id: release
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const tag = '${{ needs.build-amd64.outputs.tag }}';
|
||||
@@ -553,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
|
||||
@@ -577,7 +580,7 @@ jobs:
|
||||
# draft status. prerelease:true keeps it off the "Latest release" pointer.
|
||||
- name: Publish rc release
|
||||
if: needs.build-amd64.outputs.release_exists == 'false' && needs.build-amd64.outputs.is_rc == 'true'
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -704,7 +707,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -716,7 +719,7 @@ jobs:
|
||||
# regardless of whether the agent follows the prompt's instructions.
|
||||
- name: Generate read-only GitHub App token
|
||||
id: app-token-read
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -727,7 +730,7 @@ jobs:
|
||||
|
||||
- name: Parse tag
|
||||
id: tag
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const ref = context.ref.replace('refs/tags/', '');
|
||||
@@ -748,7 +751,7 @@ jobs:
|
||||
# contents. The Create changelog branch step below resets the file
|
||||
# onto a fresh branch from origin/main for the PR.
|
||||
- name: Checkout release tag
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ steps.tag.outputs.tag }}
|
||||
fetch-depth: 0
|
||||
@@ -805,7 +808,7 @@ jobs:
|
||||
|
||||
- name: Setup Node.js
|
||||
if: steps.check_changelog.outputs.exists == 'false' && steps.check_changelog.outputs.at_tag == 'false'
|
||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: 22
|
||||
|
||||
@@ -878,7 +881,7 @@ jobs:
|
||||
|
||||
- name: Create PR for changelog
|
||||
if: steps.check_changelog.outputs.exists == 'false'
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
github-token: ${{ steps.app-token.outputs.token }}
|
||||
script: |
|
||||
@@ -962,7 +965,7 @@ jobs:
|
||||
steps:
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
@@ -970,7 +973,7 @@ jobs:
|
||||
|
||||
- name: Parse tag
|
||||
id: tag
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const ref = context.ref.replace('refs/tags/', '');
|
||||
@@ -984,7 +987,7 @@ jobs:
|
||||
core.setOutput('version', version); // 0.22.0
|
||||
|
||||
- name: Checkout website repo
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: cozystack/website
|
||||
token: ${{ steps.app-token.outputs.token }}
|
||||
|
||||
@@ -28,23 +28,23 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
# No git pushes from this workflow; don't leave the token in .git/config
|
||||
# where a later `pnpm install` step could read it.
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up pnpm
|
||||
uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
|
||||
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
||||
with:
|
||||
# The monorepo root has no package.json; read the pnpm version from
|
||||
# the console workspace manifest (packageManager field).
|
||||
package_json_file: packages/system/dashboard/images/console/package.json
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: 22
|
||||
node-version: 24
|
||||
cache: pnpm
|
||||
cache-dependency-path: packages/system/dashboard/images/console/pnpm-lock.yaml
|
||||
|
||||
|
||||
@@ -9,9 +9,12 @@ on:
|
||||
# (tags.yaml generate-changelog) merges or when a maintainer edits an
|
||||
# existing changelog. The v* glob deliberately skips template.md /
|
||||
# patch-template.md in the same directory. Every other main push left this
|
||||
# job scanning the latest 30 releases for nothing.
|
||||
# job scanning the latest 30 releases for nothing. A docs/releasenotes/v*.md
|
||||
# file, when it has content, is the body in place of its changelog, so it
|
||||
# triggers the sync too.
|
||||
paths:
|
||||
- 'docs/changelogs/v*.md'
|
||||
- 'docs/releasenotes/v*.md'
|
||||
# Manual re-sync hatch: re-run the changelog -> release-body sync without
|
||||
# touching a changelog file (e.g. if a release body drifted out of sync).
|
||||
workflow_dispatch:
|
||||
@@ -35,18 +38,20 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Update release notes from changelogs
|
||||
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const changelogDir = 'docs/changelogs';
|
||||
// Same limit and reason as MAX_BODY_BYTES in pull-requests-release.yaml.
|
||||
const MAX_BODY_BYTES = 100000;
|
||||
|
||||
// Get releases from first page
|
||||
const releases = await github.rest.repos.listReleases({
|
||||
@@ -61,7 +66,12 @@ jobs:
|
||||
for (const release of releases.data) {
|
||||
const tag = release.tag_name;
|
||||
const changelogFile = `${tag}.md`;
|
||||
const changelogPath = path.join(changelogDir, changelogFile);
|
||||
// docs/releasenotes/<tag>.md is the body when it has content, as
|
||||
// in finalize; the changelog otherwise.
|
||||
const notesPath = path.join('docs/releasenotes', changelogFile);
|
||||
const changelogPath = fs.existsSync(notesPath) && fs.readFileSync(notesPath, 'utf8').trim()
|
||||
? notesPath
|
||||
: path.join(changelogDir, changelogFile);
|
||||
|
||||
console.log(`\nProcessing release: ${tag}`);
|
||||
|
||||
@@ -85,6 +95,16 @@ jobs:
|
||||
continue;
|
||||
}
|
||||
|
||||
// Finalize publishes a link in place of a body this long. Never
|
||||
// here: a changelog that reaches main without its release notes
|
||||
// (the tags.yaml backstop ports only the changelog) would replace
|
||||
// a good published body with it.
|
||||
const bytes = Buffer.byteLength(changelogContent, 'utf8');
|
||||
if (bytes > MAX_BODY_BYTES) {
|
||||
console.log(`::warning::${changelogPath} is ${bytes} bytes, over MAX_BODY_BYTES (${MAX_BODY_BYTES}); leaving the body of release ${tag} as it is.`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if content is already up to date
|
||||
const currentBody = release.body || '';
|
||||
if (currentBody.trim() === changelogContent.trim()) {
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
|
||||
+2
-1
@@ -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
|
||||
|
||||
@@ -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. |
|
||||
@@ -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)
|
||||
@@ -28,11 +32,11 @@ This file provides structured guidance for AI coding assistants and agents worki
|
||||
|
||||
- **Go code, Helm charts, build and test commands, project structure** (e.g., "add a field to this CRD", "fix this controller", "add a value to the postgres chart", "regenerate the values schema", "how do I build/test this", "where should I put X", "what's the convention for Y", "how is the project organized")
|
||||
- Read: [`overview.md`](./docs/agents/overview.md)
|
||||
- Action: Read the section that matches — `Go Code` (controller-runtime patterns, kubebuilder markers), `Helm Charts` (umbrella pattern, vendored upstream charts), `Values schema generation`, `Build and Development Commands`, `Testing`, `Package Structure`
|
||||
- Action: Read the section that matches — `Go Code` (controller-runtime patterns, kubebuilder markers), `Helm Charts` (umbrella pattern, vendored upstream charts, which `lookup` is allowed), `Values schema generation`, `Build and Development Commands`, `Testing`, `Package Structure`
|
||||
|
||||
- **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
|
||||
|
||||
@@ -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]`
|
||||
|
||||
@@ -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,10 @@ 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.
|
||||
# ./internal/... runs in test-controllers; ./test/... and cmd/backport-batch
|
||||
# run in targets of their own.
|
||||
go-unit-tests:
|
||||
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/...
|
||||
go test -count=1 ./pkg/... $(patsubst %,./%/...,$(filter-out cmd/backport-batch,$(wildcard cmd/*)))
|
||||
|
||||
# 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 +155,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 +190,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 +293,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
@@ -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
|
||||
|
||||
|
||||
Generated
+3
@@ -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"`
|
||||
|
||||
Generated
+6
-3
@@ -17,6 +17,9 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigSpec struct {
|
||||
// FoundationDB major.minor version to deploy
|
||||
// +kubebuilder:default:="v7.3"
|
||||
Version Version `json:"version"`
|
||||
// Cluster configuration.
|
||||
// +kubebuilder:default:={}
|
||||
Cluster Cluster `json:"cluster"`
|
||||
@@ -98,9 +101,6 @@ type Cluster struct {
|
||||
// Storage engine (ssd-2, ssd-redwood-v1, ssd-rocksdb-v1, memory).
|
||||
// +kubebuilder:default:="ssd-2"
|
||||
StorageEngine string `json:"storageEngine"`
|
||||
// Version of FoundationDB to use.
|
||||
// +kubebuilder:default:="7.3.63"
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type ClusterFaultDomain struct {
|
||||
@@ -161,3 +161,6 @@ type ImageType 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
|
||||
|
||||
// +kubebuilder:validation:Enum="v7.4";"v7.3"
|
||||
type Version string
|
||||
@@ -1,26 +1,25 @@
|
||||
module github.com/cozystack/cozystack/api/apps/v1alpha1
|
||||
|
||||
go 1.26.4
|
||||
go 1.27.2
|
||||
|
||||
require k8s.io/apimachinery v0.36.0
|
||||
|
||||
require (
|
||||
github.com/fxamacker/cbor/v2 v2.9.4 // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.9.6 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/stretchr/testify v1.12.1 // indirect
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.4 // indirect
|
||||
golang.org/x/net v0.59.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
golang.org/x/net v0.61.0 // indirect
|
||||
golang.org/x/text v0.43.0 // indirect
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
k8s.io/klog/v2 v2.140.0 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a // indirect
|
||||
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2 // indirect
|
||||
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20260721132016-d427ff9ee9ad // indirect
|
||||
k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3 // indirect
|
||||
sigs.k8s.io/json v0.0.0-20260909141634-11ed52e25bc5 // indirect
|
||||
sigs.k8s.io/randfill v1.0.0 // indirect
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.2 // indirect
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.4.2 // indirect
|
||||
)
|
||||
+14
-14
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fxamacker/cbor/v2 v2.9.4 h1:xwjVlxEMR3S605oUlgBjKLTTeGFciYPGYCtF/35LKGo=
|
||||
github.com/fxamacker/cbor/v2 v2.9.4/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
|
||||
github.com/fxamacker/cbor/v2 v2.9.6 h1:NHZPxuUjJXQjX9itA785y/5NKSlI4HQ+S7noZsmI7hs=
|
||||
github.com/fxamacker/cbor/v2 v2.9.6/go.mod h1:4EaZp6ClVunETjQ4GHmGNzysDK91Lr+r6Yz6tJqzVCw=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
@@ -32,25 +32,25 @@ go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
|
||||
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
golang.org/x/net v0.61.0 h1:RR+6j/BTBCrggOLugxa+66R8FvoN6MHlzDlZIbN9YFw=
|
||||
golang.org/x/net v0.61.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||
k8s.io/apimachinery v0.36.0 h1:jZyPzhd5Z+3h9vJLt0z9XdzW9VzNzWAUw+P1xZ9PXtQ=
|
||||
k8s.io/apimachinery v0.36.0/go.mod h1:FklypaRJt6n5wUIwWXIP6GJlIpUizTgfo1T/As+Tyxc=
|
||||
k8s.io/klog/v2 v2.140.0 h1:Tf+J3AH7xnUzZyVVXhTgGhEKnFqye14aadWv7bzXdzc=
|
||||
k8s.io/klog/v2 v2.140.0/go.mod h1:o+/RWfJ6PwpnFn7OyAG3QnO47BFsymfEfrz6XyYSSp0=
|
||||
k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a h1:xCeOEAOoGYl2jnJoHkC3hkbPJgdATINPMAxaynU2Ovg=
|
||||
k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a/go.mod h1:uGBT7iTA6c6MvqUvSXIaYZo9ukscABYi2btjhvgKGZ0=
|
||||
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2 h1:AZYQSJemyQB5eRxqcPky+/7EdBj0xi3g0ZcxxJ7vbWU=
|
||||
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2/go.mod h1:xDxuJ0whA3d0I4mf/C4ppKHxXynQ+fxnkmQH0vTHnuk=
|
||||
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 h1:IpInykpT6ceI+QxKBbEflcR5EXP7sU1kvOlxwZh5txg=
|
||||
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
|
||||
k8s.io/kube-openapi v0.0.0-20260721132016-d427ff9ee9ad h1:oXImqH8mQNk7PmvzKhmN3ddJoY6OnyM225MXwGHPm0A=
|
||||
k8s.io/kube-openapi v0.0.0-20260721132016-d427ff9ee9ad/go.mod h1:0/mqHCVhlumdJ3BhCfnjSZQE037nAhNodh1/hK0T8/I=
|
||||
k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3 h1:jVkFFVfXdXP74B/zbO3hM3hpSFD0xvhQ5U686DPurkE=
|
||||
k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3/go.mod h1:M2s5JB1lIYP3jzZdorPLHXIPJzt9vv2muW5a6L9DtNM=
|
||||
sigs.k8s.io/json v0.0.0-20260909141634-11ed52e25bc5 h1:k9jo7ED/fsfOcz3L1P20P5tjTNtsdQNwMOG2aKzHrl8=
|
||||
sigs.k8s.io/json v0.0.0-20260909141634-11ed52e25bc5/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
|
||||
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=
|
||||
sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.2 h1:kwVWMx5yS1CrnFWA/2QHyRVJ8jM6dBA80uLmm0wJkk8=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.2/go.mod h1:M3W8sfWvn2HhQDIbGWj3S099YozAsymCo/wrT5ohRUE=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.4.2 h1:qdOxHwrl2Kaag1aQEarlYcOA9vSyGCp3CIki3aW8c4Q=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.4.2/go.mod h1:M3W8sfWvn2HhQDIbGWj3S099YozAsymCo/wrT5ohRUE=
|
||||
sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs=
|
||||
sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4=
|
||||
Generated
+4
-4
@@ -24,8 +24,8 @@ type ConfigSpec struct {
|
||||
// TLS configuration. Strimzi manages the cluster PKI automatically (no cert-manager is involved for this chart): the operator auto-creates `<release>-cluster-ca-cert` and `<release>-clients-ca-cert` secrets, both exposed for client trust setup. The internal TLS listener on 9093 is always on; this toggle only controls the external listener on 9094.
|
||||
// +kubebuilder:default:={}
|
||||
Tls TLS `json:"tls"`
|
||||
// Kafka version to deploy. Upgrade-only: once a cluster's KRaft metadata is at a given version, Strimzi refuses an in-place downgrade, so lowering this on a running cluster (e.g. v3.9 back to v3.8) leaves the Kafka CR stuck in a reconcile error. Pick the target version at creation and only ever raise it.
|
||||
// +kubebuilder:default:="v3.9"
|
||||
// Kafka version to deploy. Upgrade-only: once a cluster's KRaft metadata is at a given version, Strimzi refuses an in-place downgrade, so lowering this on a running cluster (e.g. v4.3 back to v4.2) leaves the Kafka CR stuck in a reconcile error. Pick the target version at creation and only ever raise it.
|
||||
// +kubebuilder:default:="v4.3"
|
||||
Version Version `json:"version"`
|
||||
// Topics configuration.
|
||||
// +kubebuilder:default:={}
|
||||
@@ -36,7 +36,7 @@ type ConfigSpec struct {
|
||||
}
|
||||
|
||||
type Kafka struct {
|
||||
// Persistent Volume size for KRaft controller metadata (used during ZK-to-KRaft migration).
|
||||
// Persistent Volume size for KRaft controller metadata.
|
||||
// +kubebuilder:default:="5Gi"
|
||||
ControllerStorageSize resource.Quantity `json:"controllerStorageSize,omitempty"`
|
||||
// Number of Kafka replicas.
|
||||
@@ -83,5 +83,5 @@ type Topic struct {
|
||||
// +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
|
||||
|
||||
// +kubebuilder:validation:Enum="v3.9";"v3.8"
|
||||
// +kubebuilder:validation:Enum="v4.3";"v4.2"
|
||||
type Version string
|
||||
Generated
+3
-39
@@ -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,12 +41,6 @@ type ConfigSpec struct {
|
||||
// Kubernetes control-plane configuration.
|
||||
// +kubebuilder:default:={}
|
||||
ControlPlane ControlPlane `json:"controlPlane"`
|
||||
// Optional image overrides for air-gapped or rate-limited registries.
|
||||
// +kubebuilder:default:={}
|
||||
Images Images `json:"images"`
|
||||
// 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"`
|
||||
@@ -56,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.
|
||||
@@ -184,18 +178,6 @@ type HAMiAddon struct {
|
||||
ValuesOverride k8sRuntime.RawExtension `json:"valuesOverride"`
|
||||
}
|
||||
|
||||
type Images struct {
|
||||
// Image used by the bootstrap-token tenant Job (kubectl). Empty falls back to images/kubectl.tag.
|
||||
// +kubebuilder:default:=""
|
||||
Kubectl string `json:"kubectl,omitempty"`
|
||||
// Image used by the talos-csr-signer sidecar in the Kamaji control plane. Empty falls back to images/talos-csr-signer.tag.
|
||||
// +kubebuilder:default:=""
|
||||
TalosCsrSigner string `json:"talosCsrSigner,omitempty"`
|
||||
// Image used by the wait-for-kubeconfig init container. Empty falls back to images/busybox.tag.
|
||||
// +kubebuilder:default:=""
|
||||
WaitForKubeconfig string `json:"waitForKubeconfig,omitempty"`
|
||||
}
|
||||
|
||||
type IngressNginxAddon struct {
|
||||
// Enable the controller (requires nodes labeled `ingress-nginx`).
|
||||
// +kubebuilder:default:=false
|
||||
@@ -364,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
|
||||
|
||||
@@ -154,8 +154,6 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
in.Proxmox.DeepCopyInto(&out.Proxmox)
|
||||
in.Addons.DeepCopyInto(&out.Addons)
|
||||
in.ControlPlane.DeepCopyInto(&out.ControlPlane)
|
||||
out.Images = in.Images
|
||||
in.Talos.DeepCopyInto(&out.Talos)
|
||||
in.Oidc.DeepCopyInto(&out.Oidc)
|
||||
}
|
||||
|
||||
@@ -268,21 +266,6 @@ func (in *HAMiAddon) DeepCopy() *HAMiAddon {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Images) DeepCopyInto(out *Images) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Images.
|
||||
func (in *Images) DeepCopy() *Images {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Images)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *IngressNginxAddon) DeepCopyInto(out *IngressNginxAddon) {
|
||||
*out = *in
|
||||
@@ -586,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
|
||||
|
||||
+31
-33
@@ -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, CDI picks the class: the one annotated `storageclass.kubevirt.io/is-default-virt-class`, else the cluster default. 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: the chart refuses a pool whose class differs from its golden's, because across classes CDI either falls back to a host-assisted copy over the pod network (different CSI drivers or volume modes) or leaves a clone into the other class to the CSI driver. Leaving it empty makes the same comparison against the class CDI would pick. 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,28 +32,32 @@ 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.
|
||||
// +kubebuilder:default:={}
|
||||
Roles []string `json:"roles,omitempty"`
|
||||
// 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.
|
||||
// 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.
|
||||
@@ -90,9 +94,6 @@ type ConfigSpec struct {
|
||||
// Proxmox substrate settings.
|
||||
// +kubebuilder:default:={}
|
||||
Proxmox Proxmox `json:"proxmox"`
|
||||
// Optional image overrides for air-gapped or rate-limited registries.
|
||||
// +kubebuilder:default:={}
|
||||
Images Images `json:"images"`
|
||||
}
|
||||
|
||||
type GPU struct {
|
||||
@@ -100,12 +101,6 @@ type GPU struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Images struct {
|
||||
// Image used by the talos-reconcile and pre-delete unpin Jobs (kubectl). Empty falls back to images/kubectl.tag.
|
||||
// +kubebuilder:default:=""
|
||||
Kubectl string `json:"kubectl,omitempty"`
|
||||
}
|
||||
|
||||
type KernelModule struct {
|
||||
// Module name as `modprobe` takes it, e.g. `nvidia_uvm`.
|
||||
Name string `json:"name"`
|
||||
@@ -120,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"`
|
||||
}
|
||||
|
||||
@@ -169,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.
|
||||
@@ -184,7 +179,10 @@ 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.
|
||||
// Sysctl overrides rendered into Talos `machine.sysctls` on this pool's guest workers only. Values must be strings (quote numeric values); for example, `user.max_user_namespaces: "11255"` enables the user namespaces required by the Talos gVisor extension. An empty map preserves Talos defaults and the existing bootstrap hash. Adding, changing or removing an override changes the content-hashed TalosConfigTemplate and rolls the pool's workers. Supported names and values depend on the worker's Talos release and kernel; unsafe settings can weaken guest isolation or prevent workers from booting. This does not install gVisor or configure a RuntimeClass, and does not change the management hosts, control plane or other pools.
|
||||
// +kubebuilder:default:={}
|
||||
Sysctls map[string]string `json:"sysctls,omitempty"`
|
||||
// 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"`
|
||||
}
|
||||
@@ -204,7 +202,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"`
|
||||
|
||||
+12
-16
@@ -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))
|
||||
@@ -78,7 +83,6 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
in.OsImage.DeepCopyInto(&out.OsImage)
|
||||
in.Talos.DeepCopyInto(&out.Talos)
|
||||
in.Proxmox.DeepCopyInto(&out.Proxmox)
|
||||
out.Images = in.Images
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ConfigSpec.
|
||||
@@ -106,21 +110,6 @@ func (in *GPU) DeepCopy() *GPU {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Images) DeepCopyInto(out *Images) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Images.
|
||||
func (in *Images) DeepCopy() *Images {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Images)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *KernelModule) DeepCopyInto(out *KernelModule) {
|
||||
*out = *in
|
||||
@@ -218,6 +207,13 @@ func (in *Resources) DeepCopy() *Resources {
|
||||
func (in *Talos) DeepCopyInto(out *Talos) {
|
||||
*out = *in
|
||||
in.RegistryMirrors.DeepCopyInto(&out.RegistryMirrors)
|
||||
if in.Sysctls != nil {
|
||||
in, out := &in.Sysctls, &out.Sysctls
|
||||
*out = make(map[string]string, len(*in))
|
||||
for key, val := range *in {
|
||||
(*out)[key] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Talos.
|
||||
|
||||
Generated
+1
-1
@@ -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"`
|
||||
}
|
||||
|
||||
Generated
-9
@@ -45,9 +45,6 @@ type ConfigSpec struct {
|
||||
// HTTP-layer TLS configuration. Selects who issues the HTTP server certificate; TLS itself is always served.
|
||||
// +kubebuilder:default:={}
|
||||
Tls TLS `json:"tls"`
|
||||
// Container images used by the operator.
|
||||
// +kubebuilder:default:={}
|
||||
Images Images `json:"images"`
|
||||
// Node roles configuration.
|
||||
// +kubebuilder:default:={}
|
||||
NodeRoles NodeRoles `json:"nodeRoles"`
|
||||
@@ -74,12 +71,6 @@ type Dashboards struct {
|
||||
ResourcesPreset ResourcesPreset `json:"resourcesPreset"`
|
||||
}
|
||||
|
||||
type Images struct {
|
||||
// OpenSearch image.
|
||||
// +kubebuilder:default:=""
|
||||
Opensearch string `json:"opensearch"`
|
||||
}
|
||||
|
||||
type NodeRoles struct {
|
||||
// Enable data role.
|
||||
// +kubebuilder:default:=true
|
||||
|
||||
@@ -56,7 +56,6 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
in.Resources.DeepCopyInto(&out.Resources)
|
||||
out.Size = in.Size.DeepCopy()
|
||||
out.Tls = in.Tls
|
||||
out.Images = in.Images
|
||||
out.NodeRoles = in.NodeRoles
|
||||
if in.Users != nil {
|
||||
in, out := &in.Users, &out.Users
|
||||
@@ -94,21 +93,6 @@ func (in *Dashboards) DeepCopy() *Dashboards {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Images) DeepCopyInto(out *Images) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Images.
|
||||
func (in *Images) DeepCopy() *Images {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Images)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *NodeRoles) DeepCopyInto(out *NodeRoles) {
|
||||
*out = *in
|
||||
|
||||
Generated
+2
-2
@@ -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>/`.
|
||||
|
||||
Generated
+4
@@ -7,6 +7,7 @@ package rabbitmq
|
||||
import (
|
||||
resource "k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// +kubebuilder:object:root=true
|
||||
@@ -36,6 +37,9 @@ type ConfigSpec struct {
|
||||
// Enable external access from outside the cluster.
|
||||
// +kubebuilder:default:=false
|
||||
External bool `json:"external"`
|
||||
// Pod affinity applied to RabbitMQ pods (standard Kubernetes corev1.Affinity). Use podAntiAffinity with topologyKey `kubernetes.io/hostname` to spread RabbitMQ pods across nodes.
|
||||
// +kubebuilder:default:={}
|
||||
Affinity *k8sRuntime.RawExtension `json:"affinity,omitempty"`
|
||||
// TLS configuration. TLS is off unless `tls.enabled` is set to true.
|
||||
// +kubebuilder:default:={}
|
||||
Tls TLS `json:"tls,omitempty"`
|
||||
|
||||
+6
-1
@@ -21,7 +21,7 @@ limitations under the License.
|
||||
package rabbitmq
|
||||
|
||||
import (
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
@@ -55,6 +55,11 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
*out = *in
|
||||
in.Resources.DeepCopyInto(&out.Resources)
|
||||
out.Size = in.Size.DeepCopy()
|
||||
if in.Affinity != nil {
|
||||
in, out := &in.Affinity, &out.Affinity
|
||||
*out = new(runtime.RawExtension)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
out.Tls = in.Tls
|
||||
if in.Users != nil {
|
||||
in, out := &in.Users, &out.Users
|
||||
|
||||
Generated
+26
-2
@@ -7,6 +7,7 @@ package redis
|
||||
import (
|
||||
resource "k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// +kubebuilder:object:root=true
|
||||
@@ -17,7 +18,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.
|
||||
@@ -26,6 +27,9 @@ type ConfigSpec struct {
|
||||
// Default sizing preset used when `resources` is omitted.
|
||||
// +kubebuilder:default:="t1.nano"
|
||||
ResourcesPreset ResourcesPreset `json:"resourcesPreset"`
|
||||
// Kubernetes affinity configuration for Redis and Sentinel pods.
|
||||
// +kubebuilder:default:={}
|
||||
Affinity AffinitySpec `json:"affinity,omitempty"`
|
||||
// Persistent Volume Claim size available for application data.
|
||||
// +kubebuilder:default:="1Gi"
|
||||
Size resource.Quantity `json:"size"`
|
||||
@@ -51,11 +55,28 @@ 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"`
|
||||
}
|
||||
|
||||
type AffinitySpec struct {
|
||||
// Kubernetes Affinity for Redis pods. When set, redis-operator uses it verbatim and does not add its default soft podAntiAffinity; include podAntiAffinity unless replica co-location is intended.
|
||||
// +kubebuilder:default:={}
|
||||
Redis *k8sRuntime.RawExtension `json:"redis,omitempty"`
|
||||
// Kubernetes Affinity for Sentinel pods. When set, redis-operator uses it verbatim and does not add its default soft podAntiAffinity; include podAntiAffinity unless replica co-location is intended.
|
||||
// +kubebuilder:default:={}
|
||||
Sentinel *k8sRuntime.RawExtension `json:"sentinel,omitempty"`
|
||||
}
|
||||
|
||||
type Resources struct {
|
||||
// CPU available to each replica.
|
||||
Cpu resource.Quantity `json:"cpu,omitempty"`
|
||||
@@ -80,6 +101,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
|
||||
|
||||
|
||||
+28
-1
@@ -21,9 +21,34 @@ limitations under the License.
|
||||
package redis
|
||||
|
||||
import (
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *AffinitySpec) DeepCopyInto(out *AffinitySpec) {
|
||||
*out = *in
|
||||
if in.Redis != nil {
|
||||
in, out := &in.Redis, &out.Redis
|
||||
*out = new(runtime.RawExtension)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
if in.Sentinel != nil {
|
||||
in, out := &in.Sentinel, &out.Sentinel
|
||||
*out = new(runtime.RawExtension)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AffinitySpec.
|
||||
func (in *AffinitySpec) DeepCopy() *AffinitySpec {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(AffinitySpec)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Config) DeepCopyInto(out *Config) {
|
||||
*out = *in
|
||||
@@ -54,6 +79,7 @@ func (in *Config) DeepCopyObject() runtime.Object {
|
||||
func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
*out = *in
|
||||
in.Resources.DeepCopyInto(&out.Resources)
|
||||
in.Affinity.DeepCopyInto(&out.Affinity)
|
||||
out.Size = in.Size.DeepCopy()
|
||||
in.Tls.DeepCopyInto(&out.Tls)
|
||||
if in.MinReplicasToWrite != nil {
|
||||
@@ -66,6 +92,7 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
*out = new(int)
|
||||
**out = **in
|
||||
}
|
||||
out.Maxmemory = in.Maxmemory.DeepCopy()
|
||||
in.Sentinel.DeepCopyInto(&out.Sentinel)
|
||||
}
|
||||
|
||||
|
||||
Generated
+3
@@ -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
@@ -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
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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"`
|
||||
|
||||
|
||||
@@ -120,10 +120,18 @@ type ApplicationDefinitionRelease struct {
|
||||
// kstatus rules instead.
|
||||
// A CR whose CRD is not installed fails the Helm action before any wait,
|
||||
// whatever these expressions say.
|
||||
// Upstream evaluates these only when the Helm action itself has wait
|
||||
// enabled, so the release.cozystack.io/helm-install-disable-wait annotation
|
||||
// makes them a silent no-op whatever waitStrategy says: the HelmRelease
|
||||
// reports Ready as soon as helm applies the CR, with no error or warning.
|
||||
// helm-controller parses and validates these expressions and creates a
|
||||
// status reader factory before choosing how to wait. The factory constructs
|
||||
// the concrete status reader when the selected wait path creates its
|
||||
// resource manager. Invalid configuration can
|
||||
// therefore stall the HelmRelease with reason InvalidCELExpression even when
|
||||
// the release.cozystack.io/helm-install-disable-wait annotation is set or the
|
||||
// legacy wait strategy is selected. Once setup succeeds, only helm-controller's
|
||||
// poller evaluates expressions: while a Helm action waits under the poller
|
||||
// strategy, and for Helm hooks, which are still waited on when the annotation
|
||||
// disables wait. Without the annotation, the legacy strategy never evaluates
|
||||
// them. With it, hooks are evaluated whatever the strategy, because disabling
|
||||
// wait takes precedence.
|
||||
// +optional
|
||||
HealthCheckExprs []kustomize.CustomHealthCheck `json:"healthCheckExprs,omitempty"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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.
|
||||
@@ -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
|
||||
}
|
||||
@@ -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}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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 @heidi & <b>",
|
||||
"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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 ""
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Loaded 100 of 1138 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user