Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
82 changes: 82 additions & 0 deletions .context/refactoring-baseline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Refactoring Baseline

Issue: #812
Captured at: 2026-06-17
Baseline revision: `00f0fe92fb4da4dab1302d428d6209d08064e245`

## CI

Latest `main` CI run at capture time:

- Run: <https://github.com/keito4/config/actions/runs/27680039201>
- Title: `Merge pull request #839 from keito4/fix/workflow-template-validation-...`
- Status: `completed`
- Conclusion: `success`
- Started: `2026-06-17T09:42:10Z`
- Updated: `2026-06-17T09:43:25Z`
- Duration: 75 seconds

Recent `main` CI runs checked: 5 / 5 successful.

## Repository Size

Measured with tracked files only:

```bash
git ls-files | wc -l
git ls-files -z | xargs -0 wc -l | tail -1
```

- Tracked files: 379
- Total LOC: 69,933

## Coverage

Measured with:

```bash
npm run test:coverage
```

Jest coverage summary:

| Metric | Value |
| ---------- | ------ |
| Statements | 95.23% |
| Branches | 100% |
| Functions | 100% |
| Lines | 94.73% |

Test result:

- Test suites: 17 passed / 17 total
- Tests: 549 passed / 549 total

## Reference Inventory

Reference inventory script added in this phase:

```bash
script/audit-references.sh
script/audit-references.sh --format tsv
```

The script scans tracked files under `script/` and `templates/`, includes `test/`
and documentation sources, and classifies references into:

- `code/ci`
- `test`
- `docs`

## Phase Issues

The phase issue set exists:

- #812: Phase 0 safety net and baseline
- #813: Phase 1 dead asset cleanup
- #814: Phase 2 documentation SSoT
- #815: Phase 3 template/entity sync
- #816: Phase 4 CI/CD workflow consolidation
- #817: Phase 5 scripts/hooks/commands DRY
- #818: Phase 6 test quality recovery
- #819: Phase 7 environment configuration cleanup
164 changes: 164 additions & 0 deletions script/audit-references.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env bash
set -euo pipefail

FORMAT="markdown"

usage() {
cat <<'USAGE'
Usage: script/audit-references.sh [--format markdown|tsv]

Audits tracked files under script/ and templates/ and reports references from:
- code/ci
- test
- docs

The scanner matches both the repository-relative path and basename so it can
find references such as "script/foo.sh" and "foo.sh".
USAGE
}

while [ "$#" -gt 0 ]; do
case "$1" in
--format)
FORMAT="${2:-}"
shift 2
;;
Comment on lines +23 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Handle missing --format value before shifting arguments.

shift 2 is unconditional, so script/audit-references.sh --format exits via shell error instead of your controlled validation path. Add an explicit arity check and fail with usage/status 2.

Proposed fix
   case "$1" in
     --format)
+      if [ "$#" -lt 2 ] || [ -z "${2:-}" ]; then
+        echo "Missing value for --format (expected: markdown|tsv)" >&2
+        usage >&2
+        exit 2
+      fi
       FORMAT="${2:-}"
       shift 2
       ;;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
