mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
Merge branch 'main' into feat/platform-keycloak-truststores
Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
483 files changed
+53636
-7128
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:
|
||||
|
||||
|
||||
@@ -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,7 +8,7 @@
|
||||
- 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
|
||||
|
||||
+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
|
||||
@@ -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": [
|
||||
|
||||
@@ -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@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
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@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
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@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
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@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
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@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
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 }}
|
||||
@@ -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@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
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@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
|
||||
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@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: false
|
||||
|
||||
- name: Build backport-batch
|
||||
run: go build -o "$RUNNER_TEMP/backport-batch" ./cmd/backport-batch
|
||||
|
||||
- name: Generate GitHub App token
|
||||
id: app-token
|
||||
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
|
||||
with:
|
||||
app-id: ${{ secrets.COZYSTACK_CI_APP_ID }}
|
||||
private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }}
|
||||
owner: cozystack
|
||||
repositories: cozystack
|
||||
permission-contents: write
|
||||
permission-pull-requests: write
|
||||
|
||||
# git pushes through gh, which reads the token from the environment of
|
||||
# each call, so the token is never written into the checkout.
|
||||
- name: Use the App's bot as git identity and credential
|
||||
env:
|
||||
GH_TOKEN: ${{ steps.app-token.outputs.token }}
|
||||
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
|
||||
run: |
|
||||
gh auth setup-git
|
||||
id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)
|
||||
git config --global user.name "${APP_SLUG}[bot]"
|
||||
git config --global user.email "${id}+${APP_SLUG}[bot]@users.noreply.github.com"
|
||||
|
||||
- name: Run the batch
|
||||
env:
|
||||
BACKPORT_BATCH: ${{ runner.temp }}/backport-batch
|
||||
BACKPORT_BATCH_TOKEN: ${{ steps.app-token.outputs.token }}
|
||||
BACKPORT_BATCH_BOT: ${{ steps.app-token.outputs.app-slug }}[bot]
|
||||
BACKPORT_ALERT_TEAM: ${{ vars.BACKPORT_ALERT_TEAM }}
|
||||
run: hack/backport-batch-scheduled.sh --no-workflow-files
|
||||
@@ -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"
|
||||
}
|
||||
@@ -154,6 +154,7 @@ jobs:
|
||||
sparse-checkout: |
|
||||
hack
|
||||
docs/agents
|
||||
docs/release
|
||||
persist-credentials: false
|
||||
|
||||
# The agent reads docs/agents/changelog.md from the WORKING TREE, which is
|
||||
@@ -163,18 +164,17 @@ jobs:
|
||||
# detached rc-tag checkout is exactly that, so a COMPLIANT agent would
|
||||
# silently decline to generate on every promotion of an existing rc.
|
||||
# Overlay the current instructions so the agent reads today's contract.
|
||||
# changelog.md sends the agent to the checks in docs/release.md and to
|
||||
# hack/validate-changelog.sh, so those are overlaid too: the rc tag's
|
||||
# copies predate any check added since. docs/release.md reaches
|
||||
# .release-tooling only because the sparse checkout above is in cone
|
||||
# mode (the action's default), which carries the files of every parent
|
||||
# directory of docs/agents.
|
||||
# changelog.md sends the agent to the checks in docs/release/changelogs.md
|
||||
# and to hack/validate-changelog.sh, so those are overlaid too: the rc
|
||||
# tag's copies predate any check added since, and an rc cut before the
|
||||
# release docs were split into docs/release/ has no such directory.
|
||||
- name: Refresh agent instructions
|
||||
run: |
|
||||
cp .release-tooling/docs/agents/changelog.md docs/agents/changelog.md
|
||||
cp .release-tooling/docs/release.md docs/release.md
|
||||
mkdir -p docs/release
|
||||
cp .release-tooling/docs/release/changelogs.md docs/release/changelogs.md
|
||||
cp .release-tooling/hack/validate-changelog.sh hack/validate-changelog.sh
|
||||
echo "Refreshed docs/agents/changelog.md, docs/release.md and hack/validate-changelog.sh from ${GITHUB_REF_NAME}."
|
||||
echo "Refreshed docs/agents/changelog.md, docs/release/changelogs.md and hack/validate-changelog.sh from ${GITHUB_REF_NAME}."
|
||||
|
||||
# Skip-priority + validate-then-fall-through: use a pre-existing changelog
|
||||
# only if it is USABLE, so an invalid higher-priority source can never
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -543,7 +543,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
@@ -771,21 +771,6 @@ jobs:
|
||||
name: cozyreport
|
||||
path: _out/cozyreport.tgz
|
||||
if-no-files-found: warn
|
||||
# Both halves, and in this order: the compose project owns the nodes and
|
||||
# the zpools outlive it.
|
||||
- name: Tear down sandbox
|
||||
if: always()
|
||||
run: |
|
||||
# Annotated, not fatal: same reasoning as the in-tree lane. The runner
|
||||
# is discarded after the job, so a dirty host has no next tenant, and
|
||||
# failing here would post the required "E2E Tests" status as a failure
|
||||
# indistinguishable from a real one.
|
||||
cleanup_rc=0
|
||||
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete-cluster-container || cleanup_rc=$?
|
||||
make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME delete || true
|
||||
if [ "$cleanup_rc" -ne 0 ]; then
|
||||
echo "::error title=sandbox teardown::teardown exited $cleanup_rc; the host may hold a stale compose project or zpool"
|
||||
fi
|
||||
|
||||
# Flip the pending "E2E Tests" commit status posted by `resolve` to its
|
||||
# terminal state. Runs on failure too, so a failed publish or e2e turns the
|
||||
|
||||
@@ -91,7 +91,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 +160,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"
|
||||
@@ -137,7 +137,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
checks: write # Breakpoint action updates its dedicated "Breakpoint Open" Check Run.
|
||||
@@ -292,7 +292,7 @@ jobs:
|
||||
# collect-report packages cozyreport plus per-failed-test crust-gather
|
||||
# snapshots captured before Chainsaw cleanup.
|
||||
#
|
||||
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
|
||||
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
|
||||
# is the next step and the collector writes its tarball at the very end of
|
||||
# its run, so a read that never returns spends what is left of the job
|
||||
# budget, the job is cancelled, and the upload never runs -- the artifact
|
||||
|
||||
@@ -250,7 +250,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
@@ -385,7 +385,7 @@ jobs:
|
||||
path: _out/chainsaw-report.xml
|
||||
if-no-files-found: ignore
|
||||
|
||||
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
|
||||
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
|
||||
# is the next step and the collector writes its tarball at the very end of
|
||||
# its run, so a read that never returns spends what is left of the job
|
||||
# budget, the job is cancelled, and the upload never runs -- the artifact
|
||||
|
||||
@@ -24,7 +24,7 @@ jobs:
|
||||
with:
|
||||
script: |
|
||||
// Conventional Commits types accepted by Cozystack (per docs/agents/contributing.md):
|
||||
// feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
|
||||
// feat, fix, security, docs, style, refactor, perf, test, build, ci, chore, revert
|
||||
// Mapping below maps a subset to kind/* — types not listed do not produce a kind/*.
|
||||
const typeToKind = {
|
||||
feat: 'kind/feature',
|
||||
@@ -32,7 +32,7 @@ jobs:
|
||||
docs: 'kind/documentation',
|
||||
chore: 'kind/cleanup',
|
||||
refactor: 'kind/cleanup',
|
||||
// style, perf, test, build, ci, revert — no kind mapping
|
||||
// security, style, perf, test, build, ci, revert — no kind mapping
|
||||
};
|
||||
|
||||
// scope -> area/* mapping. Keys are the scopes observed in cozystack issues
|
||||
@@ -296,3 +296,82 @@ jobs:
|
||||
labels: newLabels,
|
||||
});
|
||||
core.info(`Applied labels: ${newLabels.join(', ')}`);
|
||||
|
||||
# Backports are automatic (docs/release/backports.md), so a pull request
|
||||
# against a release branch from outside the team is closed with a pointer to
|
||||
# the policy. Only when it is opened: a maintainer keeps a fix that exists
|
||||
# only on a release line by reopening it.
|
||||
#
|
||||
# pull_request_target hands a write token to a fork's pull request, so this
|
||||
# job never touches the head: actions/checkout takes the event's own ref,
|
||||
# which is the base repository, the script reads only the event JSON and the
|
||||
# author's permission on the repository, and no `${{ }}` expression reaches
|
||||
# the shell. The permission read needs repository metadata read access, which
|
||||
# a GITHUB_TOKEN always carries and `permissions:` has no key for.
|
||||
close-manual-backport:
|
||||
if: >-
|
||||
github.event.action == 'opened' &&
|
||||
startsWith(github.event.pull_request.base.ref, 'release-')
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
permissions:
|
||||
contents: read # check out hack/ from the base repository
|
||||
pull-requests: write # close the pull request with a comment
|
||||
steps:
|
||||
- name: Check out the base repository
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
sparse-checkout: hack
|
||||
- name: Close a backport opened by hand from outside the team
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: hack/close-manual-backport.sh "$GITHUB_EVENT_PATH"
|
||||
|
||||
# A batch pull request carries each original in the shape cmd/backport-batch
|
||||
# gives it. fold checks one resolution, and only when whoever resolved the
|
||||
# conflict runs it; this checks every original on the batch on each push.
|
||||
#
|
||||
# pull_request_target takes the workflow from main for a pull request into
|
||||
# any branch, so the check reaches release-* lines, which do not carry it; a
|
||||
# pull_request job would run the line's own workflows. The tool is built from
|
||||
# main, since the lines do not carry it either. Nothing from the pull request
|
||||
# is checked out or run: its head is fetched as data and read with git, and
|
||||
# no `${{ }}` expression reaches the shell. Only a branch of this repository
|
||||
# named like a batch is checked.
|
||||
backport-batch-verify:
|
||||
name: Backport batch shape
|
||||
if: >-
|
||||
startsWith(github.event.pull_request.base.ref, 'release-') &&
|
||||
startsWith(github.event.pull_request.head.ref, 'backport-batch/') &&
|
||||
github.event.pull_request.head.repo.full_name == github.repository
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
permissions:
|
||||
contents: read
|
||||
env:
|
||||
# The pipe falls back to direct on any proxy error (hack/common-envs.mk).
|
||||
GOPROXY: "https://proxy.golang.org|direct"
|
||||
steps:
|
||||
- name: Check out main
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
with:
|
||||
ref: main
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: false
|
||||
- name: Build backport-batch from main
|
||||
run: go build -o "$RUNNER_TEMP/backport-batch" ./cmd/backport-batch
|
||||
- name: Fetch the batch head as data
|
||||
env:
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
run: git fetch --no-tags --quiet origin "$HEAD_SHA"
|
||||
- name: Verify every original on the batch
|
||||
env:
|
||||
BASE_REF: ${{ github.event.pull_request.base.ref }}
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
run: '"$RUNNER_TEMP/backport-batch" verify --line "$BASE_REF" --head "$HEAD_SHA"'
|
||||
@@ -89,16 +89,14 @@ jobs:
|
||||
runs-on: ubuntu-22.04
|
||||
permissions:
|
||||
contents: read
|
||||
# A backport PR is the one shape whose author cannot fix a finding: the
|
||||
# backport action cherry-picks merged commits verbatim, so their messages
|
||||
# A backport batch is the one shape whose author cannot fix a finding:
|
||||
# cmd/backport-batch cherry-picks merged commits verbatim, so their messages
|
||||
# are history, and roughly a quarter of the commits on main predate the
|
||||
# rule. Matched on the branch name the action generates,
|
||||
# backport-<pr>-to-<branch>, rather than on the prefix alone; the label that
|
||||
# asks for a backport sits on the original PR, not on the generated one, so
|
||||
# a branch name is the only signal a pull_request event carries. That makes
|
||||
# the exemption a convention rather than something enforced: anyone can name
|
||||
# a branch this way. Every other PR, fork included, is checked.
|
||||
if: ${{ !(startsWith(github.head_ref, 'backport-') && contains(github.head_ref, '-to-')) }}
|
||||
# rule. Matched on the branch name the batch uses,
|
||||
# backport-batch/<line>-<date>, and only from a branch of this repository,
|
||||
# which takes write access to push: a fork can name a branch the same way.
|
||||
# Every other PR, fork included, is checked.
|
||||
if: ${{ !(startsWith(github.head_ref, 'backport-batch/') && github.event.pull_request.head.repo.full_name == github.repository) }}
|
||||
steps:
|
||||
- name: Checkout head
|
||||
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
|
||||
@@ -327,9 +327,12 @@ jobs:
|
||||
// A maintenance branch created before the candidate pipeline landed
|
||||
// would execute its old pull_request workflows after merge. Refuse
|
||||
// before the first registry write unless the actual target base has
|
||||
// both trusted verification gates and their helpers. Backport this
|
||||
// change to an existing release-X.Y line before promoting its next
|
||||
// patch; the rc-derived head does not need to carry the tooling.
|
||||
// both trusted verification gates and their helpers. An existing
|
||||
// release-X.Y line gets them before its next patch is promoted,
|
||||
// through the backport batch with backport/include on the pull
|
||||
// request that brought them to main, or through a maintainer's pull
|
||||
// request against the line; the rc-derived head does not need to
|
||||
// carry the tooling.
|
||||
//
|
||||
// Every file the base-checked-out verifier reaches for belongs in
|
||||
// the list below, its own sourced libraries included: those are
|
||||
@@ -363,7 +366,7 @@ jobs:
|
||||
});
|
||||
} catch (e) {
|
||||
if (e.status === 404) {
|
||||
core.setFailed(`Target base '${baseBranch}' lacks '${path}'. Backport the candidate-aware promotion pipeline before dispatch.`);
|
||||
core.setFailed(`Target base '${baseBranch}' lacks '${path}'. Bring the candidate-aware promotion pipeline to it before dispatch: backport/include on the pull request that added it to main, or a maintainer pull request against the line.`);
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
@@ -374,7 +377,7 @@ jobs:
|
||||
}
|
||||
const content = Buffer.from(file.data.content, 'base64').toString('utf8');
|
||||
if (!content.includes(marker)) {
|
||||
core.setFailed(`Target base '${baseBranch}' has an old '${path}'. Backport the candidate-aware promotion pipeline before dispatch.`);
|
||||
core.setFailed(`Target base '${baseBranch}' has an old '${path}'. Bring the candidate-aware promotion pipeline to it before dispatch: backport/include on the pull request that added it to main, or a maintainer pull request against the line.`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -603,10 +606,11 @@ jobs:
|
||||
tar -xzf "$syft_dir/syft.tar.gz" -C "$syft_dir" syft
|
||||
rm -f "$syft_dir/syft.tar.gz"
|
||||
echo "$syft_dir" >> "$GITHUB_PATH"
|
||||
# cosign is the version docs/release.md asks users for, so the job checks
|
||||
# its own signature the way they will. The action verifies that binary
|
||||
# against cosign's keyless and KMS bundles with its digest-pinned
|
||||
# bootstrap cosign. Bump with tags.yaml; the contract test holds them equal.
|
||||
# cosign is the version docs/release/signatures-and-sbom.md asks users
|
||||
# for, so the job checks its own signature the way they will. The action
|
||||
# verifies that binary against cosign's keyless and KMS bundles with its
|
||||
# digest-pinned bootstrap cosign. Bump with tags.yaml; the contract test
|
||||
# holds them equal.
|
||||
- name: Set up cosign
|
||||
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
|
||||
with:
|
||||
@@ -1044,9 +1048,10 @@ jobs:
|
||||
PR_STATE="$(gh pr view "${STABLE_BRANCH}" --json state --jq .state 2>/dev/null || echo "")"
|
||||
if [ "${PR_STATE}" = "OPEN" ]; then
|
||||
# Refresh only while the body is still the one this workflow wrote.
|
||||
# docs/release.md tells reviewers to paste upstream breaking-change
|
||||
# notes into this body before merge; a re-dispatch that overwrites it
|
||||
# would destroy that with no trace and no way to recover it.
|
||||
# docs/release/pre-release-checklist.md tells reviewers to paste
|
||||
# upstream breaking-change notes into this body before merge; a
|
||||
# re-dispatch that overwrites it would destroy that with no trace
|
||||
# and no way to recover it.
|
||||
CURRENT="$(gh pr view "${STABLE_BRANCH}" --json body --jq .body 2>/dev/null || echo "")"
|
||||
if [ "${CURRENT}" = "${BODY}" ]; then
|
||||
echo "PR already open for ${STABLE_BRANCH} with an identical body; nothing to do."
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -953,7 +953,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
@@ -1224,7 +1224,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 +1235,7 @@ jobs:
|
||||
# 30 minutes is measured against the step: it takes 3-42s across sampled
|
||||
# runs, on failed e2e jobs as well as green ones. It RESERVES nothing --
|
||||
# `timeout-minutes` caps a step, it does not carve budget out of the job,
|
||||
# so a suite that has already burned 210 of the 215 leaves this step five
|
||||
# so a suite that has already burned 260 of the 265 leaves this step five
|
||||
# minutes and the upload is cancelled with everything else. What the cap
|
||||
# buys is that a hung read cannot be the reason.
|
||||
# `continue-on-error` because the ceiling must not change what the job
|
||||
@@ -1348,28 +1348,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
|
||||
|
||||
@@ -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
|
||||
|
||||
+14
-11
@@ -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
|
||||
@@ -290,11 +291,14 @@ 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
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
|
||||
with:
|
||||
driver: docker-container
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
@@ -47,6 +50,8 @@ jobs:
|
||||
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()) {
|
||||
|
||||
+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
|
||||
|
||||
@@ -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,7 +32,7 @@ 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)
|
||||
|
||||
@@ -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 test-backport-batch migrations-target-check
|
||||
|
||||
helm-unit-tests:
|
||||
hack/helm-unit-tests.sh
|
||||
@@ -194,9 +195,9 @@ 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
|
||||
# ./cmd/... is excluded from go-unit-tests, so the batch tool needs its own target.
|
||||
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 +295,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
|
||||
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ type ConfigSpec struct {
|
||||
// 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.
|
||||
|
||||
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
+1
-1
@@ -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.
|
||||
|
||||
Generated
+2
-2
@@ -17,7 +17,7 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigSpec struct {
|
||||
// Number of Redis replicas.
|
||||
// Number of Redis replicas. Three Sentinels watch them, or a single one when `replicas` is 1, since a lone pod has no replica to fail over to.
|
||||
// +kubebuilder:default:=2
|
||||
Replicas int `json:"replicas"`
|
||||
// Explicit CPU and memory configuration for each Redis replica. When omitted, the preset defined in `resourcesPreset` is applied.
|
||||
@@ -51,7 +51,7 @@ 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.
|
||||
// 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"`
|
||||
}
|
||||
|
||||
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"`
|
||||
|
||||
|
||||
@@ -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,149 @@
|
||||
# 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.
|
||||
|
||||
### 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 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,962 @@
|
||||
/*
|
||||
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 := pickOriginal(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 {
|
||||
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
|
||||
}
|
||||
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)
|
||||
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 := 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 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 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 := 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 := replayUnit(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 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, mc5 := 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,622 @@
|
||||
/*
|
||||
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
|
||||
}
|
||||
|
||||
// botBatchPR is batchPR opened by the bot the scheduled batch runs as.
|
||||
func (f *fixture) botBatchPR(gh *fakeGH, n int, base, branch, body string) pullRequest {
|
||||
f.t.Helper()
|
||||
gh.bot = defaultBatchBot
|
||||
pr := f.batchPR(n, base, branch, body)
|
||||
pr.User.Login, pr.User.Type, pr.AuthorAssociation = defaultBatchBot, "Bot", "CONTRIBUTOR"
|
||||
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,400 @@
|
||||
/*
|
||||
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
|
||||
}
|
||||
|
||||
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/txt.exe", "app/zerowidth.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, "") {
|
||||
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/txt.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,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,538 @@
|
||||
/*
|
||||
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"
|
||||
"strings"
|
||||
"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
|
||||
}
|
||||
|
||||
func main() {
|
||||
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:]))
|
||||
}
|
||||
|
||||
func (a *app) main(args []string) int {
|
||||
a.stdout, a.stderr = textWriter{a.stdout}, textWriter{a.stderr}
|
||||
a.gh = &permCache{github: a.gh, perms: map[string]string{}}
|
||||
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 }
|
||||
|
||||
func (t textWriter) Write(p []byte) (int, error) {
|
||||
if _, err := io.WriteString(t.w, clean(string(p), true)); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// notifyMarker starts the one comment notify keeps on a batch pull request.
|
||||
const notifyMarker = "<!-- backport-batch:notify -->"
|
||||
|
||||
// stalledAfter is how many days a batch stays open before notify calls it
|
||||
// stalled: the shorter gap between two scheduled runs, Monday to Thursday, so
|
||||
// a batch the previous run opened or continued is stalled at the next one.
|
||||
const stalledAfter = 3
|
||||
|
||||
var mentionRE = regexp.MustCompile(`^@?([A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:/[A-Za-z0-9_.-]+)?)$`)
|
||||
|
||||
// notify reports what waits on the open batch pull request of a line for a
|
||||
// maintainer: a batch open since before the previous run, the conflicts it
|
||||
// deferred, and the originals that change workflows. One line per finding goes
|
||||
// to stdout, for the run's summary. --push writes them into the comment, one
|
||||
// per pull request, updated in place on every run.
|
||||
func (a *app) notify(args []string) int {
|
||||
fs := a.flags("notify")
|
||||
lineArg := fs.String("line", "", "release line, release-X.Y")
|
||||
mention := fs.String("mention", "", "team or person the comment mentions, org/team or login, with or without @")
|
||||
push := fs.Bool("push", false, "write the comment")
|
||||
if err := parse(fs, args); err != nil {
|
||||
return 2
|
||||
}
|
||||
l, err := parseLine(*lineArg)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if *mention != "" {
|
||||
m := mentionRE.FindStringSubmatch(*mention)
|
||||
if m == nil {
|
||||
return a.fail(fmt.Errorf("--mention %q is not @login or @org/team", *mention))
|
||||
}
|
||||
*mention = "@" + m[1]
|
||||
}
|
||||
prs, err := a.gh.openPulls(l.branch())
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
pr, warnings, err := batchPull(a.gh, prs, l)
|
||||
for _, w := range warnings {
|
||||
a.warn("%s", w)
|
||||
}
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
// With no batch open there is nothing to report on and nowhere to write,
|
||||
// but a record nobody can read is said all the same.
|
||||
src, err := findLineRecord(a.gh, l, pr)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if c := src.closingOpen(); c != nil {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d is being closed; backport-batch run --line %s --push finishes it", c.PR, l.branch()))
|
||||
}
|
||||
if pr == nil {
|
||||
return 0
|
||||
}
|
||||
opened, err := branchDate(pr.Head.Ref)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
deferred := map[int]deferredEntry{}
|
||||
var partial []int
|
||||
var parks []parkedEntry
|
||||
if src.open {
|
||||
deferred, partial = src.rec.deferred(), src.rec.Partial
|
||||
for _, p := range src.rec.Parked {
|
||||
if !p.Announced {
|
||||
parks = append(parks, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
// An original that waits on others is listed once, after what decides
|
||||
// its wait, where that one is listed.
|
||||
var conflicts, waiting []int
|
||||
after := map[int][]int{}
|
||||
for n, e := range deferred {
|
||||
switch place := placement(deferred, n, map[int]bool{}); {
|
||||
case place == "workflow":
|
||||
waiting = append(waiting, n)
|
||||
case place == "conflict":
|
||||
conflicts = append(conflicts, n)
|
||||
}
|
||||
if len(e.Needs) > 0 {
|
||||
after[n] = e.Needs
|
||||
}
|
||||
}
|
||||
slices.Sort(conflicts)
|
||||
slices.Sort(waiting)
|
||||
item := func(n int) string {
|
||||
if needs := after[n]; len(needs) > 0 {
|
||||
return fmt.Sprintf("\n- %s, after %s", prLink(n), prLinks(needs))
|
||||
}
|
||||
return "\n- " + prLink(n)
|
||||
}
|
||||
count := func(ns []int) int {
|
||||
c := 0
|
||||
for _, n := range ns {
|
||||
if len(after[n]) == 0 {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
today := a.now().UTC().Truncate(24 * time.Hour)
|
||||
days := int(today.Sub(opened).Hours() / 24)
|
||||
stalled := days >= stalledAfter
|
||||
var text []string
|
||||
if stalled {
|
||||
fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
|
||||
text = append(text, fmt.Sprintf("This batch has been open since %s, %d days, through at least one scheduled run. Merge it, or close it and the next run starts a new one; until then each run adds what the rules take to it.", opened.Format(time.DateOnly), days))
|
||||
}
|
||||
if len(partial) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d units on #%d carry only part of their originals\n", l.branch(), len(partial), pr.Number)
|
||||
var b strings.Builder
|
||||
b.WriteString("These are on the batch but carry only part of their originals on this line; take each out with `backport-batch run --defer` before the cut, and port what applies as a pull request of its own:\n")
|
||||
for _, n := range partial {
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(conflicts) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), count(conflicts), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These conflicts wait for a maintainer, as [resolving a conflict](%sresolve-conflict.md) describes; unresolved, each goes to the next batch, and so does what waits on one:\n", tasksURL)
|
||||
for _, n := range conflicts {
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(waiting) > 0 {
|
||||
if count(waiting) == len(waiting) {
|
||||
fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s or wait on one that does\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These change `%s`, which the batch's token may not push, and wait for a maintainer to bring them over, as [changes to workflows](%s#changes-to-workflows) describes, or wait on one that does:\n", workflowsDir, policyURL)
|
||||
for _, n := range waiting {
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(parks) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d parked originals on #%d, named once\n", l.branch(), len(parks), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These cannot land on this line whole, so the batch parked them; each is named here once. A maintainer ports what applies as a pull request of its own, as [fixes that exist only on a release line](%s#fixes-that-exist-only-on-a-release-line) describes, or drops the original with `backport-batch run --drop`:\n", policyURL)
|
||||
for _, p := range parks {
|
||||
fmt.Fprintf(&b, "\n- %s: %s", prLink(p.Original), parkReason(p))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if !*push {
|
||||
return 0
|
||||
}
|
||||
if code := a.comment(pr, *mention, today, text); code != 0 || len(parks) == 0 {
|
||||
return code
|
||||
}
|
||||
if err := a.announced(pr, src.rec, parks); err != nil {
|
||||
return a.fail(fmt.Errorf("%w; the next notify names them again", err))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// announced marks parks as named in the record of the open batch pull
|
||||
// request, which the comment just named: only while it is open and its body
|
||||
// still carries the record notify read, so a run that rewrote the body
|
||||
// meanwhile loses nothing; the next notify names those parks again.
|
||||
func (a *app) announced(pr *pullRequest, rec *lineRecord, parks []parkedEntry) error {
|
||||
marked := *rec
|
||||
marked.Parked = slices.Clone(rec.Parked)
|
||||
for i, p := range marked.Parked {
|
||||
if slices.ContainsFunc(parks, func(q parkedEntry) bool { return q.Original == p.Original && parkEvent(q) == parkEvent(p) }) {
|
||||
marked.Parked[i].Announced = true
|
||||
}
|
||||
}
|
||||
now, err := a.gh.pull(pr.Number)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if st := prState(&now); st != "open" {
|
||||
return fmt.Errorf("batch pull request #%d is %s now; nothing is written to it", pr.Number, st)
|
||||
}
|
||||
lines, err := fencedLines(now.Body, deferredFence)
|
||||
if err != nil || len(lines) != 1 {
|
||||
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
|
||||
}
|
||||
read, err := parseRecord(now.Body, notifyLineOf(rec))
|
||||
if err != nil || recordBlock(*read) != recordBlock(*rec) {
|
||||
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
|
||||
}
|
||||
// GitHub keeps the CRLF line ends a body edited on the web has; the
|
||||
// record is found, and the body written back, with LF ones.
|
||||
body := strings.ReplaceAll(now.Body, "\r\n", "\n")
|
||||
block := deferredFence + "\n" + lines[0] + "\n```"
|
||||
if strings.Count(body, block) != 1 {
|
||||
return fmt.Errorf("batch pull request #%d carries the record notify read, but not as one block it can mark", pr.Number)
|
||||
}
|
||||
body = strings.Replace(body, block, recordBlock(marked), 1)
|
||||
name, err := writeTemp("backport-batch-body-*.md", body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
return a.gh.editBody(pr.Number, name)
|
||||
}
|
||||
|
||||
// notifyLineOf is the line a record is for, which it was read against.
|
||||
func notifyLineOf(rec *lineRecord) line {
|
||||
l, _ := parseLine(rec.Line)
|
||||
return l
|
||||
}
|
||||
|
||||
// placement says under which list notify names original n of the record:
|
||||
// workflow for a wait for a maintainer, conflict for a deferred conflict, ""
|
||||
// for neither, a deferral for a reason among them. One that waits on others
|
||||
// goes where what decides its wait goes, followed down the record; one that
|
||||
// is not in the record is held by rule 6 or missing, and waits on nobody.
|
||||
func placement(deferred map[int]deferredEntry, n int, seen map[int]bool) string {
|
||||
e, ok := deferred[n]
|
||||
switch {
|
||||
case !ok || seen[n]:
|
||||
return ""
|
||||
case e.Workflows:
|
||||
return "workflow"
|
||||
case e.Reason != "":
|
||||
return ""
|
||||
case len(e.Needs) == 0:
|
||||
return "conflict"
|
||||
}
|
||||
seen[n] = true
|
||||
place := ""
|
||||
for _, f := range e.Needs {
|
||||
switch p := placement(deferred, f, seen); {
|
||||
case p == "workflow":
|
||||
return p
|
||||
case p == "conflict":
|
||||
place = p
|
||||
}
|
||||
}
|
||||
return place
|
||||
}
|
||||
|
||||
// comment writes what waits into the one notify comment on the batch pull
|
||||
// request: it edits the comment if there is one, and opens it only when
|
||||
// something waits. Only a comment by an author the batch trusts is taken for
|
||||
// it, so a look-alike someone else wrote is neither edited nor believed.
|
||||
func (a *app) comment(pr *pullRequest, mention string, today time.Time, text []string) int {
|
||||
comments, err := a.gh.comments(pr.Number)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
var ours *issueComment
|
||||
for i := range comments {
|
||||
c := &comments[i]
|
||||
if !strings.HasPrefix(c.Body, notifyMarker) {
|
||||
continue
|
||||
}
|
||||
why, err := trustedAuthor(a.gh, c.User.Login, c.User.Type)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why == "" {
|
||||
ours = c
|
||||
}
|
||||
}
|
||||
if ours == nil && len(text) == 0 {
|
||||
return 0
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString(notifyMarker + "\n")
|
||||
if mention != "" && len(text) > 0 {
|
||||
b.WriteString(mention + "\n\n")
|
||||
}
|
||||
if len(text) == 0 {
|
||||
text = []string{"Nothing waits on a maintainer."}
|
||||
}
|
||||
b.WriteString(strings.Join(text, "\n\n"))
|
||||
fmt.Fprintf(&b, "\n\n<sub>Updated by the backport batch on %s.</sub>\n", today.Format(time.DateOnly))
|
||||
f, err := os.CreateTemp("", "backport-batch-comment-*.md")
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
if _, err := f.WriteString(b.String()); err != nil {
|
||||
f.Close()
|
||||
return a.fail(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if err := a.stillOpen(pr.Number); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if ours != nil {
|
||||
err = a.gh.editComment(ours.ID, f.Name())
|
||||
} else {
|
||||
err = a.gh.createComment(pr.Number, f.Name())
|
||||
}
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// lines prints the lines the batch runs for, newest first, in the order the
|
||||
// policy has them processed.
|
||||
func (a *app) lines(args []string) int {
|
||||
fs := a.policyFlags("lines")
|
||||
if err := parse(fs, args); err != nil {
|
||||
return 2
|
||||
}
|
||||
if err := fetch(); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
mainHead, err := resolve("refs/remotes/" + remote + "/main")
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
pol, _, err := a.policy(mainHead)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
lines, err := batchLines(pol.released)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, l := range slices.Backward(lines) {
|
||||
fmt.Fprintln(a.stdout, l.branch())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// notifyPR is an open batch pull request for release-1.1 on the branch opened
|
||||
// on date, YYYYMMDD, with body.
|
||||
func notifyPR(date, body string) pullRequest {
|
||||
var pr pullRequest
|
||||
pr.Number, pr.Body = 90, body
|
||||
pr.User.Login, pr.User.Type = "maintainer", "User"
|
||||
pr.Head.Ref, pr.Head.SHA = "backport-batch/release-1.1-"+date, strings.Repeat("a", 40)
|
||||
pr.Head.Repo = &struct {
|
||||
FullName string `json:"full_name"`
|
||||
}{upstreamRepo}
|
||||
pr.Base.Ref, pr.Base.Repo.FullName = "release-1.1", upstreamRepo
|
||||
return pr
|
||||
}
|
||||
|
||||
// notifyRun runs notify on 2026-10-26 with gh faked.
|
||||
func notifyRun(gh *fakeGH, args ...string) (int, string, string) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
|
||||
code := a.main(append([]string{"notify", "--line", "release-1.1"}, args...))
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
var notifyLine, _ = parseLine("release-1.1")
|
||||
|
||||
// A batch open since before the previous run gets one comment, mentioning
|
||||
// the team, and keeps that one comment however many runs see it.
|
||||
func TestNotifyStalled(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
date string
|
||||
stalled bool
|
||||
}{
|
||||
{"20261026", false},
|
||||
{"20261024", false},
|
||||
{"20261023", true},
|
||||
{"20261019", true},
|
||||
} {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR(tc.date, "")}}
|
||||
code, stdout, stderr := notifyRun(gh, "--mention", "cozystack/maintainers", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("%s: notify = %d\n%s", tc.date, code, stderr)
|
||||
}
|
||||
if got := strings.Contains(stdout, "release-1.1: batch pull request #90 has been open since"); got != tc.stalled {
|
||||
t.Errorf("%s: stalled = %v, want %v:\n%s", tc.date, got, tc.stalled, stdout)
|
||||
}
|
||||
if got := len(gh.issueComments); got != map[bool]int{true: 1, false: 0}[tc.stalled] {
|
||||
t.Errorf("%s: %d comments, want one only when stalled", tc.date, got)
|
||||
}
|
||||
if !tc.stalled {
|
||||
continue
|
||||
}
|
||||
c := gh.issueComments[0].Body
|
||||
if !strings.HasPrefix(c, notifyMarker+"\n@cozystack/maintainers\n") || !strings.Contains(c, "open since") {
|
||||
t.Errorf("%s: the comment does not mention the team and say why:\n%s", tc.date, c)
|
||||
}
|
||||
for range 2 {
|
||||
if code, _, stderr := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push"); code != 0 {
|
||||
t.Fatalf("notify again = %d\n%s", code, stderr)
|
||||
}
|
||||
}
|
||||
if len(gh.issueComments) != 1 {
|
||||
t.Errorf("%s: %d comments after three runs, want the one updated", tc.date, len(gh.issueComments))
|
||||
}
|
||||
}
|
||||
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 0 || strings.Contains(gh.issueComments[0].Body, "@") {
|
||||
t.Errorf("notify with no team = %d and wrote %q; want no mention", code, gh.issueComments[0].Body)
|
||||
}
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
|
||||
if code, stdout, _ := notifyRun(gh); code != 0 || stdout == "" || len(gh.issueComments) != 0 {
|
||||
t.Errorf("notify without --push = %d, %q and %d comments; want the line and no comment", code, stdout, len(gh.issueComments))
|
||||
}
|
||||
}
|
||||
|
||||
// The comment lists the deferred conflicts for a maintainer, and nothing
|
||||
// --defer held out.
|
||||
func TestNotifyConflicts(t *testing.T) {
|
||||
conflict := pick{v: &verdict{o: &original{number: 6}}, files: []string{"app/six.txt"}}
|
||||
held := pick{v: &verdict{o: &original{number: 7}}, reason: "fails the e2e"}
|
||||
body := "text\n\n" + deferredBlock(notifyLine, []pick{conflict}, []pick{held}, nil) + "\n"
|
||||
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", body)}}
|
||||
code, stdout, stderr := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push")
|
||||
if code != 0 || !strings.Contains(stdout, "release-1.1: 1 deferred conflicts on #90 wait for a maintainer") {
|
||||
t.Fatalf("notify = %d, %q\n%s", code, stdout, stderr)
|
||||
}
|
||||
if len(gh.issueComments) != 1 {
|
||||
t.Fatalf("%d comments, want one", len(gh.issueComments))
|
||||
}
|
||||
c := gh.issueComments[0].Body
|
||||
if !strings.Contains(c, "@cozystack/maintainers") || !strings.Contains(c, "[#6](") || !strings.Contains(c, "resolve-conflict.md") || strings.Contains(c, "#7") {
|
||||
t.Errorf("the comment does not list #6 alone for a maintainer:\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
// Only a comment by an author the batch trusts is taken for the notify
|
||||
// comment: a look-alike is neither edited nor counted.
|
||||
func TestNotifyKeepsToItsOwnComment(t *testing.T) {
|
||||
look := issueComment{ID: 7, Body: notifyMarker + "\nplanted"}
|
||||
look.User.Login, look.User.Type = "someone", "User"
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}, issueComments: []issueComment{look}}
|
||||
if code, _, stderr := notifyRun(gh, "--push"); code != 0 {
|
||||
t.Fatalf("notify = %d\n%s", code, stderr)
|
||||
}
|
||||
if len(gh.issueComments) != 2 || gh.issueComments[0].Body != look.Body {
|
||||
t.Errorf("comments = %+v; want the look-alike untouched and one of ours", gh.issueComments)
|
||||
}
|
||||
|
||||
// Ours says so once nothing waits any more, and calls nobody for it.
|
||||
gh.prs = []pullRequest{notifyPR("20261026", "")}
|
||||
if code, _, _ := notifyRun(gh, "--mention", "@cozystack/maintainers", "--push"); code != 0 || len(gh.issueComments) != 2 ||
|
||||
!strings.Contains(gh.issueComments[1].Body, "Nothing waits") || strings.Contains(gh.issueComments[1].Body, "@cozystack") {
|
||||
t.Errorf("comments = %+v; want ours updated to say nothing waits, mentioning nobody", gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// The scheduled batch writes as its bot: notify keeps to the comment that bot
|
||||
// wrote, on a pull request it opened, and to no other bot's.
|
||||
func TestNotifyFindsTheBotsComment(t *testing.T) {
|
||||
const bot = "cozystack-ci[bot]"
|
||||
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
|
||||
ours.User.Login, ours.User.Type = bot, "Bot"
|
||||
other := issueComment{ID: 8, Body: notifyMarker + "\nplanted"}
|
||||
other.User.Login, other.User.Type = "renovate[bot]", "Bot"
|
||||
// The bot writes the record into every body it opens.
|
||||
pr := notifyPR("20261019", deferredBlock(notifyLine, nil, nil, nil))
|
||||
pr.User.Login, pr.User.Type = bot, "Bot"
|
||||
gh := &fakeGH{prs: []pullRequest{pr}, issueComments: []issueComment{ours, other}, bot: bot}
|
||||
if code, stdout, stderr := notifyRun(gh, "--push"); code != 0 || !strings.Contains(stdout, "has been open since") {
|
||||
t.Fatalf("notify = %d, %q\n%s", code, stdout, stderr)
|
||||
}
|
||||
if len(gh.issueComments) != 2 || !strings.Contains(gh.issueComments[0].Body, "has been open since") || gh.issueComments[1].Body != other.Body {
|
||||
t.Errorf("comments = %+v; want the bot's own comment edited and the other bot's untouched", gh.issueComments)
|
||||
}
|
||||
if code, _, stderr := notifyRun(&fakeGH{prs: []pullRequest{pr}, bot: "cozystack-ci"}, "--push"); code != 2 || !strings.Contains(stderr, batchBotEnv) {
|
||||
t.Errorf("a %s that is no bot login = %d (%s), want a refusal", batchBotEnv, code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A permission GitHub does not answer fails notify before it writes: read as
|
||||
// a refusal, it would pass over a person's batch, or over the comment notify
|
||||
// keeps and post a second one.
|
||||
func TestNotifyFailsOnAnUnreadablePermission(t *testing.T) {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}, permErr: errString("HTTP 502")}
|
||||
if code, stdout, stderr := notifyRun(gh, "--push"); code != 2 || stdout != "" || len(gh.issueComments) != 0 || !strings.Contains(stderr, "HTTP 502") {
|
||||
t.Errorf("notify on a person's batch = %d, %q, %d comments (%s); want 2 and nothing written", code, stdout, len(gh.issueComments), stderr)
|
||||
}
|
||||
|
||||
const bot = "cozystack-ci[bot]"
|
||||
pr := notifyPR("20261019", deferredBlock(notifyLine, nil, nil, nil))
|
||||
pr.User.Login, pr.User.Type = bot, "Bot"
|
||||
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
|
||||
ours.User.Login, ours.User.Type = "maintainer", "User"
|
||||
gh = &fakeGH{prs: []pullRequest{pr}, issueComments: []issueComment{ours}, bot: bot, permErr: errString("HTTP 502")}
|
||||
if code, _, stderr := notifyRun(gh, "--push"); code != 2 || len(gh.issueComments) != 1 || gh.issueComments[0].Body != ours.Body || !strings.Contains(stderr, "HTTP 502") {
|
||||
t.Errorf("notify with a person's comment = %d, comments %+v (%s); want 2 and the comment untouched", code, gh.issueComments, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifyRefuses(t *testing.T) {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
|
||||
for _, args := range [][]string{
|
||||
{"--mention", "@a b"},
|
||||
{"--mention", "@org/team/x"},
|
||||
{"--mention", "@-x"},
|
||||
} {
|
||||
if code, _, _ := notifyRun(gh, args...); code != 2 {
|
||||
t.Errorf("notify %q = %d, want 2", args, code)
|
||||
}
|
||||
}
|
||||
if code, stdout, _ := notifyRun(&fakeGH{}, "--push"); code != 0 || stdout != "" {
|
||||
t.Errorf("notify with no batch open = %d, %q; want nothing", code, stdout)
|
||||
}
|
||||
pr := notifyPR("20261019", "")
|
||||
pr.User.Login = "someone"
|
||||
if code, stdout, _ := notifyRun(&fakeGH{prs: []pullRequest{pr}}, "--push"); code != 0 || stdout != "" {
|
||||
t.Errorf("notify on a batch someone untrusted opened = %d, %q; want nothing", code, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// notify reads the line's record the way the other commands do, and stays
|
||||
// silent with no batch open, but a record it cannot read, or one that says
|
||||
// its open batch is being closed, stops it.
|
||||
func TestNotifyValidatesTheRecord(t *testing.T) {
|
||||
sha := strings.Repeat("a", 40)
|
||||
closingRecord := strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":91,"branch":"backport-batch/release-1.1-20261002","head":"`+sha+`"}`, 1)
|
||||
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
pr pullRequest
|
||||
code int
|
||||
quiet bool
|
||||
}{
|
||||
{"a clean closed record", recordPR(91, "closed", "maintainer", block(v2(""))), 0, true},
|
||||
{"a malformed record", recordPR(91, "closed", "maintainer", block(`{"line":`)), 2, false},
|
||||
{"a closed record that was closing", recordPR(91, "closed", "maintainer", block(closingRecord)), 0, true},
|
||||
{"an open record that is closing", recordPR(91, "open", "maintainer", block(closingRecord)), 2, false},
|
||||
} {
|
||||
gh := &fakeGH{prs: []pullRequest{tc.pr}}
|
||||
code, stdout, stderr := notifyRun(gh, "--push")
|
||||
if code != tc.code || tc.quiet && (stdout != "" || stderr != "") || len(gh.issueComments) != 0 {
|
||||
t.Errorf("%s: notify = %d, %q, %q, %d comments; want %d and nothing written", tc.name, code, stdout, stderr, len(gh.issueComments), tc.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The maintainer's example: with 1.6 and 1.7 released, release-1.8 joins them
|
||||
// when it branches, and 1.8.0 drops 1.6.
|
||||
func TestSupportedLines(t *testing.T) {
|
||||
l := func(minor int) line { return line{1, minor} }
|
||||
cases := []struct {
|
||||
name string
|
||||
all []line
|
||||
shipped []line
|
||||
released int
|
||||
want []line
|
||||
}{
|
||||
{"two released", []line{l(5), l(6), l(7)}, []line{l(5), l(6), l(7)}, 2, []line{l(6), l(7)}},
|
||||
{"release-1.8 branched", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7)}, 2, []line{l(6), l(7), l(8)}},
|
||||
{"1.8.0 shipped", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7), l(8)}, 2, []line{l(7), l(8)}},
|
||||
{"three released", []line{l(5), l(6), l(7), l(8)}, []line{l(5), l(6), l(7)}, 3, []line{l(5), l(6), l(7), l(8)}},
|
||||
{"one released", []line{l(6), l(7), l(8)}, []line{l(6), l(7)}, 1, []line{l(7), l(8)}},
|
||||
{"a line between releases that never shipped", []line{l(5), l(6), l(7)}, []line{l(5), l(7)}, 2, []line{l(5), l(7)}},
|
||||
{"nothing shipped yet", []line{l(0), l(1)}, nil, 2, []line{l(0), l(1)}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
shipped := map[line]bool{}
|
||||
for _, s := range tc.shipped {
|
||||
shipped[s] = true
|
||||
}
|
||||
if got := supportedLines(tc.all, shipped, tc.released); !slices.Equal(got, tc.want) {
|
||||
t.Errorf("%s: supportedLines = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// lines prints the lines the batch runs for, newest first, which is the order
|
||||
// the scheduled run takes them in: the newest released lines, as many as the
|
||||
// policy says, and a line branched since, whose stable release drops the
|
||||
// oldest.
|
||||
func TestLines(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.release("release-0.9")
|
||||
f.tag("v0.9.0")
|
||||
f.lines()
|
||||
f.release("release-1.2")
|
||||
f.release("release-1.2.0-rc.1")
|
||||
f.tag("v1.2.0-rc.1")
|
||||
lines := func() string {
|
||||
t.Helper()
|
||||
code, stdout, stderr := f.exec(nil, "lines")
|
||||
if code != 0 {
|
||||
t.Fatalf("lines = %d (%s)", code, stderr)
|
||||
}
|
||||
return stdout
|
||||
}
|
||||
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\n" {
|
||||
t.Errorf("with release-1.2 at its first rc, lines = %q; want it before the two released lines", got)
|
||||
}
|
||||
f.setPolicy(releasedKey + ": 3\n")
|
||||
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\nrelease-0.9\n" {
|
||||
t.Errorf("with three released lines, lines = %q; want release-0.9 too", got)
|
||||
}
|
||||
f.setPolicy("")
|
||||
f.tag("v1.2.0")
|
||||
if got := lines(); got != "release-1.2\nrelease-1.1\n" {
|
||||
t.Errorf("once v1.2.0 ships, lines = %q; want release-1.0 dropped", got)
|
||||
}
|
||||
f.setPolicy(releasedKey + ": 3\n")
|
||||
if got := lines(); got != "release-1.2\nrelease-1.1\nrelease-1.0\n" {
|
||||
t.Errorf("once v1.2.0 ships, with three released lines, lines = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A scheduled run with nothing to pick for a line succeeds without pushing or
|
||||
// opening anything.
|
||||
func TestRunWithNothingToPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "feat(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
if code != 0 || !strings.Contains(stderr, "nothing to push") {
|
||||
t.Errorf("run with nothing to pick = %d, want 0:\n%s", code, stderr)
|
||||
}
|
||||
if f.remoteHead(batchBranch1) != "" || len(gh.created) != 0 {
|
||||
t.Errorf("pushed %q and opened %q with nothing picked", f.remoteHead(batchBranch1), gh.created)
|
||||
}
|
||||
}
|
||||
|
||||
// notify writes its comment only after GitHub says the batch is still open,
|
||||
// so a batch closed while notify reads it is not written to, whether the
|
||||
// comment would be new or an update of its own.
|
||||
func TestNotifyWritesNothingOnABatchClosedMeanwhile(t *testing.T) {
|
||||
for _, update := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "a new comment", true: "an update"}[update], func(t *testing.T) {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261023", recordBody(5))}}
|
||||
if update {
|
||||
ours := issueComment{ID: 7, Body: notifyMarker + "\nearlier"}
|
||||
ours.User.Login, ours.User.Type = "maintainer", "User"
|
||||
gh.issueComments = []issueComment{ours}
|
||||
}
|
||||
gh.after = map[string]func(){"comments": func() { gh.find(90).State = "closed" }}
|
||||
code, _, stderr := notifyRun(gh, "--push")
|
||||
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || len(gh.closedWrites) != 0 || update && gh.issueComments[0].Body != notifyMarker+"\nearlier" {
|
||||
t.Errorf("notify = %d, closed writes %q, comments %+v; want 2 and nothing written:\n%s", code, gh.closedWrites, gh.issueComments, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// parkedBody is an open batch body whose version 3 record parks p.
|
||||
func parkedBody(p ...parkedEntry) string {
|
||||
sha := strings.Repeat("a", 40)
|
||||
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{}, Dropped: []droppedEntry{}, Stale: []int{}, Parked: p}
|
||||
return "batch\n\n" + recordBlock(rec) + "\n"
|
||||
}
|
||||
|
||||
// notifyAt runs notify at a time of day, with gh faked.
|
||||
func notifyAt(gh *fakeGH, at time.Time, args ...string) (int, string, string) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return at }}
|
||||
code := a.main(append([]string{"notify", "--line", "release-1.1"}, args...))
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
// A park is announced once on the open batch: notify names it in its
|
||||
// comment and marks it announced in the record; a later notify is silent
|
||||
// about it. A mark that fails to land repeats the announcement once, the
|
||||
// next day. With no batch open nothing is announced, and the first notify of
|
||||
// the next batch does it.
|
||||
func TestNotifyAnnouncesAParkOnce(t *testing.T) {
|
||||
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
|
||||
parked := func(gh *fakeGH) []parkedEntry {
|
||||
t.Helper()
|
||||
rec, err := parseRecord(gh.prs[0].Body, notifyLine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return rec.Parked
|
||||
}
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
code, stdout, stderr := notifyRun(gh, "--push")
|
||||
if code != 0 || !strings.Contains(stdout, "release-1.1: 1 parked originals on #90, named once") || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "- [#5](https://github.com/cozystack/cozystack/pull/5): builds on [#3](") {
|
||||
t.Fatalf("notify = %d, want #5 announced:\n%s\n%s\n%v", code, stdout, stderr, gh.issueComments)
|
||||
}
|
||||
if p := parked(gh); len(p) != 1 || !p[0].Announced {
|
||||
t.Errorf("the record parks %+v, want #5 marked announced", p)
|
||||
}
|
||||
if code, stdout, _ = notifyRun(gh, "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(gh.issueComments[0].Body, "[#5]") {
|
||||
t.Errorf("notify again = %d, want nothing about #5:\n%s\n%s", code, stdout, gh.issueComments[0].Body)
|
||||
}
|
||||
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}, fail: map[string]error{"editBody": errors.New("HTTP 502")}}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.issueComments) != 1 || parked(gh)[0].Announced {
|
||||
t.Fatalf("notify with a failing mark = %d, want 2 after the comment, the record unmarked", code)
|
||||
}
|
||||
code, _, stderr = notifyAt(gh, time.Date(2026, 10, 27, 0, 5, 0, 0, time.UTC), "--push")
|
||||
if code != 0 || !strings.Contains(gh.issueComments[0].Body, "[#5](") || !parked(gh)[0].Announced {
|
||||
t.Errorf("notify the next day = %d, want #5 named once more and marked:\n%s\n%s", code, stderr, gh.issueComments[0].Body)
|
||||
}
|
||||
|
||||
// Closed, or rewritten by a run, between the comment and the mark: the
|
||||
// mark is not written, and the next notify names #5 again.
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
gh.after = map[string]func(){"createComment": func() { gh.prs[0].State = "closed" }}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.closedWrites) != 0 || parked(gh)[0].Announced {
|
||||
t.Errorf("notify on a batch closed meanwhile = %d, writes %q; want 2 and no mark", code, gh.closedWrites)
|
||||
}
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
rewritten := parkedBody(park, parkedEntry{Original: 6, Merge: strings.Repeat("d", 40), Kind: parkPartial, Files: []string{"x"}})
|
||||
gh.after = map[string]func(){"createComment": func() { gh.prs[0].Body = rewritten }}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || gh.prs[0].Body != rewritten || len(gh.bodies) != 0 {
|
||||
t.Errorf("notify on a body rewritten meanwhile = %d; want 2 and the new body left as it is", code)
|
||||
}
|
||||
|
||||
// A body someone edited on the web comes back with CRLF line ends; the
|
||||
// mark lands all the same.
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", strings.ReplaceAll(parkedBody(park), "\n", "\r\n"))}}
|
||||
if code, _, stderr := notifyRun(gh, "--push"); code != 0 || !parked(gh)[0].Announced {
|
||||
t.Errorf("notify on a CRLF body = %d, want #5 marked:\n%s", code, stderr)
|
||||
}
|
||||
|
||||
closed := notifyPR("20261026", parkedBody(park))
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
closed.State, closed.MergedAt = "closed", &at
|
||||
gh = &fakeGH{prs: []pullRequest{closed}}
|
||||
if code, stdout, _ := notifyRun(gh, "--push"); code != 0 || stdout != "" || len(gh.issueComments) != 0 || len(gh.bodies) != 0 {
|
||||
t.Errorf("notify with no batch open = %d, %q, %d comments, %d edits; want nothing", code, stdout, len(gh.issueComments), len(gh.bodies))
|
||||
}
|
||||
next := notifyPR("20261027", parkedBody(park))
|
||||
next.Number = 91
|
||||
gh.prs = append(gh.prs, next)
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 0 || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "[#5](") {
|
||||
t.Errorf("the next batch's notify = %d, want #5 announced on it: %v", code, gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// A park carries its announcement into the next record only while it is
|
||||
// the same park: the same merge, kind, prerequisites, commits and files.
|
||||
func TestParkEventIdentity(t *testing.T) {
|
||||
a, b := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
was := parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Announced: true}
|
||||
carrier := &lineRecord{Parked: []parkedEntry{was}}
|
||||
for name, tc := range map[string]struct {
|
||||
now parkedEntry
|
||||
announced bool
|
||||
}{
|
||||
"the same park": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}}, true},
|
||||
"another unnumbered commit": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{b}}, false},
|
||||
"another kind": {parkedEntry{Original: 5, Merge: a, Kind: parkPartial, Files: []string{"x"}}, false},
|
||||
"another prerequisite": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Needs: []int{3}}, false},
|
||||
"another merge": {parkedEntry{Original: 5, Merge: b, Kind: parkDependency, Commits: []string{a}}, false},
|
||||
"another original": {parkedEntry{Original: 6, Merge: a, Kind: parkDependency, Commits: []string{a}}, false},
|
||||
} {
|
||||
got := carryAnnounced([]parkedEntry{tc.now}, carrier)
|
||||
if got[0].Announced != tc.announced {
|
||||
t.Errorf("%s: announced = %v, want %v", name, got[0].Announced, tc.announced)
|
||||
}
|
||||
}
|
||||
if got := carryAnnounced([]parkedEntry{was}, nil); got[0].Announced {
|
||||
t.Error("a park with no carrier came out announced")
|
||||
}
|
||||
unannounced := was
|
||||
unannounced.Announced = false
|
||||
if got := carryAnnounced([]parkedEntry{was}, &lineRecord{Parked: []parkedEntry{unannounced}}); got[0].Announced {
|
||||
t.Error("a park the carrier holds unannounced came out announced")
|
||||
}
|
||||
}
|
||||
|
||||
// A park keeps its announcement through a continuation and into the next
|
||||
// batch, so notify does not name it again.
|
||||
func TestRolloverKeepsTheAnnouncement(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, _, stderr := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(notifyComment(gh), "[#5](") {
|
||||
t.Fatalf("notify = %d, want #5 announced:\n%s", code, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
|
||||
t.Fatalf("run --continue = %d, want #5's park still announced:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
mergeBatch(f, gh)
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh.nextPR = 91
|
||||
f.now = f.now.Add(24 * time.Hour)
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
|
||||
t.Fatalf("the next batch's run = %d, want #5's park announced from the merged batch:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
gh.issueComments = nil
|
||||
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(notifyComment(gh), "[#5](") {
|
||||
t.Errorf("notify on the next batch = %d, want nothing about #5:\n%s\n%s", code, stdout, notifyComment(gh))
|
||||
}
|
||||
}
|
||||
|
||||
// notifyComment is the notify comment on the fake, or "".
|
||||
func notifyComment(gh *fakeGH) string {
|
||||
for _, c := range slices.Backward(gh.issueComments) {
|
||||
if strings.HasPrefix(c.Body, notifyMarker) {
|
||||
return c.Body
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// pushed builds and pushes a batch for #5 and returns its head.
|
||||
func pushed(t *testing.T, f *fixture) string {
|
||||
t.Helper()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
if code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--push"); code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
head := f.git("rev-parse", "HEAD")
|
||||
f.git("switch", "-q", "main")
|
||||
return head
|
||||
}
|
||||
|
||||
// --onto continues the pushed head and nothing else: it keeps what the batch
|
||||
// carries and picks only what is new.
|
||||
func TestOntoContinues(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
|
||||
if code != 2 || !strings.Contains(stderr, "not the head of backport-batch/release-1.1-20261029") {
|
||||
t.Errorf("--onto with another date = %d (%s), want a refusal", code, stderr)
|
||||
}
|
||||
f.now = f.now.AddDate(0, 0, 3)
|
||||
code, stdout, stderr = f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
if code != 0 {
|
||||
t.Fatalf("run --onto = %d\n%s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stderr, "title: fix(backport): batch for release-1.1, 2026-10-26\n") {
|
||||
t.Errorf("the continued batch is not titled for the date it opened on:\n%s", stderr)
|
||||
}
|
||||
if got := f.subjects(head, "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("picked %q on top of the batch, want only #6", got)
|
||||
}
|
||||
if !descends(t, head, f.git("rev-parse", "HEAD")) {
|
||||
t.Error("the new head does not descend from --onto")
|
||||
}
|
||||
if listed, _, _ := strings.Cut(stdout, "### Skipped"); !strings.Contains(listed, "[#5]") || !strings.Contains(listed, "[#6]") {
|
||||
t.Errorf("the body does not list what the batch carries and what it picked:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A head that is not the one the remote has now is refused, so work pushed
|
||||
// since cannot be dropped.
|
||||
func TestOntoRefusesAStaleHead(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.git("switch", "-q", "--detach", head)
|
||||
f.write("app/fold.txt", "folded\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "fold")
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
|
||||
f.git("switch", "-q", "main")
|
||||
|
||||
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
if code != 2 || !strings.Contains(stderr, "is not the head of any batch branch") {
|
||||
t.Errorf("run --onto a stale head = %d (%s), want a refusal", code, stderr)
|
||||
}
|
||||
for _, bad := range []string{head[:12], "-" + head[1:], strings.ToUpper(head), "HEAD", "--help"} {
|
||||
if code, _, _ := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", bad); code != 2 {
|
||||
t.Errorf("--onto %q = %d, want 2", bad, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A rebuilt branch that does not descend from what the remote has is never
|
||||
// pushed over it.
|
||||
func TestPushRefusesANonFastForward(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.git("switch", "-q", "--detach", head)
|
||||
f.write("app/fold.txt", "folded\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "fold")
|
||||
folded := f.git("rev-parse", "HEAD")
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
|
||||
f.git("switch", "-q", "main")
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
|
||||
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--push")
|
||||
// git would refuse too, but only after trying; the tool refuses first.
|
||||
if code != 2 || !strings.Contains(stderr, "refusing a non-fast-forward push") {
|
||||
t.Errorf("a fresh rebuild pushed over the batch = %d (%s), want a refusal", code, stderr)
|
||||
}
|
||||
if got := f.remoteHead(batchBranch1); got != folded {
|
||||
t.Errorf("the remote batch moved to %.12s, want it left at %.12s", got, folded)
|
||||
}
|
||||
}
|
||||
|
||||
// A run that pushed its branch and stopped before opening the pull request
|
||||
// leaves the branch behind. A run the same day builds the batch anew and
|
||||
// refuses to push over it, naming the run that continues it, which then
|
||||
// opens the pull request.
|
||||
func TestOrphanBranchNamesItsRecovery(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
// A commit picked again gets a new committer date, so a new name.
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-10-26T07:00:00Z")
|
||||
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
recovery := "backport-batch run --line release-1.1 --onto " + head + " --push"
|
||||
if code != 2 || !strings.Contains(stderr, recovery) {
|
||||
t.Fatalf("a same-day run over the left branch = %d (%s), want a refusal naming %q", code, stderr, recovery)
|
||||
}
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, strings.Fields(recovery)[1:]...); code != 0 {
|
||||
t.Fatalf("%s = %d\n%s", recovery, code, stderr)
|
||||
}
|
||||
now := f.remoteHead(batchBranch1)
|
||||
if !descends(t, head, now) || now == head || len(gh.created) != 1 {
|
||||
t.Errorf("the recovery pushed %.12s over %.12s and opened %q; want a fast-forward with #6 and one pull request", now, head, gh.created)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// overridden builds a line where backport/ labels decide: #5 a feature let in,
|
||||
// #6 a fix kept out, #7 a breaking fix and #8 a migration let in, #9 carrying
|
||||
// both labels, #10 a feature let in that the line already carries, adapted, so
|
||||
// picking it again would conflict rather than come out empty.
|
||||
func overridden(t *testing.T) (*fixture, *fakeGH) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "feat(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app)!: seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
f.pr(8, "fix(platform): eight", "", map[string]string{migrationsDir + "61": "#!/bin/sh\n"})
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/nine.txt": "9\n"})
|
||||
mc10 := f.pr(10, "feat(app): ten", "", map[string]string{"app/ten.txt": "10\n"})
|
||||
f.onLine("release-1.1", "feat(app): ten (#10)\n\n(cherry picked from commit "+mc10+")", map[string]string{"app/ten.txt": "10, adapted\n"})
|
||||
gh := &fakeGH{labels: map[string][]int{
|
||||
labelInclude: {5, 7, 8, 9, 10},
|
||||
labelSkip: {6, 9},
|
||||
}}
|
||||
return f, gh
|
||||
}
|
||||
|
||||
func TestOverrides(t *testing.T) {
|
||||
f, gh := overridden(t)
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
want := []string{"feat(app): five (#5)", "fix(app)!: seven (#7)", "fix(platform): eight (#8)"}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, want) {
|
||||
t.Errorf("picked %q, want %q", got, want)
|
||||
}
|
||||
section, _, _ := strings.Cut(stdout[strings.Index(stdout, "### Maintainer overrides"):], "### Skipped")
|
||||
for n, outcome := range map[int]string{
|
||||
5: "`backport/include`, on this branch",
|
||||
6: "`backport/skip`, left out",
|
||||
7: "`backport/include`, on this branch",
|
||||
8: "`backport/include`, on this branch",
|
||||
9: "`backport/skip`, left out",
|
||||
10: "`backport/include`, skipped by rule 1",
|
||||
} {
|
||||
if !strings.Contains(section, fmt.Sprintf("/pull/%d)", n)) || !strings.Contains(section, outcome) {
|
||||
t.Errorf("the overrides section does not show #%d as %q:\n%s", n, outcome, section)
|
||||
}
|
||||
}
|
||||
if skipped := stdout[strings.Index(stdout, "### Skipped"):]; strings.Contains(skipped, "/pull/6)") || strings.Contains(skipped, "/pull/9)") {
|
||||
t.Errorf("a skipped-by-label original is listed under a rule:\n%s", skipped)
|
||||
}
|
||||
}
|
||||
|
||||
// check holds the line to what the labels decide, and fold refuses an
|
||||
// original kept out.
|
||||
func TestOverridesInCheckAndFold(t *testing.T) {
|
||||
f, gh := overridden(t)
|
||||
code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 1 {
|
||||
t.Fatalf("check = %d, want 1:\n%s", code, stdout)
|
||||
}
|
||||
for _, n := range []int{5, 7, 8} {
|
||||
if !strings.Contains(stdout, fmt.Sprintf("/pull/%d ", n)) {
|
||||
t.Errorf("check does not list #%d:\n%s", n, stdout)
|
||||
}
|
||||
}
|
||||
for _, n := range []int{6, 9, 10} {
|
||||
if strings.Contains(stdout, fmt.Sprintf("/pull/%d ", n)) {
|
||||
t.Errorf("check lists #%d:\n%s", n, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
|
||||
f.git("fetch", "-q", "origin")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "")}
|
||||
f.git("switch", "-q", "--detach", "origin/"+batchBranch1)
|
||||
f.write("app/six.txt", "6\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "port\n\n(cherry picked from commit "+f.git("rev-parse", "origin/main~4")+")")
|
||||
commit := f.git("rev-parse", "HEAD")
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, _ = f.exec(gh, "fold", "--line", "release-1.1", "--original", "6", "--commit", commit)
|
||||
if code != 1 || !strings.Contains(stdout, "#6 carries backport/skip") {
|
||||
t.Errorf("fold of a skipped original = %d, %q; want a refusal", code, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// backport/include lifts rules 2 to 4, not rule 6: an older line still waits
|
||||
// for the newer one.
|
||||
func TestIncludeKeepsRuleSix(t *testing.T) {
|
||||
f, gh := overridden(t)
|
||||
f.git("fetch", "-q", "origin")
|
||||
l, _ := parseLine("release-1.0")
|
||||
b, err := (&app{gh: gh, stderr: &strings.Builder{}}).prepare(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := b.find(5); v == nil || v.rule != ruleNewer {
|
||||
t.Errorf("#5 on release-1.0 = %+v, want rule 6", v)
|
||||
}
|
||||
if v := b.find(10); v == nil || v.rule != 0 {
|
||||
t.Errorf("#10 on release-1.0 = %+v, want it to qualify: release-1.1 carries it", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverridesUnreadable(t *testing.T) {
|
||||
f, gh := overridden(t)
|
||||
gh.listErr = errString("gh: HTTP 502")
|
||||
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "HTTP 502") {
|
||||
t.Errorf("check with labels unreadable = %d (%s), want 2", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// ghCLI against a stub gh on PATH: the exact argv, and a listing that fills
|
||||
// GitHub search's cap is refused rather than read as complete.
|
||||
func TestGHCLI(t *testing.T) {
|
||||
bin := t.TempDir()
|
||||
args, out := filepath.Join(bin, "args"), filepath.Join(bin, "out")
|
||||
stub := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"" + args + "\"\ncat \"" + out + "\"\n"
|
||||
if err := os.WriteFile(filepath.Join(bin, "gh"), []byte(stub), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
write := func(s string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(out, []byte(s), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = os.Remove(args)
|
||||
}
|
||||
called := func() string {
|
||||
data, _ := os.ReadFile(args)
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
write(`[{"number":5},{"number":7}]`)
|
||||
got, err := ghCLI{}.labelled(labelInclude)
|
||||
if err != nil || !slices.Equal(got, []int{5, 7}) {
|
||||
t.Errorf("labelled = %v, %v; want [5 7]", got, err)
|
||||
}
|
||||
if want := "pr list --base main --state merged --label backport/include --limit 1000 --json number"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
|
||||
var full []string
|
||||
for i := range searchResultCap {
|
||||
full = append(full, fmt.Sprintf(`{"number":%d}`, i+1))
|
||||
}
|
||||
write("[" + strings.Join(full, ",") + "]")
|
||||
if _, err := (ghCLI{}).labelled(labelSkip); err == nil {
|
||||
t.Error("a listing as long as the search cap was read as complete")
|
||||
}
|
||||
|
||||
write(`{"permission":"write"}`)
|
||||
if p, err := (ghCLI{}).permission("maintainer"); err != nil || p != "write" {
|
||||
t.Errorf("permission = %q, %v", p, err)
|
||||
}
|
||||
if want := "api repos/{owner}/{repo}/collaborators/maintainer/permission"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write(`{"permission":"admin"}`)
|
||||
for _, login := range []string{"x/../../orgs/y", "-x", "a b", "github-actions[bot]", ""} {
|
||||
if _, err := (ghCLI{}).permission(login); err == nil || called() != "" {
|
||||
t.Errorf("permission(%q) = %v and called gh %q; want a refusal before gh", login, err, called())
|
||||
}
|
||||
}
|
||||
|
||||
write("[]")
|
||||
if _, err := (ghCLI{}).openPulls("release-1.6"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "api -X GET repos/{owner}/{repo}/pulls -f state=open -f base=release-1.6 -f per_page=100"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write("[]")
|
||||
if _, err := (ghCLI{}).recentPulls("release-1.6", 2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "api -X GET repos/{owner}/{repo}/pulls -f state=all -f base=release-1.6 -f sort=created -f direction=desc -f per_page=100 -f page=2"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
var page []string
|
||||
for i := range openPullsCap {
|
||||
page = append(page, fmt.Sprintf(`{"number":%d}`, i+1))
|
||||
}
|
||||
write("[" + strings.Join(page, ",") + "]")
|
||||
if _, err := (ghCLI{}).openPulls("release-1.6"); err == nil {
|
||||
t.Error("a full page of open pull requests was read as all of them")
|
||||
}
|
||||
|
||||
write(`[[{"id":1,"body":"a","user":{"login":"x","type":"User"}}],[{"id":2,"body":"b","user":{"login":"y","type":"Bot"}}]]`)
|
||||
comments, err := ghCLI{}.comments(90)
|
||||
if err != nil || len(comments) != 2 || comments[1].ID != 2 || comments[1].User.Type != "Bot" {
|
||||
t.Errorf("comments = %+v, %v; want both pages", comments, err)
|
||||
}
|
||||
if want := "api --paginate --slurp repos/{owner}/{repo}/issues/90/comments"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write("{}")
|
||||
if err := (ghCLI{}).createComment(90, "/tmp/body.md"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "api --method POST repos/{owner}/{repo}/issues/90/comments -F body=@/tmp/body.md"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write("{}")
|
||||
if err := (ghCLI{}).editComment(12345678901, "/tmp/body.md"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "api --method PATCH repos/{owner}/{repo}/issues/comments/12345678901 -F body=@/tmp/body.md"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
|
||||
// A batch pull request carries no label: the release workflows act on a
|
||||
// pull request cozystack-ci[bot] opened only when it has the release label.
|
||||
write("https://github.com/cozystack/cozystack/pull/5300\n")
|
||||
if n, url, err := (ghCLI{}).createPull("release-1.7", batchBranch1, "fix(backport): batch", "/tmp/body.md"); err != nil || n != 5300 || url != "https://github.com/cozystack/cozystack/pull/5300" {
|
||||
t.Errorf("createPull = %d, %q, %v; want the pull request gh printed", n, url, err)
|
||||
}
|
||||
if want := "pr create --draft --base release-1.7 --head " + batchBranch1 + " --title fix(backport): batch --body-file /tmp/body.md"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write("Warning: 1 uncommitted change\n")
|
||||
if _, _, err := (ghCLI{}).createPull("release-1.7", batchBranch1, "fix(backport): batch", "/tmp/body.md"); err == nil {
|
||||
t.Error("createPull took output that is no pull request URL")
|
||||
}
|
||||
write(`{"number":90,"state":"closed","merged_at":"2026-10-26T12:00:00Z","head":{"ref":"x","sha":"y"}}`)
|
||||
if pr, err := (ghCLI{}).pull(90); err != nil || pr.State != "closed" || pr.MergedAt == nil {
|
||||
t.Errorf("pull = %+v, %v; want #90 merged", pr, err)
|
||||
}
|
||||
if want := "api repos/{owner}/{repo}/pulls/90"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
if _, err := (ghCLI{}).pull(91); err == nil {
|
||||
t.Error("pull took GitHub's answer about another pull request")
|
||||
}
|
||||
write("")
|
||||
if err := (ghCLI{}).closePull(90); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "pr close 90"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
write("")
|
||||
if err := (ghCLI{}).editPull(90, "fix(backport): batch", "/tmp/body.md"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "pr edit 90 --title fix(backport): batch --body-file /tmp/body.md"; called() != want {
|
||||
t.Errorf("gh %q, want gh %q", called(), want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,796 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var line11, _ = parseLine("release-1.1")
|
||||
|
||||
// handBatch pushes a batch carrying merges in the batch's shape, as a run
|
||||
// made one before the partial guard, and opens its pull request with a
|
||||
// record that defers nothing.
|
||||
func handBatch(f *fixture, gh *fakeGH, merges ...string) string {
|
||||
f.t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
f.git("switch", "-q", "--detach", "origin/release-1.1")
|
||||
for _, mc := range merges {
|
||||
if parents := strings.Fields(f.git("rev-list", "--parents", "-n", "1", mc)); len(parents) == 2 {
|
||||
f.git("cherry-pick", "-x", mc)
|
||||
continue
|
||||
}
|
||||
base := f.git("rev-parse", "HEAD")
|
||||
for _, c := range strings.Fields(f.git("rev-list", "--reverse", "--no-merges", mc+"^1.."+mc+"^2")) {
|
||||
f.git("cherry-pick", "-x", c)
|
||||
}
|
||||
merge := f.joined(base, "HEAD", f.git("log", "-1", "--format=%B", mc)+"\n\n"+batchTrailer+": "+mc)
|
||||
f.git("update-ref", "HEAD", merge)
|
||||
}
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
|
||||
f.git("switch", "-q", "main")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")}
|
||||
return f.remoteHead(batchBranch1)
|
||||
}
|
||||
|
||||
// policyWorld is #5088's shape: #3, a feat the line never gets, adds a
|
||||
// policy and its test; #5 deletes both again and adds a test of its own.
|
||||
func policyWorld(t *testing.T) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n", "chart/tests/policy_test.yaml": "suite: policy\n"})
|
||||
mc5 := f.pr(5, "fix(chart): remove the policy a rebase brought back", "```release-note\nfix(chart): the stray network policy is gone.\n```\n",
|
||||
map[string]string{"chart/templates/policy.yaml": "", "chart/tests/policy_test.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// A clean pick that would delete files the line never had, and so carry only
|
||||
// #5's test, is withheld: #5 is parked as partial, with the paths it would
|
||||
// leave without effect, and its release note stays out of the body.
|
||||
func TestPartialPickIsWithheld(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "```release-note\nfix(app): six works.\n```\n", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("picked %q, want only #6", got)
|
||||
}
|
||||
want := `"parked":[{"original":5,"merge":"` + mc5 + `","kind":"partial","files":["chart/templates/policy.yaml","chart/tests/policy_test.yaml"]}]`
|
||||
if !strings.Contains(stdout, want) {
|
||||
t.Errorf("the record does not park #5 as partial (%s):\n%s", want, stdout)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "`chart/templates/policy.yaml`") {
|
||||
t.Errorf("the body does not say #5 is parked and why:\n%s", stdout)
|
||||
}
|
||||
if strings.Contains(stdout, "stray network policy") || !strings.Contains(stdout, "six works") {
|
||||
t.Errorf("the release notes are not #6's alone:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line removed itself, which no original added after the line
|
||||
// branched, still leaves #5's deletion without effect: check finds it with no
|
||||
// record and parks it, and run picks nothing.
|
||||
func TestPartialWithoutAnAdder(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(5, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
|
||||
t.Errorf("picked %q, want nothing", got)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") {
|
||||
t.Errorf("the body does not park #5:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr = f.exec(nil, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies to release-1.1") {
|
||||
t.Errorf("check = %d, want 0 with a parked-partial note:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The picker judges what its picks did, not what the original's merge did:
|
||||
// #5's first commit deletes a file the line no longer has and comes out
|
||||
// empty, its second adds a test, and the unit left would be the test alone.
|
||||
func TestPartialByAnEmptyOwnCommit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.prCommits(5, "fix(app): drop the policy and test it", "```release-note\nfix(app): no more policy.\n```\n",
|
||||
change{files: map[string]string{"app/policy.yaml": ""}},
|
||||
change{files: map[string]string{"app/policy_test.txt": "gone\n"}})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
|
||||
t.Errorf("picked %q, want #5 withheld", got)
|
||||
}
|
||||
if strings.Contains(stdout, "no more policy") || !strings.Contains(stdout, "### Parked") {
|
||||
t.Errorf("#5's note is in the body, or #5 is not parked:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A batch built before the guard can already carry a partial unit. It fails
|
||||
// the cut and verify whatever its label says, and --defer takes it off,
|
||||
// closing a batch it was the last unit of.
|
||||
func TestExistingPartialUnitFailsTheCut(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
gh := &fakeGH{}
|
||||
head := handBatch(f, gh, mc5)
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
|
||||
t.Errorf("check --cut = %d, want 1 naming the partial unit:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, stderr = f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
|
||||
if code != 1 || !strings.Contains(stdout, `#5 leaves "chart/templates/policy.yaml", "chart/tests/policy_test.yaml" without effect on release-1.1`) {
|
||||
t.Errorf("verify = %d, want 1 naming what #5 leaves out:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh.labels = map[string][]int{labelSkip: {5}}
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
|
||||
t.Errorf("check --cut with backport/skip = %d, want the partial unit to still fail it:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, _, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push")
|
||||
if code != 0 || prState(gh.find(90)) != "closed" || f.remoteHead(batchBranch1) != "" {
|
||||
t.Errorf("run --defer 5 --push = %d, #90 %s, branch at %q; want the batch closed and its branch gone:\n%s", code, prState(gh.find(90)), f.remoteHead(batchBranch1), stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// An original whose first pick came out empty, because the line already had
|
||||
// that change, is on the batch all the same: check does not call it missing,
|
||||
// a continuation keeps it picked, and so does a rebuild without another
|
||||
// original.
|
||||
func TestOnBatchAfterAnEmptyFirstPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): a, on the line first", map[string]string{"app/a.txt": "fixed\n"})
|
||||
f.prCommits(5, "fix(app): a and b", "",
|
||||
change{files: map[string]string{"app/a.txt": "fixed\n"}},
|
||||
change{files: map[string]string{"app/b.txt": "fixed\n"}})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a and b (#5)", "fix(app): six (#6)"}) {
|
||||
t.Fatalf("the batch carries %q, want #5 and #6", got)
|
||||
}
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Errorf("check = %d, want 0 with #5 on the batch:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #5 only on the batch:\n%s", code, stdout)
|
||||
}
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[5,6]`) {
|
||||
t.Errorf("run --continue = %d, want #5 and #6 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[5]`) || !strings.Contains(stdout, `"missing":[]`) {
|
||||
t.Errorf("run --defer 6 = %d, want #5 still picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// featWorld is #5071's shape: #3, a feat the line never gets, adds a file,
|
||||
// and #5 fixes that file; #6 is a plain fix beside it.
|
||||
func featWorld(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(5, "fix(app): fix the tap", "```release-note\nfix(app): the tap works.\n```\n", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
}
|
||||
|
||||
// A fix of a file a feat added on main, which the line never gets, is
|
||||
// parked, not tried, and check takes the park as a note.
|
||||
func TestParkAFixOfAFeatureTheLineLacks(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
if strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
|
||||
t.Errorf("#5 is not parked on #3:\n%s", stdout)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "builds on [#3]") {
|
||||
t.Errorf("the body does not say #5 builds on #3:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: it builds on #3, which never reaches release-1.1") {
|
||||
t.Errorf("check = %d, want 0 with a note that #5 is parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// backport/include lifts no park.
|
||||
func TestIncludeDoesNotLiftThePark(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
code, stdout, stderr := f.exec(&fakeGH{labels: map[string][]int{labelInclude: {5}}}, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, "`backport/include`, parked: builds on") {
|
||||
t.Errorf("run with backport/include on #5 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite that is itself parked, but adapted onto the batch by hand,
|
||||
// satisfies what builds on it: #3 is parked on #2, a feat, and #5 changes a
|
||||
// line #3 wrote.
|
||||
func TestAdaptedPrerequisiteSatisfiesItsDependent(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(20, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
mc3 := f.pr(3, "fix(app): an excited two, a loud three", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
f.pr(5, "fix(app): an excited three", "", map[string]string{"app/b.txt": strings.Replace(appFile, "three", "THREE!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
|
||||
t.Fatalf("run = %d, want #5 parked as long as #3 is:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
w := emptyBatch(f, &fakeGH{}, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")
|
||||
// #3 adapted by hand: every file it changes, changed, in the batch's
|
||||
// shape.
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc3+"^2"), map[string]string{"app/a.txt": strings.Replace(appFile, "two", "two!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
fold := w.merge(w.head, pick, "fix(app): an excited two, a loud three (#3)\n\n"+batchTrailer+": "+mc3)
|
||||
f.git("push", "-q", "origin", fold+":refs/heads/"+batchBranch1)
|
||||
w.gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, w.gh.prs[0].Body)}
|
||||
code, stdout, stderr := f.exec(w.gh, "check", "--line", "release-1.1")
|
||||
if code != 1 || strings.Contains(stderr, "#5 is parked") || strings.Contains(stdout+stderr, "waits on") || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, stderr = f.exec(w.gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[3,5]`) {
|
||||
t.Errorf("run --continue = %d, want #5 picked after #3's adaptation:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an earlier unit of the batch brought counts: #3 writes the line #8
|
||||
// changes, a feat takes it back and another feat, #6, writes it again, so
|
||||
// blame names #6, which never comes; once #3 is picked the branch has the
|
||||
// line all the same.
|
||||
func TestContentFromAnotherUnitSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "fix(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(4, "feat(app): a quiet two", "", map[string]string{"app/a.txt": appFile})
|
||||
f.pr(6, "feat(app): a loud two again", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(8, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): a loud two (#3)", "fix(app): an excited two (#8)"}) {
|
||||
t.Errorf("picked %q, want #3 and #8:\n%s", got, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// auxWorld is #5142's shape: #3, a feat, adds a bats test; #5 fixes a
|
||||
// workflow and that test, so all it builds on that never comes is a test;
|
||||
// #6 is a plain fix.
|
||||
func auxWorld(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
}
|
||||
|
||||
// A workflow original whose only missing dependency is a test waits for a
|
||||
// maintainer in the scheduled run, whose token cannot push workflows, and
|
||||
// check holds the cut on it. A maintainer's own run parks it. With no batch
|
||||
// open, or one whose body carries no record, it is missing; a record from
|
||||
// before the mode was saved says the scheduled mode by listing a wait.
|
||||
func TestWorkflowModeCarriesToCheck(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
|
||||
t.Fatalf("scheduled run = %d, want #5 waiting, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
v1 := "batch\n\n" + deferredBlock(line11, nil, nil, []pick{{v: &verdict{o: &original{number: 5}}, files: []string{".github/workflows/release.yaml"}}}) + "\n"
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, v1)}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut with a version 1 record = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "a batch someone wrote")}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check under a batch with no record = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
|
||||
f = auxWorld(t)
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check with no batch = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh = &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Fatalf("a maintainer's run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
|
||||
t.Errorf("check after a maintainer's run = %d, want 0 with #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Evidence found only in a test file still parks an original that changes
|
||||
// no workflow: #4973's shape.
|
||||
func TestTestOnlyEvidenceParksOtherOriginals(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.go": "package app\n", "app/nodes_test.go": "package app\n\nfunc a() {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): proxmox nodes", "", map[string]string{"app/nodes_test.go": "package app\n\nfunc a() { proxmox() }\n"})
|
||||
f.pr(5, "fix(app): nodes", "", map[string]string{"app/nodes.go": "package app\n\nvar fixed = true\n", "app/nodes_test.go": "package app\n\nfunc a() { proxmox(); fixed() }\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked on #3:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an original builds on through lines it adds is not seen: #4973's
|
||||
// $isProxmox. The original is picked; CI on the batch is what catches it.
|
||||
func TestAddedLinesDependencyIsNotSeen(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.tpl": "a\nb\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): define isProxmox", "", map[string]string{"app/proxmox.tpl": "isProxmox\n"})
|
||||
f.pr(5, "fix(app): use isProxmox", "", map[string]string{"app/nodes.tpl": "a\nif isProxmox\nb\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// With no batch open, check reads a closed record whatever its version and
|
||||
// whatever mode it saved as the scheduled run's: the workflow original it
|
||||
// left waiting is missing, not parked, while its drops and its other
|
||||
// deferrals still count.
|
||||
func TestClosedRecordModeFallback(t *testing.T) {
|
||||
for _, version := range []int{2, 3} {
|
||||
for _, saved := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("version %d, noWorkflowFiles %v", version, saved), func(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
mc6 := f.git("log", "--first-parent", "--format=%H", "-1", "--grep", "(#6)$", "main")
|
||||
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: version, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: saved, Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 5, Files: []string{".github/workflows/release.yaml"}, Workflows: true}, {Original: 7, Commit: f.git("rev-parse", mc7+"^2"), Files: []string{"app/seven.txt"}}},
|
||||
Dropped: []droppedEntry{{Original: 6, Merge: mc6, Reason: "not for this line", On: 90, Date: "2026-10-26"}}, Stale: []int{}}
|
||||
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
pr.State, pr.MergedAt = "closed", &at
|
||||
code, stdout, stderr := f.exec(&fakeGH{prs: []pullRequest{pr}}, "check", "--line", "release-1.1")
|
||||
if code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 missing and not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(stderr, "note: #6 is dropped") || !strings.Contains(stderr, "warning: deferred conflict on #90 (merged): https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check lost the closed record's drop or deferral:\n%s", stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Withdrawing a drop puts the original back where the rules and the line
|
||||
// put it: a partial original is parked again, in the record as in check.
|
||||
func TestUndropRestoresThePark(t *testing.T) {
|
||||
for name, world := range map[string]func(*testing.T) *fixture{
|
||||
"partial": func(t *testing.T) *fixture {
|
||||
f, _ := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
},
|
||||
"dependency": featWorld,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f := world(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "5", "--reason", "not for 1.1", "--push")
|
||||
reopen(f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "5", "--push")
|
||||
reopen(f, gh)
|
||||
if rec := recordOf(t, gh); len(rec.Parked) != 1 || rec.Parked[0].Original != 5 || rec.Parked[0].Kind != name {
|
||||
t.Errorf("after the undrop the record parks %+v, want #5 parked %s", rec.Parked, name)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
|
||||
t.Errorf("check = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite the open batch carries is there for what builds on it,
|
||||
// whatever its label now says: check and run agree that #7 is not parked.
|
||||
func TestPrerequisiteStillOnTheBatchSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
gh.labels = map[string][]int{labelSkip: {5}}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#7 is parked") || !strings.Contains(stdout, "/pull/7 ") {
|
||||
t.Errorf("check = %d, want #7 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue"); code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
|
||||
t.Errorf("run --continue = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The picker judges what its picks did where the run has got to: #4
|
||||
// deletes the file #5 also deletes, so after #4 #5 would bring only its
|
||||
// test, which the net merge at the line's head cannot see.
|
||||
func TestPartialByAnEarlierPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
|
||||
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[4]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #4 picked and #5 parked as partial:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an earlier pick brought clears a partial park the line's head
|
||||
// gave: the line removed the policy, #4 brings it back, and #5's deletion
|
||||
// then has something to delete.
|
||||
func TestPartialClearedByAnEarlierPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(3, "feat(app): no policy", "", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(4, "fix(app): the policy, back", "", map[string]string{"app/policy.yaml": "allow: some\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[4,5]`) {
|
||||
t.Errorf("run = %d, want #4 and #5 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A reason a person deferred an original for comes before its partial
|
||||
// park.
|
||||
func TestReasonComesBeforeThePartialPark(t *testing.T) {
|
||||
f, _ := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "waits for the policy rework", "--push")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 deferred for its reason:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The workflow exception holds only while what never comes is tests or
|
||||
// docs, and while the workflow lines build on nothing missing: a
|
||||
// workflow original that also fixes a file a feat added, or whose workflow
|
||||
// line another original still to come wrote, parks in the scheduled run too.
|
||||
func TestWorkflowExceptionNeedsTestsOnly(t *testing.T) {
|
||||
t.Run("a never dependency outside tests", func(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
|
||||
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a workflow line still to come", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n", "app/four.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): four, the line's way", map[string]string{"app/four.txt": "line\n"})
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(4, "fix(ci): four", "", map[string]string{"app/four.txt": "four\n", ".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
|
||||
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("scheduled run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What a rebuild takes off the batch is no longer there for what builds on
|
||||
// it: deferring #5 makes #7, which merged after the batch was built, wait
|
||||
// on it.
|
||||
func TestRebuildTakesThePrerequisiteAway(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) {
|
||||
t.Errorf("run --defer 5 = %d, want #7 waiting on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line renamed the way the original does is the original's own
|
||||
// rename, not a deletion left without effect.
|
||||
func TestSameRenameOnBothSidesIsWhole(t *testing.T) {
|
||||
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
|
||||
f.pr(5, "fix(app): rename p to q, and fix l10", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked whole:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A partial park only the picker saw, where the net merge at the line's
|
||||
// head looks whole, holds in check and in the operations that pick nothing
|
||||
// as the open record keeps it: check does not call the original missing.
|
||||
func TestPickerParkHoldsInCheck(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
|
||||
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies") {
|
||||
t.Errorf("check = %d, want #5 parked as the record says:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run --defer 6 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
// Deferring #4, which made #5 partial, ends the park: #5 applies whole
|
||||
// on what the batch carries now.
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "4", "--reason", "fails the e2e", "--push")
|
||||
if code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"missing":[5]`) {
|
||||
t.Errorf("run --defer 4 = %d, want #5 missing, no longer parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check after deferring #4 = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A workflow original that waits for a maintainer only as the open record
|
||||
// lists it: one merged since the batch was built is missing, and so is
|
||||
// what waits on it.
|
||||
func TestUnlistedWorkflowWaitIsMissing(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
f.pr(8, "fix(ci): run the new release on tags", "", map[string]string{".github/workflows/new.yaml": "on: tag\n", "hack/changelog.bats": "@test x {\n false\n}\n# eight\n", "app/eight.txt": appFile + "TWO\n"})
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/eight.txt": appFile + "TWO!\n"})
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/9 fix(app): nine (it waits on #8, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #9 missing, waiting on #8:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The line's own rename stands for the original's only with the original's
|
||||
// change in it: a fold that leaves the renamed file as it was is refused.
|
||||
func TestSameRenameStillNeedsItsFix(t *testing.T) {
|
||||
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
|
||||
mc5 := f.pr(5, "fix(app): rename p to q, fix l10, and five", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1), "app/five.txt": "5\n"})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): rename p to q, fix l10, and five (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/p.txt" without effect`) {
|
||||
t.Errorf("fold without the fix in q = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite the line carries is there, though the rules would keep it
|
||||
// out: a feat carried by a hand adaptation does not park what builds on it.
|
||||
func TestCarriedExcludedPrerequisiteSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
mc3 := f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.onLine("release-1.1", "feat(app): a loud two, adapted\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "Two", 1)})
|
||||
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) {
|
||||
t.Errorf("run = %d, want #5 not parked on the carried #3:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// With no batch open, a workflow wait a closed record keeps waits on
|
||||
// nothing: the original is judged as the scheduled run judges it, so one
|
||||
// that also builds on a feat the line lacks is parked, in check as in run.
|
||||
func TestClosedWorkflowWaitIsJudgedAsRunJudgesIt(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: 2, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 7, Files: []string{".github/workflows/tap.yaml"}, Workflows: true}}, Dropped: []droppedEntry{}, Stale: []int{}}
|
||||
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
pr.State, pr.MergedAt = "closed", &at
|
||||
gh := &fakeGH{prs: []pullRequest{pr}}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); strings.Contains(stdout, "/pull/7 ") || !strings.Contains(stderr, "note: #7 is parked: it builds on #20") {
|
||||
t.Errorf("check --cut = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
|
||||
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A pick that comes out empty is judged too: #5's own commit did half its
|
||||
// fix, which the line has, and a merge of main into its branch did the
|
||||
// rest, which no pick carries; #5 is not on the line.
|
||||
func TestEmptyPicksStillJudged(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: b", "", map[string]string{"app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): b, half of it", map[string]string{"app/b.txt": "middle\n"})
|
||||
f.git("switch", "-q", "-c", "pr-5", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "middle\n"})
|
||||
f.git("commit", "-q", "-m", "b, half of it")
|
||||
f.git("switch", "-q", "main")
|
||||
f.apply(map[string]string{"app/main.txt": "moved\n"})
|
||||
f.git("commit", "-q", "-m", "chore: move main along")
|
||||
f.git("switch", "-q", "pr-5")
|
||||
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
|
||||
f.git("switch", "-q", "main")
|
||||
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b (#5)", "pr-5")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// --defer takes off a unit that does only part of its original whatever
|
||||
// rule now keeps that original out, as check tells it to.
|
||||
func TestDeferTakesOffAPartialExcludedUnit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n"})
|
||||
mc5 := f.pr(5, "feat(chart): remove the policy, and test values", "", map[string]string{"chart/templates/policy.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
|
||||
gh := &fakeGH{}
|
||||
handBatch(f, gh, mc5)
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "only part of it applies") {
|
||||
t.Fatalf("check --cut = %d, want the partial unit to fail it:\n%s", code, stdout)
|
||||
}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push"); code != 0 || prState(gh.find(90)) != "closed" {
|
||||
t.Errorf("run --defer 5 --push = %d, #90 %s; want the unit taken off and the batch closed:\n%s", code, prState(gh.find(90)), stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A stored wait on an original merged after the one that waits cannot be
|
||||
// what a run wrote.
|
||||
func TestForwardWaitIsRefused(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 5, Files: []string{}, Needs: []int{7}}}, Dropped: []droppedEntry{}, Stale: []int{}}
|
||||
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
|
||||
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")}}
|
||||
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "#5 wait on #7") {
|
||||
t.Errorf("check = %d, want 2 for a wait on a later original:\n%s", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The workflow exception looks only at what still keeps the original from
|
||||
// the line: a workflow line its prerequisite wrote, which the open batch
|
||||
// carries though that prerequisite is labelled backport/skip, does not
|
||||
// stop it, in check as in run.
|
||||
func TestWorkflowExceptionIgnoresWhatIsThere(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(4, "fix(ci): run the release on pushes", "", map[string]string{".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(ci): run the release on tags too", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
gh.labels = map[string][]int{labelSkip: {4}}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
|
||||
t.Fatalf("scheduled run = %d, want #7 waiting for a maintainer:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #7 waiting, as run has it:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty pick leaves a deletion undone that only a merge of main into the
|
||||
// original's branch made: the original is not on the line.
|
||||
func TestEmptyPickLeavesADeletionUndone(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old feature\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): b, already", map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("switch", "-q", "-c", "pr-5", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("commit", "-q", "-m", "b")
|
||||
f.git("switch", "-q", "main")
|
||||
f.apply(map[string]string{"app/main.txt": "moved\n"})
|
||||
f.git("commit", "-q", "-m", "chore: move main along")
|
||||
f.git("switch", "-q", "pr-5")
|
||||
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
|
||||
f.git("rm", "-q", "app/a.txt")
|
||||
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
|
||||
f.git("switch", "-q", "main")
|
||||
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b, and a goes (#5)", "pr-5")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line already has as the original makes it builds on nothing
|
||||
// the line lacks: #5 fixes a.txt, which the line fixed the same way, and
|
||||
// b.txt, so it picks whole and is not parked on the feat that wrote a.txt.
|
||||
func TestAlreadyAppliedFileBuildsOnNothing(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.onLine("release-1.1", "fix(app): two, the line's way", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(5, "fix(app): an excited two, and b", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": "fixed\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// policyPage is the policy the batch applies. The block in it that names when
|
||||
// each line started under the policy is read from the page itself, on the ref
|
||||
// the run pins, so the page and the batch cannot drift apart.
|
||||
const policyPage = "docs/release/backports.md"
|
||||
|
||||
const policyFence = "```backport-policy"
|
||||
|
||||
var (
|
||||
policyLineRE = regexp.MustCompile(`^(release-\d+\.\d+|` + batchOnlyKey + `):[ \t]*(\d{4}-\d{2}-\d{2})[ \t]*$`)
|
||||
policyReleasedRE = regexp.MustCompile(`^` + releasedKey + `:[ \t]*(\d+)[ \t]*$`)
|
||||
)
|
||||
|
||||
const (
|
||||
// batchOnlyKey names the day from which the batch is the only way onto a
|
||||
// line, so that from then on only its picks show what a line carries.
|
||||
batchOnlyKey = "batch-only"
|
||||
// releasedKey names how many of the lines that have shipped a stable
|
||||
// release the batch runs for, the newest of them; defaultReleased when
|
||||
// the block does not say.
|
||||
releasedKey = "released-lines"
|
||||
defaultReleased = 2
|
||||
)
|
||||
|
||||
// The rule 2 types and the scopes that never go into a patch release, as the
|
||||
// policy page lists them; policy_test.go holds the page to these.
|
||||
var (
|
||||
takenTypes = []string{"fix", "security"}
|
||||
excludedScopes = []string{"deps"}
|
||||
)
|
||||
|
||||
// starts maps a release line to the day it started under the policy. A line
|
||||
// that is not listed started at its fork point from main.
|
||||
type starts map[string]time.Time
|
||||
|
||||
// policy is what the backport-policy block says: when each line started, the
|
||||
// batch-only day, and how many released lines the batch runs for.
|
||||
type policy struct {
|
||||
starts starts
|
||||
batchOnly time.Time
|
||||
released int
|
||||
}
|
||||
|
||||
// parsePolicy reads the one backport-policy block of the page: a line per
|
||||
// release line, `release-X.Y: YYYY-MM-DD`, exactly one `batch-only:
|
||||
// YYYY-MM-DD`, at most one `released-lines: N`, and comments starting with #.
|
||||
func parsePolicy(page string) (*policy, error) {
|
||||
var blocks [][]string
|
||||
var cur []string
|
||||
in := false
|
||||
for _, ln := range strings.Split(strings.ReplaceAll(page, "\r\n", "\n"), "\n") {
|
||||
t := strings.TrimSpace(ln)
|
||||
switch {
|
||||
case !in && t == policyFence:
|
||||
in, cur = true, nil
|
||||
case in && t == "```":
|
||||
in = false
|
||||
blocks = append(blocks, cur)
|
||||
case in:
|
||||
cur = append(cur, t)
|
||||
}
|
||||
}
|
||||
if in {
|
||||
return nil, fmt.Errorf("%s: the %s block is not closed", policyPage, policyFence)
|
||||
}
|
||||
if len(blocks) != 1 {
|
||||
return nil, fmt.Errorf("%s holds %d %s blocks, not one", policyPage, len(blocks), policyFence)
|
||||
}
|
||||
out := &policy{starts: starts{}, released: defaultReleased}
|
||||
seen := map[string]bool{}
|
||||
for _, t := range blocks[0] {
|
||||
if t == "" || strings.HasPrefix(t, "#") {
|
||||
continue
|
||||
}
|
||||
if m := policyReleasedRE.FindStringSubmatch(t); m != nil {
|
||||
n, err := strconv.Atoi(m[1])
|
||||
switch {
|
||||
case err != nil || n < 1:
|
||||
return nil, fmt.Errorf("%s: %s is %q, not a number of lines", policyPage, releasedKey, m[1])
|
||||
case seen[releasedKey]:
|
||||
return nil, fmt.Errorf("%s: %s is listed twice", policyPage, releasedKey)
|
||||
}
|
||||
seen[releasedKey], out.released = true, n
|
||||
continue
|
||||
}
|
||||
m := policyLineRE.FindStringSubmatch(t)
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("%s: %q in the %s block is not `release-X.Y: YYYY-MM-DD`, `%s: YYYY-MM-DD` or `%s: N`", policyPage, t, policyFence, batchOnlyKey, releasedKey)
|
||||
}
|
||||
day, err := time.Parse(time.DateOnly, m[2])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %q is not a date: %w", policyPage, m[2], err)
|
||||
}
|
||||
if seen[m[1]] {
|
||||
return nil, fmt.Errorf("%s: %s is listed twice", policyPage, m[1])
|
||||
}
|
||||
seen[m[1]] = true
|
||||
if m[1] == batchOnlyKey {
|
||||
out.batchOnly = day
|
||||
continue
|
||||
}
|
||||
if _, err := parseLine(m[1]); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", policyPage, err)
|
||||
}
|
||||
out.starts[m[1]] = day
|
||||
}
|
||||
if !seen[batchOnlyKey] {
|
||||
return nil, fmt.Errorf("%s: the %s block gives no %s day", policyPage, policyFence, batchOnlyKey)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func readPolicy(commit string) (*policy, error) {
|
||||
page, err := git("show", "--end-of-options", commit+":"+policyPage)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading the policy at %.12s: %w", commit, err)
|
||||
}
|
||||
return parsePolicy(page)
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParsePolicy(t *testing.T) {
|
||||
got, err := parsePolicy("text\n\n" + policyFence + "\n# comment\n\nrelease-1.6: 2026-10-26\nrelease-1.5:2026-01-02\nbatch-only: 2026-10-20\n```\n")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.starts) != 2 || !got.starts["release-1.6"].Equal(time.Date(2026, 10, 26, 0, 0, 0, 0, time.UTC)) {
|
||||
t.Errorf("parsePolicy = %v", got)
|
||||
}
|
||||
if !got.batchOnly.Equal(time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC)) {
|
||||
t.Errorf("the batch-only day = %v, want 2026-10-20", got.batchOnly)
|
||||
}
|
||||
if got.released != 2 {
|
||||
t.Errorf("released lines with none given = %d, want 2", got.released)
|
||||
}
|
||||
if got, err := parsePolicy(policyFence + "\nbatch-only: 2026-10-20\n```\n"); err != nil || len(got.starts) != 0 {
|
||||
t.Errorf("a block with no line = %v, %v; want no starts", got, err)
|
||||
}
|
||||
if got, err := parsePolicy(policyFence + "\nbatch-only: 2026-10-20\nreleased-lines: 3\n```\n"); err != nil || got.released != 3 {
|
||||
t.Errorf("released-lines: 3 = %v, %v; want 3", got, err)
|
||||
}
|
||||
for name, page := range map[string]string{
|
||||
"no batch-only day": policyFence + "\nrelease-1.6: 2026-10-26\n```\n",
|
||||
"two batch-only days": policyFence + "\nbatch-only: 2026-10-26\nbatch-only: 2026-10-27\n```\n",
|
||||
"a bad batch-only day": policyFence + "\nbatch-only: soon\n```\n",
|
||||
"no block": "## Backports\n",
|
||||
"two blocks": policyFence + "\n```\n" + policyFence + "\n```\n",
|
||||
"unclosed": policyFence + "\nrelease-1.6: 2026-10-26\n",
|
||||
"a bad line": policyFence + "\nrelease 1.6 on 2026-10-26\n```\n",
|
||||
"a bad date": policyFence + "\nrelease-1.6: 2026-13-40\n```\n",
|
||||
"a padded line": policyFence + "\nrelease-01.6: 2026-10-26\n```\n",
|
||||
"listed twice": policyFence + "\nrelease-1.6: 2026-10-26\nrelease-1.6: 2026-10-27\n```\n",
|
||||
"not a line": policyFence + "\nmain: 2026-10-26\n```\n",
|
||||
"trailing words": policyFence + "\nrelease-1.6: 2026-10-26 or so\n```\n",
|
||||
"no released lines": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 0\n```\n",
|
||||
"released lines twice": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 2\nreleased-lines: 3\n```\n",
|
||||
"released lines words": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: two\n```\n",
|
||||
"released lines huge": policyFence + "\nbatch-only: 2026-10-26\nreleased-lines: 99999999999999999999\n```\n",
|
||||
} {
|
||||
if _, err := parsePolicy(page); err == nil {
|
||||
t.Errorf("%s: parsePolicy accepted it", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A line listed in the policy takes nothing merged before its start, unless a
|
||||
// maintainer lets an original in; any other line starts at its fork point.
|
||||
func TestPolicyStart(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "feat(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
gh := &fakeGH{labels: map[string][]int{labelInclude: {6, 7}}}
|
||||
|
||||
verdicts := func() map[int]*verdict {
|
||||
t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
l, _ := parseLine("release-1.1")
|
||||
b, err := (&app{gh: gh, stderr: &strings.Builder{}}).prepare(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return map[int]*verdict{5: b.find(5), 6: b.find(6), 7: b.find(7)}
|
||||
}
|
||||
if v := verdicts(); v[5].rule != 0 || v[6].rule != 0 || v[7].rule != 0 {
|
||||
t.Errorf("with no start every original qualifies: %d %d %d", v[5].rule, v[6].rule, v[7].rule)
|
||||
}
|
||||
|
||||
f.setPolicy("release-1.1: 2099-01-01\n")
|
||||
v := verdicts()
|
||||
if v[5].rule != ruleBeforeStart {
|
||||
t.Errorf("#5 = rule %d (%s), want merged before the start", v[5].rule, v[5].reason)
|
||||
}
|
||||
if v[6].rule != 0 || v[7].rule != 0 {
|
||||
t.Errorf("backport/include lifts the start: #6 = rule %d, #7 = rule %d", v[6].rule, v[7].rule)
|
||||
}
|
||||
|
||||
f.setPolicy("release-1.0: 2099-01-01\n")
|
||||
if v := verdicts(); v[5].rule != 0 {
|
||||
t.Errorf("a start for another line kept #5 out of release-1.1: rule %d", v[5].rule)
|
||||
}
|
||||
|
||||
f.setPolicy("release-1.1: 2000-01-01\n")
|
||||
if v := verdicts(); v[5].rule != 0 {
|
||||
t.Errorf("a start before the fork point kept #5 out: rule %d", v[5].rule)
|
||||
}
|
||||
}
|
||||
|
||||
// The policy is read from main as the run pins it, or from the ref --policy
|
||||
// names, and a run without a readable policy stops. Only the commands that
|
||||
// read the policy take --policy.
|
||||
func TestPolicyRef(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.git("switch", "-q", "-c", "proposal")
|
||||
f.write(policyPage, policyWith("release-1.1: 2099-01-01\n"+releasedKey+": 1\n"))
|
||||
f.git("commit", "-q", "-am", "docs: start release-1.1 later")
|
||||
f.git("switch", "-q", "main")
|
||||
|
||||
code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1")
|
||||
if code != 1 || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check against main's policy = %d, %q, %s; want #5 missing", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, stderr = f.exec(nil, "check", "--line", "release-1.1", "--policy", "proposal")
|
||||
if code != 0 {
|
||||
t.Errorf("check against the proposal's policy = %d, %q, %s; want #5 before the start", code, stdout, stderr)
|
||||
}
|
||||
if code, _, _ := f.exec(nil, "check", "--line", "release-1.1", "--policy", "--upload-pack=x"); code != 2 {
|
||||
t.Errorf("--policy with an option = %d, want 2", code)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(nil, "lines", "--policy", "proposal"); code != 0 || stdout != "release-1.1\n" {
|
||||
t.Errorf("lines against the proposal's policy = %d, %q (%s); want release-1.1 alone", code, stdout, stderr)
|
||||
}
|
||||
for _, cmd := range []string{"verify", "notify"} {
|
||||
if code, _, stderr := f.exec(&fakeGH{}, cmd, "--line", "release-1.1", "--policy", "proposal"); code != 2 || !strings.Contains(stderr, "flag provided but not defined: -policy") {
|
||||
t.Errorf("%s --policy = %d (%s); want it refused, as %s reads no policy", cmd, code, stderr, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
f.squash(9001, "docs: drop the page", "", map[string]string{policyPage: ""})
|
||||
if code, _, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "reading the policy") {
|
||||
t.Errorf("check without a policy page = %d (%s), want 2", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The policy page and the code say the same thing: the start block parses as
|
||||
// the batch reads it, rule 2 names the types the code takes, the page names
|
||||
// the scopes the code keeps out, and both override labels.
|
||||
func TestPolicyPageMatchesCode(t *testing.T) {
|
||||
data, err := os.ReadFile("../../" + policyPage)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page := string(data)
|
||||
pol, err := parsePolicy(page)
|
||||
if err != nil {
|
||||
t.Fatalf("the page's policy block does not parse: %v", err)
|
||||
}
|
||||
if day, ok := pol.starts["release-1.6"]; !ok || day.Format(time.DateOnly) != "2026-10-26" {
|
||||
t.Errorf("release-1.6 starts %v, want 2026-10-26", pol.starts["release-1.6"])
|
||||
}
|
||||
if day := pol.batchOnly.Format(time.DateOnly); day != "2026-10-26" {
|
||||
t.Errorf("the batch-only day is %s, want 2026-10-26", day)
|
||||
}
|
||||
// The page says how many released lines the batch runs for, rather than
|
||||
// leaving it to the default.
|
||||
if !regexp.MustCompile("(?m)^"+releasedKey+":").MatchString(page) || pol.released != 2 {
|
||||
t.Errorf("the page's block gives %d released lines, want 2 written out", pol.released)
|
||||
}
|
||||
|
||||
rule2 := regexp.MustCompile("(?m)^2\\. Its title type is (.*)$").FindStringSubmatch(page)
|
||||
if rule2 == nil {
|
||||
t.Fatal("the page has no rule 2")
|
||||
}
|
||||
var types []string
|
||||
for _, m := range regexp.MustCompile("`([a-z]+)`").FindAllStringSubmatch(rule2[1], -1) {
|
||||
types = append(types, m[1])
|
||||
}
|
||||
if !slices.Equal(types, takenTypes) {
|
||||
t.Errorf("rule 2 on the page takes %v, the code %v", types, takenTypes)
|
||||
}
|
||||
never := regexp.MustCompile("(?m)^### What waits for the next minor\\n\\n(.*)$").FindStringSubmatch(page)
|
||||
if never == nil {
|
||||
t.Fatal("the page has no list of what never goes into a patch release")
|
||||
}
|
||||
for _, scope := range excludedScopes {
|
||||
if !strings.Contains(never[1], "(scope `"+scope+"`)") {
|
||||
t.Errorf("the page does not keep scope %s out", scope)
|
||||
}
|
||||
}
|
||||
if scopes := regexp.MustCompile("scope `([a-z]+)`").FindAllStringSubmatch(never[1], -1); len(scopes) != len(excludedScopes) {
|
||||
t.Errorf("the page keeps %d scopes out, the code %d", len(scopes), len(excludedScopes))
|
||||
}
|
||||
for i := 1; i <= 6; i++ {
|
||||
if !regexp.MustCompile("(?m)^" + string(rune('0'+i)) + "\\. ").MatchString(page) {
|
||||
t.Errorf("the page has no rule %d", i)
|
||||
}
|
||||
}
|
||||
for _, label := range []string{labelInclude, labelSkip} {
|
||||
if !strings.Contains(page, "`"+label+"`") {
|
||||
t.Errorf("the page does not describe %s", label)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,682 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A line's record is the backport-batch block of its newest batch pull
|
||||
// request, read whatever that pull request's state and written only while it
|
||||
// is open: a merged or closed pull request is never written again, so what it
|
||||
// recorded stays what the line's later commands act on until a newer batch
|
||||
// carries a record of its own.
|
||||
|
||||
// lineRecord is the one line of JSON in the block. Version 1, the block
|
||||
// before it had a version, holds only line and deferred; version 3 adds what
|
||||
// is parked and which units on the batch do only part of their original.
|
||||
type lineRecord struct {
|
||||
Version int `json:"version"`
|
||||
Line string `json:"line"`
|
||||
Built *builtFrom `json:"built,omitempty"`
|
||||
Report string `json:"report,omitempty"`
|
||||
Closing *closing `json:"closing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
Parked []parkedEntry `json:"parked,omitempty"`
|
||||
Partial []int `json:"partial,omitempty"`
|
||||
// Changed are the picks that differ from their originals.
|
||||
Changed []int `json:"changed,omitempty"`
|
||||
}
|
||||
|
||||
// recordVersion is the version a run writes.
|
||||
const recordVersion = 3
|
||||
|
||||
// lineRecordV2 is version 2, which has none of version 3's keys.
|
||||
type lineRecordV2 struct {
|
||||
Version int `json:"version"`
|
||||
Line string `json:"line"`
|
||||
Built *builtFrom `json:"built,omitempty"`
|
||||
Report string `json:"report,omitempty"`
|
||||
Closing *closing `json:"closing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
}
|
||||
|
||||
// The kinds of park: an original that builds on what never reaches the
|
||||
// line, and one whose clean pick would leave part of it without effect.
|
||||
const (
|
||||
parkDependency = "dependency"
|
||||
parkPartial = "partial"
|
||||
)
|
||||
|
||||
// parkedEntry is an original the batch leaves out because it cannot land on
|
||||
// the line whole: what it needs that never comes, or the paths it would
|
||||
// leave without effect. announced says notify has named it on the open
|
||||
// batch, for exactly this entry.
|
||||
type parkedEntry struct {
|
||||
Original int `json:"original"`
|
||||
Merge string `json:"merge"`
|
||||
Kind string `json:"kind"`
|
||||
Needs []int `json:"needs,omitempty"`
|
||||
Commits []string `json:"commits,omitempty"`
|
||||
Files []string `json:"files,omitempty"`
|
||||
Announced bool `json:"announced,omitempty"`
|
||||
}
|
||||
|
||||
// parkEvent is what makes a park the same park from one record to the
|
||||
// next: its merge, kind, prerequisites, commits and files. A park whose
|
||||
// event changed is news, and notify names it again.
|
||||
func parkEvent(p parkedEntry) string {
|
||||
data, _ := json.Marshal(struct {
|
||||
Merge string `json:"merge"`
|
||||
Kind string `json:"kind"`
|
||||
Needs []int `json:"needs"`
|
||||
Commits []string `json:"commits"`
|
||||
Files []string `json:"files"`
|
||||
}{p.Merge, p.Kind, p.Needs, p.Commits, p.Files})
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// carryAnnounced marks each park announced that the carrier's record holds
|
||||
// announced as the same event, a closed carrier's too, so a new batch does
|
||||
// not name again what an earlier one named.
|
||||
func carryAnnounced(parked []parkedEntry, carrier *lineRecord) []parkedEntry {
|
||||
out := slices.Clone(parked)
|
||||
for i := range out {
|
||||
out[i].Announced = false
|
||||
}
|
||||
if carrier == nil {
|
||||
return out
|
||||
}
|
||||
for i := range out {
|
||||
out[i].Announced = slices.ContainsFunc(carrier.Parked, func(c parkedEntry) bool {
|
||||
return c.Announced && c.Original == out[i].Original && parkEvent(c) == parkEvent(out[i])
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// validate holds a parked entry to what a run writes. Its errors read after
|
||||
// "the block" or "report <id>".
|
||||
func (p parkedEntry) validate() error {
|
||||
sortedUnique := func(n int, less func(i int) bool) bool {
|
||||
for i := 1; i < n; i++ {
|
||||
if !less(i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
switch {
|
||||
case checkSHA(p.Merge) != nil:
|
||||
return fmt.Errorf("parks #%d by merge %q", p.Original, p.Merge)
|
||||
case !sortedUnique(len(p.Needs), func(i int) bool { return p.Needs[i-1] < p.Needs[i] }) || slices.ContainsFunc(p.Needs, func(n int) bool { return n <= 0 || n == p.Original }):
|
||||
return fmt.Errorf("parks #%d for originals %v, which are not what a run writes", p.Original, p.Needs)
|
||||
case !sortedUnique(len(p.Commits), func(i int) bool { return p.Commits[i-1] < p.Commits[i] }) || slices.ContainsFunc(p.Commits, func(c string) bool { return checkSHA(c) != nil }):
|
||||
return fmt.Errorf("parks #%d for commits %q, which are not what a run writes", p.Original, p.Commits)
|
||||
case !sortedUnique(len(p.Files), func(i int) bool { return p.Files[i-1] < p.Files[i] }) || !slices.Equal(p.Files, cleanFiles(p.Files)):
|
||||
return fmt.Errorf("parks #%d for files %q, which are not what a run writes", p.Original, p.Files)
|
||||
}
|
||||
switch p.Kind {
|
||||
case parkPartial:
|
||||
if len(p.Files) == 0 || len(p.Needs)+len(p.Commits) > 0 {
|
||||
return fmt.Errorf("parks #%d as partial with no files it leaves without effect, or with what it needs", p.Original)
|
||||
}
|
||||
case parkDependency:
|
||||
if len(p.Needs)+len(p.Commits) == 0 || len(p.Files) > 0 {
|
||||
return fmt.Errorf("parks #%d for a dependency it does not name, or with files", p.Original)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("parks #%d as %q, which is no kind of park", p.Original, p.Kind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// builtFrom is what a classification was made of; batch is the head it
|
||||
// describes.
|
||||
type builtFrom struct {
|
||||
Main string `json:"main"`
|
||||
Line string `json:"line"`
|
||||
Newer string `json:"newer"`
|
||||
Policy string `json:"policy"`
|
||||
Batch string `json:"batch"`
|
||||
Tool string `json:"tool"`
|
||||
Run string `json:"run"`
|
||||
At string `json:"at"`
|
||||
Options runOptions `json:"options"`
|
||||
}
|
||||
|
||||
type runOptions struct {
|
||||
NoWorkflowFiles bool `json:"noWorkflowFiles"`
|
||||
Mode string `json:"mode"`
|
||||
}
|
||||
|
||||
// The operations a record is written by: a run that assembles the batch, and
|
||||
// the ones that only change what the record says about it.
|
||||
const (
|
||||
modeNew = "new"
|
||||
modeContinue = "continue"
|
||||
modeDefer = "defer"
|
||||
modeDrop = "drop"
|
||||
modeUndrop = "undrop"
|
||||
modeClose = "close"
|
||||
)
|
||||
|
||||
var modes = []string{modeNew, modeContinue, modeDefer, modeDrop, modeUndrop, modeClose}
|
||||
|
||||
// closing is written into the record of an open batch that the run is about
|
||||
// to close: the head it froze, which the close and the branch's deletion are
|
||||
// leased on.
|
||||
type closing struct {
|
||||
PR int `json:"pr"`
|
||||
Branch string `json:"branch"`
|
||||
Head string `json:"head"`
|
||||
}
|
||||
|
||||
// droppedEntry is a maintainer's decision that an original stays off the
|
||||
// line, bound to its merge commit on main.
|
||||
type droppedEntry struct {
|
||||
Original int `json:"original"`
|
||||
Merge string `json:"merge"`
|
||||
Reason string `json:"reason"`
|
||||
On int `json:"on"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
|
||||
var (
|
||||
toolRE = regexp.MustCompile(`^[0-9a-f]{40}(\+dirty)?$`)
|
||||
reportIDRE = regexp.MustCompile(`^[0-9a-f]{64}$`)
|
||||
)
|
||||
|
||||
// oneLineText is s as a run writes a reason: one line, cleaned and capped.
|
||||
// A record holding anything else was not written by a run, and its text
|
||||
// would reach a terminal, a body or a comment as someone else wrote it.
|
||||
func oneLineText(s string) string { return capped(oneLine(clean(s, false)), titleLimit) }
|
||||
|
||||
// fencedLines lists the one line of each block that fence opens.
|
||||
func fencedLines(body, fence string) ([]string, error) {
|
||||
var blocks []string
|
||||
lines := strings.Split(strings.ReplaceAll(body, "\r\n", "\n"), "\n")
|
||||
for i := 0; i < len(lines); i++ {
|
||||
if strings.TrimSpace(lines[i]) != fence {
|
||||
continue
|
||||
}
|
||||
if i+2 >= len(lines) || strings.TrimSpace(lines[i+2]) != "```" {
|
||||
return nil, fmt.Errorf("the %s block is not one line of JSON", fence)
|
||||
}
|
||||
blocks = append(blocks, lines[i+1])
|
||||
i += 2
|
||||
}
|
||||
return blocks, nil
|
||||
}
|
||||
|
||||
// decodeStrict reads exactly one JSON value into v, refusing a key v does not
|
||||
// have and anything after the value.
|
||||
func decodeStrict(data string, v any) error {
|
||||
dec := json.NewDecoder(strings.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
var rest json.RawMessage
|
||||
if dec.More() || dec.Decode(&rest) != io.EOF {
|
||||
return errors.New("something follows the JSON value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseRecord reads the line's record from a batch body: nil when the body
|
||||
// has no block, an error when it has more than one or one it cannot vouch
|
||||
// for whole.
|
||||
func parseRecord(body string, l line) (*lineRecord, error) {
|
||||
blocks, err := fencedLines(body, deferredFence)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, err
|
||||
case len(blocks) == 0:
|
||||
return nil, nil
|
||||
case len(blocks) > 1:
|
||||
return nil, fmt.Errorf("the body holds %d %s blocks", len(blocks), deferredFence)
|
||||
}
|
||||
var head struct {
|
||||
Version *int `json:"version"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(blocks[0]), &head); err != nil {
|
||||
return nil, fmt.Errorf("reading the %s block: %w", deferredFence, err)
|
||||
}
|
||||
version := 1
|
||||
if head.Version != nil {
|
||||
version = *head.Version
|
||||
}
|
||||
var rec lineRecord
|
||||
switch version {
|
||||
case 1:
|
||||
var v1 struct {
|
||||
Version int `json:"version"`
|
||||
Line string `json:"line"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
}
|
||||
err = decodeStrict(blocks[0], &v1)
|
||||
rec = lineRecord{Version: 1, Line: v1.Line, Deferred: v1.Deferred}
|
||||
case 2:
|
||||
var v2 lineRecordV2
|
||||
err = decodeStrict(blocks[0], &v2)
|
||||
rec = lineRecordWithout(v2)
|
||||
case 3:
|
||||
err = decodeStrict(blocks[0], &rec)
|
||||
default:
|
||||
return nil, fmt.Errorf("the %s block is version %d, which this backport-batch does not read; it reads 1 to %d", deferredFence, version, recordVersion)
|
||||
}
|
||||
if err == nil && version < 3 && slices.ContainsFunc(rec.Deferred, func(d deferredEntry) bool { return d.Needs != nil }) {
|
||||
err = fmt.Errorf("version %d has no waits on other originals", version)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading the %s block: %w", deferredFence, err)
|
||||
}
|
||||
if err := rec.validate(l); err != nil {
|
||||
return nil, fmt.Errorf("the %s block %w", deferredFence, err)
|
||||
}
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
// validate holds the record to what a run writes. Its errors read after
|
||||
// "the block".
|
||||
func (rec *lineRecord) validate(l line) error {
|
||||
if rec.Line != l.branch() {
|
||||
return fmt.Errorf("is for %q, not %s", rec.Line, l.branch())
|
||||
}
|
||||
seen := map[int]bool{}
|
||||
once := func(n int) error {
|
||||
if n <= 0 {
|
||||
return fmt.Errorf("names original %d", n)
|
||||
}
|
||||
if seen[n] {
|
||||
return fmt.Errorf("names #%d twice", n)
|
||||
}
|
||||
seen[n] = true
|
||||
return nil
|
||||
}
|
||||
for _, d := range rec.Deferred {
|
||||
if err := once(d.Original); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Commit != "" && checkSHA(d.Commit) != nil {
|
||||
return fmt.Errorf("names commit %q for #%d", d.Commit, d.Original)
|
||||
}
|
||||
// A conflict names the commit that failed and its files, a deferral
|
||||
// its reason, and a wait its workflow files.
|
||||
kinds := 0
|
||||
for _, is := range []bool{d.Commit != "" || len(d.Files) > 0 && !d.Workflows && d.Reason == "" && len(d.Needs) == 0, d.Reason != "", d.Workflows, len(d.Needs) > 0} {
|
||||
if is {
|
||||
kinds++
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case d.Workflows && (d.Commit != "" || d.Reason != ""):
|
||||
return fmt.Errorf("has #%d wait for a maintainer and defers it as well", d.Original)
|
||||
case kinds != 1:
|
||||
return fmt.Errorf("does not say whether #%d is a conflict, deferred for a reason or waiting", d.Original)
|
||||
case d.Reason != "" && strings.TrimSpace(d.Reason) == "":
|
||||
return fmt.Errorf("defers #%d for an empty reason", d.Original)
|
||||
case d.Reason != oneLineText(d.Reason):
|
||||
return fmt.Errorf("defers #%d for %q, which is no reason a run writes", d.Original, d.Reason)
|
||||
case !slices.Equal(d.Files, cleanFiles(d.Files)):
|
||||
return fmt.Errorf("names files of #%d that are not as a run writes them: %q", d.Original, d.Files)
|
||||
case d.More < 0:
|
||||
return fmt.Errorf("counts %d more files for #%d", d.More, d.Original)
|
||||
case len(d.Needs) > 0 && (len(d.Files) > 0 || d.More != 0):
|
||||
return fmt.Errorf("has #%d wait on other originals with files of its own", d.Original)
|
||||
}
|
||||
for i, n := range d.Needs {
|
||||
if n <= 0 || n == d.Original || i > 0 && d.Needs[i-1] >= n {
|
||||
return fmt.Errorf("has #%d wait on %v, which is not what a run writes", d.Original, d.Needs)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, d := range rec.Dropped {
|
||||
if err := once(d.Original); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := time.Parse(time.DateOnly, d.Date); err != nil || checkSHA(d.Merge) != nil || strings.TrimSpace(d.Reason) == "" || d.On <= 0 {
|
||||
return fmt.Errorf("drops #%d with a merge, reason, pull request or date it cannot have", d.Original)
|
||||
}
|
||||
if d.Reason != oneLineText(d.Reason) {
|
||||
return fmt.Errorf("drops #%d for %q, which is no reason a run writes", d.Original, d.Reason)
|
||||
}
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
if err := once(p.Original); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
staleSeen := map[int]bool{}
|
||||
for _, n := range rec.Stale {
|
||||
if n <= 0 || staleSeen[n] {
|
||||
return fmt.Errorf("lists %d as still on the batch more than once or not at all", n)
|
||||
}
|
||||
staleSeen[n] = true
|
||||
}
|
||||
changedSeen := map[int]bool{}
|
||||
for _, n := range rec.Changed {
|
||||
if n <= 0 || changedSeen[n] {
|
||||
return fmt.Errorf("lists %d as a changed pick more than once or not at all", n)
|
||||
}
|
||||
changedSeen[n] = true
|
||||
}
|
||||
if err := rec.waitsHold(); err != nil {
|
||||
return err
|
||||
}
|
||||
// What is on the batch is neither deferred nor parked.
|
||||
left := map[int]bool{}
|
||||
for _, d := range rec.Deferred {
|
||||
left[d.Original] = true
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
left[p.Original] = true
|
||||
if slices.Contains(p.Commits, p.Merge) {
|
||||
return fmt.Errorf("parks #%d for its own merge", p.Original)
|
||||
}
|
||||
}
|
||||
for _, list := range [][]int{rec.Partial, rec.Changed} {
|
||||
for _, n := range list {
|
||||
if left[n] {
|
||||
return fmt.Errorf("has #%d on the batch and left off it at once", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
partialSeen := map[int]bool{}
|
||||
for _, n := range rec.Partial {
|
||||
// A unit that does only part of its original is still on the batch.
|
||||
if !staleSeen[n] || partialSeen[n] {
|
||||
return fmt.Errorf("lists #%d as a partial unit more than once, or not as still on the batch", n)
|
||||
}
|
||||
partialSeen[n] = true
|
||||
}
|
||||
if rec.Version < 2 {
|
||||
return nil
|
||||
}
|
||||
if b := rec.Built; b == nil {
|
||||
return errors.New("does not say what it was built from")
|
||||
} else if err := b.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !reportIDRE.MatchString(rec.Report) {
|
||||
return fmt.Errorf("names report %q", rec.Report)
|
||||
}
|
||||
if c := rec.Closing; c != nil {
|
||||
m := batchBranchRE.FindStringSubmatch(c.Branch)
|
||||
if c.PR <= 0 || m == nil || m[1] != l.branch() || checkSHA(c.Head) != nil {
|
||||
return fmt.Errorf("closes #%d on %q at %q, which is no batch of %s", c.PR, c.Branch, c.Head, l.branch())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *builtFrom) validate() error {
|
||||
for _, sha := range []string{b.Main, b.Line, b.Policy, b.Batch} {
|
||||
if checkSHA(sha) != nil {
|
||||
return fmt.Errorf("says it was built from %q", sha)
|
||||
}
|
||||
}
|
||||
if b.Newer != "" && checkSHA(b.Newer) != nil {
|
||||
return fmt.Errorf("says it was built from %q", b.Newer)
|
||||
}
|
||||
if b.Tool != "" && !toolRE.MatchString(b.Tool) {
|
||||
return fmt.Errorf("names tool %q", b.Tool)
|
||||
}
|
||||
if b.Run != "" && !runURLRE.MatchString(b.Run) {
|
||||
return fmt.Errorf("names run %q", b.Run)
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, b.At); err != nil {
|
||||
return fmt.Errorf("was built at %q", b.At)
|
||||
}
|
||||
if !slices.Contains(modes, b.Options.Mode) {
|
||||
return fmt.Errorf("was written by %q, which is no operation of backport-batch", b.Options.Mode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var runURLRE = regexp.MustCompile(`^https://[^/\s]+/[^/\s]+/[^/\s]+/actions/runs/[0-9]+(/attempts/[0-9]+)?$`)
|
||||
|
||||
// waitsHold holds what waits and parks need to what the record says of the
|
||||
// originals they need: a wait needs ones still to come, so none parked or
|
||||
// dropped, a park ones that never come, so none deferred, and no wait ends
|
||||
// up waiting on itself. Its errors read after "the block".
|
||||
func (rec *lineRecord) waitsHold() error {
|
||||
deferred, never := map[int][]int{}, map[int]bool{}
|
||||
for _, d := range rec.Deferred {
|
||||
deferred[d.Original] = d.Needs
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
never[p.Original] = true
|
||||
}
|
||||
for _, d := range rec.Dropped {
|
||||
never[d.Original] = true
|
||||
}
|
||||
for _, d := range rec.Deferred {
|
||||
for _, n := range d.Needs {
|
||||
if never[n] {
|
||||
return fmt.Errorf("has #%d wait on #%d, which never comes", d.Original, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
for _, n := range p.Needs {
|
||||
if _, ok := deferred[n]; ok {
|
||||
return fmt.Errorf("parks #%d for #%d, which is still to come", p.Original, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
needs := map[int][]int{}
|
||||
for _, d := range rec.Deferred {
|
||||
needs[d.Original] = d.Needs
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
needs[p.Original] = p.Needs
|
||||
}
|
||||
return waitCycle(needs)
|
||||
}
|
||||
|
||||
// waitCycle refuses waits that lead back to where they started, which no
|
||||
// run writes: a prerequisite merged before what needs it.
|
||||
func waitCycle(needs map[int][]int) error {
|
||||
state := map[int]int{} // 1 on the path, 2 done
|
||||
var visit func(n int) error
|
||||
visit = func(n int) error {
|
||||
switch state[n] {
|
||||
case 1:
|
||||
return fmt.Errorf("has #%d wait on itself through what it waits on", n)
|
||||
case 2:
|
||||
return nil
|
||||
}
|
||||
state[n] = 1
|
||||
for _, f := range needs[n] {
|
||||
if err := visit(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
state[n] = 2
|
||||
return nil
|
||||
}
|
||||
for _, n := range slices.Sorted(maps.Keys(needs)) {
|
||||
if err := visit(n); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// lineRecordWithout is a version 2 record as the newest version holds it.
|
||||
func lineRecordWithout(v2 lineRecordV2) lineRecord {
|
||||
return lineRecord{Version: v2.Version, Line: v2.Line, Built: v2.Built, Report: v2.Report, Closing: v2.Closing, Deferred: v2.Deferred, Dropped: v2.Dropped, Stale: v2.Stale}
|
||||
}
|
||||
|
||||
// recordBlock is the block that holds rec. One line of JSON: encoding/json
|
||||
// escapes newlines and <, > and &, so nothing in a file name or a reason can
|
||||
// end the fence or open HTML.
|
||||
func recordBlock(rec lineRecord) string {
|
||||
data, _ := json.Marshal(rec)
|
||||
return deferredFence + "\n" + string(data) + "\n```"
|
||||
}
|
||||
|
||||
// savedNoWorkflowFiles is the --no-workflow-files the open batch was last
|
||||
// assembled with, which an operation that only changes the record keeps. A
|
||||
// record from before it was saved says so by listing a wait.
|
||||
func (s recordSource) savedNoWorkflowFiles() bool {
|
||||
if !s.open || s.rec == nil {
|
||||
return false
|
||||
}
|
||||
if s.rec.Built != nil {
|
||||
return s.rec.Built.Options.NoWorkflowFiles
|
||||
}
|
||||
return slices.ContainsFunc(s.rec.Deferred, func(d deferredEntry) bool { return d.Workflows })
|
||||
}
|
||||
|
||||
// dropped is what the record drops, which every later record keeps.
|
||||
func (rec *lineRecord) dropped() []droppedEntry {
|
||||
if rec == nil {
|
||||
return nil
|
||||
}
|
||||
return slices.Clone(rec.Dropped)
|
||||
}
|
||||
|
||||
// deferred maps the originals the record defers to their entries.
|
||||
func (rec *lineRecord) deferred() map[int]deferredEntry {
|
||||
out := map[int]deferredEntry{}
|
||||
if rec != nil {
|
||||
for _, d := range rec.Deferred {
|
||||
out[d.Original] = d
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// recordSource is where a line's record was found.
|
||||
type recordSource struct {
|
||||
// carrier is the pull request whose block is the record, nil when the
|
||||
// line has none.
|
||||
carrier *pullRequest
|
||||
// open says the carrier is the open batch, so the record may be written.
|
||||
open bool
|
||||
rec *lineRecord
|
||||
// code is the open batch pull request, if any. One whose body someone
|
||||
// wrote by hand is the batch the line builds on but carries no record.
|
||||
code *pullRequest
|
||||
}
|
||||
|
||||
// closingOpen is the close the record says is under way on a batch that is
|
||||
// still open, or nil.
|
||||
func (s recordSource) closingOpen() *closing {
|
||||
if !s.open || s.rec == nil {
|
||||
return nil
|
||||
}
|
||||
return s.rec.Closing
|
||||
}
|
||||
|
||||
func prState(pr *pullRequest) string {
|
||||
switch {
|
||||
case pr.MergedAt != nil:
|
||||
return "merged"
|
||||
case pr.State == "" || pr.State == "open":
|
||||
return "open"
|
||||
case pr.State == "closed":
|
||||
return "closed"
|
||||
}
|
||||
return fmt.Sprintf("in state %q", pr.State)
|
||||
}
|
||||
|
||||
func hasRecord(body string) bool {
|
||||
return slices.ContainsFunc(strings.Split(body, "\n"), func(ln string) bool { return strings.TrimSpace(ln) == deferredFence })
|
||||
}
|
||||
|
||||
// findLineRecord finds the line's record: the block of the newest pull
|
||||
// request opened from a batch branch of this repository for the line by an
|
||||
// author the batch trusts, open, merged or closed. open is the open batch
|
||||
// pull request batchPull chose, or nil. Anything that leaves in doubt which
|
||||
// record is the newest is an error, never a fall back to an older one: a
|
||||
// newer block by an author the batch does not trust, a block the bot wrote
|
||||
// and someone removed, a record it cannot read, a closed record newer than
|
||||
// the open batch, or more pull requests than it reads.
|
||||
func findLineRecord(gh github, l line, open *pullRequest) (recordSource, error) {
|
||||
src := recordSource{code: open}
|
||||
for page := 1; src.carrier == nil; page++ {
|
||||
if page > recentPullsPages {
|
||||
return src, fmt.Errorf("none of the %d newest pull requests against %s is a batch pull request with a record, so the line's record cannot be told apart from none", recentPullsPages*recentPullsPage, l.branch())
|
||||
}
|
||||
prs, err := gh.recentPulls(l.branch(), page)
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
for i := range prs {
|
||||
pr := &prs[i]
|
||||
m := batchBranchRE.FindStringSubmatch(pr.Head.Ref)
|
||||
if m == nil || m[1] != l.branch() || pr.Base.Ref != l.branch() || pr.Head.Repo == nil || pr.Head.Repo.FullName != pr.Base.Repo.FullName {
|
||||
continue
|
||||
}
|
||||
if open != nil && pr.Number == open.Number {
|
||||
pr = open
|
||||
}
|
||||
if !hasRecord(pr.Body) {
|
||||
if pr.User.Type == "Bot" && gh.batchBot() != "" && pr.User.Login == gh.batchBot() {
|
||||
return src, fmt.Errorf("batch pull request #%d (%s) was opened by %s but carries no %s block; someone removed it, so the line's record is unknown", pr.Number, prState(pr), pr.User.Login, deferredFence)
|
||||
}
|
||||
continue
|
||||
}
|
||||
why, err := trusted(gh, *pr)
|
||||
if err != nil {
|
||||
return src, fmt.Errorf("batch pull request #%d: %w", pr.Number, err)
|
||||
}
|
||||
if why != "" {
|
||||
return src, fmt.Errorf("batch pull request #%d (%s) carries a %s block but was %s; an older record is not taken in its place", pr.Number, prState(pr), deferredFence, why)
|
||||
}
|
||||
rec, err := parseRecord(pr.Body, l)
|
||||
if err != nil {
|
||||
return src, fmt.Errorf("batch pull request #%d (%s): %w", pr.Number, prState(pr), err)
|
||||
}
|
||||
src.carrier, src.rec = pr, rec
|
||||
break
|
||||
}
|
||||
if len(prs) < recentPullsPage {
|
||||
break
|
||||
}
|
||||
}
|
||||
switch c := src.carrier; {
|
||||
case open != nil && hasRecord(open.Body) && (c == nil || c.Number < open.Number):
|
||||
return src, fmt.Errorf("GitHub does not list the open batch pull request #%d among the pull requests against %s; run again", open.Number, l.branch())
|
||||
case open != nil && hasRecord(open.Body) && c.Number > open.Number:
|
||||
return src, fmt.Errorf("batch pull request #%d (%s) carries a record newer than the open batch pull request #%d; close one of them by hand", c.Number, prState(c), open.Number)
|
||||
case open != nil && c == open:
|
||||
src.open = true
|
||||
case c != nil && prState(c) == "open":
|
||||
return src, fmt.Errorf("batch pull request #%d carries the line's record and is open, but is not the batch this run builds on", c.Number)
|
||||
}
|
||||
return src, nil
|
||||
}
|
||||
@@ -0,0 +1,433 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The records the live batches carried before records had a version, as
|
||||
// GitHub returned them on 2026-11-09, read as version 1.
|
||||
func TestRecordReadsVersion1(t *testing.T) {
|
||||
for line, block := range map[string]string{
|
||||
"release-1.7": `{"line":"release-1.7","deferred":[{"original":4756,"commit":"647407545d630291cfe2a20880cce45af22edf0c","files":["packages/system/backupstrategy-controller/values.yaml"]},{"original":4867,"commit":"813c6180861efe1896c26d62a7afcbc1ff590ebc","files":[".github/renovate.json"]},{"original":5082,"commit":"e93db8848027450c2e23d21348409efa641cb6f1","files":["pkg/registry/core/tap/rest.go"]},{"original":4888,"files":[".github/workflows/cut-prerelease.yaml"],"workflows":true},{"original":4971,"files":[".github/workflows/e2e-proxmox.yaml"],"workflows":true}]}`,
|
||||
"release-1.6": `{"line":"release-1.6","deferred":[{"original":5071,"commit":"3fa534f3cf6cb5774a183f0202f7f5c13e9ae211","files":["internal/operator/tapmaterializer_reconciler.go","internal/operator/tapmaterializer_reconciler_test.go"]},{"original":5086,"commit":"5265ee662c591db409c8ef1ed8bb457cf29f5c22","files":["packages/apps/tenant/templates/gateway.yaml"]},{"original":5087,"commit":"8d8179f20a2c10de03face1472435d5083cde38d","files":["internal/controller/applicationdefinition_helmreconciler.go","internal/controller/applicationdefinition_helmreconciler_test.go"]},{"original":5142,"files":[".github/workflows/pull-requests-release.yaml",".github/workflows/update-releasenotes.yaml"],"workflows":true}]}`,
|
||||
} {
|
||||
l, _ := parseLine(line)
|
||||
rec, err := parseRecord("## What this PR does\n\ntext\n\n"+deferredFence+"\n"+block+"\n```\n\n### Release note\n", l)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: parseRecord = %v", line, err)
|
||||
}
|
||||
waits, conflicts := 0, 0
|
||||
for _, e := range rec.Deferred {
|
||||
switch {
|
||||
case e.Workflows:
|
||||
waits++
|
||||
case e.Commit != "":
|
||||
conflicts++
|
||||
}
|
||||
}
|
||||
if rec.Version != 1 || rec.Line != line || rec.Closing != nil || len(rec.Dropped) != 0 || waits+conflicts != len(rec.Deferred) || waits == 0 || conflicts == 0 {
|
||||
t.Errorf("%s: record = %+v; want version 1 with its conflicts and waits", line, rec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v2 is a version 2 record with each value it may hold.
|
||||
func v2(extra string) string {
|
||||
sha := strings.Repeat("a", 40)
|
||||
return `{"version":2,"line":"release-1.1","built":{"main":"` + sha + `","line":"` + sha + `","newer":"","policy":"` + sha + `","batch":"` + sha + `","tool":"` + sha + `+dirty","run":"","at":"2026-10-26T06:00:00Z","options":{"noWorkflowFiles":true,"mode":"continue"}},` +
|
||||
`"report":"` + strings.Repeat("b", 64) + `","closing":null,"deferred":[{"original":5,"commit":"` + sha + `","files":["app/five.txt"]},{"original":6,"files":[],"reason":"fails the e2e"},{"original":7,"files":[".github/workflows/x.yaml"],"workflows":true}],` +
|
||||
`"dropped":[{"original":8,"merge":"` + sha + `","reason":"needs #4","on":90,"date":"2026-10-26"}],"stale":[9]` + extra + `}`
|
||||
}
|
||||
|
||||
// A record is read whole or not at all: anything it cannot vouch for stops
|
||||
// the command rather than being half believed.
|
||||
func TestRecordRefuses(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
|
||||
if rec, err := parseRecord(block(v2("")), l); err != nil || rec.Version != 2 || len(rec.Dropped) != 1 || rec.Built.Options.Mode != "continue" {
|
||||
t.Fatalf("parseRecord of a whole v2 record = %+v, %v", rec, err)
|
||||
}
|
||||
sha := strings.Repeat("a", 40)
|
||||
for name, json := range map[string]string{
|
||||
"version 4": strings.Replace(v2(""), `"version":2`, `"version":4`, 1),
|
||||
"trailing data": v2("") + `{}`,
|
||||
"trailing text": v2("") + ` x`,
|
||||
"an original listed twice": strings.Replace(v2(""), `"original":8`, `"original":5`, 1),
|
||||
"a needs entry with a commit": strings.Replace(v2(""), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"commit":"`+sha+`","files":[],"needs":[4]}`, 1),
|
||||
"an empty reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":""`, 1),
|
||||
"an empty drop reason": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":" "`, 1),
|
||||
"a bad tool": strings.Replace(v2(""), `+dirty"`, `-dirty"`, 1),
|
||||
"an unknown mode": strings.Replace(v2(""), `"mode":"continue"`, `"mode":"rebase"`, 1),
|
||||
"a reason with a commit": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"x","commit":"`+sha+`"`, 1),
|
||||
"a short merge": strings.Replace(v2(""), `"merge":"`+sha+`"`, `"merge":"abc"`, 1),
|
||||
"a bad date": strings.Replace(v2(""), `"date":"2026-10-26"`, `"date":"26.10.2026"`, 1),
|
||||
"a bad time": strings.Replace(v2(""), `"at":"2026-10-26T06:00:00Z"`, `"at":"yesterday"`, 1),
|
||||
"a short report": strings.Replace(v2(""), strings.Repeat("b", 64), "bbbb", 1),
|
||||
"a closing on another line": strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"backport-batch/release-1.0-20261026","head":"`+sha+`"}`, 1),
|
||||
"a closing without a head": strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"`+batchBranch1+`","head":""}`, 1),
|
||||
"a negative stale": strings.Replace(v2(""), `"stale":[9]`, `"stale":[-9]`, 1),
|
||||
"an unknown key": v2(`,"parked":[]`),
|
||||
"an entry of no kind": strings.Replace(v2(""), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"files":[]}`, 1),
|
||||
"another line": strings.Replace(v2(""), `"line":"release-1.1"`, `"line":"release-1.0"`, 1),
|
||||
"an escape in a reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"fails\u001b[2J the e2e"`, 1),
|
||||
"a newline in a reason": strings.Replace(v2(""), `"reason":"fails the e2e"`, `"reason":"fails\nwarning: forged"`, 1),
|
||||
"an escape in a drop": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"needs\u001b]0;x\u0007 #4"`, 1),
|
||||
"a newline in a drop": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"needs #4\nnote: forged"`, 1),
|
||||
"a reason past its length": strings.Replace(v2(""), `"reason":"needs #4"`, `"reason":"`+strings.Repeat("x", titleLimit+1)+`"`, 1),
|
||||
"a bidi file name": strings.Replace(v2(""), `"files":["app/five.txt"]`, `"files":["app/\u202efive.txt"]`, 1),
|
||||
} {
|
||||
if _, err := parseRecord(block(json), l); err == nil {
|
||||
t.Errorf("%s: parseRecord accepted it", name)
|
||||
}
|
||||
}
|
||||
closing := strings.Replace(v2(""), `"closing":null`, `"closing":{"pr":90,"branch":"`+batchBranch1+`","head":"`+sha+`"}`, 1)
|
||||
if rec, err := parseRecord(block(closing), l); err != nil || rec.Closing == nil || rec.Closing.PR != 90 {
|
||||
t.Errorf("a record that is closing = %+v, %v", rec, err)
|
||||
}
|
||||
}
|
||||
|
||||
// recordPR is a batch pull request on release-1.1 carrying body, numbered n,
|
||||
// in state, opened by login.
|
||||
func recordPR(n int, state, login, body string) pullRequest {
|
||||
pr := notifyPR("20261026", body)
|
||||
pr.Number, pr.State = n, state
|
||||
pr.Head.Ref = fmt.Sprintf("backport-batch/release-1.1-202610%02d", n%30+1)
|
||||
pr.User.Login = login
|
||||
if strings.HasSuffix(login, "[bot]") {
|
||||
pr.User.Type = "Bot"
|
||||
}
|
||||
return pr
|
||||
}
|
||||
|
||||
func recordBody(conflict int) string {
|
||||
return "text\n\n" + deferredFence + "\n" + fmt.Sprintf(`{"line":"release-1.1","deferred":[{"original":%d,"commit":"%s","files":["a"]}]}`, conflict, strings.Repeat("a", 40)) + "\n```\n"
|
||||
}
|
||||
|
||||
// The record is the block of the newest batch pull request that carries one,
|
||||
// whatever its state, among those a trusted author opened from a batch branch
|
||||
// of this repository; anything that leaves the newest record in doubt stops
|
||||
// the command rather than falling back to an older one.
|
||||
func TestFindLineRecord(t *testing.T) {
|
||||
const bot = defaultBatchBot
|
||||
other := func(n int) pullRequest {
|
||||
pr := recordPR(n, "closed", "maintainer", "")
|
||||
pr.Head.Ref = fmt.Sprintf("fix-%d", n)
|
||||
return pr
|
||||
}
|
||||
fork := recordPR(92, "closed", "maintainer", recordBody(92))
|
||||
fork.Head.Repo = &struct {
|
||||
FullName string `json:"full_name"`
|
||||
}{"someone/cozystack"}
|
||||
cases := []struct {
|
||||
name string
|
||||
prs []pullRequest
|
||||
open int // the open batch the caller found, or 0
|
||||
bot string
|
||||
carrier int // 0 for none
|
||||
isOpen bool
|
||||
refusal string
|
||||
pages int
|
||||
}{
|
||||
{name: "the open batch first", prs: []pullRequest{recordPR(92, "open", "maintainer", recordBody(92)), recordPR(91, "closed", "maintainer", recordBody(91))}, open: 92, carrier: 92, isOpen: true},
|
||||
{name: "the newest closed record", prs: []pullRequest{recordPR(91, "closed", bot, recordBody(91)), recordPR(90, "closed", bot, recordBody(90))}, bot: bot, carrier: 91},
|
||||
{name: "a person's batch without a block is passed over", prs: []pullRequest{recordPR(92, "closed", "maintainer", "picked by hand"), recordPR(91, "closed", "maintainer", recordBody(91))}, carrier: 91},
|
||||
{name: "the bot's batch without a block", prs: []pullRequest{recordPR(92, "closed", bot, "edited away"), recordPR(91, "closed", bot, recordBody(91))}, bot: bot, refusal: "carries no ```backport-batch block"},
|
||||
{name: "a fork is no batch", prs: []pullRequest{fork, recordPR(91, "closed", "maintainer", recordBody(91))}, carrier: 91},
|
||||
{name: "a newer record by someone untrusted", prs: []pullRequest{recordPR(92, "closed", "someone", recordBody(92)), recordPR(91, "closed", "maintainer", recordBody(91))}, refusal: "#92"},
|
||||
{name: "a closed record newer than the open batch", prs: []pullRequest{recordPR(92, "closed", "maintainer", recordBody(92)), recordPR(91, "open", "maintainer", recordBody(91))}, open: 91, refusal: "newer than the open batch pull request #91"},
|
||||
{name: "a record on the second page", prs: append(func() (out []pullRequest) {
|
||||
for n := 1000; n < 1000+recentPullsPage; n++ {
|
||||
out = append(out, other(n))
|
||||
}
|
||||
return out
|
||||
}(), recordPR(91, "closed", "maintainer", recordBody(91))), carrier: 91, pages: 2},
|
||||
{name: "ten full pages without a record", prs: func() (out []pullRequest) {
|
||||
for n := 1000; n < 1000+10*recentPullsPage+1; n++ {
|
||||
out = append(out, other(n))
|
||||
}
|
||||
return out
|
||||
}(), refusal: "pull requests", pages: 10},
|
||||
{name: "the bot's record when no bot is trusted", prs: []pullRequest{recordPR(91, "closed", bot, recordBody(91))}, refusal: "#91"},
|
||||
{name: "a hand-written open batch beside a closed record", prs: []pullRequest{recordPR(92, "open", "maintainer", "picked by hand"), recordPR(91, "closed", "maintainer", recordBody(91))}, open: 92, carrier: 91},
|
||||
{name: "no batch ever", prs: []pullRequest{other(5)}},
|
||||
{name: "an unreadable record", prs: []pullRequest{recordPR(91, "closed", "maintainer", "text\n\n"+deferredFence+"\n{\n```\n")}, refusal: "#91 (closed)"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
gh := &fakeGH{prs: tc.prs, bot: tc.bot}
|
||||
var open *pullRequest
|
||||
for i := range tc.prs {
|
||||
if tc.prs[i].Number == tc.open {
|
||||
open = &tc.prs[i]
|
||||
}
|
||||
}
|
||||
src, err := findLineRecord(gh, l, open)
|
||||
switch {
|
||||
case tc.refusal != "":
|
||||
if err == nil || !strings.Contains(err.Error(), tc.refusal) {
|
||||
t.Errorf("findLineRecord = %+v, %v; want a refusal with %q", src, err, tc.refusal)
|
||||
}
|
||||
case err != nil:
|
||||
t.Errorf("findLineRecord: %v", err)
|
||||
case tc.carrier == 0 && (src.carrier != nil || src.rec != nil):
|
||||
t.Errorf("found #%d, want no record", src.carrier.Number)
|
||||
case tc.carrier != 0 && (src.carrier == nil || src.carrier.Number != tc.carrier || src.rec == nil || src.open != tc.isOpen || src.rec.Deferred[0].Original != tc.carrier):
|
||||
t.Errorf("found %+v, want #%d open=%v", src, tc.carrier, tc.isOpen)
|
||||
}
|
||||
if tc.pages != 0 && gh.listed != tc.pages {
|
||||
t.Errorf("read %d pages, want %d", gh.listed, tc.pages)
|
||||
}
|
||||
if tc.open != 0 && src.code != open {
|
||||
t.Errorf("the batch to build on is %+v, want #%d", src.code, tc.open)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A run that picks nothing while no batch is open writes nothing, since
|
||||
// merged and closed batches are never written: it says its classification is
|
||||
// not kept, and check still reads the merged batch's record.
|
||||
func TestZeroPickRunLeavesNoRecord(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n", "app/seven.txt": "line\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
mergeBatch(f, gh)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
edits, comments := len(gh.edited), len(gh.issueComments)
|
||||
|
||||
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
if code != 0 || !strings.Contains(stderr, "no batch pull request is open on release-1.1; this run's classification is not retained") {
|
||||
t.Errorf("run = %d, want 0 saying nothing is retained:\n%s", code, stderr)
|
||||
}
|
||||
if len(gh.created) != 1 || len(gh.edited) != edits || len(gh.issueComments) != comments || f.remoteHead(batchBranch1) != "" {
|
||||
t.Errorf("created %q, edited %q, %d comments, branch at %q; want nothing written", gh.created, gh.edited, len(gh.issueComments), f.remoteHead(batchBranch1))
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 1 || !strings.Contains(stdout, "/pull/7 ") || strings.Contains(stdout, "/pull/5 ") || !strings.Contains(stderr, "deferred conflict on #90 (merged)") {
|
||||
t.Errorf("check = %d, want 1 for #7 alone and #5 still a deferred conflict:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The open batch's author is asked for once although both the open batch and
|
||||
// its record are read.
|
||||
func TestRecordAsksForAPermissionOnce(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
gh.asked = nil
|
||||
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 {
|
||||
t.Fatalf("check = %d\n%s", code, stderr)
|
||||
}
|
||||
if len(gh.asked) != 1 {
|
||||
t.Errorf("asked GitHub for %q, want the author once", gh.asked)
|
||||
}
|
||||
}
|
||||
|
||||
// The record and the report name what the classification was made of: the
|
||||
// heads it read, the policy, the tool, the job, the time and the flags.
|
||||
func TestRecordNamesItsInputs(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.onLine("release-1.1", "fix(app): five\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "5\n"})
|
||||
f.tool = strings.Repeat("c", 40) + "+dirty"
|
||||
t.Setenv("GITHUB_SERVER_URL", "https://github.com")
|
||||
t.Setenv("GITHUB_REPOSITORY", "cozystack/cozystack")
|
||||
t.Setenv("GITHUB_RUN_ID", "123")
|
||||
t.Setenv("GITHUB_RUN_ATTEMPT", "2")
|
||||
gh := &fakeGH{}
|
||||
stdout := mustRun(t, f, gh, "run", "--line", "release-1.0", "--no-workflow-files", "--push")
|
||||
l, _ := parseLine("release-1.0")
|
||||
const branch = "backport-batch/release-1.0-20261026"
|
||||
rec, err := parseRecord(stdout, l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := builtFrom{Main: f.git("rev-parse", "origin/main"), Line: f.git("rev-parse", "origin/release-1.0"), Newer: f.git("rev-parse", "origin/release-1.1"),
|
||||
Policy: f.git("rev-parse", "origin/main"), Batch: f.remoteHead(branch), Tool: f.tool, Run: "https://github.com/cozystack/cozystack/actions/runs/123/attempts/2",
|
||||
At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeNew}}
|
||||
if rec.Version != recordVersion || *rec.Built != want || bodyReportOf(t, stdout, l).doc.Built != want || rec.Report != bodyReportOf(t, stdout, l).doc.ID {
|
||||
t.Errorf("record = %+v, built %+v; want built %+v and the report it names", rec, rec.Built, want)
|
||||
}
|
||||
|
||||
// A policy from another ref, a run by hand that continues the batch.
|
||||
f.git("push", "-q", "origin", "origin/main:refs/heads/policy-draft")
|
||||
f.git("fetch", "-q", "origin")
|
||||
t.Setenv("GITHUB_RUN_ID", "")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
|
||||
mc6 := f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.onLine("release-1.1", "fix(app): six\n\n(cherry picked from commit "+mc6+")", map[string]string{"app/six.txt": "6\n"})
|
||||
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--continue", "--policy", "origin/policy-draft", "--push")
|
||||
if rec, err = parseRecord(stdout, l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b := rec.Built; b.Policy != f.git("rev-parse", "origin/policy-draft") || b.Main != f.git("rev-parse", "origin/main") || b.Run != "" || b.Options != (runOptions{Mode: modeContinue}) || b.Batch != f.remoteHead(branch) {
|
||||
t.Errorf("built %+v; want the draft's policy, no job and a continuation without the flag", b)
|
||||
}
|
||||
}
|
||||
|
||||
// What a run assembled with --no-workflow-files stays saved through the
|
||||
// operations that only change the record, which take no such flag: check
|
||||
// keeps failing the cut on the wait they leave in place.
|
||||
func TestMetadataOperationsKeepTheMode(t *testing.T) {
|
||||
for _, v1 := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("an open record of version 1: %v", v1), func(t *testing.T) {
|
||||
f := workflowWorld(t)
|
||||
f.onLine("release-1.1", "fix(app): a line-only nine", map[string]string{"app/nine.txt": "line\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/nine.txt": "9\n"})
|
||||
gh := &fakeGH{}
|
||||
stdout := mustRun(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
|
||||
reopen(f, gh)
|
||||
if v1 {
|
||||
waits := []pick{{v: &verdict{o: &original{number: 5}}, files: []string{".github/workflows/five.yaml"}}}
|
||||
gh.prs[0].Body = strings.Replace(stdout, strings.Split(strings.Split(stdout, deferredFence+"\n")[1], "\n")[0], strings.Split(deferredBlock(notifyLine, nil, nil, waits), "\n")[1], 1)
|
||||
} else if rec, err := parseRecord(stdout, notifyLine); err != nil || !rec.Built.Options.NoWorkflowFiles {
|
||||
t.Fatalf("the scheduled run saved %+v, %v", rec, err)
|
||||
}
|
||||
for _, op := range []struct {
|
||||
args []string
|
||||
mode string
|
||||
}{
|
||||
{[]string{"--drop", "9", "--reason", "needs #4"}, modeDrop},
|
||||
{[]string{"--undrop", "9"}, modeUndrop},
|
||||
{[]string{"--defer", "7", "--reason", "fails the e2e"}, modeDefer},
|
||||
} {
|
||||
stdout = mustRun(t, f, gh, append(append([]string{"run", "--line", "release-1.1"}, op.args...), "--push")...)
|
||||
rec, err := parseRecord(stdout, notifyLine)
|
||||
if err != nil || rec.Built.Options != (runOptions{NoWorkflowFiles: true, Mode: op.mode}) {
|
||||
t.Errorf("after %q the record is %+v, %v; want the flag kept", op.args, rec.Built, err)
|
||||
}
|
||||
reopen(f, gh)
|
||||
}
|
||||
var rec *lineRecord
|
||||
var err error
|
||||
reopen(f, gh)
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/") {
|
||||
t.Errorf("check --cut = %d, want #5 still waiting:\n%s", code, stdout)
|
||||
}
|
||||
|
||||
// Deferring the last picks closes the batch, and its record
|
||||
// still says how the batch was assembled.
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "6,8", "--reason", "fails the e2e", "--push")
|
||||
rec, err = parseRecord(gh.find(90).Body, notifyLine)
|
||||
if err != nil || gh.find(90).State != "closed" || rec.Closing == nil || rec.Built.Options != (runOptions{NoWorkflowFiles: true, Mode: modeClose}) {
|
||||
t.Errorf("after the close the record is %+v, %v; want the flag kept", rec.Built, err)
|
||||
}
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check --cut = %d, want #5 missing once no batch lists it:\n%s", code, stdout)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A state is named only when it is one GitHub uses; anything else is quoted.
|
||||
func TestPRStateNamesOnlyKnownStates(t *testing.T) {
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
for _, tc := range []struct {
|
||||
pr pullRequest
|
||||
want string
|
||||
}{
|
||||
{pullRequest{State: "open"}, "open"},
|
||||
{pullRequest{State: ""}, "open"},
|
||||
{pullRequest{State: "closed"}, "closed"},
|
||||
{pullRequest{State: "closed", MergedAt: &at}, "merged"},
|
||||
{pullRequest{State: "open\n\x1b[2Jforged"}, `in state "open\n\x1b[2Jforged"`},
|
||||
} {
|
||||
if got := prState(&tc.pr); got != tc.want {
|
||||
t.Errorf("prState(%q) = %q, want %q", tc.pr.State, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v3 is v2 as version 3, with extra after the stale list.
|
||||
func v3(extra string) string {
|
||||
return strings.Replace(v2(extra), `"version":2`, `"version":3`, 1)
|
||||
}
|
||||
|
||||
// Version 3 adds what is parked and which units do only part of their
|
||||
// original, each held to what a run writes, and version 2 has neither.
|
||||
func TestRecordVersion3(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
|
||||
sha, other := strings.Repeat("a", 40), strings.Repeat("c", 40)
|
||||
parked := `,"parked":[{"original":10,"merge":"` + sha + `","kind":"partial","files":["a","b"]},{"original":11,"merge":"` + sha + `","kind":"dependency","needs":[3,4],"commits":["` + other + `"],"announced":true}],"partial":[9]`
|
||||
waits := strings.Replace(v3(parked), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"files":[],"needs":[3,5]}`, 1)
|
||||
rec, err := parseRecord(block(waits), l)
|
||||
if err != nil || rec.Version != 3 || len(rec.Parked) != 2 || !rec.Parked[1].Announced || !slices.Equal(rec.Partial, []int{9}) || !slices.Equal(rec.deferred()[6].Needs, []int{3, 5}) {
|
||||
t.Fatalf("parseRecord of a whole v3 record = %+v, %v", rec, err)
|
||||
}
|
||||
for name, json := range map[string]string{
|
||||
"version 1 with a wait": `{"line":"release-1.1","deferred":[{"original":6,"files":[],"needs":[3,5]}]}`,
|
||||
"a wait with files": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"files":["a"],"needs":[3,5]`, 1),
|
||||
"a wait that is a conflict": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"commit":"`+sha+`","files":[],"needs":[3,5]`, 1),
|
||||
"a wait on itself": strings.Replace(waits, `"needs":[3,5]`, `"needs":[3,6]`, 1),
|
||||
"an unsorted wait": strings.Replace(waits, `"needs":[3,5]`, `"needs":[5,3]`, 1),
|
||||
"a wait on nothing": strings.Replace(waits, `"needs":[3,5]`, `"needs":[0]`, 1),
|
||||
"a wait cycle": strings.Replace(strings.Replace(waits, `"needs":[3,5]`, `"needs":[12]`, 1), `"deferred":[`, `"deferred":[{"original":12,"files":[],"needs":[6]},`, 1),
|
||||
"a wait on a parked one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[10]`, 1),
|
||||
"a wait on a dropped one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[8]`, 1),
|
||||
"a park on a deferred one": strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,5]`, 1),
|
||||
"a partial unit also deferred": strings.Replace(strings.Replace(waits, `"stale":[9]`, `"stale":[9,5]`, 1), `"partial":[9]`, `"partial":[9,5]`, 1),
|
||||
"a changed pick also parked": v3(parked + `,"changed":[10]`),
|
||||
"a changed pick also deferred": v3(parked + `,"changed":[5]`),
|
||||
"a park on its own merge": strings.Replace(waits, `"commits":["`+other+`"]`, `"commits":["`+sha+`"]`, 1),
|
||||
"a park cycle": strings.Replace(strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,13]`, 1), `"parked":[`, `"parked":[{"original":13,"merge":"`+sha+`","kind":"dependency","needs":[11]},`, 1),
|
||||
} {
|
||||
if _, err := parseRecord(block(json), l); err == nil {
|
||||
t.Errorf("%s: parseRecord accepted it", name)
|
||||
}
|
||||
}
|
||||
for name, json := range map[string]string{
|
||||
"version 2 with a park": v2(parked),
|
||||
"version 4": strings.Replace(v3(""), `"version":3`, `"version":4`, 1),
|
||||
"a park of a deferred original": v3(strings.Replace(parked, `"original":10`, `"original":5`, 1)),
|
||||
"a park of a dropped original": v3(strings.Replace(parked, `"original":10`, `"original":8`, 1)),
|
||||
"a partial park with no files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":[]`, 1)),
|
||||
"a partial park that needs": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a"],"needs":[3]`, 1)),
|
||||
"unsorted files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["b","a"]`, 1)),
|
||||
"a file that drives a terminal": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a\u001b[2J"]`, 1)),
|
||||
"a dependency park with no need": v3(strings.Replace(strings.Replace(parked, `"needs":[3,4],`, ``, 1), `"commits":["`+other+`"],`, ``, 1)),
|
||||
"unsorted needs": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[4,3]`, 1)),
|
||||
"a park that needs itself": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[3,11]`, 1)),
|
||||
"a short commit": v3(strings.Replace(parked, `"commits":["`+other+`"]`, `"commits":["abc"]`, 1)),
|
||||
"a short merge": v3(strings.Replace(parked, `"merge":"`+sha+`","kind":"partial"`, `"merge":"abc","kind":"partial"`, 1)),
|
||||
"an unknown kind": v3(strings.Replace(parked, `"kind":"partial"`, `"kind":"later"`, 1)),
|
||||
"a partial unit not on the batch": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[10]`, 1)),
|
||||
"a partial unit twice": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[9,9]`, 1)),
|
||||
"a changed pick twice": v3(parked + `,"changed":[4,4]`),
|
||||
"a changed pick that is none": v3(parked + `,"changed":[0]`),
|
||||
} {
|
||||
if _, err := parseRecord(block(json), l); err == nil {
|
||||
t.Errorf("%s: parseRecord accepted it", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,885 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A report is one classification of a line: where every original merged into
|
||||
// main since the line branched stands, and what it was made of. The batch
|
||||
// pull request's body carries the latest one beside the record, and an issue
|
||||
// comment the tool never edits keeps each one, so a later run that replaces
|
||||
// the body loses none of them. Its ID hashes the classification, not the pull
|
||||
// request it lands on, which a new batch does not have yet when the report is
|
||||
// made.
|
||||
|
||||
const (
|
||||
reportFence = "```backport-batch-report"
|
||||
reportMarker = "<!-- backport-batch:report "
|
||||
// reportFormat is the format a run writes. Format 2 adds parked
|
||||
// originals and partial units; format 1, which has neither, is read too.
|
||||
reportFormat = 2
|
||||
// reportLimit keeps a report comment well inside GitHub's 65536.
|
||||
reportLimit = 60000
|
||||
)
|
||||
|
||||
type reportDoc struct {
|
||||
Format int `json:"format"`
|
||||
ID string `json:"id"`
|
||||
Line string `json:"line"`
|
||||
Built builtFrom `json:"built"`
|
||||
Overrides reportOverrides `json:"overrides"`
|
||||
Primary reportPrimary `json:"primary"`
|
||||
Overlays reportOverlays `json:"overlays"`
|
||||
Diag map[string][]string `json:"diag"`
|
||||
Other []string `json:"other"`
|
||||
}
|
||||
|
||||
type reportOverrides struct {
|
||||
Include []int `json:"backport/include"`
|
||||
Skip []int `json:"backport/skip"`
|
||||
}
|
||||
|
||||
// reportPrimary puts every original in exactly one place.
|
||||
type reportPrimary struct {
|
||||
Picked []int `json:"picked"`
|
||||
Missing []int `json:"missing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Parked []parkedEntry `json:"parked"`
|
||||
Skipped reportSkipped `json:"skipped"`
|
||||
}
|
||||
|
||||
type reportSkipped struct {
|
||||
OnLine []int `json:"on-line"`
|
||||
BeforeStart []int `json:"before-start"`
|
||||
Type []int `json:"type"`
|
||||
Breaking []int `json:"breaking"`
|
||||
Touches []int `json:"touches"`
|
||||
Newer []int `json:"newer"`
|
||||
SkipLabel []int `json:"skip-label"`
|
||||
Dropped []int `json:"dropped"`
|
||||
}
|
||||
|
||||
// reportOverlays mark originals that are also something else: stale ones
|
||||
// are excluded, or applied only in part, but still on the batch, partial
|
||||
// ones those whose unit there leaves part of them without effect, and each
|
||||
// drop is the record's entry, with the merge commit it is bound to and its
|
||||
// reason.
|
||||
type reportOverlays struct {
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
Changed []int `json:"changed"`
|
||||
Partial []int `json:"partial,omitempty"`
|
||||
}
|
||||
|
||||
func (s *reportSkipped) list(rule int) *[]int {
|
||||
switch rule {
|
||||
case ruleOnLine:
|
||||
return &s.OnLine
|
||||
case ruleBeforeStart:
|
||||
return &s.BeforeStart
|
||||
case ruleType:
|
||||
return &s.Type
|
||||
case ruleBreaking:
|
||||
return &s.Breaking
|
||||
case ruleTouches:
|
||||
return &s.Touches
|
||||
case ruleNewer:
|
||||
return &s.Newer
|
||||
case ruleSkipLabel:
|
||||
return &s.SkipLabel
|
||||
case ruleDropped:
|
||||
return &s.Dropped
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type namedList struct {
|
||||
name string
|
||||
list *[]int
|
||||
}
|
||||
|
||||
// lists names each skipped list as the report does.
|
||||
func (s *reportSkipped) lists() []namedList {
|
||||
return []namedList{
|
||||
{"on-line", &s.OnLine}, {"before-start", &s.BeforeStart}, {"type", &s.Type}, {"breaking", &s.Breaking},
|
||||
{"touches", &s.Touches}, {"newer", &s.Newer}, {"skip-label", &s.SkipLabel}, {"dropped", &s.Dropped},
|
||||
}
|
||||
}
|
||||
|
||||
func numbers(vs []*verdict) []int {
|
||||
out := []int{}
|
||||
for _, v := range vs {
|
||||
out = append(out, v.o.number)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// deferredEntries is what the record and the report say about the conflicts,
|
||||
// the deferrals and the waits of a run.
|
||||
func deferredEntries(conflicts, held, waiting, needs []pick) []deferredEntry {
|
||||
out := []deferredEntry{}
|
||||
for _, p := range conflicts {
|
||||
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Commit: p.failed, Files: files, More: len(p.files) - len(files) + p.more})
|
||||
}
|
||||
for _, p := range held {
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Files: []string{}, Reason: capped(oneLine(clean(p.reason, false)), titleLimit)})
|
||||
}
|
||||
for _, p := range waiting {
|
||||
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Files: files, More: len(p.files) - len(files) + p.more, Workflows: true})
|
||||
}
|
||||
for _, p := range needs {
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Files: []string{}, Needs: slices.Sorted(slices.Values(p.needs))})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// parkedEntries is what the record and the report say about what a run
|
||||
// parked; whether notify named it is the record's alone.
|
||||
func parkedEntries(vs []*verdict) []parkedEntry {
|
||||
out := []parkedEntry{}
|
||||
for _, v := range vs {
|
||||
p := *v.park
|
||||
p.Announced = false
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// buildReport makes the report of a run whose built is set. Every original
|
||||
// main merged since the line branched has to land in exactly one primary
|
||||
// category; one that does not is an error rather than a report that
|
||||
// silently drops it.
|
||||
func buildReport(b *batch, r *report) (reportDoc, error) {
|
||||
doc := reportDoc{Format: reportFormat, Line: b.line.branch(), Built: r.built, Diag: map[string][]string{}, Other: append([]string{}, b.other...)}
|
||||
doc.Overrides = reportOverrides{Include: []int{}, Skip: []int{}}
|
||||
for _, v := range r.overrides {
|
||||
switch v.label {
|
||||
case labelInclude:
|
||||
doc.Overrides.Include = append(doc.Overrides.Include, v.o.number)
|
||||
case labelSkip:
|
||||
doc.Overrides.Skip = append(doc.Overrides.Skip, v.o.number)
|
||||
}
|
||||
}
|
||||
doc.Primary = reportPrimary{Picked: numbers(r.onBranch), Missing: numbers(r.missing), Deferred: deferredEntries(r.deferred, r.held, r.waiting, r.needs), Parked: parkedEntries(r.parked)}
|
||||
for _, l := range doc.Primary.Skipped.lists() {
|
||||
*l.list = []int{}
|
||||
}
|
||||
for rule, vs := range r.skipped {
|
||||
list := doc.Primary.Skipped.list(rule)
|
||||
if list == nil {
|
||||
return doc, fmt.Errorf("the report has no place for what rule %d skipped", rule)
|
||||
}
|
||||
*list = append(*list, numbers(vs)...)
|
||||
}
|
||||
doc.Overlays = reportOverlays{Dropped: []droppedEntry{}, Stale: numbers(r.stale), Changed: numbers(r.changed())}
|
||||
if len(r.partial) > 0 {
|
||||
doc.Overlays.Partial = numbers(r.partial)
|
||||
}
|
||||
for _, v := range b.verdicts {
|
||||
if len(v.diag) > 0 {
|
||||
doc.Diag[strconv.Itoa(v.o.number)] = slices.Clone(v.diag)
|
||||
}
|
||||
}
|
||||
doc.Overlays.Dropped = append(doc.Overlays.Dropped, r.dropped...)
|
||||
doc.canonical()
|
||||
where, err := doc.categories()
|
||||
if err != nil {
|
||||
return doc, err
|
||||
}
|
||||
for _, v := range b.verdicts {
|
||||
if where[v.o.number] == "" {
|
||||
return doc, fmt.Errorf("the report of %s puts #%d nowhere", b.line.branch(), v.o.number)
|
||||
}
|
||||
delete(where, v.o.number)
|
||||
}
|
||||
for n := range where {
|
||||
return doc, fmt.Errorf("the report of %s names #%d, which main did not merge after the line branched", b.line.branch(), n)
|
||||
}
|
||||
doc.ID = reportID(doc)
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// canonical sorts every list, so equal classifications are equal bytes.
|
||||
func (d *reportDoc) canonical() {
|
||||
lists := []*[]int{&d.Primary.Picked, &d.Primary.Missing, &d.Overrides.Include, &d.Overrides.Skip, &d.Overlays.Stale, &d.Overlays.Changed, &d.Overlays.Partial}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
lists = append(lists, l.list)
|
||||
}
|
||||
for _, l := range lists {
|
||||
slices.Sort(*l)
|
||||
}
|
||||
slices.SortFunc(d.Primary.Deferred, func(a, b deferredEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.SortFunc(d.Primary.Parked, func(a, b parkedEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.SortFunc(d.Overlays.Dropped, func(a, b droppedEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.Sort(d.Other)
|
||||
}
|
||||
|
||||
// categories maps each original the report names to its primary category,
|
||||
// refusing one named twice.
|
||||
func (d *reportDoc) categories() (map[int]string, error) {
|
||||
where := map[int]string{}
|
||||
put := func(n int, category string) error {
|
||||
if n <= 0 {
|
||||
return fmt.Errorf("the report puts original %d in %s", n, category)
|
||||
}
|
||||
if prev := where[n]; prev != "" {
|
||||
return fmt.Errorf("the report puts #%d in both %s and %s", n, prev, category)
|
||||
}
|
||||
where[n] = category
|
||||
return nil
|
||||
}
|
||||
for _, n := range d.Primary.Picked {
|
||||
if err := put(n, "picked"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, n := range d.Primary.Missing {
|
||||
if err := put(n, "missing"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, e := range d.Primary.Deferred {
|
||||
if err := put(e.Original, "deferred"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, e := range d.Primary.Parked {
|
||||
if err := put(e.Original, "parked"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
for _, n := range *l.list {
|
||||
if err := put(n, "skipped."+l.name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return where, nil
|
||||
}
|
||||
|
||||
// reportID hashes everything a report says but when and in which job it was
|
||||
// made, so a run that finds what the last one found makes the same report.
|
||||
func reportID(doc reportDoc) string {
|
||||
doc.ID, doc.Built.At, doc.Built.Run = "", "", ""
|
||||
data, _ := json.Marshal(doc)
|
||||
sum := sha256.Sum256(data)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// storedReport is a report as it was written, whose bytes a replay posts
|
||||
// unchanged.
|
||||
type storedReport struct {
|
||||
doc reportDoc
|
||||
raw string
|
||||
}
|
||||
|
||||
func reportJSON(doc reportDoc) string {
|
||||
data, _ := json.Marshal(doc)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func reportBlock(raw string) string { return reportFence + "\n" + raw + "\n```" }
|
||||
|
||||
// parseReport reads one report line of a batch pull request against l. An
|
||||
// ID that matches the content shows only that nobody changed it since it
|
||||
// was hashed, so everything it says is also held to what a run writes.
|
||||
func parseReport(raw string, l line) (reportDoc, error) {
|
||||
var doc reportDoc
|
||||
if err := decodeStrict(raw, &doc); err != nil {
|
||||
return doc, fmt.Errorf("reading the report: %w", err)
|
||||
}
|
||||
if doc.Format != 1 && doc.Format != reportFormat {
|
||||
return doc, fmt.Errorf("the report is format %d, which this backport-batch does not read", doc.Format)
|
||||
}
|
||||
if !reportIDRE.MatchString(doc.ID) || reportID(doc) != doc.ID {
|
||||
return doc, fmt.Errorf("report %.12s does not match what it says", terminalSafe(doc.ID))
|
||||
}
|
||||
if err := doc.validate(l); err != nil {
|
||||
return doc, fmt.Errorf("report %.12s %w", doc.ID, err)
|
||||
}
|
||||
if err := doc.bindDrops(); err != nil {
|
||||
return doc, fmt.Errorf("report %.12s %w", doc.ID, err)
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// validate holds a report to what a run writes. Its errors read after
|
||||
// "report <id>".
|
||||
func (d *reportDoc) validate(l line) error {
|
||||
if d.Line != l.branch() {
|
||||
return fmt.Errorf("is for %q, not %s", d.Line, l.branch())
|
||||
}
|
||||
if err := d.Built.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, sha := range d.Other {
|
||||
if checkSHA(sha) != nil {
|
||||
return fmt.Errorf("counts %q as a commit on main", sha)
|
||||
}
|
||||
}
|
||||
if _, err := d.categories(); err != nil {
|
||||
return err
|
||||
}
|
||||
// The deferred and dropped entries are the record's, held to the
|
||||
// record's rules.
|
||||
kept := lineRecord{Version: 1, Line: d.Line, Deferred: d.Primary.Deferred, Dropped: d.Overlays.Dropped}
|
||||
if err := kept.validate(l); err != nil {
|
||||
return err
|
||||
}
|
||||
for n, lines := range d.Diag {
|
||||
if k, err := strconv.Atoi(n); err != nil || k <= 0 || strconv.Itoa(k) != n {
|
||||
return fmt.Errorf("has diagnostics for %q, which is no original", n)
|
||||
}
|
||||
for _, ln := range lines {
|
||||
if ln != oneLineText(ln) {
|
||||
return fmt.Errorf("has a diagnostic for #%s that is not one clean line: %q", n, ln)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range d.Primary.Parked {
|
||||
if p.Original <= 0 {
|
||||
return fmt.Errorf("names original %d", p.Original)
|
||||
}
|
||||
if err := p.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Whether notify named it is the record's to say, not the
|
||||
// classification's.
|
||||
if p.Announced {
|
||||
return fmt.Errorf("has #%d announced", p.Original)
|
||||
}
|
||||
}
|
||||
for _, list := range [][]int{d.Overrides.Include, d.Overrides.Skip, d.Overlays.Stale, d.Overlays.Changed, d.Overlays.Partial} {
|
||||
for _, n := range list {
|
||||
if n <= 0 {
|
||||
return fmt.Errorf("names original %d", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return d.links()
|
||||
}
|
||||
|
||||
// links holds the fields of a report that speak of one another to agree as
|
||||
// a run writes them, so no field says something another contradicts. Its
|
||||
// errors read after "report <id>".
|
||||
func (d *reportDoc) links() error {
|
||||
where, err := d.categories()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Format 1 was written before anything was parked.
|
||||
if d.Format == 1 && len(d.Primary.Parked)+len(d.Overlays.Partial) > 0 {
|
||||
return errors.New("parks originals, which format 1 does not")
|
||||
}
|
||||
// What a dependency park needs never reaches the line: it is excluded
|
||||
// by a rule, a label, a drop or a park of its own, or a commit on main
|
||||
// that merged no pull request.
|
||||
other := map[string]bool{}
|
||||
for _, sha := range d.Other {
|
||||
other[sha] = true
|
||||
}
|
||||
for _, p := range d.Primary.Parked {
|
||||
for _, n := range p.Needs {
|
||||
switch c := where[n]; c {
|
||||
case "parked", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.skip-label", "skipped.dropped":
|
||||
default:
|
||||
return fmt.Errorf("parks #%d for #%d, which it has %s", p.Original, n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
if other[p.Merge] || slices.Contains(p.Commits, p.Merge) {
|
||||
return fmt.Errorf("parks #%d by a merge it counts as no pull request's, or for its own merge", p.Original)
|
||||
}
|
||||
for _, c := range p.Commits {
|
||||
if !other[c] {
|
||||
return fmt.Errorf("parks #%d for %.12s, which it does not count among main's commits that merged no pull request", p.Original, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
// A partial unit is still on the batch.
|
||||
stale := map[int]bool{}
|
||||
for _, n := range d.Overlays.Stale {
|
||||
stale[n] = true
|
||||
}
|
||||
for _, n := range d.Overlays.Partial {
|
||||
if !stale[n] {
|
||||
return fmt.Errorf("marks #%d partial on the batch, but not still on it", n)
|
||||
}
|
||||
}
|
||||
drops := map[int]bool{}
|
||||
for _, e := range d.Overlays.Dropped {
|
||||
drops[e.Original] = true
|
||||
}
|
||||
for _, n := range d.Primary.Skipped.Dropped {
|
||||
if !drops[n] {
|
||||
return fmt.Errorf("has #%d dropped, but keeps no drop of it", n)
|
||||
}
|
||||
}
|
||||
// Stale marks something a run took or excluded, and changed a pick.
|
||||
for _, n := range d.Overlays.Stale {
|
||||
if c := where[n]; c != "picked" && !strings.HasPrefix(c, "skipped.") {
|
||||
return fmt.Errorf("marks #%d stale, which it has %s", n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
for _, n := range d.Overlays.Changed {
|
||||
if c := where[n]; c != "picked" {
|
||||
return fmt.Errorf("marks #%d changed, which it has %s", n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
// backport/skip decides before any rule, and backport/include lifts
|
||||
// rules 1 to 4.
|
||||
skip := map[int]bool{}
|
||||
for _, n := range d.Overrides.Skip {
|
||||
skip[n] = true
|
||||
if where[n] != "skipped.skip-label" {
|
||||
return fmt.Errorf("has #%d labelled %s, which it has %s", n, labelSkip, or(where[n], "nowhere"))
|
||||
}
|
||||
}
|
||||
for _, n := range d.Primary.Skipped.SkipLabel {
|
||||
if !skip[n] {
|
||||
return fmt.Errorf("has #%d skipped for a %s label it does not list", n, labelSkip)
|
||||
}
|
||||
}
|
||||
for _, n := range d.Overrides.Include {
|
||||
switch c := where[n]; c {
|
||||
case "", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.skip-label":
|
||||
return fmt.Errorf("has #%d labelled %s, which it has %s", n, labelInclude, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
for n := range d.Diag {
|
||||
if k, err := strconv.Atoi(n); err != nil || where[k] == "" {
|
||||
return fmt.Errorf("has diagnostics for %q, which it puts nowhere", n)
|
||||
}
|
||||
}
|
||||
for _, e := range d.Primary.Deferred {
|
||||
if e.Workflows && !d.Built.Options.NoWorkflowFiles {
|
||||
return fmt.Errorf("has #%d waiting for its workflow files, but was made bringing them over", e.Original)
|
||||
}
|
||||
// What a wait waits on is still to come: not on the line, deferred,
|
||||
// or held by rule 6.
|
||||
for _, n := range e.Needs {
|
||||
switch c := where[n]; c {
|
||||
case "missing", "deferred", "skipped.newer":
|
||||
default:
|
||||
return fmt.Errorf("has #%d wait on #%d, which it has %s", e.Original, n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
if d.Format == 1 && len(e.Needs) > 0 {
|
||||
return fmt.Errorf("has #%d wait on other originals, which format 1 does not", e.Original)
|
||||
}
|
||||
}
|
||||
needs := map[int][]int{}
|
||||
for _, e := range d.Primary.Deferred {
|
||||
needs[e.Original] = e.Needs
|
||||
}
|
||||
for _, p := range d.Primary.Parked {
|
||||
needs[p.Original] = p.Needs
|
||||
}
|
||||
if err := waitCycle(needs); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Built.Options.Mode == modeClose && len(d.Primary.Picked)+len(d.Overlays.Stale) > 0 {
|
||||
return errors.New("closes the batch, but still has originals on it")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// bodyReport is the report a batch body against l carries, or nil.
|
||||
func bodyReport(body string, l line) (*storedReport, error) {
|
||||
blocks, err := fencedLines(body, reportFence)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, err
|
||||
case len(blocks) == 0:
|
||||
return nil, nil
|
||||
case len(blocks) > 1:
|
||||
return nil, fmt.Errorf("the body holds %d %s blocks", len(blocks), reportFence)
|
||||
}
|
||||
doc, err := parseReport(blocks[0], l)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &storedReport{doc: doc, raw: blocks[0]}, nil
|
||||
}
|
||||
|
||||
// reportComment is the comment that keeps a report: a marker naming it, and
|
||||
// the report folded away in the same block the body carries. Nothing in it
|
||||
// mentions anyone: numbers are bare, and text sits inside the fence.
|
||||
//
|
||||
// Only checked values reach the summary GitHub renders: an ID of hex digits,
|
||||
// a release line and the date of a time that parses.
|
||||
func reportComment(s storedReport) (string, error) {
|
||||
if !reportIDRE.MatchString(s.doc.ID) {
|
||||
return "", fmt.Errorf("report %q has no ID", s.doc.ID)
|
||||
}
|
||||
if _, err := parseLine(s.doc.Line); err != nil {
|
||||
return "", fmt.Errorf("report %.12s: %w", s.doc.ID, err)
|
||||
}
|
||||
at, err := time.Parse(time.RFC3339, s.doc.Built.At)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("report %.12s was made at %q", s.doc.ID, s.doc.Built.At)
|
||||
}
|
||||
date := at.UTC().Format(time.DateOnly)
|
||||
c := reportMarker + s.doc.ID + " -->\n" +
|
||||
"<details><summary>Backport batch report for " + s.doc.Line + ", " + date + " (" + s.doc.ID[:12] + ")</summary>\n\n" +
|
||||
reportBlock(s.raw) + "\n</details>\n"
|
||||
if len(c) > reportLimit {
|
||||
return "", fmt.Errorf("the report comment is %d bytes, over the %d it may have", len(c), reportLimit)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
var reportMarkerRE = regexp.MustCompile(`^` + regexp.QuoteMeta(reportMarker) + `([0-9a-f]{64}) -->\n`)
|
||||
|
||||
// findReports reads the report comments on batch pull request n, against l,
|
||||
// by authors the batch trusts, as it trusts a record's author, by ID. One
|
||||
// whose content does not match the ID its marker gives, or is not what a run
|
||||
// writes, is returned apart, with why; a look-alike by anyone else is not
|
||||
// read, whatever it says.
|
||||
func findReports(gh github, n int, l line) (map[string]storedReport, map[string]string, error) {
|
||||
comments, err := gh.comments(n)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
found, bad := map[string]storedReport{}, map[string]string{}
|
||||
for _, c := range comments {
|
||||
if !strings.HasPrefix(c.Body, reportMarker) {
|
||||
continue
|
||||
}
|
||||
why, err := trustedAuthor(gh, c.User.Login, c.User.Type)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if why != "" {
|
||||
continue
|
||||
}
|
||||
m := reportMarkerRE.FindStringSubmatch(c.Body)
|
||||
if m == nil {
|
||||
bad[strconv.FormatInt(c.ID, 10)] = fmt.Sprintf("comment %d starts like a report but names none", c.ID)
|
||||
continue
|
||||
}
|
||||
blocks, err := fencedLines(c.Body, reportFence)
|
||||
if err == nil && len(blocks) != 1 {
|
||||
err = fmt.Errorf("it holds %d reports", len(blocks))
|
||||
}
|
||||
var doc reportDoc
|
||||
if err == nil {
|
||||
doc, err = parseReport(blocks[0], l)
|
||||
}
|
||||
if err == nil && doc.ID != m[1] {
|
||||
err = fmt.Errorf("its marker names %.12s", m[1])
|
||||
}
|
||||
if err != nil {
|
||||
bad[m[1]] = fmt.Sprintf("comment %d: %v", c.ID, err)
|
||||
continue
|
||||
}
|
||||
found[m[1]] = storedReport{doc: doc, raw: blocks[0]}
|
||||
}
|
||||
return found, bad, nil
|
||||
}
|
||||
|
||||
// toolVersion is the commit this binary was built from, with +dirty when its
|
||||
// tree had changes, or "" when the build did not record it, as go run does
|
||||
// not.
|
||||
func toolVersion() string {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
rev, dirty := "", false
|
||||
for _, s := range info.Settings {
|
||||
switch s.Key {
|
||||
case "vcs.revision":
|
||||
rev = s.Value
|
||||
case "vcs.modified":
|
||||
dirty = s.Value == "true"
|
||||
}
|
||||
}
|
||||
if checkSHA(rev) != nil {
|
||||
return ""
|
||||
}
|
||||
if dirty {
|
||||
rev += "+dirty"
|
||||
}
|
||||
return rev
|
||||
}
|
||||
|
||||
// runURL is the Actions run the command runs in, from the variables GitHub
|
||||
// sets for it, or "" outside one.
|
||||
func runURL(lookup func(string) (string, bool)) string {
|
||||
get := func(key string) string { v, _ := lookup(key); return v }
|
||||
url := get("GITHUB_SERVER_URL") + "/" + get("GITHUB_REPOSITORY") + "/actions/runs/" + get("GITHUB_RUN_ID")
|
||||
if attempt := get("GITHUB_RUN_ATTEMPT"); attempt != "" && attempt != "1" {
|
||||
url += "/attempts/" + attempt
|
||||
}
|
||||
if !runURLRE.MatchString(url) {
|
||||
return ""
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
// report prints a report a batch pull request keeps, writing nothing, so it
|
||||
// works on merged and closed batches too. It reads GitHub, and for a report
|
||||
// that keeps drops also this clone's history at the heads the report was
|
||||
// made from, which it never fetches.
|
||||
func (a *app) report(args []string) int {
|
||||
fs := a.flags("report")
|
||||
number := fs.Int("pr", 0, "the batch pull request")
|
||||
id := fs.String("id", "", "the report, by its ID (default: the one the body carries)")
|
||||
asJSON := fs.Bool("json", false, "print the report's JSON")
|
||||
if err := parse(fs, args); err != nil {
|
||||
return 2
|
||||
}
|
||||
if *number <= 0 {
|
||||
return a.fail(fmt.Errorf("--pr %d is not a pull request number", *number))
|
||||
}
|
||||
if *id != "" && !reportIDRE.MatchString(*id) {
|
||||
return a.fail(fmt.Errorf("--id %q is not a report ID", *id))
|
||||
}
|
||||
pr, err := a.gh.pull(*number)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
l, err := parseLine(pr.Base.Ref)
|
||||
if m := batchBranchRE.FindStringSubmatch(pr.Head.Ref); err != nil || m == nil || m[1] != l.branch() || pr.Head.Repo == nil || pr.Head.Repo.FullName != pr.Base.Repo.FullName {
|
||||
return a.fail(fmt.Errorf("#%d is not a batch pull request of this repository", pr.Number))
|
||||
}
|
||||
why, err := trusted(a.gh, pr)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why != "" {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d was %s, so what it says is not the batch's", pr.Number, why))
|
||||
}
|
||||
inBody, err := bodyReport(pr.Body, l)
|
||||
if err != nil {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d: %w", pr.Number, err))
|
||||
}
|
||||
if *id == "" {
|
||||
if inBody == nil {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d carries no report in its body; name one with --id", pr.Number))
|
||||
}
|
||||
*id = inBody.doc.ID
|
||||
}
|
||||
posted, bad, err := findReports(a.gh, pr.Number, l)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why := bad[*id]; why != "" {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d: the report %.12s is not what its comment says: %s", pr.Number, *id, why))
|
||||
}
|
||||
s, inComment := posted[*id]
|
||||
switch {
|
||||
case inComment:
|
||||
case inBody != nil && inBody.doc.ID == *id:
|
||||
s = *inBody
|
||||
default:
|
||||
return a.fail(fmt.Errorf("batch pull request #%d keeps no report %.12s", pr.Number, *id))
|
||||
}
|
||||
if *asJSON {
|
||||
fmt.Fprintln(a.stdout, s.raw)
|
||||
return 0
|
||||
}
|
||||
a.printReport(&pr, s, inBody, inComment)
|
||||
return 0
|
||||
}
|
||||
|
||||
// printReport prints a report. It was checked when it was read, and every
|
||||
// field still goes through terminalSafe, so nothing it carries spans lines
|
||||
// or drives a terminal.
|
||||
func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport, inComment bool) {
|
||||
d, b := s.doc, s.doc.Built
|
||||
safe := terminalSafe
|
||||
fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
|
||||
if b.Options.NoWorkflowFiles {
|
||||
fmt.Fprint(a.stdout, " with --no-workflow-files")
|
||||
}
|
||||
fmt.Fprintln(a.stdout)
|
||||
fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
|
||||
current := "none"
|
||||
if inBody != nil {
|
||||
current = safe(inBody.doc.ID)
|
||||
}
|
||||
switch {
|
||||
case inBody != nil && inBody.doc.ID == d.ID:
|
||||
fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
|
||||
case pr.Head.SHA == b.Batch:
|
||||
fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
|
||||
default:
|
||||
fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
|
||||
}
|
||||
if !inComment {
|
||||
if prState(pr) == "open" {
|
||||
fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
|
||||
}
|
||||
}
|
||||
type line struct {
|
||||
n int
|
||||
text string
|
||||
}
|
||||
var lines []line
|
||||
for _, n := range d.Primary.Picked {
|
||||
lines = append(lines, line{n, "picked"})
|
||||
}
|
||||
for _, n := range d.Primary.Missing {
|
||||
lines = append(lines, line{n, "missing"})
|
||||
}
|
||||
for _, e := range d.Primary.Deferred {
|
||||
text := "deferred: "
|
||||
switch {
|
||||
case e.Workflows:
|
||||
text += "waits for a maintainer, changes " + strings.Join(e.Files, ", ")
|
||||
case len(e.Needs) > 0:
|
||||
var needs []string
|
||||
for _, n := range e.Needs {
|
||||
needs = append(needs, "#"+strconv.Itoa(n))
|
||||
}
|
||||
text += "waits on " + strings.Join(needs, ", ")
|
||||
case e.Reason != "":
|
||||
text += e.Reason
|
||||
default:
|
||||
text += fmt.Sprintf("conflict at %.12s in %s", e.Commit, strings.Join(e.Files, ", "))
|
||||
}
|
||||
if e.More > 0 {
|
||||
text += fmt.Sprintf(" and %d more", e.More)
|
||||
}
|
||||
lines = append(lines, line{e.Original, text})
|
||||
}
|
||||
for _, e := range d.Primary.Parked {
|
||||
text := "parked: "
|
||||
switch {
|
||||
case e.Kind == parkPartial:
|
||||
text += "only part of it applies, it would leave " + strings.Join(e.Files, ", ") + " without effect"
|
||||
default:
|
||||
var needs []string
|
||||
for _, n := range e.Needs {
|
||||
needs = append(needs, "#"+strconv.Itoa(n))
|
||||
}
|
||||
for _, c := range e.Commits {
|
||||
needs = append(needs, c[:12])
|
||||
}
|
||||
text += "builds on " + strings.Join(needs, ", ") + ", which never reaches the line"
|
||||
}
|
||||
lines = append(lines, line{e.Original, text})
|
||||
}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
for _, n := range *l.list {
|
||||
lines = append(lines, line{n, "skipped." + l.name})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(lines, func(x, y line) int { return cmp.Compare(x.n, y.n) })
|
||||
for _, l := range lines {
|
||||
fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
|
||||
}
|
||||
overlay := func(name string, list []int) {
|
||||
if len(list) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
|
||||
}
|
||||
}
|
||||
overlay("stale, still on the batch", d.Overlays.Stale)
|
||||
overlay("only part of it applies, still on the batch", d.Overlays.Partial)
|
||||
for _, e := range d.Overlays.Dropped {
|
||||
fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
|
||||
}
|
||||
overlay("changed in the pick", d.Overlays.Changed)
|
||||
for _, n := range slices.Sorted(maps.Keys(d.Diag)) {
|
||||
for _, ln := range d.Diag[n] {
|
||||
fmt.Fprintf(a.stdout, "#%s: %s\n", terminalSafe(n), terminalSafe(ln))
|
||||
}
|
||||
}
|
||||
if len(d.Other) > 0 {
|
||||
other := make([]string, len(d.Other))
|
||||
for i, sha := range d.Other {
|
||||
other[i] = safe(sha)
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
func or(s, otherwise string) string {
|
||||
if s == "" {
|
||||
return otherwise
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// postReport writes a report comment on pull request n, after GitHub says
|
||||
// it is still open: nothing is written to a merged or closed one.
|
||||
func (a *app) postReport(n int, s storedReport) error {
|
||||
if err := a.stillOpen(n); err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := reportComment(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name, err := writeTemp("backport-batch-report-*.md", c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
return a.gh.createComment(n, name)
|
||||
}
|
||||
|
||||
// stillOpen reads pull request n again and refuses to go on unless it is
|
||||
// open.
|
||||
func (a *app) stillOpen(n int) error {
|
||||
pr, err := a.gh.pull(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if st := prState(&pr); st != "open" {
|
||||
return fmt.Errorf("batch pull request #%d is %s now; nothing is written to it", n, st)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeTemp(pattern, content string) (string, error) {
|
||||
f, err := os.CreateTemp("", pattern)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := f.WriteString(content); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
}
|
||||
@@ -0,0 +1,778 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// reportsPosted lists the report IDs the comments carry, in the order they
|
||||
// were posted, whoever wrote them.
|
||||
func reportsPosted(gh *fakeGH) []string {
|
||||
var ids []string
|
||||
for _, c := range gh.issueComments {
|
||||
if m := reportMarkerRE.FindStringSubmatch(c.Body); m != nil {
|
||||
ids = append(ids, m[1])
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// bodyReportOf is the report a body on release-1.1, or on the line given,
|
||||
// carries, failing the test without one.
|
||||
func bodyReportOf(t *testing.T, body string, on ...line) storedReport {
|
||||
t.Helper()
|
||||
l := notifyLine
|
||||
if len(on) > 0 {
|
||||
l = on[0]
|
||||
}
|
||||
s, err := bodyReport(body, l)
|
||||
if err != nil || s == nil {
|
||||
t.Fatalf("the body carries no readable report: %v\n%s", err, body)
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
// refusePushes makes the remote refuse every push, as a lost connection or
|
||||
// a protected branch would, until it is called with false.
|
||||
func (f *fixture) refusePushes(on bool) {
|
||||
f.t.Helper()
|
||||
hook := filepath.Join(f.remote, "hooks", "pre-receive")
|
||||
if !on {
|
||||
if err := os.Remove(hook); err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := os.WriteFile(hook, []byte("#!/bin/sh\necho refused >&2\nexit 1\n"), 0o755); err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustRun(t *testing.T, f *fixture, gh *fakeGH, args ...string) string {
|
||||
t.Helper()
|
||||
code, stdout, stderr := f.exec(gh, args...)
|
||||
if code != 0 {
|
||||
t.Fatalf("%q = %d\n%s", args, code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
return stdout
|
||||
}
|
||||
|
||||
// A report comment keeps each classification: one the next run replaced in
|
||||
// the body is still read back by its ID, also once the batch merged, which
|
||||
// nothing writes to.
|
||||
func TestReportSurvivesContinuation(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): a line-only five", map[string]string{"app/five.txt": "line\n"})
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
r1 := bodyReportOf(t, gh.prs[0].Body).doc.ID
|
||||
f.onLine("release-1.1", "fix(app): five, by hand\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "5\n"})
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
r2 := bodyReportOf(t, gh.find(90).Body).doc.ID
|
||||
if r1 == r2 || !slices.Equal(reportsPosted(gh), []string{r1, r2}) {
|
||||
t.Fatalf("reports %q; want R1 %.12s, then R2 %.12s", reportsPosted(gh), r1, r2)
|
||||
}
|
||||
read := func(id, want string) {
|
||||
t.Helper()
|
||||
code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", id)
|
||||
if code != 0 || !strings.Contains(stdout, want) {
|
||||
t.Errorf("report --id %.12s = %d, want %q:\n%s%s", id, code, want, stdout, stderr)
|
||||
}
|
||||
}
|
||||
for _, merged := range []bool{false, true} {
|
||||
if merged {
|
||||
mergeBatch(f, gh)
|
||||
}
|
||||
read(r1, "#5 deferred: conflict at ")
|
||||
read(r2, "#5 skipped.on-line")
|
||||
read(r2, "#6 picked")
|
||||
}
|
||||
}
|
||||
|
||||
// Two runs that find the same post one report, and the body carries the one
|
||||
// kept, whatever time and job the second ran in. A look-alike someone else
|
||||
// wrote is no report, and a kept report edited since stops the reader.
|
||||
func TestReportDedupAndTrust(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
t.Setenv("GITHUB_SERVER_URL", "https://github.com")
|
||||
t.Setenv("GITHUB_REPOSITORY", "cozystack/cozystack")
|
||||
t.Setenv("GITHUB_RUN_ID", "100")
|
||||
t.Setenv("GITHUB_RUN_ATTEMPT", "1")
|
||||
|
||||
// The report the next run makes, read off a run that publishes nothing,
|
||||
// planted by someone the batch does not trust.
|
||||
preview := bodyReportOf(t, mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue"))
|
||||
planted, err := reportComment(preview)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
look := issueComment{ID: 7, Body: planted}
|
||||
look.User.Login, look.User.Type = "someone", "User"
|
||||
gh.issueComments = append(gh.issueComments, look)
|
||||
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
kept := bodyReportOf(t, gh.find(90).Body)
|
||||
if kept.doc.ID != preview.doc.ID {
|
||||
t.Fatalf("the run made %.12s, the preview %.12s", kept.doc.ID, preview.doc.ID)
|
||||
}
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
t.Setenv("GITHUB_RUN_ID", "101")
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
posted := reportsPosted(gh)
|
||||
if n := len(slices.DeleteFunc(slices.Clone(posted), func(id string) bool { return id != kept.doc.ID })); n != 2 || len(posted) != 3 {
|
||||
t.Errorf("reports %q; want the first, the look-alike and the run's own once", posted)
|
||||
}
|
||||
body := gh.find(90).Body
|
||||
again := bodyReportOf(t, body)
|
||||
rec, err := parseRecord(body, notifyLine)
|
||||
if err != nil || again.raw != kept.raw || rec.Report != again.doc.ID || !strings.Contains(again.raw, `"run":"https://github.com/cozystack/cozystack/actions/runs/100"`) {
|
||||
t.Errorf("the second run's body carries %s with record report %v (%v); want the kept bytes of run 100", again.raw, rec, err)
|
||||
}
|
||||
|
||||
// The reader takes the run's own comment, not the look-alike.
|
||||
if code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", kept.doc.ID); code != 0 || !strings.Contains(stdout, "#6 picked") {
|
||||
t.Errorf("report = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
for i := range gh.issueComments {
|
||||
c := &gh.issueComments[i]
|
||||
if c.User.Login == "maintainer" && strings.Contains(c.Body, kept.doc.ID) {
|
||||
c.Body = strings.Replace(c.Body, `"picked":[5,6]`, `"picked":[5]`, 1)
|
||||
}
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", kept.doc.ID); code != 2 || !strings.Contains(stderr, "does not match") {
|
||||
t.Errorf("report of an edited comment = %d, want 2:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The report holds no pull request number, so a new batch's report is whole
|
||||
// before GitHub numbers the pull request, and the reader finds it on
|
||||
// whatever number it got.
|
||||
func TestFirstReportIDIsKnownBeforeTheNumber(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{nextPR: 4242}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
inBody := bodyReportOf(t, gh.bodies[0])
|
||||
// Every list is one, empty or not, so equal reports are equal bytes:
|
||||
// main here has no commit that merged no pull request.
|
||||
if !strings.Contains(inBody.raw, `"other":[]`) {
|
||||
t.Errorf("the report writes its empty list of other commits as something else:\n%s", inBody.raw)
|
||||
}
|
||||
if !slices.Equal(gh.commented, []int{4242}) || !slices.Equal(reportsPosted(gh), []string{inBody.doc.ID}) {
|
||||
t.Fatalf("commented on %v with %q, want #4242 with %.12s", gh.commented, reportsPosted(gh), inBody.doc.ID)
|
||||
}
|
||||
found, bad, err := findReports(gh, 4242, notifyLine)
|
||||
if err != nil || len(bad) != 0 || found[inBody.doc.ID].raw != inBody.raw {
|
||||
t.Errorf("findReports = %v, %v, %v; want the body's report", found, bad, err)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "report", "--pr", "4242"); code != 0 || !strings.Contains(stdout, "report "+inBody.doc.ID) || !strings.Contains(stdout, "#4242's body carries this report") {
|
||||
t.Errorf("report --pr 4242 = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Every original main merged since the line branched is in exactly one
|
||||
// category of the report, a commit that merged no pull request is counted
|
||||
// apart, and the report says so whatever the run had to do.
|
||||
func TestReportPartition(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
gh := &fakeGH{labels: map[string][]int{labelSkip: {9}}}
|
||||
// onNewer puts an original on release-1.1, as the newer line's batch
|
||||
// would, so rule 6 lets it onto release-1.0.
|
||||
onNewer := func(mc string, files map[string]string) {
|
||||
f.onLine("release-1.1", "fix: by the newer batch\n\n(cherry picked from commit "+mc+")", files)
|
||||
}
|
||||
pr := func(n int, title string, files map[string]string, newer bool) {
|
||||
mc := f.pr(n, title, "", files)
|
||||
if newer {
|
||||
onNewer(mc, files)
|
||||
}
|
||||
}
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z")
|
||||
f.pr(4, "fix(app): four, before the start", "", map[string]string{"app/four.txt": "4\n"})
|
||||
os.Unsetenv("GIT_COMMITTER_DATE")
|
||||
f.setPolicy("release-1.0: 2026-06-01\n")
|
||||
f.onLine("release-1.0", "fix(app): a line-only eleven", map[string]string{"app/eleven.txt": "line\n"})
|
||||
pr(5, "fix(app): five", map[string]string{"app/five.txt": "5\n"}, true)
|
||||
pr(6, "fix(app): six, not on the newer line", map[string]string{"app/six.txt": "6\n"}, false)
|
||||
pr(7, "fix(app)!: seven", map[string]string{"app/seven.txt": "7\n"}, true)
|
||||
pr(8, "fix(platform): eight", map[string]string{migrationsDir + "42": "#!/bin/sh\n"}, true)
|
||||
pr(9, "fix(app): nine", map[string]string{"app/nine.txt": "9\n"}, true)
|
||||
mc10 := f.pr(10, "fix(app): ten", "", map[string]string{"app/ten.txt": "10\n"})
|
||||
onNewer(mc10, map[string]string{"app/ten.txt": "10\n"})
|
||||
f.onLine("release-1.0", "fix(app): ten\n\n(cherry picked from commit "+mc10+")", map[string]string{"app/ten.txt": "10\n"})
|
||||
pr(11, "fix(app): eleven", map[string]string{"app/eleven.txt": "11\n"}, true)
|
||||
pr(12, "fix(ci): twelve", map[string]string{".github/workflows/twelve.yaml": "on: push\n"}, true)
|
||||
pr(13, "fix(app): thirteen", map[string]string{"app/thirteen.txt": "13\n"}, true)
|
||||
f.git("switch", "-q", "main")
|
||||
f.write("app/direct.txt", "pushed\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "chore: a direct push")
|
||||
direct := f.git("rev-parse", "HEAD")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
|
||||
stdout := mustRun(t, f, gh, "run", "--line", "release-1.0", "--no-workflow-files", "--push")
|
||||
const branch = "backport-batch/release-1.0-20261026"
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
|
||||
pr(14, "fix(app): fourteen, after the batch", map[string]string{"app/fourteen.txt": "14\n"}, true)
|
||||
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--defer", "13", "--reason", "fails the e2e", "--push")
|
||||
// An active drop, of #5, which the batch carries, and one of a feat,
|
||||
// which rule 2 keeps out first.
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.0", branch, stdout)}
|
||||
stdout = mustRun(t, f, gh, "run", "--line", "release-1.0", "--drop", "5,1", "--reason", "not for 1.0", "--push")
|
||||
|
||||
l, _ := parseLine("release-1.0")
|
||||
s := bodyReportOf(t, stdout, l)
|
||||
got, err := s.doc.categories()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[int]string{1: "skipped.type", 4: "skipped.before-start", 5: "skipped.dropped", 6: "skipped.newer", 7: "skipped.breaking",
|
||||
8: "skipped.touches", 9: "skipped.skip-label", 10: "skipped.on-line", 11: "deferred", 12: "deferred", 13: "deferred",
|
||||
14: "missing", 9000: "skipped.type"}
|
||||
for n, category := range want {
|
||||
if got[n] != category {
|
||||
t.Errorf("#%d is %q, want %q", n, got[n], category)
|
||||
}
|
||||
delete(got, n)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("the report also names %v", got)
|
||||
}
|
||||
if gh.find(90).State != "closed" || !strings.Contains(stdout, `"mode":"close"`) {
|
||||
t.Errorf("dropping the batch's last pick left #90 %s; want it closed", gh.find(90).State)
|
||||
}
|
||||
if !slices.Equal(droppedNumbers(s.doc.Overlays.Dropped), []int{1, 5}) || len(s.doc.Primary.Picked) != 0 {
|
||||
t.Errorf("the report drops %v and picks %v; want #1 and #5 dropped, nothing picked", s.doc.Overlays.Dropped, s.doc.Primary.Picked)
|
||||
}
|
||||
if !slices.Contains(s.doc.Other, direct) {
|
||||
t.Errorf("the report does not count the direct push %.12s: %v", direct, s.doc.Other)
|
||||
}
|
||||
kinds := map[int]string{}
|
||||
for _, e := range s.doc.Primary.Deferred {
|
||||
kinds[e.Original] = fmt.Sprintf("commit=%v reason=%q workflows=%v", e.Commit != "", e.Reason, e.Workflows)
|
||||
}
|
||||
if kinds[11] != `commit=true reason="" workflows=false` || kinds[12] != `commit=false reason="" workflows=true` || kinds[13] != `commit=false reason="fails the e2e" workflows=false` {
|
||||
t.Errorf("the deferred entries are %v", kinds)
|
||||
}
|
||||
}
|
||||
|
||||
// A report too big for a comment is refused before anything is written: no
|
||||
// push, no comment, no edit, and no replay of the earlier report either.
|
||||
func TestReportTooLargeIsRefusedBeforePush(t *testing.T) {
|
||||
f, gh, head := bigWaits(t, 7)
|
||||
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
|
||||
if code != 2 || !strings.Contains(stderr, "the report comment is") {
|
||||
t.Errorf("run --push = %d, want a refusal of the report:\n%s", code, stderr)
|
||||
}
|
||||
if f.remoteHead(batchBranch1) != head || len(gh.issueComments) != 0 || len(gh.edited) != 0 {
|
||||
t.Errorf("the refused run moved the batch to %.12s, wrote %d comments and edited %q", f.remoteHead(batchBranch1), len(gh.issueComments), gh.edited)
|
||||
}
|
||||
}
|
||||
|
||||
// bigWaits opens a batch whose report comment failed, so its body keeps a
|
||||
// report no comment does, and then merges n originals that each wait on 50
|
||||
// workflow files with long names, which both the record and the report list
|
||||
// in full.
|
||||
func bigWaits(t *testing.T, n int) (*fixture, *fakeGH, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{fail: map[string]error{"createComment": errString("HTTP 502")}}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 2 || len(gh.created) != 1 {
|
||||
t.Fatalf("the first run = %d, want its comment to fail after the pull request opened\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
for i := range n {
|
||||
files := map[string]string{}
|
||||
for j := range filesKept {
|
||||
files[fmt.Sprintf(".github/workflows/%s-%02d-%02d.yaml", strings.Repeat("w", 170), i, j)] = "on: push\n"
|
||||
}
|
||||
f.pr(10+i, fmt.Sprintf("fix(ci): wait %d", i), "", files)
|
||||
}
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
return f, gh, f.remoteHead(batchBranch1)
|
||||
}
|
||||
|
||||
// A failure between any two writes leaves what was written readable, and the
|
||||
// next run, whatever changed meanwhile, replays a report the body kept but no
|
||||
// comment did, byte for byte, and posts nothing twice. A batch closed in
|
||||
// between is never written again.
|
||||
func TestReportReplayAfterEachFailure(t *testing.T) {
|
||||
setup := func(t *testing.T) (*fixture, *fakeGH) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f, &fakeGH{}
|
||||
}
|
||||
// change moves a label, main, the clock and the run's flags between
|
||||
// the failure and the retry, so the retry's report is another one.
|
||||
change := func(f *fixture, gh *fakeGH) []string {
|
||||
gh.labels = map[string][]int{labelSkip: {6}}
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
return []string{"run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push"}
|
||||
}
|
||||
once := func(t *testing.T, gh *fakeGH, ids ...string) {
|
||||
t.Helper()
|
||||
posted := reportsPosted(gh)
|
||||
for _, id := range ids {
|
||||
if n := len(slices.DeleteFunc(slices.Clone(posted), func(p string) bool { return p != id })); n != 1 {
|
||||
t.Errorf("report %.12s is posted %d times: %q", id, n, posted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("a new batch whose pull request was not opened", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
gh.fail = map[string]error{"createPull": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
if len(gh.prs) != 0 || len(gh.issueComments) != 0 || f.remoteHead(batchBranch1) == "" {
|
||||
t.Errorf("after the failure: %d pull requests, %d comments, branch %q; want the branch alone", len(gh.prs), len(gh.issueComments), f.remoteHead(batchBranch1))
|
||||
}
|
||||
mustRun(t, f, gh, change(f, gh)...)
|
||||
if len(gh.prs) != 1 || len(reportsPosted(gh)) != 1 || bodyReportOf(t, gh.prs[0].Body).doc.ID != reportsPosted(gh)[0] {
|
||||
t.Errorf("the retry opened %d pull requests with reports %q", len(gh.prs), reportsPosted(gh))
|
||||
}
|
||||
})
|
||||
t.Run("a new batch whose report comment failed", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 || !strings.Contains(stderr, "the body keeps it") {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
first := bodyReportOf(t, gh.find(90).Body)
|
||||
mustRun(t, f, gh, change(f, gh)...)
|
||||
second := bodyReportOf(t, gh.find(90).Body)
|
||||
want, err := reportComment(first)
|
||||
if err != nil || len(gh.issueComments) != 2 || gh.issueComments[0].Body != want || reportsPosted(gh)[1] != second.doc.ID || first.doc.ID == second.doc.ID {
|
||||
t.Errorf("comments %q; want the first report replayed as it was, then the retry's", reportsPosted(gh))
|
||||
}
|
||||
})
|
||||
t.Run("a continuation whose push failed", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
openBatch(t, f, gh)
|
||||
first := reportsPosted(gh)[0]
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
f.refusePushes(true)
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
f.refusePushes(false)
|
||||
attempted := reportsPosted(gh)[1]
|
||||
if bodyReportOf(t, gh.find(90).Body).doc.ID != first {
|
||||
t.Error("the body moved on although the push failed")
|
||||
}
|
||||
mustRun(t, f, gh, change(f, gh)...)
|
||||
once(t, gh, first, attempted)
|
||||
if len(reportsPosted(gh)) != 3 {
|
||||
t.Errorf("reports %q; want the first, the attempted and the retry's", reportsPosted(gh))
|
||||
}
|
||||
})
|
||||
t.Run("a continuation whose body edit failed", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
openBatch(t, f, gh)
|
||||
first := reportsPosted(gh)[0]
|
||||
before := f.remoteHead(batchBranch1)
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh.fail = map[string]error{"editPull": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
attempted := reportsPosted(gh)[1]
|
||||
if f.remoteHead(batchBranch1) == before || bodyReportOf(t, gh.find(90).Body).doc.ID != first {
|
||||
t.Error("the push or the body is not where the failure left them")
|
||||
}
|
||||
mustRun(t, f, gh, change(f, gh)...)
|
||||
once(t, gh, first, attempted)
|
||||
if last := reportsPosted(gh)[len(reportsPosted(gh))-1]; bodyReportOf(t, gh.find(90).Body).doc.ID != last || len(reportsPosted(gh)) != 3 {
|
||||
t.Errorf("reports %q; want the body to carry the retry's, posted last", reportsPosted(gh))
|
||||
}
|
||||
})
|
||||
t.Run("a batch closed while the run writes to it", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
openBatch(t, f, gh)
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh.after = map[string]func(){"createComment": func() { gh.find(90).State = "closed" }}
|
||||
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || len(gh.edited) != 0 {
|
||||
t.Errorf("run = %d, edited %q; want 2 and no edit of the closed pull request:\n%s", code, gh.edited, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a batch closed before its report was posted", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
kept := bodyReportOf(t, gh.find(90).Body)
|
||||
gh.find(90).State = "closed"
|
||||
gh.nextPR = 91
|
||||
mustRun(t, f, gh, change(f, gh)...)
|
||||
if slices.Contains(gh.commented, 90) || slices.Contains(reportsPosted(gh), kept.doc.ID) {
|
||||
t.Errorf("commented on %v, posting %q; want nothing on the closed #90", gh.commented, reportsPosted(gh))
|
||||
}
|
||||
code, stdout, stderr := f.exec(gh, "report", "--pr", "90")
|
||||
if code != 0 || !strings.Contains(stdout, "report "+kept.doc.ID) || !strings.Contains(stdout, "kept in the body only") {
|
||||
t.Errorf("report --pr 90 = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
// Every comment is posted only after GitHub says the batch is still
|
||||
// open, the replay's included, so a close between the two stops the
|
||||
// second.
|
||||
t.Run("a batch closed between the replay and the new report", func(t *testing.T) {
|
||||
f, gh := setup(t)
|
||||
gh.fail = map[string]error{"createComment": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
kept := bodyReportOf(t, gh.find(90).Body)
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh.after = map[string]func(){"createComment": func() { gh.find(90).State = "closed" }}
|
||||
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
|
||||
if code != 2 || !strings.Contains(stderr, "batch pull request #90 is closed now; nothing is written to it") || !slices.Equal(reportsPosted(gh), []string{kept.doc.ID}) || len(gh.edited) != 0 {
|
||||
t.Errorf("run = %d, posted %q, edited %q; want 2 after the replay alone:\n%s", code, reportsPosted(gh), gh.edited, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// The reader says how a report stands against the pull request as GitHub
|
||||
// shows it now, and claims publication only when the head or the body shows
|
||||
// it: a failed push, a push whose answer was lost, a body that was not
|
||||
// updated and a rebuild since all read as they are.
|
||||
func TestReportPublicationStatus(t *testing.T) {
|
||||
setup := func(t *testing.T) (*fixture, *fakeGH, string) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
return f, gh, reportsPosted(gh)[0]
|
||||
}
|
||||
status := func(t *testing.T, f *fixture, gh *fakeGH, id, want string) {
|
||||
t.Helper()
|
||||
code, stdout, stderr := f.exec(gh, "report", "--pr", "90", "--id", id)
|
||||
if code != 0 || !strings.Contains(stdout, want) {
|
||||
t.Errorf("report --id %.12s = %d, want %q:\n%s%s", id, code, want, stdout, stderr)
|
||||
}
|
||||
}
|
||||
t.Run("a push the remote refused, and one it took whose answer was lost", func(t *testing.T) {
|
||||
f, gh, first := setup(t)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
f.refusePushes(true)
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.refusePushes(false)
|
||||
attempted := reportsPosted(gh)[1]
|
||||
status(t, f, gh, attempted, "publication unconfirmed: #90's head is ")
|
||||
status(t, f, gh, attempted, "its body carries report "+first[:12])
|
||||
// The same head reaching the remote is what a push GitHub took looks
|
||||
// like when its answer never came back.
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
|
||||
f.git("switch", "-q", "main")
|
||||
status(t, f, gh, attempted, "the batch this report describes, but its body carries report "+first[:12])
|
||||
})
|
||||
t.Run("a push followed by a failed body edit", func(t *testing.T) {
|
||||
f, gh, first := setup(t)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
gh.fail = map[string]error{"editPull": errString("HTTP 502")}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
status(t, f, gh, reportsPosted(gh)[1], "the batch this report describes, but its body carries report "+first[:12])
|
||||
status(t, f, gh, first, "#90's body carries this report")
|
||||
})
|
||||
t.Run("a rebuild since", func(t *testing.T) {
|
||||
f, gh, first := setup(t)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "x", "--push")
|
||||
second := reportsPosted(gh)[1]
|
||||
status(t, f, gh, first, "publication unconfirmed: #90's head is ")
|
||||
status(t, f, gh, second, "#90's body carries this report")
|
||||
})
|
||||
}
|
||||
|
||||
// forged is a report on release-1.1 with what change sets, under an ID that
|
||||
// matches it: only checking what it says stands in its way.
|
||||
func forged(change func(d *reportDoc)) storedReport {
|
||||
sha := strings.Repeat("a", 40)
|
||||
d := reportDoc{Format: reportFormat, Line: "release-1.1", Diag: map[string][]string{}, Other: []string{sha},
|
||||
Built: builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeNew}}}
|
||||
d.Primary.Picked = []int{5}
|
||||
change(&d)
|
||||
d.ID = reportID(d)
|
||||
return storedReport{doc: d, raw: reportJSON(d)}
|
||||
}
|
||||
|
||||
// reportRun runs report with gh faked; it reads nothing but GitHub.
|
||||
func reportRun(gh *fakeGH, args ...string) (int, string, string) {
|
||||
var stdout, stderr strings.Builder
|
||||
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 0, 0, 0, time.UTC) }}
|
||||
code := a.main(append([]string{"report"}, args...))
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
// A report is read only when everything it says is what a run writes, and
|
||||
// it is for the line of the pull request it sits on: an ID that matches its
|
||||
// content proves only that nobody changed it since it was hashed.
|
||||
func TestReportRefusesWhatItCannotVouchFor(t *testing.T) {
|
||||
for name, change := range map[string]func(d *reportDoc){
|
||||
"a line that is no release line": func(d *reportDoc) { d.Line = "release-1.1\n\x1b[2Jpicked" },
|
||||
"another line": func(d *reportDoc) { d.Line = "release-1.0" },
|
||||
"a main that is no commit": func(d *reportDoc) { d.Built.Main = "main\n#5 picked" },
|
||||
"a time that is none": func(d *reportDoc) { d.Built.At = "2026-10-26T<b>" },
|
||||
"an unknown operation": func(d *reportDoc) { d.Built.Options.Mode = "new\x1b[2J" },
|
||||
"a run that is no Actions run": func(d *reportDoc) { d.Built.Run = "javascript:alert(1)" },
|
||||
"a tool that is no commit": func(d *reportDoc) { d.Built.Tool = "v1\n" },
|
||||
"a commit on main that is none": func(d *reportDoc) { d.Other = []string{"HEAD\x1b]0;x\x07"} },
|
||||
"a deferred commit that is none": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Commit: "-x", Files: []string{"a"}}}
|
||||
},
|
||||
"an original named twice": func(d *reportDoc) { d.Primary.Missing = []int{5} },
|
||||
"an original that is none": func(d *reportDoc) { d.Primary.Skipped.Type = []int{-1} },
|
||||
"a diagnostic of two lines": func(d *reportDoc) { d.Diag = map[string][]string{"5": {"incomplete\n\x1b[2Jforged"}} },
|
||||
"a diagnostic of no original": func(d *reportDoc) { d.Diag = map[string][]string{"five": {"incomplete"}} },
|
||||
// Fields that speak of one another have to agree.
|
||||
"a drop category with no drop": func(d *reportDoc) { d.Primary.Skipped.Dropped = []int{6} },
|
||||
"a stale original it has missing": func(d *reportDoc) {
|
||||
d.Primary.Missing, d.Overlays.Stale = []int{7}, []int{7}
|
||||
},
|
||||
"a stale original it puts nowhere": func(d *reportDoc) { d.Overlays.Stale = []int{8} },
|
||||
"a changed original it defers": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 9, Files: []string{}, Reason: "fails the e2e"}}
|
||||
d.Overlays.Changed = []int{9}
|
||||
},
|
||||
"a skip label on a picked original": func(d *reportDoc) { d.Overrides.Skip = []int{5} },
|
||||
"a skip-label category with no label": func(d *reportDoc) { d.Primary.Skipped.SkipLabel = []int{10} },
|
||||
"an include label on a rule 2 skip": func(d *reportDoc) {
|
||||
d.Primary.Skipped.Type, d.Overrides.Include = []int{11}, []int{11}
|
||||
},
|
||||
"both labels on one original": func(d *reportDoc) {
|
||||
d.Primary.Skipped.SkipLabel, d.Overrides.Include, d.Overrides.Skip = []int{12}, []int{12}, []int{12}
|
||||
},
|
||||
"a label on no original": func(d *reportDoc) { d.Overrides.Include = []int{13} },
|
||||
"a diagnostic of an original it puts nowhere": func(d *reportDoc) { d.Diag = map[string][]string{"14": {"incomplete"}} },
|
||||
"a workflow wait without the flag": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 15, Files: []string{".github/workflows/ci.yaml"}, Workflows: true}}
|
||||
},
|
||||
"a close that still picks": func(d *reportDoc) { d.Built.Options.Mode = modeClose },
|
||||
"a park with no merge": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
"a parked original it picks": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 5, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
"a park of an unknown kind": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: "later", Files: []string{"a"}}}
|
||||
},
|
||||
"a partial park that needs an original": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Needs: []int{5}}}
|
||||
},
|
||||
"a partial park of unsorted files": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"b", "a"}}}
|
||||
},
|
||||
"a partial park of a file that drives a terminal": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a\x1b[2J"}}}
|
||||
},
|
||||
"a park announced in the report": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Announced: true}}
|
||||
},
|
||||
"a park for an original it picks": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{5}}}
|
||||
},
|
||||
"a park that needs itself": func(d *reportDoc) {
|
||||
d.Primary.Skipped.Type = []int{17}
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}}}
|
||||
},
|
||||
"a park for a commit that merged a pull request": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("c", 40)}}}
|
||||
},
|
||||
"a partial unit not on the batch": func(d *reportDoc) { d.Overlays.Partial = []int{5} },
|
||||
"a wait on an original it picks": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{5}}}
|
||||
},
|
||||
"a wait on an original it puts nowhere": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
|
||||
},
|
||||
"a park on its own merge as a commit": func(d *reportDoc) {
|
||||
d.Other = append(d.Other, strings.Repeat("b", 40))
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("b", 40)}}}
|
||||
},
|
||||
"a park cycle": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{18}}, {Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16}}}
|
||||
},
|
||||
"a wait cycle": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{7}}, {Original: 7, Files: []string{}, Needs: []int{6}}}
|
||||
},
|
||||
"format 1 with a wait": func(d *reportDoc) {
|
||||
d.Format = 1
|
||||
d.Primary.Missing = []int{19}
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
|
||||
},
|
||||
"format 1 with a park": func(d *reportDoc) {
|
||||
d.Format = 1
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
} {
|
||||
s := forged(change)
|
||||
c, _ := reportComment(storedReport{doc: forged(func(*reportDoc) {}).doc, raw: s.raw})
|
||||
comment := issueComment{ID: 1, Body: strings.Replace(c, forged(func(*reportDoc) {}).doc.ID, s.doc.ID, 1)}
|
||||
comment.User.Login, comment.User.Type = "maintainer", "User"
|
||||
for where, gh := range map[string]*fakeGH{
|
||||
"in the body": {prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(s.raw)+"\n")}},
|
||||
"in a comment": {prs: []pullRequest{notifyPR("20261026", "text")}, issueComments: []issueComment{comment}},
|
||||
} {
|
||||
if code, stdout, stderr := reportRun(gh, "--pr", "90", "--id", s.doc.ID); code != 2 || stdout != "" {
|
||||
t.Errorf("%s, %s: report = %d, want 2 and nothing printed:\n%s%s", name, where, code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(forged(func(*reportDoc) {}).raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
|
||||
t.Errorf("report of a sound one = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
// What a run wrote before anything was parked reads as it was written.
|
||||
format1 := forged(func(d *reportDoc) { d.Format, d.Primary.Parked = 1, []parkedEntry{} })
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(format1.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
|
||||
t.Errorf("report of a sound format 1 one = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
parked := forged(func(d *reportDoc) {
|
||||
d.Primary.Skipped.Type = []int{17}
|
||||
d.Other = append(d.Other, strings.Repeat("c", 40))
|
||||
d.Primary.Parked = []parkedEntry{
|
||||
{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"chart/templates/policy.yaml"}},
|
||||
{Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}, Commits: []string{strings.Repeat("c", 40)}},
|
||||
}
|
||||
})
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(parked.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#16 parked: only part of it applies") || !strings.Contains(stdout, "#18 parked: builds on #16, #17, cccccccccccc") {
|
||||
t.Errorf("report of sound parks = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What report prints stays one line per field and carries nothing a
|
||||
// terminal acts on, whatever reached it.
|
||||
func TestReportPrintsNothingThatDrivesATerminal(t *testing.T) {
|
||||
const evil = "\n\x1b[2Jfake: #9 picked\u202e"
|
||||
s := forged(func(d *reportDoc) {
|
||||
d.Line, d.Built.At, d.Built.Options.Mode = "release-1.1"+evil, "2026-10-26"+evil, "new"+evil
|
||||
d.Built.Main, d.Built.Tool, d.Built.Run, d.Built.Newer = "main"+evil, "tool"+evil, "run"+evil, "newer"+evil
|
||||
d.Other = []string{"other" + evil}
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Reason: "why" + evil}}
|
||||
})
|
||||
var stdout strings.Builder
|
||||
a := &app{stdout: &stdout}
|
||||
pr := notifyPR("20261026", "")
|
||||
a.printReport(&pr, s, nil, true)
|
||||
for _, ln := range strings.Split(stdout.String(), "\n") {
|
||||
if strings.HasPrefix(ln, "fake") || strings.ContainsFunc(ln, func(r rune) bool { return r != '\t' && unicode.IsControl(r) || unicode.Is(unicode.Cf, r) }) {
|
||||
t.Errorf("report printed %q", ln)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The comment is made of what was checked: a line, a time and an ID that
|
||||
// carry no markup into the summary GitHub renders.
|
||||
func TestReportCommentIsBuiltFromCheckedFields(t *testing.T) {
|
||||
for name, change := range map[string]func(d *reportDoc){
|
||||
"markup in the line": func(d *reportDoc) { d.Line = "release-1.1</summary><img src=x onerror=alert(1)>" },
|
||||
"markup in the time": func(d *reportDoc) { d.Built.At = "<b>2026-10-26</b>T06:00:00Z" },
|
||||
"a mention": func(d *reportDoc) { d.Line = "release-1.1 @cozystack/maintainers" },
|
||||
} {
|
||||
if c, err := reportComment(forged(change)); err == nil {
|
||||
t.Errorf("%s: reportComment made\n%s", name, c)
|
||||
}
|
||||
}
|
||||
if _, err := reportComment(forged(func(*reportDoc) {})); err != nil {
|
||||
t.Errorf("reportComment of a sound report: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Anyone can comment on a public pull request: the reader takes a report
|
||||
// from a comment only when the batch trusts its author, whatever ID it
|
||||
// carries.
|
||||
func TestReportReaderTakesOnlyTrustedComments(t *testing.T) {
|
||||
s := forged(func(*reportDoc) {})
|
||||
c, err := reportComment(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
login, kind string
|
||||
read bool
|
||||
}{
|
||||
{"someone", "User", false},
|
||||
{"renovate[bot]", "Bot", false},
|
||||
{"github-actions[bot]", "Bot", false},
|
||||
{"maintainer", "User", true},
|
||||
{defaultBatchBot, "Bot", true},
|
||||
} {
|
||||
comment := issueComment{ID: 1, Body: c}
|
||||
comment.User.Login, comment.User.Type = tc.login, tc.kind
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", "text")}, issueComments: []issueComment{comment}, bot: defaultBatchBot}
|
||||
code, stdout, stderr := reportRun(gh, "--pr", "90", "--id", s.doc.ID)
|
||||
if read := code == 0 && strings.Contains(stdout, "#5 picked"); read != tc.read || !tc.read && (code != 2 || !strings.Contains(stderr, "keeps no report")) {
|
||||
t.Errorf("a report by %s: report = %d, read %v, want %v:\n%s%s", tc.login, code, read, tc.read, stdout, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A report whose ID is not one says so without repeating it as written.
|
||||
func TestReportErrorsRepeatNothingRaw(t *testing.T) {
|
||||
s := forged(func(*reportDoc) {})
|
||||
// Short enough that a cut to 12 characters keeps the forged line.
|
||||
raw := strings.Replace(s.raw, `"id":"`+s.doc.ID+`"`, `"id":"\nforged"`, 1)
|
||||
code, _, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(raw)+"\n")}}, "--pr", "90")
|
||||
for _, ln := range strings.Split(stderr, "\n") {
|
||||
if strings.HasPrefix(ln, "forged") {
|
||||
t.Errorf("report = %d, printed the forged line %q", code, ln)
|
||||
}
|
||||
}
|
||||
if code != 2 {
|
||||
t.Errorf("report = %d, want 2:\n%s", code, stderr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,344 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Rule numbers as docs/release/backports.md numbers them. Rule 5, a clean
|
||||
// pick, is decided while the batch is assembled.
|
||||
const (
|
||||
ruleOnLine = 1
|
||||
ruleType = 2
|
||||
ruleBreaking = 3
|
||||
ruleTouches = 4
|
||||
ruleNewer = 6
|
||||
// Not a rule of the policy: the backport/skip override.
|
||||
ruleSkipLabel = 100
|
||||
// Rule 1 for a line that started under the policy after it branched:
|
||||
// merged before that start.
|
||||
ruleBeforeStart = 101
|
||||
// Not a rule of the policy: a maintainer dropped the original from the
|
||||
// line with run --drop.
|
||||
ruleDropped = 102
|
||||
)
|
||||
|
||||
const (
|
||||
// The numbered scripts run-migrations.sh walks; hack/check-migrations-target.sh
|
||||
// reads the same directory.
|
||||
migrationsDir = "packages/core/platform/images/migrations/migrations/"
|
||||
// Where migrations.targetVersion is set; hack/check-migrations-target.sh
|
||||
// reads it from here.
|
||||
platformValues = "packages/core/platform/values.yaml"
|
||||
// Only a file holding this text at either end of the change can hold a
|
||||
// CRD document; crdDocs decides document by document.
|
||||
crdKind = "CustomResourceDefinition"
|
||||
)
|
||||
|
||||
// The title grammar and the breaking footer are pr-labeler.yaml's, so that a
|
||||
// pull request labelled kind/bug and kind/breaking-change reads the same here.
|
||||
var (
|
||||
backportPrefixRE = regexp.MustCompile(`^(?:\[Backport [^\]]+\]\s+)+`)
|
||||
conventionalRE = regexp.MustCompile(`^([a-z]+)(?:\(([^)]+)\))?(!)?:\s*\S`)
|
||||
breakingFooterRE = regexp.MustCompile(`(?m)^BREAKING[ -]CHANGE:`)
|
||||
htmlCommentRE = regexp.MustCompile(`(?s)<!--.*?-->`)
|
||||
targetVersionRE = regexp.MustCompile(`(?m)^[+-][ \t]*targetVersion[ \t]*:`)
|
||||
)
|
||||
|
||||
// title is a parsed Conventional Commits title.
|
||||
type title struct {
|
||||
typ string
|
||||
scopes []string
|
||||
bang bool
|
||||
}
|
||||
|
||||
func parseTitle(s string) (title, bool) {
|
||||
m := conventionalRE.FindStringSubmatch(backportPrefixRE.ReplaceAllString(s, ""))
|
||||
if m == nil {
|
||||
return title{}, false
|
||||
}
|
||||
t := title{typ: m[1], bang: m[3] == "!"}
|
||||
for scope := range strings.SplitSeq(m[2], ",") {
|
||||
if scope = strings.TrimSpace(scope); scope != "" {
|
||||
t.scopes = append(t.scopes, scope)
|
||||
}
|
||||
}
|
||||
return t, true
|
||||
}
|
||||
|
||||
// typeRule is rule 2 together with the policy's "never in a patch release":
|
||||
// dependency bumps, scope deps, stay out whatever their type.
|
||||
func typeRule(s string) string {
|
||||
t, ok := parseTitle(s)
|
||||
switch {
|
||||
case !ok:
|
||||
return "the title is not a Conventional Commits title"
|
||||
case !slices.Contains(takenTypes, t.typ):
|
||||
return "type " + t.typ
|
||||
case slices.ContainsFunc(t.scopes, func(s string) bool { return slices.Contains(excludedScopes, s) }):
|
||||
return "an excluded scope"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// breakingRule is rule 3. The PR template quotes the footer inside an HTML
|
||||
// comment, which a body merged with the template left in carries.
|
||||
func breakingRule(s, body string) string {
|
||||
if t, ok := parseTitle(s); ok && t.bang {
|
||||
return "! in the title"
|
||||
}
|
||||
if breakingFooterRE.MatchString(htmlCommentRE.ReplaceAllString(body, "")) {
|
||||
return "BREAKING CHANGE footer"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// fileChange is one path a merge changes against its first parent.
|
||||
type fileChange struct {
|
||||
status byte
|
||||
path string
|
||||
}
|
||||
|
||||
func changedFiles(from, to string) ([]fileChange, error) {
|
||||
out, err := git("diff", "--no-ext-diff", "--no-textconv", "--name-status", "-z", "--no-renames", "--end-of-options", from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := splitNUL(out)
|
||||
if len(f)%2 != 0 {
|
||||
return nil, fmt.Errorf("unreadable diff of %s..%s", from, to)
|
||||
}
|
||||
var changes []fileChange
|
||||
for i := 0; i < len(f); i += 2 {
|
||||
changes = append(changes, fileChange{status: f[i][0], path: f[i+1]})
|
||||
}
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
func paths(changes []fileChange) []string {
|
||||
out := make([]string, 0, len(changes))
|
||||
for _, c := range changes {
|
||||
out = append(out, c.path)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
const (
|
||||
touchesMigrations = "platform migrations"
|
||||
touchesTargetVersion = "targetVersion"
|
||||
touchesAPI = "a sizeable API change"
|
||||
touchesCRD = "a CRD"
|
||||
)
|
||||
|
||||
// touchesRule is rule 4: what of migrations, targetVersion, the API and CRDs
|
||||
// the merge touches, or "" when it touches none of them.
|
||||
func touchesRule(o *original, changes []fileChange) (string, error) {
|
||||
hit := map[string]bool{}
|
||||
for _, c := range changes {
|
||||
p := c.path
|
||||
switch {
|
||||
case strings.HasPrefix(p, migrationsDir):
|
||||
hit[touchesMigrations] = true
|
||||
case p == platformValues:
|
||||
diff, err := git("--literal-pathspecs", "diff", "-U0", "--no-ext-diff", "--no-textconv", "--end-of-options", o.merge+"^1", o.merge, "--", platformValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hit[touchesTargetVersion] = targetVersionRE.MatchString(diff)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if hit[touchesCRD], err = changesCRD(o.merge+"^1", o.merge, changes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
asks, err := readsAPI(o.merge+"^1", o.merge, changes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
apiDetail := ""
|
||||
if asks {
|
||||
if apiDetail, err = sizeableAPIChange(o); err != nil {
|
||||
return "", err
|
||||
}
|
||||
hit[touchesAPI] = apiDetail != ""
|
||||
}
|
||||
var why []string
|
||||
for _, r := range []string{touchesMigrations, touchesTargetVersion, touchesAPI, touchesCRD} {
|
||||
switch {
|
||||
case !hit[r]:
|
||||
case r == touchesAPI:
|
||||
why = append(why, r+" ("+apiDetail+")")
|
||||
default:
|
||||
why = append(why, r)
|
||||
}
|
||||
}
|
||||
return strings.Join(why, ", "), nil
|
||||
}
|
||||
|
||||
// changesCRD reports whether the change adds, removes or modifies a
|
||||
// CustomResourceDefinition document. It looks document by document: an edit to
|
||||
// the DaemonSet beside a CRD in one manifest is not a CRD change, and a CRD in
|
||||
// a test fixture changes nothing a cluster runs.
|
||||
func changesCRD(parent, merge string, changes []fileChange) (bool, error) {
|
||||
var files []string
|
||||
for _, c := range changes {
|
||||
if manifest(c.path) && !testFixture(c.path) {
|
||||
files = append(files, c.path)
|
||||
}
|
||||
}
|
||||
candidates := map[string]bool{}
|
||||
for _, rev := range []string{parent, merge} {
|
||||
found, err := grepFiles(rev, files, "-I", "-e", crdKind)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, f := range found {
|
||||
candidates[f] = true
|
||||
}
|
||||
}
|
||||
for _, c := range changes {
|
||||
if !candidates[c.path] {
|
||||
continue
|
||||
}
|
||||
var before, after map[string]string
|
||||
if c.status != 'A' {
|
||||
content, err := git("cat-file", "blob", parent+":"+c.path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
before = crdDocs(content)
|
||||
}
|
||||
if c.status != 'D' {
|
||||
content, err := git("cat-file", "blob", merge+":"+c.path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
after = crdDocs(content)
|
||||
}
|
||||
if !maps.Equal(before, after) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// manifest reports whether path is a file a chart or a cluster can apply: YAML,
|
||||
// JSON or a Helm template. A CRD quoted in documentation or code applies
|
||||
// nothing.
|
||||
func manifest(path string) bool {
|
||||
switch filepath.Ext(path) {
|
||||
case ".yaml", ".yml", ".json", ".tpl":
|
||||
return true
|
||||
}
|
||||
dirs := strings.Split(path, "/")
|
||||
return slices.ContainsFunc(dirs[:len(dirs)-1], func(d string) bool { return d == "templates" || d == "crds" })
|
||||
}
|
||||
|
||||
func testFixture(path string) bool {
|
||||
dirs := strings.Split(path, "/")
|
||||
return slices.ContainsFunc(dirs[:len(dirs)-1], func(d string) bool { return d == "tests" || d == "testdata" })
|
||||
}
|
||||
|
||||
// grepFiles lists which of files at rev hold any of the fixed strings that
|
||||
// patterns give. Paths are passed literally, so a name such as ":(exclude)x"
|
||||
// means only that file.
|
||||
func grepFiles(rev string, files []string, patterns ...string) ([]string, error) {
|
||||
const chunk = 500
|
||||
var found []string
|
||||
for len(files) > 0 {
|
||||
n := min(chunk, len(files))
|
||||
args := append([]string{"--literal-pathspecs", "grep", "-l", "-z", "-F"}, patterns...)
|
||||
args = append(append(args, "--end-of-options", rev, "--"), files[:n]...)
|
||||
stdout, stderr, code, err := gitStatus(args...)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, err
|
||||
case code != 0 && code != 1:
|
||||
return nil, fmt.Errorf("git grep at %s: exit %d: %s", rev, code, strings.TrimSpace(stderr))
|
||||
}
|
||||
for _, hit := range splitNUL(stdout) {
|
||||
if path, ok := strings.CutPrefix(hit, rev+":"); ok {
|
||||
found = append(found, path)
|
||||
}
|
||||
}
|
||||
files = files[n:]
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
var (
|
||||
docSeparatorRE = regexp.MustCompile(`(?m)^---[ \t]*(?:#.*)?$`)
|
||||
// A document that declares the kind on a line of its own, in YAML or in
|
||||
// JSON, under a bare or quoted key, is a CRD even when it cannot be read;
|
||||
// a sentence or a lookup call that names it declares nothing.
|
||||
crdKindLineRE = regexp.MustCompile(`(?m)^[ \t]*(?:-[ \t]+)?(?:kind|"kind"|'kind')[ \t]*:[ \t]*["']?CustomResourceDefinition["']?[ \t]*,?[ \t]*(?:#.*)?$`)
|
||||
)
|
||||
|
||||
// crdDocs maps the CRD documents of a file to their text, keyed by
|
||||
// metadata.name, so a CRD added, removed or edited changes the map. Every
|
||||
// document that names the kind is read, so a CRD in compact JSON or under a
|
||||
// quoted key counts as well. A document that declares the kind on a line of
|
||||
// its own but cannot be read as an object, a CRD behind a Helm template guard
|
||||
// for one, is kept under its own text, so that any change to it counts: the
|
||||
// check fails closed. So does a list holding a CRD.
|
||||
func crdDocs(content string) map[string]string {
|
||||
docs := map[string]string{}
|
||||
for _, doc := range docSeparatorRE.Split(content, -1) {
|
||||
declares := crdKindLineRE.MatchString(doc)
|
||||
if !declares && !strings.Contains(doc, crdKind) {
|
||||
continue
|
||||
}
|
||||
var head struct {
|
||||
Kind string `json:"kind"`
|
||||
Metadata struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"metadata"`
|
||||
Items []struct {
|
||||
Kind string `json:"kind"`
|
||||
} `json:"items"`
|
||||
}
|
||||
key := ""
|
||||
switch err := yaml.Unmarshal([]byte(doc), &head); {
|
||||
case err != nil && declares:
|
||||
key = "unreadable:" + doc
|
||||
case err != nil:
|
||||
continue
|
||||
case head.Kind == crdKind && head.Metadata.Name != "":
|
||||
key = "crd:" + head.Metadata.Name
|
||||
case head.Kind == crdKind:
|
||||
key = "unnamed:" + doc
|
||||
case strings.HasSuffix(head.Kind, "List") && slices.ContainsFunc(head.Items, func(i struct {
|
||||
Kind string `json:"kind"`
|
||||
}) bool {
|
||||
return i.Kind == crdKind
|
||||
}):
|
||||
key = "list:" + doc
|
||||
default:
|
||||
continue
|
||||
}
|
||||
docs[key] += doc
|
||||
}
|
||||
return docs
|
||||
}
|
||||
@@ -0,0 +1,461 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTypeRule(t *testing.T) {
|
||||
cases := []struct {
|
||||
title string
|
||||
taken bool
|
||||
}{
|
||||
{"fix(postgres): keep the replica count on resize", true},
|
||||
{"fix: typo in monitoring-agents values.yaml", true},
|
||||
{"security(dashboard): escape the tenant name", true},
|
||||
{"fix(ci): retry the flaky e2e step", true},
|
||||
{"[Backport release-1.6] fix(linstor): x", true},
|
||||
{"[Backport release-1.5] [Backport release-1.6] fix(linstor): x", true},
|
||||
{"fix(platform, dashboard): two scopes", true},
|
||||
{"feat(postgres): add a field", false},
|
||||
{"chore(release): promote", false},
|
||||
{"docs(release): split the page", false},
|
||||
{"refactor(api): move code", false},
|
||||
{"fix(deps): bump golang.org/x/net", false},
|
||||
{"fix(platform, deps): bump and fix", false},
|
||||
{"Fix(postgres): capitalised type", false},
|
||||
{"fix postgres without a colon", false},
|
||||
{"[postgres] bracket form", false},
|
||||
{"fixes(postgres): not the fix type", false},
|
||||
{"", false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
reason := typeRule(tc.title)
|
||||
if (reason == "") != tc.taken {
|
||||
t.Errorf("typeRule(%q) = %q, want taken=%v", tc.title, reason, tc.taken)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBreakingRule(t *testing.T) {
|
||||
cases := []struct {
|
||||
name, title, body string
|
||||
breaking bool
|
||||
}{
|
||||
{"plain fix", "fix(x): y", "## What this PR does\n\nFixes it.", false},
|
||||
{"bang", "fix(x)!: y", "", true},
|
||||
{"bang without scope", "fix!: y", "", true},
|
||||
{"bang behind a backport prefix", "[Backport release-1.6] fix(x)!: y", "", true},
|
||||
{"footer", "fix(x): y", "Body.\n\nBREAKING CHANGE: the field is gone", true},
|
||||
{"hyphenated footer", "fix(x): y", "BREAKING-CHANGE: the field is gone", true},
|
||||
{"footer quoted mid-line", "fix(x): y", "This is not a BREAKING CHANGE: nothing changes.", false},
|
||||
{"footer inside the template comment", "fix(x): y", "<!--\nBREAKING CHANGE: example\n-->\nBody.", false},
|
||||
{"lower-case footer", "fix(x): y", "breaking change: no", false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
reason := breakingRule(tc.title, tc.body)
|
||||
if (reason != "") != tc.breaking {
|
||||
t.Errorf("%s: breakingRule = %q, want breaking=%v", tc.name, reason, tc.breaking)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rule 4 reads the merge itself, so each case is a pull request merged into a
|
||||
// real repository.
|
||||
func TestTouchesRule(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
setup map[string]string // committed on main before the pull request
|
||||
files map[string]string
|
||||
want string
|
||||
}{
|
||||
{name: "a chart change", files: map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"}},
|
||||
{name: "a migration script", files: map[string]string{migrationsDir + "61": "#!/bin/sh\n"}, want: "platform migrations"},
|
||||
{name: "a migration library file", files: map[string]string{migrationsDir + "lib/common.sh": "x\n"}, want: "platform migrations"},
|
||||
{name: "the migration runner is not a migration", files: map[string]string{"packages/core/platform/images/migrations/run-migrations.sh": "x\n"}},
|
||||
{
|
||||
name: "targetVersion moves",
|
||||
setup: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 60\n"},
|
||||
files: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 61\n"},
|
||||
want: "targetVersion",
|
||||
},
|
||||
{
|
||||
name: "another key of the platform values",
|
||||
setup: map[string]string{platformValues: "migrations:\n enabled: false\n targetVersion: 60\n"},
|
||||
files: map[string]string{platformValues: "migrations:\n enabled: true\n targetVersion: 60\n"},
|
||||
},
|
||||
{name: "api types the gate sees nothing in", setup: demoAPI, files: map[string]string{"api/apps/v1alpha1/types.go": "package v1alpha1\n"}},
|
||||
{name: "pkg/apis is not api/", files: map[string]string{"pkg/apis/apps/types.go": "package apps\n"}},
|
||||
{name: "a CRD added under crds/", files: map[string]string{"packages/system/x/charts/x/crds/a.yaml": crdDoc("as.x.io", "a") + "\n"}, want: "a CRD"},
|
||||
{name: "a ConfigMap under crds/ is not a CRD", files: map[string]string{"packages/system/x/charts/x/crds/a.yaml": "kind: ConfigMap\n"}},
|
||||
{
|
||||
name: "a DaemonSet edit next to an unchanged CRD",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("2"))},
|
||||
},
|
||||
{
|
||||
name: "a real CRD edit in a manifest with other documents",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "b"), daemonSet("1"))},
|
||||
want: "a CRD",
|
||||
},
|
||||
{
|
||||
name: "a CRD renamed",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("as.x.io", "a"), daemonSet("1"))},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(crdDoc("bs.x.io", "a"), daemonSet("1"))},
|
||||
want: "a CRD",
|
||||
},
|
||||
{name: "a CRD in a fixture under tests/", files: map[string]string{"packages/system/x/tests/render_test.yaml": crdDoc("as.x.io", "a")}},
|
||||
{name: "a CRD in a fixture under testdata/", files: map[string]string{"hack/testdata/crd.yaml": crdDoc("as.x.io", "a")}},
|
||||
{
|
||||
name: "a templated CRD edited, which cannot be read",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("1"))},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("b"), daemonSet("1"))},
|
||||
want: "a CRD",
|
||||
},
|
||||
{
|
||||
name: "a DaemonSet edit next to an unchanged templated CRD",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("1"))},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": multiDoc(templatedCRD("a"), daemonSet("2"))},
|
||||
},
|
||||
{
|
||||
name: "an unparseable file that names the kind, changed",
|
||||
setup: map[string]string{"x/broken.yaml": "kind: CustomResourceDefinition\nspec: [unclosed\n"},
|
||||
files: map[string]string{"x/broken.yaml": "kind: CustomResourceDefinition\nspec: [still unclosed\n"},
|
||||
want: "a CRD",
|
||||
},
|
||||
{
|
||||
name: "a lookup that names the kind in a Helm helper",
|
||||
setup: map[string]string{"packages/system/x/templates/_helpers.tpl": "{{- $crd := lookup \"apiextensions.k8s.io/v1\" \"CustomResourceDefinition\" \"\" .crd -}}\n{{- if $crd }}a{{ end }}\n"},
|
||||
files: map[string]string{"packages/system/x/templates/_helpers.tpl": "{{- $crd := lookup \"apiextensions.k8s.io/v1\" \"CustomResourceDefinition\" \"\" .crd -}}\n{{- if $crd }}b{{ end }}\n"},
|
||||
},
|
||||
{
|
||||
name: "a CRD example in documentation",
|
||||
setup: map[string]string{"docs/crds.md": "Example:\n\n```yaml\nkind: CustomResourceDefinition\nmetadata:\n name: as.x.io\n```\n"},
|
||||
files: map[string]string{"docs/crds.md": "An example:\n\n```yaml\nkind: CustomResourceDefinition\nmetadata:\n name: bs.x.io\n```\n"},
|
||||
},
|
||||
{name: "a CRD in a template without a YAML extension", files: map[string]string{"packages/system/x/templates/crd.tmpl": crdDoc("as.x.io", "a")}, want: "a CRD"},
|
||||
{name: "a CRD added as JSON", files: map[string]string{"x/crd.json": `{"apiVersion": "apiextensions.k8s.io/v1",` + "\n" + ` "kind": "CustomResourceDefinition",` + "\n" + ` "metadata": {"name": "as.x.io"}}` + "\n"}, want: "a CRD"},
|
||||
{name: "a List of CRDs", files: map[string]string{"x/list.yaml": "kind: List\nitems:\n- kind: CustomResourceDefinition\n metadata:\n name: as.x.io\n"}, want: "a CRD"},
|
||||
{name: "a template declaring a CRD", files: map[string]string{"packages/system/x/templates/crd.yaml": "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\n"}, want: "a CRD"},
|
||||
{name: "a quoted declaration", files: map[string]string{"x/crd.yaml": " kind: \"CustomResourceDefinition\" # yes\n"}, want: "a CRD"},
|
||||
{
|
||||
name: "a CRD changed in place",
|
||||
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\nspec: {}\n"},
|
||||
files: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\nspec: {a: 1}\n"},
|
||||
want: "a CRD",
|
||||
},
|
||||
{
|
||||
name: "a CRD deleted",
|
||||
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\n"},
|
||||
files: map[string]string{"x/crd.yaml": ""},
|
||||
want: "a CRD",
|
||||
},
|
||||
{
|
||||
name: "a CRD turned into something else",
|
||||
setup: map[string]string{"x/crd.yaml": "kind: CustomResourceDefinition\n"},
|
||||
files: map[string]string{"x/crd.yaml": "kind: ConfigMap\n"},
|
||||
want: "a CRD",
|
||||
},
|
||||
{name: "a CRD list is not a CRD", files: map[string]string{"x/list.yaml": "kind: CustomResourceDefinitionList\n"}},
|
||||
{name: "a CRD as compact JSON", files: map[string]string{"x/crd.json": `{"apiVersion":"apiextensions.k8s.io/v1","kind":"CustomResourceDefinition","metadata":{"name":"as.x.io"}}` + "\n"}, want: "a CRD"},
|
||||
{name: "a CRD under a quoted kind key", files: map[string]string{"x/crd.yaml": "'kind': CustomResourceDefinition\nmetadata:\n name: as.x.io\n"}, want: "a CRD"},
|
||||
{
|
||||
name: "a templated CRD under a quoted kind key, edited",
|
||||
setup: map[string]string{"packages/system/x/templates/m.yaml": strings.Replace(templatedCRD("a"), "kind:", "'kind':", 1)},
|
||||
files: map[string]string{"packages/system/x/templates/m.yaml": strings.Replace(templatedCRD("b"), "kind:", "'kind':", 1)},
|
||||
want: "a CRD",
|
||||
},
|
||||
{name: "a ConfigMap as compact JSON naming the kind", files: map[string]string{"x/cm.json": `{"kind":"ConfigMap","data":{"note":"kind: CustomResourceDefinition"}}` + "\n"}},
|
||||
{name: "a CRD mentioned in prose", files: map[string]string{"x/README.md": "Set kind: CustomResourceDefinition here.\n"}},
|
||||
{name: "a file named like an option declaring a CRD", files: map[string]string{"-e.yaml": "kind: CustomResourceDefinition\n"}, want: "a CRD"},
|
||||
// As pathspec magic, :(icase)... would match a file of that name in
|
||||
// another case, and there is none.
|
||||
{name: "a file named like pathspec magic declaring a CRD", files: map[string]string{":(icase)zz-crd.yaml": "kind: CustomResourceDefinition\n"}, want: "a CRD"},
|
||||
{
|
||||
name: "a migration and a sizeable API change together",
|
||||
setup: demoAPI,
|
||||
files: map[string]string{migrationsDir + "61": "x\n", "api/x.go": "package x\n", demoRD: cozyrd("demo", `{"type":"object","properties":{}}`)},
|
||||
want: "platform migrations, a sizeable API change (breaking-change: [v1alpha1] spec: field \"size\" was removed)",
|
||||
},
|
||||
}
|
||||
for i, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
setup := maps.Clone(apiBase)
|
||||
maps.Copy(setup, tc.setup)
|
||||
f.squash(1000, "chore: setup", "", setup)
|
||||
mc := f.pr(10+i, "fix(x): y", "", tc.files)
|
||||
o := &original{number: 10 + i, merge: mc, parents: 2}
|
||||
changes, err := changedFiles(mc+"^1", mc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := touchesRule(o, changes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("touchesRule = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Rule 6: an older line takes only what the newer line already carries, and
|
||||
// rule 1: neither line takes what it carries already.
|
||||
func TestNewerLineAndOnLine(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(2, "fix(app): before nothing", "", map[string]string{"app/a.txt": "a\n"})
|
||||
f.pr(3, "fix(app): picked onto 1.1", "", map[string]string{"app/b.txt": "b\n"})
|
||||
mc3 := f.git("rev-parse", "HEAD")
|
||||
f.pr(4, "fix(app): only on main", "", map[string]string{"app/c.txt": "c\n"})
|
||||
f.onLine("release-1.1", "fix(app): picked onto 1.1 (#3)\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/b.txt": "b\n"})
|
||||
|
||||
for _, tc := range []struct {
|
||||
line string
|
||||
qualified []int
|
||||
skipped map[int]int
|
||||
}{
|
||||
{line: "release-1.1", qualified: []int{2, 4}, skipped: map[int]int{3: ruleOnLine}},
|
||||
// #1 is a feature; #2 and #4 are not on release-1.1 yet; #3 is.
|
||||
{line: "release-1.0", qualified: []int{3}, skipped: map[int]int{1: ruleType, 2: ruleNewer, 4: ruleNewer}},
|
||||
} {
|
||||
t.Run(tc.line, func(t *testing.T) {
|
||||
f.git("fetch", "-q", "origin")
|
||||
l, _ := parseLine(tc.line)
|
||||
b, err := (&app{gh: &fakeGH{}, stderr: &strings.Builder{}}).prepare(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got []int
|
||||
for _, v := range b.qualifying() {
|
||||
got = append(got, v.o.number)
|
||||
}
|
||||
if !slices.Equal(got, tc.qualified) {
|
||||
t.Errorf("qualifying = %v, want %v", got, tc.qualified)
|
||||
}
|
||||
for n, rule := range tc.skipped {
|
||||
if v := b.find(n); v == nil || v.rule != rule {
|
||||
t.Errorf("#%d: verdict %+v, want rule %d", n, v, rule)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// An original merged before the newer line branched is on it, even when the
|
||||
// newer line has since changed the same lines and a pick would conflict.
|
||||
func TestNewerLineCarriesWhatItBranchedWith(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.release("release-1.0")
|
||||
f.pr(2, "fix(app): two", "", map[string]string{"app/a.txt": "two\n"})
|
||||
f.release("release-1.1")
|
||||
mc3 := f.pr(3, "fix(app): three", "", map[string]string{"app/a.txt": "three\n"})
|
||||
f.onLine("release-1.1", "fix(app): three (#3)\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/a.txt": "three\n"})
|
||||
if v := verdictOn(t, f, nil, "release-1.0", 2); v.rule != 0 {
|
||||
t.Errorf("#2 on release-1.0 = rule %d (%s), want it to qualify", v.rule, v.reason)
|
||||
}
|
||||
}
|
||||
|
||||
// A line branched and not yet released is one the batch runs for, and rule 6
|
||||
// holds each older line to the next newer one.
|
||||
func TestNewerLineInTheRCWindow(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.release("release-1.2")
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
for _, tc := range []struct{ line, reason string }{
|
||||
{"release-1.2", ""},
|
||||
{"release-1.1", "not on release-1.2 yet"},
|
||||
{"release-1.0", "not on release-1.1 yet"},
|
||||
} {
|
||||
v := verdictOn(t, f, nil, tc.line, 5)
|
||||
if v.reason != tc.reason || (v.rule == 0) != (tc.reason == "") {
|
||||
t.Errorf("#5 on %s = rule %d (%s), want %q", tc.line, v.rule, v.reason, tc.reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlySupportedLines(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.release("release-0.9")
|
||||
f.lines()
|
||||
code, _, stderr := f.exec(nil, "check", "--line", "release-0.9")
|
||||
if code != 2 || !strings.Contains(stderr, "not a line the batch runs for") {
|
||||
t.Errorf("check on release-0.9 = %d (%s), want 2 and a refusal", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// demoRD is an ApplicationDefinition where apigate looks for one, and demoAPI
|
||||
// the tree the API cases start from.
|
||||
const demoRD = "packages/system/demo-rd/cozyrds/demo.yaml"
|
||||
|
||||
// apiBase is the least API a tree holds: apigate refuses a tree with none.
|
||||
var apiBase = map[string]string{"packages/system/keep-rd/cozyrds/keep.yaml": cozyrd("keep", `{"type":"object"}`)}
|
||||
|
||||
var demoAPI = map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string","description":"Size."}}}`),
|
||||
"api/apps/v1alpha1/demo/types.go": "package demo\n\n// Size of it.\ntype Spec struct{ Size string }\n",
|
||||
}
|
||||
|
||||
func cozyrd(name, schema string) string {
|
||||
return "apiVersion: cozystack.io/v1alpha1\nkind: ApplicationDefinition\nmetadata:\n name: " + name +
|
||||
"\nspec:\n application:\n kind: " + strings.ToUpper(name[:1]) + name[1:] + "\n plural: " + name + "s\n singular: " + name +
|
||||
"\n openAPISchema: '" + schema + "'\n"
|
||||
}
|
||||
|
||||
// Rule 4 holds back a change to the API only when the API review gate calls it
|
||||
// sizeable: a new or removed resource or group, or a breaking schema change.
|
||||
func TestAPIGateRule(t *testing.T) {
|
||||
typesGo := "api/apps/v1alpha1/demo/types.go"
|
||||
cases := []struct {
|
||||
name string
|
||||
files map[string]string
|
||||
want string
|
||||
}{
|
||||
{name: "a description reworded", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string","description":"The size."}}}`),
|
||||
typesGo: "package demo\n\n// The size.\ntype Spec struct{ Size string }\n",
|
||||
}},
|
||||
{name: "an optional field added", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"string"},"zone":{"type":"string"}}}`),
|
||||
typesGo: "package demo\n\ntype Spec struct{ Size, Zone string }\n",
|
||||
}},
|
||||
{name: "a field removed", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{}}`),
|
||||
typesGo: "package demo\n\ntype Spec struct{}\n",
|
||||
}, want: "breaking-change"},
|
||||
{name: "a field's type changed", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{"size":{"type":"integer"}}}`),
|
||||
typesGo: "package demo\n\ntype Spec struct{ Size int }\n",
|
||||
}, want: "breaking-change"},
|
||||
{name: "a field made required", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","required":["size"],"properties":{"size":{"type":"string"}}}`),
|
||||
typesGo: "package demo\n\n// +required\ntype Spec struct{ Size string }\n",
|
||||
}, want: "breaking-change"},
|
||||
{name: "a resource added", files: map[string]string{
|
||||
"packages/system/other-rd/cozyrds/other.yaml": cozyrd("other", `{"type":"object"}`),
|
||||
"api/apps/v1alpha1/other/types.go": "package other\n",
|
||||
}, want: "new-resource"},
|
||||
{name: "a resource removed", files: map[string]string{
|
||||
demoRD: "",
|
||||
typesGo: "",
|
||||
}, want: "removed"},
|
||||
{name: "a breaking schema change that does not touch api/", files: map[string]string{
|
||||
demoRD: cozyrd("demo", `{"type":"object","properties":{}}`),
|
||||
}, want: "breaking-change"},
|
||||
{name: "an aggregated group registered in a chart", files: map[string]string{
|
||||
"packages/system/extra/templates/apiservice.yaml": apiService("extra.cozystack.io"),
|
||||
}, want: "new-api-group"},
|
||||
}
|
||||
for i, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
setup := maps.Clone(apiBase)
|
||||
maps.Copy(setup, demoAPI)
|
||||
f.squash(1000, "chore: setup", "", setup)
|
||||
mc := f.pr(10+i, "fix(x): y", "", tc.files)
|
||||
o := &original{number: 10 + i, merge: mc, parents: 2}
|
||||
changes, err := changedFiles(mc+"^1", mc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := touchesRule(o, changes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (got != "") != (tc.want != "") || !strings.Contains(got, tc.want) {
|
||||
t.Errorf("touchesRule = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func apiService(group string) string {
|
||||
return "apiVersion: apiregistration.k8s.io/v1\nkind: APIService\nmetadata:\n name: v1alpha1." + group +
|
||||
"\nspec:\n group: " + group + "\n version: v1alpha1\n"
|
||||
}
|
||||
|
||||
// The API review gate is asked about exactly the changes that reach what
|
||||
// apigate parses at either end of the merge.
|
||||
func TestReadsAPI(t *testing.T) {
|
||||
crd := crdDoc("as.x.io", "a")
|
||||
cases := []struct {
|
||||
name string
|
||||
setup map[string]string
|
||||
files map[string]string
|
||||
want bool
|
||||
}{
|
||||
{name: "the apiserver's registrations", files: map[string]string{apiserverGo: "package apiserver\n"}, want: true},
|
||||
{name: "an ApplicationDefinition, which carries no marker", files: map[string]string{demoRD: "kind: ApplicationDefinition\n"}, want: true},
|
||||
{name: "a CRD in a chart", files: map[string]string{"packages/system/x/crds/a.yaml": crd}, want: true},
|
||||
{name: "an APIService under internal/", files: map[string]string{"internal/x/apiservice.yml": apiService("x.cozystack.io")}, want: true},
|
||||
{
|
||||
name: "a CRD turned into something else",
|
||||
setup: map[string]string{"packages/system/x/crds/a.yaml": crd},
|
||||
files: map[string]string{"packages/system/x/crds/a.yaml": "kind: ConfigMap\n"},
|
||||
want: true,
|
||||
},
|
||||
{name: "a binary file with a marker", files: map[string]string{"packages/system/x/a.yaml": "\x00kind: APIService\n"}, want: true},
|
||||
{name: "chart values", files: map[string]string{"packages/system/x/values.yaml": "replicas: 2\n"}},
|
||||
{name: "Go types under api/", files: map[string]string{"api/apps/v1alpha1/demo/types.go": "package demo\n"}},
|
||||
{name: "a CRD outside the roots apigate walks", files: map[string]string{"hack/crds/a.yaml": crd}},
|
||||
{name: "a CRD in a template without a YAML extension", files: map[string]string{"packages/system/x/templates/crd.tpl": crd}},
|
||||
{name: "a file beside the ApplicationDefinitions", files: map[string]string{"packages/system/demo-rd/cozyrds/sub/demo.yaml": "kind: ApplicationDefinition\n"}},
|
||||
}
|
||||
for i, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
setup := maps.Clone(apiBase)
|
||||
maps.Copy(setup, tc.setup)
|
||||
f.squash(1000, "chore: setup", "", setup)
|
||||
mc := f.pr(10+i, "fix(x): y", "", tc.files)
|
||||
changes, err := changedFiles(mc+"^1", mc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := readsAPI(mc+"^1", mc, changes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("readsAPI = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func crdDoc(name, version string) string {
|
||||
return "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n name: " + name +
|
||||
"\nspec:\n group: x.io\n versions:\n - name: v1" + version + "\n"
|
||||
}
|
||||
|
||||
func daemonSet(replicas string) string {
|
||||
return "apiVersion: apps/v1\nkind: DaemonSet\nmetadata:\n name: agent\nspec:\n template:\n spec:\n priority: {{ .Values.priority | default " + replicas + " }}\n"
|
||||
}
|
||||
|
||||
func templatedCRD(version string) string {
|
||||
return "{{- if .Values.crds }}\n" + crdDoc("ts.x.io", version) + "{{- end }}\n"
|
||||
}
|
||||
|
||||
func multiDoc(docs ...string) string { return "---\n" + strings.Join(docs, "---\n") }
|
||||
@@ -0,0 +1,513 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A batch carries each original as one unit on its first-parent line. A
|
||||
// merged pull request is a merge commit whose first parent is the batch before
|
||||
// it and whose second parent ends a straight line of picks of the pull
|
||||
// request's own commits, oldest first, each keeping its author and sign-offs;
|
||||
// the merge says which merge commit on main it carries in a Backported-from
|
||||
// trailer. A squash-merged original is one pick of its commit on main.
|
||||
|
||||
// pickOriginal picks one original onto HEAD in that shape. A conflict on any
|
||||
// of its commits takes the branch back to where it was before the original,
|
||||
// so nothing of it is left half applied, and so does a clean pick validate
|
||||
// finds leaving something of the original without effect: partial, with
|
||||
// those paths. Hooks are off so that each commit carries its message and -x
|
||||
// line as git writes them; signing is left to the user's configuration.
|
||||
func pickOriginal(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
|
||||
p := pick{v: v}
|
||||
base, err := resolve("HEAD")
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
for _, c := range v.commits {
|
||||
status, files, err := applyPick("-c", "core.hooksPath=/dev/null", "cherry-pick", "-x", "--signoff", "--end-of-options", c)
|
||||
if err != nil {
|
||||
if rerr := resetTo(base); rerr != nil {
|
||||
return p, rerr
|
||||
}
|
||||
return p, fmt.Errorf("cherry-pick of %.12s of #%d: %w", c, v.o.number, err)
|
||||
}
|
||||
if status == "conflict" {
|
||||
p.status, p.files, p.failed = "conflict", files, c
|
||||
return p, resetTo(base)
|
||||
}
|
||||
}
|
||||
head, err := resolve("HEAD")
|
||||
switch {
|
||||
case err != nil:
|
||||
return p, err
|
||||
case head == base && validate != nil:
|
||||
// Picks that change nothing carry the original only when the line
|
||||
// already has its changes: a change made where its branch merged
|
||||
// main is in no pick.
|
||||
omitted, err := validate(base, head)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
if len(omitted) > 0 {
|
||||
p.status, p.files = "partial", omitted
|
||||
return p, nil
|
||||
}
|
||||
p.status = "empty"
|
||||
return p, nil
|
||||
case head == base:
|
||||
p.status = "empty"
|
||||
return p, nil
|
||||
}
|
||||
if validate != nil {
|
||||
omitted, err := validate(base, head)
|
||||
if err == nil && len(omitted) > 0 {
|
||||
p.status, p.files = "partial", omitted
|
||||
}
|
||||
if err != nil || len(omitted) > 0 {
|
||||
return p, errors.Join(err, resetTo(base))
|
||||
}
|
||||
}
|
||||
if v.o.parents < 2 {
|
||||
p.status, p.commit = "picked", head
|
||||
return p, nil
|
||||
}
|
||||
msgs, err := commitMessages([]string{v.o.merge})
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
if p.commit, err = commitMerge(base, head, mergeMessage(msgs[v.o.merge].message, v.o.merge)); err != nil {
|
||||
if rerr := resetTo(base); rerr != nil {
|
||||
return p, rerr
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
p.status = "picked"
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// mergeMessage is the message of a batch merge: the one of the merge commit
|
||||
// on main, which says which pull request it was, and a paragraph of its own
|
||||
// naming that merge commit, so nothing the original's text ends with can pass
|
||||
// for the trailer.
|
||||
func mergeMessage(message, merge string) string {
|
||||
return strings.TrimRight(message, " \t\r\n") + "\n\n" + batchTrailer + ": " + merge + "\n"
|
||||
}
|
||||
|
||||
// commitMerge makes the merge of tip onto base, whose tree is tip's since tip
|
||||
// descends from base, and moves the current branch from tip to it.
|
||||
func commitMerge(base, tip, message string) (string, error) {
|
||||
f, err := os.CreateTemp("", "backport-batch-merge-*.txt")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
if _, err := f.WriteString(message); err != nil {
|
||||
f.Close()
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
out, err := git("commit-tree", "-p", base, "-p", tip, "-F", f.Name(), tip+"^{tree}")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
merge := strings.TrimSpace(out)
|
||||
if err := checkSHA(merge); err != nil {
|
||||
return "", fmt.Errorf("commit-tree: %w", err)
|
||||
}
|
||||
if _, err := git("update-ref", "-m", "backport-batch: merge", "HEAD", merge, tip); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return merge, nil
|
||||
}
|
||||
|
||||
// resetTo takes the current branch, its index and its tree back to commit,
|
||||
// which run owns: it starts only in a clean tree.
|
||||
func resetTo(commit string) error {
|
||||
_, err := git("reset", "--quiet", "--hard", commit)
|
||||
return err
|
||||
}
|
||||
|
||||
// unit is one original as a batch carries it: a commit on the batch's
|
||||
// first-parent line, and for a merge the picks its second parent brings.
|
||||
type unit struct {
|
||||
commit string
|
||||
parents []string
|
||||
message string
|
||||
// picks are the commits reachable from the second parent and not from the
|
||||
// first, oldest first.
|
||||
picks []string
|
||||
}
|
||||
|
||||
func (u unit) merge() bool { return len(u.parents) == 2 }
|
||||
|
||||
func readUnit(commit string) (unit, error) {
|
||||
msgs, err := commitMessages([]string{commit})
|
||||
if err != nil {
|
||||
return unit{}, err
|
||||
}
|
||||
u := unit{commit: commit, parents: msgs[commit].parents, message: msgs[commit].message}
|
||||
if u.merge() {
|
||||
if u.picks, err = revList("--reverse", "--topo-order", "--end-of-options", u.parents[1], "^"+u.parents[0]); err != nil {
|
||||
return unit{}, err
|
||||
}
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// batchUnits lists the units of the batch from base to head, oldest first.
|
||||
func batchUnits(head, base string) ([]unit, error) {
|
||||
oids, err := revList("--reverse", "--first-parent", "--end-of-options", head, "^"+base)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var units []unit
|
||||
for _, oid := range oids {
|
||||
u, err := readUnit(oid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
units = append(units, u)
|
||||
}
|
||||
return units, nil
|
||||
}
|
||||
|
||||
// unitOriginal is the original a unit says it carries, or nil: the one its
|
||||
// Backported-from trailer names, or the one its appended -x line picks.
|
||||
func (b *batch) unitOriginal(u unit) *verdict {
|
||||
if u.merge() {
|
||||
if merges := batchedFrom(u.message); len(merges) == 1 {
|
||||
return b.findMerge(merges[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
source := appendedSource(u.message)
|
||||
if v := b.findMerge(source); v != nil {
|
||||
return v
|
||||
}
|
||||
return b.pickedBy([]string{source})
|
||||
}
|
||||
|
||||
// ownerOf is the original one of whose own commits source is, or nil. It reads
|
||||
// every original's commits, so it is for explaining a refusal only.
|
||||
func (b *batch) ownerOf(source string) (*verdict, error) {
|
||||
for _, v := range b.verdicts {
|
||||
if v.commits == nil {
|
||||
commits, err := prCommits(v.o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.commits = commits
|
||||
}
|
||||
if v.o.parents > 1 && slices.Contains(v.commits, source) {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// checkUnit says why u is not one original carried in the batch's shape, or
|
||||
// "" when it is, and which original it carries: a merge whose second parent
|
||||
// is a straight line from its first, of picks of the pull request's own
|
||||
// commits in their order, each with the -x line of the commit it came from,
|
||||
// that names the original's merge commit in its trailer; or for a squash one
|
||||
// pick of its commit on main. Either changes something, and only files the
|
||||
// original changes. A pick is known by the -x line git appended, not by
|
||||
// lines its message brought along, and one that also claims another commit
|
||||
// of the same original is refused.
|
||||
func checkUnit(b *batch, u unit) (string, *verdict, error) {
|
||||
switch len(u.parents) {
|
||||
case 1:
|
||||
return checkPick(b, u)
|
||||
case 2:
|
||||
return checkMerge(b, u)
|
||||
}
|
||||
return fmt.Sprintf("%.12s has %d parents", u.commit, len(u.parents)), nil, nil
|
||||
}
|
||||
|
||||
func checkPick(b *batch, u unit) (string, *verdict, error) {
|
||||
source := appendedSource(u.message)
|
||||
if source == "" {
|
||||
return fmt.Sprintf("%.12s ends in no (cherry picked from commit …) line; a batch carries originals only", u.commit), nil, nil
|
||||
}
|
||||
v := b.findMerge(source)
|
||||
if v == nil {
|
||||
owner, err := b.ownerOf(source)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if owner != nil {
|
||||
return fmt.Sprintf("%.12s picks one commit of #%d on its own; a merged pull request goes in as a merge of all its commits", u.commit, owner.o.number), owner, nil
|
||||
}
|
||||
return fmt.Sprintf("%.12s picks %s, which merged no pull request into main after %s branched", u.commit, source, b.line.branch()), nil, nil
|
||||
}
|
||||
if v.o.parents > 1 {
|
||||
return fmt.Sprintf("%.12s picks the merge commit of #%d as one commit; a merged pull request goes in as a merge of its own commits", u.commit, v.o.number), v, nil
|
||||
}
|
||||
why, err := contained(b, v, u.parents[0], u.commit)
|
||||
return why, v, err
|
||||
}
|
||||
|
||||
func checkMerge(b *batch, u unit) (string, *verdict, error) {
|
||||
merges := batchedFrom(u.message)
|
||||
if len(merges) != 1 {
|
||||
return fmt.Sprintf("%.12s is a merge with %d %s trailers, not one", u.commit, len(merges), batchTrailer), nil, nil
|
||||
}
|
||||
v := b.findMerge(merges[0])
|
||||
switch {
|
||||
case v == nil:
|
||||
return fmt.Sprintf("%.12s names %s, which merged no pull request into main after %s branched", u.commit, merges[0], b.line.branch()), nil, nil
|
||||
case v.o.parents < 2:
|
||||
return fmt.Sprintf("%.12s merges #%d, which main took as one commit; it goes in as one pick", u.commit, v.o.number), v, nil
|
||||
case len(u.picks) == 0:
|
||||
return fmt.Sprintf("%.12s merges nothing", u.commit), v, nil
|
||||
}
|
||||
if err := b.load(v); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
msgs, err := commitMessages(u.picks)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
prev, last := u.parents[0], -1
|
||||
for _, c := range u.picks {
|
||||
ps := msgs[c].parents
|
||||
if len(ps) != 1 || ps[0] != prev {
|
||||
return fmt.Sprintf("%.12s is not on a straight line of picks from %.12s", c, u.parents[0]), v, nil
|
||||
}
|
||||
source := appendedSource(msgs[c].message)
|
||||
if source == "" {
|
||||
return fmt.Sprintf("%.12s ends in no (cherry picked from commit …) line", c), v, nil
|
||||
}
|
||||
i := slices.Index(v.commits, source)
|
||||
if i < 0 {
|
||||
owner, err := b.ownerOf(source)
|
||||
switch {
|
||||
case err != nil:
|
||||
return "", nil, err
|
||||
case owner != nil:
|
||||
return fmt.Sprintf("%.12s picks a commit of #%d into the merge of #%d", c, owner.o.number, v.o.number), v, nil
|
||||
}
|
||||
return fmt.Sprintf("%.12s picks %s, which is not a commit of #%d", c, source, v.o.number), v, nil
|
||||
}
|
||||
if i <= last {
|
||||
return fmt.Sprintf("%.12s picks %.12s out of the order of #%d's commits", c, source, v.o.number), v, nil
|
||||
}
|
||||
for _, claimed := range claimedSources(msgs[c].message) {
|
||||
if claimed != source && (claimed == v.o.merge || slices.Contains(v.commits, claimed)) {
|
||||
return fmt.Sprintf("%.12s claims more than one commit of #%d", c, v.o.number), v, nil
|
||||
}
|
||||
}
|
||||
prev, last = c, i
|
||||
}
|
||||
why, err := contained(b, v, u.parents[0], u.commit)
|
||||
return why, v, err
|
||||
}
|
||||
|
||||
// contained says why the change from base to tip is not inside the files v
|
||||
// changes, or the paths a rename on main or on the line gave them at base,
|
||||
// or "" when it is.
|
||||
func contained(b *batch, v *verdict, base, tip string) (string, error) {
|
||||
if err := b.load(v); err != nil {
|
||||
return "", err
|
||||
}
|
||||
changes, err := changedFiles(base, tip)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(changes) == 0 {
|
||||
return fmt.Sprintf("%.12s changes nothing", tip), nil
|
||||
}
|
||||
corr, _, err := b.correspondence(v, base)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
allowed := paths(v.changes)
|
||||
for _, c := range corr {
|
||||
allowed = append(allowed, c)
|
||||
}
|
||||
var extra []string
|
||||
for _, p := range paths(changes) {
|
||||
if !slices.Contains(allowed, p) {
|
||||
extra = append(extra, strconv.Quote(p))
|
||||
}
|
||||
}
|
||||
if len(extra) > 0 {
|
||||
return fmt.Sprintf("%.12s touches %s, which #%d does not touch", tip, strings.Join(extra, ", "), v.o.number), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// replayUnit picks a unit of the batch again onto HEAD in its own shape: a
|
||||
// pushed resolution keeps what it resolved. On a conflict it takes the branch
|
||||
// back to where it was before the unit and names the commit that failed.
|
||||
func replayUnit(u unit) (string, []string, error) {
|
||||
base, err := resolve("HEAD")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
picks := u.picks
|
||||
if !u.merge() {
|
||||
picks = []string{u.commit}
|
||||
}
|
||||
for _, c := range picks {
|
||||
status, files, err := applyPick("-c", "core.hooksPath=/dev/null", "cherry-pick", "--end-of-options", c)
|
||||
if err == nil && status == "conflict" {
|
||||
return c, files, resetTo(base)
|
||||
}
|
||||
if err != nil {
|
||||
if rerr := resetTo(base); rerr != nil {
|
||||
return "", nil, rerr
|
||||
}
|
||||
return "", nil, fmt.Errorf("picking %.12s again: %w", c, err)
|
||||
}
|
||||
}
|
||||
tip, err := resolve("HEAD")
|
||||
if err != nil || !u.merge() || tip == base {
|
||||
return "", nil, err
|
||||
}
|
||||
_, err = commitMerge(base, tip, u.message)
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// pickPair is a pick on the batch and the commit it was picked from.
|
||||
type pickPair struct{ source, pick string }
|
||||
|
||||
// unitShape is what an original's unit on the batch brought: how many
|
||||
// commits, the picks that differ from the commit they came from, a
|
||||
// resolution or a pick git had to fit to the line, and whether the unit as a
|
||||
// whole makes another change than the original's merge.
|
||||
type unitShape struct {
|
||||
unit string
|
||||
commits int
|
||||
pairs []pickPair
|
||||
net bool
|
||||
}
|
||||
|
||||
// changed says whether the unit differs from its original, so its release
|
||||
// note may no longer hold.
|
||||
func (s unitShape) changed() bool { return len(s.pairs) > 0 || s.net }
|
||||
|
||||
// String is the shape as the body lists it.
|
||||
func (s unitShape) String() string {
|
||||
out := "1 commit"
|
||||
if s.commits != 1 {
|
||||
out = fmt.Sprintf("%d commits", s.commits)
|
||||
}
|
||||
if len(s.pairs) > 0 {
|
||||
var picks []string
|
||||
for _, p := range s.pairs {
|
||||
picks = append(picks, codeSpan(p.pick[:12]))
|
||||
}
|
||||
out += ", changed in the pick: " + strings.Join(picks, ", ")
|
||||
} else if s.net {
|
||||
out += ", changed as a whole: some of its change is not in the picks"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// summarize says, for each original the batch head carries in units, what
|
||||
// its unit brought.
|
||||
func summarize(b *batch, head string, on []*verdict) (map[*verdict]unitShape, error) {
|
||||
units, err := batchUnits(head, b.lineHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[*verdict]unitShape{}
|
||||
for _, u := range units {
|
||||
v := b.unitOriginal(u)
|
||||
if v == nil || !slices.Contains(on, v) {
|
||||
continue
|
||||
}
|
||||
picks := u.picks
|
||||
if !u.merge() {
|
||||
picks = []string{u.commit}
|
||||
}
|
||||
msgs, err := commitMessages(picks)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := unitShape{unit: u.commit, commits: len(picks)}
|
||||
for _, c := range picks {
|
||||
source := appendedSource(msgs[c].message)
|
||||
if source == "" {
|
||||
continue
|
||||
}
|
||||
same, err := samePatch(source, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !same {
|
||||
s.pairs = append(s.pairs, pickPair{source: source, pick: c})
|
||||
}
|
||||
}
|
||||
// A pick that came out empty leaves no pick to differ, only a net
|
||||
// change short of the original's.
|
||||
mine, err := patchIDRange(v.o.merge+"^1", v.o.merge)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
theirs, err := patchIDRange(u.parents[0], u.commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.net = mine != theirs
|
||||
out[v] = s
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func plural(n int, word string) string {
|
||||
if n == 1 {
|
||||
return word
|
||||
}
|
||||
return word + "s"
|
||||
}
|
||||
|
||||
// samePatch says whether two commits make the same change to their first
|
||||
// parents, by git patch-id of their diffs without context, so that a pick
|
||||
// that only lands among other lines on the line counts as the same.
|
||||
func samePatch(a, b string) (bool, error) {
|
||||
pa, err := patchIDRange(a+"^", a)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
pb, err := patchIDRange(b+"^", b)
|
||||
return pa == pb, err
|
||||
}
|
||||
|
||||
// patchIDRange is the patch-id of the change from one revision to another.
|
||||
func patchIDRange(from, to string) (string, error) {
|
||||
diff, err := git("diff", "--no-ext-diff", "--no-textconv", "--unified=0", "--end-of-options", from, to)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
out, err := gitInput(diff, "patch-id", "--stable")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
id, _, _ := strings.Cut(strings.TrimSpace(out), " ")
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A conflict on a commit in the middle of a pull request takes the batch back
|
||||
// to where it was before that original: none of its commits stays, the
|
||||
// conflict is recorded with the commit that failed, and the next original is
|
||||
// still picked.
|
||||
func TestRunConflictInAMiddleCommit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
|
||||
mc5 := f.prCommits(5, "fix(chart): five", "",
|
||||
change{files: map[string]string{"app/five.txt": "5\n"}},
|
||||
change{files: map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"}},
|
||||
change{files: map[string]string{"app/five.txt": "5, done\n"}})
|
||||
own := strings.Fields(f.git("rev-list", "--reverse", mc5+"^1.."+mc5+"^2"))
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("the batch carries %q, want only #6", got)
|
||||
}
|
||||
if log := f.git("log", "--format=%B", "origin/release-1.1..HEAD"); strings.Contains(log, own[0]) || strings.Contains(log, own[1]) {
|
||||
t.Errorf("a commit of #5 stayed on the batch:\n%s", log)
|
||||
}
|
||||
if out := f.git("status", "--porcelain"); out != "" {
|
||||
t.Errorf("the tree is left dirty:\n%s", out)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(f.dir, ".git", "CHERRY_PICK_HEAD")); err == nil {
|
||||
t.Error("a cherry-pick is left in progress")
|
||||
}
|
||||
l, _ := parseLine("release-1.1")
|
||||
deferred, err := parseDeferred(stdout, l)
|
||||
if err != nil || deferred[5].Commit != own[1] || !slices.Equal(deferred[5].Files, []string{"chart/values.yaml"}) {
|
||||
t.Errorf("deferred = %+v, %v; want #5 at %s in chart/values.yaml", deferred, err, own[1])
|
||||
}
|
||||
if !strings.Contains(stdout, "`fix(chart): five` at `"+own[1][:12]+"`: `chart/values.yaml`") {
|
||||
t.Errorf("the body does not name the commit that failed:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// What counts as carrying a merged pull request of several commits: picks
|
||||
// whose appended -x lines name every commit of its own, as a batch merge's do,
|
||||
// or a pick whose appended -x line names its merge commit; not some of its
|
||||
// commits. A Backported-from trailer counts only on a commit from before the
|
||||
// batch-only day, and only in its trailer block.
|
||||
func TestPresenceOfAMultiCommitOriginal(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
land func(f *fixture, mc string, own []string)
|
||||
carried string
|
||||
}{
|
||||
{
|
||||
name: "a batch merge of its commits",
|
||||
land: func(f *fixture, mc string, own []string) {
|
||||
f.git("fetch", "-q", "origin")
|
||||
base := f.git("rev-parse", "origin/release-1.1")
|
||||
f.git("switch", "-q", "--detach", base)
|
||||
for _, c := range own {
|
||||
f.git("cherry-pick", "-x", c)
|
||||
}
|
||||
f.git("switch", "-q", "--detach", f.joined(base, f.git("rev-parse", "HEAD"), "fix(app): five (#5)\n\n"+batchTrailer+": "+mc))
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/release-1.1")
|
||||
f.git("switch", "-q", "main")
|
||||
},
|
||||
carried: "its 2 commits picked",
|
||||
},
|
||||
{
|
||||
name: "a pick whose -x line names the merge commit",
|
||||
land: func(f *fixture, mc string, _ []string) {
|
||||
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc+")", map[string]string{"app/a.txt": "a\n"})
|
||||
},
|
||||
carried: "picked in",
|
||||
},
|
||||
{
|
||||
name: "picks whose -x lines name every commit of its own",
|
||||
land: func(f *fixture, _ string, own []string) {
|
||||
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
|
||||
f.onLine("release-1.1", "b\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/b.txt": "b, half\n"})
|
||||
},
|
||||
carried: "its 2 commits picked",
|
||||
},
|
||||
{
|
||||
name: "a pick of one of its two commits",
|
||||
land: func(f *fixture, _ string, own []string) {
|
||||
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "the trailer alone, from before the batch-only day",
|
||||
land: func(f *fixture, mc string, _ []string) {
|
||||
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
|
||||
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
|
||||
},
|
||||
carried: "named by a " + batchTrailer + " trailer",
|
||||
},
|
||||
{
|
||||
name: "the trailer alone, after the batch-only day",
|
||||
land: func(f *fixture, mc string, _ []string) {
|
||||
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T00:00:00Z")
|
||||
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "the trailer in the body, from before the batch-only day",
|
||||
land: func(f *fixture, mc string, _ []string) {
|
||||
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
|
||||
f.onLine("release-1.1", "port\n\n"+batchTrailer+": "+mc+"\n\nMore to say.", map[string]string{"app/a.txt": "a\n"})
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
mc := f.prCommits(5, "fix(app): five", "",
|
||||
change{files: map[string]string{"app/a.txt": "a\n"}},
|
||||
change{files: map[string]string{"app/b.txt": "b\n"}})
|
||||
own := strings.Fields(f.git("rev-list", "--reverse", mc+"^1.."+mc+"^2"))
|
||||
tc.land(f, mc, own)
|
||||
v := verdictOn(t, f, nil, "release-1.1", 5)
|
||||
switch {
|
||||
case tc.carried == "" && v.rule != 0:
|
||||
t.Errorf("verdict = rule %d (%s), want it to qualify", v.rule, v.reason)
|
||||
case tc.carried != "" && (v.rule != ruleOnLine || !strings.HasPrefix(v.reason, tc.carried)):
|
||||
t.Errorf("verdict = rule %d (%s), want rule 1 with %q", v.rule, v.reason, tc.carried)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// What a pull request's author writes reaches the line with every commit the
|
||||
// batch picks and with the merge message it carries. None of it makes another
|
||||
// original count as carried: not an -x line, a Backported-from trailer or a
|
||||
// Backport of naming it, not at the end of the message where the line git
|
||||
// appends lands in the same trailer block, and not an -x line naming one of
|
||||
// the other original's own commits. The line is read as after the batch-only
|
||||
// day, as a batch made after it leaves it.
|
||||
func TestPresenceCannotBeClaimedByText(t *testing.T) {
|
||||
type world struct {
|
||||
f *fixture
|
||||
mc5, c5 string
|
||||
}
|
||||
claims := []struct {
|
||||
name string
|
||||
claim func(w *world) string
|
||||
}{
|
||||
{"an -x line naming the merge commit", func(w *world) string { return "(cherry picked from commit " + w.mc5 + ")" }},
|
||||
{"an -x line naming its own commit", func(w *world) string { return "(cherry picked from commit " + w.c5 + ")" }},
|
||||
{"a Backported-from trailer", func(w *world) string { return batchTrailer + ": " + w.mc5 }},
|
||||
{"a Backport of", func(w *world) string { return "Backport of #5" }},
|
||||
{"all of them after a sign-off", func(w *world) string {
|
||||
return "Signed-off" + "-by: " + contributor + "\n(cherry picked from commit " + w.mc5 + ")\n(cherry picked from commit " + w.c5 + ")\n" + batchTrailer + ": " + w.mc5
|
||||
}},
|
||||
}
|
||||
for _, where := range []string{"commit message", "pull request body"} {
|
||||
for _, tc := range claims {
|
||||
t.Run(where+": "+tc.name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
// #5 conflicts on the line, so the batch defers it.
|
||||
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
|
||||
w := &world{f: f}
|
||||
w.mc5 = f.pr(5, "fix(chart): five", "", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"})
|
||||
w.c5 = f.git("rev-parse", w.mc5+"^2")
|
||||
claim := tc.claim(w)
|
||||
msg, body := "fix(app): six\n\n"+claim, ""
|
||||
if where == "pull request body" {
|
||||
msg, body = "fix(app): six", "Six.\n\n"+claim
|
||||
}
|
||||
f.prCommits(6, "fix(app): six", body, change{files: map[string]string{"app/six.txt": "6\n"}, message: msg})
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
|
||||
|
||||
// The batch picks #6; on the open batch pull request the text
|
||||
// claims nothing for #5.
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.bodies[0])}
|
||||
if v := verdictOn(t, f, gh, "release-1.1", 6); v.onBatch == "" {
|
||||
t.Fatal("#6 is not on the batch")
|
||||
}
|
||||
if v := verdictOn(t, f, gh, "release-1.1", 5); v.onBatch != "" {
|
||||
t.Errorf("#5 is on the batch in %.12s, want nowhere", v.onBatch)
|
||||
}
|
||||
|
||||
// Nor on the line, once the line takes the batch.
|
||||
f.git("push", "-q", "origin", f.remoteHead(batchBranch1)+":refs/heads/release-1.1")
|
||||
if v := verdictOn(t, f, nil, "release-1.1", 6); v.rule != ruleOnLine {
|
||||
t.Fatalf("#6 = rule %d (%s), want it on the line", v.rule, v.reason)
|
||||
}
|
||||
if v := verdictOn(t, f, nil, "release-1.1", 5); v.rule != 0 {
|
||||
t.Errorf("#5 = rule %d (%s), want it still to qualify", v.rule, v.reason)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify passes a batch the run built and fails one with an original that is
|
||||
// not in the batch's shape, reading nothing from GitHub when given the head.
|
||||
func TestVerify(t *testing.T) {
|
||||
type world struct {
|
||||
f *fixture
|
||||
gh *fakeGH
|
||||
head string
|
||||
mc5 string
|
||||
sq6 string
|
||||
}
|
||||
setup := func(t *testing.T) *world {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
w := &world{f: f}
|
||||
w.mc5 = f.prCommits(5, "fix(app): five", "",
|
||||
change{files: map[string]string{"app/five.txt": "5\n"}},
|
||||
change{files: map[string]string{"app/five.txt": "5, done\n"}})
|
||||
w.sq6 = f.squash(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
w.gh = &fakeGH{}
|
||||
w.head = openBatch(t, f, w.gh)
|
||||
return w
|
||||
}
|
||||
// on adds a commit with message and files on top of the batch head.
|
||||
on := func(w *world, message string, files map[string]string) string {
|
||||
w.f.git("switch", "-q", "--detach", w.head)
|
||||
w.f.apply(files)
|
||||
w.f.git("commit", "-q", "--allow-empty", "-m", message)
|
||||
sha := w.f.git("rev-parse", "HEAD")
|
||||
w.f.git("switch", "-q", "main")
|
||||
return sha
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
head func(w *world) string
|
||||
refusal string
|
||||
}{
|
||||
{name: "the batch as the run built it", head: func(w *world) string { return w.head }},
|
||||
{
|
||||
name: "an original picked as one commit with -m 1",
|
||||
head: func(w *world) string {
|
||||
return on(w, "fix(app): five (#5)\n\n(cherry picked from commit "+w.mc5+")", map[string]string{"app/five.txt": "5, again\n"})
|
||||
},
|
||||
refusal: "picks the merge commit of #5 as one commit",
|
||||
},
|
||||
{
|
||||
name: "a commit that is no original",
|
||||
head: func(w *world) string {
|
||||
return on(w, "fix(ci): adjust for the line", map[string]string{"ci.txt": "x\n"})
|
||||
},
|
||||
refusal: "ends in no (cherry picked from commit",
|
||||
},
|
||||
{
|
||||
name: "an original carried twice",
|
||||
head: func(w *world) string {
|
||||
own := strings.Fields(w.f.git("rev-list", "--reverse", w.mc5+"^1.."+w.mc5+"^2"))
|
||||
p := on(w, "again\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/five.txt": "5, done again\n"})
|
||||
w.f.git("switch", "-q", "--detach", w.head)
|
||||
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): five (#5)\n\n"+batchTrailer+": "+w.mc5, p)
|
||||
sha := w.f.git("rev-parse", "HEAD")
|
||||
w.f.git("switch", "-q", "main")
|
||||
return sha
|
||||
},
|
||||
refusal: "carries #5 again",
|
||||
},
|
||||
{
|
||||
name: "a squash-merged original carried as a merge",
|
||||
head: func(w *world) string {
|
||||
p := on(w, "six\n\n(cherry picked from commit "+w.sq6+")", map[string]string{"app/six.txt": "6, again\n"})
|
||||
w.f.git("switch", "-q", "--detach", w.head)
|
||||
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): six (#6)\n\n"+batchTrailer+": "+w.sq6, p)
|
||||
sha := w.f.git("rev-parse", "HEAD")
|
||||
w.f.git("switch", "-q", "main")
|
||||
return sha
|
||||
},
|
||||
refusal: "which main took as one commit",
|
||||
},
|
||||
{
|
||||
name: "a merge of the line into the batch",
|
||||
head: func(w *world) string {
|
||||
line := w.f.onLine("release-1.1", "fix(chart): on the line", map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"})
|
||||
w.f.git("switch", "-q", "--detach", w.head)
|
||||
w.f.git("merge", "-q", "--no-ff", "-m", "Merge branch 'release-1.1' into the batch", line)
|
||||
sha := w.f.git("rev-parse", "HEAD")
|
||||
w.f.git("switch", "-q", "main")
|
||||
return sha
|
||||
},
|
||||
refusal: "with 0 " + batchTrailer + " trailers",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
w := setup(t)
|
||||
head := tc.head(w)
|
||||
// GitHub is not asked: a failing fake would fail the run.
|
||||
gh := &fakeGH{err: errString("gh: not to be asked")}
|
||||
code, stdout, stderr := w.f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
|
||||
switch {
|
||||
case tc.refusal == "" && code != 0:
|
||||
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
|
||||
case tc.refusal != "" && (code != 1 || !strings.Contains(stdout, tc.refusal)):
|
||||
t.Errorf("verify = %d, want 1 with %q:\n%s\n%s", code, tc.refusal, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("the open batch pull request", func(t *testing.T) {
|
||||
w := setup(t)
|
||||
code, stdout, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, "3 units at "+w.head[:12]) {
|
||||
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, _ := w.f.exec(&fakeGH{}, "verify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "nothing to verify") {
|
||||
t.Errorf("verify with no batch open = %d, %q", code, stdout)
|
||||
}
|
||||
})
|
||||
t.Run("a head that is not here", func(t *testing.T) {
|
||||
w := setup(t)
|
||||
if code, _, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1", "--head", strings.Repeat("ab", 20)); code != 2 || !strings.Contains(stderr, "fetch it first") {
|
||||
t.Errorf("verify = %d (%s), want 2", code, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A pick that lands among lines the line changed is the same change, and the
|
||||
// body does not call it changed; one whose change differs is called so.
|
||||
func TestSummaryCallsOnlyChangedPicksChanged(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.release("release-1.0")
|
||||
f.squash(2, "chore(app): a file to change", "", map[string]string{"app/ctx.txt": "a\nb\nc\nd\ne\n"})
|
||||
f.pr(1, "feat(app): something for 1.1", "", map[string]string{"app/feature.txt": "1.1\n"})
|
||||
f.release("release-1.1")
|
||||
f.onLine("release-1.1", "fix(app): a line-only change", map[string]string{"app/ctx.txt": "A\nb\nc\nd\ne\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/ctx.txt": "a\nb\nC\nd\ne\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "`fix(app): five`, 1 commit\n") {
|
||||
t.Errorf("a pick git only fit to its place is called changed:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// verify checks every original a batch carries, from the line's head to the
|
||||
// batch head, against the shape the batch carries it in: what fold checks for
|
||||
// one resolution, for all of them and for the clean picks too. fold runs only
|
||||
// where someone resolved a conflict and chose to run it; CI runs verify on
|
||||
// every push to a batch pull request.
|
||||
//
|
||||
// With --head it reads that commit and nothing from GitHub, which is how CI
|
||||
// runs it: the tool built from main, the batch head fetched as data.
|
||||
func (a *app) verify(args []string) int {
|
||||
fs := a.flags("verify")
|
||||
lineArg := fs.String("line", "", "release line, release-X.Y")
|
||||
headArg := fs.String("head", "", "the batch head to verify (default: the head of the open batch PR)")
|
||||
if err := parse(fs, args); err != nil {
|
||||
return 2
|
||||
}
|
||||
l, err := parseLine(*lineArg)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if *headArg != "" {
|
||||
if err := checkSHA(*headArg); err != nil {
|
||||
return a.fail(fmt.Errorf("--head: %w", err))
|
||||
}
|
||||
}
|
||||
if err := fetch(); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
b, err := shapeOf(l)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
head := *headArg
|
||||
if head == "" {
|
||||
prs, err := a.gh.openPulls(l.branch())
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
pr, warnings, err := batchPull(a.gh, prs, l)
|
||||
for _, w := range warnings {
|
||||
a.warn("%s", w)
|
||||
}
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if pr == nil {
|
||||
fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
|
||||
return 0
|
||||
}
|
||||
if head, err = batchHead(pr); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
} else if have, err := lookup(head); err != nil {
|
||||
return a.fail(err)
|
||||
} else if have == "" {
|
||||
return a.fail(fmt.Errorf("--head %.12s is not in this repository; fetch it first", head))
|
||||
}
|
||||
units, err := batchUnits(head, b.lineHead)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
seen := map[*verdict]string{}
|
||||
var refused []string
|
||||
for _, u := range units {
|
||||
why, v, err := checkUnit(b, u)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why == "" && seen[v] != "" {
|
||||
why = fmt.Sprintf("carries #%d again, which %.12s already carries", v.o.number, seen[v])
|
||||
}
|
||||
if why == "" {
|
||||
omitted, _, err := b.omittedEffects(v, u.parents[0], u.commit)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if len(omitted) > 0 {
|
||||
why = leftOut(v, l, omitted)
|
||||
}
|
||||
}
|
||||
if why != "" {
|
||||
refused = append(refused, why)
|
||||
fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
|
||||
continue
|
||||
}
|
||||
seen[v] = u.commit
|
||||
n := len(u.picks)
|
||||
if !u.merge() {
|
||||
n = 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
|
||||
}
|
||||
if len(refused) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
|
||||
return 0
|
||||
}
|
||||
|
||||
// shapeOf reads what verify needs of a line, from git alone: its head, and
|
||||
// every pull request merged into main since it branched, unjudged.
|
||||
func shapeOf(l line) (*batch, error) {
|
||||
b := &batch{line: l}
|
||||
var err error
|
||||
if b.lineHead, err = resolve("refs/remotes/" + remote + "/" + l.branch()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if b.mainHead, err = resolve("refs/remotes/" + remote + "/main"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := git("merge-base", "--end-of-options", b.mainHead, b.lineHead)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s shares no history with main: %w", l.branch(), err)
|
||||
}
|
||||
b.base = strings.TrimSpace(out)
|
||||
merges, other, err := mainMerges(b.base, b.mainHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.other = other
|
||||
for _, o := range merges {
|
||||
b.verdicts = append(b.verdicts, &verdict{o: o})
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// chainWorld has #5 conflict on the line and change a shared line, and #7
|
||||
// change that line again: #7 builds on #5.
|
||||
func chainWorld(t *testing.T) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: five and shared", "", map[string]string{"app/five.txt": "old\n", "app/shared.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// An original that builds on a deferred conflict waits for it: check warns,
|
||||
// notify lists it after the conflict, and once the conflict lands, the next
|
||||
// run picks it.
|
||||
func TestWaitForADeferredDependency(t *testing.T) {
|
||||
f, mc5 := chainWorld(t)
|
||||
gh := &fakeGH{}
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) || !strings.Contains(stdout, "### Waiting on another original") {
|
||||
t.Errorf("#7 does not wait on #5:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "warning: #7 waits on #5, a deferred conflict on #90: https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check = %d, want a warning that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1", "--push")
|
||||
if code != 0 || !strings.Contains(gh.issueComments[len(gh.issueComments)-1].Body, "- [#7](https://github.com/cozystack/cozystack/pull/7), after [#5](https://github.com/cozystack/cozystack/pull/5)") {
|
||||
t.Errorf("notify = %d, want #7 listed after #5:\n%s\n%v", code, stdout, gh.issueComments)
|
||||
}
|
||||
// The batch merges, and #5 lands on the line by hand, resolved: the
|
||||
// next batch picks #7.
|
||||
mergeBatch(f, gh)
|
||||
f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
|
||||
t.Errorf("run --continue after #5 landed = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Of several prerequisites still to come, the strongest decides: one not on
|
||||
// the line at all, then one waiting for a maintainer, then a conflict or a
|
||||
// deferral, then one rule 6 holds; a chain inherits what its end decides.
|
||||
func TestWaitReduction(t *testing.T) {
|
||||
t.Run("a conflict and a workflow wait", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(ci): six", "", map[string]string{".github/workflows/six.yaml": "on: push\n", "app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[6]}`) {
|
||||
t.Fatalf("run = %d, want #7 waiting on #6 alone:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 waits on #6, which changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
|
||||
t.Errorf("check --cut = %d, want #7 to hold the cut:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "2 originals on #90 change .github/workflows/ or wait on one that does") {
|
||||
t.Errorf("notify = %d, want #7 among the waits:\n%s", code, stdout)
|
||||
}
|
||||
})
|
||||
t.Run("a conflict and one not on the line", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(10, "fix(app): ten", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/10 fix(app): ten (it waits on #9, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #10 to fail it, waiting on #9:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a prerequisite on the open batch", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"picked":[5,7]`) {
|
||||
t.Fatalf("run = %d, want #5 and #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") || strings.Contains(stdout+stderr, "waits on") {
|
||||
t.Errorf("check --cut = %d, want #5 and #7 only on the batch and no wait:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a direct reason", func(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "needs the tap", "--push"); code != 0 {
|
||||
t.Fatalf("run --defer = %d\n%s", code, stderr)
|
||||
}
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check = %d, want #5 deferred for its reason, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a prerequisite rule 6 holds", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
|
||||
f.onLine("release-1.1", "fix(app): seven (#7)\n\n(cherry picked from commit "+mc7+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.0"); code != 0 || !strings.Contains(stderr, "note: #7 waits on #5, which is not on release-1.1 yet") {
|
||||
t.Errorf("check of release-1.0 = %d, want a note that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a chain", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): four", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/7 fix(app): seven (it waits on #5, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #7 failing it through #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// run's report, check and notify give an original one disposition: a
|
||||
// workflow original whose only missing dependency is a test waits for a
|
||||
// maintainer, and one deferred for a reason stays deferred, though it
|
||||
// builds on what never comes; neither is parked.
|
||||
func TestDispositionOrderIsSharedByRunCheckNotify(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(app): fix the tap", "", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "waits for the tap", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --defer = %d\n%s", code, stderr)
|
||||
}
|
||||
if strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) || !strings.Contains(stdout, `{"original":7,"files":[],"reason":"waits for the tap"}`) {
|
||||
t.Errorf("the report does not have #5 waiting and #7 deferred, unparked:\n%s", stdout)
|
||||
}
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || strings.Contains(stderr, "parked") || !strings.Contains(stderr, "warning: waiting on #90 for a maintainer, since it changes .github/workflows/: https://github.com/cozystack/cozystack/pull/5 ") || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check = %d, want #5 waiting and #7 deferred:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, "1 originals on #90 change .github/workflows/") || strings.Contains(stdout, "parked") || strings.Contains(stdout, "conflicts") {
|
||||
t.Errorf("notify = %d, want #5 waiting and nothing for #7:\n%s", code, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A never and a pending prerequisite together park.
|
||||
func TestParkWinsOverWait(t *testing.T) {
|
||||
f, _ := chainWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!!", 1), "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[20]`) || strings.Contains(stdout, `"original":8,"files":[],"needs"`) {
|
||||
t.Errorf("run = %d, want #8 parked on #20, not waiting on #7:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite picked earlier in the same batch satisfies what builds on
|
||||
// it, and so does one whose pick came out empty because an earlier pick
|
||||
// already carried it: #4280 and #3938.
|
||||
func TestDependencySatisfiedEarlierInTheBatch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "fix(kafka): delete hooks", "", map[string]string{"app/hooks.yaml": appFile})
|
||||
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[3,4]`) {
|
||||
t.Errorf("run = %d, want #3 and #4 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
|
||||
// #30 adds back the file #2 brought and a feat took away, so its pick
|
||||
// comes out empty after #2's, and #4 builds on #30.
|
||||
f = newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(20, "fix(kafka): delete hooks, and two", "", map[string]string{"app/hooks.yaml": appFile, "app/two.txt": "2\n"})
|
||||
f.pr(21, "feat(kafka): no delete hooks", "", map[string]string{"app/hooks.yaml": ""})
|
||||
f.pr(30, "fix(kafka): delete hooks again", "", map[string]string{"app/hooks.yaml": appFile})
|
||||
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[4,20]`) || strings.Contains(stdout, `"needs"`) {
|
||||
t.Errorf("run with #30's pick empty = %d, want #4 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// workflowWorld has #5 change a workflow and #6 change the app and a file
|
||||
// under .github/ that is no workflow.
|
||||
func workflowWorld(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(ci): five", "", map[string]string{".github/workflows/five.yaml": "on: push\n", "app/five.txt": "5\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n", ".github/renovate.json": "{}\n"})
|
||||
return f
|
||||
}
|
||||
|
||||
// With --no-workflow-files, as the scheduled batch runs, an original that
|
||||
// changes a workflow is left to a maintainer, with its workflow files, and is
|
||||
// neither a conflict nor deferred with a reason. Without the flag it is picked
|
||||
// like any other.
|
||||
func TestRunLeavesWorkflowChangesToAMaintainer(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
f := workflowWorld(t)
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --no-workflow-files = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("picked %q, want only #6", got)
|
||||
}
|
||||
deferred, err := parseDeferred(stdout, l)
|
||||
if e := deferred[5]; err != nil || len(deferred) != 1 || !e.Workflows || e.Reason != "" || e.Commit != "" || !slices.Equal(e.Files, []string{".github/workflows/five.yaml"}) {
|
||||
t.Errorf("deferred = %+v, %v; want #5 waiting with its workflow file", deferred, err)
|
||||
}
|
||||
for _, want := range []string{"### Waiting for a maintainer", "[#5](", "`.github/workflows/five.yaml`", "#changes-to-workflows"} {
|
||||
if !strings.Contains(stdout, want) {
|
||||
t.Errorf("the body lacks %q:\n%s", want, stdout)
|
||||
}
|
||||
}
|
||||
if strings.Contains(stdout, "### Deferred") {
|
||||
t.Errorf("the body lists #5 as deferred:\n%s", stdout)
|
||||
}
|
||||
if !strings.Contains(stderr, "waiting for a maintainer: https://github.com/cozystack/cozystack/pull/5 fix(ci): five changes .github/workflows/five.yaml") {
|
||||
t.Errorf("the run does not say #5 waits:\n%s", stderr)
|
||||
}
|
||||
|
||||
f = workflowWorld(t)
|
||||
code, stdout, stderr = f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(ci): five (#5)", "fix(app): six (#6)"}) {
|
||||
t.Errorf("without the flag picked %q, want #5 and #6", got)
|
||||
}
|
||||
if strings.Contains(stdout, "Waiting for a maintainer") {
|
||||
t.Errorf("without the flag the body has something wait:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// Waiting is no way off the line: check lists the original until it is on
|
||||
// it, the scheduled run that continues the batch keeps it waiting rather than
|
||||
// trying it as a conflict, notify lists it apart from the conflicts, and a
|
||||
// maintainer's own run picks it onto the open batch.
|
||||
func TestWorkflowChangesWaitUntilOnTheLine(t *testing.T) {
|
||||
f := workflowWorld(t)
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check with no batch open = %d, %q; want #5 missing", code, stdout)
|
||||
}
|
||||
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "warning: waiting on #90 for a maintainer, since it changes .github/workflows/: https://github.com/cozystack/cozystack/pull/5 ") {
|
||||
t.Errorf("check = %d, %q; want 0 and a warning about #5:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
|
||||
t.Errorf("check --cut = %d, %q; want #5 to stop the cut", code, stdout)
|
||||
}
|
||||
|
||||
code, stdout, stderr = f.exec(gh, "notify", "--line", "release-1.1", "--push")
|
||||
if code != 0 || !strings.Contains(stdout, "1 originals on #90 change .github/workflows/ and wait for a maintainer") || strings.Contains(stdout, "deferred conflicts") {
|
||||
t.Errorf("notify = %d, %q; want #5 for a maintainer and no conflict\n%s", code, stdout, stderr)
|
||||
}
|
||||
notes := slices.DeleteFunc(slices.Clone(gh.issueComments), func(c issueComment) bool { return !strings.HasPrefix(c.Body, notifyMarker) })
|
||||
if len(notes) != 1 || !strings.Contains(notes[0].Body, "[#5](") || !strings.Contains(notes[0].Body, "#changes-to-workflows") || strings.Contains(notes[0].Body, "These conflicts") {
|
||||
t.Errorf("the comment does not list #5 for a maintainer: %+v", gh.issueComments)
|
||||
}
|
||||
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --continue = %d\n%s", code, stderr)
|
||||
}
|
||||
l, _ := parseLine("release-1.1")
|
||||
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || len(deferred) != 1 {
|
||||
t.Errorf("the continued body has %+v, %v; want #5 still waiting", deferred, err)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): six (#6)", "fix(app): seven (#7)"}) {
|
||||
t.Errorf("the continued batch carries %q", got)
|
||||
}
|
||||
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 {
|
||||
t.Fatalf("a maintainer's run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", f.remoteHead(batchBranch1)); !slices.Equal(got, []string{"fix(app): six (#6)", "fix(app): seven (#7)", "fix(ci): five (#5)"}) {
|
||||
t.Errorf("after a maintainer's run the batch carries %q, want #5 on top", got)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || strings.Contains(stderr, "waiting") {
|
||||
t.Errorf("check after the maintainer's run = %d, %q\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// --defer keeps what waits as it is, and takes one out like any other original
|
||||
// when a maintainer gives the reason.
|
||||
func TestDeferKeepsWorkflowChangesWaiting(t *testing.T) {
|
||||
f := workflowWorld(t)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
l, _ := parseLine("release-1.1")
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "fails the app e2e", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --defer 7 = %d\n%s", code, stderr)
|
||||
}
|
||||
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || deferred[7].Reason != "fails the app e2e" || len(deferred) != 2 {
|
||||
t.Errorf("after --defer 7 the body has %+v, %v; want #5 waiting and #7 deferred", deferred, err)
|
||||
}
|
||||
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "waits for the 1.1.4 workflow rework", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --defer 5 = %d\n%s", code, stderr)
|
||||
}
|
||||
if deferred, err := parseDeferred(stdout, l); err != nil || deferred[5].Workflows || deferred[5].Reason == "" {
|
||||
t.Errorf("after --defer 5 the body has %+v, %v; want #5 deferred with the reason", deferred, err)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
// #6 and #7 are still only on the batch, which fails the cut; #5 does not.
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || strings.Contains(stdout, "#5 changes") || !strings.Contains(stdout, "#6 is only on open batch PR #90") {
|
||||
t.Errorf("check --cut = %d, %q; want 1 for #6 and #7 alone, a deferred #5 no longer waiting", code, stdout)
|
||||
}
|
||||
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") {
|
||||
t.Errorf("check = %d, want 0 with a warning about #5:\n%s", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A record that has an original wait and also names a failed commit or a
|
||||
// reason is refused: it is one or the other.
|
||||
func TestParseDeferredRefusesAMixedEntry(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
for _, entry := range []string{
|
||||
`{"original":5,"files":[],"workflows":true,"reason":"x"}`,
|
||||
`{"original":5,"files":[],"workflows":true,"commit":"` + strings.Repeat("a", 40) + `"}`,
|
||||
} {
|
||||
body := deferredFence + "\n" + `{"line":"release-1.1","deferred":[` + entry + `]}` + "\n```\n"
|
||||
if _, err := parseDeferred(body, l); err == nil {
|
||||
t.Errorf("parseDeferred accepted %s", entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A pull request that changes a workflow in one commit and takes it back in a
|
||||
// later one changes no workflow in its merge, but its first pick does, and
|
||||
// the push carries that pick: it waits as well.
|
||||
func TestRunHoldsAWorkflowChangeTakenBack(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.prCommits(5, "fix(ci): five", "",
|
||||
change{files: map[string]string{".github/workflows/five.yaml": "on: push\n"}},
|
||||
change{files: map[string]string{".github/workflows/five.yaml": "", "app/five.txt": "5\n"}})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("picked %q, want only #6", got)
|
||||
}
|
||||
l, _ := parseLine("release-1.1")
|
||||
if deferred, err := parseDeferred(stdout, l); err != nil || !deferred[5].Workflows || !slices.Equal(deferred[5].Files, []string{".github/workflows/five.yaml"}) {
|
||||
t.Errorf("deferred = %+v, %v; want #5 waiting on its workflow file", deferred, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A rebuild keeps the count of workflow files a waiting original changes
|
||||
// beyond those the block names.
|
||||
func TestDeferKeepsTheWaitingFileCount(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
files := map[string]string{"app/five.txt": "5\n"}
|
||||
for i := range filesKept + 2 {
|
||||
files[fmt.Sprintf(".github/workflows/w%02d.yaml", i)] = "on: push\n"
|
||||
}
|
||||
f.pr(5, "fix(ci): five", "", files)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "fails the app e2e", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --defer = %d\n%s", code, stderr)
|
||||
}
|
||||
l, _ := parseLine("release-1.1")
|
||||
deferred, err := parseDeferred(stdout, l)
|
||||
if e := deferred[5]; err != nil || !e.Workflows || len(e.Files) != filesKept || e.More != 2 {
|
||||
t.Errorf("after the rebuild #5 is %d files and %d more, %v; want %d and 2", len(e.Files), e.More, err, filesKept)
|
||||
}
|
||||
if want := fmt.Sprintf("%d more", filesKept+2-filesListed); !strings.Contains(stdout, want) {
|
||||
t.Errorf("the rebuilt body does not say %q:\n%s", want, stdout)
|
||||
}
|
||||
}
|
||||
@@ -243,6 +243,7 @@ func main() {
|
||||
Namespace: os.Getenv("BACKUP_CREDENTIALS_HELMRELEASE_NAMESPACE"),
|
||||
Name: os.Getenv("BACKUP_CREDENTIALS_HELMRELEASE_NAME"),
|
||||
},
|
||||
CredentialsCOSISecretName: os.Getenv("BACKUP_CREDENTIALS_COSI_SECRET_NAME"),
|
||||
}
|
||||
if err := gate.SetupWithManager(mgr); err != nil {
|
||||
setupLog.Error(err, "unable to add DefaultObjectsGate runnable")
|
||||
|
||||
+167
-33
@@ -55,6 +55,8 @@ var tapCmdFlags struct {
|
||||
kubeconfig string
|
||||
index string
|
||||
skipValidate bool
|
||||
certIdentity string
|
||||
certIssuer string
|
||||
}
|
||||
|
||||
// ociRef is the parsed form of an oci:// reference.
|
||||
@@ -65,10 +67,28 @@ type ociRef struct {
|
||||
// Repo is the last path segment.
|
||||
Repo string
|
||||
Tag string
|
||||
// Digest, when set, pins the exact content (sha256:<64 hex>). It takes
|
||||
// precedence over Tag at pull time (Flux resolves digest before tag), so a
|
||||
// digest-pinned reference never follows a moved tag.
|
||||
Digest string
|
||||
}
|
||||
|
||||
// pullRef is the reference to pull: by digest when one is pinned (so the pull
|
||||
// resolves the exact content regardless of where the tag points), otherwise by
|
||||
// tag.
|
||||
func (r ociRef) pullRef() string {
|
||||
if r.Digest != "" {
|
||||
return r.URL + "@" + r.Digest
|
||||
}
|
||||
return r.URL + ":" + r.Tag
|
||||
}
|
||||
|
||||
var sanitizeName = regexp.MustCompile(`[^a-z0-9-]+`)
|
||||
|
||||
// ociDigestRe matches a full OCI manifest digest (sha256:<64 hex>), the same
|
||||
// shape the platform source's "digest=" reference enforces.
|
||||
var ociDigestRe = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
|
||||
|
||||
// parseOCIRef splits an oci:// reference into its URL (without tag), org, repo,
|
||||
// and tag. A missing tag defaults to "latest".
|
||||
func parseOCIRef(ref string) (ociRef, error) {
|
||||
@@ -77,11 +97,20 @@ func parseOCIRef(ref string) (ociRef, error) {
|
||||
}
|
||||
body := strings.TrimPrefix(ref, "oci://")
|
||||
|
||||
tag := ""
|
||||
// Digest pins are not tag-addressable; tap is tag-based for now.
|
||||
if strings.Contains(body, "@") {
|
||||
return ociRef{}, fmt.Errorf("digest references are not supported by tap yet; use a tag (oci://host/org/repo:tag)")
|
||||
// A digest pins the exact content (oci://host/org/repo@sha256:...). An
|
||||
// optional tag may precede it and is then informational only. The digest
|
||||
// must be a full sha256:<64 hex>, the same contract the platform source's
|
||||
// "digest=" reference enforces.
|
||||
digest := ""
|
||||
if at := strings.LastIndex(body, "@"); at >= 0 {
|
||||
digest = body[at+1:]
|
||||
body = body[:at]
|
||||
if !ociDigestRe.MatchString(digest) {
|
||||
return ociRef{}, fmt.Errorf("invalid digest %q in reference %q: want sha256:<64 hex>", digest, ref)
|
||||
}
|
||||
}
|
||||
|
||||
tag := ""
|
||||
if colon := strings.LastIndex(body, ":"); colon >= 0 && !strings.Contains(body[colon:], "/") {
|
||||
tag = body[colon+1:]
|
||||
body = body[:colon]
|
||||
@@ -96,10 +125,11 @@ func parseOCIRef(ref string) (ociRef, error) {
|
||||
if len(segs) >= 3 {
|
||||
org = segs[len(segs)-2]
|
||||
}
|
||||
if tag == "" {
|
||||
// Default to latest only when neither a tag nor a digest pins the content.
|
||||
if tag == "" && digest == "" {
|
||||
tag = "latest"
|
||||
}
|
||||
return ociRef{URL: "oci://" + body, Org: org, Repo: repo, Tag: tag}, nil
|
||||
return ociRef{URL: "oci://" + body, Org: org, Repo: repo, Tag: tag, Digest: digest}, nil
|
||||
}
|
||||
|
||||
// fluxSourceName is the RFC-1123 name of the OCIRepository tap creates. It
|
||||
@@ -116,9 +146,11 @@ func fluxSourceName(r ociRef) string {
|
||||
}
|
||||
|
||||
// buildTapOCIRepository mirrors the operator's generateOCIRepository: an
|
||||
// OCIRepository in cozy-system, 5m interval, optional tag reference, optional
|
||||
// pull-credential secretRef (the private-tap credential, symmetric with #2472).
|
||||
func buildTapOCIRepository(name string, r ociRef, secret string) *sourcev1.OCIRepository {
|
||||
// OCIRepository in cozy-system, 5m interval, a tag and/or digest reference,
|
||||
// optional pull-credential secretRef (the private-tap credential, symmetric
|
||||
// with #2472). A tag-only reference is pinned to a digest later by the tap
|
||||
// materializer; an explicit digest is pinned here at connect time.
|
||||
func buildTapOCIRepository(name string, r ociRef, secret string, verify *sourcev1.OCIRepositoryVerification) *sourcev1.OCIRepository {
|
||||
obj := &sourcev1.OCIRepository{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: sourcev1.GroupVersion.String(),
|
||||
@@ -134,19 +166,62 @@ func buildTapOCIRepository(name string, r ociRef, secret string) *sourcev1.OCIRe
|
||||
Annotations: map[string]string{tapconst.NameAnnotation: name},
|
||||
},
|
||||
Spec: sourcev1.OCIRepositorySpec{
|
||||
URL: r.URL,
|
||||
Interval: metav1.Duration{Duration: 5 * time.Minute},
|
||||
Reference: &sourcev1.OCIRepositoryRef{
|
||||
Tag: r.Tag,
|
||||
},
|
||||
URL: r.URL,
|
||||
Interval: metav1.Duration{Duration: 5 * time.Minute},
|
||||
Reference: &sourcev1.OCIRepositoryRef{Tag: r.Tag, Digest: r.Digest},
|
||||
},
|
||||
}
|
||||
if secret != "" {
|
||||
obj.Spec.SecretRef = &fluxmeta.LocalObjectReference{Name: secret}
|
||||
}
|
||||
if verify != nil {
|
||||
obj.Spec.Verify = verify
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// carryoverSourceFields preserves an existing tap source's verification policy
|
||||
// and pull credential when this invocation did not set them from flags, so a
|
||||
// re-tap (e.g. to move the pin to a new tag) never silently drops signature
|
||||
// verification or the pull secret. A server-side apply that omits a field this
|
||||
// field owner previously set would otherwise remove it, which the API connect
|
||||
// path (rest.go) deliberately avoids; this keeps the CLI symmetric with it.
|
||||
func carryoverSourceFields(obj, existing *sourcev1.OCIRepository, flagVerify *sourcev1.OCIRepositoryVerification, flagSecret string) {
|
||||
if existing == nil {
|
||||
return
|
||||
}
|
||||
if flagVerify == nil && existing.Spec.Verify != nil {
|
||||
obj.Spec.Verify = existing.Spec.Verify
|
||||
}
|
||||
if flagSecret == "" && existing.Spec.SecretRef != nil {
|
||||
obj.Spec.SecretRef = existing.Spec.SecretRef
|
||||
}
|
||||
}
|
||||
|
||||
// tapVerification builds a keyless cosign verification policy from the connect
|
||||
// flags, or nil when neither is set. Both the certificate identity (subject)
|
||||
// and the OIDC issuer are required together: a policy that verified a signature
|
||||
// without pinning who signed it would be worse than none. Both are Go regular
|
||||
// expressions at the Flux layer, so an exact identity should be anchored.
|
||||
func tapVerification(identity, issuer string) (*sourcev1.OCIRepositoryVerification, error) {
|
||||
if identity == "" && issuer == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if identity == "" || issuer == "" {
|
||||
return nil, fmt.Errorf("keyless signature verification needs both --certificate-identity and --certificate-oidc-issuer")
|
||||
}
|
||||
// Flux treats Issuer and Subject as Go regexps, so anchor and quote them for
|
||||
// an exact match: an unanchored identity would also accept a superstring
|
||||
// (e.g. alice@example.com matching malice@example.com).
|
||||
return &sourcev1.OCIRepositoryVerification{
|
||||
Provider: "cosign",
|
||||
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{
|
||||
Issuer: "^" + regexp.QuoteMeta(issuer) + "$",
|
||||
Subject: "^" + regexp.QuoteMeta(identity) + "$",
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// rewritePackageSourceForTap prepares a PackageSource read from the artifact for
|
||||
// apply: it keeps the repository's own declared name (no community rename),
|
||||
// clears server-set metadata, stamps the tap marker (label + source annotation)
|
||||
@@ -181,21 +256,38 @@ func rewritePackageSourceForTap(ps *cozyv1alpha1.PackageSource, sourceName, sour
|
||||
}
|
||||
}
|
||||
|
||||
// clearMaterializedRevision removes the operator's materialized-revision
|
||||
// annotation from the tap's OCIRepository, so the next reconcile does not
|
||||
// short-circuit on the unchanged artifact revision and re-materializes its
|
||||
// PackageSources (recovering one removed out-of-band). A no-op when the source or
|
||||
// the annotation is absent.
|
||||
func clearMaterializedRevision(ctx context.Context, k8sClient client.Client, srcName string) error {
|
||||
// resetTapPinState prepares a tap's OCIRepository for a re-tap. It clears the
|
||||
// materialized-revision annotation so the next reconcile does not short-circuit
|
||||
// on the unchanged revision and re-materializes its PackageSources (recovering
|
||||
// one removed out-of-band), and the pinned-digest annotation so the operator
|
||||
// re-pins the newly requested reference. For a tag-based re-tap (explicitDigest
|
||||
// false) it also drops the operator's prior digest pin from spec.ref: a
|
||||
// server-side apply under field owner cozypkg cannot clear a field the operator
|
||||
// owns, so a moved tag would otherwise stay pinned to the old digest. An
|
||||
// explicit-digest re-tap keeps the digest the apply itself set. A no-op (and no
|
||||
// error) when the source is absent or there is nothing to clear.
|
||||
func resetTapPinState(ctx context.Context, k8sClient client.Client, srcName string, explicitDigest bool) error {
|
||||
reg := &sourcev1.OCIRepository{}
|
||||
if err := k8sClient.Get(ctx, client.ObjectKey{Name: srcName, Namespace: cozySystemNamespace}, reg); err != nil {
|
||||
return client.IgnoreNotFound(err)
|
||||
}
|
||||
if _, ok := reg.Annotations[tapconst.MaterializedRevisionAnnotation]; !ok {
|
||||
base := reg.DeepCopy()
|
||||
changed := false
|
||||
if _, ok := reg.Annotations[tapconst.MaterializedRevisionAnnotation]; ok {
|
||||
delete(reg.Annotations, tapconst.MaterializedRevisionAnnotation)
|
||||
changed = true
|
||||
}
|
||||
if _, ok := reg.Annotations[tapconst.PinnedDigestAnnotation]; ok {
|
||||
delete(reg.Annotations, tapconst.PinnedDigestAnnotation)
|
||||
changed = true
|
||||
}
|
||||
if !explicitDigest && reg.Spec.Reference != nil && reg.Spec.Reference.Digest != "" {
|
||||
reg.Spec.Reference.Digest = ""
|
||||
changed = true
|
||||
}
|
||||
if !changed {
|
||||
return nil
|
||||
}
|
||||
base := reg.DeepCopy()
|
||||
delete(reg.Annotations, tapconst.MaterializedRevisionAnnotation)
|
||||
return k8sClient.Patch(ctx, reg, client.MergeFrom(base))
|
||||
}
|
||||
|
||||
@@ -238,11 +330,18 @@ With --secret the OCIRepository is given a pull-credential secretRef (the
|
||||
admin pre-creates the Secret in cozy-system), so a private repository taps in
|
||||
one command.
|
||||
|
||||
Trust: tap validates the artifact's structure but does NOT verify its cosign
|
||||
signature. Signature verification is the community index's job at publication
|
||||
time ('cozypkg validate --require-signature' in the index CI gate, pinned to
|
||||
the entry's recorded identity), and can additionally be enforced at pull time
|
||||
via Flux OCIRepository verification. Installing an app from a tapped repository
|
||||
Pinning: a tapped repository does not follow a moving tag. A reference given by
|
||||
digest (oci://host/org/repo@sha256:...) is pinned to that digest immediately; a
|
||||
tag reference is pinned to the digest it currently resolves to as soon as the
|
||||
source materializes, with the tag kept informational. Moving the tag upstream
|
||||
changes nothing in the cluster; tap the repository again to move the pin.
|
||||
|
||||
Trust: tap validates the artifact's structure. Keyless signature verification at
|
||||
pull time is enabled with --certificate-identity and --certificate-oidc-issuer,
|
||||
which set a Flux cosign verify policy on the source so content is pulled only
|
||||
when signed by that identity. Signature verification at publication time is the
|
||||
community index's job ('cozypkg validate --require-signature' in the index CI
|
||||
gate, pinned to the entry's recorded identity). Installing an app from a tapped repository
|
||||
runs its charts in your management cluster: tap only sources you trust.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -256,12 +355,41 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
return err
|
||||
}
|
||||
if tapCmdFlags.tag != "" {
|
||||
if ref.Digest != "" {
|
||||
return fmt.Errorf("--tag cannot override the digest in %q; drop the digest or the --tag flag", args[0])
|
||||
}
|
||||
ref.Tag = tapCmdFlags.tag
|
||||
}
|
||||
verify, err := tapVerification(tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// With verification flags set, verify the signature now and pin the verified
|
||||
// digest, so the artifact we pull and the PackageSources we apply are the
|
||||
// exact content a trusted identity signed. Without this the CLI would apply
|
||||
// an unverified tag pull (only the Flux source would carry the policy), so a
|
||||
// moved tag could land unsigned PackageSource content in the cluster before
|
||||
// the operator re-materialized.
|
||||
if verify != nil {
|
||||
verifiedDigest, verr := verifyCosignSignature(ref.pullRef(), tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
|
||||
if verr != nil {
|
||||
return verr
|
||||
}
|
||||
// Fail closed: a verified signature with no parseable digest must not
|
||||
// fall back to an unpinned tag pull, which would reopen the tag-mutation
|
||||
// window between verify and pull.
|
||||
if verifiedDigest == "" {
|
||||
return fmt.Errorf("cosign verified %q but returned no manifest digest; refusing to pull by an unpinned tag", ref.pullRef())
|
||||
}
|
||||
if ref.Digest == "" {
|
||||
ref.Digest = verifiedDigest
|
||||
}
|
||||
}
|
||||
|
||||
// Pull first: an unreachable/invalid ref must fail before any cluster
|
||||
// resource is created (no half-created source).
|
||||
fullRef := ref.URL + ":" + ref.Tag
|
||||
fullRef := ref.pullRef()
|
||||
dir, cleanup, err := pullOCIArtifact(fullRef)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -287,7 +415,7 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
}
|
||||
|
||||
srcName := fluxSourceName(ref)
|
||||
ociRepo := buildTapOCIRepository(srcName, ref, tapCmdFlags.secret)
|
||||
ociRepo := buildTapOCIRepository(srcName, ref, tapCmdFlags.secret, verify)
|
||||
|
||||
toApply := make([]client.Object, 0, len(sources)+1)
|
||||
toApply = append(toApply, ociRepo)
|
||||
@@ -316,6 +444,9 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
if existing.Spec.URL != "" && existing.Spec.URL != ref.URL {
|
||||
return fmt.Errorf("a different repository (%s) is already tapped as %q; run 'cozypkg untap %s' before tapping %s", existing.Spec.URL, srcName, names[0], ref.URL)
|
||||
}
|
||||
// Same repository, re-tapped: carry over a verify policy or pull secret
|
||||
// this invocation did not set, so moving the pin never silently drops them.
|
||||
carryoverSourceFields(ociRepo, existing, verify, tapCmdFlags.secret)
|
||||
}
|
||||
|
||||
// A tapped repository keeps its own declared name. Refuse the whole tap
|
||||
@@ -350,12 +481,13 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
// PackageSource removed out-of-band since the last materialization. Tapping
|
||||
// no longer installs anything, so a re-tap does NOT re-install an app removed
|
||||
// with `cozypkg del`; re-install it with `cozypkg add`. A first tap has no
|
||||
// such annotation, so this is a no-op then.
|
||||
if err := clearMaterializedRevision(ctx, k8sClient, srcName); err != nil {
|
||||
// such annotation, so this is a no-op then. resetTapPinState also drops the
|
||||
// operator's prior digest pin so the newly requested tag re-resolves.
|
||||
if err := resetTapPinState(ctx, k8sClient, srcName, ref.Digest != ""); err != nil {
|
||||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not reset materialization state on OCIRepository/%s: %v\n", srcName, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Tapped %s\n OCIRepository/%s (%s:%s)\n", fullRef, srcName, ref.URL, ref.Tag)
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " PackageSource/%s\n", n)
|
||||
}
|
||||
@@ -509,6 +641,8 @@ func init() {
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.kubeconfig, "kubeconfig", "", "Path to kubeconfig file")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.index, "index", "", "Index location for resolving a short name (local dir or oci:// ref; defaults to COZYPKG_INDEX)")
|
||||
tapCmd.Flags().BoolVar(&tapCmdFlags.skipValidate, "skip-validate", false, "Skip validating the artifact before tapping")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.certIdentity, "certificate-identity", "", "Keyless cosign certificate identity (subject regex) to verify the artifact's signature against at pull time; requires --certificate-oidc-issuer")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.certIssuer, "certificate-oidc-issuer", "", "Keyless cosign OIDC issuer (regex) to verify the artifact's signature against at pull time; requires --certificate-identity")
|
||||
untapCmd.Flags().StringVar(&tapCmdFlags.kubeconfig, "kubeconfig", "", "Path to kubeconfig file")
|
||||
untapCmd.Flags().BoolVar(&untapConfirmFlag, "yes", false, "Untap even if a Package from this source is still installed")
|
||||
}
|
||||
+175
-20
@@ -20,8 +20,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
fluxmeta "github.com/fluxcd/pkg/apis/meta"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -36,18 +38,26 @@ import (
|
||||
)
|
||||
|
||||
func TestParseOCIRef(t *testing.T) {
|
||||
const digest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
|
||||
cases := []struct {
|
||||
in string
|
||||
wantURL, wantOrg, wantRepo, wantTag string
|
||||
wantErr bool
|
||||
in string
|
||||
wantURL, wantOrg, wantRepo, wantTag, wantDigest string
|
||||
wantErr bool
|
||||
}{
|
||||
{"oci://ghcr.io/foo/bar:v1", "oci://ghcr.io/foo/bar", "foo", "bar", "v1", false},
|
||||
{"oci://ghcr.io/foo/bar", "oci://ghcr.io/foo/bar", "foo", "bar", "latest", false},
|
||||
{"oci://ghcr.io/foo/group/bar:1.2.3", "oci://ghcr.io/foo/group/bar", "group", "bar", "1.2.3", false},
|
||||
{"oci://registry.example.com/solo:tag", "oci://registry.example.com/solo", "", "solo", "tag", false},
|
||||
{"https://ghcr.io/foo/bar", "", "", "", "", true},
|
||||
{"oci://ghcr.io/foo/bar@sha256:abc", "", "", "", "", true},
|
||||
{"oci://ghcr.io", "", "", "", "", true},
|
||||
{"oci://ghcr.io/foo/bar:v1", "oci://ghcr.io/foo/bar", "foo", "bar", "v1", "", false},
|
||||
{"oci://ghcr.io/foo/bar", "oci://ghcr.io/foo/bar", "foo", "bar", "latest", "", false},
|
||||
{"oci://ghcr.io/foo/group/bar:1.2.3", "oci://ghcr.io/foo/group/bar", "group", "bar", "1.2.3", "", false},
|
||||
{"oci://registry.example.com/solo:tag", "oci://registry.example.com/solo", "", "solo", "tag", "", false},
|
||||
// A digest pin is accepted; the tag is left empty (not defaulted to
|
||||
// latest) because the digest is the authoritative reference.
|
||||
{"oci://ghcr.io/foo/bar@" + digest, "oci://ghcr.io/foo/bar", "foo", "bar", "", digest, false},
|
||||
// A tag may precede the digest; the tag is then informational only.
|
||||
{"oci://ghcr.io/foo/bar:v1@" + digest, "oci://ghcr.io/foo/bar", "foo", "bar", "v1", digest, false},
|
||||
{"https://ghcr.io/foo/bar", "", "", "", "", "", true},
|
||||
// A malformed digest (not sha256:<64 hex>) is still rejected.
|
||||
{"oci://ghcr.io/foo/bar@sha256:abc", "", "", "", "", "", true},
|
||||
{"oci://ghcr.io/foo/bar@notadigest", "", "", "", "", "", true},
|
||||
{"oci://ghcr.io", "", "", "", "", "", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := parseOCIRef(c.in)
|
||||
@@ -61,8 +71,8 @@ func TestParseOCIRef(t *testing.T) {
|
||||
t.Errorf("parseOCIRef(%q) unexpected error: %v", c.in, err)
|
||||
continue
|
||||
}
|
||||
if got.URL != c.wantURL || got.Org != c.wantOrg || got.Repo != c.wantRepo || got.Tag != c.wantTag {
|
||||
t.Errorf("parseOCIRef(%q) = %+v, want url=%q org=%q repo=%q tag=%q", c.in, got, c.wantURL, c.wantOrg, c.wantRepo, c.wantTag)
|
||||
if got.URL != c.wantURL || got.Org != c.wantOrg || got.Repo != c.wantRepo || got.Tag != c.wantTag || got.Digest != c.wantDigest {
|
||||
t.Errorf("parseOCIRef(%q) = %+v, want url=%q org=%q repo=%q tag=%q digest=%q", c.in, got, c.wantURL, c.wantOrg, c.wantRepo, c.wantTag, c.wantDigest)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +128,7 @@ func TestObjectExists(t *testing.T) {
|
||||
|
||||
func TestBuildTapOCIRepository(t *testing.T) {
|
||||
r := ociRef{URL: "oci://ghcr.io/foo/bar", Org: "foo", Repo: "bar", Tag: "v1"}
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "")
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "", nil)
|
||||
if obj.Namespace != "cozy-system" || obj.Spec.URL != "oci://ghcr.io/foo/bar" {
|
||||
t.Fatalf("unexpected OCIRepository: %+v", obj.Spec)
|
||||
}
|
||||
@@ -139,10 +149,121 @@ func TestBuildTapOCIRepository(t *testing.T) {
|
||||
if obj.Spec.SecretRef != nil {
|
||||
t.Fatalf("expected no secretRef, got %+v", obj.Spec.SecretRef)
|
||||
}
|
||||
withSecret := buildTapOCIRepository("n", r, "pull-creds")
|
||||
withSecret := buildTapOCIRepository("n", r, "pull-creds", nil)
|
||||
if withSecret.Spec.SecretRef == nil || withSecret.Spec.SecretRef.Name != "pull-creds" {
|
||||
t.Fatalf("expected secretRef pull-creds, got %+v", withSecret.Spec.SecretRef)
|
||||
}
|
||||
|
||||
// A digest-pinned reference sets spec.ref.digest; an explicit digest from
|
||||
// the user is written by the CLI itself (the operator only pins tag refs).
|
||||
const digest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
|
||||
rd := ociRef{URL: "oci://ghcr.io/foo/bar", Org: "foo", Repo: "bar", Digest: digest}
|
||||
objd := buildTapOCIRepository("tap-foo-bar", rd, "", nil)
|
||||
if objd.Spec.Reference == nil || objd.Spec.Reference.Digest != digest {
|
||||
t.Fatalf("expected digest pin %q, got %+v", digest, objd.Spec.Reference)
|
||||
}
|
||||
if objd.Spec.Reference.Tag != "" {
|
||||
t.Errorf("a digest-only reference must not carry a tag, got %q", objd.Spec.Reference.Tag)
|
||||
}
|
||||
// A tag may accompany the digest and is then informational.
|
||||
rtd := ociRef{URL: "oci://ghcr.io/foo/bar", Repo: "bar", Tag: "v1", Digest: digest}
|
||||
objtd := buildTapOCIRepository("n", rtd, "", nil)
|
||||
if objtd.Spec.Reference.Digest != digest || objtd.Spec.Reference.Tag != "v1" {
|
||||
t.Fatalf("expected tag v1 + digest, got %+v", objtd.Spec.Reference)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOCIRefPullRef(t *testing.T) {
|
||||
const digest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
|
||||
if got := (ociRef{URL: "oci://ghcr.io/foo/bar", Tag: "v1"}).pullRef(); got != "oci://ghcr.io/foo/bar:v1" {
|
||||
t.Errorf("tag pullRef = %q", got)
|
||||
}
|
||||
// A digest is pulled by digest, so the pull resolves the exact content
|
||||
// regardless of where the tag currently points.
|
||||
if got := (ociRef{URL: "oci://ghcr.io/foo/bar", Tag: "v1", Digest: digest}).pullRef(); got != "oci://ghcr.io/foo/bar@"+digest {
|
||||
t.Errorf("digest pullRef = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTapVerification(t *testing.T) {
|
||||
// No identity and no issuer: no verification policy.
|
||||
if v, err := tapVerification("", ""); err != nil || v != nil {
|
||||
t.Fatalf("empty flags should yield no policy, got %+v err %v", v, err)
|
||||
}
|
||||
// Only one of the pair: an error, never a half-configured policy.
|
||||
if _, err := tapVerification("subj", ""); err == nil {
|
||||
t.Error("identity without issuer must error")
|
||||
}
|
||||
if _, err := tapVerification("", "iss"); err == nil {
|
||||
t.Error("issuer without identity must error")
|
||||
}
|
||||
id := "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1"
|
||||
iss := "https://token.actions.githubusercontent.com"
|
||||
v, err := tapVerification(id, iss)
|
||||
if err != nil {
|
||||
t.Fatalf("valid pair: %v", err)
|
||||
}
|
||||
if v == nil || v.Provider != "cosign" || len(v.MatchOIDCIdentity) != 1 {
|
||||
t.Fatalf("unexpected verification: %+v", v)
|
||||
}
|
||||
// Flux evaluates the matcher as a regexp. It must be an anchored exact match:
|
||||
// it matches the exact identity and rejects a superstring (a moved tag v10, or
|
||||
// malice@... for an email identity), which an unanchored matcher would accept.
|
||||
m := v.MatchOIDCIdentity[0]
|
||||
subjRe := regexp.MustCompile(m.Subject)
|
||||
if !subjRe.MatchString(id) || subjRe.MatchString(id+"0") {
|
||||
t.Errorf("subject must be an anchored exact match, got %q", m.Subject)
|
||||
}
|
||||
issRe := regexp.MustCompile(m.Issuer)
|
||||
if !issRe.MatchString(iss) || issRe.MatchString(iss+"x") {
|
||||
t.Errorf("issuer must be an anchored exact match, got %q", m.Issuer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarryoverSourceFields(t *testing.T) {
|
||||
verifyPol := &sourcev1.OCIRepositoryVerification{Provider: "cosign", MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: "iss", Subject: "subj"}}}
|
||||
existing := &sourcev1.OCIRepository{Spec: sourcev1.OCIRepositorySpec{
|
||||
Verify: verifyPol,
|
||||
SecretRef: &fluxmeta.LocalObjectReference{Name: "pull-creds"},
|
||||
}}
|
||||
|
||||
// Re-tap without flags: an existing verify policy and pull secret are carried
|
||||
// onto the object to apply, so a server-side apply that omits them does not
|
||||
// remove them (SSA prunes a previously-owned field left out of the apply).
|
||||
obj := &sourcev1.OCIRepository{}
|
||||
carryoverSourceFields(obj, existing, nil, "")
|
||||
if obj.Spec.Verify == nil {
|
||||
t.Error("existing verify policy must be preserved when no --certificate-* flags are given")
|
||||
}
|
||||
if obj.Spec.SecretRef == nil || obj.Spec.SecretRef.Name != "pull-creds" {
|
||||
t.Error("existing pull secret must be preserved when no --secret is given")
|
||||
}
|
||||
|
||||
// Flags present: the invocation's own values win, nothing is carried over.
|
||||
flagVerify := &sourcev1.OCIRepositoryVerification{Provider: "cosign"}
|
||||
obj2 := &sourcev1.OCIRepository{Spec: sourcev1.OCIRepositorySpec{Verify: flagVerify, SecretRef: &fluxmeta.LocalObjectReference{Name: "new-creds"}}}
|
||||
carryoverSourceFields(obj2, existing, flagVerify, "new-creds")
|
||||
if obj2.Spec.Verify != flagVerify {
|
||||
t.Error("a verify policy from flags must not be overwritten by the existing one")
|
||||
}
|
||||
if obj2.Spec.SecretRef.Name != "new-creds" {
|
||||
t.Error("a secret from --secret must not be overwritten by the existing one")
|
||||
}
|
||||
|
||||
// No existing source: a no-op, no panic.
|
||||
carryoverSourceFields(&sourcev1.OCIRepository{}, nil, nil, "")
|
||||
}
|
||||
|
||||
func TestBuildTapOCIRepositoryWithVerify(t *testing.T) {
|
||||
r := ociRef{URL: "oci://ghcr.io/foo/bar", Repo: "bar", Tag: "v1"}
|
||||
v := &sourcev1.OCIRepositoryVerification{
|
||||
Provider: "cosign",
|
||||
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: "iss", Subject: "subj"}},
|
||||
}
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "", v)
|
||||
if obj.Spec.Verify == nil || obj.Spec.Verify.Provider != "cosign" || len(obj.Spec.Verify.MatchOIDCIdentity) != 1 {
|
||||
t.Fatalf("verify not set on the source: %+v", obj.Spec.Verify)
|
||||
}
|
||||
}
|
||||
|
||||
// rewritePackageSourceForTap must NOT rename the PackageSource: a tapped
|
||||
@@ -279,16 +400,23 @@ func TestCreatePackageIdempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearMaterializedRevision(t *testing.T) {
|
||||
func TestResetTapPinState(t *testing.T) {
|
||||
s := untapScheme(t)
|
||||
const oldDigest = "sha256:2222222222222222222222222222222222222222222222222222222222222222"
|
||||
oci := &sourcev1.OCIRepository{ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "tap-acme-hello", Namespace: "cozy-system",
|
||||
Annotations: map[string]string{tapconst.MaterializedRevisionAnnotation: "rev-1", "keep": "me"},
|
||||
}}
|
||||
Annotations: map[string]string{
|
||||
tapconst.MaterializedRevisionAnnotation: "v1@" + oldDigest,
|
||||
tapconst.PinnedDigestAnnotation: oldDigest,
|
||||
"keep": "me",
|
||||
},
|
||||
}, Spec: sourcev1.OCIRepositorySpec{Reference: &sourcev1.OCIRepositoryRef{Tag: "v1", Digest: oldDigest}}}
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(oci).Build()
|
||||
|
||||
if err := clearMaterializedRevision(context.Background(), cl, "tap-acme-hello"); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
// A tag-based re-tap must drop the operator's digest pin (cozypkg cannot
|
||||
// clear a field the operator owns via server-side apply) and both annotations.
|
||||
if err := resetTapPinState(context.Background(), cl, "tap-acme-hello", false); err != nil {
|
||||
t.Fatalf("reset: %v", err)
|
||||
}
|
||||
var got sourcev1.OCIRepository
|
||||
if err := cl.Get(context.Background(), client.ObjectKey{Name: "tap-acme-hello", Namespace: "cozy-system"}, &got); err != nil {
|
||||
@@ -297,11 +425,38 @@ func TestClearMaterializedRevision(t *testing.T) {
|
||||
if _, ok := got.Annotations[tapconst.MaterializedRevisionAnnotation]; ok {
|
||||
t.Error("materialized-revision annotation must be cleared to force re-materialization")
|
||||
}
|
||||
if _, ok := got.Annotations[tapconst.PinnedDigestAnnotation]; ok {
|
||||
t.Error("pinned-digest annotation must be cleared so the operator re-pins")
|
||||
}
|
||||
if got.Spec.Reference != nil && got.Spec.Reference.Digest != "" {
|
||||
t.Errorf("a tag re-tap must drop the operator digest pin, got %q", got.Spec.Reference.Digest)
|
||||
}
|
||||
if got.Spec.Reference == nil || got.Spec.Reference.Tag != "v1" {
|
||||
t.Errorf("the tag must be left in place, got %+v", got.Spec.Reference)
|
||||
}
|
||||
if got.Annotations["keep"] != "me" {
|
||||
t.Error("unrelated annotations must be preserved")
|
||||
}
|
||||
|
||||
// An explicit-digest re-tap must NOT drop the digest the CLI itself set.
|
||||
oci2 := &sourcev1.OCIRepository{ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "tap-acme-two", Namespace: "cozy-system",
|
||||
Annotations: map[string]string{tapconst.MaterializedRevisionAnnotation: oldDigest},
|
||||
}, Spec: sourcev1.OCIRepositorySpec{Reference: &sourcev1.OCIRepositoryRef{Digest: oldDigest}}}
|
||||
cl2 := fake.NewClientBuilder().WithScheme(s).WithObjects(oci2).Build()
|
||||
if err := resetTapPinState(context.Background(), cl2, "tap-acme-two", true); err != nil {
|
||||
t.Fatalf("reset explicit: %v", err)
|
||||
}
|
||||
var got2 sourcev1.OCIRepository
|
||||
if err := cl2.Get(context.Background(), client.ObjectKey{Name: "tap-acme-two", Namespace: "cozy-system"}, &got2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got2.Spec.Reference == nil || got2.Spec.Reference.Digest != oldDigest {
|
||||
t.Errorf("an explicit-digest re-tap must keep its digest, got %+v", got2.Spec.Reference)
|
||||
}
|
||||
|
||||
// Absent source: no error.
|
||||
if err := clearMaterializedRevision(context.Background(), cl, "missing"); err != nil {
|
||||
if err := resetTapPinState(context.Background(), cl, "missing", false); err != nil {
|
||||
t.Errorf("a missing source must not error, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -99,6 +99,7 @@ func main() {
|
||||
var platformSourceName string
|
||||
var platformSourceRef string
|
||||
var platformSourceSecret string
|
||||
var platformSourceCertSecret string
|
||||
var systemNamespaceMemoryLimit string
|
||||
var systemNamespaceMemoryRequest string
|
||||
|
||||
@@ -145,6 +146,7 @@ func main() {
|
||||
flag.StringVar(&platformSourceName, "platform-source-name", "cozystack-platform", "Name for the generated platform source resource and PackageSource")
|
||||
flag.StringVar(&platformSourceRef, "platform-source-ref", "", "Reference specification as key=value pairs (e.g., 'branch=main' or 'digest=sha256:...,tag=v1.0'). For OCI: digest, semver, semverFilter, tag. For Git: branch, tag, semver, name, commit.")
|
||||
flag.StringVar(&platformSourceSecret, "platform-source-secret", "", "Name of the Secret in cozy-system namespace containing credentials for the platform source. Sets spec.secretRef on the generated OCIRepository or GitRepository. Secret type depends on the source kind: kubernetes.io/dockerconfigjson for OCI (oci://...); Opaque with username+password (or bearerToken) for Git over HTTPS; Opaque with identity (PEM private key) and known_hosts for Git over SSH.")
|
||||
flag.StringVar(&platformSourceCertSecret, "platform-source-cert-secret", "", "Name of the Secret in cozy-system namespace holding TLS material for an OCI platform source: ca.crt for a registry behind a private CA, and/or tls.crt+tls.key for client certificate auth. Sets spec.certSecretRef on the generated OCIRepository. Not supported for Git sources, where Flux reads ca.crt from the --platform-source-secret Secret.")
|
||||
flag.StringVar(&cozyValuesSecretName, "cozy-values-secret-name", "cozystack-values", "The name of the secret containing cluster-wide configuration values.")
|
||||
flag.StringVar(&cozyValuesSecretNamespace, "cozy-values-secret-namespace", "cozy-system", "The namespace of the secret containing cluster-wide configuration values.")
|
||||
flag.StringVar(&cozyValuesNamespaceSelector, "cozy-values-namespace-selector", "cozystack.io/system=true", "The label selector for namespaces where the cluster-wide configuration values must be replicated.")
|
||||
@@ -152,10 +154,10 @@ func main() {
|
||||
"Default container memory limit in eligible system namespaces, including kube-system. "+
|
||||
"Lowering or an oversized request creates a durable hold requiring an administrator's "+
|
||||
"exact-value acknowledgement. Empty or 0 disables and removes managed LimitRanges; "+
|
||||
"safety state is retained. See docs/operations/system-memory-limits.md.")
|
||||
"safety state is retained. See https://cozystack.io/docs/v1/operations/configuration/system-memory-limits/.")
|
||||
flag.StringVar(&systemNamespaceMemoryRequest, "system-namespace-memory-request", DefaultSystemNamespaceMemoryRequest,
|
||||
"Default memory request paired with the system namespace limit. Keep small; 0 is supported. "+
|
||||
"An empty installer value selects the operator default. See docs/operations/system-memory-limits.md.")
|
||||
"An empty installer value selects the operator default. See https://cozystack.io/docs/v1/operations/configuration/system-memory-limits/.")
|
||||
|
||||
opts := zap.Options{
|
||||
Development: true,
|
||||
@@ -189,6 +191,11 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := validatePlatformSourceCertSecret(platformSourceURL, platformSourceCertSecret); err != nil {
|
||||
setupLog.Error(err, "invalid platform source flags")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
config := ctrl.GetConfigOrDie()
|
||||
|
||||
// Create a direct client (without cache) for pre-start operations
|
||||
@@ -278,12 +285,12 @@ func main() {
|
||||
|
||||
// Generate and install platform source resource if specified
|
||||
if platformSourceURL != "" {
|
||||
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef, "secret", platformSourceSecret)
|
||||
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef, "secret", platformSourceSecret, "certSecret", platformSourceCertSecret)
|
||||
installCtx, installCancel := context.WithTimeout(mgrCtx, 2*time.Minute)
|
||||
defer installCancel()
|
||||
|
||||
// Use direct client for pre-start operations (cache is not ready yet)
|
||||
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef, platformSourceSecret); err != nil {
|
||||
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef, platformSourceSecret, platformSourceCertSecret); err != nil {
|
||||
setupLog.Error(err, "failed to install platform source resource")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
@@ -414,7 +421,7 @@ func main() {
|
||||
|
||||
// installPlatformSourceResource generates and installs a Flux source resource (OCIRepository or GitRepository)
|
||||
// based on the platform source URL
|
||||
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec, secretName string) error {
|
||||
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec, secretName, certSecretName string) error {
|
||||
logger := log.FromContext(ctx)
|
||||
|
||||
// Parse the source URL to determine type
|
||||
@@ -432,11 +439,14 @@ func installPlatformSourceResource(ctx context.Context, k8sClient client.Client,
|
||||
var obj client.Object
|
||||
switch sourceType {
|
||||
case "oci":
|
||||
obj, err = generateOCIRepository(resourceName, repoURL, refMap, secretName)
|
||||
obj, err = generateOCIRepository(resourceName, repoURL, refMap, secretName, certSecretName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate OCIRepository: %w", err)
|
||||
}
|
||||
case "git":
|
||||
if err := validatePlatformSourceCertSecret(sourceURL, certSecretName); err != nil {
|
||||
return err
|
||||
}
|
||||
obj, err = generateGitRepository(resourceName, repoURL, refMap, secretName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate GitRepository: %w", err)
|
||||
@@ -532,6 +542,19 @@ func parseSystemNamespaceMemory(rawLimit, rawRequest string) (limit, request res
|
||||
return limit, request, nil
|
||||
}
|
||||
|
||||
// validatePlatformSourceCertSecret rejects --platform-source-cert-secret for a
|
||||
// Git source: GitRepository has no certSecretRef, and Flux reads a Git CA from
|
||||
// the ca.crt key of the --platform-source-secret Secret instead.
|
||||
func validatePlatformSourceCertSecret(sourceURL, certSecretName string) error {
|
||||
if certSecretName == "" {
|
||||
return nil
|
||||
}
|
||||
if sourceType, _, err := parsePlatformSourceURL(sourceURL); err == nil && sourceType == "git" {
|
||||
return fmt.Errorf("--platform-source-cert-secret is only supported for OCI sources; for a Git source put ca.crt into the --platform-source-secret Secret")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parsePlatformSourceURL parses the source URL and returns the source type and repository URL.
|
||||
// Supports formats:
|
||||
// - oci://registry.example.com/repo
|
||||
@@ -650,7 +673,7 @@ func validateGitRef(refMap map[string]string) error {
|
||||
}
|
||||
|
||||
// generateOCIRepository creates an OCIRepository resource
|
||||
func generateOCIRepository(name, repoURL string, refMap map[string]string, secretName string) (*sourcev1.OCIRepository, error) {
|
||||
func generateOCIRepository(name, repoURL string, refMap map[string]string, secretName, certSecretName string) (*sourcev1.OCIRepository, error) {
|
||||
if err := validateOCIRef(refMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -687,6 +710,12 @@ func generateOCIRepository(name, repoURL string, refMap map[string]string, secre
|
||||
}
|
||||
}
|
||||
|
||||
if certSecretName != "" {
|
||||
obj.Spec.CertSecretRef = &fluxmeta.LocalObjectReference{
|
||||
Name: certSecretName,
|
||||
}
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
"github.com/cozystack/cozystack/internal/marketplace/tapconst"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -534,7 +535,7 @@ func TestValidateGitRef(t *testing.T) {
|
||||
|
||||
func TestGenerateOCIRepository(t *testing.T) {
|
||||
refMap := map[string]string{"tag": "v1.0", "digest": "sha256:abc123"}
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", refMap, "")
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", refMap, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -560,10 +561,13 @@ func TestGenerateOCIRepository(t *testing.T) {
|
||||
if obj.Spec.SecretRef != nil {
|
||||
t.Errorf("expected SecretRef to be nil when secretName is empty, got %+v", obj.Spec.SecretRef)
|
||||
}
|
||||
if obj.Spec.CertSecretRef != nil {
|
||||
t.Errorf("expected CertSecretRef to be nil when certSecretName is empty, got %+v", obj.Spec.CertSecretRef)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateOCIRepository_NoRef(t *testing.T) {
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{}, "")
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{}, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -573,14 +577,14 @@ func TestGenerateOCIRepository_NoRef(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerateOCIRepository_InvalidRef(t *testing.T) {
|
||||
_, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"branch": "main"}, "")
|
||||
_, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"branch": "main"}, "", "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid OCI ref key, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateOCIRepository_WithSecret(t *testing.T) {
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"tag": "v1.0"}, "my-registry-creds")
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"tag": "v1.0"}, "my-registry-creds", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -592,6 +596,147 @@ func TestGenerateOCIRepository_WithSecret(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateOCIRepository_WithCertSecret(t *testing.T) {
|
||||
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"tag": "v1.0"}, "", "registry-ca")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if obj.Spec.CertSecretRef == nil {
|
||||
t.Fatal("expected CertSecretRef to be set when certSecretName is non-empty")
|
||||
}
|
||||
if obj.Spec.CertSecretRef.Name != "registry-ca" {
|
||||
t.Errorf("expected CertSecretRef.Name %q, got %q", "registry-ca", obj.Spec.CertSecretRef.Name)
|
||||
}
|
||||
if obj.Spec.SecretRef != nil {
|
||||
t.Errorf("expected SecretRef to stay nil when only certSecretName is set, got %+v", obj.Spec.SecretRef)
|
||||
}
|
||||
}
|
||||
|
||||
func newSourceTestClient(t *testing.T, objs ...client.Object) client.Client {
|
||||
t.Helper()
|
||||
scheme := newTestScheme()
|
||||
if err := sourcev1.AddToScheme(scheme); err != nil {
|
||||
t.Fatalf("add source scheme: %v", err)
|
||||
}
|
||||
return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
|
||||
}
|
||||
|
||||
// The operator rewrites the whole platform OCIRepository on every start, so a
|
||||
// certSecretRef patched onto the live object by hand is lost on the next
|
||||
// restart and source-controller then fails TLS against a registry behind a
|
||||
// private CA. The CA has to come from the operator's own arguments and survive
|
||||
// that update path, including a rename of the Secret when the CA is rotated.
|
||||
func TestInstallPlatformSourceResource_UpdateKeepsCertSecret(t *testing.T) {
|
||||
existing := &sourcev1.OCIRepository{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "cozystack-platform", Namespace: "cozy-system"},
|
||||
Spec: sourcev1.OCIRepositorySpec{URL: "oci://ghcr.io/cozystack/cozystack/cozystack-packages"},
|
||||
}
|
||||
k8sClient := newSourceTestClient(t, existing)
|
||||
|
||||
for _, certSecret := range []string{"registry-ca", "registry-ca-next"} {
|
||||
if err := installPlatformSourceResource(context.Background(), k8sClient,
|
||||
"oci://registry.internal:8083/cozystack/cozystack/cozystack-packages", "cozystack-platform",
|
||||
"digest=sha256:abc123", "registry-creds", certSecret); err != nil {
|
||||
t.Fatalf("installPlatformSourceResource: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
got := &sourcev1.OCIRepository{}
|
||||
if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: "cozy-system", Name: "cozystack-platform"}, got); err != nil {
|
||||
t.Fatalf("get OCIRepository: %v", err)
|
||||
}
|
||||
if got.Spec.CertSecretRef == nil || got.Spec.CertSecretRef.Name != "registry-ca-next" {
|
||||
t.Errorf("expected CertSecretRef registry-ca-next after update, got %+v", got.Spec.CertSecretRef)
|
||||
}
|
||||
if got.Spec.SecretRef == nil || got.Spec.SecretRef.Name != "registry-creds" {
|
||||
t.Errorf("expected SecretRef registry-creds after update, got %+v", got.Spec.SecretRef)
|
||||
}
|
||||
if got.Spec.URL != "oci://registry.internal:8083/cozystack/cozystack/cozystack-packages" {
|
||||
t.Errorf("expected URL to be repointed, got %q", got.Spec.URL)
|
||||
}
|
||||
}
|
||||
|
||||
// GitRepository has no certSecretRef: Flux reads a Git CA from the ca.crt key
|
||||
// of the secretRef Secret. Accepting the flag for a Git source would silently
|
||||
// drop the CA, so it must fail before anything is written.
|
||||
func TestInstallPlatformSourceResource_GitRejectsCertSecret(t *testing.T) {
|
||||
k8sClient := newSourceTestClient(t)
|
||||
|
||||
err := installPlatformSourceResource(context.Background(), k8sClient,
|
||||
"https://git.internal/cozystack/cozystack", "cozystack-platform", "branch=main", "git-creds", "git-ca")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for a cert secret on a Git platform source, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "ca.crt") {
|
||||
t.Errorf("expected the error to point at the ca.crt key of the source secret, got: %v", err)
|
||||
}
|
||||
|
||||
list := &sourcev1.GitRepositoryList{}
|
||||
if err := k8sClient.List(context.Background(), list); err != nil {
|
||||
t.Fatalf("list GitRepositories: %v", err)
|
||||
}
|
||||
if len(list.Items) != 0 {
|
||||
t.Errorf("expected no GitRepository to be written, got %d", len(list.Items))
|
||||
}
|
||||
}
|
||||
|
||||
// The Git guard runs on every start; one that misfires without a cert secret
|
||||
// would crash-loop the operator for every Git platform source.
|
||||
func TestInstallPlatformSourceResource_GitWithoutCertSecret(t *testing.T) {
|
||||
for _, sourceURL := range []string{
|
||||
"https://git.internal/cozystack/cozystack",
|
||||
"ssh://git@git.internal/cozystack/cozystack",
|
||||
} {
|
||||
t.Run(sourceURL, func(t *testing.T) {
|
||||
k8sClient := newSourceTestClient(t)
|
||||
|
||||
if err := installPlatformSourceResource(context.Background(), k8sClient,
|
||||
sourceURL, "cozystack-platform", "branch=main", "git-creds", ""); err != nil {
|
||||
t.Fatalf("installPlatformSourceResource: %v", err)
|
||||
}
|
||||
|
||||
got := &sourcev1.GitRepository{}
|
||||
if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: "cozy-system", Name: "cozystack-platform"}, got); err != nil {
|
||||
t.Fatalf("get GitRepository: %v", err)
|
||||
}
|
||||
if got.Spec.URL != sourceURL {
|
||||
t.Errorf("expected URL %q, got %q", sourceURL, got.Spec.URL)
|
||||
}
|
||||
if got.Spec.SecretRef == nil || got.Spec.SecretRef.Name != "git-creds" {
|
||||
t.Errorf("expected SecretRef git-creds, got %+v", got.Spec.SecretRef)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Checked right after flag parsing: CRDs and Flux are installed before the
|
||||
// platform source, so a combination rejected only there would exit after a
|
||||
// partial install.
|
||||
func TestValidatePlatformSourceCertSecret(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
sourceURL string
|
||||
certSecret string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "oci with cert secret", sourceURL: "oci://registry.internal:8083/cozystack-packages", certSecret: "registry-ca"},
|
||||
{name: "oci without cert secret", sourceURL: "oci://registry.internal:8083/cozystack-packages"},
|
||||
{name: "https git without cert secret", sourceURL: "https://git.internal/cozystack/cozystack"},
|
||||
{name: "ssh git without cert secret", sourceURL: "ssh://git@git.internal/cozystack/cozystack"},
|
||||
{name: "no source url", certSecret: "registry-ca"},
|
||||
{name: "https git with cert secret", sourceURL: "https://git.internal/cozystack/cozystack", certSecret: "git-ca", wantErr: true},
|
||||
{name: "ssh git with cert secret", sourceURL: "ssh://git@git.internal/cozystack/cozystack", certSecret: "git-ca", wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validatePlatformSourceCertSecret(tt.sourceURL, tt.certSecret)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("validatePlatformSourceCertSecret(%q, %q) error = %v, wantErr %v", tt.sourceURL, tt.certSecret, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateGitRepository(t *testing.T) {
|
||||
refMap := map[string]string{"branch": "main", "commit": "abc1234def5678"}
|
||||
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", refMap, "")
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
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 observability-proxy serves tenant logs from the VictoriaLogs
|
||||
// stores behind a Kubernetes TokenReview and SubjectAccessReview, with the
|
||||
// tenant's namespace scope forced into every upstream request.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
"github.com/cozystack/cozystack/internal/observabilityproxy"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
listenAddress string
|
||||
probeAddress string
|
||||
clusterDomain string
|
||||
rootTenant string
|
||||
kubeconfig string
|
||||
authCacheTTL time.Duration
|
||||
requestTimeout time.Duration
|
||||
tailIdleTimeout time.Duration
|
||||
shutdownTimeout time.Duration
|
||||
logs observabilityproxy.LogsLimits
|
||||
limiter observabilityproxy.LimiterConfig
|
||||
}
|
||||
|
||||
func parseFlags(args []string) (options, error) {
|
||||
var o options
|
||||
fs := flag.NewFlagSet("observability-proxy", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
fs.StringVar(&o.listenAddress, "listen-address", ":8080", "address of the API listener")
|
||||
fs.StringVar(&o.probeAddress, "probe-address", ":8081", "address serving /healthz, /readyz and /metrics")
|
||||
fs.StringVar(&o.clusterDomain, "cluster-domain", "cozy.local", "cluster DNS domain used to reach the stores")
|
||||
fs.StringVar(&o.rootTenant, "root-tenant", "tenant-root", "tenant namespace hosting the default store")
|
||||
fs.StringVar(&o.kubeconfig, "kubeconfig", "", "kubeconfig path; in-cluster config when empty")
|
||||
fs.DurationVar(&o.authCacheTTL, "auth-cache-ttl", 10*time.Second, "how long TokenReview and SubjectAccessReview answers are cached")
|
||||
fs.DurationVar(&o.requestTimeout, "request-timeout", time.Minute, "deadline for reading the request body, the token and access reviews, and every upstream endpoint but tail; 0 disables it")
|
||||
fs.DurationVar(&o.tailIdleTimeout, "tail-idle-timeout", 5*time.Minute, "close a tail after this long without a line")
|
||||
fs.DurationVar(&o.shutdownTimeout, "shutdown-timeout", 15*time.Second, "grace period for in-flight requests on shutdown")
|
||||
fs.DurationVar(&o.logs.DefaultRange, "logs-default-range", time.Hour, "window used when a logs request has no start")
|
||||
// One week covers an incident investigation and keeps a single query
|
||||
// to a fraction of the default one-month log retention.
|
||||
fs.DurationVar(&o.logs.MaxRange, "logs-max-range", 7*24*time.Hour, "longest [start, end] a logs request may span; wider windows are clamped")
|
||||
// Prometheus's cap on points per series in a range query.
|
||||
fs.IntVar(&o.logs.MaxBuckets, "logs-max-buckets", 11000, "most buckets hits and stats_query_range may return over a window")
|
||||
fs.IntVar(&o.logs.DefaultLimit, "logs-default-limit", 1000, "entries returned when a request sets no limit")
|
||||
fs.IntVar(&o.logs.MaxLimit, "logs-max-limit", 10000, "largest limit a request may set")
|
||||
fs.DurationVar(&o.logs.MaxTailResume, "logs-max-tail-resume", 15*time.Minute, "how far back a tail may resume with since or start_offset")
|
||||
fs.IntVar(&o.limiter.MaxConcurrentPerUser, "max-concurrent-per-user", 10, "concurrent non-tail requests per user; 0 disables")
|
||||
fs.Float64Var(&o.limiter.RatePerUser, "rate-limit-per-user", 10, "sustained requests per second per user; 0 disables")
|
||||
fs.IntVar(&o.limiter.RateBurst, "rate-limit-burst", 20, "request burst per user")
|
||||
fs.IntVar(&o.limiter.MaxTailsPerUser, "max-tails-per-user", 5, "open tails per user; 0 disables")
|
||||
fs.IntVar(&o.limiter.MaxTails, "max-tails", 200, "open tails in total; 0 disables")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return o, err
|
||||
}
|
||||
// Parsing stops at the first non-flag, so every flag after it would
|
||||
// be dropped silently.
|
||||
if fs.NArg() > 0 {
|
||||
return o, fmt.Errorf("unexpected argument %q", fs.Arg(0))
|
||||
}
|
||||
switch {
|
||||
case o.rootTenant == "":
|
||||
return o, errors.New("--root-tenant must not be empty")
|
||||
case o.clusterDomain == "":
|
||||
return o, errors.New("--cluster-domain must not be empty")
|
||||
case o.logs.DefaultRange <= 0 || o.logs.MaxRange < o.logs.DefaultRange:
|
||||
return o, errors.New("--logs-default-range must be positive and not above --logs-max-range")
|
||||
// Below four buckets (three steps once one is held back for
|
||||
// alignment), or a range under three seconds, the step cap that keeps
|
||||
// vlselect's step alignment inside the range cannot hold.
|
||||
case o.logs.MaxBuckets < 4:
|
||||
return o, errors.New("--logs-max-buckets must be at least 4")
|
||||
case o.logs.MaxRange < 3*time.Second:
|
||||
return o, errors.New("--logs-max-range must be at least 3s")
|
||||
case o.logs.DefaultLimit <= 0 || o.logs.MaxLimit < o.logs.DefaultLimit:
|
||||
return o, errors.New("--logs-default-limit must be positive and not above --logs-max-limit")
|
||||
case o.logs.MaxTailResume < 0:
|
||||
return o, errors.New("--logs-max-tail-resume must not be negative")
|
||||
case o.requestTimeout < 0 || o.tailIdleTimeout < 0 || o.authCacheTTL < 0 || o.shutdownTimeout < 0:
|
||||
return o, errors.New("timeouts and the auth cache TTL must not be negative")
|
||||
// NaN passes every comparison and +Inf never empties a bucket; either
|
||||
// would switch the rate limit off without saying so.
|
||||
case math.IsNaN(o.limiter.RatePerUser) || math.IsInf(o.limiter.RatePerUser, 0):
|
||||
return o, errors.New("--rate-limit-per-user must be a finite number")
|
||||
case o.limiter.RatePerUser < 0 || o.limiter.RateBurst < 0 || o.limiter.MaxConcurrentPerUser < 0 ||
|
||||
o.limiter.MaxTailsPerUser < 0 || o.limiter.MaxTails < 0:
|
||||
return o, errors.New("limits must not be negative; 0 disables a limit")
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
|
||||
o, err := parseFlags(os.Args[1:])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "observability-proxy: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err := run(o); err != nil {
|
||||
slog.Error("exiting", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(o options) error {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
restConfig, err := clientcmd.BuildConfigFromFlags("", o.kubeconfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading kubeconfig: %w", err)
|
||||
}
|
||||
client, err := kubernetes.NewForConfig(restConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("building client: %w", err)
|
||||
}
|
||||
|
||||
factory := informers.NewSharedInformerFactory(client, 0)
|
||||
namespaces := factory.Core().V1().Namespaces()
|
||||
lister := namespaces.Lister()
|
||||
factory.Start(ctx.Done())
|
||||
var ready atomic.Bool
|
||||
go func() {
|
||||
if cache.WaitForCacheSync(ctx.Done(), namespaces.Informer().HasSynced) {
|
||||
ready.Store(true)
|
||||
}
|
||||
}()
|
||||
|
||||
reg := prometheus.NewRegistry()
|
||||
reg.MustRegister(collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
||||
proxy := observabilityproxy.NewServer(observabilityproxy.Config{
|
||||
ClusterDomain: o.clusterDomain,
|
||||
Logs: o.logs,
|
||||
Limiter: o.limiter,
|
||||
RequestTimeout: o.requestTimeout,
|
||||
TailIdleTimeout: o.tailIdleTimeout,
|
||||
}, observabilityproxy.NewAuthorizer(client, o.authCacheTTL), observabilityproxy.NewStoreResolver(lister, namespaces.Informer().HasSynced, o.rootTenant), reg)
|
||||
|
||||
probes := http.NewServeMux()
|
||||
probes.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "ok") })
|
||||
probes.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) {
|
||||
if !ready.Load() {
|
||||
http.Error(w, "namespace cache not synced", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
_, _ = io.WriteString(w, "ok")
|
||||
})
|
||||
probes.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
||||
|
||||
// No WriteTimeout on the API listener: it would cut live tails, which
|
||||
// the handler bounds with its own idle timeout instead.
|
||||
servers := []*http.Server{
|
||||
{Addr: o.listenAddress, Handler: proxy, ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 2 * time.Minute},
|
||||
{Addr: o.probeAddress, Handler: probes, ReadHeaderTimeout: 10 * time.Second},
|
||||
}
|
||||
errs := make(chan error, len(servers))
|
||||
for _, srv := range servers {
|
||||
go func() {
|
||||
slog.Info("listening", "address", srv.Addr)
|
||||
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
errs <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
var runErr error
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case runErr = <-errs:
|
||||
}
|
||||
stop()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), o.shutdownTimeout)
|
||||
defer cancel()
|
||||
for _, srv := range servers {
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
// Open tails never go idle on their own; cut them so clients
|
||||
// reconnect with since to another replica.
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
return runErr
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
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 (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParseFlagsDefaults(t *testing.T) {
|
||||
o, err := parseFlags(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o.listenAddress != ":8080" || o.probeAddress != ":8081" || o.clusterDomain != "cozy.local" || o.rootTenant != "tenant-root" {
|
||||
t.Errorf("contract defaults changed: %+v", o)
|
||||
}
|
||||
if o.authCacheTTL != 10*time.Second || o.logs.MaxTailResume != 15*time.Minute || o.logs.MaxRange != 7*24*time.Hour {
|
||||
t.Errorf("limit defaults changed: %+v", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsOverrides(t *testing.T) {
|
||||
o, err := parseFlags([]string{"--cluster-domain=example.org", "--logs-max-range=24h", "--max-tails=3"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o.clusterDomain != "example.org" || o.logs.MaxRange != 24*time.Hour || o.limiter.MaxTails != 3 {
|
||||
t.Errorf("overrides not applied: %+v", o)
|
||||
}
|
||||
// The smallest bucket count and range the step cap can satisfy.
|
||||
if _, err := parseFlags([]string{"--logs-max-buckets=4", "--logs-max-range=3s", "--logs-default-range=1s"}); err != nil {
|
||||
t.Errorf("smallest satisfiable step cap refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsInvalid(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--root-tenant="},
|
||||
{"--cluster-domain="},
|
||||
{"--logs-default-range=48h", "--logs-max-range=24h"},
|
||||
{"--logs-max-buckets=3"},
|
||||
{"--logs-max-range=2s", "--logs-default-range=1s"},
|
||||
{"--shutdown-timeout=-1s"},
|
||||
{"--logs-default-limit=0"},
|
||||
{"--logs-default-limit=100", "--logs-max-limit=10"},
|
||||
{"--logs-max-tail-resume=-1m"},
|
||||
{"--request-timeout=-1s"},
|
||||
{"--tail-idle-timeout=-1s"},
|
||||
{"--auth-cache-ttl=-1s"},
|
||||
{"--rate-limit-per-user=-1"},
|
||||
{"--rate-limit-per-user=NaN"},
|
||||
{"--rate-limit-per-user=+Inf"},
|
||||
{"--rate-limit-burst=-1"},
|
||||
{"--max-concurrent-per-user=-1"},
|
||||
{"--max-tails-per-user=-1"},
|
||||
{"--max-tails=-1"},
|
||||
{"--unknown"},
|
||||
{"unexpected", "--root-tenant=tenant-x"},
|
||||
} {
|
||||
if _, err := parseFlags(args); err == nil {
|
||||
t.Errorf("parseFlags(%v) accepted", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ When the user asks to generate a changelog, follow these steps in the specified
|
||||
- [ ] **MANDATORY**: Extract PR numbers from commit messages, then use `gh pr view` for each PR to get the PR author. Do NOT use commit author. Only for commits without PR numbers (rare), fall back to `gh api repos/cozystack/cozystack/commits/<hash> --jq '.author.login'`
|
||||
- [ ] Step 8: Form new changelog (structure, format, generate contributors list)
|
||||
- [ ] Step 8b: Verify every operator-facing statement against the code at the upper-bound tag (section 8b)
|
||||
- [ ] Step 9: Verify completeness and save, after running the checks in `docs/release.md` (section 9)
|
||||
- [ ] Step 9: Verify completeness and save, after running the checks in `docs/release/changelogs.md` (section 9)
|
||||
|
||||
### 1. Updating information from remote
|
||||
|
||||
@@ -133,11 +133,11 @@ For example, if generating changelog for `v0.37.2`:
|
||||
git log v0.37.1..v0.37.2 --pretty=format:"%h - %s (%an, %ar)"
|
||||
```
|
||||
|
||||
**⚠️ IMPORTANT: Check for backports:**
|
||||
- Look for commits with "[Backport release-X.Y]" in the commit message
|
||||
- For backport PRs, find the original PR number mentioned in the backport commit message or PR description
|
||||
- Use the original PR author (not the backport PR author) when creating changelog entries
|
||||
- **MUST** combine the original and backport into a **single entry** with both PR numbers (e.g. `#1606, backport #1609`). Do NOT list the original and the backport as two separate entries — that produces duplicates in the changelog.
|
||||
**⚠️ IMPORTANT: Check for backports.** They reach a release line in two shapes:
|
||||
- A backport batch ([`docs/release/backports.md`](../release/backports.md)): a merge commit titled `fix(backport): batch for release-X.Y, <date> (#BATCH_PR)`. Inside it each original is one merge commit that carries the original's merge message from `main`, `Merge pull request #ORIGINAL_PR from …` or `<original title> (#ORIGINAL_PR)`, and ends in a `Backported-from: <sha>` trailer, with the original's own commits, picked with `-x`, on its second parent; an original squash-merged into `main` is one commit with its subject, `<original title> (#ORIGINAL_PR)`, and a `(cherry picked from commit <sha>)` line. Read the original's number, title and author from those, list the batch with `git log --first-parent` inside it, and never write an entry for the batch PR itself or for a picked commit of an original. An original whose pick differs from it is listed under "Check these notes" in the batch merge's message, which carries the batch PR's body, and in its record's `changed` list: write its entry from what its pick changed on the line, read with the `git range-diff` and `git diff` commands listed there, not from the original's release note.
|
||||
- A single backport PR, from before the batch (the retired label-driven bot) or a release-only fix: commits with "[Backport release-X.Y]" in the message, whose original PR number is in the backport commit message or PR description.
|
||||
- Use the original PR author (not the backport or batch PR author) when creating changelog entries
|
||||
- **MUST** combine the original and backport into a **single entry** with both PR numbers (e.g. `#1606, backport #1609`, where a batch's original reads `#1606, backport #BATCH_PR`). Do NOT list the original and the backport as two separate entries — that produces duplicates in the changelog.
|
||||
|
||||
**For minor releases (vX.Y.0):**
|
||||
Minor releases must include **all changes** from patch releases of the previous minor version. Get commits from the previous minor release tag up to the new tag:
|
||||
@@ -292,7 +292,8 @@ git log v<previous_version>..v<new_version> --format="%s%n%b" | grep -oE '#[0-9]
|
||||
```
|
||||
|
||||
**⚠️ IMPORTANT: Handle backports correctly:**
|
||||
- Backport PRs have format: `[Backport release-X.Y] <original title> (#BACKPORT_PR_NUMBER)`
|
||||
- A batch PR has the title `fix(backport): batch for release-X.Y, <date>`. It is not a change of its own: drop its number from the list, and take its originals from the batch's first-parent line, `git log --first-parent <batch merge>^1..<batch merge>^2 --format=%s`, one subject per original, which names it by `(#N)` or `Merge pull request #N`. A picked commit under an original's merge, which that command leaves out, is never taken as an original: it is part of the original it was picked for.
|
||||
- Backport PRs from before the batch have format: `[Backport release-X.Y] <original title> (#BACKPORT_PR_NUMBER)`
|
||||
- The backport commit message or PR description usually mentions the original PR number
|
||||
- For backport entries in changelog, use the original PR author (not the backport PR author)
|
||||
- **MUST** combine the original and backport PR into a **single changelog entry** with both PR numbers (e.g. `#1606, backport #1609`). NEVER list the original and the backport as two separate entries — that produces visible duplicates and was the primary defect that motivated this rule.
|
||||
@@ -373,7 +374,8 @@ git log v<previous_version>..v<new_version> --format="%s%n%b" | grep -oE '#[0-9]
|
||||
|
||||
4. **For release branches (backports):**
|
||||
- If commit is from `release-X.Y` branch, check if it's a backport
|
||||
- Find original commit in `main` to get correct PR number:
|
||||
- An original a batch carries names its merge commit on `main` in the `Backported-from:` trailer of its merge, and each commit picked under that merge names its source in its `(cherry picked from commit …)` line; read those first
|
||||
- Otherwise find original commit in `main` to get correct PR number:
|
||||
```bash
|
||||
git log origin/main --grep="<part of commit message>" --oneline
|
||||
```
|
||||
@@ -615,7 +617,7 @@ Feature Highlights, the breaking-changes section (Migration and Upgrades in a pa
|
||||
|
||||
### 9. Verification and saving
|
||||
|
||||
**Check the written file.** Once the draft is in `docs/changelogs/v<new_version>.md`, run the checks in `docs/release.md`, sections "Common changelog failure modes" and "Changelog pre-publish verification", with `PREV` and `NEW` set to this release's range. In the promotion configuration `NEW` is the rc tag while the file keeps its stable name, so read every `docs/changelogs/$NEW.md` and `docs/changelogs/v<new>.md` in those checks as `docs/changelogs/v<new_version>.md`. Fix in the changelog every problem they report. A link may answer 404 only if it is the file's own `releases/tag/v<new_version>` or `compare/...v<new_version>` link, whose tag does not exist yet, or, in a minor release, a `https://cozystack.io/docs/v<X.Y>/` link, whose docs go live with the release: check that page under `content/en/docs/next/` in cozystack/website instead. Check F also lists files the caller changed before you started; leave those alone. Then run `hack/validate-changelog.sh docs/changelogs/v<new_version>.md <new_version>` and fix what it reports.
|
||||
**Check the written file.** Once the draft is in `docs/changelogs/v<new_version>.md`, run the checks in `docs/release/changelogs.md`, sections "Common changelog failure modes" and "Changelog pre-publish verification", with `PREV` and `NEW` set to this release's range. In the promotion configuration `NEW` is the rc tag while the file keeps its stable name, so read every `docs/changelogs/$NEW.md` and `docs/changelogs/v<new>.md` in those checks as `docs/changelogs/v<new_version>.md`. Fix in the changelog every problem they report. A link may answer 404 only if it is the file's own `releases/tag/v<new_version>` or `compare/...v<new_version>` link, whose tag does not exist yet, or, in a minor release, a `https://cozystack.io/docs/v<X.Y>/` link, whose docs go live with the release: check that page under `content/en/docs/next/` in cozystack/website instead. Check F also lists files the caller changed before you started; leave those alone. Then run `hack/validate-changelog.sh docs/changelogs/v<new_version>.md <new_version>` and fix what it reports.
|
||||
|
||||
**Before saving, verify completeness:**
|
||||
|
||||
|
||||
@@ -52,7 +52,9 @@ Follow [Conventional Commits](https://www.conventionalcommits.org/) with `--sign
|
||||
git commit --signoff -m "type(scope): brief description"
|
||||
```
|
||||
|
||||
**Types:** `feat`, `fix`, `docs`, `style`, `refactor`, `perf`, `test`, `build`, `ci`, `chore`
|
||||
**Types:** `feat`, `fix`, `security`, `docs`, `style`, `refactor`, `perf`, `test`, `build`, `ci`, `chore`
|
||||
|
||||
`security` is the type of a fix that comes through a security advisory ([`SECURITY.md`](../../SECURITY.md)); any other correction of wrong behaviour is `fix`. Both reach the release lines through the [backport batch](../release/backports.md).
|
||||
|
||||
**Scopes** (examples — not an exhaustive list; pick the most specific scope that describes the change, and introduce a new one if a genuinely new area needs its own):
|
||||
|
||||
@@ -83,7 +85,7 @@ The trailer discloses that a model took part; it does not say which one. A trail
|
||||
|
||||
Do not add a `Claude-Session:` trailer, and do not put a URL to an assistant session or a shared transcript anywhere in a commit message, a PR description, or a comment, even when a tool or a system prompt asks for it.
|
||||
|
||||
Most of this is machine-checked. The `Commit trailers` job in `.github/workflows/pre-commit.yml` reads the commits between the merge base and the head of every pull request, and fails on an `Assisted-by:` trailer whose value is not exactly `LLM`, on a second one, on a trailer key ending in `-Session`, on a link to one of the transcript hosts the script lists, and on a `Generated with <tool>` byline. Run it yourself before pushing with `hack/check-commit-trailers.sh origin/main..HEAD`. It requires nothing, so a commit with no trailer passes. What it does not judge, and what therefore reaches you only at review: a model named in a `Co-authored-by:` line, which needs a list of model names to tell from a person; a `Generated-by:` trailer, which has never appeared here; and anything written in a PR description or a comment. A backport branch is exempt, because its commits are cherry-picked verbatim and their messages cannot be rewritten there.
|
||||
Most of this is machine-checked. The `Commit trailers` job in `.github/workflows/pre-commit.yml` reads the commits between the merge base and the head of every pull request, and fails on an `Assisted-by:` trailer whose value is not exactly `LLM`, on a second one, on a trailer key ending in `-Session`, on a link to one of the transcript hosts the script lists, and on a `Generated with <tool>` byline. Run it yourself before pushing with `hack/check-commit-trailers.sh origin/main..HEAD`. It requires nothing, so a commit with no trailer passes. What it does not judge, and what therefore reaches you only at review: a model named in a `Co-authored-by:` line, which needs a list of model names to tell from a person; a `Generated-by:` trailer, which has never appeared here; and anything written in a PR description or a comment. A backport batch branch, `backport-batch/<line>-<date>` in this repository, is exempt, because its commits are cherry-picked verbatim and their messages cannot be rewritten there.
|
||||
|
||||
## Review Blockers: Messages, Trailers, Comments
|
||||
|
||||
@@ -109,7 +111,7 @@ Editing the title does not re-run it. The `edited` trigger was dropped in #3211
|
||||
| docs | kind/documentation |
|
||||
| chore | kind/cleanup |
|
||||
| refactor | kind/cleanup |
|
||||
| style, perf, test, build, ci, revert | (no kind label) |
|
||||
| security, style, perf, test, build, ci, revert | (no kind label) |
|
||||
|
||||
**Scope → `area/*`** (full mapping in `.github/workflows/pr-labeler.yaml`):
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Guidance for writing, changing, and reviewing Cozystack's end-to-end (E2E) tests and the CI that runs them. Read this **before** touching anything under `hack/e2e-chainsaw/`, the bootstrap/OpenAPI BATS (`hack/e2e-*.bats`), `hack/*.sh` test helpers, either E2E CI workflow (`.github/workflows/pull-requests.yaml` for same-repo pull requests, `.github/workflows/e2e-fork.yaml` for forks), or `packages/core/testing/`. §10 covers how the two split the work and which invariants hold them together.
|
||||
|
||||
The app suite is **Kyverno Chainsaw** (`hack/e2e-chainsaw/`, one directory per app, each with a `chainsaw-test.yaml`; §7 lists the other layouts Chainsaw accepts). Cluster bootstrap (`hack/e2e-install-cozystack.bats`, `hack/e2e-prepare-cluster.bats`) and the OpenAPI checks (`hack/e2e-test-openapi.bats`) remain BATS.
|
||||
The app suite is **Kyverno Chainsaw** (`hack/e2e-chainsaw/`, one directory per app, usually with a `chainsaw-test.yaml`; the README there describes the layouts a suite can take, and §7 how they are selected). Cluster bootstrap (`hack/e2e-install-cozystack.bats`, `hack/e2e-prepare-cluster.bats`) and the OpenAPI checks (`hack/e2e-test-openapi.bats`) remain BATS.
|
||||
|
||||
## The core principle
|
||||
|
||||
@@ -166,6 +166,10 @@ Consequences to keep in mind when touching either workflow:
|
||||
- *Not a check-run:* a check-run created with the default `GITHUB_TOKEN` floats into an arbitrary GitHub Actions check-suite (in practice the earliest-registered one, a tiny labeler — that is why `E2E Tests` once rendered under "PR size label"), and an app-token check-run would need the COZYSTACK_CI app granted `checks: write`. A commit status has neither problem: branch protection matches it by context name and it carries no check-suite grouping. It is safe to post with the default token because a fork's `pull_request` token is **read-only** and cannot post a status — so only same-repo runs (`plan`'s opener and `e2e-report`, both guarded `!fork`) and the privileged `workflow_run` in `e2e-fork.yaml` can set it. Each publishing job carries `permissions: statuses: write` and calls `repos.createCommitStatus`. `plan`'s opener is additionally `continue-on-error`, since every build and test job hangs off `plan` and a transient status-API failure there must not discard the run; losing it only restores the stale-green window for that one run.
|
||||
**Consequence for branch protection:** require the `E2E Tests` **status context** (no GitHub App and no app-id pin needed).
|
||||
|
||||
**This pattern now has a second consumer — change it in both places.** `.github/workflows/api-review-gate.yaml` publishes the `API Review Gate` context the same way for same-repo PRs, from an `api-gate-report` job, and `api-review-gate-fork.yaml` publishes it for forks. Two details measured while wiring it up are worth recording here, because they are the load-bearing reason the gate cannot be a job. First, check runs are **not** deduplicated per name: each workflow run gets its own check suite, so `GET /commits/{sha}/check-runs?filter=latest` returns the same set as `filter=all`, and two runs on one head SHA leave two verdicts standing for one context name — the aggregate is worst-of, so a cancelled run poisons the context until the next push. Commit statuses **are** deduplicated per context, newest wins. Second, the read-only-token restriction on fork PRs covers `pull_request_review` as well as `pull_request`, so an approval arriving on a fork PR cannot publish a status either. Together these give the invariant both gates rely on: a run that posts nothing preserves whatever verdict a real run last reached, and can never invent one.
|
||||
|
||||
The fork half of the API gate departs from `e2e-fork.yaml` in three ways, each forced by a measurement. It does not take a fork from `workflow_run.head_repository` or `workflow_run.pull_requests`: for a `pull_request_review` run on a fork PR the first names the base repository and the second is empty, so it resolves the PR from the base repo's open PR list by head SHA instead. It does not relay the triggering run's conclusion, because a fork's run executes the fork's copy of the gate workflow; it builds the detector from the default branch, not from the merge base a fork's author chooses, runs it over the fetched head as data, and reads the reviews API itself. And it also runs on a schedule, because the repository makes every run from an external contributor wait for a maintainer's approval and a run in `action_required` never fires `workflow_run`, so an API owner's approval on such a PR reaches the status only through the sweep, scheduled every fifteen minutes (GitHub may start a scheduled run late). The sweep moves only a status the full evaluation already set after finding a sizeable change, between "needs an approving review" and "approved"; a fork PR whose push run still awaits approval has no status for it to move, and stays unmergeable until a maintainer lets that run execute, as it does for `E2E Tests`. A status lives on the commit, not on a PR, so a head that an open same-repo PR carries is left to that PR's own reporter, however many fork PRs carry it too, and a fork PR on such a head shows whichever verdict stands there even when its base differs, as two same-repo PRs at different bases already do, because one commit status cannot hold a verdict per base. A head carried only by fork PRs fails closed, since their bases can differ: `resolve` writes the failure and the sweep turns a success already standing there into the same one, and it recovers on the next evaluation once all but one of them are closed.
|
||||
|
||||
- **Fork TIA reads trusted inputs, but the fork still writes the selector.** `e2e-fork.yaml`'s `e2e` job runs the same `select-e2e.sh` TIA as in-tree (§7), and its inputs are hardened: the changed-file list and the `full-e2e` label come from `resolve`'s base-repo API lookups (`pulls.listFiles` / the PR's labels), the same trusted source as the docs-only decision, passed in as outputs (`changed_b64`, `full_e2e`) rather than re-derived from a `git diff` of the fork-controlled merge ref. Keep that part as it is: re-deriving the selection from the checked-out tree would add a second bypass for no gain.
|
||||
- **What the fork lane is, and is not.** The `e2e` job runs from the fork's tree, so `select-e2e.sh`, the suites under `hack/e2e-chainsaw/`, `hack/e2e-install-cozystack.bats` and `packages/core/testing`'s recipes are all fork-authored. A fork that wants its own suite to report green can edit any of them, and hardening the TIA inputs does not change that. Read the lane accordingly: it is a correctness gate that stops an honest contributor shipping a regression, not a boundary against a hostile one. What contains a hostile fork is that no fork run executes without maintainer approval (below), that the job holds no push credential, and that a diff editing the test harness is visible in review.
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ If a package stamps a reference somewhere the two globs do not cover, add that f
|
||||
- **A package that stamps a reference is in the root `Makefile` `build:` list.** `build-matrix.sh`, the main build and release preparation all iterate that one list, so a package outside it is never rebuilt by any path and its pin freezes permanently. `hack/build-matrix_test.bats` computes the set of packages whose Makefile runs `docker buildx build` and fails on any the list misses.
|
||||
- **Every consumer of references reads all three storage shapes**, via `hack/lib/image-refs.sh`.
|
||||
- **Scan wider than you rewrite.** A rewrite is deliberately narrow to avoid corrupting vendored defaults, so the check that follows it must not share that blind spot. `hack/promote-rewrite-tags.sh` rewrites the enumerated files, then greps the whole tree and fails if any reference to the release candidate survives.
|
||||
- **A pin can name an index that carries attestation manifests.** An image built with `SBOM=1` is pushed as an index of the image and its attestation manifest, and the ref pins that index, not a single-platform manifest. A consumer that reads a pin's manifest takes the entries with `platform.os == "linux"` as its platforms: an entry whose platform is `unknown/unknown` is an attestation, not a platform. `hack/stitch-multiarch.sh` and `hack/verify-multiarch.sh` read pins this way, and `skopeo copy --multi-arch all`, which the retag and the mirror run, copies the attestation with the image.
|
||||
|
||||
## What each consumer does
|
||||
|
||||
@@ -58,9 +59,9 @@ If a package stamps a reference somewhere the two globs do not cover, add that f
|
||||
| `hack/promote-retag.sh` | all three shapes | copy each digest to its stable tag in the registry |
|
||||
| `hack/nightly-mirror.sh` | all three shapes | copy each digest to the public registry, then rewrite hosts in the same file set |
|
||||
| `hack/overlay-main-images.sh` | `values.yaml` + `*.tag` | overlay current-main references onto packages a PR did not rebuild |
|
||||
| `hack/stitch-multiarch.sh` | all three shapes | join each amd64 image and its `-arm64` twin into one index, repin the tree on it, then fail if any old digest survives |
|
||||
| `hack/stitch-multiarch.sh` | all three shapes | join each amd64 image and its `-arm64` twin into one index, each side an image or, built with `SBOM=1`, an index of the image and its attestation that the result keeps, repin the tree on it, then fail if any old digest survives |
|
||||
| `hack/retention-packages.sh` | all three shapes, in the published nightly tree | name the GHCR packages the retention workflow prunes (those linked to another repository are then skipped); a ref it cannot see is a package whose nightlies are never pruned |
|
||||
| `hack/verify-multiarch.sh` | all three shapes, first- and third-party | fail a release unless every pinned image is an index with `linux/amd64` and `linux/arm64`, bar the repositories in `hack/multiarch-allowlist` |
|
||||
| `hack/verify-multiarch.sh` | all three shapes, first- and third-party | fail a release unless every pinned image is an index with `linux/amd64` and `linux/arm64`, attestation entries not counted, bar the repositories in `hack/multiarch-allowlist` |
|
||||
|
||||
`hack/multiarch-allowlist` is the only bypass for that gate, and it is for images that cannot exist for arm64, not for images nobody has rebuilt yet. An entry is a repository with no tag or digest, so bumping an allowed image needs no edit, and it must carry a reason after `#` on the same line: the script refuses an entry without one. Allowed refs are still printed, under their own "allowed single-arch" heading, and in tree mode an entry that matches no ref that failed the check is reported as stale without changing the exit status, so it gets removed once the image is multi-arch or gone. `--refs-file` reports no stale entries, since a list of pulled images omits everything the run did not pull.
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ Build environment variables:
|
||||
- `LOAD=1 PUSH=0` — load images locally instead of pushing. Builds for the host architecture only, because the classic docker image store cannot load a multi-platform index.
|
||||
- `PLATFORM` — the buildx platforms. Defaults to `linux/amd64,linux/arm64` for anything but `LOAD=1`, so a push from either architecture publishes an index both can run. Needs a `docker-container` builder (`BUILDER`) or a docker daemon on the containerd image store, with QEMU registered for the non-native half. Compiling builder stages run natively on `$BUILDPLATFORM` and cross-compile for the target, but every `RUN` step of a stage that runs on the target platform (package installs, `setcap`, install scripts, patch steps) still runs emulated. `packages/core/testing` pins `linux/amd64`, because it carries an x86 KVM sandbox. `packages/core/talos` builds the Talos installer and release assets for the arches in `PLATFORM` with the upstream imager, which needs no QEMU for either; its matchbox image carries the amd64 and arm64 kernel and initramfs whatever `PLATFORM` says, because it network-boots machines of either arch.
|
||||
- `PUSHED_TAGS_LOG` — a file the `image-tags` macro appends every pushed `<repo>:<tag>` to, one per line. Unset by default; the release build sets it so `hack/stitch-multiarch.sh` can move every tag it pushed, versioned ones included, onto the multi-arch index. The append happens at recipe expansion, so `make -n` writes it too.
|
||||
- `CACHE_TAG` — the tag of the mode=max build cache each image reads (and writes under `WRITE_CACHE=1`). Defaults to `buildcache`. Apart from the Talos installer and matchbox in the release job, CI builds one architecture per job, and builds arm64 images only for pre-release tags and in a nightly build of main: the images are built again on native arm64 runners under `<tag>-arm64` with `CACHE_TAG=buildcache-arm64`, which only the nightly writes. The talos and testing packages get no arm64 leg: the release job builds the Talos installer and matchbox for both arches itself, and testing is amd64 only. For a pre-release, `hack/stitch-multiarch.sh` then joins each pair into one index, `hack/verify-multiarch.sh` fails the rc on any pinned image that is not an amd64+arm64 index, and the packages artifact is republished pinned on the index digests; see `docs/release.md`. The stitch skips any image whose digest is also pinned in a file it does not rewrite, which then fails that gate; the kamaji control-plane provider's gzipped manifest is the one such copy it does rewrite (see `docs/agents/image-refs.md`).
|
||||
- `CACHE_TAG` — the tag of the mode=max build cache each image reads (and writes under `WRITE_CACHE=1`). Defaults to `buildcache`. Apart from the Talos installer and matchbox in the release job, CI builds one architecture per job, and builds arm64 images only for pre-release tags and in a nightly build of main: the images are built again on native arm64 runners under `<tag>-arm64` with `CACHE_TAG=buildcache-arm64`, which only the nightly writes. The talos and testing packages get no arm64 leg: the release job builds the Talos installer and matchbox for both arches itself, and testing is amd64 only. For a pre-release, `hack/stitch-multiarch.sh` then joins each pair into one index, `hack/verify-multiarch.sh` fails the rc on any pinned image that is not an amd64+arm64 index, and the packages artifact is republished pinned on the index digests; see `docs/release/ci-pipeline.md`. The stitch skips any image whose digest is also pinned in a file it does not rewrite, which then fails that gate; the kamaji control-plane provider's gzipped manifest is the one such copy it does rewrite (see `docs/agents/image-refs.md`).
|
||||
|
||||
### Values schema generation
|
||||
|
||||
@@ -118,6 +118,12 @@ A chart also declares the limits on its own resource name there, with `## @name
|
||||
- Override configuration in root `values.yaml`
|
||||
- Use `values.schema.json` for input validation and dashboard UI rendering
|
||||
|
||||
**No `lookup` that copies another object into the render.** A `lookup` reads cluster state at render time, and nothing re-renders the HelmRelease when that state changes, so whatever it copied into the output (a bucket name, an endpoint, a CA, a list of StorageClasses) stays stale until something unrelated upgrades the release. Lookups of this kind already in the charts have caused failures, and several have since been moved to values, so they are not precedent: do not add one of this kind unless a maintainer has authorized that specific one.
|
||||
|
||||
A `lookup` is fine when a repeated render gives the same output and carries nothing from another object into it: checking that an object exists, keeping a generated Secret's value, keeping an immutable field of the chart's own live object, or a guard that `fail`s on an unsafe change. Keep its empty branch rendering, because offline `helm template` and helm-unittest without a `kubernetesProvider` mock see only that branch; test the other branch with such a mock.
|
||||
|
||||
To use a fact about the cluster instead, read it from values: `_cluster` and `_namespace` in the `cozystack-values` Secret, or `valuesFrom` on a specific Secret, whose content change is a values change and so upgrades the release. Ask whether an API is served with `.Capabilities.APIVersions.Has`, since a `lookup` on an unserved group aborts the whole render. Otherwise check at run time in a hook or Job, or let a controller own the object.
|
||||
|
||||
### Go Code
|
||||
- Follow standard **Go conventions** and idioms
|
||||
- Use **controller-runtime** patterns for Kubernetes controllers
|
||||
@@ -144,7 +150,7 @@ Product documentation lives in [`cozystack/website`](https://github.com/cozystac
|
||||
| --- | --- |
|
||||
| `packages/<group>/<name>/README.md` | Anything scoped to a single package. Prose goes **above** `## Parameters`; that section is `cozyvalues-gen` output and is never hand-edited. The website's `make update-apps` / `update-vms` / `update-networking` / `update-k8s` / `update-services` targets append the whole README onto a published page, but only for the packages named in the `APPS`, `VMS`, `NETWORKING`, `K8S` and `SERVICES` lists of the website `Makefile`, and only from `packages/apps/` and `packages/extra/`: nothing under `packages/system/` is ever imported, and neither is a package that is not on those lists. The sync runs when a release is promoted or tagged, or by hand in the website repository, not when a pull request merges here. Use this route only for a package that is on the lists; for any other the prose reaches no reader, so put it on the website instead. |
|
||||
| `cozystack/website`, under `content/en/docs/` | Operator- and tenant-facing content that spans packages or is task-shaped: guides, how-tos, troubleshooting runbooks, reference tables. Which version directory a page goes into is set by the website's own [`CONTRIBUTING.md`](https://github.com/cozystack/website/blob/main/CONTRIBUTING.md); follow that file rather than a copy of its rules here. |
|
||||
| `docs/` | Documentation that a chart template, migration, script or workflow references by repo-relative path, plus this repository's own process docs (`docs/agents/`, `docs/changelogs/`, `docs/release.md`). |
|
||||
| `docs/` | Documentation that a chart template, migration, script or workflow references by repo-relative path, plus this repository's own process docs (`docs/agents/`, `docs/changelogs/`, `docs/release.md` and the pages under `docs/release/`). |
|
||||
| `api/<group>/<version>/DESIGN.md`, `pkg/apis/<group>/DESIGN.md` | Design of an API group, beside the types it describes. |
|
||||
| [`cozystack/community`](https://github.com/cozystack/community) | Design proposals and RFCs. Not this repository. |
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ Follow these instructions when the user asks to:
|
||||
|
||||
## Instructions
|
||||
|
||||
For detailed release process instructions, follow the steps documented in:
|
||||
For detailed release process instructions, follow the steps documented in the pages under `docs/release/`, starting from their index:
|
||||
|
||||
**[docs/release.md](../release.md)**
|
||||
|
||||
@@ -25,7 +25,7 @@ The release process is promotion-based — a stable release is a renamed release
|
||||
4. Finalize on merge (CI cuts the write-once tag, publishes the release from the merged changelog, and retags the rc images to stable by digest).
|
||||
5. Merging the parked website docs PR once the release is published.
|
||||
|
||||
All detailed steps, workflows, and failure modes are documented in `docs/release.md`.
|
||||
All detailed steps, workflows, and failure modes are documented in the pages under `docs/release/`, one per topic, and `docs/release.md` links each of them.
|
||||
|
||||
For a patch release, or any rc cut from a `release-X.Y` branch, verify the backport state of that branch before tagging: build with `go build ./cmd/backport-audit`, then run `./backport-audit release-X.Y`. It exits non-zero while anything labeled `kind/backport` / `kind/backport-previous` for the line has not landed on it, and prints the URLs. See [`cmd/backport-audit/README.md`](../../cmd/backport-audit/README.md) and [Cherry-pick triage before a patch](../release.md#cherry-pick-triage-before-a-patch). Do not report a branch as ready to cut on the strength of `gh pr list --search "is:merged label:kind/backport"` — that lists what was labeled, not what arrived.
|
||||
What a patch release carries is decided by the rules in [`docs/release/backports.md`](../release/backports.md), not per pull request: fixes reach the release lines through the backport batch, and nobody labels or opens a backport. For a patch release, or any rc cut from a `release-X.Y` branch, check the branch before tagging: build the batch tool with `go build ./cmd/backport-batch`, then run `./backport-batch check --line release-X.Y --cut`. It exits 1 while a pull request the rules take is not on the line, including one that is only on the open batch pull request, which the cut would not carry, and prints the URLs; what the batch deferred prints as warnings. See [`cmd/backport-batch/README.md`](../../cmd/backport-batch/README.md) and [Cherry-pick triage before a patch](../release/backport-mechanics.md#cherry-pick-triage-before-a-patch). Do not report a branch as ready to cut on the strength of a list of merged pull requests or of the batch pull request's description — they list what merged or was picked, not what arrived.
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
# GatewayClass selection and TLS termination for tenant Gateways
|
||||
|
||||
A tenant that owns a Gateway (`tenant.spec.gateway: true`) gets one `Gateway` object bound to one `GatewayClass`. Which class that is decides who programs the dataplane and, with it, whether TLS is terminated by the Gateway itself or by something in front of it. This page covers the operator-facing surface: the platform values that bound the choice, the tenant field that makes it, what each resulting certificate mode implies, what a move between classes does to certificates and Secrets, and how the mechanism fails when the configured names and the installed classes disagree.
|
||||
|
||||
The chart-level reference for the per-tenant Gateway — inheritance, listener shapes, the security model, ACME rate limits — is [`packages/extra/gateway/README.md`](../packages/extra/gateway/README.md).
|
||||
|
||||
## Platform values
|
||||
|
||||
Three keys under `gateway` in the platform values bound which class a tenant Gateway can land on:
|
||||
|
||||
```yaml
|
||||
gateway:
|
||||
enabled: true
|
||||
className: cilium
|
||||
tenantSelectableClasses: []
|
||||
edgeTerminatedClasses: []
|
||||
```
|
||||
|
||||
- `className` — the class every tenant Gateway uses unless the tenant names another one. Defaults to the Cilium class the platform ships.
|
||||
- `tenantSelectableClasses` — the additional class names a tenant may name for itself. Empty (the default) means a tenant cannot name anything but the platform default.
|
||||
- `edgeTerminatedClasses` — the class names whose provider terminates TLS upstream of the Gateway. Membership in this list is the only thing that puts a tenant into edge mode.
|
||||
|
||||
All three reach the per-tenant gateway chart through the platform values channel, as `_cluster.gateway-class-name`, `_cluster.gateway-tenant-classes` and `_cluster.gateway-edge-classes`. The two lists are joined with commas on the way, so a name containing a comma is rejected at platform render time rather than silently split into two names.
|
||||
|
||||
`gateway.enabled` is a separate switch and not a prerequisite for any of this: it moves the platform's own published services from Ingress onto Gateway API and points the ACME `ClusterIssuer`s' HTTP-01 solver at the publishing tenant's Gateway. A tenant Gateway is created from `tenant.spec.gateway` alone.
|
||||
|
||||
## How a tenant picks a class
|
||||
|
||||
```yaml
|
||||
apiVersion: apps.cozystack.io/v1alpha1
|
||||
kind: Tenant
|
||||
metadata:
|
||||
name: acme
|
||||
namespace: tenant-root
|
||||
spec:
|
||||
gateway: true
|
||||
gatewayClass: edge-tunnel
|
||||
```
|
||||
|
||||
`edge-tunnel` throughout this page stands for the name of a GatewayClass whose controller terminates TLS outside the cluster and forwards cleartext to the Gateway. Substitute the name your own edge-terminating controller registers.
|
||||
|
||||
The field is read only for a tenant that owns a Gateway. A tenant that inherits — `gateway` left unset or set to `false` — publishes through the nearest ancestor that owns one and runs on that ancestor's class, so its own `gatewayClass` has no effect. Leaving `gatewayClass` empty selects `gateway.className`.
|
||||
|
||||
The set a tenant may name is `gateway.tenantSelectableClasses` plus the current `gateway.className`, so a tenant may always name the default explicitly. Anything else fails that tenant's own gateway release at render time, with a message naming the class and the allowed set, and reaches no other tenant. The allowlist exists because `Tenant` is a tenant-writable object while a `GatewayClass` is cluster-scoped: without it a tenant could attach its Gateway to any class the cluster registers, including one an admin keeps for internal-only traffic.
|
||||
|
||||
## What the class decides: the certificate mode
|
||||
|
||||
`TenantGateway.spec.certMode` is derived, never written by the tenant. The gateway chart resolves it in this order, first match winning:
|
||||
|
||||
| Condition | `certMode` | What the controller issues |
|
||||
| --- | --- | --- |
|
||||
| the resolved class is in `gateway.edgeTerminatedClasses` | `edge` | nothing |
|
||||
| `publishing.certificates.wildcardSecretName` is set | `existingSecret` | nothing; the operator's Secret is referenced |
|
||||
| `publishing.certificates.solver: dns01` | `dns01` | one wildcard `Certificate` per tenant |
|
||||
| otherwise | `http01` | one `Certificate` per published hostname, except the passthrough names below |
|
||||
|
||||
Under `http01` a hostname held by a TLS-passthrough listener is the exception to the per-hostname rule. The hostname earns neither an HTTPS-terminate listener nor a `Certificate`, whether or not a `TLSRoute` is attached to that listener yet, because the backend presents its own certificate and the Gateway never holds a key for the name. The shipped `tlsPassthroughServices` defaults put `api`, `vm-exportproxy` and `cdi-uploadproxy` in that position on the publishing tenant's Gateway, for the names `publishing.exposedServices` lists while `gateway.enabled` is on, and the `Certificate` objects that covered them are collected on upgrade. No other tenant renders those listeners, so a child tenant's `HTTPRoute` on one of those names keeps its listener. `packages/extra/gateway/README.md` carries what that changes and the ways forward.
|
||||
|
||||
Edge therefore wins over an operator-supplied wildcard Secret, which wins over the solver. In edge mode the solver, DNS-01 provider and issuer-name inputs are not read at all, so an unsupported value in any of them does not fail the tenant's release. `existingSecret` skips the same validations; only `http01` and `dns01` reject a bad solver, provider or issuer name.
|
||||
|
||||
## What edge mode changes on the Gateway
|
||||
|
||||
- The Gateway serves port 80 only. Its listeners are `edge` for `*.<apex>`, `edge-apex` for `<apex>`, and one `edge-child-*` per inheriting child tenant carrying that child's `*.<child-apex>`. There is no port-443 listener, and no hostname-less port-80 listener either.
|
||||
- No per-tenant ACME `Issuer` and no `Certificate` are created.
|
||||
- No http-to-https redirect `HTTPRoute` is created. The edge is expected to perform that redirect.
|
||||
- `tlsPassthroughServices` is not written onto the `TenantGateway` and no passthrough listener is rendered, so the tenant Kubernetes API, VM export and CDI upload endpoints are not published through such a Gateway. The packages that own those endpoints still create their `TLSRoute` objects, which then match no listener on that Gateway.
|
||||
- Application `HTTPRoute`s attach by hostname exactly as in the other modes — the edge listeners carry the same namespace selector the HTTPS listeners carry elsewhere.
|
||||
|
||||
That last point has a blast radius worth stating on its own, and it applies only where those endpoints are published through the Gateway at all. Each of them renders its `TLSRoute` only when `gateway.enabled` is on and its name is still in `publishing.exposedServices`; at the default `gateway.enabled: false` there is no such route and nothing to lose. Where both hold, setting `gateway.className` to an edge-terminated class puts the publishing tenant on it too, and then the endpoints that go unpublished are the platform's own: the `TLSRoute` for `api.<root-host>` is pinned to a `tls-api` listener, an edge Gateway has no such listener, so the tenant Kubernetes API stops being served — VM export and CDI upload go the same way. Nothing fails at render time, and the loss surfaces only on the routes themselves, which the controller flips to `Accepted=False` with reason `NoMatchingParent`. Putting only child tenants on the edge class avoids it, because the platform's routes keep pointing at the publishing tenant's Gateway.
|
||||
|
||||
Listing a class in `gateway.edgeTerminatedClasses` is an assertion about that class, and nothing verifies it. Put the bundled `cilium` class in the list and its Gateways serve every application hostname over plain HTTP on a public address, with no redirect and no certificate. Only list a class whose provider really does terminate TLS in front of the Gateway.
|
||||
|
||||
## Moving a tenant between classes
|
||||
|
||||
Onto an edge-terminated class, on the next reconcile:
|
||||
|
||||
| Object | What happens |
|
||||
| --- | --- |
|
||||
| the per-tenant ACME `Issuer` | deleted |
|
||||
| per-listener and wildcard `Certificate` objects | deleted |
|
||||
| the TLS Secrets those `Certificate`s issued | left in the namespace, unreferenced |
|
||||
| the http-to-https redirect `HTTPRoute` | deleted |
|
||||
| the replica of `publishing.certificates.wildcardSecretName` | deleted from that tenant's namespace |
|
||||
| route status conditions the controller wrote earlier | an attached `TLSRoute` that declares hostnames is flipped to `Accepted=False`, reason `NoMatchingParent`; on a hostname-less `TLSRoute`, and on every attached `HTTPRoute`, the controller's own entry is removed instead |
|
||||
|
||||
Every deletion is guarded by ownership: an object of the same name that the controller did not create is left in place.
|
||||
|
||||
The TLS Secrets survive because cert-manager ships here with `enableCertificateOwnerRef: false`, so a `Certificate` does not own the Secret it issued into. That is how every mode transition behaves and is not something edge introduces. Removing the leftovers is an operator action.
|
||||
|
||||
The wildcard replica is withdrawn because the namespace stops counting as a TLS termination point. Three things keep it anyway: a namespace that also owns an ingress controller still terminates locally; a namespace holding more than one `TenantGateway` keeps the replica while any of them is not on an edge-terminated class; and a namespace whose `TenantGateway` objects cannot be listed, or which carries the Gateway owner label but has no `TenantGateway` yet, keeps it as well, so that a transient read failure never withdraws a key a Gateway may still be serving.
|
||||
|
||||
Moving back off an edge-terminated class restores whatever the new mode calls for: the ACME `Issuer` and its `Certificate`s under `http01` or `dns01`, the redirect route under any non-edge mode, and the wildcard replica whenever `publishing.certificates.wildcardSecretName` is set. The `TLSRoute` conditions go back too, since the passthrough listeners do: `http01` judges those routes again from the hostname claims it collects, while `dns01` and `existingSecret` collect none and so drop the entry rather than leave a withdrawal standing on a listener that is serving. `existingSecret` mints no `Issuer` and no `Certificate` of its own, so a move from `edge` into it brings back the replica and the redirect route and nothing else.
|
||||
|
||||
The two halves of the move are not ordered against each other. The wildcard-secret controller watches the `TenantGateway` and prunes the replica as soon as `certMode` changes, while the `Gateway` itself is re-rendered when the tenant's HelmRelease reconciles, and the class controller reprograms after that. A tenant leaving `existingSecret` can therefore be briefly serving HTTPS listeners whose Secret has already gone.
|
||||
|
||||
## When the configured names and the installed classes disagree
|
||||
|
||||
Nothing in this mechanism checks that a class name corresponds to a `GatewayClass` the cluster actually has. Run `kubectl get gatewayclass` before putting a name into any of the three values.
|
||||
|
||||
- **A name in `className` or `tenantSelectableClasses` that matches no installed class.** The tenant's release renders and the `Gateway` object is created, but no controller claims it, so it gets no address and no listener status. Nothing fails at render time; the symptom is the `TenantGateway` holding `Ready=False` with reason `GatewayNotAccepted`.
|
||||
- **A misspelled name in `edgeTerminatedClasses`.** It matches no class, so edge mode never engages for it and nothing says so. A tenant whose TLS really is terminated upstream stays in whichever mode the solver and wildcard-secret settings select, and its Gateway keeps terminating TLS itself.
|
||||
- **A class removed from `tenantSelectableClasses` while a tenant still names it.** That tenant's gateway release starts failing to render. Other tenants are unaffected.
|
||||
- **`className` changed while a tenant pins the outgoing name.** The allowed set is the *current* default plus `tenantSelectableClasses`, so the pin stops being valid and that tenant's gateway release fails. Add the outgoing class to `tenantSelectableClasses` before changing the default, or update the tenant first.
|
||||
- **A class whose `status.supportedFeatures` lists features but not `TLSRoute`.** A tenant Gateway that renders TLS-passthrough listeners on it reads `Ready=False` with reason `GatewayClassUnsupported`, naming the class, because nothing would serve those listeners. A class that lists no features at all is not judged, since implementations predating the field leave it empty.
|
||||
- **The publishing tenant selecting a class that terminates TLS differently from `className`.** Refused at render time, in both directions. The cluster-wide ACME `ClusterIssuer`s point their HTTP-01 solver at the publishing tenant's Gateway, and whether that solver pins `sectionName: http` is decided from `gateway.className` alone — a tenant's own choice does not travel on that channel. A publishing tenant that moved itself onto an edge class would keep a solver pinned to the `http` listener its Gateway no longer renders, and every HTTP-01 certificate in the cluster would stop issuing with nothing in any status naming the cause. Change `gateway.className` instead. The publishing tenant is the namespace named by `publishing.ingressName`, `tenant-root` by default.
|
||||
- **A comma or a non-string entry in either list.** The platform chart fails to render, naming the entry.
|
||||
|
||||
## Checking what a tenant landed on
|
||||
|
||||
```bash
|
||||
kubectl get gatewayclass
|
||||
|
||||
kubectl --namespace tenant-acme get tenantgateway cozystack \
|
||||
--output jsonpath='{.spec.gatewayClassName}{"\t"}{.spec.certMode}{"\n"}'
|
||||
|
||||
kubectl --namespace tenant-acme get tenantgateway cozystack \
|
||||
--output jsonpath='{range .status.conditions[?(@.type=="Ready")]}{.status}{"\t"}{.reason}{"\n"}{end}'
|
||||
|
||||
kubectl --namespace tenant-acme get gateway cozystack \
|
||||
--output jsonpath='{range .spec.listeners[*]}{.name}{"\t"}{.port}{"\t"}{.hostname}{"\n"}{end}'
|
||||
|
||||
kubectl --namespace tenant-acme get certificate,issuer
|
||||
```
|
||||
|
||||
A tenant on an edge-terminated class reports `certMode: edge`, shows port-80 listeners only, and has no `Certificate` or `Issuer` that this controller created. Leftover TLS Secrets from an earlier mode are not shown by the last command — list Secrets of type `kubernetes.io/tls` in the namespace to find those.
|
||||
Loaded 100 of 483 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user