henry3260 commented on code in PR #71753: URL: https://github.com/apache/airflow/pull/71753#discussion_r4035671402
########## scripts/ci/prek/update_go_sdk_readme_matrix.py: ########## @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# /// script +# requires-python = ">=3.10,<3.11" +# dependencies = ["PyYAML>=6.0", "rich>=13.6.0"] +# /// +"""Regenerate the Go SDK compatibility table in ``go-sdk/README.md``. + +Renders the Markdown matrix from ``go-sdk/capabilities.yaml`` between the AUTO-GENERATED markers. +Exits non-zero when the file changed so the contributor re-stages it. The hook also verifies that +the manifest's supervisor schema version matches the Go runtime constant embedded in bundle metadata. +""" + +from __future__ import annotations + +import re +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) + +from common_prek_utils import console, insert_documentation +from lang_sdk_compat_matrix import ( + AIRFLOW_ROOT_PATH, + LANG_SDKS, + README_MATRIX_FOOTER, + README_MATRIX_HEADER, + CapabilitiesDoc, + load_capabilities, + render_markdown_table, +) + +SDK_ID = "go" +GO_MESSAGES = AIRFLOW_ROOT_PATH / "go-sdk" / "pkg" / "execution" / "messages.go" +SCHEMA_VERSION_PATTERN = re.compile( + r'^\s*(?:const\s+)?SupervisorSchemaVersion(?:\s+[A-Za-z_]\w*(?:\.\w+)*)?\s*=\s*"(?P<version>[^"]+)"' +) + + +def read_go_schema_version() -> str | None: + """Read ``SupervisorSchemaVersion`` from the Go runtime source.""" + for line in GO_MESSAGES.read_text().splitlines(): + if match := SCHEMA_VERSION_PATTERN.match(line): + return match.group("version") + return None + + +def check_schema_version(doc: CapabilitiesDoc) -> bool: + """Whether the manifest agrees with the schema version embedded in Go bundle metadata.""" + try: + go_version = read_go_schema_version() + except OSError as error: + console.print(f"[red]Could not read {GO_MESSAGES}: {error}[/]") + return False Review Comment: `read_go_schema_version()` returns `None` when the constant is gone or its declaration stops matching `SCHEMA_VERSION_PATTERN`, and that falls through to the mismatch branch so a renamed constant reports `SupervisorSchemaVersion is None` and tells the users to `Update capabilities.yaml to match`, which cannot fix it. The TypeScript sibling hook already separates the two cases sth like: ```suggestion try: go_version = read_go_schema_version() except OSError as error: console.print(f"[red]Could not read {GO_MESSAGES}: {error}[/]") return False if go_version is None: console.print( "[red]Could not read SupervisorSchemaVersion from go-sdk/pkg/execution/messages.go: " "the constant is missing, or its declaration no longer matches this hook's pattern. " "Restore the constant, or update SCHEMA_VERSION_PATTERN if the declaration changed. " "Editing go-sdk/capabilities.yaml cannot fix this.[/]" ) return False ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
