Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/standards-deploy-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#
# Triggered on any PR that touches:
# - scripts/deploy-standard-workflows.sh
# - scripts/standards-deploy-driver.sh
# - scripts/lib/standards-deploy.sh
# - test/scripts/deploy-standard-workflows/**
# - test/scripts/standards-deploy-driver/**
# - test/scripts/lib/standards-deploy.bats
#
# Gates (all must pass before merge):
Expand All @@ -19,22 +21,28 @@ on:
pull_request:
paths:
- 'scripts/deploy-standard-workflows.sh'
- 'scripts/standards-deploy-driver.sh'
- 'scripts/lib/standards-deploy.sh'
- 'scripts/lib/ring-pins.sh'
- 'test/scripts/deploy-standard-workflows/**'
- 'test/scripts/standards-deploy-driver/**'
- 'test/scripts/lib/standards-deploy.bats'
- 'test/scripts/lib/ring-pins.bats'
- '.github/workflows/standards-deploy.yml'
- '.github/workflows/standards-deploy-tests.yml'
push:
branches:
- main
paths:
- 'scripts/deploy-standard-workflows.sh'
- 'scripts/standards-deploy-driver.sh'
- 'scripts/lib/standards-deploy.sh'
- 'scripts/lib/ring-pins.sh'
- 'test/scripts/deploy-standard-workflows/**'
- 'test/scripts/standards-deploy-driver/**'
- 'test/scripts/lib/standards-deploy.bats'
- 'test/scripts/lib/ring-pins.bats'
- '.github/workflows/standards-deploy.yml'
- '.github/workflows/standards-deploy-tests.yml'

permissions:
Expand Down Expand Up @@ -66,6 +74,7 @@ jobs:
set -euo pipefail
shellcheck --severity=warning -x \
scripts/deploy-standard-workflows.sh \
scripts/standards-deploy-driver.sh \
scripts/lib/standards-deploy.sh \
scripts/lib/ring-pins.sh

Expand All @@ -74,4 +83,5 @@ jobs:
bats --print-output-on-failure \
test/scripts/lib/standards-deploy.bats \
test/scripts/lib/ring-pins.bats \
test/scripts/deploy-standard-workflows/
test/scripts/deploy-standard-workflows/ \
test/scripts/standards-deploy-driver/
59 changes: 59 additions & 0 deletions .github/workflows/standards-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Scheduled fleet sweep that brings every org repo up to the standard workflow
# stubs — the automation the compliance audit was missing (petry-projects/.github#851).
#
# The audit only *files* remediation issues; it never deploys. This workflow runs
# scripts/deploy-standard-workflows.sh across the fleet on the org cadence (and on
# demand), opening `standards-sync` PRs wherever a repo drifts — a missing required
# stub, or a stub pinned off its agent's current channel. Compliant repos open
# nothing (the deploy script is idempotent), and SKIP_REPOS / SKIP_OVERRIDES are
# respected by the script. This is a driver, not a reimplementation: all deploy
# logic lives in the script and its libs.
#
# Pairs with #850-B (auto-merge) so the sweep -> PR -> merge loop closes with no
# manual intervention.
name: Standards Deploy
# readme-report: Weekly fleet sweep that opens standards-sync PRs for repos drifted off the org-standard workflow stubs.

on:
schedule:
- cron: '0 8 * * 1' # Every Monday at 08:00 UTC — offset from the Fri 12:00 UTC compliance audit
workflow_dispatch:
inputs:
target_repo:
description: 'Limit the sweep to a single repo (name). Leave blank to sweep the whole fleet.'
required: false
type: string
target_workflow:
description: 'Limit to a single workflow stub (e.g. dev-lead.yml). Leave blank for all deployable.'
required: false
type: string
dry_run:
description: 'Dry run — plan drift only, open no PRs'
required: false
default: false
type: boolean
Comment thread
don-petry marked this conversation as resolved.
Resolved

permissions: {}

concurrency:
group: standards-deploy
cancel-in-progress: false # Let an in-flight sweep finish to avoid partial PR state

jobs:
deploy:
name: Sweep fleet and open standards-sync PRs
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read # only to check out this repo; all cross-repo writes use the PAT below
env:
GH_TOKEN: ${{ secrets.ORG_SCORECARD_TOKEN }}
TARGET_REPO: ${{ inputs.target_repo }}
TARGET_WORKFLOW: ${{ inputs.target_workflow }}
DRY_RUN: ${{ inputs.dry_run == true && 'true' || 'false' }}
steps:
- name: Checkout .github repo
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

