Skip to content

Pre-Commit #37

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

Merged
merged 5 commits into from
May 10, 2022
Merged

Pre-Commit #37

merged 5 commits into from
May 10, 2022

Conversation

kchason
Copy link
Member

@kchason kchason commented Mar 21, 2022

  • Provides configuration and documentation for pre-commit
  • Configures pre-commit to run in the GitHub Actions

@ajnelson-nist
Copy link
Member

I found one concern by trying out pre-commit with the virtual environment built as part of this repository's testing workflow. In this sequence of events:

source tests/venv/bin/activate
pre-commit install
deactivate
rm -rf tests/venv  # NOTE: This happens as part of 'make clean'

I have to manually undo the effects of pre-commit install by running rm .git/hooks/pre-commit. Otherwise, git commit ceases to function. This is a significant usability issue, worth warning folks away from. It looks like this would affect folks who follow development starting with the CI steps (clone, then run make check).

It looks like this PR has no automatic effect on CI. I see no impact on CI, so if I forgot to run pre-commit install, it looks like code could still be committed and merged without black intervening in the Github space. In a similar effect, PR 35 added a test to confirm black formatting, but stopped short of incorporating that test into make check.

So, it looks like this PR adds a beneficial way to guarantee a pre-commit script runs, but only for people who can run pre-commit install into a durable environment. That could be system-level, or anything except the virtual environment built as part of the make-oriented development workflow used in this repository. Is that a fair assessment, @kchason ? If so, the new CONTRIBUTE.md section should be adjusted. (It should be adjusted anyway - there's a missing newline, first patched line in that file.)

These are technical questions that are incidental to the main policy question: Will black be guaranteed to have run on every commit (or at least branch head) of this repository before merging into develop? If we think pre-commit handles this for non-Posix (/non-WSL) folks, and make can handle it for Posix (/WSL) folks, we can move to encoding that policy in a follow-on PR. I do suggest that be a follow-on PR, for one last review from the Adoption Committee.

@kchason
Copy link
Member Author

kchason commented Mar 28, 2022

I haven't yet experienced the venv issue but I'll attempt to reproduce to further troubleshoot.

With regards to the CI, pre-commit exits with an error code of 1 if it does not conform to the defined rule-set so it forces compliance with the black formatting, however the contributor chooses to format the code (with a make process, pre-commit, manual black CLI tool, or manually edited).

@ajnelson-nist
Copy link
Member

ajnelson-nist commented Mar 29, 2022

@kchason I left most of how to re-produce the issue in the Bash snippet above. Please let me know if you want me to script the whole toe-stubbing.

Meanwhile, I thought of another solution that avoids the make clean issue. The problem is supporting these constraints:

  • Not installing the tests/venv virtual environment, because it can be reasonably expected to be deleted frequently.
  • Not requiring an elevated-permissions call to enable pre-commit outside of the tests/venv environment (e.g. in the system-wide Python installation). So far, the CASE utilities do not require admin/root to install, and I'd strongly prefer to keep that true.

My current thinking is to make a dedicated virtual environment in the repository root, once, and not delete it on make clean calls. In Make style, it can be done by adding this recipe's target as a dependency of all:

.venv-pre-commit/var/built.log:
        rm -rf .venv-pre-commit
        python3 -m venv .venv-pre-commit
        source .venv-pre-commit/bin/activate && pip install pre-commit
        source .venv-pre-commit/bin/activate && pre-commit install
        mkdir -p .venv-pre-commit/var
        touch $@

If this rule is an all dependency, pre-commit will work after one run of make, and still work after make clean. This should only be integrated after a .pre-commit-config.yaml file is tracked.

How do we feel about making a persistent virtual environment?

ajnelson-nist added a commit to casework/CASE-Examples that referenced this pull request Apr 11, 2022
This dedicated virtual environment is intended to persist through
`make clean`, not disrupting the ability to run regular Git commits if
the "primary" virtual environment is removed.  The `pre-commit`
experience it produces should align make- and local-venv-oriented users
with users who have a more global availability of `pre-commit`.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
@ajnelson-nist
Copy link
Member

Another behavior I've observed, which is likely to impact make users:

Say a file is edited, but not staged for committing (no git add has been run yet). If the pre-commit hooks are run, either via committing some other file(s) or by running pre-commit run, the edited file is stashed with git stash so it stays out of the way of pre-commit doing its review, and then the stash is "popped", restoring the edited file.

It seems that this temporary removal keeps text editor sessions with that edited file open happy because it happens and then reverts quickly enough that the editor either doesn't have time to complain about losing its open file.

However, the stash push-pop cycle updates the timestamp on the edited file. This will cause an impact on a make user, because any re-run of make later will see that updated timestamp and run its update steps accordingly.

So, overall impact of incorporating pre-commit seems it will include unexpected re-runs of workflows.

ajnelson-nist added a commit to casework/CASE-Examples that referenced this pull request Apr 12, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-PROV-O that referenced this pull request Apr 18, 2022
@ajnelson-nist
Copy link
Member

I've been trying this in a few other repositories. It has some behaviors that induce mildly unpleasant side effects for Make and Vim users related to git stash, but it hasn't caused sufficiently big problems that I'd oppose integrating it here.

I'm going to add a Make step to set up that dedicated pre-commit virtual environment, but otherwise there's only one last matter I'd like to know for this PR:

The version of Black listed in the pre-commit setup is hard-coded. What is the practice you'd suggest for keeping that decently fresh? Should we make a pre-release checklist for pull requests into main, one of the steps being reviewing .pre-commit-config.yaml?

Disclaimer:
Participation by NIST in the creation of the documentation of mentioned
software is not intended to imply a recommendation or endorsement by the
National Institute of Standards and Technology, nor is it intended to
imply that any specific software is necessarily the best available for
the purpose.

References:
* [AC-215] Evaluate pre-commit usage on casework repositories
* [AC-216] Apply Black to all casework Python code bases
* casework#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
Disclaimer:
Participation by NIST in the creation of the documentation of mentioned
software is not intended to imply a recommendation or endorsement by the
National Institute of Standards and Technology, nor is it intended to
imply that any specific software is necessarily the best available for
the purpose.

References:
* [AC-215] Evaluate pre-commit usage on casework repositories
* [AC-216] Apply Black to all casework Python code bases
* casework#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
Disclaimer:
Participation by NIST in the creation of the documentation of mentioned
software is not intended to imply a recommendation or endorsement by the
National Institute of Standards and Technology, nor is it intended to
imply that any specific software is necessarily the best available for
the purpose.

References:
* [AC-215] Evaluate pre-commit usage on casework repositories
* casework#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
@ajnelson-nist
Copy link
Member

@kchason , I added three commits to the pre-commit-black branch on this repository. I don't think I have push permission to your fork, and it's perfectly fine for you to not give me that permission.

If you agree with the three commits, please merge them into your branch so this PR will pick them up.

I still look forward to hearing what you think is practical for handling keeping the hard-coded versions fresh.

@ajnelson-nist
Copy link
Member

@kchason , I've pushed a catch-up merge that handles what apparently has become a merge conflict since your branch state. Can you please catch your branch up with the pre-commit-black branch on this repository?

@kchason
Copy link
Member Author

kchason commented May 10, 2022

Should be all set now

@ajnelson-nist
Copy link
Member

Thanks! I'll merge once CI passes.

@ajnelson-nist ajnelson-nist merged commit a085b62 into casework:develop May 10, 2022
ajnelson-nist added a commit to casework/casework.github.io that referenced this pull request May 17, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/casework.github.io that referenced this pull request May 17, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-PROV-O that referenced this pull request May 17, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to ajnelson-nist/gddrescue_mapfile_to_dfxml that referenced this pull request May 18, 2022
Pre-commit usage was evaluated here:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to ucoProject/UCO that referenced this pull request May 26, 2022
ajnelson-nist added a commit to ajnelson-nist/CASE-Examples-QC that referenced this pull request Jun 23, 2022
A follow-on patch will apply Python changes based on pre-commit review.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/casework.github.io that referenced this pull request Jul 22, 2022
It is not clear why this file was not regenerated earlier.  Current
suspected reason is `pre-commit`'s Git operations updating timestamps,
causing `make` to think it was already up to date.  `make clean && make`
regenerated this.

References:
* ucoProject/UCO#383
* casework/CASE-Utilities-Python#37 (comment)

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Utility-SHACL-Inheritance-Reviewer that referenced this pull request Jul 30, 2022
This implementation was copied from case-utils.

A follow-on patch will apply pre-commit effects.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-GNU-Time that referenced this pull request Dec 16, 2022
A follow-on patch will add pre-commit to CI.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-GNU-Time that referenced this pull request Dec 16, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-GNU-Time that referenced this pull request Dec 16, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-GNU-Time that referenced this pull request Dec 16, 2022
A follow-on patch will add pre-commit to CI.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-GNU-Time that referenced this pull request Dec 16, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to casework/CASE-Implementation-ExifTool that referenced this pull request Dec 21, 2022
References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to ucoProject/UCO that referenced this pull request Mar 13, 2023
This patch only applies mechanical changes from a tool used in the CASE
Python utilities.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to ajnelson-nist/INDXParse that referenced this pull request Jul 26, 2023
Issues 38 and 41 began discussions on a testing framework that would
eventually include static type review.

Work done recently with @sldouglas-nist has been adding type signatures.
Testing of each submitted pull request has included a manual run of
`mypy` against some objective script files within INDXParse.

This patch begins automating the review of type signatures, following
the results of some discussions on Issue 38 - namely, use of Make.  Make
is used for the static type review, intentionally instead of
`pre-commit`, due to a certain behavior that could arise: If
`pre-commit` encounters any failure that it can't automatically fix, the
user in their local editing environment is prevented from completing the
`git commit` command.  A change to a type signature can have a wide-
ranging effect, and this patch is added to prevent that user-experience.
(Some of this has been discussed at further length in `case-utils` PR
37, where `pre-commit` was added to another project.)

The Makefile added in this patch runs `mypy` review over selected files
analyzed to date, with the expectation that eventually the command will
become `mypy indxparse`, not enumerating specific files.  The GitHub
Action is updated to include `make check` (parameterized for the
runner's selected Python) as the last step run.

The Makefile also confirms that the package installs in a Python 3
virtual environment, and incorporates the `mypy` package through a new
feature, `testing`.

Disclaimer: Participation by NIST in the creation of the documentation
of mentioned software is not intended to imply a recommendation or
endorsement by the National Institute of Standards and Technology, nor
is it intended to imply that any specific software is necessarily the
best available for the purpose.

References:
* casework/CASE-Utilities-Python#37
* williballenthin#38
* williballenthin#41

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to dfxml-working-group/dfxml_python that referenced this pull request Jun 21, 2024
This patch draws on Cyber Domain Ontology deployments from the noted
PRs.

References:
* casework/CASE-Utilities-Python#37
* Cyber-Domain-Ontology/CDO-Shapes-Example#1

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
ajnelson-nist added a commit to dfxml-working-group/dfxml_python that referenced this pull request Jun 21, 2024
Text adapted from contributions to `case-utils` by @kchason.

Disclaimer:
Participation by NIST in the creation of the documentation of mentioned
software is not intended to imply a recommendation or endorsement by the
National Institute of Standards and Technology, nor is it intended to
imply that any specific software is necessarily the best available for
the purpose.

References:
* casework/CASE-Utilities-Python#37

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants