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
20 changes: 11 additions & 9 deletions scripts/seed-repo-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ set -euo pipefail
# STANDARDS_REPO repo holding the canonical standards/ (default petry-projects/.github).
# STANDARDS_DIR optional local checkout of STANDARDS_REPO; when set, files are
# read from disk instead of the GitHub API (regeneration seam).
# DEPENDABOT_STACK standards/dependabot/<stack> to ship as .github/dependabot.yml
# (default node).
# DEPENDABOT_STACK standards/dependabot/<stack>.yml to ship as .github/dependabot.yml
# (default frontend; one of: frontend, backend-go, backend-python,
# backend-rust, fullstack, infra-terraform).
# GH_TOKEN PAT with repo scope (create a branch + PR on TEMPLATE_REPO).

DRY_RUN="${DRY_RUN:-false}"
TEMPLATE_REPO="${TEMPLATE_REPO:-petry-projects/repo-template}"
STANDARDS_REPO="${STANDARDS_REPO:-petry-projects/.github}"
DEPENDABOT_STACK="${DEPENDABOT_STACK:-node}"
DEPENDABOT_STACK="${DEPENDABOT_STACK:-frontend}"

_is_dry() { [ "$DRY_RUN" = "true" ]; }

Expand Down Expand Up @@ -249,10 +250,11 @@ after you create your repo from the template:

## 1. Pick your Dependabot stack

`.github/dependabot.yml` ships with a default stack. Replace it with the stack
that matches your project (the canonical stacks live in
`petry-projects/.github` under `standards/dependabot/<stack>/`). For example, a
Node project uses the `node` stack; a Go project uses the `go` stack.
`.github/dependabot.yml` ships with the `frontend` (npm) stack by default.
Replace it with the stack that matches your project — the canonical stacks live
in `petry-projects/.github` under `standards/dependabot/<stack>.yml`. Available
stacks: `frontend` (npm), `backend-go`, `backend-python`, `backend-rust`,
`fullstack` (npm + Go + Terraform), and `infra-terraform`.

## 2. Customize `ci.yml` for your stack

