From 9b297b14a629210cb1be5692526538e85009c0dc Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 22 Oct 2026 23:04:52 +0800 Subject: [PATCH] ci: deploy private build checks and macOS release publishers --- .github/actions/checkout-private/action.yaml | 35 + .github/workflows/build-gpgui-linux.yaml | 63 +- .github/workflows/build.yaml | 498 +++++++++----- .github/workflows/publish-macos-assets.yaml | 135 ++++ .github/workflows/publish-update-feeds.yaml | 620 ++++++++++++++++++ .github/workflows/release-checks.yaml | 24 + scripts/select-macos-artifact.sh | 32 + scripts/test-private-desktop.sh | 54 ++ scripts/tests/test_private_ci.py | 215 ++++++ scripts/tests/test_validate_macos_appcast.py | 116 ++++ scripts/tests/test_validate_macos_release.py | 99 +++ .../tests/test_validate_update_manifest.py | 177 +++++ scripts/validate-macos-appcast.py | 142 ++++ scripts/validate-macos-release.py | 67 ++ scripts/validate-update-manifest.py | 219 +++++++ 15 files changed, 2330 insertions(+), 166 deletions(-) create mode 100644 .github/actions/checkout-private/action.yaml create mode 100644 .github/workflows/publish-macos-assets.yaml create mode 100644 .github/workflows/publish-update-feeds.yaml create mode 100644 scripts/select-macos-artifact.sh create mode 100644 scripts/test-private-desktop.sh create mode 100644 scripts/tests/test_private_ci.py create mode 100644 scripts/tests/test_validate_macos_appcast.py create mode 100644 scripts/tests/test_validate_macos_release.py create mode 100644 scripts/tests/test_validate_update_manifest.py create mode 100755 scripts/validate-macos-appcast.py create mode 100644 scripts/validate-macos-release.py create mode 100644 scripts/validate-update-manifest.py diff --git a/.github/actions/checkout-private/action.yaml b/.github/actions/checkout-private/action.yaml new file mode 100644 index 0000000..a0c3ea4 --- /dev/null +++ b/.github/actions/checkout-private/action.yaml @@ -0,0 +1,35 @@ +name: Checkout private source quietly +description: Fetch private source without printing Git diagnostics or retaining credentials. +inputs: + token: + required: true + description: Token with read access to the private repository + ref: + required: true + description: Branch, tag, or full commit SHA + path: + required: true + description: Destination within the workspace +runs: + using: composite + steps: + - shell: bash + env: + PRIVATE_TOKEN: ${{ inputs.token }} + PRIVATE_REF: ${{ inputs.ref }} + PRIVATE_PATH: ${{ inputs.path }} + run: | + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private source checkout failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + export GIT_TERMINAL_PROMPT=0 + export GIT_CONFIG_COUNT=1 + export GIT_CONFIG_KEY_0=http.https://github.com/.extraheader + GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$PRIVATE_TOKEN" | base64 | tr -d '\n')" + export GIT_CONFIG_VALUE_0 + git init "$PRIVATE_PATH" + git -C "$PRIVATE_PATH" remote add origin https://github.com/yuezk/gpgui + git -C "$PRIVATE_PATH" fetch --no-tags --depth=1 -- origin "$PRIVATE_REF" + git -C "$PRIVATE_PATH" checkout --detach FETCH_HEAD + ) >/dev/null 2>&1 + echo "Private source checkout passed." diff --git a/.github/workflows/build-gpgui-linux.yaml b/.github/workflows/build-gpgui-linux.yaml index fd0ad43..aa2c9cb 100644 --- a/.github/workflows/build-gpgui-linux.yaml +++ b/.github/workflows/build-gpgui-linux.yaml @@ -51,20 +51,37 @@ jobs: ref: ${{ inputs.gp_ref }} path: gpgui-source/gp submodules: recursive - - name: Checkout gpgui + - name: Checkout CI helpers uses: actions/checkout@v6 + with: + path: .ci + persist-credentials: false + - name: Checkout gpgui + uses: ./.ci/.github/actions/checkout-private with: token: ${{ secrets.GH_PAT }} - repository: yuezk/gpgui ref: ${{ inputs.gpgui_ref }} path: gpgui-source/gpgui - name: Verify Linux desktop assets if: matrix.os.arch == 'amd64' && inputs.verify_desktop_assets - run: gpgui-source/gp/scripts/verify-linux-desktop-assets.sh gpgui-source/gpgui + run: | + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + gpgui-source/gp/scripts/verify-linux-desktop-assets.sh gpgui-source/gpgui + ) >/dev/null 2>&1 + echo "Private operation passed." + - name: Tarball run: | - cd gpgui-source - tar -czf gpgui.tar.gz gpgui gp + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + cd gpgui-source + tar -czf gpgui.tar.gz gpgui gp + ) >/dev/null 2>&1 + echo "Private operation passed." + - name: Build gpgui in Docker env: GPGUI_BUILDER: ${{ inputs.builder }} @@ -72,22 +89,34 @@ jobs: GPGUI_UPDATE_CHANNEL: ${{ inputs.channel }} GPGUI_LICENSE_SIGNING_KEYS: ${{ vars.GPGUI_LICENSE_SIGNING_KEYS }} run: | - docker run --rm \ - -e COREPACK_INTEGRITY_KEYS=0 \ - -e GPGUI_BUILD_VERSION \ - -e GPGUI_UPDATE_CHANNEL \ - -e GPGUI_LICENSE_SIGNING_KEYS \ - -v "$(pwd)/gpgui-source:/workspace" "yuezk/gpdev:$GPGUI_BUILDER" + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + docker run --rm \ + -e COREPACK_INTEGRITY_KEYS=0 \ + -e GPGUI_BUILD_VERSION \ + -e GPGUI_UPDATE_CHANNEL \ + -e GPGUI_LICENSE_SIGNING_KEYS \ + -v "$(pwd)/gpgui-source:/workspace" "yuezk/gpdev:$GPGUI_BUILDER" + ) >/dev/null 2>&1 + echo "Private operation passed." + - name: Install gpgui in Docker env: GPGUI_BUILDER: ${{ inputs.builder }} run: | - cd gpgui-source - tar -xJf ./*.bin.tar.xz - docker run --rm \ - -e COREPACK_INTEGRITY_KEYS=0 \ - -v "$(pwd):/workspace" "yuezk/gpdev:$GPGUI_BUILDER" \ - bash -c "cd /workspace/gpgui_*/ && ./gpgui --version" + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + cd gpgui-source + tar -xJf ./*.bin.tar.xz + docker run --rm \ + -e COREPACK_INTEGRITY_KEYS=0 \ + -v "$(pwd):/workspace" "yuezk/gpdev:$GPGUI_BUILDER" \ + bash -c "cd /workspace/gpgui_*/ && ./gpgui --version" + ) >/dev/null 2>&1 + echo "Private operation passed." + - name: Upload gpgui uses: actions/upload-artifact@v7 with: diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b605e94..f23618e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -18,7 +18,7 @@ on: required: true default: dev gpgui_branch: - description: 'gpgui ref to build (use the matching tag for stable builds)' + description: 'gpgui branch or tag to build and test (use the matching tag for stable builds)' required: true default: dev build_macos: @@ -107,6 +107,36 @@ jobs: --bin gpservice device_anchor::tests ' + test-gpgui: + needs: validate-inputs + runs-on: ubuntu-latest + timeout-minutes: 85 + permissions: + contents: read + steps: + - name: Checkout GlobalProtect-openconnect + uses: actions/checkout@v6 + with: + repository: yuezk/GlobalProtect-openconnect + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gp_branch || github.ref }} + submodules: recursive + path: source/gp + persist-credentials: false + - name: Checkout CI helpers + uses: actions/checkout@v6 + with: + path: .ci + persist-credentials: false + - name: Checkout gpgui + uses: ./.ci/.github/actions/checkout-private + with: + token: ${{ secrets.GH_PAT }} + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gpgui_branch || github.ref_name }} + path: source/gpgui + - name: Run frontend and Rust unit tests as an ordinary user + timeout-minutes: 80 + run: bash .ci/scripts/test-private-desktop.sh "$PWD/source" + tarball: needs: validate-inputs runs-on: ubuntu-latest @@ -467,93 +497,278 @@ jobs: nix build --impure --file gp/nix/ci.nix runtime-prebuilt \ --extra-experimental-features "nix-command flakes" --print-build-logs - trigger-gpgui-macos: - needs: test-gp + build-macos: + name: build-macos + needs: [test-gp, test-gpgui] if: ${{ (github.event_name == 'workflow_dispatch' && inputs.build_macos) || (startsWith(github.ref, 'refs/tags/') && vars.BUILD_MACOS_ON_TAG == 'true') }} permissions: contents: read - runs-on: ubuntu-latest - name: trigger private macOS build - outputs: - run_id: ${{ steps.dispatch.outputs.run_id }} - run_url: ${{ steps.dispatch.outputs.run_url }} + concurrency: + group: macos-build + cancel-in-progress: false + runs-on: ${{ vars.MACOS_RUNNER || 'macos-15' }} + environment: macos-release + timeout-minutes: 120 steps: - - name: Dispatch private macOS build - id: dispatch + - name: Verify runner architecture + run: test "$(uname -m)" = arm64 + + - name: Select Xcode toolchain env: - GH_TOKEN: ${{ secrets.GPGUI_ACTIONS_TOKEN }} - PUBLIC_GH_TOKEN: ${{ github.token }} - CHANNEL: ${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} - RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && (inputs.channel == 'stable' && inputs.gp_branch || 'snapshot') || github.ref_name }} - GP_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.gp_branch || github.ref_name }} - GPGUI_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.gpgui_branch || github.ref_name }} - BUILD_VERSION: ${{ github.run_number }}.${{ github.run_attempt }} - REQUEST_ID: ${{ github.run_id }}.${{ github.run_attempt }}.macos + DEVELOPER_DIR: /Applications/Xcode_26.3.app/Contents/Developer + run: | + set -euo pipefail + xcodebuild -version + xcrun swift --version + echo "DEVELOPER_DIR=$DEVELOPER_DIR" >> "$GITHUB_ENV" + + - uses: pnpm/action-setup@v5 + with: + version: 9.15.1 + + - name: Checkout CI helpers + uses: actions/checkout@v6 + with: + path: .ci + persist-credentials: false + - name: Checkout gpgui source + uses: ./.ci/.github/actions/checkout-private + with: + token: ${{ secrets.GH_PAT }} + ref: ${{ (github.event_name == 'workflow_dispatch' && inputs.gpgui_branch || github.ref_name) }} + path: source/gpgui + + - name: Checkout GlobalProtect-openconnect source + uses: actions/checkout@v6 + with: + repository: yuezk/GlobalProtect-openconnect + ref: ${{ (github.event_name == 'workflow_dispatch' && inputs.gp_branch || github.ref) }} + path: source/gp + submodules: recursive + + - name: Verify stable source commits + if: ${{ (github.event_name == 'workflow_dispatch' && inputs.channel || 'stable') == 'stable' }} + env: + GH_TOKEN: ${{ secrets.GH_PAT }} + RELEASE_TAG: ${{ (github.event_name == 'workflow_dispatch' && (inputs.channel == 'stable' && inputs.gp_branch || 'snapshot') || github.ref_name) }} + run: | + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Stable source tag verification failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + git -C source/gp fetch --no-tags origin "refs/tags/$RELEASE_TAG" + test "$(git -C source/gp rev-parse HEAD)" = "$(git -C source/gp rev-parse 'FETCH_HEAD^{commit}')" + tagged_gui_commit="$(gh api "repos/yuezk/gpgui/commits/$RELEASE_TAG" --jq .sha)" + test "$(git -C source/gpgui rev-parse HEAD)" = "$tagged_gui_commit" + ) >/dev/null 2>&1 + echo "Stable source tags verified." + + - uses: actions/setup-node@v6 + with: + node-version: 22 + + - name: Prepare release metadata + env: + FEED_URL: ${{ vars.SPARKLE_FEED_URL }} + CHANNEL: ${{ (github.event_name == 'workflow_dispatch' && inputs.channel || 'stable') }} + RELEASE_TAG: ${{ (github.event_name == 'workflow_dispatch' && (inputs.channel == 'stable' && inputs.gp_branch || 'snapshot') || github.ref_name) }} + BUILD_VERSION: ${{ format('{0}.{1}', github.run_number, github.run_attempt) }} run: | set -euo pipefail - if [[ -z "${GH_TOKEN:-}" ]]; then - echo "GPGUI_ACTIONS_TOKEN is not configured" >&2 + gp_version="$(sed -n 's/^version = "\([^"]*\)"/\1/p' source/gp/Cargo.toml | head -1)" + gpgui_version="$(sed -n 's/^version = "\([^"]*\)"/\1/p' source/gpgui/Cargo.toml | head -1)" + if [[ -z "$gp_version" || "$gp_version" != "$gpgui_version" ]]; then + echo "gp and gpgui versions must match: gp=$gp_version, gpgui=$gpgui_version" >&2 + exit 1 + fi + if [[ "$FEED_URL" != https://*/appcast.xml ]]; then + echo "SPARKLE_FEED_URL must be an HTTPS URL ending in /appcast.xml" >&2 exit 1 fi - gp_ref_path="$(jq -rn --arg ref "$GP_REF" '$ref | @uri')" - gpgui_ref_path="$(jq -rn --arg ref "$GPGUI_REF" '$ref | @uri')" - gp_sha="$(GH_TOKEN="$PUBLIC_GH_TOKEN" gh api \ - "repos/yuezk/GlobalProtect-openconnect/commits/$gp_ref_path" --jq .sha)" - gpgui_sha="$(gh api "repos/yuezk/gpgui/commits/$gpgui_ref_path" --jq .sha)" - if [[ -z "$gp_sha" || -z "$gpgui_sha" ]]; then - echo "Could not resolve release source refs" >&2 - exit 1 + if [[ "$CHANNEL" == "stable" ]]; then + artifact_version="$gpgui_version" + if [[ "$RELEASE_TAG" != "v$gpgui_version" ]]; then + echo "Release tag does not match gpgui version $gpgui_version" >&2 + exit 1 + fi + else + artifact_version="${gpgui_version}-snapshot.${BUILD_VERSION}" fi - expected_title="macOS build $CHANNEL $RELEASE_TAG ($REQUEST_ID)" - jq -n \ - --arg workflow_ref "$GPGUI_REF" \ - --arg request_id "$REQUEST_ID" \ - --arg build_version "$BUILD_VERSION" \ - --arg channel "$CHANNEL" \ - --arg release_tag "$RELEASE_TAG" \ - --arg gp_ref "$gp_sha" \ - --arg gpgui_ref "$gpgui_sha" \ - '{ - ref: $workflow_ref, - inputs: { - request_id: $request_id, - build_version: $build_version, - channel: $channel, - release_tag: $release_tag, - gp_ref: $gp_ref, - gpgui_ref: $gpgui_ref - } - }' | gh api --method POST \ - repos/yuezk/gpgui/actions/workflows/build-macos.yaml/dispatches \ - --input - + { + echo "MACOS_BUILD_NUMBER=$BUILD_VERSION" + echo "MACOS_ARTIFACT_VERSION=$artifact_version" + echo "SPARKLE_FEED_URL=$FEED_URL" + echo "SPARKLE_UPDATE_CHANNEL=$CHANNEL" + } >> "$GITHUB_ENV" - run_id="" - run_url="" - for _ in {1..40}; do - runs="$(gh api \ - 'repos/yuezk/gpgui/actions/workflows/build-macos.yaml/runs?event=workflow_dispatch&per_page=50')" - run_id="$(jq -r --arg title "$expected_title" \ - '[.workflow_runs[] | select(.display_title == $title)] | first | .id // empty' \ - <<<"$runs")" - run_url="$(jq -r --arg title "$expected_title" \ - '[.workflow_runs[] | select(.display_title == $title)] | first | .html_url // empty' \ - <<<"$runs")" - if [[ -n "$run_id" ]]; then - { - echo "run_id=$run_id" - echo "run_url=$run_url" - } >> "$GITHUB_OUTPUT" - echo "Triggered private workflow: $run_url" - exit 0 + - name: Install build dependencies + run: | + brew install \ + autoconf \ + automake \ + create-dmg \ + dylibbundler \ + gettext \ + libtool \ + meson \ + ninja \ + pkgconf \ + python + + - name: Import signing identity + env: + MACOS_CERTIFICATE_P12: ${{ secrets.MACOS_CERTIFICATE_P12 }} + MACOS_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PASSWORD }} + APPLE_NOTARY_KEY_P8: ${{ secrets.APPLE_NOTARY_KEY_P8 }} + APPLE_NOTARY_KEY_ID: ${{ secrets.APPLE_NOTARY_KEY_ID }} + APPLE_NOTARY_ISSUER_ID: ${{ secrets.APPLE_NOTARY_ISSUER_ID }} + run: | + set -euo pipefail + + for variable in \ + MACOS_CERTIFICATE_P12 \ + MACOS_CERTIFICATE_PASSWORD; do + if [[ -z "${!variable:-}" ]]; then + echo "$variable is not configured" >&2 + exit 1 + fi + done + for variable in \ + APPLE_NOTARY_KEY_P8 \ + APPLE_NOTARY_KEY_ID \ + APPLE_NOTARY_ISSUER_ID; do + if [[ -z "${!variable:-}" ]]; then + echo "$variable is not configured" >&2 + exit 1 fi - sleep 3 done - echo "Could not locate the dispatched private workflow run" >&2 - exit 1 + keychain="$RUNNER_TEMP/gp-connect.keychain-db" + keychain_password="$(uuidgen)" + certificate="$RUNNER_TEMP/signing-identity.p12" + printf '%s' "$MACOS_CERTIFICATE_P12" | base64 -D > "$certificate" + chmod 600 "$certificate" + + security create-keychain -p "$keychain_password" "$keychain" + security set-keychain-settings -lut 21600 "$keychain" + security unlock-keychain -p "$keychain_password" "$keychain" + security import "$certificate" -k "$keychain" \ + -P "$MACOS_CERTIFICATE_PASSWORD" -A -t cert -f pkcs12 + security set-key-partition-list -S apple-tool:,apple: \ + -s -k "$keychain_password" "$keychain" + security list-keychains -d user -s "$keychain" login.keychain-db + + identity="$(security find-identity -v -p codesigning "$keychain" \ + | sed -n 's/.*"\(Developer ID Application:.*\)"/\1/p' | head -1)" + if [[ -z "$identity" ]]; then + echo "A Developer ID Application signing identity was not found" >&2 + exit 1 + fi + + { + echo "CI_KEYCHAIN=$keychain" + echo "MACOS_SIGN_IDENTITY=$identity" + } >> "$GITHUB_ENV" + notary_key="$RUNNER_TEMP/AuthKey.p8" + notary_profile="gp-connect-ci" + printf '%s' "$APPLE_NOTARY_KEY_P8" > "$notary_key" + chmod 600 "$notary_key" + xcrun notarytool store-credentials "$notary_profile" \ + --key "$notary_key" \ + --key-id "$APPLE_NOTARY_KEY_ID" \ + --issuer "$APPLE_NOTARY_ISSUER_ID" \ + --keychain "$keychain" + { + echo "APPLE_NOTARY_PROFILE=$notary_profile" + echo "APPLE_NOTARY_KEYCHAIN=$keychain" + } >> "$GITHUB_ENV" + + - name: Build macOS release artifacts + env: + CHANNEL: ${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} + SPARKLE_PUBLIC_KEY: ${{ vars.SPARKLE_PUBLIC_KEY }} + GPGUI_LICENSE_SIGNING_KEYS: ${{ vars.GPGUI_LICENSE_SIGNING_KEYS }} + run: | + set -euo pipefail + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + if [[ "$CHANNEL" == "snapshot" ]]; then + export GPGUI_RELEASE_DEVTOOLS=1 + fi + source/gpgui/scripts/build-macos.sh dmg release + ) 2>&1 | awk ' + /^MACOS_STAGE: (Native dependencies|Components|Frontend|Application|Bundle|Bundle icons|Bundle metadata|Bundle licenses|Bundle libraries|Bundle frameworks|Bundle optimization|Signing|Validation|App notarization|Update archive|Disk image|Disk image notarization|Complete)$/ { + print "::notice::" $0 + fflush() + } + ' + echo "Private operation passed." + + - name: Prepare release assets + run: | + set -euo pipefail + + artifact_dir="release-assets" + rm -rf "$artifact_dir" + mkdir -p "$artifact_dir" + dmg_name="GPConnect_${MACOS_ARTIFACT_VERSION}_arm64.dmg" + archive_name="GPConnect_${MACOS_ARTIFACT_VERSION}_arm64.zip" + mv "source/gpgui/target/release/bundle/dmg/GP Connect.dmg" \ + "$artifact_dir/$dmg_name" + mv "source/gpgui/target/release/bundle/sparkle/$archive_name" \ + "$artifact_dir/$archive_name" + ( + cd "$artifact_dir" + for file in *; do + shasum -a 256 "$file" > "$file.sha256" + done + ) + + - name: Record build identity + env: + CHANNEL: ${{ (github.event_name == 'workflow_dispatch' && inputs.channel || 'stable') }} + RELEASE_TAG: ${{ (github.event_name == 'workflow_dispatch' && (inputs.channel == 'stable' && inputs.gp_branch || 'snapshot') || github.ref_name) }} + REQUEST_ID: ${{ format('{0}.{1}.macos', github.run_id, github.run_attempt) }} + BUILD_VERSION: ${{ format('{0}.{1}', github.run_number, github.run_attempt) }} + WORKFLOW_SHA: ${{ github.sha }} + run: | + GP_REF="$(git -C source/gp rev-parse HEAD)" + GPGUI_REF="$(git -C source/gpgui rev-parse HEAD)" + export GP_REF GPGUI_REF + python3 - <<'PYTHON' + import hashlib + import json + import os + from pathlib import Path + directory = Path("release-assets") + metadata = {key.lower(): os.environ[key] for key in ( + "CHANNEL", "RELEASE_TAG", "GP_REF", "GPGUI_REF", "REQUEST_ID", "BUILD_VERSION", "WORKFLOW_SHA")} + metadata["artifact_version"] = os.environ["MACOS_ARTIFACT_VERSION"] + metadata["assets"] = { + file.name: hashlib.sha256(file.read_bytes()).hexdigest() + for file in directory.iterdir() if file.is_file() + } + (directory / "release-build.json").write_text(json.dumps(metadata, indent=2) + "\n") + PYTHON + + - name: Upload macOS build artifact + uses: actions/upload-artifact@v7 + with: + name: macos-release-${{ format('{0}.{1}.macos', github.run_id, github.run_attempt) }} + if-no-files-found: error + overwrite: true + retention-days: 30 + path: release-assets/* + + - name: Remove signing keychain + if: ${{ always() }} + run: | + if [[ -n "${CI_KEYCHAIN:-}" && -f "$CI_KEYCHAIN" ]]; then + security delete-keychain "$CI_KEYCHAIN" + fi setup-bsd-matrix: needs: validate-inputs @@ -617,18 +832,28 @@ jobs: ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gp_branch || github.ref }} path: gpgui-frontend/gp submodules: recursive - - name: Checkout gpgui + - name: Checkout CI helpers uses: actions/checkout@v6 + with: + path: .ci + persist-credentials: false + - name: Checkout gpgui + uses: ./.ci/.github/actions/checkout-private with: token: ${{ secrets.GH_PAT }} - repository: yuezk/gpgui ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gpgui_branch || github.ref_name }} path: gpgui-frontend/gpgui - name: Build gpgui frontend run: | - cd gpgui-frontend/gpgui/apps/desktop - pnpm install --frozen-lockfile - pnpm build + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + cd gpgui-frontend/gpgui/apps/desktop + pnpm install --frozen-lockfile + pnpm build + ) >/dev/null 2>&1 + echo "Private operation passed." + - name: Upload gpgui frontend uses: actions/upload-artifact@v7 with: @@ -657,11 +882,15 @@ jobs: ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gp_branch || github.ref }} path: ${{ matrix.workspace }}/gp submodules: recursive - - name: Checkout gpgui + - name: Checkout CI helpers uses: actions/checkout@v6 + with: + path: .ci + persist-credentials: false + - name: Checkout gpgui + uses: ./.ci/.github/actions/checkout-private with: token: ${{ secrets.GH_PAT }} - repository: yuezk/gpgui ref: ${{ github.event_name == 'workflow_dispatch' && inputs.gpgui_branch || github.ref_name }} path: ${{ matrix.workspace }}/gpgui - name: Download gpgui frontend @@ -699,24 +928,28 @@ jobs: webkit2-gtk_41 run: | set -eu - export GPGUI_BUILD_VERSION=${{ github.run_number }}.${{ github.run_attempt }} - export GPGUI_UPDATE_CHANNEL=${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} - export GPGUI_LICENSE_SIGNING_KEYS='${{ vars.GPGUI_LICENSE_SIGNING_KEYS }}' + ( + set -eu + export GPGUI_BUILD_VERSION=${{ github.run_number }}.${{ github.run_attempt }} + export GPGUI_UPDATE_CHANNEL=${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} + export GPGUI_LICENSE_SIGNING_KEYS='${{ vars.GPGUI_LICENSE_SIGNING_KEYS }}' - cd "${{ matrix.workspace }}/gp" - cargo build --release --workspace + cd "${{ matrix.workspace }}/gp" + cargo build --release --workspace - cd ../gpgui/apps/desktop/src-tauri - cargo build --release --workspace + cd ../gpgui/apps/desktop/src-tauri + cargo build --release --workspace - cd ../../../../gp - gmake "${{ matrix.package_target }}" GPGUI_BINARY=../gpgui/target/release/gpgui - pkg install -y ".build/${{ matrix.artifact_dir }}"/artifacts/*.pkg - gpclient --version - gpauth --version - gpgui --version - rm -rf target ../gpgui/target - find ".build/${{ matrix.artifact_dir }}" -mindepth 1 -maxdepth 1 ! -name artifacts -exec rm -rf {} + + cd ../../../../gp + gmake "${{ matrix.package_target }}" GPGUI_BINARY=../gpgui/target/release/gpgui + pkg install -y ".build/${{ matrix.artifact_dir }}"/artifacts/*.pkg + gpclient --version + gpauth --version + gpgui --version + rm -rf target ../gpgui/target + find ".build/${{ matrix.artifact_dir }}" -mindepth 1 -maxdepth 1 ! -name artifacts -exec rm -rf {} + + ) >/dev/null 2>&1 + echo "BSD build passed." - name: Build on OpenBSD if: ${{ matrix.os == 'openbsd' }} uses: vmactions/openbsd-vm@v1 @@ -754,26 +987,30 @@ jobs: ln -sf /usr/local/bin/automake-1.17 /usr/local/bin/automake run: | set -eu - export AUTOCONF_VERSION=2.72 - export AUTOMAKE_VERSION=1.17 - export GPGUI_BUILD_VERSION=${{ github.run_number }}.${{ github.run_attempt }} - export GPGUI_UPDATE_CHANNEL=${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} - export GPGUI_LICENSE_SIGNING_KEYS='${{ vars.GPGUI_LICENSE_SIGNING_KEYS }}' + ( + set -eu + export AUTOCONF_VERSION=2.72 + export AUTOMAKE_VERSION=1.17 + export GPGUI_BUILD_VERSION=${{ github.run_number }}.${{ github.run_attempt }} + export GPGUI_UPDATE_CHANNEL=${{ github.event_name == 'workflow_dispatch' && inputs.channel || 'stable' }} + export GPGUI_LICENSE_SIGNING_KEYS='${{ vars.GPGUI_LICENSE_SIGNING_KEYS }}' - cd "${{ matrix.workspace }}/gp" - cargo build --release --workspace + cd "${{ matrix.workspace }}/gp" + cargo build --release --workspace - cd ../gpgui/apps/desktop/src-tauri - cargo build --release --workspace + cd ../gpgui/apps/desktop/src-tauri + cargo build --release --workspace - cd ../../../../gp - gmake "${{ matrix.package_target }}" GPGUI_BINARY=../gpgui/target/release/gpgui - pkg_add -D unsigned ".build/${{ matrix.artifact_dir }}"/artifacts/*.tgz - gpclient --version - gpauth --version - gpgui --version - rm -rf target ../gpgui/target - find ".build/${{ matrix.artifact_dir }}" -mindepth 1 -maxdepth 1 ! -name artifacts -exec rm -rf {} + + cd ../../../../gp + gmake "${{ matrix.package_target }}" GPGUI_BINARY=../gpgui/target/release/gpgui + pkg_add -D unsigned ".build/${{ matrix.artifact_dir }}"/artifacts/*.tgz + gpclient --version + gpauth --version + gpgui --version + rm -rf target ../gpgui/target + find ".build/${{ matrix.artifact_dir }}" -mindepth 1 -maxdepth 1 ! -name artifacts -exec rm -rf {} + + ) >/dev/null 2>&1 + echo "BSD build passed." - name: Upload BSD package uses: actions/upload-artifact@v7 with: @@ -793,6 +1030,7 @@ jobs: artifact-metadata: write needs: - test-gp + - test-gpgui - nix-source - nix-prebuilt - tarball @@ -896,48 +1134,10 @@ jobs: run: | cd gh-release/gp/scripts && ./gh-release.sh "$RELEASE_TAG" - wait-gpgui-macos: - if: ${{ !cancelled() && needs.trigger-gpgui-macos.result == 'success' }} - needs: - - trigger-gpgui-macos - runs-on: ubuntu-latest - timeout-minutes: 130 - steps: - - name: Wait for private macOS build - env: - GH_TOKEN: ${{ secrets.GPGUI_ACTIONS_TOKEN }} - PRIVATE_RUN_ID: ${{ needs.trigger-gpgui-macos.outputs.run_id }} - PRIVATE_RUN_URL: ${{ needs.trigger-gpgui-macos.outputs.run_url }} - run: | - set -euo pipefail - - if [[ -z "$PRIVATE_RUN_ID" ]]; then - echo "The private macOS workflow was not started" >&2 - exit 1 - fi - for _ in $(seq 1 480); do - run="$(gh api "repos/yuezk/gpgui/actions/runs/$PRIVATE_RUN_ID")" - status="$(jq -r '.status' <<<"$run")" - conclusion="$(jq -r '.conclusion // ""' <<<"$run")" - if [[ "$status" == "completed" ]]; then - echo "Private macOS build completed: $PRIVATE_RUN_URL" - if [[ "$conclusion" != "success" ]]; then - echo "Private macOS build concluded with: $conclusion" >&2 - exit 1 - fi - exit 0 - fi - sleep 15 - done - - gh api --method POST \ - "repos/yuezk/gpgui/actions/runs/$PRIVATE_RUN_ID/cancel" >/dev/null || true - echo "Private macOS build timed out: $PRIVATE_RUN_URL" >&2 - exit 1 - publish-docker-image: needs: - test-gp + - test-gpgui - build-docker-image if: ${{ (github.event_name == 'workflow_dispatch' && !inputs.build_only) || (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')) }} runs-on: ubuntu-latest diff --git a/.github/workflows/publish-macos-assets.yaml b/.github/workflows/publish-macos-assets.yaml new file mode 100644 index 0000000..2a07d3a --- /dev/null +++ b/.github/workflows/publish-macos-assets.yaml @@ -0,0 +1,135 @@ +name: Publish macOS assets + +on: + workflow_dispatch: + inputs: + build_run_id: + description: 'Public gp Build run ID with a successful build-macos job' + required: true + type: string + release_tag: + description: 'Existing public release tag (vX.Y.Z or snapshot)' + required: true + type: string + +concurrency: + group: publish-macos-assets + cancel-in-progress: false + +permissions: + contents: write + actions: read + +jobs: + publish: + runs-on: ubuntu-latest + environment: macos-release + timeout-minutes: 15 + steps: + - uses: actions/checkout@v6 + - name: Check completed build and public release + id: build + env: + GH_TOKEN: ${{ github.token }} + BUILD_REPOSITORY: yuezk/GlobalProtect-openconnect + BUILD_RUN_ID: ${{ inputs.build_run_id }} + RELEASE_TAG: ${{ inputs.release_tag }} + run: | + set -euo pipefail + [[ "$BUILD_RUN_ID" =~ ^[0-9]+$ ]] + [[ "$RELEASE_TAG" == snapshot || "$RELEASE_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] + run="$(gh api "repos/$BUILD_REPOSITORY/actions/runs/$BUILD_RUN_ID")" + jq -e '(.event == "workflow_dispatch" or .event == "push") and + .path == ".github/workflows/build.yaml"' <<<"$run" >/dev/null + attempt="$(jq -r '.run_attempt' <<<"$run")" + artifact_name="$(bash scripts/select-macos-artifact.sh "$BUILD_REPOSITORY" "$BUILD_RUN_ID" "$attempt")" + { + echo "artifact=$artifact_name" + echo "workflow_commit=$(jq -r '.head_sha' <<<"$run")" + } >> "$GITHUB_OUTPUT" + gh release view "$RELEASE_TAG" \ + --repo yuezk/GlobalProtect-openconnect --json isDraft,isPrerelease \ + --jq 'select(.isDraft == false and .isPrerelease == false)' | jq -e . + + - name: Download completed build artifacts + uses: actions/download-artifact@v8 + with: + repository: yuezk/GlobalProtect-openconnect + github-token: ${{ github.token }} + run-id: ${{ inputs.build_run_id }} + name: ${{ steps.build.outputs.artifact }} + path: release-assets + + - name: Validate build identity and checksums + id: metadata + env: + RELEASE_TAG: ${{ inputs.release_tag }} + WORKFLOW_COMMIT: ${{ steps.build.outputs.workflow_commit }} + ARTIFACT_NAME: ${{ steps.build.outputs.artifact }} + run: | + set -euo pipefail + metadata="$(python3 scripts/validate-macos-release.py release-assets \ + --release-tag "$RELEASE_TAG" --workflow-commit "$WORKFLOW_COMMIT")" + [[ "$ARTIFACT_NAME" == "macos-release-$(jq -r '.request_id' <<<"$metadata")" ]] + { + echo "gp_ref=$(jq -r '.gp_ref' <<<"$metadata")" + echo "gpgui_ref=$(jq -r '.gpgui_ref' <<<"$metadata")" + echo "channel=$(jq -r '.channel' <<<"$metadata")" + } >> "$GITHUB_OUTPUT" + + - name: Checkout public upload helper + uses: actions/checkout@v6 + with: + repository: yuezk/GlobalProtect-openconnect + ref: ${{ steps.metadata.outputs.gp_ref }} + path: gp-release + + - name: Check stable tags + if: ${{ steps.metadata.outputs.channel == 'stable' }} + env: + GH_TOKEN: ${{ secrets.GH_PAT }} + RELEASE_TAG: ${{ inputs.release_tag }} + GP_REF: ${{ steps.metadata.outputs.gp_ref }} + GPGUI_REF: ${{ steps.metadata.outputs.gpgui_ref }} + run: | + set -euo pipefail + git -C gp-release fetch --no-tags origin "refs/tags/$RELEASE_TAG" + if [[ "$(git -C gp-release rev-parse 'FETCH_HEAD^{commit}')" != "$GP_REF" ]]; then + echo "gp source commit does not match $RELEASE_TAG" >&2 + exit 1 + fi + if ! tagged_gui_commit="$(gh api "repos/yuezk/gpgui/commits/$RELEASE_TAG" --jq .sha 2>/dev/null)"; then + echo "Private source tag verification failed; diagnostics suppressed." >&2 + exit 1 + fi + if [[ "$tagged_gui_commit" != "$GPGUI_REF" ]]; then + echo "gpgui source commit does not match $RELEASE_TAG" >&2 + exit 1 + fi + + - name: Publish and verify macOS assets + env: + GH_TOKEN: ${{ github.token }} + RELEASE_REPOSITORY: yuezk/GlobalProtect-openconnect + RELEASE_TAG: ${{ inputs.release_tag }} + CHANNEL: ${{ steps.metadata.outputs.channel }} + run: | + set -euo pipefail + files=(release-assets/*.dmg release-assets/*.zip release-assets/*.sha256) + args=() + if [[ "$CHANNEL" == snapshot ]]; then + args+=(--clobber) + fi + gp-release/scripts/upload-release-assets.sh "${args[@]}" "$RELEASE_TAG" "${files[@]}" + gh release download "$RELEASE_TAG" --repo yuezk/GlobalProtect-openconnect \ + --pattern 'GPConnect_*_arm64.*' --dir published-assets + python3 - <<'PY' + import hashlib + import json + from pathlib import Path + metadata = json.loads(Path("release-assets/release-build.json").read_text()) + for name, digest in metadata["assets"].items(): + if hashlib.sha256((Path("published-assets") / name).read_bytes()).hexdigest() != digest: + raise SystemExit(f"Published asset checksum mismatch: {name}") + PY + echo "Published verified macOS assets to $RELEASE_TAG" >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/publish-update-feeds.yaml b/.github/workflows/publish-update-feeds.yaml new file mode 100644 index 0000000..d2879f1 --- /dev/null +++ b/.github/workflows/publish-update-feeds.yaml @@ -0,0 +1,620 @@ +name: Publish update feeds + +on: + workflow_call: + secrets: + GH_PAT: + required: true + CLOUDFLARE_ACCOUNT_ID: + required: false + CLOUDFLARE_API_TOKEN: + required: false + SPARKLE_PRIVATE_KEY: + required: false + inputs: + gpgui_ref: + required: true + type: string + channel: + required: true + type: string + release_tag: + required: true + type: string + archive_name: + required: false + type: string + gp_ref: + required: true + type: string + highlights: + required: false + type: string + unix_platforms: + required: false + default: linux,freebsd,openbsd + type: string + workflow_dispatch: + inputs: + gpgui_ref: + description: gpgui ref containing the update feed Worker + required: true + default: main + type: string + channel: + description: Update channel + required: true + type: choice + options: + - stable + - snapshot + release_tag: + description: Release containing the macOS update archive + required: true + type: string + archive_name: + description: Exact ZIP asset name; leave empty when the release has one macOS update archive + required: false + type: string + gp_ref: + description: GlobalProtect-openconnect ref containing release helpers + required: true + default: main + type: string + highlights: + description: Stable release highlights, one per line + required: false + type: string + unix_platforms: + description: Comma-separated Unix platforms with published assets (linux,freebsd,openbsd) + required: true + default: linux,freebsd,openbsd + type: string + +concurrency: + group: update-feeds + cancel-in-progress: false + +jobs: + publish: + runs-on: ${{ vars.MACOS_RUNNER || 'macos-15' }} + environment: macos-release + permissions: + contents: write + env: + REPOSITORY: yuezk/GlobalProtect-openconnect + RELEASE_TAG: ${{ inputs.release_tag }} + CHANNEL: ${{ inputs.channel }} + REQUESTED_ARCHIVE: ${{ inputs.archive_name }} + FEED_URL: ${{ vars.SPARKLE_FEED_URL }} + EXPECTED_TEAM_ID: ${{ vars.MACOS_TEAM_ID }} + UPDATES_WORKER_NAME: ${{ vars.UPDATES_WORKER_NAME || 'gp-updates' }} + SPARKLE_VERSION: 2.9.2 + UPDATE_HIGHLIGHTS: ${{ inputs.highlights }} + UNIX_PLATFORMS: ${{ inputs.unix_platforms }} + SPARKLE_ARCHIVE_SHA256: b83e37436774556ed055e0244b297ef2c790e0737393bf65bf495fcbba6eed65 + steps: + - name: Validate inputs + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + SPARKLE_PRIVATE_KEY: ${{ secrets.SPARKLE_PRIVATE_KEY }} + run: | + set -euo pipefail + + if [[ "$CHANNEL" != "stable" && "$CHANNEL" != "snapshot" ]]; then + echo "channel must be stable or snapshot" >&2 + exit 1 + fi + if [[ ! "$UNIX_PLATFORMS" =~ ^(linux|freebsd|openbsd)(,(linux|freebsd|openbsd))*$ ]]; then + echo "unix_platforms must list published Unix targets separated by commas" >&2 + exit 1 + fi + if [[ "$CHANNEL" == "snapshot" && "$RELEASE_TAG" != "snapshot" ]]; then + echo "The snapshot channel must publish to the snapshot release" >&2 + exit 1 + fi + if [[ "$CHANNEL" == "stable" && "$RELEASE_TAG" == "snapshot" ]]; then + echo "The stable channel cannot publish to the snapshot release" >&2 + exit 1 + fi + if [[ "$CHANNEL" == "stable" && -z "${UPDATE_HIGHLIGHTS//[$'\t\r\n ']/}" ]]; then + echo "Stable updates require at least one release highlight" >&2 + exit 1 + fi + if [[ -z "$SPARKLE_PRIVATE_KEY" ]]; then + echo "SPARKLE_PRIVATE_KEY is not configured" >&2 + exit 1 + fi + if [[ -z "$EXPECTED_TEAM_ID" ]]; then + echo "MACOS_TEAM_ID is not configured" >&2 + exit 1 + fi + for variable in CLOUDFLARE_ACCOUNT_ID CLOUDFLARE_API_TOKEN; do + if [[ -z "${!variable:-}" ]]; then + echo "$variable is not configured" >&2 + exit 1 + fi + done + if [[ "$FEED_URL" != https://*/appcast.xml ]]; then + echo "SPARKLE_FEED_URL must be an HTTPS URL ending in /appcast.xml" >&2 + exit 1 + fi + + - name: Checkout release infrastructure + uses: actions/checkout@v6 + + - name: Checkout public release helpers + uses: actions/checkout@v6 + with: + repository: yuezk/GlobalProtect-openconnect + ref: ${{ inputs.gp_ref }} + path: gp-release + + - name: Checkout update feed Worker + uses: ./.github/actions/checkout-private + with: + token: ${{ secrets.GH_PAT }} + ref: ${{ inputs.gpgui_ref }} + path: gpgui + + - uses: pnpm/action-setup@v5 + with: + version: 9.15.1 + + - uses: actions/setup-node@v6 + with: + node-version: 22 + + - name: Install Worker tooling + run: | + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + pnpm --dir gpgui/apps/update-feed-worker install --frozen-lockfile --ignore-scripts + ) >/dev/null 2>&1 + echo "Private operation passed." + + + - name: Prepare Sparkle tools + run: | + set -euo pipefail + + archive="$RUNNER_TEMP/Sparkle.zip" + curl --fail --location --silent --show-error \ + "https://github.com/sparkle-project/Sparkle/releases/download/$SPARKLE_VERSION/Sparkle-for-Swift-Package-Manager.zip" \ + --output "$archive" + echo "$SPARKLE_ARCHIVE_SHA256 $archive" | shasum -a 256 --check + ditto -x -k "$archive" "$RUNNER_TEMP/sparkle" + test -x "$RUNNER_TEMP/sparkle/bin/generate_appcast" + + - name: Validate GitHub release + id: release + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + + release="$(gh release view "$RELEASE_TAG" --repo "$REPOSITORY" \ + --json tagName,isDraft,isPrerelease,publishedAt)" + if [[ "$(jq -r '.tagName' <<<"$release")" != "$RELEASE_TAG" ]] || \ + [[ "$(jq -r '.isDraft' <<<"$release")" != "false" ]]; then + echo "Release must be published with the requested tag" >&2 + exit 1 + fi + is_prerelease="$(jq -r '.isPrerelease' <<<"$release")" + if [[ "$is_prerelease" != "false" ]]; then + echo "Updates require a non-prerelease GitHub release" >&2 + exit 1 + fi + published_at="$(jq -r '.publishedAt' <<<"$release")" + if [[ ! "$published_at" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T ]]; then + echo "Release has no valid publication date" >&2 + exit 1 + fi + echo "released_at=${published_at%%T*}" >> "$GITHUB_OUTPUT" + + - name: Download update archive + id: archive + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + + mkdir -p "$RUNNER_TEMP/updates" + archive_name="$REQUESTED_ARCHIVE" + if [[ -z "$archive_name" ]]; then + archive_name="$(gh release view "$RELEASE_TAG" --repo "$REPOSITORY" --json assets \ + --jq '[.assets[] | select(.name | test("^GPConnect_[0-9A-Za-z._-]+_arm64\\.zip$"))] | sort_by(.createdAt) | last | .name // ""')" + if [[ -z "$archive_name" ]]; then + echo "No macOS update archive found on $RELEASE_TAG" >&2 + exit 1 + fi + fi + + if [[ ! "$archive_name" =~ ^GPConnect_[0-9A-Za-z._-]+_arm64\.zip$ ]]; then + echo "Invalid macOS update archive name: $archive_name" >&2 + exit 1 + fi + + gh release download "$RELEASE_TAG" --repo "$REPOSITORY" \ + --pattern "$archive_name" \ + --pattern "$archive_name.sha256" \ + --dir "$RUNNER_TEMP/updates" + { + echo "name=$archive_name" + echo "path=$RUNNER_TEMP/updates/$archive_name" + echo "checksum_path=$RUNNER_TEMP/updates/$archive_name.sha256" + } >> "$GITHUB_OUTPUT" + + - name: Verify update archive + id: bundle + env: + ARCHIVE_PATH: ${{ steps.archive.outputs.path }} + CHECKSUM_PATH: ${{ steps.archive.outputs.checksum_path }} + run: | + set -euo pipefail + + expected_digest="$(awk 'NR == 1 { print $1 }' "$CHECKSUM_PATH")" + actual_digest="$(shasum -a 256 "$ARCHIVE_PATH" | awk '{ print $1 }')" + if [[ -z "$expected_digest" || "$actual_digest" != "$expected_digest" ]]; then + echo "Update archive checksum does not match its release checksum" >&2 + exit 1 + fi + + extract_dir="$RUNNER_TEMP/extracted" + ditto -x -k "$ARCHIVE_PATH" "$extract_dir" + app="$extract_dir/GP Connect.app" + test -d "$app" + codesign --verify --deep --strict --verbose=2 "$app" + + signing_info="$(codesign -d --verbose=4 "$app" 2>&1)" + xcrun stapler validate "$app" + grep -q '^Authority=Developer ID Application:' <<<"$signing_info" + grep -q '^CodeDirectory .*flags=.*runtime' <<<"$signing_info" + team_identifier="$(sed -n 's/^TeamIdentifier=//p' <<<"$signing_info")" + if [[ "$team_identifier" != "$EXPECTED_TEAM_ID" ]]; then + echo "Unexpected Apple Team Identifier: $team_identifier" >&2 + exit 1 + fi + + identifier="$(sed -n 's/^Identifier=//p' <<<"$signing_info")" + if [[ "$identifier" != "com.yuezk.gpgui" ]]; then + echo "Unexpected bundle identifier: $identifier" >&2 + exit 1 + fi + file "$app/Contents/MacOS/gpgui" | grep -q 'arm64' + + feed_url="$(plutil -extract SUFeedURL raw "$app/Contents/Info.plist")" + if [[ "$feed_url" != "$FEED_URL" ]]; then + echo "Unexpected Sparkle feed URL: $feed_url" >&2 + exit 1 + fi + + update_channel="$(plutil -extract GPUpdateChannel raw "$app/Contents/Info.plist")" + if [[ "$update_channel" != "$CHANNEL" ]]; then + echo "Unexpected update channel: $update_channel" >&2 + exit 1 + fi + + bundle_version="$(plutil -extract CFBundleVersion raw "$app/Contents/Info.plist")" + short_version="$(plutil -extract CFBundleShortVersionString raw "$app/Contents/Info.plist")" + if [[ ! "$bundle_version" =~ ^[0-9]+(\.[0-9]+){0,2}$ ]]; then + echo "Invalid bundle version: $bundle_version" >&2 + exit 1 + fi + if [[ "$CHANNEL" == "stable" ]]; then + expected_tag="v$short_version" + expected_archive="GPConnect_${short_version}_arm64.zip" + else + expected_tag="snapshot" + expected_archive="GPConnect_${short_version}-snapshot.${bundle_version}_arm64.zip" + fi + if [[ "$RELEASE_TAG" != "$expected_tag" ]]; then + echo "Release tag $RELEASE_TAG does not match $expected_tag" >&2 + exit 1 + fi + if [[ "${{ steps.archive.outputs.name }}" != "$expected_archive" ]]; then + echo "Archive name does not match packaged version: $expected_archive" >&2 + exit 1 + fi + echo "version=$bundle_version" >> "$GITHUB_OUTPUT" + echo "short_version=$short_version" >> "$GITHUB_OUTPUT" + + - name: Prepare Unix update manifest + env: + RELEASED_AT: ${{ steps.release.outputs.released_at }} + run: | + set -euo pipefail + + manifest="$RUNNER_TEMP/updates/latest.json" + current_manifest="$RUNNER_TEMP/current-latest.json" + latest_url="${FEED_URL%/appcast.xml}/latest.json" + status="$(curl --location --silent --show-error \ + --output "$current_manifest" --write-out '%{http_code}' "$latest_url")" + generate_args=( + --repository "$REPOSITORY" + --channel "$CHANNEL" + --version "${{ steps.bundle.outputs.short_version }}" + ) + IFS=',' read -r -a platforms <<<"$UNIX_PLATFORMS" + for platform in "${platforms[@]}"; do + generate_args+=(--platform "$platform") + done + case "$status" in + 200) + python3 scripts/validate-update-manifest.py validate "$current_manifest" \ + --repository "$REPOSITORY" + generate_args+=(--input "$current_manifest") + ;; + 404) + ;; + *) + echo "Could not check the current Unix update manifest: HTTP $status" >&2 + exit 1 + ;; + esac + + highlights="$RUNNER_TEMP/update-highlights.txt" + if [[ "$CHANNEL" == "stable" ]]; then + printf '%s\n' "$UPDATE_HIGHLIGHTS" > "$highlights" + released_at="$RELEASED_AT" + else + printf 'Development snapshot after %s.\n' \ + "${{ steps.bundle.outputs.short_version }}" > "$highlights" + generate_args+=(--build "${{ steps.bundle.outputs.version }}") + released_at="$(date -u +%F)" + fi + + python3 scripts/validate-update-manifest.py generate \ + "${generate_args[@]}" \ + --released-at "$released_at" \ + --highlights-file "$highlights" \ + --output "$manifest" + + - name: Fetch current appcast + id: current-appcast + env: + ARCHIVE_NAME: ${{ steps.archive.outputs.name }} + run: | + set -euo pipefail + + appcast="$RUNNER_TEMP/updates/appcast.xml" + archive_url="https://github.com/$REPOSITORY/releases/download/$RELEASE_TAG/$ARCHIVE_NAME" + status="$(curl --location --silent --show-error \ + --output "$appcast" --write-out '%{http_code}' "$FEED_URL")" + case "$status" in + 200) + test -s "$appcast" + decision="$(python3 scripts/validate-macos-appcast.py status "$appcast" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}")" + if [[ "$decision" == "older" ]]; then + echo "Candidate build is older than the published appcast" >&2 + exit 1 + fi + if [[ "$decision" == "same" ]]; then + python3 scripts/validate-macos-appcast.py validate "$appcast" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}" \ + --short-version "${{ steps.bundle.outputs.short_version }}" \ + --channel "$CHANNEL" \ + --archive-url "$archive_url" + fi + echo "decision=$decision" >> "$GITHUB_OUTPUT" + ;; + 404) + rm -f "$appcast" + echo "decision=newer" >> "$GITHUB_OUTPUT" + ;; + *) + echo "Could not fetch the current appcast: HTTP $status" >&2 + exit 1 + ;; + esac + + - name: Generate appcast + if: ${{ steps.current-appcast.outputs.decision == 'newer' }} + env: + ARCHIVE_NAME: ${{ steps.archive.outputs.name }} + SPARKLE_PRIVATE_KEY: ${{ secrets.SPARKLE_PRIVATE_KEY }} + run: | + set -euo pipefail + + embedded_public_key="$(plutil -extract SUPublicEDKey raw \ + "$RUNNER_TEMP/extracted/GP Connect.app/Contents/Info.plist")" + derived_public_key="$(swift -e ' + import Foundation + import CryptoKit + let encoded = ProcessInfo.processInfo.environment["SPARKLE_PRIVATE_KEY"]! + .trimmingCharacters(in: .whitespacesAndNewlines) + let privateKey = try Curve25519.Signing.PrivateKey( + rawRepresentation: Data(base64Encoded: encoded)!) + print(privateKey.publicKey.rawRepresentation.base64EncodedString()) + ')" + if [[ "$derived_public_key" != "$embedded_public_key" ]]; then + echo "Sparkle private key does not match the public key embedded in the application" >&2 + exit 1 + fi + + download_prefix="https://github.com/$REPOSITORY/releases/download/$RELEASE_TAG/" + generate_args=( + --download-url-prefix "$download_prefix" + --full-release-notes-url "https://github.com/$REPOSITORY/releases/tag/$RELEASE_TAG" + --maximum-deltas 0 + --maximum-versions 1 + --versions "${{ steps.bundle.outputs.version }}" + --ed-key-file - + ) + if [[ "$CHANNEL" == "snapshot" ]]; then + generate_args+=(--channel snapshot) + fi + printf '%s' "$SPARKLE_PRIVATE_KEY" | \ + "$RUNNER_TEMP/sparkle/bin/generate_appcast" \ + "${generate_args[@]}" "$RUNNER_TEMP/updates" + + appcast="$RUNNER_TEMP/updates/appcast.xml" + test -s "$appcast" + python3 scripts/validate-macos-appcast.py validate "$appcast" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}" \ + --short-version "${{ steps.bundle.outputs.short_version }}" \ + --channel "$CHANNEL" \ + --archive-url "$download_prefix$ARCHIVE_NAME" + + - name: Publish appcast + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + + appcast="$RUNNER_TEMP/updates/appcast.xml" + archive_url="https://github.com/$REPOSITORY/releases/download/$RELEASE_TAG/${{ steps.archive.outputs.name }}" + if [[ "$CHANNEL" == "stable" ]] && \ + gh release view "$RELEASE_TAG" --repo "$REPOSITORY" --json assets \ + --jq '.assets[].name' | grep -Fxq appcast.xml; then + existing="$RUNNER_TEMP/existing-release-appcast.xml" + gh release download "$RELEASE_TAG" --repo "$REPOSITORY" \ + --pattern appcast.xml --output "$existing" + python3 scripts/validate-macos-appcast.py validate "$existing" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}" \ + --short-version "${{ steps.bundle.outputs.short_version }}" \ + --channel "$CHANNEL" \ + --archive-url "$archive_url" + else + if [[ "$CHANNEL" == "snapshot" ]]; then + RELEASE_REPOSITORY="$REPOSITORY" \ + gp-release/scripts/upload-release-assets.sh \ + --clobber "$RELEASE_TAG" "$appcast" + else + RELEASE_REPOSITORY="$REPOSITORY" \ + gp-release/scripts/upload-release-assets.sh "$RELEASE_TAG" "$appcast" + fi + fi + + - name: Validate final appcast + env: + ARCHIVE_NAME: ${{ steps.archive.outputs.name }} + run: | + python3 scripts/validate-macos-appcast.py validate \ + "$RUNNER_TEMP/updates/appcast.xml" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}" \ + --short-version "${{ steps.bundle.outputs.short_version }}" \ + --channel "$CHANNEL" \ + --archive-url "https://github.com/$REPOSITORY/releases/download/$RELEASE_TAG/$ARCHIVE_NAME" + + - name: Deploy update feed worker + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + run: | + trap 'status=$?; if [ "$status" -ne 0 ]; then echo "::error::Private operation failed; diagnostics suppressed."; fi' EXIT + ( + set -euo pipefail + + cp "$RUNNER_TEMP/updates/appcast.xml" gpgui/apps/update-feed-worker/public/appcast.xml + if [[ -f "$RUNNER_TEMP/updates/latest.json" ]]; then + cp "$RUNNER_TEMP/updates/latest.json" gpgui/apps/update-feed-worker/public/latest.json + fi + pnpm --dir gpgui/apps/update-feed-worker exec wrangler deploy \ + --config wrangler.jsonc \ + --name "$UPDATES_WORKER_NAME" + ) >/dev/null 2>&1 + echo "Private operation passed." + + - name: Verify published appcast + env: + ARCHIVE_NAME: ${{ steps.archive.outputs.name }} + run: | + set -euo pipefail + + published_appcast="$RUNNER_TEMP/published-appcast.xml" + for _ in 1 2 3 4 5 6 7 8 9 10; do + if curl --fail --location --silent --show-error \ + --output "$published_appcast" "$FEED_URL" && \ + python3 scripts/validate-macos-appcast.py validate "$published_appcast" \ + --repository "$REPOSITORY" \ + --build-version "${{ steps.bundle.outputs.version }}" \ + --short-version "${{ steps.bundle.outputs.short_version }}" \ + --channel "$CHANNEL" \ + --archive-url "https://github.com/$REPOSITORY/releases/download/$RELEASE_TAG/$ARCHIVE_NAME"; then + exit 0 + fi + sleep 3 + done + echo "Published appcast did not become available at $FEED_URL" >&2 + exit 1 + + - name: Verify published Unix update manifest + run: | + set -euo pipefail + + latest_url="${FEED_URL%/appcast.xml}/latest.json" + published_manifest="$RUNNER_TEMP/published-latest.json" + expected=( + --channel "$CHANNEL" + --version "${{ steps.bundle.outputs.short_version }}" + ) + if [[ "$CHANNEL" == "snapshot" ]]; then + expected+=(--build "${{ steps.bundle.outputs.version }}") + fi + for _ in 1 2 3 4 5 6 7 8 9 10; do + if curl --fail --location --silent --show-error \ + --output "$published_manifest" "$latest_url" && \ + python3 scripts/validate-update-manifest.py validate "$published_manifest" \ + --repository "$REPOSITORY" "${expected[@]}"; then + if ! cmp --silent "$RUNNER_TEMP/updates/latest.json" "$published_manifest"; then + sleep 3 + continue + fi + exit 0 + fi + sleep 3 + done + echo "Published Unix update manifest did not become available at $latest_url" >&2 + exit 1 + + - name: Prune expired snapshot assets + if: ${{ inputs.channel == 'snapshot' }} + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + + assets="$RUNNER_TEMP/snapshot-assets.json" + delete_list="$RUNNER_TEMP/delete-snapshot-assets.txt" + gh release view snapshot --repo "$REPOSITORY" --json assets > "$assets" + ASSETS="$assets" python3 - <<'PY' > "$delete_list" + import json + import os + import re + + with open(os.environ["ASSETS"], encoding="utf-8") as file: + assets = json.load(file)["assets"] + + archive_pattern = re.compile( + r"^(GPConnect_.+-snapshot\.(\d+(?:\.\d+){1,2}))_arm64\.zip$" + ) + asset_pattern = re.compile( + r"^(GPConnect_.+-snapshot\.\d+(?:\.\d+){1,2})_arm64\.(?:zip|dmg)(?:\.sha256)?$" + ) + builds = [] + for asset in assets: + match = archive_pattern.match(asset["name"]) + if match: + builds.append((tuple(map(int, match.group(2).split("."))), match.group(1))) + keep = {stem for _, stem in sorted(builds, reverse=True)[:2]} + for asset in assets: + match = asset_pattern.match(asset["name"]) + if match and not any(asset["name"].startswith(stem + "_arm64.") for stem in keep): + print(asset["name"]) + PY + + while IFS= read -r asset; do + if [[ -n "$asset" ]] && ! gh release delete-asset snapshot "$asset" \ + --repo "$REPOSITORY" --yes; then + echo "::warning::Could not remove expired snapshot asset: $asset" >&2 + fi + done < "$delete_list" diff --git a/.github/workflows/release-checks.yaml b/.github/workflows/release-checks.yaml index c0e5485..cb6b2ff 100644 --- a/.github/workflows/release-checks.yaml +++ b/.github/workflows/release-checks.yaml @@ -4,18 +4,34 @@ on: pull_request: paths: - '.github/workflows/**' + - '.github/actions/**' + - 'scripts/select-macos-artifact.sh' + - 'scripts/test-private-desktop.sh' + - 'scripts/tests/test_private_ci.py' - 'scripts/wait-for-publication.py' - 'scripts/select-publication-targets.py' - 'scripts/verify-docker-publication.sh' - 'scripts/tests/test_*publication.py' + - 'scripts/validate-macos-release.py' + - 'scripts/validate-macos-appcast.py' + - 'scripts/validate-update-manifest.py' + - 'scripts/tests/test_validate_*.py' push: branches: [main] paths: - '.github/workflows/**' + - '.github/actions/**' + - 'scripts/select-macos-artifact.sh' + - 'scripts/test-private-desktop.sh' + - 'scripts/tests/test_private_ci.py' - 'scripts/wait-for-publication.py' - 'scripts/select-publication-targets.py' - 'scripts/verify-docker-publication.sh' - 'scripts/tests/test_*publication.py' + - 'scripts/validate-macos-release.py' + - 'scripts/validate-macos-appcast.py' + - 'scripts/validate-update-manifest.py' + - 'scripts/tests/test_validate_*.py' permissions: contents: read @@ -36,3 +52,11 @@ jobs: shellcheck scripts/verify-docker-publication.sh - name: Test publication verification run: python3 -m unittest discover -s scripts/tests -p 'test_*publication.py' + - name: Test macOS publication and update feed validation + run: python3 -m unittest discover -s scripts/tests -p 'test_validate_*.py' + - name: Check private CI and macOS artifact selection + run: | + bash -n scripts/select-macos-artifact.sh + shellcheck scripts/select-macos-artifact.sh scripts/test-private-desktop.sh + bash -n scripts/test-private-desktop.sh + python3 -m unittest discover -s scripts/tests -p 'test_private_ci.py' diff --git a/scripts/select-macos-artifact.sh b/scripts/select-macos-artifact.sh new file mode 100644 index 0000000..0abf094 --- /dev/null +++ b/scripts/select-macos-artifact.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Find the latest execution of build-macos, retaining builds across unrelated reruns. +set -euo pipefail + +repository=$1 +run_id=$2 +latest_attempt=$3 +[[ "$run_id" =~ ^[0-9]+$ && "$latest_attempt" =~ ^[1-9][0-9]*$ ]] + +for ((attempt = latest_attempt; attempt >= 1; attempt--)); do + jobs=$(gh api --paginate "repos/$repository/actions/runs/$run_id/attempts/$attempt/jobs" \ + --jq '.jobs[] | select(.name == "build-macos")' | jq -s .) + if [[ $(jq length <<<"$jobs") == 0 ]]; then + continue + fi + if ! jq -e 'length == 1 and .[0].status == "completed" and .[0].conclusion == "success"' <<<"$jobs" >/dev/null; then + echo "The latest macOS job must have completed successfully." >&2 + exit 1 + fi + artifact_name="macos-release-$run_id.$attempt.macos" + artifacts=$(gh api --paginate "repos/$repository/actions/runs/$run_id/artifacts" \ + --jq '.artifacts[]' | jq -s --arg name "$artifact_name" '[.[] | select(.name == $name)]') + if ! jq -e 'length == 1 and .[0].expired == false' <<<"$artifacts" >/dev/null; then + echo "The latest macOS build must have one unexpired artifact." >&2 + exit 1 + fi + echo "$artifact_name" + exit 0 +done + +echo "No macOS job was found in this run." >&2 +exit 1 diff --git a/scripts/test-private-desktop.sh b/scripts/test-private-desktop.sh new file mode 100644 index 0000000..da475ac --- /dev/null +++ b/scripts/test-private-desktop.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Emit only host-owned stage names; never forward private command output. +set -euo pipefail + +source_directory=$1 +container="gp-private-tests-$$" +trap 'timeout --kill-after=5s 30s docker rm --force "$container" >/dev/null 2>&1 || true' EXIT + +run_stage() { + local name=$1 limit=$2 status started=$SECONDS + shift 2 + echo "$name: started (limit $limit)." + if timeout --kill-after=30s "$limit" "$@" >/dev/null 2>&1; then + echo "$name: passed ($((SECONDS - started))s)." + else + status=$? + if [[ "$status" == 124 ]]; then + echo "::error::$name: timed out after $((SECONDS - started))s (limit $limit); diagnostics suppressed." + elif [[ "$status" == 137 ]]; then + echo "::error::$name: killed after $((SECONDS - started))s (possible timeout or memory pressure); diagnostics suppressed." + else + echo "::error::$name: failed after $((SECONDS - started))s (exit $status); diagnostics suppressed." + fi + exit "$status" + fi +} + +run_stage 'Start test container' 5m docker run --detach --rm \ + --name "$container" \ + --user vscode \ + -e HOME=/home/vscode \ + -e CARGO_TARGET_DIR=/tmp/gpgui-test-target \ + -e COREPACK_INTEGRITY_KEYS=0 \ + -e LIBXML2_STATIC=1 \ + -v "$source_directory:/source:ro" \ + yuezk/gpdev:binary-builder-tauri2 sleep infinity + +# Tauri writes schemas into the checkout; keep the host source read-only. +# shellcheck disable=SC2016 # Expand id inside the container, not on the host. +run_stage 'Prepare test workspace' 5m docker exec "$container" sh -ec ' + test "$(id -u)" -ne 0 + cp -R /source /tmp/gpgui-test-workspace +' +workspace=/tmp/gpgui-test-workspace/gpgui +run_stage 'Install frontend dependencies' 10m \ + docker exec --workdir "$workspace" "$container" pnpm install --frozen-lockfile +run_stage 'Frontend tests' 5m \ + docker exec --workdir "$workspace" "$container" pnpm --filter gpgui test +run_stage 'Frontend build' 10m \ + docker exec --workdir "$workspace" "$container" pnpm --filter gpgui build +run_stage 'Compile Rust tests' 30m \ + docker exec --workdir "$workspace" "$container" cargo test --workspace --locked --no-run +run_stage 'Rust tests' 10m \ + docker exec --workdir "$workspace" "$container" cargo test --workspace --locked diff --git a/scripts/tests/test_private_ci.py b/scripts/tests/test_private_ci.py new file mode 100644 index 0000000..7e0b99d --- /dev/null +++ b/scripts/tests/test_private_ci.py @@ -0,0 +1,215 @@ +import json +import os +from pathlib import Path +import re +import subprocess +import sys +import tempfile +import textwrap +import unittest + + +ROOT = Path(__file__).resolve().parents[2] + + +def run_block(path, step=None): + source = (ROOT / path).read_text() + if step: + source = source.split('- name: ' + step + '\n', 1)[1] + match = re.search(r'(?m)^( +)run: \|\n', source) + indent = len(match[1]) + 2 + lines = [] + for line in source[match.end():].splitlines(): + if line.strip() and len(line) - len(line.lstrip()) < indent: + break + lines.append(line[indent:]) + return '\n'.join(lines) + + +class PrivateOutputTests(unittest.TestCase): + def test_private_checkout_supports_branch_tag_and_sha_without_retaining_token(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + origin = root / 'origin' + subprocess.run(['git', 'init', '-b', 'main', str(origin)], check=True, capture_output=True) + git_env = dict(os.environ, GIT_AUTHOR_NAME='Test', GIT_AUTHOR_EMAIL='test@example.com', + GIT_COMMITTER_NAME='Test', GIT_COMMITTER_EMAIL='test@example.com') + subprocess.run(['git', '-C', str(origin), 'commit', '--allow-empty', '-m', 'PRIVATE_SUBJECT'], + env=git_env, check=True, capture_output=True) + subprocess.run(['git', '-C', str(origin), 'tag', 'v1.0.0'], check=True) + sha = subprocess.check_output(['git', '-C', str(origin), 'rev-parse', 'HEAD'], text=True).strip() + config = root / 'gitconfig' + config.write_text(f'[url "{origin.as_uri()}"]\n insteadOf = https://github.com/yuezk/gpgui\n') + for index, ref in enumerate(('main', 'v1.0.0', sha)): + destination = root / f'checkout-{index}' + env = dict(os.environ, GIT_CONFIG_GLOBAL=str(config), PRIVATE_TOKEN='test-token', + PRIVATE_REF=ref, PRIVATE_PATH=str(destination)) + result = subprocess.run(['bash', '-e', '-o', 'pipefail', '-c', + run_block('.github/actions/checkout-private/action.yaml')], + env=env, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertNotIn('PRIVATE_SUBJECT', result.stdout + result.stderr) + self.assertNotIn('test-token', (destination / '.git/config').read_text()) + self.assertNotIn('extraheader', (destination / '.git/config').read_text()) + actual = subprocess.check_output(['git', '-C', str(destination), 'rev-parse', 'HEAD'], text=True) + self.assertEqual(actual.strip(), sha) + + def test_private_commands_hide_stdout_and_stderr_and_preserve_failure(self): + cases = [ + ('.github/workflows/build.yaml', 'Build macOS release artifacts'), + ('.github/workflows/build.yaml', 'Build gpgui frontend'), + ('.github/workflows/build-gpgui-linux.yaml', 'Build gpgui in Docker'), + ('.github/workflows/publish-update-feeds.yaml', 'Install Worker tooling'), + ('.github/workflows/publish-update-feeds.yaml', 'Deploy update feed worker'), + ('.github/actions/checkout-private/action.yaml', None), + ] + for path, step in cases: + for status in (0, 19): + with self.subTest(step=step, status=status), tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + stub = '#!/bin/sh\necho PRIVATE_STDOUT\necho PRIVATE_STDERR >&2\necho \"MACOS_STAGE: Signing\"\necho \"MACOS_STAGE: Signing PRIVATE_DETAIL\"\nexit "$TEST_STATUS"\n' + for command in ('git', 'docker', 'pnpm', 'source/gpgui/scripts/build-macos.sh'): + executable = root / command + executable.parent.mkdir(parents=True, exist_ok=True) + executable.write_text(stub) + executable.chmod(0o755) + (root / 'gpgui-frontend/gpgui/apps/desktop').mkdir(parents=True) + (root / 'gpgui/apps/update-feed-worker/public').mkdir(parents=True) + (root / 'updates').mkdir() + (root / 'updates/appcast.xml').write_text('fixture') + env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', + TEST_STATUS=str(status), CHANNEL='snapshot', GPGUI_BUILDER='fixture', + RUNNER_TEMP=str(root), UPDATES_WORKER_NAME='fixture', + PRIVATE_TOKEN='test-token', PRIVATE_REF='test-ref', PRIVATE_PATH='private') + result = subprocess.run(['bash', '-e', '-c', run_block(path, step)], + cwd=root, env=env, text=True, capture_output=True) + self.assertEqual(result.returncode, status, result.stdout + result.stderr) + self.assertNotIn('PRIVATE_STD', result.stdout + result.stderr) + self.assertNotIn('PRIVATE_DETAIL', result.stdout + result.stderr) + if step == 'Build macOS release artifacts': + self.assertIn('::notice::MACOS_STAGE: Signing', result.stdout) + self.assertNotIn('test-token', result.stdout + result.stderr) + self.assertIn('passed' if status == 0 else 'failed', result.stdout) + + +class PrivateTestStageTests(unittest.TestCase): + def run_stages(self, mode): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + docker = root / 'docker' + docker.write_text(textwrap.dedent('''\ + #!/usr/bin/env python3 + import json, os, sys, time + with open(os.environ['CALLS'], 'a') as output: + output.write(json.dumps(sys.argv[1:]) + '\\n') + print('PRIVATE_STDOUT') + print('PRIVATE_STDERR', file=sys.stderr) + if 'cargo' in sys.argv and '--no-run' not in sys.argv: + if os.environ['MODE'] == 'hang': + time.sleep(60) + if os.environ['MODE'] == 'fail': + sys.exit(19) + ''')) + timeout = root / 'timeout' + timeout.write_text(textwrap.dedent('''\ + #!/usr/bin/env python3 + import subprocess, sys + assert sys.argv[1].startswith('--kill-after=') + assert sys.argv[2].endswith(('m', 's')) + try: + result = subprocess.run(sys.argv[3:], timeout=2) + sys.exit(result.returncode) + except subprocess.TimeoutExpired: + sys.exit(124) + ''')) + for executable in (docker, timeout): + executable.write_text(executable.read_text().replace("#!/usr/bin/env python3", f"#!{sys.executable}")) + executable.chmod(0o755) + calls = root / 'calls.jsonl' + env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', MODE=mode, CALLS=str(calls)) + result = subprocess.run(['bash', str(ROOT / 'scripts/test-private-desktop.sh'), str(root)], + env=env, text=True, capture_output=True, timeout=10) + self.assertNotIn('PRIVATE_STD', result.stdout + result.stderr) + recorded = [json.loads(line) for line in calls.read_text().splitlines()] + self.assertEqual(recorded[-1][:2], ['rm', '--force']) + return result + + def test_success_reports_each_stage(self): + result = self.run_stages('success') + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(result.stdout.count(': started'), 7) + self.assertEqual(result.stdout.count(': passed'), 7) + self.assertIn('Compile Rust tests: passed', result.stdout) + self.assertIn('Rust tests: passed', result.stdout) + + def test_failure_names_stage_and_preserves_exit(self): + result = self.run_stages('fail') + self.assertEqual(result.returncode, 19) + self.assertIn('Rust tests: failed', result.stdout) + self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed') + + def test_hung_tests_report_timeout_and_cleanup(self): + result = self.run_stages('hang') + self.assertEqual(result.returncode, 124) + self.assertIn('Rust tests: timed out', result.stdout) + self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed') + + +class MacOSArtifactSelectionTests(unittest.TestCase): + def select(self, jobs, artifacts=None): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + gh = root / 'gh' + gh.write_text(textwrap.dedent('''\ + #!/usr/bin/env python3 + import json, os, re, sys + endpoint = sys.argv[3] + if endpoint.endswith('/artifacts'): + records = json.loads(os.environ['ARTIFACTS']) + else: + attempt = re.search(r'/attempts/([0-9]+)/jobs', endpoint)[1] + records = json.loads(os.environ['JOBS']).get(attempt, []) + for record in records: + print(json.dumps(record)) + ''')) + gh.chmod(0o755) + env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', JOBS=json.dumps(jobs), + ARTIFACTS=json.dumps(artifacts if artifacts is not None else [ + {'name': 'macos-release-42.1.macos', 'expired': False}])) + return subprocess.run(['bash', str(ROOT / 'scripts/select-macos-artifact.sh'), 'owner/repo', '42', '3'], + env=env, capture_output=True, text=True) + + def job(self, conclusion='success', status='completed'): + return {'name': 'build-macos', 'status': status, 'conclusion': conclusion} + + def test_unrelated_reruns_reuse_successful_macos_attempt(self): + result = self.select({'1': [self.job()]}) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), 'macos-release-42.1.macos') + + def test_newer_macos_execution_wins(self): + result = self.select({'1': [self.job()], '3': [self.job()]}, [ + {'name': f'macos-release-42.{attempt}.macos', 'expired': False} for attempt in (1, 3)]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), 'macos-release-42.3.macos') + + def test_never_falls_back_past_failed_cancelled_skipped_or_running_macos(self): + for conclusion, status in [('failure', 'completed'), ('cancelled', 'completed'), + ('skipped', 'completed'), (None, 'in_progress')]: + with self.subTest(conclusion=conclusion): + result = self.select({'1': [self.job()], '2': [self.job(conclusion, status)]}) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, '') + + def test_missing_expired_or_duplicate_artifacts_are_rejected(self): + valid = {'name': 'macos-release-42.1.macos', 'expired': False} + for artifacts in ([], [dict(valid, expired=True)], [valid, valid]): + with self.subTest(artifacts=artifacts): + self.assertNotEqual(self.select({'1': [self.job()]}, artifacts).returncode, 0) + + def test_missing_macos_job_is_rejected(self): + self.assertNotEqual(self.select({}).returncode, 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/scripts/tests/test_validate_macos_appcast.py b/scripts/tests/test_validate_macos_appcast.py new file mode 100644 index 0000000..6b25d07 --- /dev/null +++ b/scripts/tests/test_validate_macos_appcast.py @@ -0,0 +1,116 @@ +import subprocess +import tempfile +import unittest +from pathlib import Path + + +REPOSITORY = "yuezk/GlobalProtect-openconnect" +SCRIPT = Path(__file__).parents[1] / "validate-macos-appcast.py" + + +def item(version, short_version, archive, channel=None): + channel_xml = f"{channel}" if channel else "" + return f""" + + {version} + {short_version} + {channel_xml} + + + """ + + +def appcast(*items): + return f""" + + {''.join(items)} + + """ + + +class ValidateMacosAppcastTests(unittest.TestCase): + def run_validator(self, content, *arguments): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "appcast.xml" + path.write_text(content, encoding="utf-8") + return subprocess.run( + ["python3", str(SCRIPT), *arguments, str(path), "--repository", REPOSITORY], + check=False, + capture_output=True, + text=True, + ) + + def test_reports_candidate_status(self): + archive = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip" + content = appcast(item("806.1", "2.6.4", archive)) + + same = self.run_validator( + content, "status", "--build-version", "806.1" + ) + newer = self.run_validator( + content, "status", "--build-version", "807.1" + ) + older = self.run_validator( + content, "status", "--build-version", "805.1" + ) + + self.assertEqual((same.returncode, same.stdout.strip()), (0, "same")) + self.assertEqual((newer.returncode, newer.stdout.strip()), (0, "newer")) + self.assertEqual((older.returncode, older.stdout.strip()), (0, "older")) + + def test_validates_stable_and_snapshot_items(self): + stable = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip" + snapshot = ( + f"https://github.com/{REPOSITORY}/releases/download/snapshot/" + "GPConnect_2.6.4-snapshot.807.1_arm64.zip" + ) + content = appcast( + item("806.1", "2.6.4", stable), + item("807.1", "2.6.4", snapshot, "snapshot"), + ) + + result = self.run_validator( + content, + "validate", + "--build-version", + "807.1", + "--short-version", + "2.6.4", + "--channel", + "snapshot", + "--archive-url", + snapshot, + ) + + self.assertEqual(result.returncode, 0, result.stderr) + + def test_compares_builds_across_channels(self): + stable = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip" + snapshot = ( + f"https://github.com/{REPOSITORY}/releases/download/snapshot/" + "GPConnect_2.6.4-snapshot.807.1_arm64.zip" + ) + content = appcast( + item("2.6.4", "2.6.4", stable), + item("807.1", "2.6.4", snapshot, "snapshot"), + ) + + result = self.run_validator( + content, "status", "--build-version", "2.7.0" + ) + + self.assertEqual((result.returncode, result.stdout.strip()), (0, "older")) + + def test_rejects_external_archive_urls(self): + content = appcast(item("806.1", "2.6.4", "https://example.com/update.zip")) + + result = self.run_validator( + content, "status", "--build-version", "807.1" + ) + + self.assertNotEqual(result.returncode, 0) + self.assertIn("unexpected update archive URL", result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/tests/test_validate_macos_release.py b/scripts/tests/test_validate_macos_release.py new file mode 100644 index 0000000..c655b7f --- /dev/null +++ b/scripts/tests/test_validate_macos_release.py @@ -0,0 +1,99 @@ +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + +spec = importlib.util.spec_from_file_location("macos_release", Path(__file__).parents[1] / "validate-macos-release.py") +release = importlib.util.module_from_spec(spec) +spec.loader.exec_module(release) + + +class MacOSReleaseTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.directory = Path(self.temp.name) + self.commit = "a" * 40 + self.metadata = {"gp_ref": "b" * 40, "gpgui_ref": "d" * 40, "workflow_sha": self.commit, "channel": "stable", + "release_tag": "v3.0.1", "artifact_version": "3.0.1", + "request_id": "42.1.macos", "build_version": "903.1", "assets": {}} + for extension in ("dmg", "zip"): + name = f"GPConnect_3.0.1_arm64.{extension}" + content = b"signed artifact" + digest = hashlib.sha256(content).hexdigest() + (self.directory / name).write_bytes(content) + checksum = f"{digest} {name}\n".encode() + (self.directory / (name + ".sha256")).write_bytes(checksum) + self.metadata["assets"][name] = digest + self.metadata["assets"][name + ".sha256"] = hashlib.sha256(checksum).hexdigest() + self.save() + + def save(self): + (self.directory / "release-build.json").write_text(json.dumps(self.metadata)) + + def test_matching_build_passes(self): + self.assertEqual(release.validate(self.directory, "v3.0.1", self.commit), self.metadata) + + def test_another_release_cannot_use_this_build(self): + with self.assertRaisesRegex(ValueError, "release tag"): + release.validate(self.directory, "v3.0.2", self.commit) + + def test_another_run_commit_cannot_use_this_build(self): + with self.assertRaisesRegex(ValueError, "public workflow"): + release.validate(self.directory, "v3.0.1", "c" * 40) + + def test_source_commit_cannot_replace_workflow_identity(self): + with self.assertRaisesRegex(ValueError, "public workflow"): + release.validate(self.directory, "v3.0.1", self.metadata["gp_ref"]) + + def test_invalid_workflow_commit_is_rejected(self): + self.metadata["workflow_sha"] = "short" + self.save() + with self.assertRaisesRegex(ValueError, "Invalid workflow_sha"): + release.validate(self.directory, "v3.0.1", "short") + + def test_corrupt_download_is_rejected(self): + (self.directory / "GPConnect_3.0.1_arm64.zip").write_bytes(b"changed") + with self.assertRaisesRegex(ValueError, "checksum mismatch"): + release.validate(self.directory, "v3.0.1", self.commit) + + def test_unexpected_file_is_rejected(self): + (self.directory / "unexpected.dmg").write_bytes(b"extra") + with self.assertRaisesRegex(ValueError, "Unexpected"): + release.validate(self.directory, "v3.0.1", self.commit) + + def test_missing_checksum_is_rejected(self): + self.metadata["assets"].pop("GPConnect_3.0.1_arm64.zip.sha256") + self.save() + with self.assertRaisesRegex(ValueError, "exactly"): + release.validate(self.directory, "v3.0.1", self.commit) + + def test_snapshot_requires_matching_build_identity(self): + self.metadata.update(channel="snapshot", release_tag="snapshot", + artifact_version="3.0.1-snapshot.903.2") + self.save() + with self.assertRaisesRegex(ValueError, "snapshot artifact version"): + release.validate(self.directory, "snapshot", self.commit) + + def test_matching_snapshot_passes(self): + version = "3.0.1-snapshot.903.1" + assets = {} + for name in self.metadata["assets"]: + file = self.directory / name + new_name = name.replace("3.0.1", version) + content = file.read_bytes() + if name.endswith(".sha256"): + content = content.replace(b"3.0.1", version.encode()) + file.unlink() + (self.directory / new_name).write_bytes(content) + assets[new_name] = hashlib.sha256(content).hexdigest() + self.metadata.update(channel="snapshot", release_tag="snapshot", + artifact_version=version, assets=assets) + self.save() + self.assertEqual(release.validate(self.directory, "snapshot", self.commit), self.metadata) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/tests/test_validate_update_manifest.py b/scripts/tests/test_validate_update_manifest.py new file mode 100644 index 0000000..a6e96af --- /dev/null +++ b/scripts/tests/test_validate_update_manifest.py @@ -0,0 +1,177 @@ +import importlib.util +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +MODULE_PATH = Path(__file__).parents[1] / "validate-update-manifest.py" +SPEC = importlib.util.spec_from_file_location("validate_update_manifest", MODULE_PATH) +MODULE = importlib.util.module_from_spec(SPEC) +assert SPEC.loader is not None +SPEC.loader.exec_module(MODULE) + +REPOSITORY = "yuezk/GlobalProtect-openconnect" + + +class UpdateManifestTests(unittest.TestCase): + def stable(self, version="2.6.5"): + return { + "version": version, + "releasedAt": "2026-08-01", + "releaseUrl": f"https://github.com/{REPOSITORY}/releases/tag/v{version}", + "platforms": ["linux", "freebsd", "openbsd"], + "highlights": ["Stable release."], + } + + def snapshot(self, base_version="2.6.5", build="17.1"): + return { + "baseVersion": base_version, + "build": build, + "releasedAt": "2026-08-07", + "releaseUrl": f"https://github.com/{REPOSITORY}/releases/tag/snapshot", + "platforms": ["linux", "freebsd", "openbsd"], + "highlights": ["Development snapshot."], + } + + def manifest(self): + return {"schemaVersion": 1, "stable": self.stable(), "snapshot": self.snapshot()} + + def generate(self, directory, channel, version, build=None, input_manifest=None): + directory = Path(directory) + highlights = directory / f"{channel}-highlights.txt" + output = directory / "latest.json" + highlights.write_text(f"{channel.title()} change.\n", encoding="utf-8") + args = type( + "Args", + (), + { + "repository": REPOSITORY, + "channel": channel, + "version": version, + "build": build, + "platform": None, + "released_at": "2026-08-07", + "highlights_file": highlights, + "input": input_manifest, + "output": output, + }, + )() + MODULE.generate(args) + return json.loads(output.read_text(encoding="utf-8")) + + def test_validates_catalog_with_both_channels(self): + result = MODULE.validate_manifest(self.manifest(), REPOSITORY) + + self.assertEqual(result["stable"]["version"], "2.6.5") + self.assertEqual(result["snapshot"]["build"], "17.1") + + def test_accepts_catalog_with_one_channel(self): + manifest = self.manifest() + manifest["stable"] = None + + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_rejects_empty_catalog_and_unknown_fields(self): + with self.assertRaisesRegex(ValueError, "stable or snapshot"): + MODULE.validate_manifest({"schemaVersion": 1, "stable": None, "snapshot": None}, REPOSITORY) + + manifest = self.manifest() + manifest["extra"] = True + with self.assertRaisesRegex(ValueError, "top-level"): + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_rejects_invalid_channel_urls(self): + manifest = self.manifest() + manifest["stable"]["releaseUrl"] = f"https://github.com/{REPOSITORY}/releases/tag/snapshot" + with self.assertRaisesRegex(ValueError, "stable releaseUrl"): + MODULE.validate_manifest(manifest, REPOSITORY) + + manifest = self.manifest() + manifest["snapshot"]["releaseUrl"] = f"https://github.com/{REPOSITORY}/releases/tag/v2.6.5" + with self.assertRaisesRegex(ValueError, "snapshot releaseUrl"): + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_rejects_invalid_snapshot_build(self): + for build in ("", "17.beta", "01.1", "1.2.3.4", f"{2**64}.1"): + with self.subTest(build=build): + manifest = self.manifest() + manifest["snapshot"]["build"] = build + with self.assertRaisesRegex(ValueError, "snapshot build"): + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_rejects_version_components_larger_than_the_client_supports(self): + manifest = self.manifest() + manifest["stable"] = self.stable(f"{2**64}.0.0") + + with self.assertRaisesRegex(ValueError, "integer range"): + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_rejects_unknown_and_duplicate_platforms(self): + manifest = self.manifest() + manifest["stable"]["platforms"] = ["linux", "netbsd"] + with self.assertRaisesRegex(ValueError, "unsupported"): + MODULE.validate_manifest(manifest, REPOSITORY) + + manifest["stable"]["platforms"] = ["linux", "linux"] + with self.assertRaisesRegex(ValueError, "duplicates"): + MODULE.validate_manifest(manifest, REPOSITORY) + + def test_generates_and_merges_channels(self): + with tempfile.TemporaryDirectory() as directory: + generated = self.generate(directory, "stable", "2.6.5") + self.assertIsNone(generated["snapshot"]) + self.assertEqual(generated["stable"]["platforms"], ["linux", "freebsd", "openbsd"]) + + current = Path(directory) / "current.json" + current.write_text(json.dumps(generated), encoding="utf-8") + generated = self.generate(directory, "snapshot", "2.6.5", "17.1", current) + + self.assertEqual(generated["stable"]["version"], "2.6.5") + self.assertEqual(generated["snapshot"]["baseVersion"], "2.6.5") + self.assertEqual(generated["snapshot"]["build"], "17.1") + + def test_cli_limits_stable_to_published_platforms_and_preserves_snapshot(self): + with tempfile.TemporaryDirectory() as directory: + directory = Path(directory) + current = directory / "current.json" + current.write_text(json.dumps(self.manifest()), encoding="utf-8") + highlights = directory / "highlights.txt" + highlights.write_text("Refreshed client icons.\n", encoding="utf-8") + output = directory / "latest.json" + result = subprocess.run( + [sys.executable, str(MODULE_PATH), "generate", "--repository", REPOSITORY, + "--channel", "stable", "--version", "3.0.5", "--platform", "linux", + "--released-at", "2026-10-13", "--highlights-file", str(highlights), + "--input", str(current), "--output", str(output)], + capture_output=True, text=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + generated = json.loads(output.read_text(encoding="utf-8")) + self.assertEqual(generated["stable"]["platforms"], ["linux"]) + self.assertEqual(generated["snapshot"], self.snapshot()) + + def test_rejects_stable_and_snapshot_rollbacks(self): + with tempfile.TemporaryDirectory() as directory: + current = Path(directory) / "current.json" + current.write_text(json.dumps(self.manifest()), encoding="utf-8") + + with self.assertRaisesRegex(ValueError, "stable version"): + self.generate(directory, "stable", "2.6.4", input_manifest=current) + with self.assertRaisesRegex(ValueError, "snapshot release"): + self.generate(directory, "snapshot", "2.6.5", "16.1", current) + + def test_allows_new_stable_to_supersede_snapshot_base(self): + with tempfile.TemporaryDirectory() as directory: + current = Path(directory) / "current.json" + current.write_text(json.dumps(self.manifest()), encoding="utf-8") + + generated = self.generate(directory, "stable", "2.6.6", input_manifest=current) + + self.assertEqual(generated["stable"]["version"], "2.6.6") + self.assertEqual(generated["snapshot"]["baseVersion"], "2.6.5") + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/validate-macos-appcast.py b/scripts/validate-macos-appcast.py new file mode 100755 index 0000000..137c373 --- /dev/null +++ b/scripts/validate-macos-appcast.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 + +import argparse +import xml.etree.ElementTree as ET +from dataclasses import dataclass +from pathlib import Path +from typing import Optional + + +SPARKLE_NAMESPACE = "http://www.andymatuschak.org/xml-namespaces/sparkle" +SUPPORTED_CHANNELS = {None, "snapshot"} + + +@dataclass(frozen=True) +class AppcastItem: + version: str + short_version: str + channel: Optional[str] + archive_url: str + signature: str + + +def parse_version(value: str) -> tuple[int, int, int]: + parts = value.split(".") + if not 1 <= len(parts) <= 3 or any(not part.isdigit() for part in parts): + raise ValueError(f"invalid build version: {value}") + numbers = tuple(map(int, parts)) + return numbers + (0,) * (3 - len(numbers)) + + +def parse_appcast(path: Path, repository: str) -> list[AppcastItem]: + try: + root = ET.parse(path) + except (ET.ParseError, OSError) as error: + raise ValueError(f"invalid appcast XML: {error}") from error + + download_prefix = f"https://github.com/{repository}/releases/download/" + items = [] + channel_counts: dict[Optional[str], int] = {} + versions = set() + + for element in root.findall("./channel/item"): + version = element.findtext(f"{{{SPARKLE_NAMESPACE}}}version") or "" + short_version = element.findtext(f"{{{SPARKLE_NAMESPACE}}}shortVersionString") or "" + channel = element.findtext(f"{{{SPARKLE_NAMESPACE}}}channel") + enclosure = element.find("enclosure") + if not version or not short_version or enclosure is None: + raise ValueError("appcast item is missing required version or enclosure metadata") + parse_version(version) + if channel not in SUPPORTED_CHANNELS: + raise ValueError(f"unsupported Sparkle channel: {channel}") + archive_url = enclosure.get("url", "") + signature = enclosure.get(f"{{{SPARKLE_NAMESPACE}}}edSignature", "") + length = enclosure.get("length", "") + if not archive_url.startswith(download_prefix): + raise ValueError(f"unexpected update archive URL: {archive_url}") + if not signature: + raise ValueError(f"update archive {archive_url} has no EdDSA signature") + if not length.isdigit() or int(length) <= 0: + raise ValueError(f"update archive {archive_url} has an invalid length") + if version in versions: + raise ValueError(f"duplicate build version in appcast: {version}") + + versions.add(version) + channel_counts[channel] = channel_counts.get(channel, 0) + 1 + items.append(AppcastItem(version, short_version, channel, archive_url, signature)) + + if not items: + raise ValueError("appcast contains no update items") + for channel, count in channel_counts.items(): + if count > 1: + name = channel or "stable" + raise ValueError(f"appcast contains more than one {name} update") + return items + + +def command_status(args: argparse.Namespace) -> None: + candidate = parse_version(args.build_version) + published = max( + parse_version(item.version) + for item in parse_appcast(args.appcast, args.repository) + ) + if candidate < published: + print("older") + elif candidate == published: + print("same") + else: + print("newer") + + +def command_validate(args: argparse.Namespace) -> None: + expected_channel = None if args.channel == "stable" else args.channel + matches = [ + item + for item in parse_appcast(args.appcast, args.repository) + if parse_version(item.version) == parse_version(args.build_version) + ] + if len(matches) != 1: + raise ValueError(f"expected one appcast item for build {args.build_version}") + + item = matches[0] + if item.version != args.build_version: + raise ValueError(f"appcast uses a non-canonical build version: {item.version}") + if item.short_version != args.short_version: + raise ValueError(f"unexpected short version: {item.short_version}") + if item.channel != expected_channel: + raise ValueError(f"unexpected Sparkle channel: {item.channel}") + if item.archive_url != args.archive_url: + raise ValueError(f"unexpected update archive URL: {item.archive_url}") + + +def parser() -> argparse.ArgumentParser: + root = argparse.ArgumentParser(description="Validate the GP Connect Sparkle appcast") + subcommands = root.add_subparsers(dest="command", required=True) + + status = subcommands.add_parser("status", help="compare a candidate build with the published feed") + status.add_argument("appcast", type=Path) + status.add_argument("--repository", required=True) + status.add_argument("--build-version", required=True) + status.set_defaults(handler=command_status) + + validate = subcommands.add_parser("validate", help="validate a specific update item and the complete feed") + validate.add_argument("appcast", type=Path) + validate.add_argument("--repository", required=True) + validate.add_argument("--build-version", required=True) + validate.add_argument("--short-version", required=True) + validate.add_argument("--channel", choices=("stable", "snapshot"), required=True) + validate.add_argument("--archive-url", required=True) + validate.set_defaults(handler=command_validate) + return root + + +def main() -> None: + args = parser().parse_args() + try: + args.handler(args) + except ValueError as error: + raise SystemExit(str(error)) from error + + +if __name__ == "__main__": + main() diff --git a/scripts/validate-macos-release.py b/scripts/validate-macos-release.py new file mode 100644 index 0000000..9776f9a --- /dev/null +++ b/scripts/validate-macos-release.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Validate the identity and checksums of a completed public macOS build.""" + +import argparse +import hashlib +import json +from pathlib import Path +import re + + +def validate(directory, release_tag, workflow_commit): + metadata = json.loads((directory / "release-build.json").read_text()) + for name in ("gp_ref", "gpgui_ref", "workflow_sha"): + if not re.fullmatch(r"[0-9a-f]{40}", metadata[name]): + raise ValueError(f"Invalid {name}") + if metadata["workflow_sha"] != workflow_commit: + raise ValueError("Artifact identity does not match its public workflow run") + if metadata["release_tag"] != release_tag: + raise ValueError("Artifact release tag does not match the requested release") + if not re.fullmatch(r"[0-9]+\.[0-9]+", metadata["build_version"]): + raise ValueError("Invalid build version") + if not re.fullmatch(r"[0-9]+\.[0-9]+\.macos", metadata["request_id"]): + raise ValueError("Invalid build request ID") + + version = metadata["artifact_version"] + if metadata["channel"] == "stable": + if not re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", release_tag): + raise ValueError("Invalid stable tag") + if version != release_tag[1:]: + raise ValueError("Artifact version does not match the stable tag") + elif metadata["channel"] == "snapshot": + if release_tag != "snapshot" or not re.fullmatch( + r"[0-9]+\.[0-9]+\.[0-9]+-snapshot\." + re.escape(metadata["build_version"]), version + ): + raise ValueError("Invalid snapshot artifact version") + else: + raise ValueError("Invalid channel") + + expected = {f"GPConnect_{version}_arm64.{extension}" for extension in ("dmg", "zip")} + expected |= {name + ".sha256" for name in expected} + if set(metadata["assets"]) != expected: + raise ValueError("Build must contain exactly the DMG, ZIP, and their checksums") + if {file.name for file in directory.iterdir()} != expected | {"release-build.json"}: + raise ValueError("Unexpected or missing artifact files") + for name, digest in metadata["assets"].items(): + file = directory / name + if file.is_symlink() or hashlib.sha256(file.read_bytes()).hexdigest() != digest: + raise ValueError(f"Artifact checksum mismatch: {name}") + for name in expected: + if name.endswith(".sha256"): + fields = (directory / name).read_text().split() + if fields != [metadata["assets"][name[:-7]], name[:-7]]: + raise ValueError(f"Invalid checksum file: {name}") + return metadata + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("directory", type=Path) + parser.add_argument("--release-tag", required=True) + parser.add_argument("--workflow-commit", required=True) + args = parser.parse_args() + try: + result = validate(args.directory, args.release_tag, args.workflow_commit) + except (ValueError, KeyError, OSError) as error: + parser.exit(1, f"{error}\n") + print(json.dumps(result)) diff --git a/scripts/validate-update-manifest.py b/scripts/validate-update-manifest.py new file mode 100644 index 0000000..d47ec63 --- /dev/null +++ b/scripts/validate-update-manifest.py @@ -0,0 +1,219 @@ +#!/usr/bin/env python3 + +from __future__ import annotations + +import argparse +import datetime +import json +import re +import unicodedata +from pathlib import Path +from urllib.parse import urlparse + +ALLOWED_PLATFORMS = {"linux", "freebsd", "openbsd"} +SEMVER = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$") +BUILD = re.compile(r"^(0|[1-9][0-9]*)(?:\.(0|[1-9][0-9]*)){0,2}$") +EXPECTED_TOP_LEVEL_KEYS = {"schemaVersion", "stable", "snapshot"} +EXPECTED_STABLE_KEYS = {"version", "releasedAt", "releaseUrl", "platforms", "highlights"} +EXPECTED_SNAPSHOT_KEYS = {"baseVersion", "build", "releasedAt", "releaseUrl", "platforms", "highlights"} +MAX_U64 = 2**64 - 1 + + +def version_tuple(version: str) -> tuple[int, int, int]: + match = SEMVER.fullmatch(version) + if not match: + raise ValueError("version must be stable semantic versioning") + parts = tuple(int(part) for part in match.groups()) + if any(part > MAX_U64 for part in parts): + raise ValueError("version component exceeds the supported integer range") + return parts + + +def build_tuple(build: str) -> tuple[int, int, int]: + if not BUILD.fullmatch(build): + raise ValueError("snapshot build must contain one to three numeric components") + parts = [int(part) for part in build.split(".")] + if any(part > MAX_U64 for part in parts): + raise ValueError("snapshot build component exceeds the supported integer range") + parts.extend([0] * (3 - len(parts))) + return parts[0], parts[1], parts[2] + + +def validate_release_fields(release: dict) -> None: + try: + datetime.date.fromisoformat(release["releasedAt"]) + except (TypeError, ValueError) as error: + raise ValueError("releasedAt must be an ISO calendar date") from error + + parsed_url = urlparse(release["releaseUrl"]) + if parsed_url.scheme != "https" or parsed_url.hostname != "github.com": + raise ValueError("releaseUrl must use GitHub HTTPS") + + platforms = release["platforms"] + if not isinstance(platforms, list) or not platforms: + raise ValueError("platforms must be a non-empty list") + if any(not isinstance(platform, str) or platform not in ALLOWED_PLATFORMS for platform in platforms): + raise ValueError("platforms contains an unsupported target") + if len(set(platforms)) != len(platforms): + raise ValueError("platforms contains duplicates") + + highlights = release["highlights"] + if not isinstance(highlights, list) or not 1 <= len(highlights) <= 6: + raise ValueError("highlights must contain between one and six entries") + for highlight in highlights: + if not isinstance(highlight, str) or not highlight.strip(): + raise ValueError("highlights must be non-empty strings") + if len(highlight) > 160: + raise ValueError("highlight exceeds 160 characters") + if any(unicodedata.category(character) == "Cc" for character in highlight): + raise ValueError("highlight contains control characters") + + +def validate_stable(release: object, repository: str) -> dict | None: + if release is None: + return None + if not isinstance(release, dict) or set(release) != EXPECTED_STABLE_KEYS: + raise ValueError("stable release has unexpected fields") + version = release["version"] + if not isinstance(version, str): + raise ValueError("stable version must be a string") + version_tuple(version) + expected_url = f"https://github.com/{repository}/releases/tag/v{version}" + if release["releaseUrl"] != expected_url: + raise ValueError(f"stable releaseUrl must be {expected_url}") + validate_release_fields(release) + return release + + +def validate_snapshot(release: object, repository: str) -> dict | None: + if release is None: + return None + if not isinstance(release, dict) or set(release) != EXPECTED_SNAPSHOT_KEYS: + raise ValueError("snapshot release has unexpected fields") + base_version = release["baseVersion"] + if not isinstance(base_version, str): + raise ValueError("snapshot baseVersion must be a string") + version_tuple(base_version) + build = release["build"] + if not isinstance(build, str): + raise ValueError("snapshot build must be a string") + build_tuple(build) + expected_url = f"https://github.com/{repository}/releases/tag/snapshot" + if release["releaseUrl"] != expected_url: + raise ValueError(f"snapshot releaseUrl must be {expected_url}") + validate_release_fields(release) + return release + + +def validate_manifest(data: object, repository: str) -> dict: + if not isinstance(data, dict) or set(data) != EXPECTED_TOP_LEVEL_KEYS: + raise ValueError("manifest has unexpected top-level fields") + if type(data["schemaVersion"]) is not int or data["schemaVersion"] != 1: + raise ValueError("unsupported schemaVersion") + stable = validate_stable(data["stable"], repository) + snapshot = validate_snapshot(data["snapshot"], repository) + if stable is None and snapshot is None: + raise ValueError("manifest must contain a stable or snapshot release") + return data + + +def load_highlights(path: Path) -> list[str]: + highlights = [line.strip() for line in path.read_text(encoding="utf-8").splitlines() if line.strip()] + if not highlights: + raise ValueError("at least one release highlight is required") + return highlights + + +def generate(args: argparse.Namespace) -> None: + if args.input is None: + data = {"schemaVersion": 1, "stable": None, "snapshot": None} + else: + data = validate_manifest(json.loads(args.input.read_text(encoding="utf-8")), args.repository) + + common = { + "releasedAt": args.released_at, + "platforms": args.platform if args.platform is not None else ["linux", "freebsd", "openbsd"], + "highlights": load_highlights(args.highlights_file), + } + if args.channel == "stable": + if args.build is not None: + raise ValueError("stable releases must not specify a snapshot build") + current = data["stable"] + if current is not None and version_tuple(args.version) < version_tuple(current["version"]): + raise ValueError(f"stable version {args.version} is older than {current['version']}") + data["stable"] = { + "version": args.version, + "releaseUrl": f"https://github.com/{args.repository}/releases/tag/v{args.version}", + **common, + } + else: + if args.build is None: + raise ValueError("snapshot releases require a build") + candidate = (version_tuple(args.version), build_tuple(args.build)) + current = data["snapshot"] + if current is not None: + published = (version_tuple(current["baseVersion"]), build_tuple(current["build"])) + if candidate < published: + raise ValueError("snapshot release is older than the published snapshot") + data["snapshot"] = { + "baseVersion": args.version, + "build": args.build, + "releaseUrl": f"https://github.com/{args.repository}/releases/tag/snapshot", + **common, + } + + validate_manifest(data, args.repository) + args.output.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8") + + +def validate(args: argparse.Namespace) -> None: + data = validate_manifest(json.loads(args.manifest.read_text(encoding="utf-8")), args.repository) + if args.channel is None: + return + release = data[args.channel] + if release is None: + raise ValueError(f"manifest does not contain a {args.channel} release") + version_key = "version" if args.channel == "stable" else "baseVersion" + if args.version is not None and release[version_key] != args.version: + raise ValueError(f"{args.channel} version {release[version_key]} does not match {args.version}") + if args.build is not None: + if args.channel != "snapshot": + raise ValueError("only snapshot releases have a build") + if release["build"] != args.build: + raise ValueError(f"snapshot build {release['build']} does not match {args.build}") + + +def main() -> None: + parser = argparse.ArgumentParser(description="Generate and validate the GP Connect update catalog") + subcommands = parser.add_subparsers(required=True) + + generate_parser = subcommands.add_parser("generate") + generate_parser.add_argument("--repository", required=True) + generate_parser.add_argument("--channel", choices=("stable", "snapshot"), required=True) + generate_parser.add_argument("--version", required=True) + generate_parser.add_argument("--platform", action="append", choices=sorted(ALLOWED_PLATFORMS), + help="Published Unix target; repeat for multiple targets (default: all)") + generate_parser.add_argument("--build") + generate_parser.add_argument("--released-at", required=True) + generate_parser.add_argument("--highlights-file", required=True, type=Path) + generate_parser.add_argument("--input", type=Path) + generate_parser.add_argument("--output", required=True, type=Path) + generate_parser.set_defaults(handler=generate) + + validate_parser = subcommands.add_parser("validate") + validate_parser.add_argument("manifest", type=Path) + validate_parser.add_argument("--repository", required=True) + validate_parser.add_argument("--channel", choices=("stable", "snapshot")) + validate_parser.add_argument("--version") + validate_parser.add_argument("--build") + validate_parser.set_defaults(handler=validate) + + args = parser.parse_args() + try: + args.handler(args) + except (OSError, ValueError, json.JSONDecodeError) as error: + parser.error(str(error)) + + +if __name__ == "__main__": + main()