- name: Run standards-deploy fleet sweep
run: bash scripts/standards-deploy-driver.sh
45 changes: 45 additions & 0 deletions scripts/standards-deploy-driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
# standards-deploy-driver.sh — scheduled fleet-sweep driver for the org-standard
# workflow deployer (petry-projects/.github#851, Epic #850).
#
# The compliance audit only *files* remediation issues; bringing the fleet to
# standard still required a human running deploy-standard-workflows.sh by hand.
# This driver closes that gap: the standards-deploy.yml workflow runs it on the
# org cadence (and on demand), so drifted repos get standards-sync PRs opened
# automatically.
#
# It is deliberately THIN — it adds NO deploy logic. All drift detection,
# SKIP_REPOS / SKIP_OVERRIDES handling, ring/channel pinning, idempotency, and
# PR creation live in deploy-standard-workflows.sh and its libs. This driver only
# maps workflow_dispatch inputs (environment variables) onto that script's flags
# and runs it, so the dispatch surface stays unit-testable.
#
# Inputs (environment variables, all optional):
# TARGET_REPO Limit the sweep to a single repo (--repo). Blank = whole fleet.
# TARGET_WORKFLOW Limit to a single workflow (--workflow). Blank = all deployable.
# DRY_RUN "true" -> pass --dry-run (plan only, open no PRs).
#
# Overridable for tests:
# DEPLOY_SCRIPT Path to the deploy script (default: sibling in this dir).
#
# Requires GH_TOKEN with repo scope for cross-repo branch + PR creation.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_SCRIPT="${DEPLOY_SCRIPT:-$SCRIPT_DIR/deploy-standard-workflows.sh}"

TARGET_REPO="${TARGET_REPO:-}"
TARGET_WORKFLOW="${TARGET_WORKFLOW:-}"
DRY_RUN="${DRY_RUN:-false}"

# Build the flag list from the inputs. An empty list means "whole fleet, all
# deployable workflows" — the scheduled default.
declare -a args=()
[[ -n "$TARGET_REPO" ]] && args+=(--repo "$TARGET_REPO")
[[ -n "$TARGET_WORKFLOW" ]] && args+=(--workflow "$TARGET_WORKFLOW")
[[ "$DRY_RUN" == "true" ]] && args+=(--dry-run)

echo "[driver] standards-deploy sweep — repo=${TARGET_REPO:-<all>} workflow=${TARGET_WORKFLOW:-<all>} dry_run=${DRY_RUN}"

exec bash "$DEPLOY_SCRIPT" "${args[@]}"
135 changes: 135 additions & 0 deletions test/scripts/standards-deploy-driver/driver.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env bats
# Tests for scripts/standards-deploy-driver.sh — the scheduled fleet-sweep driver
# for the org-standard workflow deployer (petry-projects/.github#851).
#
# The driver is intentionally thin: it maps workflow_dispatch inputs (env vars)
# into flags for scripts/deploy-standard-workflows.sh and runs it. Two layers are
# exercised here:
# 1. argv mapping — a fake deploy script records the flags it is handed, so the
# env→flag translation (repo / workflow / dry-run / default fleet) is pinned.
# 2. drift-detection + no-op paths — the driver runs the REAL deploy script
# against a fake `gh`, proving a drifted repo plans a PR and a compliant repo
# opens nothing (idempotent), the same guarantees the schedule relies on.

setup() {
TT_TMP="$(mktemp -d)"
REPO_ROOT="$(cd -- "${BATS_TEST_DIRNAME}/../../.." && pwd)"
DRIVER="${REPO_ROOT}/scripts/standards-deploy-driver.sh"
ARGV_LOG="${TT_TMP}/argv.log"; export ARGV_LOG
GH_CALLS="${TT_TMP}/gh-calls.log"; export GH_CALLS
: > "$GH_CALLS"
}

teardown() { rm -rf "$TT_TMP"; }

# A fake deploy script that records exactly the argv the driver hands it. Wired in
# via the DEPLOY_SCRIPT override so the argv-mapping tests never touch the network.
install_fake_deploy() {
local fake="${TT_TMP}/fake-deploy.sh"
cat > "$fake" <<'FAKE'
#!/usr/bin/env bash
{ for a in "$@"; do printf '%s\n' "$a"; done; } > "$ARGV_LOG"
FAKE
chmod +x "$fake"
DEPLOY_SCRIPT="$fake"; export DEPLOY_SCRIPT
}

