Skip to content

Commit

Permalink
Remove .pre-commit-config.yaml model in favor of existing classes
Browse files Browse the repository at this point in the history
I found that the model I created already existed (in a slightly
different form). So I've updated the existing model to work for my use
case. Also, I updated the model to extend from BaseModel instead of
BaseSettings, since we probably don't want environment variables to
override the config in the file.
  • Loading branch information
tdurk93 committed Dec 4, 2023
1 parent ed7de0e commit b6e6d59
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
34 changes: 4 additions & 30 deletions secureli/abstractions/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import subprocess
import yaml

from secureli.repositories.settings import PreCommitSettings


class InstallFailedError(Exception):
"""Attempting to invoke pre-commit to set up our repo for the given template did not succeed"""
Expand Down Expand Up @@ -50,34 +52,6 @@ class RevisionPair(pydantic.BaseModel):
newRev: str


class PreCommitHookConfig(pydantic.BaseModel):
"""
Schema for individual hooks in .pre-commit-config.yaml
"""

id: str


class PreCommitRepoConfig(pydantic.BaseModel):
"""
Schema for each item in the "repos" list in the .pre-commit-config.yaml file
"""

repo: str
rev: str
hooks: list[PreCommitHookConfig]


class PreCommitConfig(pydantic.BaseModel):
"""
Schema for .pre-commit-config.yaml file.
There are other configuration options not included in this schema
See for details: https://pre-commit.com/#pre-commit-configyaml---top-level
"""

repos: list[PreCommitRepoConfig]


class InstallResult(pydantic.BaseModel):
"""
The results of calling install
Expand Down Expand Up @@ -151,7 +125,7 @@ def execute_hooks(

def check_for_hook_updates(
self,
config: PreCommitConfig,
config: PreCommitSettings,
tags_only: bool = True,
freeze: Optional[bool] = None,
) -> dict[str, RevisionPair]:
Expand Down Expand Up @@ -286,5 +260,5 @@ def get_pre_commit_config(self, folder_path: Path):
"""
path_to_config = folder_path / ".pre-commit-config.yaml"
with open(path_to_config, "r") as f:
data = PreCommitConfig(**yaml.safe_load(f))
data = PreCommitSettings(**yaml.safe_load(f))
return data
13 changes: 9 additions & 4 deletions secureli/repositories/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class LanguageSupportSettings(BaseSettings):
command_timeout_seconds: int = Field(default=300)


class PreCommitHook(BaseSettings):
class PreCommitHook(BaseModel):
"""
Hook settings for pre-commit.
"""
Expand All @@ -96,19 +96,24 @@ class PreCommitHook(BaseSettings):
exclude_file_patterns: Optional[list[str]] = Field(default=[])


class PreCommitRepo(BaseSettings):
class PreCommitRepo(BaseModel):
"""
Repo settings for pre-commit.
"""

url: str
url: str = Field(validation_alias="repo", serialization_alias="repo")
rev: str
hooks: list[PreCommitHook] = Field(default=[])
suppressed_hook_ids: list[str] = Field(default=[])


class PreCommitSettings(BaseSettings):
class PreCommitSettings(BaseModel):
"""
Various adjustments that affect how seCureLI configures the pre-commit system.
Extends schema for .pre-commit-config.yaml file.
See for details: https://pre-commit.com/#pre-commit-configyaml---top-level
"""

repos: list[PreCommitRepo] = Field(default=[])
Expand Down
9 changes: 5 additions & 4 deletions secureli/services/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import pydantic
import re

from secureli.abstractions.pre_commit import PreCommitAbstraction, PreCommitConfig
from secureli.abstractions.pre_commit import PreCommitAbstraction
from secureli.repositories.settings import PreCommitSettings


class ScanMode(str, Enum):
Expand Down Expand Up @@ -99,7 +100,7 @@ def _parse_scan_ouput(self, folder_path: Path, output: str = "") -> ScanOuput:
"""
failures = []
failure_indexes = []
pre_commit_config: PreCommitConfig = self.pre_commit.get_pre_commit_config(
pre_commit_config: PreCommitSettings = self.pre_commit.get_pre_commit_config(
folder_path
)

Expand Down Expand Up @@ -176,7 +177,7 @@ def _remove_ansi_from_string(self, string: str) -> str:

return clean_string

def _find_repo_from_id(self, hook_id: str, config: PreCommitConfig):
def _find_repo_from_id(self, hook_id: str, config: PreCommitSettings):
"""
Retrieves the repo URL that a hook ID belongs to and returns it
:param linter_id: The hook id we want to retrieve the repo url for
Expand All @@ -186,7 +187,7 @@ def _find_repo_from_id(self, hook_id: str, config: PreCommitConfig):

for repo in config.repos:
hooks = repo.hooks
repo_str = repo.repo
repo_str = repo.url

for hook in hooks:
if hook.id == hook_id:
Expand Down
2 changes: 1 addition & 1 deletion secureli/services/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def update_hooks(
if update_result.successful and not output:
output = "No changes necessary.\n"

if update_result.successful and update_result.output:
if update_result.successful and output:
prune_result = self.pre_commit.remove_unused_hooks(folder_path)
output = output + "\nRemoving unused environments:\n" + prune_result.output

Expand Down
1 change: 1 addition & 0 deletions tests/abstractions/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def settings_dict() -> dict:
repos=[
PreCommitRepo(
url="http://example-repo.com/",
rev="master",
hooks=[
PreCommitHook(
id="hook-id",
Expand Down

0 comments on commit b6e6d59

Please sign in to comment.