This is a tool to determine if a GitHub organization's repositories
have .pre-commit-config.yaml files or not; if they do not, it will
check to see if an issue exists to create that file.
My team became privy to a situation where several secrets (credentials, API tokens, etc.) were committed to a repository. MegaLinter was run on the repository and happened to detect these secrets that, while no longer active, were artifacts from previous development iterations. We were tasked to looking into the situation and provide advice and guidance about how to prevent situations like these from happening again.
In addition to running MegaLinter whenever Pull Requests (PRs) were submitted, we recommended a layer of protection before commits were created or pushed upstream.
The pre-commit tool allows us to configure steps to run before commits are created. These steps use git's hooks functionality to invoke tools to protect our developers:
From an administrative perspective, we didn't -- and can't -- have a mechanism to determine if an individual developer did or did not use pre-commit. Similarly, we couldn't force individual developers to install or setup pre-commit on a system or repository basis.
What did know was that if a repository did not have a pre-commit configuration, we know that they do not have pre-commit protection in place.
So, this tool scans through all of the repositories in a GitHub organization and attempts to retrieve their pre-commit configuration files. For each repository, if there is no pre-commit configuration file, we create an issue in the repository. If there is a pre-commit file but that file is empty or invalid or doesn't have the elements pre-commit requires, we create an issue in the repository.
The issues this tool creates are based on a
Jinja2 template.
This template describes the background, provides instructions on
how to use pre-commit, and includes a sample pre-commit configuration
file that the developer can store in .pre-commit-config.yaml in
the repository.
The tool is configured using environment variables and/or a .env
file. This method was selected so that the tool could be run
in a containerized environment, as a CI/CD workflow, as a
scheduled process, etc..
The tool does not require disk access -- it can run in an ephermal, immutable exection environment.
Required values:
.pre-commit-config.yaml files, and
create issues on the repositories that don't meet the tool's
requirements.Optional values:
Missing pre-commit configurationopen-issue.j2close-issue.j2templates/https://api.github.com is sufficient for repositories
hosted on GitHub.com.Install the application's Python dependencies, then run the standard-library
unittest suite from the repository root:
python3 -m pip install -r requirements.txt
python3 -m unittest discover -s tests -p 'test_*.py' -v
The unit tests are offline. They replace GitHub API clients, repositories,
and issues with mocks, and use local Jinja2 templates and encoded YAML test
fixtures. No real GitHub token or organization is needed, and the tests never
write to a remote repository. The CI job named test-unit runs the same
suite on pull requests and pushes to main.
Tests describe the application's existing validation and issue-management behavior. They do not currently require full pre-commit schema validation or correct separate configuration/issue-counting defects.
Content type
Image
Digest
sha256:dd286b19e…
Size
60.1 MB
Last updated
5 days ago
docker pull wesleydeanflexion/pre-commit-checker:sha-d69852d70434f5694210ac28db0fec3ff1a5b423