[PULP-1105] Add pre-commit in github templates#1001
[PULP-1105] Add pre-commit in github templates#1001
Conversation
01fd5eb to
08b2c10
Compare
| then | ||
| pip install -r lint_requirements.txt | ||
| black . | ||
| pre-commit run -a black || true # always zero exit-code |
There was a problem hiding this comment.
In what conditions does this exit non-zero?
There was a problem hiding this comment.
If it modifies files, which means linting failed. But here we don't care, we want auto-format.
There was a problem hiding this comment.
Can it still communicate "Failed to reformat"?
There was a problem hiding this comment.
Kind of. I see this as an unexpected error:
pre-commit exits with specific codes:
1: a detected / expected error 3: an unexpected error 130: the process was interrupted by ^C
| cd "$(dirname "$(realpath -e "$0")")"/../.. | ||
|
|
||
| set -uv | ||
| set -u |
There was a problem hiding this comment.
Actually this makes it harder to have the output we want of, because it will error immediately after failure (e.g, if we invert grep exit code with $(! grep ...)).
ERROR: Detected mix of f-strings and gettext:
{RESULT}
There might be one, but I don't know a clever way of achieve this.
There was a problem hiding this comment.
I believe this should work:
EXIT_CODE=0
RESULT=$(grep -n "$PATTERN" "$@") || EXIT_CODE=$?Also I believe this script is lightweight enough to run on sh instead of bash.
There was a problem hiding this comment.
That raised an unbound variable error on the error case.
I found other option still using -e.
plugin-template
Outdated
| subprocess.run(["black", "--quiet", "."], cwd=plugin_root_dir) | ||
| subprocess.run(["pre-commit", "run", "-a", "black"], cwd=plugin_root_dir) |
There was a problem hiding this comment.
We should probably check the exit code here too.
There was a problem hiding this comment.
The intent looks the same as the update_ci part: auto-format if it can, and ignore either way.
Hence the supress error handling if the binary is not found.
Actually, this looks redundant. We'll run auto-format when we apply the template and on the CI update.
Should I drop it here (or on upadate_ci)?
There was a problem hiding this comment.
(I dropped it when running plugin-template. If you're running it manually you can apply the template and then run pre-commit to format on a different commit)
| - repo: 'https://github.com/pre-commit/pre-commit-hooks' | ||
| rev: {{ precommit_versions["pre-commit-hooks"] | quote }} |
There was a problem hiding this comment.
Does this mean these are installed from git? I'd rather have them installed from pypi.
There was a problem hiding this comment.
Yes. I believe the reason is that it's supposed to be language agnostic.
If you are concerned about mutability we could use the commit sha.
There was a problem hiding this comment.
I guess not. sha values are unmaintainable.
I just had a feeling that this is odd, now you gave me the argument I was lacking.
Thinking more about it, I don't like the fact that precommit needs to "own" the installation in the first place.
There was a problem hiding this comment.
What is the benefit over precommit "just" calling make format or make lint?
There was a problem hiding this comment.
It does some things, like handling file finding/filtering (non-trivial e.g, recognize shebang scripts), parallelization and owning the installation (python interpreter, packages, caching).
There was a problem hiding this comment.
And in some cases, skip checking if there are no changes.
| else | ||
| # original behavior (check all) | ||
| set -v | ||
| RESULT=$(grep -n -r --include \*.py "$PATTERN") | ||
| EXIT_CODE=$? | ||
| fi |
There was a problem hiding this comment.
I kept it for compatibility, similar to lint_requirements.
Although it's less likely that is actually used manually. Should I drop it?
There was a problem hiding this comment.
If that is how we are going to use it, then yes don't keep dead codepaths around.
Added a pre-commit config using the same linting steps as the lint CI job. Also added a layer to keep pre-commit and lint_requirements in sync. Slightly changed some lint scripts to adpat to pre-commit.
9dd44f8 to
80d408e
Compare
| fi | ||
|
|
||
| if [ $? -ne 1 ]; then | ||
| # grep returns 1 if it doesn't find a match |
f094a7b to
d278902
Compare
|
Ok, no more pushes until a new review. |
|
There is some concerns about how pre-commit manages dependencies with git and can't integrate with other tooling and requirement declarations. We'll look for an alternative based on a makefile and uv. |
As proposed here: https://discourse.pulpproject.org/t/proposal-using-pre-commit-for-static-checks/2170/5
This also adds pre-commit to the plugin_template repository itself.
Further improvements are:
pre-commit autoupdateand update plugin-template scriptTest PR: pulp/pulpcore#7267