Skip to content

Commit ce1d1e2

Browse files
committed
add override hooks file
1 parent 185afb6 commit ce1d1e2

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

.github/scripts/generate_pre_commit.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Generate pre-commit hooks for Java and Python projects.
22
33
This script scans a repository for Java and Python projects and generates appropriate
4-
pre-commit hooks for linting and formatting.
4+
pre-commit hooks for linting and formatting. It also merges in additional hooks from
5+
an override file.
56
"""
67

78
import os
@@ -138,8 +139,9 @@ def _get_relative_path(self, path: Path) -> str:
138139
class HookGenerator:
139140
"""Generate pre-commit hooks for projects."""
140141

141-
def __init__(self, projects: list[Project]):
142+
def __init__(self, projects: list[Project], override_file: str = None):
142143
self.projects = projects
144+
self.override_file = override_file
143145

144146
def generate_config(self) -> dict:
145147
"""Generate the complete pre-commit config."""
@@ -151,7 +153,32 @@ def generate_config(self) -> dict:
151153
else: # ProjectType.JAVA
152154
hooks.append(self._generate_spotless_hook(project))
153155

154-
return {"repos": [{"repo": "local", "hooks": hooks}]}
156+
config = {"repos": [{"repo": "local", "hooks": hooks}]}
157+
158+
# Merge override hooks if they exist
159+
if self.override_file and os.path.exists(self.override_file):
160+
try:
161+
with open(self.override_file, 'r') as f:
162+
override_config = yaml.safe_load(f)
163+
164+
if override_config and 'repos' in override_config:
165+
for override_repo in override_config['repos']:
166+
matching_repo = next(
167+
(repo for repo in config['repos']
168+
if repo['repo'] == override_repo['repo']),
169+
None
170+
)
171+
172+
if matching_repo:
173+
matching_repo['hooks'].extend(override_repo.get('hooks', []))
174+
else:
175+
config['repos'].append(override_repo)
176+
177+
print(f"Merged additional hooks from {self.override_file}")
178+
except Exception as e:
179+
print(f"Warning: Error reading override file {self.override_file}: {e}")
180+
181+
return config
155182

156183
def _generate_lint_fix_hook(self, project: Project) -> dict:
157184
"""Generate a lint-fix hook for Python projects."""
@@ -208,6 +235,7 @@ def write_yaml_with_spaces(file_path: str, data: dict):
208235

209236
def main():
210237
root_dir = os.path.abspath(os.curdir)
238+
override_file = ".github/scripts/pre-commit-override.yaml"
211239

212240
# Find projects
213241
finder = ProjectFinder(root_dir)
@@ -226,12 +254,12 @@ def main():
226254
print(f" - {project.path}")
227255

228256
# Generate and write config
229-
generator = HookGenerator(projects)
257+
generator = HookGenerator(projects, override_file)
230258
config = generator.generate_config()
231259
write_yaml_with_spaces(".pre-commit-config.yaml", config)
232260

233261
print("\nGenerated .pre-commit-config.yaml")
234262

235263

236264
if __name__ == "__main__":
237-
main()
265+
main()
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: smoke-test-cypress-lint-fix
5+
name: smoke-test cypress Lint Fix
6+
entry: ./gradlew :smoke-test:cypressLintFix
7+
language: system
8+
files: ^smoke-test/tests/cypress/.*$

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,9 @@ repos:
372372
entry: ./gradlew :test-models:spotlessApply
373373
language: system
374374
files: ^test-models/.*\.java$
375+
376+
- id: smoke-test-cypress-lint-fix
377+
name: smoke-test cypress Lint Fix
378+
entry: ./gradlew :smoke-test:cypressLintFix
379+
language: system
380+
files: ^smoke-test/tests/cypress/.*$

0 commit comments

Comments
 (0)