Skip to content

Commit d5f5806

Browse files
Merge pull request #37 from Kpler/feat/add-support-for-service-name-validatiaon
feat: add support for service name validation for gitops pre-commit
2 parents 0a640b6 + 55a8e06 commit d5f5806

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

kp_pre_commit_hooks/gitops-values-validation.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ def header_schema_version(self) -> Optional[str]:
158158
def set_header_schema_version(self, version):
159159
if self.header_schema_version == version:
160160
return
161-
header = f"# yaml-language-server: $schema={SCHEMA_BASE_URL}/v{version}/schema-platform-managed-chart.json\n"
161+
header = f"# yaml-language-server: $schema={SCHEMA_BASE_URL}/v{version}/schema-platform-managed-chart.json"
162162
content = self.path.read_text()
163163
if self.header_schema_version is None:
164-
self.path.write_text(header + content)
164+
self.path.write_text(header + "\n" + content)
165165
else:
166166
self.path.write_text(SCHEMA_HEADER_REGEXP.sub(header, content))
167167

@@ -207,12 +207,30 @@ def sync_values_files_schema_header_version(self):
207207
class ServiceInstanceConfigValidator:
208208

209209
IGNORED_VALIDATION_ERRORS = {
210-
# These 2 service names are longer than the maximum allowed (36 characters)
210+
# These below project have service names are longer than the maximum allowed (36 characters)
211+
# or have application name not prefixed with the service name
211212
# but we ignore these errors as these services were created before the rule was in place
212-
"$.platform-managed-chart.serviceName": [
213-
"earth-observation-product-catalog-api",
214-
"stream-merge-and-apply-matches-export-bol",
215-
]
213+
"stream-merge-and-apply-matches-import-bol": {
214+
"$.platform-managed-chart.serviceName": [
215+
"'stream-merge-and-apply-matches-import-bol' is too long, the maximum length is 36"
216+
]
217+
},
218+
"stream-merge-and-apply-matches-export-bol": {
219+
"$.platform-managed-chart.serviceName": [
220+
"'stream-merge-and-apply-matches-export-bol' is too long, the maximum length is 36"
221+
]
222+
},
223+
"earth-observation-product-catalog-api": {
224+
"$.platform-managed-chart.serviceName": [
225+
"'earth-observation-product-catalog-api' is too long, the maximum length is 36"
226+
]
227+
},
228+
"apply-edits-exportbol-jdbc": {
229+
"$.platform-managed-chart.serviceName": ["'sink' does not match the service folder name 'apply-edits-exportbol-jdbc'"]
230+
},
231+
"apply-edits-importbol-jdbc": {
232+
"$.platform-managed-chart.serviceName": ["'sink' does not match the service folder name 'apply-edits-exportbol-jdbc'"]
233+
},
216234
}
217235

218236
def __init__(self, service_instance_config: ServiceInstanceConfig):
@@ -227,11 +245,11 @@ def validator(self) -> Validator:
227245

228246
def validate_configuration(self) -> Sequence[Union[ValidationError, SchemaValidationError]]:
229247
try:
230-
validation_errors = list(
231-
error
232-
for error in self.validator.iter_errors((self.service_instance_config.configuration))
233-
if not self.is_ignored_error(error)
234-
)
248+
raw_validation_errors = [
249+
self.enrich_error_message(error)
250+
for error in self.validator.iter_errors(self.service_instance_config.configuration)
251+
]
252+
validation_errors = [error for error in raw_validation_errors if not self.is_ignored_error(error)]
235253
schema_validation_errors = list(self.iter_schema_validation_errors())
236254
return validation_errors + schema_validation_errors
237255

@@ -254,14 +272,24 @@ def iter_schema_validation_errors(self) -> Iterator[SchemaValidationError]:
254272
hint="This pre-commit hook will auto-fix this issue. Please commit the values files changes.",
255273
)
256274

257-
def is_ignored_error(self, error: ValidationError):
258-
return error.instance in self.IGNORED_VALIDATION_ERRORS.get(error.json_path, [])
275+
def enrich_error_message(self, error: ValidationError) -> ValidationError:
276+
if error.message.endswith("is too long") and error.schema.get("maxLength"):
277+
error.message += f', the maximum length is {error.schema["maxLength"]}'
278+
return error
279+
280+
def is_ignored_error(self, error: ValidationError) -> bool:
281+
ignored_errors_for_service = self.IGNORED_VALIDATION_ERRORS.get(self.service_instance_config.service_name, {})
282+
return error.message in ignored_errors_for_service.get(error.json_path, [])
259283

260284
def validate_additional_checks(self, validator, additional_checks, value, schema):
261285
for check in additional_checks:
262286
if check_method := getattr(self, f"validate_{camel_to_snake(check)}", None):
263287
yield from check_method(value, schema)
264288

289+
def validate_service_name_matches_service_folder(self, value, schema):
290+
if service_instance_config.path.name != value:
291+
yield ValidationError(f"'{value}' does not match the service folder name '{service_instance_config.path.name}'")
292+
265293
def validate_topic_name_compliance(self, value, schema):
266294
match = TOPIC_NAME_REGEXP.match(str(value))
267295
service_name = self.service_instance_config.service_name

0 commit comments

Comments
 (0)