Skip to content

Commit

Permalink
Add precommit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jan 7, 2023
1 parent 72544e4 commit 13d4ffe
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ jobs:
- run: python3 -m pip install --user --force-reinstall dist/c2cciutils-0.0.0-py3-none-any.whl[checks,publish]
- run: rm -rf dist build

- name: Checks pre-commit
run: |
pip install pre-commit
pre-commit run --all-files
- name: Checks
run: c2cciutils-checks

Expand Down
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: local
hooks:
- id: c2cciutils
name: c2cciutils
description: Run c2cciutils checks
entry: pre-commit-hook --no-check=prettier
language: script
always_run: true
verbose: true
require_serial: true
49 changes: 49 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
- id: all
name: c2cciutils all
description: Run c2cciutils all checks
entry: pre-commit-hook
language: script
always_run: true
require_serial: true
- id: eof
name: c2cciutils eof
description: Run c2cciutils eof checks
entry: pre-commit-hook --check=eof
language: script
always_run: true
require_serial: true
- id: workflows
name: c2cciutils workflows
description: Run c2cciutils workflows checks
entry: pre-commit-hook --check=workflows
language: script
always_run: true
require_serial: true
- id: black
name: c2cciutils black
description: Run c2cciutils black checks
entry: pre-commit-hook --check=black
language: script
always_run: true
require_serial: true
- id: isort
name: c2cciutils isort
description: Run c2cciutils isort checks
entry: pre-commit-hook --check=isort
language: script
always_run: true
require_serial: true
- id: codespell
name: c2cciutils codespell
description: Run c2cciutils codespell checks
entry: pre-commit-hook --check=codespell
language: script
always_run: true
require_serial: true
- id: prettier
name: c2cciutils prettier
description: Run c2cciutils prettier checks
entry: pre-commit-hook --check=prettier
language: script
always_run: true
require_serial: true
66 changes: 50 additions & 16 deletions c2cciutils/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
from argparse import Namespace
from io import StringIO
from typing import Any, Dict
from typing import Any, Dict, List