# Fake `gh` mirroring the deploy script's own tests: contents API 404s when
# GH_CONTENT_B64 is unset (stub missing → drift) and returns it as the file body
# when set (stub present → compliant). `gh repo list` echoes $GH_REPO_LIST.
install_gh_stub() {
local bin="${TT_TMP}/bin"
mkdir -p "$bin"
cat > "$bin/gh" <<'STUB'
#!/usr/bin/env bash
{ printf 'gh'; for a in "$@"; do printf ' %s' "$a"; done; printf '\n'; } >> "$GH_CALLS"
if [ "${1:-}" = "repo" ] && [ "${2:-}" = "list" ]; then
printf '%s\n' ${GH_REPO_LIST:-}
exit 0
fi
if [ "${1:-}" = "api" ]; then
case "$2" in
*contents*)
if [ -n "${GH_CONTENT_B64:-}" ]; then
printf '{"sha":"abc123","content":"%s"}' "$GH_CONTENT_B64"
exit 0
fi
exit 1 ;; # simulate 404 — file absent
esac
fi
exit 0
STUB
chmod +x "$bin/gh"
PATH="${bin}:${PATH}"; export PATH
}

# ---------------------------------------------------------------------------
# argv mapping
# ---------------------------------------------------------------------------
@test "maps target_repo, target_workflow, and dry_run to script flags" {
install_fake_deploy
run env TARGET_REPO=markets TARGET_WORKFLOW=dev-lead.yml DRY_RUN=true \
bash "$DRIVER"
[ "$status" -eq 0 ]
# Flags recorded in order, one per line.
run cat "$ARGV_LOG"
[ "${lines[0]}" = "--repo" ]
[ "${lines[1]}" = "markets" ]
[ "${lines[2]}" = "--workflow" ]
[ "${lines[3]}" = "dev-lead.yml" ]
[ "${lines[4]}" = "--dry-run" ]
}

@test "no inputs sweeps the whole fleet (no --repo / --workflow flags)" {
install_fake_deploy
run bash "$DRIVER"
[ "$status" -eq 0 ]
run cat "$ARGV_LOG"
# Empty argv => whole-fleet, all-workflows sweep.
[ ! -s "$ARGV_LOG" ]
}

@test "dry_run defaults off — no --dry-run flag when unset" {
install_fake_deploy
run env TARGET_REPO=markets bash "$DRIVER"
[ "$status" -eq 0 ]
! grep -qx -- '--dry-run' "$ARGV_LOG"
}

# ---------------------------------------------------------------------------
# drift-detection + no-op paths (driver over the REAL deploy script)
# ---------------------------------------------------------------------------
@test "drift path: a missing stub is planned as a PR" {
install_gh_stub
run env GH_TOKEN=x TARGET_REPO=markets TARGET_WORKFLOW=add-to-project.yml DRY_RUN=true \
bash "$DRIVER"
[ "$status" -eq 0 ]
echo "$output" | grep -qE 'Would open PR for markets .* add-to-project.yml'
# dry-run must not mutate.
! grep -q 'git/refs --method POST' "$GH_CALLS"
! grep -q 'gh pr create' "$GH_CALLS"
}

@test "no-op path: a compliant stub opens nothing" {
local template="${REPO_ROOT}/standards/workflows/add-to-project.yml"
GH_CONTENT_B64="$(base64 -w 0 "$template" 2>/dev/null || base64 -b 0 "$template")"
export GH_CONTENT_B64
install_gh_stub
run env GH_TOKEN=x TARGET_REPO=markets TARGET_WORKFLOW=add-to-project.yml DRY_RUN=true \
bash "$DRIVER"
[ "$status" -eq 0 ]
echo "$output" | grep -q 'already compliant'
# True absence check: fail if any line plans a PR. `grep -vq` was wrong — it
# succeeds whenever *any* line doesn't match, so it can't prove absence.
! echo "$output" | grep -q 'Would open PR'
}
Comment thread
don-petry marked this conversation as resolved.
Resolved

@test "fleet sweep iterates every repo from gh repo list" {
GH_REPO_LIST="markets TalkTerm"; export GH_REPO_LIST
install_gh_stub
run env GH_TOKEN=x TARGET_WORKFLOW=add-to-project.yml DRY_RUN=true bash "$DRIVER"
[ "$status" -eq 0 ]
# Both fleet repos are drifted (stub absent) => both planned, none skipped.
echo "$output" | grep -qE 'Would open PR for markets'
echo "$output" | grep -qE 'Would open PR for TalkTerm'
}
Loading