Expand Down Expand Up @@ -311,9 +313,9 @@ _emit_baseline() {
_gen_baseline "$path"
else
local content
content="$(_fetch_standard "standards/dependabot/${DEPENDABOT_STACK}/dependabot.yml")" || true
content="$(_fetch_standard "standards/dependabot/${DEPENDABOT_STACK}.yml")" || true
if [ -z "$content" ]; then
echo "::error::could not fetch standards/dependabot/${DEPENDABOT_STACK}/dependabot.yml" >&2
echo "::error::could not fetch standards/dependabot/${DEPENDABOT_STACK}.yml from ${STANDARDS_REPO}" >&2
return 1
fi
printf '%s\n' "$content"
Expand Down
88 changes: 81 additions & 7 deletions tests/test_seed_repo_template.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ setup() {
export PATH="$STUB_BIN:$PATH"
CALLS="$STUB_BIN/calls.log"; export CALLS
STANDARDS_DIR="$(mktemp -d)" || { echo "Failed to create STANDARDS_DIR" >&2; exit 1; }
mkdir -p "$STANDARDS_DIR/standards/workflows" "$STANDARDS_DIR/standards/dependabot/node"
mkdir -p "$STANDARDS_DIR/standards/workflows" "$STANDARDS_DIR/standards/dependabot"
export STANDARDS_DIR
}
teardown() {
Expand Down Expand Up @@ -57,6 +57,15 @@ _fixture_workflow() { # <name> <heredoc-content-on-stdin>
cat > "$STANDARDS_DIR/standards/workflows/$1"
}

# gh stub that returns NOTHING for every call. Used by the fail-loud tests so a
# fixture absent from STANDARDS_DIR cannot fall through to a real network fetch
# (which would succeed for files that exist in the live standards repo and make
# the test pass/fail on network state instead of the seeder's behavior).
_stub_gh_empty() {
printf '#!/usr/bin/env bash\nexit 0\n' > "$STUB_BIN/gh"
chmod +x "$STUB_BIN/gh"
}

# ── manifest: workflow set (AC #1, #5) ────────────────────────────────────────
@test "list-workflows: ships exactly the 10 named stubs" {
run bash "$SEED" --list-workflows
Expand Down Expand Up @@ -144,6 +153,45 @@ _fixture_workflow() { # <name> <heredoc-content-on-stdin>
[[ "$output" != *"./.github/workflows/dependency-audit-reusable.yml"* ]]
}

@test "emit-workflow: sonarcloud inline stub ships byte-identically (no repin)" {
# sonarcloud is kind=inline: it carries no reusable ref and must pass through
# verbatim, exactly like ci.yml. Covers the second inline workflow (#966 follow-up).
_fixture_workflow sonarcloud.yml <<'EOF'
name: SonarCloud Analysis
on: [push]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
EOF
run bash "$SEED" --emit-workflow sonarcloud.yml
[ "$status" -eq 0 ]
[ "$output" = "$(cat "$STANDARDS_DIR/standards/workflows/sonarcloud.yml")" ]
}

@test "emit-workflow: FAILS LOUD when an inline workflow's standard file is absent" {
# Regression guard for the original #966 defect: ci/sonarcloud are declared
# kind=inline but were MISSING from standards/workflows/. The emit path must
# exit non-zero with a clear error, never silently ship an empty workflow.
# No fixture for sonarcloud.yml is written → the fetch must fail.
_stub_gh_empty
run bash "$SEED" --emit-workflow sonarcloud.yml
[ "$status" -ne 0 ]
[[ "$output" == *"could not fetch standards/workflows/sonarcloud.yml"* ]]
}

@test "emit-workflow: every inline manifest stub fails loud when its standard is absent" {
# Guards the whole inline set, so a future manifest addition can't silently
# 404 at seed time the way ci.yml did.
_stub_gh_empty
for name in "${INLINE_STUBS[@]}"; do
run bash "$SEED" --emit-workflow "${name}.yml"
[ "$status" -ne 0 ] || { echo "$name did not fail loud on a missing standard" >&2; false; }
[[ "$output" == *"could not fetch standards/workflows/${name}.yml"* ]] \
|| { echo "$name missing the fail-loud error message" >&2; false; }
done
}

# ── baseline content (AC #3, #4) ──────────────────────────────────────────────
@test "baseline: CODEOWNERS is the org default rule" {
run bash "$SEED" --emit-baseline .github/CODEOWNERS
Expand Down Expand Up @@ -192,12 +240,38 @@ _fixture_workflow() { # <name> <heredoc-content-on-stdin>
# ── dependabot baseline is sourced from the chosen standards/ stack template ───
@test "baseline: dependabot.yml comes from the chosen standards/dependabot stack" {
printf 'version: 2\nupdates:\n - package-ecosystem: npm\n' \
> "$STANDARDS_DIR/standards/dependabot/node/dependabot.yml"
run env DEPENDABOT_STACK=node bash "$SEED" --emit-baseline .github/dependabot.yml
> "$STANDARDS_DIR/standards/dependabot/frontend.yml"
run env DEPENDABOT_STACK=frontend bash "$SEED" --emit-baseline .github/dependabot.yml
[ "$status" -eq 0 ]
[[ "$output" == *"package-ecosystem: npm"* ]]
}

@test "baseline: dependabot.yml stack is a flat standards/dependabot/<stack>.yml file (not a subdir)" {
# Regression guard (#966 follow-up): the canonical layout is flat files
# (standards/dependabot/frontend.yml), NOT standards/dependabot/<stack>/dependabot.yml.
printf 'version: 2\nupdates:\n - package-ecosystem: gomod\n' \
> "$STANDARDS_DIR/standards/dependabot/backend-go.yml"
run env DEPENDABOT_STACK=backend-go bash "$SEED" --emit-baseline .github/dependabot.yml
[ "$status" -eq 0 ]
[[ "$output" == *"package-ecosystem: gomod"* ]]
}

@test "baseline: default DEPENDABOT_STACK is a real flat stack file (frontend)" {
printf 'version: 2\nupdates:\n - package-ecosystem: npm\n' \
> "$STANDARDS_DIR/standards/dependabot/frontend.yml"
run bash "$SEED" --emit-baseline .github/dependabot.yml
[ "$status" -eq 0 ]
[[ "$output" == *"package-ecosystem: npm"* ]]
}

@test "baseline: FAILS LOUD when the chosen dependabot stack file is absent" {
# No fixture written → the fetch must fail non-zero, never ship an empty file.
_stub_gh_empty
run env DEPENDABOT_STACK=frontend bash "$SEED" --emit-baseline .github/dependabot.yml
[ "$status" -ne 0 ]
[[ "$output" == *"could not fetch standards/dependabot/frontend.yml"* ]]
}

# ── seeding orchestration: DRY_RUN ────────────────────────────────────────────
@test "DRY_RUN: exits 0, names the target repo, makes no write API calls" {
_stub_gh
Expand All @@ -220,8 +294,8 @@ _fixture_workflow() { # <name> <heredoc-content-on-stdin>
| _fixture_workflow "$n.yml"
fi
done
printf 'version: 2\n' > "$STANDARDS_DIR/standards/dependabot/node/dependabot.yml"
run env DEPENDABOT_STACK=node bash "$SEED" --repo petry-projects/repo-template
printf 'version: 2\n' > "$STANDARDS_DIR/standards/dependabot/frontend.yml"
run env DEPENDABOT_STACK=frontend bash "$SEED" --repo petry-projects/repo-template
[ "$status" -eq 0 ]
[ -f "$CALLS" ]
grep -q "contents/.github/workflows/auto-rebase.yml" "$CALLS"
Expand All @@ -248,8 +322,8 @@ EOF
| _fixture_workflow "$n.yml"
fi
done
printf 'version: 2\n' > "$STANDARDS_DIR/standards/dependabot/node/dependabot.yml"
run env DEPENDABOT_STACK=node bash "$SEED" --repo petry-projects/repo-template
printf 'version: 2\n' > "$STANDARDS_DIR/standards/dependabot/frontend.yml"
run env DEPENDABOT_STACK=frontend bash "$SEED" --repo petry-projects/repo-template
[ "$status" -eq 0 ]
[[ "$output" == *"PR #42 already open"* ]]
[ ! -f "$CALLS" ] || ! grep -q "pr create" "$CALLS"
Expand Down
Loading