--format)
FORMAT="${2:-}"
shift 2
;;
--format)
if [ "$#" -lt 2 ] || [ -z "${2:-}" ]; then
echo "Missing value for --format (expected: markdown|tsv)" >&2
usage >&2
exit 2
fi
FORMAT="${2:-}"
shift 2
;;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@script/audit-references.sh` around lines 23 - 26, The --format case handler
in the script unconditionally executes shift 2 without validating that a value
was actually provided for the FORMAT flag, causing a shell error instead of
controlled failure. Add an explicit check after the FORMAT assignment to verify
that the value is not empty (i.e., that a second argument exists), and if the
check fails, print a usage message and exit with status 2 before executing the
shift statement.

--help|-h)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done

case "$FORMAT" in
markdown|tsv) ;;
*)
echo "Unsupported format: $FORMAT" >&2
exit 2
;;
esac

classify_source() {
local source="$1"

case "$source" in
test/*)
echo "test"
;;
docs/*|README.md|AGENTS.md|CLAUDE.md|*.md)
echo "docs"
;;
.github/workflows/*|.github/actions/*|.github/dependabot.*|.github/labels.yml|.github/policies/*)
echo "code/ci"
;;
*)
echo "code/ci"
;;
esac
}

tracked_files() {
git ls-files
}

target_files() {
git ls-files 'script/*' 'templates/*' | while IFS= read -r file; do
[ -f "$file" ] || continue
printf '%s\n' "$file"
Comment on lines +71 to +73

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve audit paths from the repo root

When this script is invoked from any subdirectory (for example, cd script && ../script/audit-references.sh --format tsv), git ls-files still emits repo-relative paths such as script/foo.sh, but this check treats them as relative to the current subdirectory and skips every target; the command then exits successfully with only the TSV header. Please cd to git rev-parse --show-toplevel or prefix these paths with the repo root before filtering/scanning.

Useful? React with 👍 / 👎.

done
}

emit_tsv_row() {
local category="$1"
local target="$2"
local source="$3"

printf '%s\t%s\t%s\n' "$category" "$target" "$source"
}

emit_markdown_list() {
local label="$1"
local refs="$2"

local count
count=$(printf '%s\n' "$refs" | sed '/^$/d' | wc -l | tr -d ' ')
printf -- '- %s (%s)\n' "$label" "$count"
if [ "$count" -gt 0 ]; then
printf '%s\n' "$refs" | sed '/^$/d' | sed 's/^/ - `/' | sed 's/$/`/'
fi
}

if [ "$FORMAT" = "markdown" ]; then
cat <<'HEADER'
# Reference Inventory

Targets: tracked files under `script/` and `templates/`.

HEADER
else
printf 'category\ttarget\tsource\n'
fi

ZERO_CODE_TEST=()

while IFS= read -r target; do
basename_target=$(basename "$target")
code_refs=""
test_refs=""
docs_refs=""
mapfile -t sources < <(tracked_files)

while IFS= read -r source; do
[ "$source" != "$target" ] || continue
category=$(classify_source "$source")
case "$category" in
test)
test_refs="${test_refs}${source}"$'\n'
;;
docs)
docs_refs="${docs_refs}${source}"$'\n'
;;
*)
code_refs="${code_refs}${source}"$'\n'
;;
esac

if [ "$FORMAT" = "tsv" ]; then
emit_tsv_row "$category" "$target" "$source"
fi
done < <(rg -l --fixed-strings -e "$target" -e "$basename_target" -- "${sources[@]}" 2>/dev/null || true)

if [ -z "$code_refs$test_refs" ]; then
ZERO_CODE_TEST+=("$target")
fi

if [ "$FORMAT" = "markdown" ]; then
printf "## \`%s\`\n\n" "$target"
emit_markdown_list "Code/CI references" "$code_refs"
emit_markdown_list "Test references" "$test_refs"
emit_markdown_list "Documentation references" "$docs_refs"
printf '\n'
elif [ -z "$code_refs$test_refs$docs_refs" ]; then
emit_tsv_row "none" "$target" "-"
fi
done < <(target_files)

if [ "$FORMAT" = "markdown" ]; then
cat <<'SUMMARY'
## Zero Code/Test References

These targets have no code/ci or test references. Documentation-only references
are still listed above for each target.

SUMMARY
if [ "${#ZERO_CODE_TEST[@]}" -eq 0 ]; then
printf -- '- None\n'
else
printf '%s\n' "${ZERO_CODE_TEST[@]}" | sed 's/^/- `/' | sed 's/$/`/'
fi
fi
20 changes: 20 additions & 0 deletions test/integration/audit_references.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bats

load ../test_helper/test_helper

@test "audit-references reports references as TSV with required categories" {
run "$REPO_ROOT/script/audit-references.sh" --format tsv
assert_success

printf '%s\n' "$output" | grep -Fq $'test\tscript/check-image-version.sh\ttest/integration/core-scripts.bats'
printf '%s\n' "$output" | grep -Fq $'test\ttemplates/workflows/claude-health-check.yml\ttest/template-workflows.test.js'
printf '%s\n' "$output" | grep -Fq $'docs\tscript/check-image-version.sh\tscript/README.md'
}

@test "audit-references markdown includes zero code/test summary" {
run "$REPO_ROOT/script/audit-references.sh"
assert_success

printf '%s\n' "$output" | grep -Fq "# Reference Inventory"
printf '%s\n' "$output" | grep -Fq "## Zero Code/Test References"
}
5 changes: 5 additions & 0 deletions test/integration/core-scripts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ load ../test_helper/test_helper
[ -x "$REPO_ROOT/script/branch-cleanup.sh" ]
}

@test "audit-references.sh exists and is executable" {
assert_file_exists "$REPO_ROOT/script/audit-references.sh"
[ -x "$REPO_ROOT/script/audit-references.sh" ]
}

@test "check-image-version.sh exists and is executable" {
assert_file_exists "$REPO_ROOT/script/check-image-version.sh"
[ -x "$REPO_ROOT/script/check-image-version.sh" ]
Expand Down
Loading