Skip to content

Commit

Permalink
Replace some checks by pre commits hooks
Browse files Browse the repository at this point in the history
To use more standards tools
  • Loading branch information
sbrunner committed Apr 5, 2023
1 parent 26b498b commit bb568f9
Show file tree
Hide file tree
Showing 27 changed files with 570 additions and 1,440 deletions.
8 changes: 8 additions & 0 deletions .github/jsonschema-gentypes-applications.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
headers: |
"""
Automatically generated file from a JSON schema.
"""
generate:
- source: c2cciutils/schema-applications.json
destination: c2cciutils/applications_definition.py
root_name: ApplicationsConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ generate:
root_name: Configuration
api_arguments:
additional_properties: Only explicit
- source: c2cciutils/schema-applications.json
destination: c2cciutils/applications_definition.py
root_name: ApplicationsConfiguration
5 changes: 5 additions & 0 deletions .github/jsonschema-precommit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -eux

pip install --requirement=requirements.txt
touch c2cciutils/schema-applications.json c2cciutils/schema.json
make jsonschema
5 changes: 2 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
if: env.HAS_SECRETS == 'HAS_SECRETS'

- run: echo "${HOME}/.local/bin" >> ${GITHUB_PATH}
- run: cd c2cciutils; npm install
- run: python3 -m pip install --user --requirement=requirements.txt
- run: poetry build
env:
Expand All @@ -57,6 +56,8 @@ jobs:
if: failure()
- name: Checks
run: c2cciutils-checks
env:
GITHUB_EVENT: ${{ toJson(github) }}

- name: Build
run: make build
Expand Down Expand Up @@ -91,7 +92,6 @@ jobs:
fetch-depth: 0

- run: echo "${HOME}/.local/bin" >> ${GITHUB_PATH}
- run: cd c2cciutils; npm install
- run: python3 -m pip install --user --requirement=requirements.txt
- run: poetry build
env:
Expand Down Expand Up @@ -135,7 +135,6 @@ jobs:
github-gopass-ci-token: ${{secrets.GOPASS_CI_GITHUB_TOKEN}}

- run: python3 -m pip install --user --requirement=requirements.txt
- run: cd c2cciutils; npm install
- run: ~/.local/bin/poetry build
env:
POETRY_DYNAMIC_VERSIONING_BYPASS: 0.0.0
Expand Down
28 changes: 28 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- id: workflows-require-timeout
name: workflows require timeout
description: Check that the workflows have a timeout
entry: c2cciutils-checks
args:
- --check=workflow
files: ^\.github/workflows/.+\.ya?ml$
language: python
- id: check-poetry
name: poetry check
description: Validates the structure of the pyproject.toml file
entry: poetry check
language: python
additional_dependencies:
- poetry
pass_filenames: false
files: ^pyproject\.toml$
- id: lock-poetry
name: poetry lock
description: Lock the from the pyproject.toml file
entry: poetry lock
args:
- --no-update
language: python
additional_dependencies:
- poetry
pass_filenames: false
files: ^pyproject\.toml$
29 changes: 25 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,30 @@ checks: prospector ## Do the checks
prospector: build-checker ## Run Prospector
docker run --rm --volume=${PWD}:/app $(GITHUB_REPOSITORY)-checker prospector --ignore-paths=example-project/ --output=pylint --die-on-tool-error

config.md: c2cciutils/schema.json
jsonschema2md $< $@
# Needed to succeed the pre-commit
pre-commit run --files=$@ prettier > /dev/null || true

applications.md: c2cciutils/schema-applications.json
jsonschema2md $< $@
# Needed to succeed the pre-commit
pre-commit run --files=$@ prettier > /dev/null || true

c2cciutils/configuration.py: .github/jsonschema-gentypes-configuration.yaml c2cciutils/schema.json
jsonschema-gentypes --config=$<
# Needed to succeed the pre-commit
pre-commit run --files=$@ pyupgrade > /dev/null || true
pre-commit run --files=$@ isort > /dev/null || true
pre-commit run --files=$@ black > /dev/null || true

c2cciutils/applications_definition.py: .github/jsonschema-gentypes-applications.yaml c2cciutils/schema-applications.json
jsonschema-gentypes --config=$<
# Needed to succeed the pre-commit
pre-commit run --files=$@ pyupgrade > /dev/null || true
pre-commit run --files=$@ isort > /dev/null || true
pre-commit run --files=$@ black > /dev/null || true

.PHONY: jsonschema
jsonschema: ## Generate files depends on the JSON schema
jsonschema-gentypes
jsonschema2md c2cciutils/schema.json config.md
jsonschema2md c2cciutils/schema-applications.json applications.md
c2cciutils-checks --fix --check=prettier
jsonschema: config.md applications.md c2cciutils/configuration.py c2cciutils/applications_definition.py
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ The goals of C2C CI utils are:
- Git attributes
- That the timeout is present in the GitHub workflow files
- That the stabilization version (get from the Security.md) are used everywhere it's needed
- Check the code style:
- End of files
- Check the code style with [pre-commit](https://pre-commit.com):
- Black
- Isort
- Code spell
- Prettier
- ...
- Snyk tests
- Test (never failed)
- Code test (never failed, disabled by default)
Expand Down
6 changes: 4 additions & 2 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def print_versions(config: c2cciutils.configuration.PrintVersions) -> bool:
config: The print configuration
"""

for version in config.get("versions", []):
for version in config.get("versions", c2cciutils.configuration.PRINT_VERSIONS_VERSIONS_DEFAULT):
try:
sys.stdout.flush()
sys.stderr.flush()
Expand Down Expand Up @@ -565,7 +565,7 @@ def get_codespell_command(config: c2cciutils.configuration.Configuration, fix: b
config: The full configuration
fix: If we should fix the errors
"""
codespell_config = config.get("checks", {}).get("codespell", {})
codespell_config = config.get("codespell", {})
codespell_config = codespell_config if isinstance(codespell_config, dict) else {}
command = ["codespell"]
if fix:
Expand All @@ -592,4 +592,6 @@ def snyk_exec() -> Tuple[str, Dict[str, str]]:
if "SNYK_ORG" in env:
subprocess.run(["snyk", "config", "set", f"org={env['SNYK_ORG']}"], check=True, env=env)

if not os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), "node_modules")):
subprocess.run(["npm", "install"], check=True, cwd=os.path.dirname(os.path.abspath(__file__)))
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "node_modules/snyk/bin/snyk"), env
Loading

0 comments on commit bb568f9

Please sign in to comment.