Repository navigation
pre-commit alternative to explicit GitHub Actions for formatting
#440
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
knoepfel
wants to merge
6
commits into
Framework-R-D:main
Choose a base branch
from
knoepfel:pre-commit-workflow
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
576c236
ci: consolidate formatting workflows with pre-commit
knoepfel df7ebfd
fix: isolate formatting execution from privileged publishing
knoepfel a950235
ci(gating): update checks for consolidated formatting workflows
knoepfel 86d6f5b
test(scripts): make gating string concatenation explicit
knoepfel 3070940
ci: restore script coverage in pre-commit validation
knoepfel 9dbf02e
fix(scripts): simplify formatting validation and test entrypoint
knoepfel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| name: Run selected pre-commit hooks | ||
| description: Run formatting, validation, or unit-test hooks from the shared configuration | ||
| inputs: | ||
| category: | ||
| description: Hook category (formatting, validation, or tests) | ||
| required: true | ||
| from-ref: | ||
| description: Optional base revision for changed-file checks | ||
| default: "" | ||
| to-ref: | ||
| description: Optional head revision for changed-file checks | ||
| default: "" | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Install pre-commit | ||
| shell: bash | ||
| run: python -m pip install pre-commit | ||
|
|
||
| - name: Select and run hooks | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| shell: bash | ||
| env: | ||
| HOOK_CATEGORY: ${{ inputs.category }} | ||
| FROM_REF: ${{ inputs.from-ref }} | ||
| TO_REF: ${{ inputs.to-ref }} | ||
| run: | | ||
| config=$(mktemp "$RUNNER_TEMP/pre-commit.XXXXXX.yaml") | ||
| trap 'rm -f "$config"' EXIT | ||
| python - "$config" <<'PY' | ||
| import os | ||
| import sys | ||
|
|
||
| import yaml | ||
|
|
||
| formatting = { | ||
| "trailing-whitespace", "end-of-file-fixer", "mixed-line-ending", | ||
| "clang-format", "gersemi", "fix-header-guards", "jsonnet-format", | ||
| "markdownlint-cli2", "ruff", "ruff-format", "prettier", | ||
| } | ||
| tests = {"scripts-unit-tests"} | ||
| category = os.environ["HOOK_CATEGORY"] | ||
| if category not in {"formatting", "validation", "tests"}: | ||
| raise SystemExit(f"Unknown hook category: {category}") | ||
| with open(".pre-commit-config.yaml") as source: | ||
| config = yaml.safe_load(source) | ||
| configured = {hook["id"] for repo in config["repos"] for hook in repo["hooks"]} | ||
| missing = (formatting | tests) - configured | ||
| if missing: | ||
| raise SystemExit(f"Missing configured hooks: {sorted(missing)}") | ||
| selected = { | ||
| "formatting": formatting, | ||
| "validation": configured - formatting - tests, | ||
| "tests": tests, | ||
| }[category] | ||
| repos = [] | ||
| for repo in config["repos"]: | ||
| hooks = [hook for hook in repo["hooks"] if hook["id"] in selected] | ||
| for hook in hooks: | ||
| if hook["id"] == "mypy": | ||
| hook["args"] = [*hook.get("args", []), "."] | ||
| hook["pass_filenames"] = False | ||
| hook["always_run"] = True | ||
| elif hook["id"] == "scripts-unit-tests": | ||
| hook["additional_dependencies"] = [ | ||
| *hook.get("additional_dependencies", []), "pytest-cov", | ||
| ] | ||
| hook["args"] = [ | ||
| *hook.get("args", []), | ||
| "--cov=scripts", "--cov=ci", "--cov=.devcontainer", | ||
| "--cov-report=xml:coverage-scripts.xml", "--cov-report=term-missing", | ||
| ] | ||
| if hooks: | ||
| repos.append({**repo, "hooks": hooks}) | ||
| config["repos"] = repos | ||
| with open(sys.argv[1], "w") as destination: | ||
| yaml.safe_dump(config, destination) | ||
| PY | ||
| args=(--all-files) | ||
| if [[ -n "$FROM_REF" && -n "$TO_REF" ]]; then | ||
| args=(--from-ref "$FROM_REF" --to-ref "$TO_REF") | ||
| elif [[ -n "$FROM_REF" || -n "$TO_REF" ]]; then | ||
| echo "::error::Both from-ref and to-ref are required for changed-file checks" | ||
| exit 1 | ||
| fi | ||
| python -m pre_commit run --config "$config" "${args[@]}" --show-diff-on-failure | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.