diff --git a/.github/workflows/publish-apt.yml b/.github/workflows/publish-apt.yml index 9544b46..e77db51 100644 --- a/.github/workflows/publish-apt.yml +++ b/.github/workflows/publish-apt.yml @@ -2,6 +2,16 @@ name: Publish apt repository to R2 on: workflow_dispatch: + inputs: + repositories: + description: >- + Optional. Comma-separated GitHub repository URLs to build, e.g. + https://github.com/zitzhen/patchsplit,https://github.com/zitzhen/GitTune + Every URL must already be listed in Software.list, otherwise nothing is + built. Leave empty to build every listed repository. + required: false + default: '' + type: string permissions: contents: read @@ -21,15 +31,34 @@ jobs: runs-on: ubuntu-latest outputs: packages: ${{ steps.parse.outputs.packages }} + filtered: ${{ steps.parse.outputs.filtered }} steps: - name: Check out repository uses: actions/checkout@v4 - name: Build the package matrix + # The optional `repositories` input narrows the matrix to a hand-typed + # subset of Software.list. The input is checked, not trusted: a URL that + # is not listed fails this step, so an unlisted (or mistyped) repository + # never reaches the build jobs. id: parse + env: + REPOSITORIES: ${{ inputs.repositories }} run: | - packages="$(./scripts/parse-software-list.sh Software.list)" + packages="$(./scripts/select-software.sh Software.list "$REPOSITORIES")" echo "packages=$packages" >> "$GITHUB_OUTPUT" + # Whether this run is a partial rebuild is decided by comparing the + # selected matrix with the whole manifest, not by looking at the raw + # input: a value that selects every entry (or only separators) still + # publishes everything, and only a real subset needs the publish job to + # restore the packages it did not rebuild. + all_packages="$(./scripts/parse-software-list.sh Software.list)" + if [[ "$packages" == "$all_packages" ]]; then + echo "filtered=false" >> "$GITHUB_OUTPUT" + else + echo "filtered=true" >> "$GITHUB_OUTPUT" + echo "Partial rebuild: publishing will keep the packages not rebuilt" + fi echo "Build matrix: $packages" build: @@ -78,7 +107,10 @@ jobs: publish: name: Sign and publish to R2 - needs: build + # prepare is listed explicitly, not just reached through build, so that + # needs.prepare.outputs.filtered is available here without relying on + # transitive needs. + needs: [prepare, build] runs-on: ubuntu-latest env: APT_GPG_KEY_ID: ${{ vars.APT_GPG_KEY_ID || '3B310141DFDBDE77A802C0627DFBD414F81DC0BB' }} @@ -109,6 +141,38 @@ jobs: pattern: build-log-* merge-multiple: true + - name: Restore the packages already published + # A run that rebuilds only a subset of the manifest must still publish an + # index that contains every package: publish-repo.sh regenerates Packages + # from the artifacts directory alone, so without this the packages that + # were not rebuilt would disappear from the index until the next full run. + # Only runs when the input filtered the matrix, so a full run keeps + # behaving exactly as before and never re-downloads anything. + if: needs.prepare.outputs.filtered == 'true' + env: + AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} + AWS_REGION: auto + R2_ACCOUNT_ID: ${{ secrets.R2_ACCOUNT_ID }} + R2_BUCKET: ${{ secrets.R2_BUCKET }} + run: | + : "${AWS_ACCESS_KEY_ID:?AWS_ACCESS_KEY_ID is required}" + : "${AWS_SECRET_ACCESS_KEY:?AWS_SECRET_ACCESS_KEY is required}" + : "${R2_ACCOUNT_ID:?R2_ACCOUNT_ID is required}" + : "${R2_BUCKET:?R2_BUCKET is required}" + endpoint="https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com" + mkdir -p "$RUNNER_TEMP/published" + # Only the pool is downloaded: everything in the index comes from there, + # and the generated metadata is rebuilt from scratch by publish-repo.sh. + aws s3 sync "s3://${R2_BUCKET}/pool" "$RUNNER_TEMP/published/pool" \ + --endpoint-url "$endpoint" --region auto --no-progress \ + --exclude '*' --include '*.deb' + # Fold the published .deb files into the artifacts directory so they are + # re-indexed alongside the packages this run rebuilt. The script keeps + # only the newest published version of each package and skips any + # package built by this run, so a rebuild still supersedes the old .deb. + ./scripts/merge-artifacts.sh "$RUNNER_TEMP/packages" "$RUNNER_TEMP/published/pool" + - name: Import the signing key env: APT_GPG_PRIVATE_KEY: ${{ secrets.APT_GPG_PRIVATE_KEY }} diff --git a/.github/workflows/script-checks.yml b/.github/workflows/script-checks.yml index 4b363f2..c47cbcd 100644 --- a/.github/workflows/script-checks.yml +++ b/.github/workflows/script-checks.yml @@ -277,6 +277,12 @@ jobs: # builds into exit 1, which skipped publish on every run. run: ./scripts/test-build-deb-exit-status.sh + - name: Test the optional repository filter + # The workflow input is hand-typed, so it is validated: an unlisted URL + # must stop the run, and a filtered publish must still index the packages + # it did not rebuild. + run: ./scripts/test-repository-filter.sh + - name: Check build-deb.sh survives a failing build # A failing build must still leave a usable log and must not abort the # cleanup with "unbound variable" (that regressed once). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 60d1f34..10f5686 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -73,6 +73,26 @@ Packages are not published by a pull request alone. After a change is merged, a maintainer runs the **Publish apt repository to R2** workflow manually from the Actions tab. +That workflow takes an optional `repositories` input: a comma-separated list of +repository URLs, which limits the run to those packages. Every URL must already +be in `Software.list` — `scripts/select-software.sh` fails the run otherwise, and +the run then builds nothing. Leaving the input empty builds the whole manifest. +The publish job re-indexes the packages already in the bucket alongside the ones +just built (`scripts/merge-artifacts.sh`), so a filtered run never drops another +package from the apt index. Validate a filter locally before triggering a run: + +```bash +# Every listed entry (the empty input) +./scripts/select-software.sh Software.list + +# A subset; mixed or full-width separators are accepted and normalised +./scripts/select-software.sh Software.list \ + "https://github.com/zitzhen/patchsplit, https://github.com/zitzhen/GitTune" + +# Unlisted URLs fail on purpose +./scripts/select-software.sh Software.list https://github.com/not/listed +``` + ## Local validation Validate the manifest and, when changing the build pipeline, try a local build: @@ -118,6 +138,15 @@ from the workflow and runs it under bash for a given matrix URL: The `Script checks` workflow runs this for every `Software.list` entry, so a mistake here fails the pull request instead of the next publish. +The optional repository filter and the artifact merge have their own test suite, +which also asserts the workflow wiring (the `repositories` input reaching +`select-software.sh` through the environment rather than being spliced into the +script): + +```bash +./scripts/test-repository-filter.sh +``` + To test publishing metadata end to end you need a signing key available to GPG; `scripts/publish-repo.sh` expects an artifacts directory, an output directory, and a key ID. Do not export or commit the repository's private signing key. diff --git a/README-zh-cn.md b/README-zh-cn.md index 4899ade..1d418d0 100644 --- a/README-zh-cn.md +++ b/README-zh-cn.md @@ -22,7 +22,8 @@ ZGS 出现在 [zhento-Software-Source](https://github.com/zitzhen/zhento-Softwar 仓库的核心是一个 `Software.list` 清单文件,每行列出一个包含 `debian/` 打包目录的上游 GitHub 仓库地址。手动触发的 GitHub Actions 工作流会: -1. 解析 `Software.list`,生成构建矩阵; +1. 解析 `Software.list`,生成构建矩阵(可通过手动运行时的 `repositories` 输入 + 只构建指定的仓库,见[只构建指定的仓库](#只构建指定的仓库)); 2. 依次 clone 上游仓库,用 `mk-build-deps` 安装构建依赖,执行 `dpkg-buildpackage -b -us -uc` 产出 `amd64` 二进制包; 3. 用 `dpkg-scanpackages` / `apt-ftparchive` 生成 apt 索引与 `Release` 元数据; @@ -43,6 +44,8 @@ ZGS/ ├── Software.list # 上游软件清单(每行一个 GitHub 仓库 URL) ├── scripts/ │ ├── parse-software-list.sh # 解析清单,输出构建矩阵 JSON +│ ├── select-software.sh # 校验并应用可选的仓库过滤输入 +│ ├── merge-artifacts.sh # 把已发布的包重新并入索引,避免被挤掉 │ ├── diff-software-list.sh # 计算相对某个基线新增的仓库 │ ├── build-deb.sh # 从上游仓库构建 amd64 .deb │ ├── publish-repo.sh # 组装、签名并导出 apt 元数据 @@ -55,7 +58,8 @@ ZGS/ │ ├── publish-log-release.sh # 创建带日志附件的发行版 │ ├── github-app-token.sh # 生成 GitHub App 安装令牌 │ ├── generate-index.sh # 为仓库目录树生成可浏览的 index.html -│ └── test-workflow-build-step.sh # 在 bash 下执行 workflow 的 run 块 +│ ├── test-workflow-build-step.sh # 在 bash 下执行 workflow 的 run 块 +│ └── test-repository-filter.sh # 测试仓库过滤输入与产物合并 ├── .github/workflows/ # 构建检查与发布 CI ├── .githooks/ # DCO 签名的版本化 Git 钩子 ├── CONTRIBUTING.md # 贡献指南 @@ -166,6 +170,32 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log - 重复 URL 会被忽略(并打印警告); - 上游仓库**必须**包含有效的 `debian/` 目录,且能产出 `amd64` 的 `.deb`。 +## 只构建指定的仓库 + +**Publish apt repository to R2** 工作流新增了一个可选的 `repositories` 输入, +维护者可以只重建一个或几个包,而不必重建整个清单。留空即构建清单中的全部条目, +与之前的行为完全一致。 + +该输入是单行的仓库地址列表,用逗号分隔,例如: + +``` +https://github.com/zitzhen/patchsplit,https://github.com/zitzhen/GitTune +``` + +`scripts/select-software.sh` 会做如下校验: + +- 忽略首尾空格,末尾的 `.git` 或 `/` 会被归一化;因为是手工输入,也接受全角 + 逗号(`,`、`、`)和整体外层方括号; +- 每个地址**必须已经在 `Software.list` 中列出**。清单里没有的地址(或根本不是 + GitHub 仓库地址的输入)会让 `prepare` job 直接失败,**该次运行不会构建任何包**, + 请先把仓库加入清单; +- 构建矩阵按清单顺序排列,重复的地址只构建一次。 + +发布行为不受过滤影响:`publish` job 会先从存储桶下载已发布的 `.deb`,与本次刚构建 +的包一起重新生成索引,同一个包只保留最新版本并且始终以本次构建的版本为准。因此 +只构建部分仓库**不会**把其它包从 apt 索引中挤掉。相关脚本为 +`scripts/merge-artifacts.sh`。 + ## 许可证与贡献 本项目以 [AGPL-3.0-only](LICENSE) 发布。所有提交需遵循 DCO diff --git a/README.md b/README.md index 7fd479e..c66f96e 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ The repository is driven by a `Software.list` manifest file, with each line pointing to an upstream GitHub repository that contains a `debian/` packaging directory. A manually triggered GitHub Actions workflow: -1. Parses `Software.list` and produces a build matrix; +1. Parses `Software.list` and produces a build matrix, optionally narrowed to the + repositories given in the manual run's `repositories` input (see + [Building only selected repositories](#building-only-selected-repositories)); 2. Clones each upstream repository, installs its build dependencies with `mk-build-deps`, and runs `dpkg-buildpackage -b -us -uc` to produce `amd64` binary packages; @@ -54,6 +56,8 @@ ZGS/ ├── Software.list # Upstream software manifest (one GitHub repo URL per line) ├── scripts/ │ ├── parse-software-list.sh # Parses the manifest, emits the build-matrix JSON +│ ├── select-software.sh # Validates and applies the optional repository filter +│ ├── merge-artifacts.sh # Re-indexes published packages a filtered run skipped │ ├── diff-software-list.sh # Computes repositories newly added since a base revision │ ├── build-deb.sh # Builds an amd64 .deb from an upstream repository │ ├── publish-repo.sh # Assembles, signs, and exports the apt metadata @@ -66,7 +70,8 @@ ZGS/ │ ├── publish-log-release.sh # Publishes a release with the run's logs │ ├── github-app-token.sh # Mints a GitHub App installation token │ ├── generate-index.sh # Writes browsable index.html files for the tree -│ └── test-workflow-build-step.sh # Runs a workflow run: block under bash +│ ├── test-workflow-build-step.sh # Runs a workflow run: block under bash +│ └── test-repository-filter.sh # Tests the repository filter and artifact merge ├── .github/workflows/ # Build-check and publish CI ├── .githooks/ # Versioned Git hooks for DCO sign-off ├── CONTRIBUTING.md # Contribution guide @@ -196,6 +201,36 @@ Manifest rules (enforced by `scripts/parse-software-list.sh`): - The upstream repository **must** contain a valid `debian/` directory and produce an `amd64` `.deb`. +## Building only selected repositories + +The **Publish apt repository to R2** workflow has an optional `repositories` +input, so a maintainer can rebuild one or a few packages instead of the whole +manifest. Leave it empty to build every entry, which is the previous behaviour. + +The input is a comma-separated list of repository URLs on a single line, for +example: + +``` +https://github.com/zitzhen/patchsplit,https://github.com/zitzhen/GitTune +``` + +Rules enforced by `scripts/select-software.sh`: + +- Surrounding spaces are ignored, a trailing `.git` or `/` is normalized, and the + full-width comma `,` and the ideographic comma `、` are accepted, because the + field is typed by hand; +- Every URL **must already be listed** in `Software.list`. A URL that is missing + from the manifest (or is not a GitHub repository URL at all) fails the + `prepare` job, so **no package is built** by that run. Add the repository to + the manifest first; +- The matrix follows the manifest order, and repeated URLs are selected once. + +Publishing is unaffected by the filter: the `publish` job downloads the +previously published `.deb` files from the bucket and re-indexes them together +with the packages just built, keeping only the newest version of each package +and always preferring the freshly built one. A filtered run therefore never +removes another package from the apt index. See `scripts/merge-artifacts.sh`. + ## License and contributing This project is released under [AGPL-3.0-only](LICENSE). All commits must follow diff --git a/scripts/merge-artifacts.sh b/scripts/merge-artifacts.sh new file mode 100755 index 0000000..2173a1e --- /dev/null +++ b/scripts/merge-artifacts.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +# Keep previously published packages in the index when only part of the manifest +# is rebuilt. +# +# publish-repo.sh indexes exactly the .deb files handed to it and regenerates +# Packages from that directory alone. A filtered run therefore used to publish an +# index that contained only the packages it happened to rebuild, so every other +# package vanished from `apt` until the next full run, even though its .deb was +# still in the bucket. +# +# This copies the previously published .deb files into the artifacts directory so +# they are indexed again, except for packages the current run just built: the +# freshly built version always wins, so a rebuild replaces the old package +# instead of shipping both. Only the newest published version of a package is +# kept, which is the same set a full rebuild would produce. +# +# Usage: merge-artifacts.sh +set -euo pipefail + +if [[ $# -ne 2 ]]; then + echo "Usage: merge-artifacts.sh " >&2 + exit 1 +fi + +built_dir="$1" +published_dir="$2" + +if [[ ! -d "$built_dir" ]]; then + echo "Error: built artifacts directory not found: $built_dir" >&2 + exit 1 +fi + +shopt -s nullglob +built=("$built_dir"/*.deb) +shopt -u nullglob + +if (( ${#built[@]} == 0 )); then + echo "Error: no .deb files in $built_dir" >&2 + exit 1 +fi + +# A missing previous directory simply means there is nothing to preserve, which +# is the normal case for the first ever publish. +if [[ ! -d "$published_dir" ]]; then + echo "No previously published artifacts at $published_dir; nothing to preserve" >&2 + exit 0 +fi + +package_of() { + dpkg-deb -f "$1" Package 2>/dev/null || true +} + +version_of() { + dpkg-deb -f "$1" Version 2>/dev/null || true +} + +declare -A built_packages=() +for deb in "${built[@]}"; do + name="$(package_of "$deb")" + if [[ -z "$name" ]]; then + echo "Error: cannot read the Package field from $deb" >&2 + exit 1 + fi + built_packages["$name"]=1 +done + +# Pick the newest published version per package. The pool keeps every version +# ever built, so copying all of them would re-index superseded packages that a +# full rebuild never lists. +declare -A newest_file=() +declare -A newest_version=() +candidates=0 +while IFS= read -r -d '' previous; do + name="$(package_of "$previous")" + if [[ -z "$name" ]]; then + echo "Warning: skipping unreadable package file: $previous" >&2 + continue + fi + candidates=$(( candidates + 1 )) + if [[ -n "${built_packages[$name]:-}" ]]; then + continue + fi + version="$(version_of "$previous")" + # An unreadable version would make dpkg --compare-versions abort the run, so a + # damaged file in the bucket is skipped rather than failing the publish. + if [[ -z "$version" ]] || ! dpkg --validate-version "$version" >/dev/null 2>&1; then + echo "Warning: skipping package file with an unusable Version: $previous" >&2 + continue + fi + if [[ -z "${newest_file[$name]:-}" ]] \ + || dpkg --compare-versions "$version" gt "${newest_version[$name]}"; then + newest_file["$name"]="$previous" + newest_version["$name"]="$version" + fi +done < <(find "$published_dir" -type f -name '*.deb' -print0) + +if (( candidates == 0 )); then + echo "No .deb files found in $published_dir; nothing to preserve" >&2 + exit 0 +fi + +kept=0 +for name in "${!newest_file[@]}"; do + cp -f "${newest_file[$name]}" "$built_dir/" + echo "Preserved $name ${newest_version[$name]} (not rebuilt in this run)" >&2 + kept=$(( kept + 1 )) +done + +echo "Preserved $kept previously published package(s) out of $candidates file(s)" >&2 +if (( ${#built_packages[@]} > 0 )); then + echo "Rebuilt package(s) replacing their published versions: ${!built_packages[*]}" >&2 +fi diff --git a/scripts/select-software.sh b/scripts/select-software.sh new file mode 100755 index 0000000..403d1b8 --- /dev/null +++ b/scripts/select-software.sh @@ -0,0 +1,135 @@ +#!/usr/bin/env bash +# Filter Software.list down to the repositories named by the workflow's +# optional `repositories` input, which is a comma-separated list of URLs. +# +# The input is optional. When it is empty every entry in Software.list is +# returned, so the publish workflow keeps its "build everything" behaviour. +# +# Every requested repository must already be listed in Software.list: stopping +# here is the whole point of the check, because a typo would otherwise publish a +# package that the manifest does not track. Nothing is filtered when the input is +# rejected, so a run never builds a partial set by accident. +# +# Usage: select-software.sh [software-list] [repositories] +# The arguments are optional and default to "Software.list" and "". +# The input is read from the arguments rather than the environment so that an +# empty value cannot be confused with an unset one. +set -euo pipefail + +if [[ $# -gt 2 ]]; then + echo "Usage: select-software.sh [software-list] [repositories]" >&2 + exit 1 +fi + +list_file="${1:-Software.list}" +requested_raw="${2:-}" + +if [[ ! -f "$list_file" ]]; then + echo "Error: software list not found: $list_file" >&2 + exit 1 +fi + +script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" + +# The manifest parser owns URL validation and normalisation (trailing .git and +# /), so the selection reuses it instead of duplicating the rules here. +list_json="$("$script_dir/parse-software-list.sh" "$list_file")" + +tmp_dir="$(mktemp -d)" +trap 'rm -rf "$tmp_dir"' EXIT +printf '%s\n' "$list_json" > "$tmp_dir/list.json" +printf '%s\n' "$requested_raw" > "$tmp_dir/requested.txt" + +python3 - "$tmp_dir/list.json" "$tmp_dir/requested.txt" "$list_file" <<'PY' +import json +import re +import sys + +with open(sys.argv[1]) as handle: + listed = [item["url"] for item in json.load(handle)] +with open(sys.argv[2]) as handle: + raw = handle.read() + +# Accept surrounding whitespace, full-width commas and full-width brackets: the +# field is typed by hand in the Actions UI, often with a Chinese input method. +raw = raw.strip() +for old, new in ( + ("\uff0c", ","), # full-width comma + ("\u3001", ","), # ideographic comma + ("\uff08", "("), + ("\uff09", ")"), + ("\u3010", "["), + ("\u3011", "]"), + ("\u3000", " "), # ideographic space +): + raw = raw.replace(old, new) + +# A leading and trailing bracket around the whole value is optional sugar, e.g. +# "[https://github.com/a/b, https://github.com/c/d]". +if raw.startswith("[") and raw.endswith("]"): + raw = raw[1:-1] + +requested = [] +for chunk in raw.split(","): + entry = chunk.strip().strip("'\"") + if entry: + requested.append(entry) + +if not requested: + print(json.dumps([{"url": url} for url in listed], separators=(",", ":"))) + print(f"No repository filter given; all {len(listed)} listed entries " + "will be built", file=sys.stderr) + raise SystemExit(0) + +url_re = re.compile( + r"^https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(\.git)?/?$" +) + +listed_set = set(listed) +selected = [] +unknown = [] +malformed = [] +seen = set() + +for entry in requested: + if not url_re.match(entry): + malformed.append(entry) + continue + normalised = entry.rstrip("/") + if normalised.endswith(".git"): + normalised = normalised[: -len(".git")] + if normalised not in listed_set: + unknown.append((entry, normalised)) + continue + if normalised in seen: + print(f"Warning: duplicate requested repository ignored: {normalised}", + file=sys.stderr) + continue + seen.add(normalised) + selected.append(normalised) + +if malformed: + print("Error: these requested entries are not GitHub repository URLs:", + file=sys.stderr) + for entry in malformed: + print(f" {entry}", file=sys.stderr) +if unknown: + print(f"Error: these repositories are not listed in {sys.argv[3]}:", + file=sys.stderr) + for entry, normalised in unknown: + print(f" {entry}", file=sys.stderr) + if normalised != entry: + print(f" (normalised to {normalised})", file=sys.stderr) +if malformed or unknown: + print("Nothing was built. Add the repository to Software.list first, then " + "re-run the workflow.", file=sys.stderr) + raise SystemExit(1) + +# Emit in manifest order so the matrix is stable between runs, regardless of the +# order the URLs were typed in. +wanted = set(selected) +ordered = [{"url": url} for url in listed if url in wanted] + +print(json.dumps(ordered, separators=(",", ":"))) +print(f"Selected {len(ordered)} of {len(listed)} listed entries", file=sys.stderr) +PY diff --git a/scripts/test-repository-filter.sh b/scripts/test-repository-filter.sh new file mode 100755 index 0000000..f3838bb --- /dev/null +++ b/scripts/test-repository-filter.sh @@ -0,0 +1,222 @@ +#!/usr/bin/env bash +# Tests for the optional repository filter of the publish workflow. +# +# Covers scripts/select-software.sh (the input validation) and +# scripts/merge-artifacts.sh (keeping published packages in the index when only +# part of the manifest is rebuilt). +set -euo pipefail + +repo_root="$(cd -- "$(dirname -- "$0")/.." && pwd -P)" +select="$repo_root/scripts/select-software.sh" +merge="$repo_root/scripts/merge-artifacts.sh" + +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +# Count the URLs in the JSON matrix printed on stdout, ignoring the log lines the +# scripts write to stderr. +matrix_urls() { + python3 -c 'import json,sys; print("\n".join(i["url"] for i in json.load(sys.stdin)))' +} + +# --- select-software.sh ---------------------------------------------------- + +list="$tmp/Software.list" +cat > "$list" <<'LIST' +# A comment line. +https://github.com/zitzhen/patchsplit +https://github.com/zitzhen/GitTune +https://github.com/zitzhen/kzhento-installer-prompt +LIST + +# An empty input keeps the previous behaviour: every listed entry is built. +out="$("$select" "$list" "" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | wc -l)" -eq 3 \ + || fail "an empty filter must select every entry" +printf '%s\n' "$out" | matrix_urls | grep -qx 'https://github.com/zitzhen/GitTune' \ + || fail "an empty filter must keep the listed URLs" + +# A single URL selects exactly that repository. +out="$("$select" "$list" "https://github.com/zitzhen/GitTune" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls)" = "https://github.com/zitzhen/GitTune" \ + || fail "a single URL must select one repository" + +# Commas separate entries; spaces around them are ignored and the manifest order +# wins over the order they were typed in. +out="$("$select" "$list" " https://github.com/zitzhen/kzhento-installer-prompt , https://github.com/zitzhen/patchsplit " 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | paste -sd, -)" = \ + "https://github.com/zitzhen/patchsplit,https://github.com/zitzhen/kzhento-installer-prompt" \ + || fail "the matrix must follow manifest order" + +# A trailing .git or / is normalised, matching the manifest rules. +out="$("$select" "$list" "https://github.com/zitzhen/patchsplit.git" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls)" = "https://github.com/zitzhen/patchsplit" \ + || fail "a trailing .git must be normalised" +out="$("$select" "$list" "https://github.com/zitzhen/patchsplit/" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls)" = "https://github.com/zitzhen/patchsplit" \ + || fail "a trailing slash must be normalised" + +# Full-width commas and ideographic commas are accepted: the field is typed by +# hand in the Actions UI, where a Chinese input method produces them. +out="$("$select" "$list" "https://github.com/zitzhen/patchsplit,https://github.com/zitzhen/GitTune" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | wc -l)" -eq 2 \ + || fail "full-width commas must separate entries" +out="$("$select" "$list" "https://github.com/zitzhen/patchsplit、https://github.com/zitzhen/GitTune" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | wc -l)" -eq 2 \ + || fail "ideographic commas must separate entries" + +# Brackets around the whole value are optional sugar, dropped by a shell that +# strips the value, so they must not change the result. +out="$("$select" "$list" "[https://github.com/zitzhen/patchsplit, https://github.com/zitzhen/GitTune]" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | wc -l)" -eq 2 \ + || fail "surrounding brackets must be ignored" + +# Repeated URLs select the repository once. +out="$("$select" "$list" "https://github.com/zitzhen/GitTune,https://github.com/zitzhen/GitTune" 2>/dev/null)" +test "$(printf '%s\n' "$out" | matrix_urls | wc -l)" -eq 1 \ + || fail "a repeated URL must be selected once" + +# A repository that is not listed must fail the run instead of building it, and +# must not fall back to building everything. +if ! error="$("$select" "$list" "https://github.com/not/listed" 2>&1 >/dev/null)"; then + : +else + fail "an unlisted repository must be rejected" +fi +printf '%s\n' "$error" | grep -q 'not listed' \ + || fail "the rejection must say the repository is not listed" + +# One bad entry rejects the whole input: a partial build would silently skip a +# package the operator asked for. +if ! error="$("$select" "$list" "https://github.com/zitzhen/GitTune,https://github.com/not/listed" 2>&1 >/dev/null)"; then + : +else + fail "a mixed input containing an unlisted repository must be rejected" +fi +printf '%s\n' "$error" | grep -q 'https://github.com/not/listed' \ + || fail "the rejection must name the offending entry" + +# Anything that is not a GitHub repository URL is rejected too, so a typo such as +# an owner-only URL cannot be mistaken for a listed repository. +for bad in "not-a-url" "https://github.com/onlyowner" "https://gitlab.com/a/b"; do + if "$select" "$list" "$bad" >/dev/null 2>&1; then + fail "malformed entry must be rejected: $bad" + fi +done + +# A missing manifest is an error rather than an empty matrix. +if "$select" "$tmp/absent.list" "" >/dev/null 2>&1; then + fail "a missing Software.list must be rejected" +fi + +# --- merge-artifacts.sh ---------------------------------------------------- + +make_deb() { + # make_deb -> prints the .deb path + local out_dir="$1" package="$2" version="$3" + local root + root="$(mktemp -d)" + mkdir -p "$root/DEBIAN" + cat > "$root/DEBIAN/control" < +Description: test package +CONTROL + mkdir -p "$out_dir" + dpkg-deb --build "$root" "$out_dir/${package}_${version}_amd64.deb" >/dev/null 2>&1 + rm -rf "$root" +} + +built="$tmp/built" +published="$tmp/published/pool/main/g/gittune" +mkdir -p "$built" "$published" + +# This run rebuilt gittune; the pool still holds an older gittune and the two +# packages that were not rebuilt. +make_deb "$built" gittune 2.0 >/dev/null +make_deb "$tmp/old" gittune 1.0 >/dev/null +make_deb "$published" patchsplit 1.4 >/dev/null +make_deb "$published" patchsplit 1.3 >/dev/null +make_deb "$published" ddm-sync 0.9 >/dev/null + +"$merge" "$built" "$tmp/published/pool" >/dev/null 2>"$tmp/merge.log" + +# The rebuilt package replaces its published version, not ships alongside it. +test -f "$built/gittune_2.0_amd64.deb" || fail "the rebuilt package must be kept" +test ! -e "$built/gittune_1.0_amd64.deb" \ + || fail "the superseded version of a rebuilt package must be dropped" +# Unrebuilt packages are preserved so the index still lists them. +test -f "$built/patchsplit_1.4_amd64.deb" || fail "unrebuilt packages must be preserved" +test -f "$built/ddm-sync_0.9_amd64.deb" || fail "unrebuilt packages must be preserved" +# Only the newest published version is preserved, as in a full rebuild. +test ! -e "$built/patchsplit_1.3_amd64.deb" \ + || fail "only the newest published version of a package may be preserved" + +grep -q 'Preserved 2 previously published package' "$tmp/merge.log" \ + || fail "the merge must report how many packages it preserved" + +# A missing published tree (first ever publish) is not an error. +mkdir -p "$tmp/fresh" +make_deb "$tmp/fresh" patchsplit 1.4 >/dev/null +"$merge" "$tmp/fresh" "$tmp/absent" >/dev/null 2>&1 \ + || fail "a missing published tree must not fail the run" +test "$(find "$tmp/fresh" -name '*.deb' | wc -l)" -eq 1 \ + || fail "a missing published tree must leave the artifacts untouched" + +# An empty artifacts directory is an error: publishing nothing would empty the +# index, which is exactly what this script exists to prevent. +mkdir -p "$tmp/empty" +if "$merge" "$tmp/empty" "$tmp/published/pool" >/dev/null 2>&1; then + fail "an empty artifacts directory must be rejected" +fi +if "$merge" "$tmp/absent" "$tmp/published/pool" >/dev/null 2>&1; then + fail "a missing artifacts directory must be rejected" +fi +if "$merge" >/dev/null 2>&1; then + fail "the script must require both arguments" +fi + +# --- workflow wiring ------------------------------------------------------- + +# The publish workflow must expose the optional input and route it to the +# selection script; the build matrix alone previously came from the manifest. +workflow="$repo_root/.github/workflows/publish-apt.yml" +python3 - "$workflow" <<'PY' +import sys +import yaml + +doc = yaml.safe_load(open(sys.argv[1])) +dispatch = doc[True]["workflow_dispatch"] +inputs = dispatch.get("inputs") or {} +assert "repositories" in inputs, "workflow_dispatch must declare a repositories input" +assert inputs["repositories"]["required"] is False, "the input must be optional" + +steps = {step.get("name"): step for step in doc["jobs"]["prepare"]["steps"]} +parse = steps["Build the package matrix"] +assert "select-software.sh" in parse["run"], \ + "the matrix must come from select-software.sh, which validates the input" +assert parse["env"]["REPOSITORIES"] == "${{ inputs.repositories }}", \ + "the input must be passed as an environment variable, not spliced into the script" +assert "filtered=true" in parse["run"], \ + "the prepare job must report whether the matrix is a partial rebuild" +outputs = doc["jobs"]["prepare"]["outputs"] +assert "filtered" in outputs, "the filtered flag must be a prepare job output" + +publish_steps = {step.get("name"): step for step in doc["jobs"]["publish"]["steps"]} +restore = publish_steps["Restore the packages already published"] +assert restore["if"] == "needs.prepare.outputs.filtered == 'true'", \ + "restoring published packages must only happen for a filtered run" +assert "merge-artifacts.sh" in restore["run"], \ + "the restore step must merge published packages into the artifacts" +assert "prepare" in doc["jobs"]["publish"]["needs"], \ + "publish must list prepare in needs to read the filtered flag" +PY + +echo "PASS: repository filter and artifact merge behave as expected"