-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add shared YAML-config file size checker with tiered limits #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JacobPEvans
wants to merge
5
commits into
main
Choose a base branch
from
feature/file-size-yaml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9485a9
feat: add shared YAML-config file size checker with tiered limits
JacobPEvans b5e3c31
fix(pre-commit): add pass_filenames: false to file-size-check hook
JacobPEvans 5632d50
fix(file-size): add curl retries and graceful fallback on download fa…
JacobPEvans f3c6339
fix(file-size): decouple yq detection from config parsing, use raw ou…
JacobPEvans 27dc113
refactor: simplify check-file-sizes.sh from 296 to 155 lines
JacobPEvans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # ============================================================================= | ||
| # Org-wide File Size Limits | ||
| # ============================================================================= | ||
| # Per-repo overrides: create .file-size.yml in the repo root. | ||
| # Sizes are in bytes. | ||
| # ============================================================================= | ||
|
|
||
| defaults: | ||
| warn: 5120 # 5 KB | ||
| error: 10240 # 10 KB | ||
|
|
||
| extended: | ||
| warn: 10240 # 10 KB | ||
| error: 20480 # 20 KB | ||
| files: | ||
| - CHANGELOG.md | ||
| - CONTRIBUTING.md | ||
| - LABELS.md | ||
| - README.md | ||
| - repo-health-audit-prompt.md | ||
|
|
||
| exempt: | ||
| - "*.lock" | ||
| - "package-lock.json" | ||
| - "pnpm-lock.yaml" | ||
|
|
||
| scan: | ||
| - .nix | ||
| - .md | ||
| - .sh | ||
| - .yml | ||
| - .yaml | ||
| - .tf | ||
| - .py | ||
| - .j2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/usr/bin/env bash | ||
| # ============================================================================= | ||
| # File Size Checker | ||
| # ============================================================================= | ||
| # Checks file sizes against YAML config (.file-size.yml). | ||
| # Requires yq (Go mikefarah/yq or Python kislyuk/yq). | ||
| # Outputs GitHub Actions annotations in CI, plain text otherwise. | ||
| # Exit code = number of error-level violations (capped at 125). | ||
| # ============================================================================= | ||
| set -euo pipefail | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Defaults (used when no config file is present) | ||
| # --------------------------------------------------------------------------- | ||
| DEFAULT_WARN=5120 | ||
| DEFAULT_ERROR=10240 | ||
| DEFAULT_SCAN=(.nix .md .sh .yml .yaml .tf .py .j2) | ||
| EXTENDED_WARN="" | ||
| EXTENDED_ERROR="" | ||
| declare -a EXTENDED_FILES=() | ||
| declare -a EXEMPT_PATTERNS=() | ||
| declare -a SCAN_EXTENSIONS=() | ||
|
|
||
| # Resolve CI mode once at startup | ||
| CI_MODE=false | ||
| [[ -n "${GITHUB_ACTIONS:-}" ]] && CI_MODE=true | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers | ||
| # --------------------------------------------------------------------------- | ||
| emit() { | ||
| local level="$1" file="$2" msg="$3" | ||
| if $CI_MODE; then | ||
| echo "::${level} file=${file}::${msg}" | ||
| else | ||
| echo "${level^^}: ${file}: ${msg}" | ||
| fi | ||
| } | ||
|
|
||
| # Populate a global array from a command's line-by-line output | ||
| # Usage: read_into ARRAY_NAME command args... | ||
| read_into() { | ||
| local -n _arr=$1; shift | ||
| while IFS= read -r line; do | ||
| [[ -n "$line" ]] && _arr+=("$line") | ||
| done < <("$@") | ||
| } | ||
|
|
||
| # Check if a basename matches any entry in a named array (glob matching) | ||
| # Usage: matches_list ARRAY_NAME "$file" | ||
| matches_list() { | ||
| local -n _list=$1 | ||
| local bn | ||
| bn=$(basename "$2") | ||
| for pattern in "${_list[@]}"; do | ||
| # shellcheck disable=SC2254 | ||
| case "$bn" in $pattern) return 0 ;; esac | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Parse config (requires yq) | ||
| # --------------------------------------------------------------------------- | ||
| parse_config() { | ||
| local config="$1" | ||
|
|
||
| if ! command -v yq &>/dev/null; then | ||
| echo "ERROR: yq is required to parse ${config}" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| if ! yq '.' "$config" &>/dev/null; then | ||
| echo "ERROR: yq failed to parse ${config}" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| DEFAULT_WARN=$(yq -r '.defaults.warn // empty' "$config") | ||
| DEFAULT_ERROR=$(yq -r '.defaults.error // empty' "$config") | ||
| EXTENDED_WARN=$(yq -r '.extended.warn // empty' "$config") | ||
| EXTENDED_ERROR=$(yq -r '.extended.error // empty' "$config") | ||
|
|
||
| read_into EXTENDED_FILES yq -r '.extended.files // [] | .[]' "$config" | ||
| read_into EXEMPT_PATTERNS yq -r '.exempt // [] | .[]' "$config" | ||
| read_into SCAN_EXTENSIONS yq -r '.scan // [] | .[]' "$config" | ||
|
|
||
| : "${DEFAULT_WARN:=5120}" | ||
| : "${DEFAULT_ERROR:=10240}" | ||
| } | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Main | ||
| # --------------------------------------------------------------------------- | ||
| main() { | ||
| local config_file=".file-size.yml" | ||
|
|
||
| if [[ -f "$config_file" ]]; then | ||
| parse_config "$config_file" || exit 1 | ||
| fi | ||
|
|
||
| if [[ ${#SCAN_EXTENSIONS[@]} -eq 0 ]]; then | ||
| SCAN_EXTENSIONS=("${DEFAULT_SCAN[@]}") | ||
| fi | ||
|
|
||
| # Build find name predicates | ||
| local name_args=() first=true | ||
| for ext in "${SCAN_EXTENSIONS[@]}"; do | ||
| $first && first=false || name_args+=(-o) | ||
| name_args+=(-name "*${ext}") | ||
| done | ||
|
|
||
| local error_count=0 warning_count=0 | ||
| local size_bytes warn_limit error_limit size_kb limit_kb | ||
|
|
||
| while IFS= read -r -d '' file; do | ||
| if [[ ${#EXEMPT_PATTERNS[@]} -gt 0 ]] && matches_list EXEMPT_PATTERNS "$file"; then | ||
| continue | ||
| fi | ||
|
|
||
| size_bytes=$(wc -c < "$file" | tr -d ' ') | ||
| warn_limit="$DEFAULT_WARN" | ||
| error_limit="$DEFAULT_ERROR" | ||
|
|
||
| if [[ -n "${EXTENDED_WARN:-}" && -n "${EXTENDED_ERROR:-}" ]] \ | ||
| && [[ ${#EXTENDED_FILES[@]} -gt 0 ]] \ | ||
| && matches_list EXTENDED_FILES "$file"; then | ||
| warn_limit="$EXTENDED_WARN" | ||
| error_limit="$EXTENDED_ERROR" | ||
| fi | ||
|
|
||
| if [[ "$size_bytes" -ge "$error_limit" ]]; then | ||
| size_kb=$(( size_bytes / 1024 )); limit_kb=$(( error_limit / 1024 )) | ||
| emit error "$file" "${size_kb}KB exceeds ${limit_kb}KB error limit" | ||
| error_count=$(( error_count + 1 )) | ||
| elif [[ "$size_bytes" -ge "$warn_limit" ]]; then | ||
| size_kb=$(( size_bytes / 1024 )); limit_kb=$(( warn_limit / 1024 )) | ||
| emit warning "$file" "${size_kb}KB exceeds ${limit_kb}KB warning limit" | ||
| warning_count=$(( warning_count + 1 )) | ||
| fi | ||
| done < <(find . -type f \( "${name_args[@]}" \) \ | ||
| -not -path "./.git/*" -not -path "./result/*" -not -path "./node_modules/*" \ | ||
| -print0 2>/dev/null | sort -z) | ||
|
|
||
| if [[ $error_count -gt 0 || $warning_count -gt 0 ]]; then | ||
| echo "" | ||
| echo "File size check: ${error_count} error(s), ${warning_count} warning(s)" | ||
| else | ||
| echo "File size check: all files within limits" | ||
| fi | ||
|
|
||
| [[ $error_count -gt 125 ]] && exit 125 | ||
| exit "$error_count" | ||
| } | ||
|
|
||
| main "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.