Skip to content

Commit fad8204

Browse files
Merge pull request #192 from jekka001/check-edge-proto-version
Check Edge Proto Version
2 parents 9d06d6a + b98b28f commit fad8204

3 files changed

Lines changed: 260 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Copyright © 2016-2025 The Thingsboard Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
name: Check Edge Proto Version
18+
19+
on:
20+
pull_request:
21+
branches:
22+
- master
23+
- rc
24+
- develop
25+
26+
jobs:
27+
check-proto:
28+
runs-on: ubuntu-latest
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@v3
32+
33+
- name: Set up Python
34+
uses: actions/setup-python@v4
35+
with:
36+
python-version: '3.x'
37+
38+
- name: Run edge.proto version check
39+
run: python3 check_edge_version.py

add_new_edge_version.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#
2+
# Copyright © 2016-2025 The Thingsboard Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import sys
18+
import re
19+
import os
20+
import xml.etree.ElementTree as ET
21+
22+
def find_file(filename, start_dir="."):
23+
for root, dirs, files in os.walk(start_dir):
24+
if filename in files:
25+
return os.path.join(root, filename)
26+
return None
27+
28+
def get_version_from_pom(pom_path):
29+
tree = ET.parse(pom_path)
30+
root = tree.getroot()
31+
ns = {"m": "http://maven.apache.org/POM/4.0.0"}
32+
version_tag = root.find("m:version", ns)
33+
if version_tag is None or not version_tag.text:
34+
raise ValueError("Version tag not found in pom.xml")
35+
version = version_tag.text.strip()
36+
version = re.sub(r"[-A-Z]+$", "", version)
37+
return version
38+
39+
def maven_to_enum(version_str):
40+
return "V_" + version_str.replace(".", "_")
41+
42+
def add_version_to_proto(proto_path, new_version):
43+
with open(proto_path, "r") as f:
44+
lines = f.readlines()
45+
46+
version_numbers = []
47+
for line in lines:
48+
if "V_LATEST" in line:
49+
continue
50+
m = re.match(r"\s*\w+\s*=\s*(\d+);", line)
51+
if m:
52+
version_numbers.append(int(m.group(1)))
53+
54+
if version_numbers:
55+
next_value = max(version_numbers) + 1
56+
else:
57+
next_value = 0
58+
59+
v_latest_idx = None
60+
for idx, line in enumerate(lines):
61+
if "V_LATEST" in line:
62+
v_latest_idx = idx
63+
break
64+
if v_latest_idx is None:
65+
raise ValueError("V_LATEST not found in proto")
66+
67+
insert_idx = v_latest_idx
68+
while insert_idx > 0 and lines[insert_idx - 1].strip() == "":
69+
insert_idx -= 1
70+
71+
indent = " "
72+
if insert_idx - 1 >= 0:
73+
prev_line = lines[insert_idx - 1]
74+
m_indent = re.match(r"^(\s*)\S", prev_line)
75+
if m_indent:
76+
indent = m_indent.group(1)
77+
78+
new_line = f"{indent}{new_version} = {next_value};\n"
79+
80+
new_lines = lines[:insert_idx] + [new_line] + lines[insert_idx:]
81+
82+
with open(proto_path, "w") as f:
83+
f.writelines(new_lines)
84+
85+
print(f"INFO: Added {new_version} = {next_value} to {proto_path}")
86+
87+
def main():
88+
pom_path = find_file("pom.xml")
89+
if not pom_path:
90+
print("ERROR: pom.xml not found")
91+
sys.exit(1)
92+
93+
proto_path = find_file("edge.proto")
94+
if not proto_path:
95+
print("ERROR: edge.proto not found")
96+
sys.exit(1)
97+
98+
maven_version = get_version_from_pom(pom_path)
99+
enum_version = maven_to_enum(maven_version)
100+
101+
add_version_to_proto(proto_path, enum_version)
102+
103+
if __name__ == "__main__":
104+
main()

0 commit comments

Comments
 (0)