import ruamel.yaml
import yaml
Expand All @@ -25,6 +25,7 @@ def print_config(
config: None,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Print the configuration.
Expand All @@ -45,7 +46,10 @@ def print_config(


def print_environment_variables(
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace
config: None,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Print the environment variables.
Expand All @@ -63,7 +67,7 @@ def print_environment_variables(


def print_github_event(
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace, **_: Any
) -> bool:
"""
Print the GitHub event.
Expand Down Expand Up @@ -126,9 +130,7 @@ def _check_properties(


def gitattribute(
config: None,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace, **_: Any
) -> bool:
"""
Check that we don't have any error with the gitattributes.
Expand Down Expand Up @@ -163,14 +165,27 @@ def gitattribute(
F_NULL = open(os.devnull, "w", encoding="utf-8") # pylint: disable=consider-using-with


def eof(config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace) -> bool:
def _all_files(files: List[str]) -> List[str]:
"""
Return all the files in Git.
"""
results = subprocess.check_output(["git", "ls-files"], encoding="utf-8").strip().split("\n")
if files:
results = [filename for filename in results if filename in files]
return results


def eof(
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace, files: List[str]
) -> bool:
r"""
Check the non empty text files end with "\n".
Arguments:
config: The check section config
full_config: All the CI config
args: The parsed command arguments
files: The files to check
"""
del config, full_config

Expand All @@ -179,7 +194,7 @@ def eof(config: None, full_config: c2cciutils.configuration.Configuration, args:

sys.stdout.flush()
sys.stderr.flush()
for filename in subprocess.check_output(["git", "ls-files"]).decode().split("\n"):
for filename in _all_files(files):
if os.path.isfile(filename):
if (
subprocess.call(
Expand Down Expand Up @@ -207,6 +222,7 @@ def eof(config: None, full_config: c2cciutils.configuration.Configuration, args:

return success
except subprocess.CalledProcessError:
print("ccc")
c2cciutils.error(
"eof",
"Error, see above",
Expand All @@ -215,9 +231,7 @@ def eof(config: None, full_config: c2cciutils.configuration.Configuration, args:


def workflows(
config: None,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
config: None, full_config: c2cciutils.configuration.Configuration, args: Namespace, files: List[str]
) -> bool:
"""
Check each workflow have a timeout and/or do not use blacklisted images.
Expand All @@ -226,13 +240,16 @@ def workflows(
config: The check section config
full_config: All the CI config
args: The parsed command arguments
files: The files to check
"""
del config, full_config, args

success = True
files = glob.glob(".github/workflows/*.yaml")
files += glob.glob(".github/workflows/*.yml")
for filename in files:
workflow_files = glob.glob(".github/workflows/*.yaml")
workflow_files += glob.glob(".github/workflows/*.yml")
if files:
workflow_files = [filename for filename in workflow_files if filename in files]
for filename in workflow_files:
with open(filename, encoding="utf-8") as open_file:
workflow = yaml.load(open_file, Loader=yaml.SafeLoader)

Expand All @@ -252,6 +269,7 @@ def black(
config: c2cciutils.configuration.ChecksBlackConfig,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
files: List[str],
) -> bool:
"""
Run black check on all files including Python files without .py extension.
Expand All @@ -263,6 +281,7 @@ def black(
config: The check section config
full_config: All the CI config
args: The parsed command arguments
files: The files to check
"""
del full_config

Expand All @@ -274,6 +293,8 @@ def black(
cmd += ["--color", "--diff", "--check"]
cmd.append("--")
python_files = c2cciutils.get_git_files_mime(ignore_patterns_re=config.get("ignore_patterns_re", []))
if files:
python_files = [filename for filename in python_files if filename in files]
cmd += python_files
if len(python_files) > 0:
subprocess.check_call(cmd)
Expand All @@ -290,6 +311,7 @@ def isort(
config: c2cciutils.configuration.ChecksIsortConfig,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
files: List[str],
) -> bool:
"""
Run isort check on all files including Python files without .py extension.
Expand All @@ -301,6 +323,7 @@ def isort(
config: The check section config
full_config: All the CI config
args: The parsed command arguments
files: The files to check
"""
del full_config

Expand All @@ -312,6 +335,8 @@ def isort(
cmd += ["--check-only", "--diff"]
cmd.append("--")
python_files = c2cciutils.get_git_files_mime(ignore_patterns_re=config.get("ignore_patterns_re", []))
if files:
python_files = [filename for filename in python_files if filename in files]
cmd += python_files
if len(python_files) > 0:
subprocess.check_call(cmd)
Expand All @@ -328,6 +353,7 @@ def codespell(
config: c2cciutils.configuration.ChecksCodespellConfig,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
files: List[str],
) -> bool:
"""
Run codespell check on all files.
Expand All @@ -342,6 +368,7 @@ def codespell(
config: The check section config
full_config: All the CI config
args: The parsed command arguments
files: The files to check
"""

try:
Expand All @@ -353,7 +380,7 @@ def codespell(
"ignore_re", c2cciutils.configuration.CODESPELL_IGNORE_REGULAR_EXPRESSION_DEFAULT
)
]
for filename in subprocess.check_output(["git", "ls-files"]).decode().strip().split("\n"):
for filename in _all_files(files):
if os.path.isfile(filename):
include = True
for ignore_re in ignore_res:
Expand All @@ -378,6 +405,7 @@ def prettier(
config: c2cciutils.configuration.ChecksPrettierConfig,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
files: List[str],
) -> bool:
"""
Run prettier check on all the supported files.
Expand All @@ -389,13 +417,14 @@ def prettier(
config: The config
full_config: The full config
args: The parsed command arguments
files: The files to check
"""
del config, full_config

success = True

with c2cciutils.prettier.Prettier() as prettier_lib:
for filename in subprocess.check_output(["git", "ls-files"]).decode().strip().split("\n"):
for filename in _all_files(files):
if os.path.isfile(filename):
info = prettier_lib.get_info(filename)
if info.get("info", {}).get("ignored", False):
Expand Down Expand Up @@ -423,6 +452,7 @@ def snyk(
config: c2cciutils.configuration.ChecksSnykConfiguration,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Run Snyk check.
Expand All @@ -448,6 +478,7 @@ def snyk_code(
config: c2cciutils.configuration.ChecksSnykCodeConfiguration,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Run Snyk code check.
Expand All @@ -472,6 +503,7 @@ def snyk_iac(
config: c2cciutils.configuration.ChecksSnykIacConfiguration,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Run Snyk iac check.
Expand All @@ -494,6 +526,7 @@ def snyk_fix(
config: c2cciutils.configuration.ChecksSnykFixConfiguration,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Run Snyk fix.
Expand All @@ -519,6 +552,7 @@ def print_versions(
config: c2cciutils.configuration.PrintVersions,
full_config: c2cciutils.configuration.Configuration,
args: Namespace,
**_: Any,
) -> bool:
"""
Print some tools versions.
Expand Down
Loading

0 comments on commit 13d4ffe

Please sign in to comment.