|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright © 2016-2025 The Thingsboard Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import sys |
| 19 | +import re |
| 20 | +import os |
| 21 | +import xml.etree.ElementTree as ET |
| 22 | + |
| 23 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 24 | + |
| 25 | +def find_upwards(filename, start_dir=SCRIPT_DIR, max_levels=10): |
| 26 | + dirp = os.path.abspath(start_dir) |
| 27 | + for _ in range(max_levels): |
| 28 | + candidate = os.path.join(dirp, filename) |
| 29 | + if os.path.exists(candidate): |
| 30 | + return candidate |
| 31 | + parent = os.path.dirname(dirp) |
| 32 | + if parent == dirp: |
| 33 | + break |
| 34 | + dirp = parent |
| 35 | + return None |
| 36 | + |
| 37 | +def find_in_tree(root, filename): |
| 38 | + for r, dirs, files in os.walk(root): |
| 39 | + if filename in files: |
| 40 | + return os.path.join(r, filename) |
| 41 | + return None |
| 42 | + |
| 43 | +def get_version_from_pom(pom_path): |
| 44 | + tree = ET.parse(pom_path) |
| 45 | + root = tree.getroot() |
| 46 | + ns = {"m": "http://maven.apache.org/POM/4.0.0"} |
| 47 | + version_tag = root.find("m:version", ns) |
| 48 | + if version_tag is None or not version_tag.text: |
| 49 | + version_tag = root.find("version") |
| 50 | + if version_tag is None or not version_tag.text: |
| 51 | + raise ValueError("Version tag not found in pom.xml") |
| 52 | + version = version_tag.text.strip() |
| 53 | + version = re.sub(r"[-A-Z]+$", "", version) |
| 54 | + return version |
| 55 | + |
| 56 | +def maven_to_enum(version_str): |
| 57 | + return "V_" + version_str.replace(".", "_") |
| 58 | + |
| 59 | +def extract_versions(proto_path): |
| 60 | + versions = [] |
| 61 | + with open(proto_path, "r", encoding="utf-8") as f: |
| 62 | + inside_enum = False |
| 63 | + for line in f: |
| 64 | + s = line.strip() |
| 65 | + if s.startswith("enum EdgeVersion"): |
| 66 | + inside_enum = True |
| 67 | + elif inside_enum and s.startswith("}"): |
| 68 | + inside_enum = False |
| 69 | + elif inside_enum: |
| 70 | + m = re.match(r"(\w+)\s*=\s*\d+;", s) |
| 71 | + if m: |
| 72 | + versions.append(m.group(1)) |
| 73 | + return versions |
| 74 | + |
| 75 | +def main(): |
| 76 | + pom_path = None |
| 77 | + |
| 78 | + gh_workspace = os.environ.get("GITHUB_WORKSPACE") |
| 79 | + if gh_workspace: |
| 80 | + pom_path = find_in_tree(gh_workspace, "pom.xml") |
| 81 | + if not pom_path: |
| 82 | + candidate = os.path.join(gh_workspace, "pom.xml") |
| 83 | + if os.path.exists(candidate): |
| 84 | + pom_path = candidate |
| 85 | + |
| 86 | + if not pom_path: |
| 87 | + pom_path = find_upwards("pom.xml", start_dir=SCRIPT_DIR, max_levels=10) |
| 88 | + |
| 89 | + if not pom_path: |
| 90 | + print("::error::pom.xml not found (searched GITHUB_WORKSPACE and ancestors).") |
| 91 | + sys.exit(1) |
| 92 | + |
| 93 | + repo_root = os.path.dirname(pom_path) |
| 94 | + |
| 95 | + proto_path = find_in_tree(repo_root, "edge.proto") |
| 96 | + if not proto_path: |
| 97 | + print(f"::error::edge.proto not found under repository root: {repo_root}") |
| 98 | + sys.exit(1) |
| 99 | + |
| 100 | + try: |
| 101 | + maven_version = get_version_from_pom(pom_path) |
| 102 | + except Exception as e: |
| 103 | + print(f"::error::Failed to parse pom.xml: {e}") |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + enum_version = maven_to_enum(maven_version) |
| 107 | + |
| 108 | + versions = extract_versions(proto_path) |
| 109 | + if enum_version not in versions: |
| 110 | + print(f"::warning::Latest version {enum_version} is NOT present in {proto_path}") |
| 111 | + sys.exit(1) |
| 112 | + else: |
| 113 | + print(f"::notice::Latest version {enum_version} is present in {proto_path}") |
| 114 | + sys.exit(0) |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments